复制代码代码如下 :
paquet _tmp ;
importer java.awt.BorderLayout ;
importer java.awt.Color ;
importer java.awt.Graphics ;
importer java.awt.Image ;
importer java.awt.Toolkit ;
importer java.awt.image.BufferedImage ;
importer java.io.File ;
importer java.io.IOException ;
importer java.util.ArrayDeque ;
importer java.util.Deque ;
importer java.util.Iterator ;
importer java.util.LinkedList ;
importer java.util.Timer ;
importer java.util.TimerTask ;
importer javax.sound.sampled.AudioFormat ;
importer javax.sound.sampled.AudioInputStream ;
importer javax.sound.sampled.AudioSystem ;
importer javax.sound.sampled.SourceDataLine ;
importer javax.swing.JFrame ;
importer javax.swing.SwingUtilities ;
classe publique SoundTest {
la classe statique publique WaveformGraph étend JFrame {
private Deque<Short> deque = new LinkedList<Short>();
minuterie privée;
image privée mise en mémoire tampon ;
Affichage d'images privées ;
public WaveformGraph (int largeur, int hauteur) {
setSize(largeur, hauteur);
minuterie = nouveau Minuteur();
buffered = new BufferedImage(largeur, hauteur, BufferedImage.TYPE_4BYTE_ABGR);
timer.schedule (nouveau TimerTask() {
@Override public void run() {
Graphiques g = buffered.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.translate(10, getHeight()/2);
synchronisé (deque) {
taux de hauteur flottant = 1 ;
si(deque.size() > 1) {
Itérateur<Short> iter = deque.iterator();
Court p1 = iter.next();
Court 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() {
affichage = tamponné ;
repeindre();
affichage = nul ;
}
});
}
}, 100, 100);
}
@Outrepasser
peinture vide public (Graphiques g) {
super.paint(g);
si(buffered!=null) {
g.drawImage(buffered, 0, 0, null);
}
}
public void put (court v) {
synchronisé (deque) {
deque.add(v);
si(deque.size() > 500) {
deque.removeFirst();
}
}
}
public vide clair() {
deque.clear();
}
}
public static void main (String[] args) lève une exception {
//enregistrer();
WaveformGraph waveformGraph = nouveau 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());
Lecteur SourceDataLine = AudioSystem.getSourceDataLine(ais.getFormat());
joueur.open();
joueur.start();
octet[] buf = nouvel octet[4];
int lentille ;
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]));//右声道
} autre {
waveformGraph.put(buf[1]);//左声道
waveformGraph.put(buf[3]);//左声道
//waveformGraph.put(buf[2]);//右声道
//waveformGraph.put(buf[4]);//右声道
}
} autre {
if(ais.getFormat().getSampleRate() == 16) {
waveformGraph.put((short) ((buf[1] << 8) | buf[0]));
waveformGraph.put((short) ((buf[3] << 8) | buf[2]));
} autre {
waveformGraph.put(buf[1]);
waveformGraph.put(buf[2]);
waveformGraph.put(buf[3]);
waveformGraph.put(buf[4]);
}
}
joueur.write(buf, 0, len);
}
joueur.close();
ais.close();
}
public static void printFormat (format AudioFormat) {
System.out.println(format.getEncoding() + " => "
+ format.getSampleRate()+" hz, "
+ format.getSampleSizeInBits() + " bit, "
+ format.getChannels() + " canal, "
+ format.getFrameRate() + " images/seconde, "
+ format.getFrameSize() + " octets/image");
}
// l'enregistrement vide statique public () lève une exception LineUnavailableException,
//Exception interrompue {
//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());
//
//Enregistreur de thread = nouveau Thread() {
// exécution publique vide () {
//recordLine.start();
//playLine.start();
//
//FloatControl fc = (FloatControl) playLine.getControl(FloatControl.Type.MASTER_GAIN);
//valeur double = 2;
//float dB = (float) (Math.log(value == 0.0 ? 0.0001 : valeur) / Math.log(10.0) * 20.0);
//fc.setValue(dB);
//
//essayer {
//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();
//} enfin {
//recordLine.stop();
//playLine.stop();
//}
//;
//;
//enregistreur.start();
//enregistreur.join();
//}
}