diff options
author | Dave Sparks <davidsparks@android.com> | 2009-12-15 10:05:53 -0800 |
---|---|---|
committer | Dave Sparks <davidsparks@android.com> | 2009-12-23 09:47:12 -0800 |
commit | d94466c4c8054cbe68cffb132d0ad90d924dca01 (patch) | |
tree | f3e4484fe9dbfcda6b0b1bbe2b05c4d6bfa410a7 | |
parent | 011a3dfd8502aba17fb9c5b202ea2267880213d4 (diff) | |
download | frameworks_base-d94466c4c8054cbe68cffb132d0ad90d924dca01.zip frameworks_base-d94466c4c8054cbe68cffb132d0ad90d924dca01.tar.gz frameworks_base-d94466c4c8054cbe68cffb132d0ad90d924dca01.tar.bz2 |
Add SoundPool test
-rw-r--r-- | media/tests/SoundPoolTest/Android.mk | 10 | ||||
-rw-r--r-- | media/tests/SoundPoolTest/AndroidManifest.xml | 11 | ||||
-rw-r--r-- | media/tests/SoundPoolTest/res/raw/sine441.ogg | bin | 0 -> 5090 bytes | |||
-rw-r--r-- | media/tests/SoundPoolTest/res/raw/test1.mp3 | bin | 0 -> 4491 bytes | |||
-rw-r--r-- | media/tests/SoundPoolTest/res/raw/test2.ogg | bin | 0 -> 11950 bytes | |||
-rw-r--r-- | media/tests/SoundPoolTest/res/raw/test3.ogg | bin | 0 -> 18653 bytes | |||
-rw-r--r-- | media/tests/SoundPoolTest/res/raw/test4.ogg | bin | 0 -> 20983 bytes | |||
-rw-r--r-- | media/tests/SoundPoolTest/res/raw/test5.ogg | bin | 0 -> 11941 bytes | |||
-rw-r--r-- | media/tests/SoundPoolTest/src/com/android/SoundPoolTest.java | 383 |
9 files changed, 404 insertions, 0 deletions
diff --git a/media/tests/SoundPoolTest/Android.mk b/media/tests/SoundPoolTest/Android.mk new file mode 100644 index 0000000..7f947c0 --- /dev/null +++ b/media/tests/SoundPoolTest/Android.mk @@ -0,0 +1,10 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := SoundPoolTest + +include $(BUILD_PACKAGE) diff --git a/media/tests/SoundPoolTest/AndroidManifest.xml b/media/tests/SoundPoolTest/AndroidManifest.xml new file mode 100644 index 0000000..126276c --- /dev/null +++ b/media/tests/SoundPoolTest/AndroidManifest.xml @@ -0,0 +1,11 @@ +<manifest xmlns:android="http://schemas.android.com/apk/res/android" +package="com.android.soundpooltest"> + <application> + <activity android:name="SoundPoolTest" android:label="Sound Pool Test"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/media/tests/SoundPoolTest/res/raw/sine441.ogg b/media/tests/SoundPoolTest/res/raw/sine441.ogg Binary files differnew file mode 100644 index 0000000..cdf1eaa --- /dev/null +++ b/media/tests/SoundPoolTest/res/raw/sine441.ogg diff --git a/media/tests/SoundPoolTest/res/raw/test1.mp3 b/media/tests/SoundPoolTest/res/raw/test1.mp3 Binary files differnew file mode 100644 index 0000000..c1b2fdf --- /dev/null +++ b/media/tests/SoundPoolTest/res/raw/test1.mp3 diff --git a/media/tests/SoundPoolTest/res/raw/test2.ogg b/media/tests/SoundPoolTest/res/raw/test2.ogg Binary files differnew file mode 100644 index 0000000..396d78f --- /dev/null +++ b/media/tests/SoundPoolTest/res/raw/test2.ogg diff --git a/media/tests/SoundPoolTest/res/raw/test3.ogg b/media/tests/SoundPoolTest/res/raw/test3.ogg Binary files differnew file mode 100644 index 0000000..c56f68b --- /dev/null +++ b/media/tests/SoundPoolTest/res/raw/test3.ogg diff --git a/media/tests/SoundPoolTest/res/raw/test4.ogg b/media/tests/SoundPoolTest/res/raw/test4.ogg Binary files differnew file mode 100644 index 0000000..d3cbdfc --- /dev/null +++ b/media/tests/SoundPoolTest/res/raw/test4.ogg diff --git a/media/tests/SoundPoolTest/res/raw/test5.ogg b/media/tests/SoundPoolTest/res/raw/test5.ogg Binary files differnew file mode 100644 index 0000000..1c6851d --- /dev/null +++ b/media/tests/SoundPoolTest/res/raw/test5.ogg diff --git a/media/tests/SoundPoolTest/src/com/android/SoundPoolTest.java b/media/tests/SoundPoolTest/src/com/android/SoundPoolTest.java new file mode 100644 index 0000000..10167ca --- /dev/null +++ b/media/tests/SoundPoolTest/src/com/android/SoundPoolTest.java @@ -0,0 +1,383 @@ +/* + * 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.Config; +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[] { + // FIXME: Restore when Stagefright bug is fixed + R.raw.sine441, + //R.raw.test1, + R.raw.test2, + R.raw.test3, + R.raw.test4, + R.raw.test5 + }; + + private final static int MAX_STREAMS = 1; + 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 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() throws java.lang.InterruptedException { + + if (mSoundPool != null) { + if (mLoadStatus == 0) return mLoadStatus; + mSoundPool.release(); + mSoundPool = null; + } + + // create sound pool + mLoadStatus = 0; + mSoundPool = new SoundPool(MAX_STREAMS, 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); + + 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 { + + // load sound pool + initSoundPool(); + + // do tests + if (!TestSounds()) failures = failures + 1; + if (!TestScales()) failures = failures + 1; + if (!TestRates()) failures = failures + 1; + if (!TestPriority()) failures = failures + 1; + if (!TestPauseResume()) failures = failures + 1; + if (!TestVolume()) 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); + } +} + |