Gemme lyd i java
Jeg bruger denne kode til at lave en beeplyd (til et morsesystem). Lyden bliver afspillet fint, mangler bare en måde at gemme wav-filen på ?import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
public class Main
{
public static byte[] calcSin(int hz)
{
int samples = (int)44100 / hz;
byte sinCurve[] = new byte[samples];
for(int i=0;i<sinCurve.length;i++)
{
sinCurve[i] = (byte)(Math.sin(i)*250);
}
return sinCurve;
}
public static void main(String args[])
{
byte sinCurve[] = calcSin(440);
try
{
AudioFormat outFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info lineInfo = new DataLine.Info( SourceDataLine.class, outFormat, 14112);
SourceDataLine sourceLine = (SourceDataLine)AudioSystem.getLine(lineInfo);
sourceLine.open(outFormat, sourceLine.getBufferSize());
sourceLine.start();
for(int i=0;i<2000;i++)//skriv sinus 2000 gange
{
sourceLine.write(sinCurve, 0, sinCurve.length);
}
sourceLine.stop();
sourceLine.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}