Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.media.cts;
     18 
     19 import android.media.cts.R;
     20 
     21 import android.content.pm.PackageManager;
     22 import android.media.AudioFormat;
     23 import android.media.AudioRecord;
     24 import android.media.audiofx.AcousticEchoCanceler;
     25 import android.media.audiofx.AutomaticGainControl;
     26 import android.media.audiofx.NoiseSuppressor;
     27 import android.media.MediaRecorder;
     28 import android.test.AndroidTestCase;
     29 
     30 
     31 public class AudioPreProcessingTest extends AndroidTestCase {
     32 
     33     private String TAG = "AudioPreProcessingTest";
     34     // AudioRecord sampling rate
     35     private final static int SAMPLING_RATE = 8000;
     36 
     37     //-----------------------------------------------------------------
     38     // AUDIO PRE PROCESSING TESTS:
     39     //----------------------------------
     40 
     41     //-----------------------------------------------------------------
     42     // 1 - Noise Suppressor
     43     //----------------------------------
     44 
     45     //-----------------------------------------------------------------
     46     // 1.1 - creation
     47     //----------------------------------
     48 
     49     //Test case 1.1: test NS creation and release
     50     public void test1_1NsCreateAndRelease() throws Exception {
     51         if (!hasMicrophone()) {
     52             return;
     53         }
     54 
     55         AudioRecord ar = getAudioRecord();
     56         assertNotNull("could not create AudioRecord", ar);
     57 
     58         boolean isAvailable = NoiseSuppressor.isAvailable();
     59 
     60         NoiseSuppressor ns = NoiseSuppressor.create(ar.getAudioSessionId());
     61         assertTrue("NS not available but created or available and not created",
     62                 isAvailable == (ns != null));
     63         if (ns != null) {
     64             ns.release();
     65         }
     66         ar.release();
     67     }
     68 
     69     //-----------------------------------------------------------------
     70     // 1.2 - NS Enable/disable
     71     //----------------------------------
     72 
     73     //Test case 1.2: test setEnabled() and getEnabled()
     74     public void test1_2NsSetEnabledGetEnabled() throws Exception {
     75         if (!hasMicrophone()) {
     76             return;
     77         }
     78 
     79         if (!NoiseSuppressor.isAvailable()) {
     80             return;
     81         }
     82 
     83         AudioRecord ar = getAudioRecord();
     84         assertNotNull("could not create AudioRecord", ar);
     85 
     86         NoiseSuppressor ns = NoiseSuppressor.create(ar.getAudioSessionId());
     87         assertNotNull("could not create NoiseSupressor", ns);
     88         try {
     89             ns.setEnabled(true);
     90             assertTrue("invalid state from getEnabled", ns.getEnabled());
     91             ns.setEnabled(false);
     92             assertFalse("invalid state to getEnabled", ns.getEnabled());
     93             // test passed
     94         } catch (IllegalStateException e) {
     95             fail("setEnabled() in wrong state");
     96         } finally {
     97             ns.release();
     98             ar.release();
     99         }
    100     }
    101 
    102     //-----------------------------------------------------------------
    103     // 2 - Acoustic Echo Canceller
    104     //----------------------------------
    105 
    106     //-----------------------------------------------------------------
    107     // 2.1 - creation
    108     //----------------------------------
    109 
    110     //Test case 2.1: test AEC creation and release
    111     public void test2_1AecCreateAndRelease() throws Exception {
    112         if (!hasMicrophone()) {
    113             return;
    114         }
    115 
    116         AudioRecord ar = getAudioRecord();
    117         assertNotNull("could not create AudioRecord", ar);
    118 
    119         boolean isAvailable = AcousticEchoCanceler.isAvailable();
    120 
    121         AcousticEchoCanceler aec = AcousticEchoCanceler.create(ar.getAudioSessionId());
    122         assertTrue("AEC not available but created or available and not created",
    123                 isAvailable == (aec != null));
    124         if (aec != null) {
    125             aec.release();
    126         }
    127         ar.release();
    128     }
    129 
    130     //-----------------------------------------------------------------
    131     // 2.2 - AEC Enable/disable
    132     //----------------------------------
    133 
    134     //Test case 2.2: test AEC setEnabled() and getEnabled()
    135     public void test2_2AecSetEnabledGetEnabled() throws Exception {
    136         if (!hasMicrophone()) {
    137             return;
    138         }
    139 
    140         if (!AcousticEchoCanceler.isAvailable()) {
    141             return;
    142         }
    143 
    144         AudioRecord ar = getAudioRecord();
    145         assertNotNull("could not create AudioRecord", ar);
    146 
    147         AcousticEchoCanceler aec = AcousticEchoCanceler.create(ar.getAudioSessionId());
    148         assertNotNull("could not create AcousticEchoCanceler", aec);
    149         try {
    150             aec.setEnabled(true);
    151             assertTrue("invalid state from getEnabled", aec.getEnabled());
    152             aec.setEnabled(false);
    153             assertFalse("invalid state to getEnabled", aec.getEnabled());
    154             // test passed
    155         } catch (IllegalStateException e) {
    156             fail("setEnabled() in wrong state");
    157         } finally {
    158             aec.release();
    159             ar.release();
    160         }
    161     }
    162 
    163     //-----------------------------------------------------------------
    164     // 3 - Automatic Gain Control
    165     //----------------------------------
    166 
    167     //-----------------------------------------------------------------
    168     // 3.1 - creation
    169     //----------------------------------
    170 
    171     //Test case 3.1: test AGC creation and release
    172     public void test3_1AgcCreateAndRelease() throws Exception {
    173         if (!hasMicrophone()) {
    174             return;
    175         }
    176 
    177         AudioRecord ar = getAudioRecord();
    178         assertNotNull("could not create AudioRecord", ar);
    179 
    180         boolean isAvailable = AutomaticGainControl.isAvailable();
    181 
    182         AutomaticGainControl agc = AutomaticGainControl.create(ar.getAudioSessionId());
    183         assertTrue("AGC not available but created or available and not created",
    184                 isAvailable == (agc != null));
    185         if (agc != null) {
    186             agc.release();
    187         }
    188         ar.release();
    189     }
    190 
    191     //-----------------------------------------------------------------
    192     // 3.2 - AEC Enable/disable
    193     //----------------------------------
    194 
    195     //Test case 3.2: test AGC setEnabled() and getEnabled()
    196     public void test3_2AgcSetEnabledGetEnabled() throws Exception {
    197         if (!hasMicrophone()) {
    198             return;
    199         }
    200 
    201         if (!AutomaticGainControl.isAvailable()) {
    202             return;
    203         }
    204 
    205         AudioRecord ar = getAudioRecord();
    206         assertNotNull("could not create AudioRecord", ar);
    207 
    208         AutomaticGainControl agc = AutomaticGainControl.create(ar.getAudioSessionId());
    209         assertNotNull("could not create AutomaticGainControl", agc);
    210         try {
    211             agc.setEnabled(true);
    212             assertTrue("invalid state from getEnabled", agc.getEnabled());
    213             agc.setEnabled(false);
    214             assertFalse("invalid state to getEnabled", agc.getEnabled());
    215             // test passed
    216         } catch (IllegalStateException e) {
    217             fail("setEnabled() in wrong state");
    218         } finally {
    219             agc.release();
    220             ar.release();
    221         }
    222     }
    223 
    224     //-----------------------------------------------------------------
    225     // private methods
    226     //----------------------------------
    227     private boolean hasMicrophone() {
    228         return getContext().getPackageManager().hasSystemFeature(
    229                 PackageManager.FEATURE_MICROPHONE);
    230     }
    231 
    232     private AudioRecord getAudioRecord() {
    233         AudioRecord ar = null;
    234         try {
    235             ar = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
    236                     SAMPLING_RATE,
    237                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
    238                     AudioFormat.ENCODING_PCM_16BIT,
    239                     AudioRecord.getMinBufferSize(SAMPLING_RATE,
    240                             AudioFormat.CHANNEL_CONFIGURATION_MONO,
    241                             AudioFormat.ENCODING_PCM_16BIT) * 10);
    242             assertNotNull("Could not create AudioRecord", ar);
    243             assertEquals("AudioRecord not initialized",
    244                     AudioRecord.STATE_INITIALIZED, ar.getState());
    245         } catch (IllegalArgumentException e) {
    246             fail("AudioRecord invalid parameter");
    247         }
    248         return ar;
    249     }
    250 
    251 }
    252