Skip to content

Instantly share code, notes, and snippets.

@SuspendedPhan
Created November 22, 2013 07:22
Show Gist options
  • Save SuspendedPhan/7596139 to your computer and use it in GitHub Desktop.
Save SuspendedPhan/7596139 to your computer and use it in GitHub Desktop.
void genTone(){
// fill out the array
for (int i = 0; i < numSamples; ++i) {
// sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
sample[i] = Math.sin((2 * Math.PI - .001) * i / (sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
int ramp = numSamples / 20;
for (int i = 0; i < ramp; i++) {
// scale to maximum amplitude
final short val = (short) ((sample[i] * 32767) * i / ramp);
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
for (int i = ramp; i < numSamples - ramp; i++) {
// scale to maximum amplitude
final short val = (short) ((sample[i] * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
for (int i = numSamples - ramp; i < numSamples; i++) {
// scale to maximum amplitude
final short val = (short) ((sample[i] * 32767) * (numSamples - i) / ramp);
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment