My android app is trying to generate white noise to play on an android device. Currently, I’m generating an array of random numbers that is AudioTrack.getMinBufferSize(…) shorts long, and looping through in a timer. It looks like this:
INTERVAL_MILLISECONDS = (int)((double)(bufSize) / (double)(nativeSampleRate * 2) * 1000);
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {
synchronized(this){
//System.out.println("playing. Sample rate is + " + nativeSampleRate + ", buffer is " + data.length + " shorts long, interval is " + INTERVAL_MILLISECONDS);
track.write(data, 0, bufSize);
track.play();
}
}
}, 0, INTERVAL_MILLISECONDS);
But when I listen to it on my android emulator, I get white noise with a very rapid “beating” or “bleating” rhythm that’s hard to describe. I’m not a sound engineer, can someone point me in the right direction about why this is happening?
************** UPDATE ************
I just did a couple of variations, one where I made sure my sample size was a whole number of milliseconds long, and one where I made my sample size a whole second long.
With a sample size of 8820 (five milliseconds long, and still over the 8192 minimum), the beating is unchanged.
However, with a sample size of 44100 (a whole second), the beating went away. Is it possible the timer I’m using just isn’t accurate at the small intervals?
The problem seems to be the way you’re generating white noise. I don’t know the first thing about programming on the android, but if I understand the question correctly you’re creating a single buffer’s worth of random data then cycling through it over and over again.
The problem, (as your update points out) is that this single buffer is repeating several times a second. This means it’s no longer ‘white’ (i.e. completely random) noise. It’s become periodic.
Your brain is very good at picking out periodic waveforms, hence why you hear this beating. It’s your brain telling you that there’s something going on other than the white noise. Here you’re introducing a strong component around 5.38Hz, which when modulating the rest of the audio you interpret as a “beating” effect.
The beating disappears (or decreases below perceptibility) when you increase the buffer length because at 44100 samples your brain no longer has enough to latch on to, and hence fails to recognise that the sequence is periodic. The low frequency information will still be there though.
Generating perfect white noise isn’t as straightforward as you might think!. In your case I think it’s best to make the noise buffer as long as possible – depending of course on the context of the application you’re building.