Google Android Development Agency SASS
Showing posts with label audio. Show all posts
Showing posts with label audio. Show all posts

Friday, 27 November 2009

Android AudioTrack source

I've been doing a lot of work optimising our audio playback on Tub Thumper Pro for Android this week and I constantly find myself referring back to the C++ source for the OS to help solve latency issues.

For anyone doing work with the AudioTrack object, it's worthwhile bookmarking the source online so that you can browse it whenever you hit a problem.

Here's two links I find really useful:

Incidentally, I've found that the best way of getting low-latency polyphonic audio is to subclass AudioTrack and call finalize() yourself after you're done playing; I was seeing more "write blocked for xx seconds" messages before I did this, although it may be co-incidental.

Monday, 23 November 2009

Low latency audio playback in Android

One of the things which bugged me immensely about Android was it's lack of low-latency audio playback and a low-level audio API.

Unlike the iPhone, Android developers were stuck with high-level media player objects which basically only allowed you to chuck an MP3/OGG/Wav their way and hope for the best.

Well not any more my robot loving amigos!

After pulling my hair out trying different designs and playback algorithms working on the sequencing function of Tub Thumper Pro for Android, I finally found what I'd been looking for; an object for working with sample-level data, just like we've been doing on the iPhone.

android.Media.AudioTrack

I mean, it's not perfect, but here's what we're doing to run samples directly to the hardware without all of that mediaplayer-esque overhead (I've edited it to make it make sense outside of our app, but the theory is there):


//--load the sound file from the resource identifier into a byte array (c is Context)
InputStream s = c.getResources().openRawResource(resourceFile);
soundData = new byte[s.available()];
s.read(soundData);

//--setup an AudioTrack object, this one is running at 44.1Khz which worked best for us.
AudioTrack oTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT, intSize,AudioTrack.MODE_STREAM);

//--turn the audiotrack on
oTrack.play();
//--write something to the audio track, in this case the entire buffer
int playVal = oTrack.write(soundData,0,soundData.length);
//--we're turning it off here
oTrack.stop();

Tuesday, 19 May 2009

MIDI Playback in Android Cupcake

If, like me, you've been wondering why the audio support in Android was so poor, you'll be pleased to hear that Android 1.5 (Cupcake) addresses some of the issues.

I'll write a more in-depth post once I've gone through some real world examples, but one thing which I'm currently reading up on is the in-built MIDI support.

Rather than just stick a bog-standard MIDI engine in Android, the Google engineers have chosen to include a JET playback engine, which allows musical synchronisation of clips and seamless playback. It's mainly tailored towards game playing as it allows you to script the clips about to played back to tie in with the gameplay, but I'm already thinking up some cool musical ideas for it.

Have a read of the documentation on Android.com, at http://developer.android.com/guide/topics/media/jet/jetcreator_manual.html