Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Working audio implementation contributed by Joseph Lunderville

  • Loading branch information
slouken committed Jan 13, 2011
1 parent 974bb0d commit 0ac2e2aad13d7bf4a6e96485df0702e4b8ab8610
@@ -29,11 +29,15 @@
private static SDLSurface mSurface;

// Audio
private static Thread mAudioThread;
private static AudioTrack mAudioTrack;

// Load the .so
static {
System.loadLibrary("SDL");
//System.loadLibrary("SDL_image");
//System.loadLibrary("SDL_mixer");
//System.loadLibrary("SDL_ttf");
System.loadLibrary("main");
}

@@ -67,12 +71,13 @@ protected void onResume() {
// C functions we call
public static native void nativeInit();
public static native void nativeQuit();
public static native void onNativeResize(int x, int y, int format);
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeTouch(int action, float x,
float y, float p);
public static native void onNativeResize(int x, int y, int format);
public static native void onNativeAccel(float x, float y, float z);
public static native void nativeRunAudioThread();


// Java functions called from C
@@ -84,23 +89,83 @@ public static void flipBuffers() {
mSurface.flipEGL();
}

public static void updateAudio(byte [] buf) {
// Audio
private static Object buf;

if(mAudioTrack == null) {
// Hardcoded things are bad. FIXME when we have more sound stuff
// working properly.
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT,
2048,
AudioTrack.MODE_STREAM);
public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);

Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");

// Let the user pick a larger buffer if they really want -- but ye
// gods they probably shouldn't, the minimums are horrifyingly high
// latency already
desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);

mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);

audioStartThread();

Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + ((float)mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");

if (is16Bit) {
buf = new short[desiredFrames * (isStereo ? 2 : 1)];
} else {
buf = new byte[desiredFrames * (isStereo ? 2 : 1)];
}

mAudioTrack.write(buf, 0, buf.length);
mAudioTrack.play();
return buf;
}

public static void audioStartThread() {
mAudioThread = new Thread(new Runnable() {
public void run() {
mAudioTrack.play();
nativeRunAudioThread();
}
});

Log.v("SDL", "Played some audio");
// I'd take REALTIME if I could get it!
mAudioThread.setPriority(Thread.MAX_PRIORITY);
mAudioThread.start();
}

public static void audioWriteShortBuffer(short[] buffer) {
for (int i = 0; i < buffer.length; ) {
int result = mAudioTrack.write(buffer, i, buffer.length - i);
if (result > 0) {
i += result;
} else if (result == 0) {
try {
Thread.sleep(10);
} catch(InterruptedException e) {
// Nom nom
}
} else {
Log.w("SDL", "SDL audio: error return from write(short)");
return;
}
}
}

public static void audioWriteByteBuffer(byte[] buffer) {
for (int i = 0; i < buffer.length; ) {
int result = mAudioTrack.write(buffer, i, buffer.length - i);
if (result > 0) {
i += result;
} else if (result == 0) {
try {
Thread.sleep(10);
} catch(InterruptedException e) {
// Nom nom
}
} else {
Log.w("SDL", "SDL audio: error return from write(short)");
return;
}
}
}

}
@@ -279,7 +344,7 @@ public boolean initEGL() {

} catch(Exception e) {
Log.v("SDL", e + "");
for(StackTraceElement s : e.getStackTrace()) {
for (StackTraceElement s : e.getStackTrace()) {
Log.v("SDL", s.toString());
}
}
@@ -303,7 +368,7 @@ public void flipEGL() {

} catch(Exception e) {
Log.v("SDL", "flipEGL(): " + e);
for(StackTraceElement s : e.getStackTrace()) {
for (StackTraceElement s : e.getStackTrace()) {
Log.v("SDL", s.toString());
}
}

0 comments on commit 0ac2e2a

Please sign in to comment.