Created
November 22, 2013 07:22
-
-
Save SuspendedPhan/7596139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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