Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualising Android AudioTrack from a ByteStream

I'm currently using the AudioTrack to play back recorded music. This involves writing the track data to the audio buffer. How would I go about using the audio data stream to draw a wave form on the screen that represents the audio being played in real time? I've not used any advanced graphics on android before I'm not sure how to get started. Do I have to use openGL or can I implements a View? and also how do I convert the data to something useful to use for drawing.

like image 839
DeliveryNinja Avatar asked Jan 26 '11 23:01

DeliveryNinja


2 Answers

Your first step is to call setPositionNotificationPeriod(periodInFrames). This determines how often your application will receive an onPeriodicNotification call. So, if you're doing an oscilloscope-type visualization and you want to show 50 milliseconds worth of audio data on the screen at any given moment, you would use periodInFrames value of 2205 (assuming your WAV file is mono, 16 bits per sample and 44,100 Hz sample rate).

Inside your notification event, you can determine where the playback of your AudioTrack object currently is, and then get the corresponding slice of data from the original file or array. You then use then draw this slice of data to your view using ordinary 2D graphics methods (no need for openGL here). This answer has a C# sample for drawing a slice of audio data - easy to translate to java.

So your code will end up looking sort of like this:

audioTrack.setPositionNotificationPeriod(2205);
audioTrack.setPlaybackPositionUpdateListener(this);

...

public void onPeriodicNotification(AudioTrack track) {
    int pos = track.getNotificationMarkerPosition();
    short[] slice = Array.copy(_data, pos, _sliceSize) // pseudo-code
    // render the slice to the view
}
like image 86
MusiGenesis Avatar answered Sep 28 '22 05:09

MusiGenesis


Concerning that point, I would suggest that you use android.media.audiofx.Visualizer class that handles the generation of waveform arrays or frequency arrays. So the only thing you have to take care of is the drawing of the graphics.

You can use this class since the release of API 9.

like image 38
Heavyrage Avatar answered Sep 28 '22 05:09

Heavyrage