I have a program that can generate sound, I can hear the sound sequence from my headphone, now I'm trying to save the generated sound to an .au file, it should have a few seconds of sound, but instead I just heard a short beep, seems it didn't record the whole sequence, can someone tell me why ? Here are some code segments :
static byte audioData[];
......
class Command_Demo_Morse_Code implements Command
{
JTextArea taText;
//The following are audio format parameters. They may be modified by the signal generator at runtime. Values allowed by Java SDK 1.4.1 are shown in comments.
float sampleRate=16000f; // Allowable 8000,11025,16000,22050,44100
int sampleSizeInBits=16; // Allowable 8,16
int channels=2; // Allowable 1,2
boolean signed=true; // Allowable true,false
boolean bigEndian=true; // Allowable true,false
Command_Demo_Morse_Code(JTextArea taText) { this.taText=taText; }
public void exec()
{
try
{
String Morse_Code,Word_Code_String="",Space=" ",Result_Morse_Code="",Output_English=""...
Input="01";
Input=Input.toUpperCase();
Word_Code_String+="\n Input = "+Input+"\nMorse_Code = \n";
Nm_Lib.audioData=new byte[16000*4*5]; // A buffer to hold two seconds monaural and one second stereo data at 16000 samp/sec for 16-bit samples [ 5 seconds ]
for (int i=0;i<Input.length();i++)
{
Morse_Code=Nm_Lib.Get_Morse_Code(Input.c...
Result_Morse_Code+=Morse_Code+Space;
taText.append("\n ["+Input.charAt(i)+"] = ["+Morse_Code.replace("\n"," ")+"]");
// Make_Morse_Code_Sound(String A_Morse_Code,int Pitch,int Speed) // Pitch : 1 - 10 Speed : 1 - 10
Nm_Lib.Make_Morse_Code_Sound(Morse_Code.... "),5,5,Nm_Lib.audioData);
}
Result_Morse_Code=Result_Morse_Code.repl...
Word_Code_String+=Result_Morse_Code;
taText.append(Word_Code_String+"\n\n");
Output_English=Nm_Lib.Get_English_From_M...
Nm_Lib.Out("English = "+Output_English);
taText.append("English = "+Output_English);
// Get the required audio format
AudioFormat audioFormat=new AudioFormat(sampleRate,sampleSizeInBits,...
// Get an audio input stream from the ByteArrayInputStream
AudioInputStream audioInputStream=new AudioInputStream(new ByteArrayInputStream(Nm_Lib.audioData),a...
//Write the data to an output file with the name provided by the text field in the South of the GUI.
AudioSystem.write(audioInputStream,Audio... File(Nm_Lib.Dir_Data+"Morse_Code_Sound.a... // ??? Save to sound file not working ?
}
catch (Exception e) { e.printStackTrace(); }
}
}
//====================================...
public static void Make_Sound(int Hz,int Milli_Seconds,byte[] synDataBuffer) throws LineUnavailableException
{
ByteBuffer byteBuffer=null;
ShortBuffer shortBuffer=null;
if (synDataBuffer!=null)
{
byteBuffer=ByteBuffer.wrap(synDataBuffer... // Prepare the ByteBuffer and the shortBuffer for use
shortBuffer=byteBuffer.asShortBuffer();
// int byteLength=synDataBuffer.length;
}
float Sample_Rate=8000f;
byte[] buf = new byte[1];
AudioFormat af = new AudioFormat(Sample_Rate,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0;i<Milli_Seconds*8;i++)
{
double angle=i/(Sample_Rate/Hz)*2.0*Math.PI;
buf[0]=(byte)(Math.sin(angle)*110.0);
sdl.write(buf,0,1);
if (synDataBuffer!=null) shortBuffer.put((short)(200*buf[0]));
// Out(i+" shortBuffer = "+shortBuffer.toString());
}
sdl.drain();
sdl.stop();
sdl.close();
}
......
public static void Make_Morse_Code_Sound(String A_Morse_Code,int Pitch,int Speed,byte[] synDataBuffer) // Pitch : 1 - 10 Speed : 1 - 10
{
Out("A_Morse_Code = "+A_Morse_Code); // [R] = [·-· ]
if (Speed<0) Speed=0;
else if (Speed>10) Speed=10;
int One_Unit_Time=100-9*Speed;
try
{
for (int i=0;i<A_Morse_Code.length();i++)
{
switch (A_Morse_Code.charAt(i))
{
case '·' : Make_Sound(300+60*Pitch,One_Unit_Time,sy... // 1. short mark, dot or 'dit' (·) — one unit long // '·' != '.'
case '-' : Make_Sound(300+60*Pitch,3*One_Unit_Time,... // 2. longer mark, dash or 'dah' (–) — three units long
case ' ' : Thread.sleep(7*One_Unit_Time);break; // 5. medium gap (between words) — seven units long
case '?' : Make_Sound(100+60*Pitch,5*One_Unit_Time,...
default : Make_Sound(100+60*Pitch,5*One_Unit_Time,...
}
Thread.sleep(One_Unit_Time); // 3. intra-character gap (between the dots and dashes within a character) — one unit long
}
Thread.sleep(3*One_Unit_Time); // 4. short gap (between letters) — three units long
}
catch (Exception e) { }
}
Tags: