summaryrefslogtreecommitdiffstats
path: root/media/tests/SoundPoolTest/src/com/android/SoundPoolTest.java
blob: 33db2ddb900fe01f58a3b3011ac8fafc424b2fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * Copyright (c) 2009, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.soundpooltest;

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.media.AudioSystem;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.util.Log;
import java.util.HashMap;
import java.lang.Math;

import com.android.soundpooltest.R;

public class SoundPoolTest extends Activity
{
    private static final String LOG_TAG = "SoundPoolTest";
    private static final boolean DEBUG = true;
    private static final boolean VERBOSE = false;
    private TestThread mThread;

    private static final int[] mTestFiles = new int[] {
        R.raw.organ441,
        R.raw.sine441,
        R.raw.test1,
        R.raw.test2,
        R.raw.test3,
        R.raw.test4,
        R.raw.test5
    };

    private final static float SEMITONE = 1.059463094f;
    private final static float DEFAULT_VOLUME = 0.707f;
    private final static float MAX_VOLUME = 1.0f;
    private final static float MIN_VOLUME = 0.01f;
    private final static int LOW_PRIORITY = 1000;
    private final static int NORMAL_PRIORITY = 2000;
    private final static int HIGH_PRIORITY = 3000;
    private final static int DEFAULT_LOOP = -1;
    private final static int DEFAULT_SRC_QUALITY = 0;
    private final static double PI_OVER_2 = Math.PI / 2.0;

    public SoundPoolTest() {}

    private final class TestThread extends java.lang.Thread {
        private boolean mRunning;
        private SoundPool mSoundPool = null;
        private int mLastSample;
        private int mMaxStreams;
        private int mLoadStatus;
        private int[] mSounds;
        private float mScale[];

        TestThread() {
            super("SoundPool.TestThread");
        }

        private final class LoadCompleteCallback implements
            android.media.SoundPool.OnLoadCompleteListener {
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                synchronized(mSoundPool) {
                    if (DEBUG) Log.d(LOG_TAG, "Sample " + sampleId + " load status = " + status);
                    if (status != 0) {
                        mLoadStatus = status;
                    }
                    if (sampleId == mLastSample) {
                        mSoundPool.notify();
                    }
                }
            }
        }

        private int loadSound(int resId, int priority) {
            int id = mSoundPool.load(getApplicationContext(), resId, priority);
            if (id == 0) {
                Log.e(LOG_TAG, "Unable to open resource");
            }
            return id;
        }

        private int initSoundPool(int numStreams) throws java.lang.InterruptedException {

            if (mSoundPool != null) {
                if ((mMaxStreams == numStreams) && (mLoadStatus == 0)) return mLoadStatus;
                mSoundPool.release();
                mSoundPool = null;
            }

            // create sound pool
            mLoadStatus = 0;
            mMaxStreams = numStreams;
            mSoundPool = new SoundPool(numStreams, AudioSystem.STREAM_MUSIC, 0);
            mSoundPool.setOnLoadCompleteListener(new LoadCompleteCallback());
            int numSounds = mTestFiles.length;
            mSounds = new int[numSounds];

            // load sounds
            synchronized(mSoundPool) {
                for (int index = 0; index < numSounds; index++) {
                    mSounds[index] = loadSound(mTestFiles[index], NORMAL_PRIORITY);
                    mLastSample = mSounds[index];
                }
                mSoundPool.wait();
            }
            return mLoadStatus;
        }

        private boolean TestSounds() throws java.lang.InterruptedException {
            if (DEBUG) Log.d(LOG_TAG, "Begin sounds test");
            int count = mSounds.length;
            for (int index = 0; index < count; index++) {
                int id = mSoundPool.play(mSounds[index], DEFAULT_VOLUME, DEFAULT_VOLUME,
                        NORMAL_PRIORITY, DEFAULT_LOOP, 1.0f);
                if (DEBUG) Log.d(LOG_TAG, "Start note " + id);
                if (id == 0) {
                    Log.e(LOG_TAG, "Error occurred starting note");
                    return false;
                }
                sleep(450);
                mSoundPool.stop(id);
                if (DEBUG) Log.d(LOG_TAG, "Stop note " + id);
                sleep(50);
            }
            if (DEBUG) Log.d(LOG_TAG, "End scale test");
            return true;
        }

