複製程式碼如下:
包_tmp;
導入 java.awt.BorderLayout;
導入java.awt.Color;
導入java.awt.Graphics;
導入java.awt.Image;
導入java.awt.Toolkit;
導入java.awt.image.BufferedImage;
導入java.io.File;
導入java.io.IOException;
導入 java.util.ArrayDeque;
導入java.util.Deque;
導入 java.util.Iterator;
導入 java.util.LinkedList;
導入java.util.Timer;
導入java.util.TimerTask;
導入 javax.sound.sampled.AudioFormat;
導入 javax.sound.sampled.AudioInputStream;
導入 javax.sound.sampled.AudioSystem;
導入 javax.sound.sampled.SourceDataLine;
導入 javax.swing.JFrame;
導入 javax.swing.SwingUtilities;
公共類別聲音測試{
公用靜態類別 WaveformGraph 擴充 JFrame {
私有 Deque<Short> deque = new LinkedList<Short>();
私人定時器計時器;
緩衝的私有圖像;
私人圖像展示;
公共 WaveformGraph(int 寬度, int 高度) {
設定大小(寬度,高度);
計時器=新計時器();
buffered = new BufferedImage(寬度, 高度, BufferedImage.TYPE_4BYTE_ABGR);
定時器.schedule(new TimerTask() {
@Override public void run() {
圖形 g = buffered.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.translate(10, getHeight()/2);
同步(雙端隊列){
浮動高度率 = 1;
if(deque.size() > 1) {
Iterator<Short> iter = deque.iterator();
短 p1 = iter.next();
短 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() {
顯示 = 緩衝;
重繪();
顯示=空;
}
});
}
}, 100, 100);
}
@覆蓋
公共無效油漆(圖形g){
超級.paint(g);
如果(緩衝!=空){
g.drawImage(緩衝, 0, 0, null);
}
}
公共無效放(短v){
同步(雙端隊列){
雙端隊列.add(v);
if(deque.size() > 500) {
deque.removeFirst();
}
}
}
公共無效清除(){
雙端隊列.clear();
}
}
公共靜態無效主(字串[] args)拋出異常{
//記錄();
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//我的文件//我的音樂//蘇仨 - 駝背症.wav"));
printFormat(ais.getFormat());
SourceDataLine 播放器 = AudioSystem.getSourceDataLine(ais.getFormat());
播放器.open();
播放器.start();
位元組 [] buf = 新位元組 [4];
int 長度;
while((len=ais.read(buf))!=-1) {
if(ais.getFormat().getChannels() == 2) {
if(ais.getFormat().getSampleRate() == 16) {
waveformGraph.put((short) ((buf[1] << 8) | buf[0]));//左聲道
//waveformGraph.put((short) ((buf[3] << 8) | buf[2]));//右聲道
} 別的 {
waveformGraph.put(buf[1]);//左聲道
waveformGraph.put(buf[3]);//左聲道
//waveformGraph.put(buf[2]);//右聲道
//waveformGraph.put(buf[4]);//右聲道
}
} 別的 {
if(ais.getFormat().getSampleRate() == 16) {
waveformGraph.put((短) ((buf[1] << 8) | buf[0]));
waveformGraph.put((短) ((buf[3] << 8) | buf[2]));
} 別的 {
波形圖.put(buf[1]);
波形圖.put(buf[2]);
波形圖.put(buf[3]);
波形圖.put(buf[4]);
}
}
玩家.write(buf, 0, len);
}
玩家.關閉();
ais.close();
}
公共靜態無效 printFormat(音訊格式格式){
System.out.println(format.getEncoding() + " => "
+ format.getSampleRate()+" hz, "
+ format.getSampleSizeInBits() + “位,”
+ format.getChannels() + “頻道,”
+ format.getFrameRate() + “幀/秒,”
+ format.getFrameSize() + "位元組/幀");
}
//public static void record() 拋出 LineUnavailableException,
//中斷異常{
//AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000F, 16, 1, 2, 48000F, false);
//資訊 recordDevInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
//
//最終TargetDataLine recordLine = (TargetDataLine) AudioSystem.getLine(recordDevInfo);
//最終的SourceDataLine playLine = AudioSystem.getSourceDataLine(audioFormat);
//
//recordLine.open(audioFormat, recordLine.getBufferSize());
//playLine.open(audioFormat, recordLine.getBufferSize());
//
//線程記錄器 = new Thread() {
//公共無效運行(){
//recordLine.start();
//playLine.start();
//
//FloatControl fc = (FloatControl) playLine.getControl(FloatControl.Type.MASTER_GAIN);
//雙精度值 = 2;
//float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
//fc.setValue(dB);
//
//嘗試 {
//AudioInputStream in = new AudioInputStream(recordLine);
//byte[] buf = new byte[recordLine.getBufferSize()];
//int 長度;
// while((len=in.read(buf)) != -1) {
//playLine.write(buf, 0, len);
//}
//} catch (IOException e) {
//e.printStackTrace();
//} 最後 {
//recordLine.stop();
//playLine.stop();
//}
//};
//};
//錄音機.start();
//記錄器.join();
//}
}