复制代码代码如下:
pacote _tmp;
importar java.awt.BorderLayout;
importar java.awt.Color;
importar java.awt.Graphics;
importar java.awt.Image;
importar java.awt.Toolkit;
importar java.awt.image.BufferedImage;
importar java.io.File;
importar java.io.IOException;
importar java.util.ArrayDeque;
importar java.util.Deque;
importar java.util.Iterator;
importar java.util.LinkedList;
importar java.util.Timer;
importar java.util.TimerTask;
importar javax.sound.sampled.AudioFormat;
importar javax.sound.sampled.AudioInputStream;
importar javax.sound.sampled.AudioSystem;
importar javax.sound.sampled.SourceDataLine;
importar javax.swing.JFrame;
importar javax.swing.SwingUtilities;
classe pública SoundTest {
classe estática pública WaveformGraph estende JFrame {
private Deque<Short> deque = new LinkedList<Short>();
temporizador privado;
imagem privada em buffer;
exibição de imagem privada;
public WaveformGraph(int largura, int altura) {
setSize(largura,altura);
temporizador = novo temporizador();
buffered = novo BufferedImage(largura, altura, BufferedImage.TYPE_4BYTE_ABGR);
timer.schedule(new TimerTask() {
@Override public void run() {
Gráficos g = buffered.getGraphics();
g.setColor(Cor.BRANCO);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.translate(10, getHeight()/2);
sincronizado (deque) {
taxa de altura flutuante = 1;
if(deque.size() > 1) {
Iterador<Curto> iter = deque.iterator();
P1 curto = iter.next();
P2 curto = iter.next();
int x1 = 0, x2 = 0;
enquanto(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() {
mostrando = em buffer;
repintar();
mostrando = nulo;
}
});
}
}, 100, 100);
}
@Substituir
public void paint(Gráficos g) {
super.paint(g);
if(buffered!=nulo) {
g.drawImage(bufferizado, 0, 0, nulo);
}
}
público void put(v curto) {
sincronizado (deque) {
deque.add(v);
if(deque.size() > 500) {
deque.removeFirst();
}
}
}
público vazio claro() {
deque.clear();
}
}
public static void main(String[] args) lança exceção {
//registro();
WaveformGraph waveformGraph = novo 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//苏仨 - 失眠症.wav"));
printFormat(ais.getFormat());
SourceDataLine player = AudioSystem.getSourceDataLine(ais.getFormat());
jogador.open();
jogador.start();
byte[] buf = novo byte[4];
interno;
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]));//右声道
} outro {
waveformGraph.put(buf[1]);//左声道
waveformGraph.put(buf[3]);//左声道
//waveformGraph.put(buf[2]);//右声道
//waveformGraph.put(buf[4]);//右声道
}
} outro {
if(ais.getFormat().getSampleRate() == 16) {
waveformGraph.put((curto) ((buf[1] << 8) | buf[0]));
waveformGraph.put((curto) ((buf[3] << 8) | buf[2]));
} outro {
waveformGraph.put(buf[1]);
waveformGraph.put(buf[2]);
waveformGraph.put(buf[3]);
waveformGraph.put(buf[4]);
}
}
jogador.write(buf, 0, len);
}
jogador.close();
ais.close();
}
public static void printFormat(formato AudioFormat) {
System.out.println(format.getEncoding() + " => "
+ formato.getSampleRate()+"hz, "
+ format.getSampleSizeInBits() + " bit, "
+ format.getChannels() + " canal, "
+ format.getFrameRate() + " quadros/segundo, "
+ format.getFrameSize() + "bytes/quadro");
}
//public static void record() lança LineUnavailableException,
//InterruptedException{
//AudioFormat audioFormat = new AudioFormat(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());
//
//Gravador de thread = new Thread() {
//public void run() {
//recordLine.start();
//playLine.start();
//
//FloatControl fc = (FloatControl) playLine.getControl(FloatControl.Type.MASTER_GAIN);
//valor duplo = 2;
//float dB = (float) (Math.log(valor == 0,0? 0,0001: valor) / Math.log(10,0) * 20,0);
//fc.setValue(dB);
//
//tentar {
//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();
//} finalmente {
//recordLine.stop();
//playLine.stop();
//}
//};
//};
//recorder.start();
//recorder.join();
//}
}