FileDocCategorySizeDatePackage
MediaEnvReverbTest.javaAPI DocAndroid 5.1 API21290Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.functional.audio

MediaEnvReverbTest

public class MediaEnvReverbTest extends android.test.ActivityInstrumentationTestCase2
Junit / Instrumentation test case for the media AudioTrack api

Fields Summary
private String
TAG
private static final int
MILLIBEL_TOLERANCE
private static final float
DELAY_TOLERANCE
private static final float
RATIO_TOLERANCE
private static final UUID
VOLUME_EFFECT_UUID
private static final UUID
ENV_REVERB_EFFECT_UUID
private android.media.audiofx.EnvironmentalReverb
mReverb
private int
mSession
Constructors Summary
public MediaEnvReverbTest()


      
        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
    
Methods Summary
private static voidassumeTrue(java.lang.String message, boolean cond)

        assertTrue("(assume)"+message, cond);
    
private voidgetReverb(int session)

         if (mReverb == null || session != mSession) {
             if (session != mSession && mReverb != null) {
                 mReverb.release();
                 mReverb = null;
             }
             try {
                mReverb = new EnvironmentalReverb(0, session);
                mSession = session;
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "getReverb() EnvironmentalReverb not found exception: "+e);
            } catch (UnsupportedOperationException e) {
                Log.e(TAG, "getReverb() Effect library not loaded exception: "+e);
            }
         }
         assertNotNull("could not create mReverb", mReverb);
    
private voidlog(java.lang.String testName, java.lang.String message)

        Log.v(TAG, "["+testName+"] "+message);
    
private voidloge(java.lang.String testName, java.lang.String message)

        Log.e(TAG, "["+testName+"] "+message);
    
private voidreleaseReverb()

        if (mReverb != null) {
            mReverb.release();
            mReverb = null;
        }
   
protected voidsetUp()

      super.setUp();
    
protected voidtearDown()

        super.tearDown();
        releaseReverb();
    
public voidtest0_0ConstructorAndRelease()

        boolean result = false;
        String msg = "test1_0ConstructorAndRelease()";
        EnvironmentalReverb reverb = null;
         try {
            reverb = new EnvironmentalReverb(0, 0);
            assertNotNull(msg + ": could not create EnvironmentalReverb", reverb);
            try {
                assertTrue(msg +": invalid effect ID", (reverb.getId() != 0));
            } catch (IllegalStateException e) {
                msg = msg.concat(": EnvironmentalReverb not initialized");
            }
            result = true;
        } catch (IllegalArgumentException e) {
            msg = msg.concat(": EnvironmentalReverb not found");
        } catch (UnsupportedOperationException e) {
            msg = msg.concat(": Effect library not loaded");
        } finally {
            if (reverb != null) {
                reverb.release();
            }
        }
        assertTrue(msg, result);
    
public voidtest1_0Room()

        boolean result = false;
        String msg = "test1_0Room()";
        getReverb(0);
        try {
            mReverb.setRoomLevel((short)0);
            short level = mReverb.getRoomLevel();
            assertTrue(msg +": got incorrect room level",
                    (level > (0 - MILLIBEL_TOLERANCE)) &&
                    (level < (0 + MILLIBEL_TOLERANCE)));

            mReverb.setRoomHFLevel((short)-6);
            level = mReverb.getRoomHFLevel();
            assertTrue(msg +": got incorrect room HF level",
                    (level > (-6 - MILLIBEL_TOLERANCE)) &&
                    (level < (-6 + MILLIBEL_TOLERANCE)));

            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 {
            releaseReverb();
        }
        assertTrue(msg, result);
    
public voidtest1_1Decay()

        boolean result = false;
        String msg = "test1_1Decay()";
        getReverb(0);
        try {
            mReverb.setDecayTime(500);
            int time = mReverb.getDecayTime();
            assertTrue(msg +": got incorrect decay time",
                    ((float)time > (float)(500 / DELAY_TOLERANCE)) &&
                    ((float)time < (float)(500 * DELAY_TOLERANCE)));

            mReverb.setDecayHFRatio((short)1000);
            short ratio = mReverb.getDecayHFRatio();
            assertTrue(msg +": got incorrect decay HF ratio",
                    ((float)ratio > (float)(1000 / RATIO_TOLERANCE)) &&
                    ((float)ratio < (float)(1000 * RATIO_TOLERANCE)));

            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 {
            releaseReverb();
        }
        assertTrue(msg, result);
    
public voidtest1_2Reflections()

        // TODO: uncomment when early reflections are implemented
//        boolean result = false;
//        String msg = "test1_2Reflections()";
//        getReverb(0);
//        try {
//            mReverb.setReflectionsLevel((short)0);
//            short level = mReverb.getReflectionsLevel();
//            assertTrue(msg +": got incorrect reflections level",
//                    (level > (0 - MILLIBEL_TOLERANCE)) &&
//                    (level < (0 + MILLIBEL_TOLERANCE)));
//
//            mReverb.setReflectionsDelay(30);
//            int delay = mReverb.getReflectionsDelay();
//            assertTrue(msg +": got incorrect reflections delay",
//                    ((float)delay > (float)(30 / DELAY_TOLERANCE)) &&
//                    ((float)delay < (float)(30 * DELAY_TOLERANCE)));
//
//            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 {
//            releaseReverb();
//        }
//        assertTrue(msg, result);
    
public voidtest1_3Reverb()

        boolean result = false;
        String msg = "test1_3Reverb()";
        getReverb(0);
        try {
            mReverb.setReverbLevel((short)0);
            short level = mReverb.getReverbLevel();
            assertTrue(msg +": got incorrect reverb level",
                    (level > (0 - MILLIBEL_TOLERANCE)) &&
                    (level < (0 + MILLIBEL_TOLERANCE)));

            // TODO: change delay when early reflections are implemented
            mReverb.setReverbDelay(0);
            int delay = mReverb.getReverbDelay();
            assertTrue(msg +": got incorrect reverb delay", delay < 5);

            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 {
            releaseReverb();
        }
        assertTrue(msg, result);
    
public voidtest1_4DiffusionAndDensity()

        boolean result = false;
        String msg = "test1_4DiffusionAndDensity()";
        getReverb(0);
        try {
            mReverb.setDiffusion((short)500);
            short diffusion = mReverb.getDiffusion();
            assertTrue(msg +": got incorrect diffusion",
                    ((float)diffusion > (float)(500 / RATIO_TOLERANCE)) &&
                    ((float)diffusion < (float)(500 * RATIO_TOLERANCE)));

            mReverb.setDensity((short)500);
            short density = mReverb.getDensity();
            assertTrue(msg +": got incorrect density",
                    ((float)density > (float)(500 / RATIO_TOLERANCE)) &&
                    ((float)density < (float)(500 * RATIO_TOLERANCE)));

            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 {
            releaseReverb();
        }
        assertTrue(msg, result);
    
public voidtest1_5Properties()

        boolean result = false;
        String msg = "test1_5Properties()";
        getReverb(0);
        try {
            EnvironmentalReverb.Settings settings = mReverb.getProperties();
            short newRoomLevel = 0;
            if (settings.roomLevel == 0) {
                newRoomLevel = -1000;
            }
            String str = settings.toString();
            settings = new EnvironmentalReverb.Settings(str);
            settings.roomLevel = newRoomLevel;
            mReverb.setProperties(settings);
            settings = mReverb.getProperties();
            assertTrue(msg +": setProperties failed",
                    (settings.roomLevel > (newRoomLevel - MILLIBEL_TOLERANCE)) &&
                    (settings.roomLevel < (newRoomLevel + MILLIBEL_TOLERANCE)));
            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 {
            releaseReverb();
        }
        assertTrue(msg, result);
    
public voidtest2_0AuxiliarySoundModification()

        boolean result = false;
        String msg = "test2_0AuxiliarySoundModification()";
        EnergyProbe probe = null;
        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);
        getReverb(0);
        try {
            probe = new EnergyProbe(0);
            // 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);
            mp.attachAuxEffect(mReverb.getId());
            mp.setAuxEffectSendLevel(1.0f);
            mReverb.setRoomLevel((short)0);
            mReverb.setReverbLevel((short)0);
            mReverb.setDecayTime(2000);
            mReverb.setEnabled(true);
            mp.prepare();
            mp.start();
            Thread.sleep(1000);
            mp.stop();
            Thread.sleep(300);
            // measure energy around 1kHz after media player was stopped for 300 ms
            int energy1000 = probe.capture(1000);
            assertTrue(msg + ": reverb has no effect", energy1000 > 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 {
            releaseReverb();
            if (mp != null) {
                mp.release();
            }
            if (vc != null) {
                vc.release();
            }
            if (probe != null) {
                probe.release();
            }
            am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
            am.setRingerMode(ringerMode);
        }
        assertTrue(msg, result);
    
public voidtest2_1InsertSoundModification()

        boolean result = false;
        String msg = "test2_1InsertSoundModification()";
        EnergyProbe probe = null;
        AudioEffect vc = null;
        MediaPlayer mp = null;
        AudioEffect rvb = 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);

            // create reverb with UUID instead of EnvironmentalReverb constructor otherwise an
            // auxiliary reverb will be chosen by the effect framework as we are on session 0
            rvb = new AudioEffect(
                        AudioEffect.EFFECT_TYPE_NULL,
                        ENV_REVERB_EFFECT_UUID,
                        0,
                        0);

            rvb.setParameter(EnvironmentalReverb.PARAM_ROOM_LEVEL, (short)0);
            rvb.setParameter(EnvironmentalReverb.PARAM_REVERB_LEVEL, (short)0);
            rvb.setParameter(EnvironmentalReverb.PARAM_DECAY_TIME, 2000);
            rvb.setEnabled(true);

            // create probe after reverb so that it is chained behind the reverb in the
            // effect chain
            probe = new EnergyProbe(0);

            mp.prepare();
            mp.start();
            Thread.sleep(1000);
            mp.stop();
            Thread.sleep(300);
            // measure energy around 1kHz after media player was stopped for 300 ms
            int energy1000 = probe.capture(1000);
            assertTrue(msg + ": reverb has no effect", energy1000 > 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 {
            if (mp != null) {
                mp.release();
            }
            if (vc != null) {
                vc.release();
            }
            if (rvb != null) {
                rvb.release();
            }
            if (probe != null) {
                probe.release();
            }
            am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
            am.setRingerMode(ringerMode);
        }
        assertTrue(msg, result);