Code example for Visualizer

Methods: getSamplingRate, release

0
        getVisualizer(0); 
        try { 
            int captureRate = mVisualizer.getMaxCaptureRate();
            assertTrue(msg +": insufficient max capture rate",
                    captureRate >= MIN_CAPTURE_RATE_MAX);
            int samplingRate = mVisualizer.getSamplingRate();
            assertTrue(msg +": invalid sampling rate",
                    samplingRate >= MIN_SAMPLING_RATE && samplingRate <= MAX_SAMPLING_RATE);
            result = true;
        } catch (IllegalArgumentException e) {
            msg = msg.concat(": Bad parameter value");
            loge(msg, "Bad parameter value");
        } catch (UnsupportedOperationException e) {
            msg = msg.concat(": get parameter() rejected");
            loge(msg, "get parameter() rejected");
        } catch (IllegalStateException e) {
            msg = msg.concat("get parameter() called in wrong state");
            loge(msg, "get parameter() called in wrong state");
        } finally { 
            releaseVisualizer(); 
        } 
        assertTrue(msg, result);
    } 
 
    //Test case 1.1: check capture size 
    @LargeTest 
    public void test1_1CaptureSize() throws Exception { 
        boolean result = false;
        String msg = "test1_1CaptureSize()";
        getVisualizer(0); 
        try { 
            int[] range = mVisualizer.getCaptureSizeRange();
            assertTrue(msg +": insufficient min capture size",
                    range[0] <= MAX_CAPTURE_SIZE_MIN);
            assertTrue(msg +": insufficient min capture size",
                    range[1] >= MIN_CAPTURE_SIZE_MAX);
            mVisualizer.setCaptureSize(range[0]);
            assertEquals(msg +": insufficient min capture size",
                    range[0], mVisualizer.getCaptureSize());
            mVisualizer.setCaptureSize(range[1]);
            assertEquals(msg +": insufficient min capture size",
                    range[1], mVisualizer.getCaptureSize());
            result = true;
        } catch (IllegalArgumentException e) {
            msg = msg.concat(": Bad parameter value");
            loge(msg, "Bad parameter value");
        } catch (UnsupportedOperationException e) {
            msg = msg.concat(": get parameter() rejected");
            loge(msg, "get parameter() rejected");
        } catch (IllegalStateException e) {
            msg = msg.concat("get parameter() called in wrong state");
            loge(msg, "get parameter() called in wrong state");
        } finally { 
            releaseVisualizer(); 
        } 
        assertTrue(msg, result);
    } 
 
    //Test case 1.2: check scaling mode 
    @LargeTest 
    public void test1_2ScalingMode() throws Exception { 
        boolean result = false;
        String msg = "test1_2ScalingMode()";
        getVisualizer(0); 
        try { 
            int res = mVisualizer.setScalingMode(Visualizer.SCALING_MODE_AS_PLAYED);
            assertEquals(msg + ": setting SCALING_MODE_AS_PLAYED failed",
                    res, Visualizer.SUCCESS);
            int mode = mVisualizer.getScalingMode();
            assertEquals(msg + ": setting SCALING_MODE_AS_PLAYED didn't stick",
                    mode, Visualizer.SCALING_MODE_AS_PLAYED);
 
            res = mVisualizer.setScalingMode(Visualizer.SCALING_MODE_NORMALIZED);
            assertEquals(msg + ": setting SCALING_MODE_NORMALIZED failed",
                    res, Visualizer.SUCCESS);
            mode = mVisualizer.getScalingMode();
            assertEquals(msg + ": setting SCALING_MODE_NORMALIZED didn't stick",
                    mode, Visualizer.SCALING_MODE_NORMALIZED);
 
            result = true;
        } catch (IllegalStateException e) {
            msg = msg.concat("IllegalStateException");
            loge(msg, "set/get parameter() called in wrong state: " + e);
        } finally { 
            releaseVisualizer(); 
        } 
        assertTrue(msg, result);
    } 
 
    //----------------------------------------------------------------- 
    // 2 - check capture 
    //---------------------------------- 
 
    //Test case 2.0: test capture in polling mode 
    @LargeTest 
    public void test2_0PollingCapture() throws Exception { 
        boolean result = false;
        String msg = "test2_0PollingCapture()";
        AudioEffect vc = null;
        MediaPlayer mp = null;
        AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = am.getRingerMode();
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        int volume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        am.setStreamVolume(AudioManager.STREAM_MUSIC,
                           am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                           0); 
 
        try { 
            // creating a volume controller on output mix ensures that ro.audio.silent mutes 
            // audio after the effects and not before 
            vc = new AudioEffect(
                                AudioEffect.EFFECT_TYPE_NULL, 
                                VOLUME_EFFECT_UUID,
                                0, 
                                0); 
            vc.setEnabled(true);
 
            mp = new MediaPlayer();
            mp.setDataSource(MediaNames.SINE_200_1000);
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            getVisualizer(mp.getAudioSessionId());
            mVisualizer.setEnabled(true);
            // check capture on silence 
            byte[] data = new byte[mVisualizer.getCaptureSize()];
            mVisualizer.getWaveForm(data);
            int energy = computeEnergy(data, true);
            assertEquals(msg +": getWaveForm reports energy for silence",
                    0, energy);
            mVisualizer.getFft(data);
            energy = computeEnergy(data, false);
            assertEquals(msg +": getFft reports energy for silence",
                    0, energy);
            mp.prepare();
            mp.start();
            Thread.sleep(500);
            // check capture on sound 
            mVisualizer.getWaveForm(data);
            energy = computeEnergy(data, true);
            assertTrue(msg +": getWaveForm reads insufficient level",
                    energy > 0);
            mVisualizer.getFft(data);
            energy = computeEnergy(data, false);
            assertTrue(msg +": getFft reads insufficient level",
                    energy > 0);
            result = true;
        } catch (IllegalArgumentException e) {
            msg = msg.concat(": Bad parameter value");
            loge(msg, "Bad parameter value");
        } catch (UnsupportedOperationException e) {
            msg = msg.concat(": get parameter() rejected");
            loge(msg, "get parameter() rejected");
        } catch (IllegalStateException e) {
            msg = msg.concat("get parameter() called in wrong state");
            loge(msg, "get parameter() called in wrong state");
        } catch (InterruptedException e) {
            loge(msg, "sleep() interrupted");
        } 
        finally { 
            releaseVisualizer(); 
            if (mp != null) {
                mp.release();
            } 
            if (vc != null) {
                vc.release();
            } 
            am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
            am.setRingerMode(ringerMode);
        } 
        assertTrue(msg, result);
    } 
 
    //Test case 2.1: test capture with listener 
    @LargeTest 
    public void test2_1ListenerCapture() throws Exception { 
        boolean result = false;
        String msg = "test2_1ListenerCapture()";
        AudioEffect vc = null;
        MediaPlayer mp = null;
        AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = am.getRingerMode();
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        int volume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        am.setStreamVolume(AudioManager.STREAM_MUSIC,
                           am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                           0); 
 
        try { 
            // creating a volume controller on output mix ensures that ro.audio.silent mutes 
            // audio after the effects and not before 
            vc = new AudioEffect(
                                AudioEffect.EFFECT_TYPE_NULL, 
                                VOLUME_EFFECT_UUID,
                                0, 
                                0); 
            vc.setEnabled(true);
 
            mp = new MediaPlayer();
            mp.setDataSource(MediaNames.SINE_200_1000);
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
 
            getVisualizer(mp.getAudioSessionId());
            createListenerLooper(); 
            synchronized(lock) {
                try { 
                    lock.wait(1000);
                } catch(Exception e) {
                    Log.e(TAG, "Looper creation: wait was interrupted.");
                } 
            } 
            assertTrue(mInitialized);
 
            mVisualizer.setEnabled(true);
 
            // check capture on silence 
            synchronized(lock) {
                try { 
                    mCaptureWaveform = true;
                    lock.wait(1000);
                    mCaptureWaveform = false;
                } catch(Exception e) {
                    Log.e(TAG, "Capture waveform: wait was interrupted.");
                } 
            } 
            assertNotNull(msg +": waveform capture failed", mWaveform);
            int energy = computeEnergy(mWaveform, true);
            assertEquals(msg +": getWaveForm reports energy for silence",
                    0, energy);
 
            synchronized(lock) {
                try { 
                    mCaptureFft = true;
                    lock.wait(1000);
                    mCaptureFft = false;
                } catch(Exception e) {
                    Log.e(TAG, "Capture FFT: wait was interrupted.");
                } 
            } 
            assertNotNull(msg +": FFT capture failed", mFft);
            energy = computeEnergy(mFft, false);
            assertEquals(msg +": getFft reports energy for silence",
                    0, energy);
 
            mp.prepare();
            mp.start();
            Thread.sleep(500);
 
            // check capture on sound 
            synchronized(lock) {
                try { 
                    mCaptureWaveform = true;
                    lock.wait(1000);
                    mCaptureWaveform = false;
                } catch(Exception e) {
                    Log.e(TAG, "Capture waveform: wait was interrupted.");
                } 
            } 
            assertNotNull(msg +": waveform capture failed", mWaveform);
            energy = computeEnergy(mWaveform, true);
            assertTrue(msg +": getWaveForm reads insufficient level",
                    energy > 0);
 
            synchronized(lock) {
                try { 
                    mCaptureFft = true;
                    lock.wait(1000);
                    mCaptureFft = false;
                } catch(Exception e) {
                    Log.e(TAG, "Capture FFT: wait was interrupted.");
                } 
            } 
            assertNotNull(msg +": FFT capture failed", mFft);
            energy = computeEnergy(mFft, false);
            assertTrue(msg +": getFft reads insufficient level",
                    energy > 0);
 
            result = true;
        } catch (IllegalArgumentException e) {
            msg = msg.concat(": Bad parameter value");
            loge(msg, "Bad parameter value");
        } catch (UnsupportedOperationException e) {
            msg = msg.concat(": get parameter() rejected");
            loge(msg, "get parameter() rejected");
        } catch (IllegalStateException e) {
            msg = msg.concat("get parameter() called in wrong state");
            loge(msg, "get parameter() called in wrong state");
        } catch (InterruptedException e) {
            loge(msg, "sleep() interrupted");
        } 
        finally { 
            terminateListenerLooper(); 
            releaseVisualizer(); 
            if (mp != null) {
                mp.release();
            } 
            if (vc != null) {
                vc.release();
            } 
            am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
            am.setRingerMode(ringerMode);
        } 
        assertTrue(msg, result);
    } 
 
    //Test case 2.2: test capture in polling mode with volume scaling 
    @LargeTest 
    public void test2_2PollingCaptureVolumeScaling() throws Exception { 
        // test that when playing a sound, the energy measured with Visualizer in 
        //   SCALING_MODE_AS_PLAYED mode decreases when lowering the volume 
        boolean result = false;
        String msg = "test2_2PollingCaptureVolumeScaling()";
        AudioEffect vc = null;
        MediaPlayer mp = null;
        AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = am.getRingerMode();