Scenario: There is a imageview which implements ontouchlistener. When the user touches on the screen i am starting a separate thread which will be generating sin wave and i am writing it to the audiotrack here is the code of generation of sin wave.
t = new Thread() {
public void run() {
setPriority(Thread.MAX_PRIORITY);
// set the buffer size
int buffsize = AudioTrack.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
// create an audiotrack object
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
samplerate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffsize,
AudioTrack.MODE_STREAM);
float amp = (float)10000;
double twopi = 2*Math.PI;
// start audio
audioTrack.play();
short samples[] = new short[buffsize];
// synthesis loop
while(isRunning){
for(int i=0; i < buffsize; i++){
samples[i] = (short) (amp*Math.sin(ph));
ph += (twopi*fr)/sr;
if (ph > twopi)
{
ph -= twopi;
}
if(isRunning==false)
break;
}
if(isRunning==false)
break;
audioTrack.write(samples, 0, buffsize);
}
audioTrack.stop();
audioTrack.release();
}
};
As the user moves his hand on ACTION_MOVE call back i am changing the frequency . Frequency mainly varies on the distance moved by the user from the initial point. isRunnig will become true on ACTION_UP callback.
Problem: I am missing the some of the frequencies. I mean if the user moves his hand very fastly some of the frequency values are getting missed. This is mainly because of audioTrack.write(samples, 0, buffsize) This function takes some time.. i.e if i get 4 time ACTION_MOVE callback, 4 points i will get, 4 time the frequency gets varied. But audio track gets updated 2 times. What i have to do to remove this lag. Is there any better approach to do this.