Created
October 24, 2012 12:51
-
-
Save misto/3945882 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Für die Erzeugung der Töne brauchen wir zwei Parameter: die Länge eines | |
* Tons sowie die Sampling Rate, also die Abtastrate, mit der wir die Töne | |
* erstellen: http://en.wikipedia.org/wiki/Sampling_rate | |
*/ | |
private final int duration = 1; // seconds | |
private final int sampleRate = 8000; | |
/** | |
* Erstellt einen Byte-Array mit den Soundinformationen die wir für das | |
* abspielen eines Tons brauchen. | |
* | |
* @param freqOfTone | |
* Die Frequenz des gewünschten Tons. | |
*/ | |
byte[] createTone(int freqOfTone) { | |
final int numSamples = duration * sampleRate; | |
double sample[] = new double[numSamples]; | |
for (int i = 0; i < numSamples; ++i) { | |
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate / freqOfTone)); | |
} | |
/* | |
* Die "rohen" Sounddaten müssen wir nun noch ins 16Bit PCM Format | |
* umwandeln. | |
*/ | |
int idx = 0; | |
final byte generatedSnd[] = new byte[2 * numSamples]; | |
for (double dVal : sample) { | |
short val = (short) (dVal * 32767); | |
generatedSnd[idx++] = (byte) (val & 0x00ff); | |
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8); | |
} | |
return generatedSnd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment