MediaBassBoostTest.java revision db6028508c8eb31a0de1dcdfc410ddfe6df7c5ad
1/*
2 * Copyright (C) 2010 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
17package com.android.mediaframeworktest.functional.audio;
18
19import com.android.mediaframeworktest.MediaFrameworkTest;
20import com.android.mediaframeworktest.MediaNames;
21import com.android.mediaframeworktest.functional.EnergyProbe;
22import android.content.Context;
23import android.content.res.AssetFileDescriptor;
24import android.media.audiofx.AudioEffect;
25import android.media.AudioManager;
26import android.media.audiofx.BassBoost;
27import android.media.audiofx.Visualizer;
28import android.media.MediaPlayer;
29
30import android.os.Looper;
31import android.test.suitebuilder.annotation.LargeTest;
32import android.test.suitebuilder.annotation.MediumTest;
33import android.test.suitebuilder.annotation.Suppress;
34import android.test.ActivityInstrumentationTestCase2;
35import android.util.Log;
36
37import java.nio.ByteOrder;
38import java.nio.ByteBuffer;
39import java.util.UUID;
40
41/**
42 * Junit / Instrumentation test case for the media AudioTrack api
43
44 */
45public class MediaBassBoostTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
46    private String TAG = "MediaBassBoostTest";
47    private final static int MIN_ENERGY_RATIO_2 = 3;
48    private final static short TEST_STRENGTH = 500;
49    private final static int TEST_VOLUME = 4;
50
51    private BassBoost mBassBoost = null;
52    private int mSession = -1;
53
54    public MediaBassBoostTest() {
55        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
56    }
57
58    @Override
59    protected void setUp() throws Exception {
60      super.setUp();
61    }
62
63    @Override
64    protected void tearDown() throws Exception {
65        super.tearDown();
66        releaseBassBoost();
67    }
68
69    private static void assumeTrue(String message, boolean cond) {
70        assertTrue("(assume)"+message, cond);
71    }
72
73    private void log(String testName, String message) {
74        Log.v(TAG, "["+testName+"] "+message);
75    }
76
77    private void loge(String testName, String message) {
78        Log.e(TAG, "["+testName+"] "+message);
79    }
80
81    //-----------------------------------------------------------------
82    // BASS BOOST TESTS:
83    //----------------------------------
84
85
86    //-----------------------------------------------------------------
87    // 0 - constructor
88    //----------------------------------
89
90    //Test case 0.0: test constructor and release
91    @LargeTest
92    public void test0_0ConstructorAndRelease() throws Exception {
93        boolean result = false;
94        String msg = "test1_0ConstructorAndRelease()";
95        BassBoost bb = null;
96         try {
97            bb = new BassBoost(0, 0);
98            assertNotNull(msg + ": could not create BassBoost", bb);
99            try {
100                assertTrue(msg +": invalid effect ID", (bb.getId() != 0));
101            } catch (IllegalStateException e) {
102                msg = msg.concat(": BassBoost not initialized");
103            }
104            result = true;
105        } catch (IllegalArgumentException e) {
106            msg = msg.concat(": BassBoost not found");
107        } catch (UnsupportedOperationException e) {
108            msg = msg.concat(": Effect library not loaded");
109        } finally {
110            if (bb != null) {
111                bb.release();
112            }
113        }
114        assertTrue(msg, result);
115    }
116
117    //-----------------------------------------------------------------
118    // 1 - get/set parameters
119    //----------------------------------
120
121    //Test case 1.0: test strength
122    @LargeTest
123    public void test1_0Strength() throws Exception {
124        boolean result = false;
125        String msg = "test1_0Strength()";
126        getBassBoost(0);
127        try {
128            if (mBassBoost.getStrengthSupported()) {
129                mBassBoost.setStrength((short)TEST_STRENGTH);
130                short strength = mBassBoost.getRoundedStrength();
131                // allow 10% difference between set strength and rounded strength
132                assertTrue(msg +": got incorrect strength",
133                        ((float)strength > (float)TEST_STRENGTH * 0.9f) &&
134                        ((float)strength < (float)TEST_STRENGTH * 1.1f));
135            } else {
136                short strength = mBassBoost.getRoundedStrength();
137                assertTrue(msg +": got incorrect strength", strength >= 0 && strength <= 1000);
138            }
139            result = true;
140        } catch (IllegalArgumentException e) {
141            msg = msg.concat(": Bad parameter value");
142            loge(msg, "Bad parameter value");
143        } catch (UnsupportedOperationException e) {
144            msg = msg.concat(": get parameter() rejected");
145            loge(msg, "get parameter() rejected");
146        } catch (IllegalStateException e) {
147            msg = msg.concat("get parameter() called in wrong state");
148            loge(msg, "get parameter() called in wrong state");
149        } finally {
150            releaseBassBoost();
151        }
152        assertTrue(msg, result);
153    }
154
155    //Test case 1.1: test properties
156    @LargeTest
157    public void test1_1Properties() throws Exception {
158        boolean result = false;
159        String msg = "test1_1Properties()";
160        getBassBoost(0);
161        try {
162            BassBoost.Settings settings = mBassBoost.getProperties();
163            String str = settings.toString();
164            settings = new BassBoost.Settings(str);
165            mBassBoost.setProperties(settings);
166            result = true;
167        } catch (IllegalArgumentException e) {
168            msg = msg.concat(": Bad parameter value");
169            loge(msg, "Bad parameter value");
170        } catch (UnsupportedOperationException e) {
171            msg = msg.concat(": get parameter() rejected");
172            loge(msg, "get parameter() rejected");
173        } catch (IllegalStateException e) {
174            msg = msg.concat("get parameter() called in wrong state");
175            loge(msg, "get parameter() called in wrong state");
176        } finally {
177            releaseBassBoost();
178        }
179        assertTrue(msg, result);
180    }
181
182    //-----------------------------------------------------------------
183    // 2 - Effect action
184    //----------------------------------
185
186    //Test case 2.0: test actual bass boost influence on sound
187    @LargeTest
188    public void test2_0SoundModification() throws Exception {
189        boolean result = false;
190        String msg = "test2_0SoundModification()";
191        EnergyProbe probe = null;
192        AudioEffect vc = null;
193        MediaPlayer mp = null;
194        AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
195        int volume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
196        am.setStreamVolume(AudioManager.STREAM_MUSIC,
197                           TEST_VOLUME,
198                           0);
199
200        try {
201            probe = new EnergyProbe(0);
202            // creating a volume controller on output mix ensures that ro.audio.silent mutes
203            // audio after the effects and not before
204            vc = new AudioEffect(
205                    AudioEffect.EFFECT_TYPE_NULL,
206                    UUID.fromString("119341a0-8469-11df-81f9-0002a5d5c51b"),
207                      0,
208                      0);
209            vc.setEnabled(true);
210
211            mp = new MediaPlayer();
212            mp.setDataSource(MediaNames.SINE_200_1000);
213            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
214            getBassBoost(mp.getAudioSessionId());
215            mp.prepare();
216            mp.start();
217            Thread.sleep(200);
218            // measure reference energy around 1kHz
219            int refEnergy200 = probe.capture(200);
220            int refEnergy1000 = probe.capture(1000);
221            mBassBoost.setStrength((short)1000);
222            mBassBoost.setEnabled(true);
223            Thread.sleep(500);
224            // measure energy around 1kHz with band level at min
225            int energy200 = probe.capture(200);
226            int energy1000 = probe.capture(1000);
227            // verify that the energy ration between low and high frequencies is at least
228            // MIN_ENERGY_RATIO_2 times higher with bassboost on.
229            assertTrue(msg + ": bass boost has no effect",
230                    ((float)energy200/(float)energy1000) >
231                    (MIN_ENERGY_RATIO_2 * ((float)refEnergy200/(float)refEnergy1000)));
232            result = true;
233        } catch (IllegalArgumentException e) {
234            msg = msg.concat(": Bad parameter value");
235            loge(msg, "Bad parameter value");
236        } catch (UnsupportedOperationException e) {
237            msg = msg.concat(": get parameter() rejected");
238            loge(msg, "get parameter() rejected");
239        } catch (IllegalStateException e) {
240            msg = msg.concat("get parameter() called in wrong state");
241            loge(msg, "get parameter() called in wrong state");
242        } catch (InterruptedException e) {
243            loge(msg, "sleep() interrupted");
244        }
245        finally {
246            releaseBassBoost();
247            if (mp != null) {
248                mp.release();
249            }
250            if (vc != null) {
251                vc.release();
252            }
253            if (probe != null) {
254                probe.release();
255            }
256            am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
257        }
258        assertTrue(msg, result);
259    }
260    //-----------------------------------------------------------------
261    // private methods
262    //----------------------------------
263
264    private void getBassBoost(int session) {
265         if (mBassBoost == null || session != mSession) {
266             if (session != mSession && mBassBoost != null) {
267                 mBassBoost.release();
268                 mBassBoost = null;
269             }
270             try {
271                mBassBoost = new BassBoost(0, session);
272                mSession = session;
273            } catch (IllegalArgumentException e) {
274                Log.e(TAG, "getBassBoost() BassBoost not found exception: "+e);
275            } catch (UnsupportedOperationException e) {
276                Log.e(TAG, "getBassBoost() Effect library not loaded exception: "+e);
277            }
278         }
279         assertNotNull("could not create mBassBoost", mBassBoost);
280    }
281
282    private void releaseBassBoost() {
283        if (mBassBoost != null) {
284            mBassBoost.release();
285            mBassBoost = null;
286        }
287   }
288
289}
290