复制代码代码如下:
paket _tmp;
impor java.awt.BorderLayout;
impor java.awt.Color;
impor java.awt.Grafik;
impor java.awt.Gambar;
impor java.awt.Toolkit;
impor java.awt.image.BufferedImage;
impor java.io.File;
impor java.io.IOException;
impor java.util.ArrayDeque;
import java.util.Deque;
impor java.util.Iterator;
impor java.util.LinkedList;
impor java.util.Timer;
impor java.util.TimerTask;
impor javax.sound.sampled.AudioFormat;
impor javax.sound.sampled.AudioInputStream;
impor javax.sound.sampled.AudioSystem;
impor javax.sound.sampled.SourceDataLine;
impor javax.swing.JFrame;
impor javax.swing.SwingUtilities;
Tes Suara kelas publik {
kelas statis publik WaveformGraph memperluas JFrame {
pribadi Deque<Short> deque = new LinkedList<Short>();
pengatur waktu pribadi;
Gambar pribadi di-buffer;
Pertunjukan Gambar pribadi;
Grafik Bentuk Gelombang publik(int lebar, int tinggi) {
setUkuran(lebar, tinggi);
pengatur waktu = pengatur waktu baru();
buffered = new BufferedImage(lebar, tinggi, BufferedImage.TYPE_4BYTE_ABGR);
timer.jadwal(TimerTask baru() {
@Override public void run() {
Grafik g = buffered.getGraphics();
g.setColor(Warna.PUTIH);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Warna.HITAM);
g.translate(10, getHeight()/2);
disinkronkan (deque) {
tingkat ketinggian float = 1;
if(deque.ukuran() > 1) {
Iterator<Pendek> iter = deque.iterator();
P1 pendek = iter.next();
P2 pendek = iter.next();
int x1 = 0, x2 = 0;
while(iter.hasNext()) {
g.drawLine(x1, (int)(p1*tinggiRate), x2, (int)(p2*tinggiRate));
hal1 = hal2;
p2 = iter.next();
x1 = x2;
x2 += 1;
}
}
}
g.buang();
SwingUtilities.invokeLater(runnable baru() {
@Override public void run() {
menampilkan = di-buffer;
mengecat ulang();
menampilkan = nol;
}
});
}
}, 100, 100);
}
@Mengesampingkan
cat kekosongan publik(Grafik g) {
super.cat(g);
if(buffer!=null) {
g.drawImage(buffer, 0, 0, null);
}
}
public void put(v pendek) {
disinkronkan (deque) {
deque.tambahkan(v);
if(deque.ukuran() > 500) {
deque.removeFirst();
}
}
}
kekosongan publik jelas() {
deque.clear();
}
}
public static void main(String[] args) melempar Pengecualian {
//catatan();
WaveformGraph waveformGraph = Grafik Gelombang baru(500, 300);
waveformGraph.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
waveformGraph.setVisible(benar);
AudioInputStream ais = AudioSystem.getAudioInputStream(File baru("C://Documents and Settings//wml//My Documents//My Music//苏仨 - 失眠症.wav"));
printFormat(ais.getFormat());
Pemutar SourceDataLine = AudioSystem.getSourceDataLine(ais.getFormat());
pemain.open();
pemain.mulai();
byte[] buf = byte baru[4];
ke dalam len;
while((len=ais.baca(buf))!=-1) {
if(ais.getFormat().getChannels() == 2) {
jika(ais.getFormat().getSampleRate() == 16) {
waveformGraph.put((short) ((buf[1] << 8) | buf[0]));//左声道
//waveformGraph.put((pendek) ((buf[3] << 8) | buf[2]));//右声道
} kalau tidak {
waveformGraph.put(buf[1]);//左声道
waveformGraph.put(buf[3]);//左声道
//waveformGraph.put(buf[2]);//右声道
//waveformGraph.put(buf[4]);//右声道
}
} kalau tidak {
jika(ais.getFormat().getSampleRate() == 16) {
waveformGraph.put((short) ((buf[1] << 8) | buf[0]));
waveformGraph.put((short) ((buf[3] << 8) | buf[2]));
} kalau tidak {
waveformGraph.put(buf[1]);
waveformGraph.put(buf[2]);
waveformGraph.put(buf[3]);
waveformGraph.put(buf[4]);
}
}
pemain.tulis(buf, 0, len);
}
pemain.close();
ais.close();
}
public static void printFormat(format AudioFormat) {
Sistem.keluar.println(format.getEncoding() + " => "
+ format.getSampleRate()+" hz, "
+ format.getSampleSizeInBits() + " sedikit, "
+ format.getChannels() + " saluran, "
+ format.getFrameRate() + " frame/detik, "
+ format.getFrameSize() + " byte/bingkai");
}
//catatan kekosongan statis publik() menampilkan LineUnavailableException,
//InterruptedException {
//AudioFormat audioFormat = AudioFormat baru(AudioFormat.Encoding.PCM_SIGNED, 48000F, 16, 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());
//
//Perekam thread = Thread baru() {
//jalankan kekosongan publik() {
//recordLine.start();
//playLine.mulai();
//
//FloatControl fc = (FloatControl) playLine.getControl(FloatControl.Type.MASTER_GAIN);
//nilai ganda = 2;
//float dB = (float) (Math.log(nilai == 0,0 ? 0,0001 : nilai) / Math.log(10.0) * 20.0);
//fc.setValue(dB);
//
//mencoba {
//AudioInputStream di = AudioInputStream baru(recordLine);
//byte[] buf = byte baru[recordLine.getBufferSize()];
//int len;
//sementara((len=dalam.baca(buf)) != -1) {
//playLine.write(buf, 0, len);
//}
//} tangkapan (IOException e) {
//e.printStackTrace();
//} Akhirnya {
//recordLine.stop();
//playLine.stop();
//}
//};
//};
//perekam.mulai();
//perekam.join();
//}
}