Hi
I'm trying to do a simple realtime/streaming sinewave generator using
AudioTrack, but i havent found any samples on how to use the
AudioTrack class in such a way that it seems possible, i'm assuming
it's possible to generate data on the fly and write it out as audio.
I've tried calling write() within onPeriodicNotification callback, or
using a thread that just loops and pushes data into write(), niether
of them seems to work the way i expect. the write method seems to be
synchronous therefore there's a small gap in the playback between each
write, when generating the next buffer.
Im running out of ideas, should i use a static buffer and call
reloadStaticData() after i written data to the buffer? or is there any
polling/callback way of doing this?
this is the simple sinewave generator used, it just generates a bunch
of data and writes it onto the track:
private float phase;
private float freqphase;
public void fillBuffer( AudioTrack track, int samples )
{
double hz = 700.0 + Math.sin( freqphase ) * 400.0;
Log.i(TAG, "fillBuffer called, "+samples+" samples, start at "+hz+"
Hz.");
// tv.setText("hz="+hz);
byte[] data = new byte[samples];
for( int j=0; j<samples; j++ )
{
hz = 700.0 + Math.sin( freqphase ) * 400.0;
data[j] = (byte)(128.0 + 127.0*Math.sin( phase * Math.PI /
22050.0 ));
phase += hz;
freqphase += 0.00001;
}
track.write(data, 0, samples);
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---