1 package org.libsdl.app;
3 import android.media.*;
4 import android.os.Build;
5 import android.util.Log;
9 protected static final String
TAG =
"SDLAudio";
22 switch (audioFormat) {
23 case AudioFormat.ENCODING_PCM_8BIT:
25 case AudioFormat.ENCODING_PCM_16BIT:
27 case AudioFormat.ENCODING_PCM_FLOAT:
30 return Integer.toString(audioFormat);
34 protected static int[]
open(
boolean isCapture,
int sampleRate,
int audioFormat,
int desiredChannels,
int desiredFrames) {
39 Log.v(TAG,
"Opening " + (isCapture ?
"capture" :
"playback") +
", requested " + desiredFrames +
" frames of " + desiredChannels +
" channel " +
getAudioFormatString(audioFormat) +
" audio at " + sampleRate +
" Hz");
42 if (Build.VERSION.SDK_INT < 21) {
43 if (desiredChannels > 2) {
46 if (sampleRate < 8000) {
48 }
else if (sampleRate > 48000) {
53 if (audioFormat == AudioFormat.ENCODING_PCM_FLOAT) {
54 int minSDKVersion = (isCapture ? 23 : 21);
55 if (Build.VERSION.SDK_INT < minSDKVersion) {
56 audioFormat = AudioFormat.ENCODING_PCM_16BIT;
61 case AudioFormat.ENCODING_PCM_8BIT:
64 case AudioFormat.ENCODING_PCM_16BIT:
67 case AudioFormat.ENCODING_PCM_FLOAT:
71 Log.v(TAG,
"Requested format " + audioFormat +
", getting ENCODING_PCM_16BIT");
72 audioFormat = AudioFormat.ENCODING_PCM_16BIT;
78 switch (desiredChannels) {
80 channelConfig = AudioFormat.CHANNEL_IN_MONO;
83 channelConfig = AudioFormat.CHANNEL_IN_STEREO;
86 Log.v(TAG,
"Requested " + desiredChannels +
" channels, getting stereo");
88 channelConfig = AudioFormat.CHANNEL_IN_STEREO;
92 switch (desiredChannels) {
94 channelConfig = AudioFormat.CHANNEL_OUT_MONO;
97 channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
100 channelConfig = AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
103 channelConfig = AudioFormat.CHANNEL_OUT_QUAD;
106 channelConfig = AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
109 channelConfig = AudioFormat.CHANNEL_OUT_5POINT1;
112 channelConfig = AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER;
115 if (Build.VERSION.SDK_INT >= 23) {
116 channelConfig = AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
118 Log.v(TAG,
"Requested " + desiredChannels +
" channels, getting 5.1 surround");
120 channelConfig = AudioFormat.CHANNEL_OUT_5POINT1;
124 Log.v(TAG,
"Requested " + desiredChannels +
" channels, getting stereo");
126 channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
168 frameSize = (sampleSize * desiredChannels);
175 minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
177 minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
179 desiredFrames = Math.max(desiredFrames, (minBufferSize + frameSize - 1) / frameSize);
181 int[] results =
new int[4];
184 if (mAudioRecord == null) {
185 mAudioRecord =
new AudioRecord(MediaRecorder.AudioSource.DEFAULT, sampleRate,
186 channelConfig, audioFormat, desiredFrames * frameSize);
189 if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
190 Log.e(TAG,
"Failed during initialization of AudioRecord");
191 mAudioRecord.release();
196 mAudioRecord.startRecording();
199 results[0] = mAudioRecord.getSampleRate();
200 results[1] = mAudioRecord.getAudioFormat();
201 results[2] = mAudioRecord.getChannelCount();
202 results[3] = desiredFrames;
205 if (mAudioTrack == null) {
206 mAudioTrack =
new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
211 if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
214 Log.e(TAG,
"Failed during initialization of Audio Track");
215 mAudioTrack.release();
223 results[0] = mAudioTrack.getSampleRate();
224 results[1] = mAudioTrack.getAudioFormat();
225 results[2] = mAudioTrack.getChannelCount();
226 results[3] = desiredFrames;
229 Log.v(TAG,
"Opening " + (isCapture ?
"capture" :
"playback") +
", got " + results[3] +
" frames of " + results[2] +
" channel " +
getAudioFormatString(results[1]) +
" audio at " + results[0] +
" Hz");
237 public static int[]
audioOpen(
int sampleRate,
int audioFormat,
int desiredChannels,
int desiredFrames) {
238 return open(
false, sampleRate, audioFormat, desiredChannels, desiredFrames);
245 if (mAudioTrack == null) {
246 Log.e(TAG,
"Attempted to make audio call with uninitialized audio!");
250 for (
int i = 0;
i < buffer.length;) {
251 int result = mAudioTrack.write(buffer,
i, buffer.length -
i, AudioTrack.WRITE_BLOCKING);
254 }
else if (result == 0) {
257 }
catch(InterruptedException
e) {
261 Log.w(TAG,
"SDL audio: error return from write(float)");
271 if (mAudioTrack == null) {
272 Log.e(TAG,
"Attempted to make audio call with uninitialized audio!");
276 for (
int i = 0;
i < buffer.length;) {
277 int result = mAudioTrack.write(buffer,
i, buffer.length -
i);
280 }
else if (result == 0) {
283 }
catch(InterruptedException
e) {
287 Log.w(TAG,
"SDL audio: error return from write(short)");
297 if (mAudioTrack == null) {
298 Log.e(TAG,
"Attempted to make audio call with uninitialized audio!");
302 for (
int i = 0;
i < buffer.length; ) {
303 int result = mAudioTrack.write(buffer,
i, buffer.length -
i);
306 }
else if (result == 0) {
309 }
catch(InterruptedException
e) {
313 Log.w(TAG,
"SDL audio: error return from write(byte)");
322 public static int[]
captureOpen(
int sampleRate,
int audioFormat,
int desiredChannels,
int desiredFrames) {
323 return open(
true, sampleRate, audioFormat, desiredChannels, desiredFrames);
328 return mAudioRecord.read(buffer, 0, buffer.length, blocking ? AudioRecord.READ_BLOCKING : AudioRecord.READ_NON_BLOCKING);
333 if (Build.VERSION.SDK_INT < 23) {
334 return mAudioRecord.read(buffer, 0, buffer.length);
336 return mAudioRecord.read(buffer, 0, buffer.length, blocking ? AudioRecord.READ_BLOCKING : AudioRecord.READ_NON_BLOCKING);
342 if (Build.VERSION.SDK_INT < 23) {
343 return mAudioRecord.read(buffer, 0, buffer.length);
345 return mAudioRecord.read(buffer, 0, buffer.length, blocking ? AudioRecord.READ_BLOCKING : AudioRecord.READ_NON_BLOCKING);
351 if (mAudioTrack != null) {
353 mAudioTrack.release();
360 if (mAudioRecord != null) {
362 mAudioRecord.release();
static native int nativeSetupJNI()
static int [] audioOpen(int sampleRate, int audioFormat, int desiredChannels, int desiredFrames)
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
static AudioRecord mAudioRecord
static int captureReadByteBuffer(byte[] buffer, boolean blocking)
static void audioWriteShortBuffer(short[] buffer)
static void captureClose()
static AudioTrack mAudioTrack
static int captureReadShortBuffer(short[] buffer, boolean blocking)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
static int [] open(boolean isCapture, int sampleRate, int audioFormat, int desiredChannels, int desiredFrames)
static int [] captureOpen(int sampleRate, int audioFormat, int desiredChannels, int desiredFrames)
static int captureReadFloatBuffer(float[] buffer, boolean blocking)
static void audioWriteFloatBuffer(float[] buffer)
static String getAudioFormatString(int audioFormat)
static void audioWriteByteBuffer(byte[] buffer)