        private boolean TestScales() throws java.lang.InterruptedException {
            if (DEBUG) Log.d(LOG_TAG, "Begin scale test");

            // interate through pitch table
            int count = mScale.length;
            for (int step = 0; step < count; step++) {
                int id = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                        NORMAL_PRIORITY, DEFAULT_LOOP, mScale[step]);
                if (DEBUG) Log.d(LOG_TAG, "Start note " + id);
                if (id == 0) {
                    Log.e(LOG_TAG, "Error occurred starting note");
                    return false;
                }
                sleep(450);
                mSoundPool.stop(id);
                if (DEBUG) Log.d(LOG_TAG, "Stop note " + id);
                sleep(50);
            }
            if (DEBUG) Log.d(LOG_TAG, "End sounds test");
            return true;
        }

        private boolean TestRates() throws java.lang.InterruptedException {
            if (DEBUG) Log.d(LOG_TAG, "Begin rate test");

            // start the note
            int count = mScale.length;
            int id = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    NORMAL_PRIORITY, DEFAULT_LOOP, mScale[0]);
            if (DEBUG) Log.d(LOG_TAG, "Start note " + id);
            if (id == 0) {
                Log.e(LOG_TAG, "Test failed - exiting");
                return false;
            }

            // modify the pitch
            for (int step = 1; step < count; step++) {
                sleep(250);
                mSoundPool.setRate(id, mScale[step]);
                if (DEBUG) Log.d(LOG_TAG, "Change rate " + mScale[step]);
            }
            mSoundPool.stop(id);
            if (DEBUG) Log.d(LOG_TAG, "End rate test");
            return true;
        }

        private boolean TestPriority() throws java.lang.InterruptedException {
            if (DEBUG) Log.d(LOG_TAG, "Begin priority test");
            boolean result = true;

            // play a normal priority looping sound
            int normalId = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    NORMAL_PRIORITY, DEFAULT_LOOP, 1.0f);
            if (DEBUG) Log.d(LOG_TAG, "Start note " + normalId);
            if (normalId == 0) {
                Log.e(LOG_TAG, "Error occurred starting note");
                return false;
            }
            sleep(250);

            // play a low priority sound
            int id = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    LOW_PRIORITY, DEFAULT_LOOP, 1.0f);
            if (id > 0) {
                Log.e(LOG_TAG, "Normal > Low priority test failed");
                result = false;
                mSoundPool.stop(id);
            } else {
                Log.e(LOG_TAG, "Normal > Low priority test passed");
            }
            sleep(250);

            // play a high priority sound
            id = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    HIGH_PRIORITY, DEFAULT_LOOP, 1.0f);
            if (id == 0) {
                Log.e(LOG_TAG, "High > Normal priority test failed");
                result = false;
            } else {
                Log.e(LOG_TAG, "High > Normal priority test passed");
            }
            sleep(250);
            mSoundPool.stop(id);

            // stop normal note
            mSoundPool.stop(normalId);

            if (DEBUG) Log.d(LOG_TAG, "End priority test");
            return result;
        }

        private boolean TestPauseResume() throws java.lang.InterruptedException {
            if (DEBUG) Log.d(LOG_TAG, "Begin pause/resume test");
            boolean result = true;

            // play a normal priority looping sound
            int id = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    NORMAL_PRIORITY, DEFAULT_LOOP, 1.0f);
            if (DEBUG) Log.d(LOG_TAG, "Start note " + id);
            if (id == 0) {
                Log.e(LOG_TAG, "Error occurred starting note");
                return false;
            }
            sleep(250);

            // pause and resume sound a few times
            for (int count = 0; count < 5; count++) {
                mSoundPool.pause(id);
                sleep(250);
                mSoundPool.resume(id);
                sleep(250);
            }

            mSoundPool.stop(id);

            // play 5 sounds, forces one to be stolen
            int ids[] = new int[5];
            for (int i = 0; i < 5; i++) {
                ids[i] = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                        NORMAL_PRIORITY, DEFAULT_LOOP, mScale[i]);
                if (DEBUG) Log.d(LOG_TAG, "Start note " + ids[i]);
                if (ids[i] == 0) {
                    Log.e(LOG_TAG, "Error occurred starting note");
                    return false;
                }
                sleep(250);
            }

            // pause and resume sound a few times
            for (int count = 0; count < 5; count++) {
                mSoundPool.autoPause();
                sleep(250);
                mSoundPool.autoResume();
                sleep(250);
            }

            for (int i = 0; i < 5; i++) {
                mSoundPool.stop(ids[i]);
            }

            if (DEBUG) Log.d(LOG_TAG, "End pause/resume test");
            return result;
        }

        private boolean TestVolume() throws java.lang.InterruptedException {
            if (DEBUG) Log.d(LOG_TAG, "Begin volume test");

            // start the note
            int id = mSoundPool.play(mSounds[0], 0.0f, 1.0f, NORMAL_PRIORITY, DEFAULT_LOOP, mScale[0]);
            if (DEBUG) Log.d(LOG_TAG, "Start note " + id);
            if (id == 0) {
                Log.e(LOG_TAG, "Test failed - exiting");
                return false;
            }

            // pan from left to right
            for (int count = 0; count < 101; count++) {
                sleep(20);
                double radians = PI_OVER_2 * count / 100.0;
                float leftVolume = (float) Math.sin(radians);
                float rightVolume = (float) Math.cos(radians);
                mSoundPool.setVolume(id, leftVolume, rightVolume);
                if (DEBUG) Log.d(LOG_TAG, "Change volume (" + leftVolume + "," + rightVolume + ")");
            }

            mSoundPool.stop(id);
            if (DEBUG) Log.d(LOG_TAG, "End volume test");
            return true;
        }

        public void run() {
            if (DEBUG) Log.d(LOG_TAG, "Test thread running");

            // initialize
            mRunning = true;
            int failures = 0;

            // initialize pitch table
            float pitch = 0.5f;
            mScale = new float[13];
            for (int i = 0; i < 13; ++i) {
                mScale[i] = pitch;
                pitch *= SEMITONE;
            }

            try {

                // do single stream tests
                initSoundPool(1);
                if (!TestSounds()) failures = failures + 1;
                if (!TestScales()) failures = failures + 1;
                if (!TestRates()) failures = failures + 1;
                if (!TestPriority()) failures = failures + 1;
                if (!TestVolume()) failures = failures + 1;

                // do multiple stream tests
                initSoundPool(4);
                if (!TestPauseResume()) failures = failures + 1;

            } catch (java.lang.InterruptedException e) {
                if (DEBUG) Log.d(LOG_TAG, "Test interrupted");
                failures = failures + 1;
            } finally {
                mRunning = false;
            }

            // release sound pool
            if (mSoundPool != null) {
                mSoundPool.release();
                mSoundPool = null;
            }

            // output stats
            if (DEBUG) Log.d(LOG_TAG, "Test thread exit");
            if (failures == 0) {
                Log.i(LOG_TAG, "All tests passed");
            } else {
                Log.i(LOG_TAG, failures + " tests failed");
            }
        }

        public void quit() {
            if (DEBUG) Log.d(LOG_TAG, "interrupt");
            interrupt();
            while (mRunning) {
                try {
                    sleep(20);
                } catch (java.lang.InterruptedException e) { }
            }
            if (DEBUG) Log.d(LOG_TAG, "quit");
        }
    }

    private void startTests() {
        mThread = new TestThread();
        mThread.start();
    }

    protected void onPause()
    {
        Log.v(LOG_TAG, "onPause");
        super.onPause();
        mThread.quit();
        mThread = null;
    }

    protected void onResume()
    {
        Log.v(LOG_TAG, "onResume");
        super.onResume();
        startTests();
    }

    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }
}