Package _tmp;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Java.awt.Toolkit;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.ArrayDeque;
Import Java.util.Deque;
Import Java.util.Iterator;
Import java.util.LinkedList;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import Javax.sound.sampled.AudioFormat;
Import Javax.sound.sampled.AudioInputStream;
Import Javax.sound.sampled.AudioSystem;
Import Javax.sound.sampled.SourceDataLine;
Import Javax.swing.JFrame;
Import javax.swing.SwingUtilities;
public class Soundtest {
public static class Waveformgraph extends JFrame {
Private deque<short> Deque = new linkedlist<short> ();
private timer timer;
Private Image buffered;
Private Image showing;
public waveformgraph (int width, int height) {
SetSize (width, height);
Timer = new timer ();
buffered = new BufferedImage (width, height, bufferedimage.type_4byte_abgr);
Timer.schedule (New TimerTask () {
@Override public void Run () {
Graphics g = buffered.getgraphics ();
G.setcolor (Color.White);
G.fillrect (0, 0, getwidth (), getheight ());
G.setcolor (Color.Black);
G.translate (GetHeight ()/2);
Synchronized (deque) {
float heightrate = 1;
if (Deque.size () > 1) {
Iterator<short> iter = Deque.iterator ();
Short P1 = Iter.next ();
Short P2 = Iter.next ();
int x1 = 0, x2 = 0;
while (Iter.hasnext ()) {
G.drawline (x1, (int) (p1*heightrate), x2, (int) (p2*heightrate));
P1 = p2;
P2 = Iter.next ();
x1 = x2;
x2 + 1;
}
}
}
G.dispose ();
Swingutilities.invokelater (New Runnable () {
@Override public void Run () {
showing = buffered;
Repaint ();
showing = null;
}
});
}
}, 100, 100);
}
@Override
public void Paint (Graphics g) {
Super.paint (g);
if (buffered!=null) {
G.drawimage (buffered, 0, 0, NULL);
}
}
public void put (short v) {
Synchronized (deque) {
Deque.add (v);
if (Deque.size () > 500) {
Deque.removefirst ();
}
}
}
public void Clear () {
Deque.clear ();
}
}
public static void Main (string[] args) throws Exception {
Record ();
Waveformgraph waveformgraph = new Waveformgraph (500, 300);
Waveformgraph.setdefaultcloseoperation (Jframe.exit_on_close);
Waveformgraph.setvisible (TRUE);
Audioinputstream AIS = Audiosystem.getaudioinputstream (New File ("C:\\Documents and Settings\\wml\\my Documents\\My music\\ su sa-insomnia. wav "));
PrintFormat (Ais.getformat ());
Sourcedataline player = Audiosystem.getsourcedataline (Ais.getformat ());
Player.open ();
Player.start ();
byte[] buf = new Byte[4];
int Len;
while ((Len=ais.read (BUF))!=-1) {
if (Ais.getformat (). Getchannels () = = 2) {
if (Ais.getformat (). Getsamplerate () = = 16) {
Waveformgraph.put ((short) ((Buf[1] << 8) | buf[0])//left channel
Waveformgraph.put ((short) ((Buf[3] << 8) | buf[2))/right channel
} else {
Waveformgraph.put (buf[1]);//left channel
Waveformgraph.put (buf[3]);//left channel
Waveformgraph.put (buf[2]);/Right Channel
Waveformgraph.put (buf[4]);/Right Channel
}
} else {
if (Ais.getformat (). Getsamplerate () = = 16) {
Waveformgraph.put ((short) ((Buf[1] << 8) | buf[0]);
Waveformgraph.put ((short) ((Buf[3] << 8) | buf[2]);
} else {
Waveformgraph.put (buf[1]);
Waveformgraph.put (buf[2]);
Waveformgraph.put (Buf[3]);
Waveformgraph.put (Buf[4]);
}
}
Player.write (buf, 0, Len);
}
Player.close ();
Ais.close ();
}
public static void PrintFormat (Audioformat format) {
System.out.println (format.getencoding () + "=>"
+ format.getsamplerate () + "Hz,"
+ format.getsamplesizeinbits () + "bit,"
+ format.getchannels () + "channel,"
+ format.getframerate () + "Frames/second,"
+ format.getframesize () + "Bytes/frame");
}
public static void record () throws Lineunavailableexception,
interruptedexception {
Audioformat Audioformat = new Audioformat (AudioFormat.Encoding.PCM_SIGNED, 48000F, 1, 2, 48000F, false);
Info recorddevinfo = new Dataline.info (Targetdataline.class, Audioformat);
//
Final Targetdataline recordline = (targetdataline) audiosystem.getline (recorddevinfo);
Final Sourcedataline playline = Audiosystem.getsourcedataline (Audioformat);
//
Recordline.open (Audioformat, Recordline.getbuffersize ());
Playline.open (Audioformat, Recordline.getbuffersize ());
//
Thread recorder = new Thread () {
public void Run () {
Recordline.start ();
Playline.start ();
//
Floatcontrol FC = (Floatcontrol) Playline.getcontrol (FloatControl.Type.MASTER_GAIN);
Double value = 2;
float DB = (float) (Math.log (value = = 0.0? 0.0001:value)/Math.log (10.0) * 20.0);
Fc.setvalue (DB);
//
try {
Audioinputstream in = new Audioinputstream (recordline);
byte[] buf = new byte[recordline.getbuffersize ()];
int Len;
while ((Len=in.read (BUF))!=-1) {
Playline.write (buf, 0, Len);
// }
catch (IOException e) {
E.printstacktrace ();
finally {
Recordline.stop ();
Playline.stop ();
// }
// };
// };
Recorder.start ();
Recorder.join ();
// }
}