I'm supposed to implement an sinus-generator in java. As an input you give the frequency, amplitute and phase and as an output a .wav file is supposed to be generated.
private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
// total samples = (duration in second) * (samples per second)
byte[] sin = new byte[seconds * sampleRate];
double samplingInterval = (double) (sampleRate / frequencyOfSignal);
System.out.println("Sampling Frequency : "+sampleRate);
System.out.println("Frequency of Signal : "+frequencyOfSignal);
System.out.println("Sampling Interval : "+samplingInterval);
for (int i = 0; i < sin.length; i++) {
double angle = (2.0 * Math.PI * i) / samplingInterval;
sin[i] = (byte) (Math.sin(angle) * 127);
//System.out.println("" + sin[i]);
}
return sin;
}
I generated a sinus like this, but I have huge problems with creating a .wav out of this. I tried some libraries, but especially the header is giving me headache.
Any ideas of how I can implement this easily?