The following was taken from an Android app:
public void genTone(int freq){
for(int i = 0; i<numSamples; i++){
samples[i] = Math.pow(-1, (float)(i / (sampleRate/freq)));
}
int idx = 0;
int volume = 32767 * cx/wide;
for (final double dVal : samples) {
final short val = (short) ((dVal+1) * volume);
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
if(isRecording){
toRec.add((byte)(val & 0x00ff));
toRec.add((byte)((val & 0xff00) >>> 8));
}
}
}
The above code is a Java function in an Android app the generate a square wave with a specified frequency. The frequency is determined by an integer 'note' which is the last recorded position of a MotionEvent divided by the height of the screen. The frequency is 440 * 2^(note/12). I've made the program output in text the note and frequency, and it outputs what I want it to, but at certain notes, even though it outputs a different frequency in text, it sounds exactly the same. Is 8000 too low a sampleRate(per second)? Is this a well-known bug? Anything you can help me with?