Ok so I have a frequency generator which uses AudioTrack to send PCM
data to the hardware. Here's the code I'm using for that:
private class playSoundTask extends AsyncTask<Void, Void, Void> {
float frequency;
float increment;
float angle = 0;
short samples[] = new short[1024];
@Override
protected void onPreExecute() {
int minSize = AudioTrack.getMinBufferSize( 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT );
track = new AudioTrack( AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minSize, AudioTrack.MODE_STREAM);
track.play();
}
@Override
protected Void doInBackground(Void... params) {
while( Main.this.isPlaying) {
frequency = (float)Main.this.slider.getProgress();
increment = (float)(2*Math.PI) * frequency / 44100;
for( int i = 0; i < samples.length; i++ ) {
samples[i] = (short)((float)Math.sin( angle )*Short.MAX_VALUE);
angle += increment;
}
track.write(samples, 0, samples.length);
}
return null;
}
}
The frequency is tied to a slide bar, and the correct value is being
reported in the while loop. Everything is fine and dandy when I start
the app. When you drag your finger along the slide bar you get a nice
sweeping sound. But after around 10 seconds of playing around with it,
the audio starts to get wonky. If a small change is made to the
frequency, either nothing will happen, or it will change a few whole
seconds later. This made me think that it was a buffering issue, that
I had generated too much data and it had to get to the new samples.
But if the change is large enough (usually over 1500 Hz), then it
works, but there's a noticable staggered sound, not a clean sweep.
Does anyone see a problem with my code, or know of an issue that would
be causing this?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" 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-developers?hl=en