├── .gitignore ├── .gitmodules ├── OpenSlStreamSample ├── .classpath ├── .project ├── AndroidManifest.xml ├── Makefile ├── ic_launcher-web.png ├── jni │ ├── Android.mk │ ├── Application.mk │ ├── lowpass.c │ └── lowpass.h ├── libs │ ├── android-support-v4.jar │ ├── armeabi-v7a │ │ └── liblowpass.so │ ├── armeabi │ │ └── liblowpass.so │ └── x86 │ │ └── liblowpass.so ├── proguard-project.txt ├── project.properties ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── layout │ │ └── activity_main.xml │ ├── menu │ │ └── main.xml │ ├── values-sw600dp │ │ └── dimens.xml │ ├── values-sw720dp-land │ │ └── dimens.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── noisepages │ └── nettoyeur │ └── openslstreamsample │ ├── Lowpass.java │ ├── MainActivity.java │ └── OpenSlParams.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | obj/ 15 | 16 | # Local configuration file (sdk path, etc) 17 | local.properties 18 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "OpenSlStreamSample/jni/opensl_stream"] 2 | path = OpenSlStreamSample/jni/opensl_stream 3 | url = git://github.com/nettoyeurny/opensl_stream.git 4 | -------------------------------------------------------------------------------- /OpenSlStreamSample/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /OpenSlStreamSample/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | OpenSlStreamSample 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /OpenSlStreamSample/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 12 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /OpenSlStreamSample/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | javah -classpath bin/classes -o jni/lowpass.h com.noisepages.nettoyeur.openslstreamsample.Lowpass 3 | ndk-build 4 | -------------------------------------------------------------------------------- /OpenSlStreamSample/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/ic_launcher-web.png -------------------------------------------------------------------------------- /OpenSlStreamSample/jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := lowpass 6 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/jni 7 | LOCAL_LDLIBS := -lOpenSLES -llog 8 | LOCAL_SRC_FILES := lowpass.c opensl_stream/opensl_stream.c 9 | include $(BUILD_SHARED_LIBRARY) 10 | -------------------------------------------------------------------------------- /OpenSlStreamSample/jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_OPTIM := release 2 | APP_ABI := armeabi armeabi-v7a x86 3 | -------------------------------------------------------------------------------- /OpenSlStreamSample/jni/lowpass.c: -------------------------------------------------------------------------------- 1 | #include "lowpass.h" 2 | 3 | #include 4 | 5 | #include "opensl_stream/opensl_stream.h" 6 | 7 | // Container holding an opensl_stream instance as well as two properties of the 8 | // lowpass filter, the smoothing factor and the previous sample. 9 | static struct lowpass { 10 | OPENSL_STREAM *os; 11 | int alpha; 12 | int prev; 13 | }; 14 | 15 | // Audio processing callback; this is the heart and soul of this file. 16 | static void process(void *context, int sample_rate, int buffer_frames, 17 | int input_channels, const short *input_buffer, 18 | int output_channels, short *output_buffer) { 19 | if (input_channels > 0) { 20 | struct lowpass *lp = (struct lowpass *)context; 21 | // We use gcc atomics here because alpha may change concurrently. 22 | int alpha = __sync_fetch_and_or(&lp->alpha, 0); 23 | int i; 24 | for (i = 0; i < buffer_frames; ++i) { 25 | // No need to protect lp->prev with gcc atomics because we won't change 26 | // it concurrently. 27 | int v = (alpha * input_buffer[input_channels * i] + 28 | (100 - alpha) * lp->prev) / 100; 29 | lp->prev = v; 30 | int j; 31 | for (j = 0; j < output_channels; ++j) { 32 | output_buffer[output_channels * i + j] = v; 33 | } 34 | } 35 | } 36 | } 37 | 38 | JNIEXPORT jlong JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_open 39 | (JNIEnv *env, jclass clazz, jint sampleRate, jint bufferSize) { 40 | struct lowpass *lp = malloc(sizeof(struct lowpass)); 41 | if (lp) { 42 | lp->alpha = 100; 43 | lp->os = opensl_open(sampleRate, 1, 2, bufferSize, process, lp); 44 | if (!lp->os) { 45 | free(lp); 46 | lp = NULL; 47 | } 48 | } 49 | return (jlong) lp; 50 | } 51 | 52 | JNIEXPORT void JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_close 53 | (JNIEnv *env, jclass clazz, jlong p) { 54 | struct lowpass *lp = (struct lowpass *)p; 55 | opensl_close(lp->os); 56 | free(lp); 57 | } 58 | 59 | JNIEXPORT jint JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_start 60 | (JNIEnv *env, jclass clazz, jlong p) { 61 | struct lowpass *lp = (struct lowpass *)p; 62 | lp->prev = 0; 63 | return opensl_start(lp->os); 64 | } 65 | 66 | JNIEXPORT void JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_stop 67 | (JNIEnv *env, jclass clazz, jlong p) { 68 | struct lowpass *lp = (struct lowpass *)p; 69 | opensl_pause(lp->os); 70 | } 71 | 72 | JNIEXPORT void JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_setAlpha 73 | (JNIEnv *env, jclass clazz, jlong p, jint alpha) { 74 | struct lowpass *lp = (struct lowpass *)p; 75 | __sync_bool_compare_and_swap(&lp->alpha, lp->alpha, alpha); 76 | } 77 | 78 | JNIEXPORT jboolean JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_isRunning 79 | (JNIEnv *env, jclass clazz, jlong p) { 80 | struct lowpass *lp = (struct lowpass *)p; 81 | return opensl_is_running(lp->os); 82 | } 83 | -------------------------------------------------------------------------------- /OpenSlStreamSample/jni/lowpass.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class com_noisepages_nettoyeur_openslstreamsample_Lowpass */ 4 | 5 | #ifndef _Included_com_noisepages_nettoyeur_openslstreamsample_Lowpass 6 | #define _Included_com_noisepages_nettoyeur_openslstreamsample_Lowpass 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: com_noisepages_nettoyeur_openslstreamsample_Lowpass 12 | * Method: open 13 | * Signature: (II)J 14 | */ 15 | JNIEXPORT jlong JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_open 16 | (JNIEnv *, jclass, jint, jint); 17 | 18 | /* 19 | * Class: com_noisepages_nettoyeur_openslstreamsample_Lowpass 20 | * Method: close 21 | * Signature: (J)V 22 | */ 23 | JNIEXPORT void JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_close 24 | (JNIEnv *, jclass, jlong); 25 | 26 | /* 27 | * Class: com_noisepages_nettoyeur_openslstreamsample_Lowpass 28 | * Method: start 29 | * Signature: (J)I 30 | */ 31 | JNIEXPORT jint JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_start 32 | (JNIEnv *, jclass, jlong); 33 | 34 | /* 35 | * Class: com_noisepages_nettoyeur_openslstreamsample_Lowpass 36 | * Method: stop 37 | * Signature: (J)V 38 | */ 39 | JNIEXPORT void JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_stop 40 | (JNIEnv *, jclass, jlong); 41 | 42 | /* 43 | * Class: com_noisepages_nettoyeur_openslstreamsample_Lowpass 44 | * Method: setAlpha 45 | * Signature: (JI)V 46 | */ 47 | JNIEXPORT void JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_setAlpha 48 | (JNIEnv *, jclass, jlong, jint); 49 | 50 | /* 51 | * Class: com_noisepages_nettoyeur_openslstreamsample_Lowpass 52 | * Method: isRunning 53 | * Signature: (J)Z 54 | */ 55 | JNIEXPORT jboolean JNICALL Java_com_noisepages_nettoyeur_openslstreamsample_Lowpass_isRunning 56 | (JNIEnv *, jclass, jlong); 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | #endif 62 | -------------------------------------------------------------------------------- /OpenSlStreamSample/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/libs/android-support-v4.jar -------------------------------------------------------------------------------- /OpenSlStreamSample/libs/armeabi-v7a/liblowpass.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/libs/armeabi-v7a/liblowpass.so -------------------------------------------------------------------------------- /OpenSlStreamSample/libs/armeabi/liblowpass.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/libs/armeabi/liblowpass.so -------------------------------------------------------------------------------- /OpenSlStreamSample/libs/x86/liblowpass.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/libs/x86/liblowpass.so -------------------------------------------------------------------------------- /OpenSlStreamSample/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /OpenSlStreamSample/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /OpenSlStreamSample/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /OpenSlStreamSample/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /OpenSlStreamSample/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nettoyeurny/opensl_stream_sample/226536b3ed6c7639c65aeabe12f8008651dced85/OpenSlStreamSample/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /OpenSlStreamSample/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 18 | 19 | 26 | 27 | 34 | 35 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values-sw600dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 128dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | OpenSlStreamSample 5 | Settings 6 | Hello world! 7 | 8 | 9 | -------------------------------------------------------------------------------- /OpenSlStreamSample/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /OpenSlStreamSample/src/com/noisepages/nettoyeur/openslstreamsample/Lowpass.java: -------------------------------------------------------------------------------- 1 | package com.noisepages.nettoyeur.openslstreamsample; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * Simple lowpass filter implementation using opensl_stream. 7 | */ 8 | public class Lowpass { 9 | 10 | static { 11 | // Load the associated native library, which does most of the work. 12 | System.loadLibrary("lowpass"); 13 | } 14 | 15 | private long streamPtr; // Slightly naughty: We store a C pointer in a field of type long. 16 | 17 | /** 18 | * Constructs a lowpass filter object for the given sample rate and buffer size. 19 | * 20 | * @param sampleRate in Hertz. 21 | * @param bufferSize in frames. 22 | * @throws IOException if the sample rate or buffer size are not supported. 23 | */ 24 | public Lowpass(int sampleRate, int bufferSize) throws IOException { 25 | streamPtr = open(sampleRate, bufferSize); 26 | if (streamPtr == 0) { 27 | throw new IOException("Unsupported audio parameters: " + sampleRate + ", " + bufferSize); 28 | } 29 | } 30 | 31 | /** 32 | * Must be called before this object is garbage collected. Safe to call more than once. 33 | */ 34 | public void close() { 35 | if (streamPtr != 0) { 36 | close(streamPtr); 37 | streamPtr = 0; 38 | } 39 | } 40 | 41 | /** 42 | * Starts the OpenSL audio stream; will have no effect if the object has already been started. May 43 | * not be called after close() has been called. 44 | * 45 | * @throws IOException if the stream cannot be started. 46 | */ 47 | public void start() throws IOException { 48 | if (streamPtr == 0) { 49 | throw new IllegalStateException("Stream closed."); 50 | } 51 | if (start(streamPtr) != 0) { 52 | throw new IOException("Unable to start OpenSL stream."); 53 | } 54 | } 55 | 56 | /** 57 | * Stops the OpenSL audio stream; will have no effect if the object has already been started. May 58 | * not be called after close() has been called. 59 | */ 60 | public void stop() { 61 | if (streamPtr == 0) { 62 | throw new IllegalStateException("Stream closed."); 63 | } 64 | stop(streamPtr); 65 | } 66 | 67 | /** 68 | * Sets the smoothing factor alpha; safe to call while the stream is running. May not be called 69 | * after close() has been called. 70 | * 71 | * @param Smoothing factor in percent, 0 <= alpha <= 100. 72 | */ 73 | public void setAlpha(int alpha) { 74 | if (streamPtr == 0) { 75 | throw new IllegalStateException("Stream closed."); 76 | } 77 | setAlpha(streamPtr, alpha); 78 | } 79 | 80 | /** 81 | * May not be called after close() has been called. 82 | * 83 | * @return true if the OpenSL audio stream filter is running. 84 | */ 85 | public boolean isRunning() { 86 | if (streamPtr == 0) { 87 | throw new IllegalStateException("Stream closed."); 88 | } 89 | return isRunning(streamPtr); 90 | } 91 | 92 | private static native long open(int sampleRate, int bufferSize); 93 | 94 | private static native void close(long streamPtr); 95 | 96 | private static native int start(long streamPtr); 97 | 98 | private static native void stop(long streamPtr); 99 | 100 | private static native void setAlpha(long streamPtr, int alpha); 101 | 102 | private static native boolean isRunning(long streamPtr); 103 | } 104 | -------------------------------------------------------------------------------- /OpenSlStreamSample/src/com/noisepages/nettoyeur/openslstreamsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.noisepages.nettoyeur.openslstreamsample; 2 | 3 | import java.io.IOException; 4 | 5 | import android.app.Activity; 6 | import android.os.Bundle; 7 | import android.view.Menu; 8 | import android.widget.CompoundButton; 9 | import android.widget.CompoundButton.OnCheckedChangeListener; 10 | import android.widget.SeekBar; 11 | import android.widget.SeekBar.OnSeekBarChangeListener; 12 | import android.widget.Switch; 13 | 14 | /** 15 | * Main activity for OpenSL sample app. This class is essentially boilerplate; most of the 16 | * interesting bits are in {@link OpenSlParams} and {@link Lowpass}. 17 | */ 18 | public class MainActivity extends Activity 19 | implements 20 | OnCheckedChangeListener, 21 | OnSeekBarChangeListener { 22 | 23 | private Lowpass lowpass; 24 | private SeekBar filterBar; 25 | private Switch playSwitch; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_main); 31 | filterBar = (SeekBar) findViewById(R.id.filterBar); 32 | filterBar.setOnSeekBarChangeListener(this); 33 | playSwitch = (Switch) findViewById(R.id.playSwitch); 34 | playSwitch.setOnCheckedChangeListener(this); 35 | } 36 | 37 | @Override 38 | public boolean onCreateOptionsMenu(Menu menu) { 39 | getMenuInflater().inflate(R.menu.main, menu); 40 | return true; 41 | } 42 | 43 | @Override 44 | protected void onStart() { 45 | super.onStart(); 46 | try { 47 | OpenSlParams params = OpenSlParams.createInstance(this); 48 | lowpass = new Lowpass(params.getSampleRate(), params.getBufferSize()); 49 | lowpass.setAlpha(filterBar.getProgress()); 50 | if (playSwitch.isChecked()) { 51 | lowpass.start(); 52 | } 53 | } catch (IOException e) { 54 | throw new RuntimeException(e); 55 | } 56 | } 57 | 58 | @Override 59 | protected void onStop() { 60 | super.onStop(); 61 | lowpass.close(); 62 | } 63 | 64 | @Override 65 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 66 | if (isChecked) { 67 | try { 68 | lowpass.start(); 69 | } catch (IOException e) { 70 | throw new RuntimeException(e); 71 | } 72 | } else { 73 | lowpass.stop(); 74 | } 75 | } 76 | 77 | @Override 78 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 79 | lowpass.setAlpha(progress); 80 | } 81 | 82 | @Override 83 | public void onStartTrackingTouch(SeekBar seekBar) { 84 | // Do nothing. 85 | } 86 | 87 | @Override 88 | public void onStopTrackingTouch(SeekBar seekBar) { 89 | // Do nothing. 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /OpenSlStreamSample/src/com/noisepages/nettoyeur/openslstreamsample/OpenSlParams.java: -------------------------------------------------------------------------------- 1 | package com.noisepages.nettoyeur.openslstreamsample; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.media.AudioManager; 6 | import android.os.Build; 7 | import android.util.Log; 8 | 9 | /** 10 | * This class illustrates how to query OpenSL config parameters on Jelly Bean MR1 while maintaining 11 | * backward compatibility with older versions of Android. The trick is to place the new API calls in 12 | * an inner class that will only be loaded if we're running on JB MR1 or later. 13 | */ 14 | public abstract class OpenSlParams { 15 | 16 | /** 17 | * @return The recommended sample rate in Hz. 18 | */ 19 | public abstract int getSampleRate(); 20 | 21 | /** 22 | * @return The recommended buffer size in frames. 23 | */ 24 | public abstract int getBufferSize(); 25 | 26 | /** 27 | * @param context, e.g., the current activity. 28 | * @return OpenSlParams instance for the given context. 29 | */ 30 | public static OpenSlParams createInstance(Context context) { 31 | return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 32 | ? new JellyBeanMr1OpenSlParams(context) 33 | : new DefaultOpenSlParams(); 34 | } 35 | 36 | private OpenSlParams() { 37 | // Not meant to be instantiated except here. 38 | } 39 | 40 | // Implementation for Jelly Bean MR1 or later. 41 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) 42 | private static class JellyBeanMr1OpenSlParams extends OpenSlParams { 43 | 44 | private final int sampleRate; 45 | private final int bufferSize; 46 | 47 | private JellyBeanMr1OpenSlParams(Context context) { 48 | AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 49 | // Provide default values in case config lookup fails. 50 | int sr = 44100; 51 | int bs = 64; 52 | try { 53 | // If possible, query the native sample rate and buffer size. 54 | sr = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)); 55 | bs = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)); 56 | } catch (NumberFormatException e) { 57 | Log.w(getClass().getName(), "Failed to read native OpenSL config: " + e); 58 | } 59 | sampleRate = sr; 60 | bufferSize = bs; 61 | } 62 | 63 | @Override 64 | public int getSampleRate() { 65 | return sampleRate; 66 | } 67 | 68 | @Override 69 | public int getBufferSize() { 70 | return bufferSize; 71 | } 72 | }; 73 | 74 | // Default factory for Jelly Bean or older. 75 | private static class DefaultOpenSlParams extends OpenSlParams { 76 | @Override 77 | public int getSampleRate() { 78 | return 44100; 79 | } 80 | 81 | @Override 82 | public int getBufferSize() { 83 | return 64; 84 | } 85 | }; 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | opensl_stream_sample 2 | ==================== 3 | 4 | A sample app illustrating how to use opensl_stream. In addition to the usage of the C API, it also shows how to configure OpenSL, using new API calls in Android 4.2 (Jelly Bean MR1) if possible and resorting to reasonable default values if necessary. 5 | 6 | This sample app will work with Android 4.0 (Ice Cream Sandwich) or later; opensl_stream itself, however, only requires Android 2.3 (Gingerbread) or later. 7 | 8 | Make sure to clone this repository with "--recursive" to get the opensl_stream submodule. 9 | --------------------------------------------------------------------------------