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);
    } 
 
    //----------------------------------------------------------------- 
    // 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 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, 
                    UUID.fromString("119341a0-8469-11df-81f9-0002a5d5c51b"),
                      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);
        } 
        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 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, 
                    UUID.fromString("119341a0-8469-11df-81f9-0002a5d5c51b"),
                      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);
        } 
        assertTrue(msg, result);
    } 
 
    //----------------------------------------------------------------- 
    // private methods 
    //---------------------------------- 
 
    private int computeEnergy(byte[] data, boolean unsigned) {
        int energy = 0;
        if (data.length != 0) {
            for (int i = 0; i < data.length; i++) {
                int tmp;
                // convert from unsigned 8 bit to signed 16 bit 
                if (unsigned) {
                    tmp = ((int)data[i] & 0xFF) - 128;
                } else { 
                    tmp = (int)data[i];
                } 
                energy += tmp*tmp;
            } 
            energy /= data.length;
        } 
        return energy;
    } 
 
    private void getVisualizer(int session) {
         if (mVisualizer == null || session != mSession) {
             if (session != mSession && mVisualizer != null) {
                 mVisualizer.release();
                 mVisualizer = null;
             } 
             try { 
                mVisualizer = new Visualizer(session);
                mSession = session;
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "getVisualizer() Visualizer not found exception: "+e);
            } catch (UnsupportedOperationException e) {
                Log.e(TAG, "getVisualizer() Effect library not loaded exception: "+e);
            } 
         } 
         assertNotNull("could not create mVisualizer", mVisualizer);
    } 
 
    private void releaseVisualizer() { 
        if (mVisualizer != null) {
            mVisualizer.release();
            mVisualizer = null;
        } 
   } 
 
    private void createListenerLooper() {