I researched Sine wave generating on Android for a long time and I got some sample code online. However, I still got in confusion when I code by myself. I use the code like following
double[] buf = new double[(int) sampleRate];
byte[] buffer = new byte[(int) sampleRate];
double sampleInterval = sampleRate/frequency;
for(int i=0; i<sampleRate;i++){
buf[i] = (Math.sin((2.0*Math.PI*i)/sampleInterval));
buffer[i] = (byte)(buf[i]*Byte.MAX_VALUE);
}
while(flag) {
audioTrack.write(buffer, 0, buffer.length);
}
- I there is too much noise when I play the sine wave buffer generated by the formula "Math.sin((2.0*Math.PI*i)/sampleInterval)". I don't know it is the problem of Java & Android or my coding. Both on the simulator and my real Sony Phone showed the noise. But the same formula with Matlab on my classmate's laptop can work well. I have also tried to use DataSourceLine in desktop java code. The noise also exists.
2.The sound generated tend to be unstable, sometimes it is continuous, but sometimes it can be intermittent with obvious regular ups and downs in the sound.
How can I improve my code to makes it stable and accurate?