文件名称:java绘制音频波形图
文件大小:4KB
文件格式:JAVA
更新时间:2012-08-17 16:28:57
音频波形图
使用jdk16编译
import java.awt.Graphics;
import java.awt.GridLayout;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
/**
*
*/
/**
* @author Administrator
*
*/
public class Musicline extends JFrame implements Runnable {
private byte[] audioData = null;
private int intBytes = 0;
private byte[] ml = new byte[1];
private int[] drawl = null;
/** Creates new form Musicline */
public Musicline() {
initComponents();
Graphics g;
g = this.getGraphics();
}
public void paint(Graphics g) {
g.clearRect(0, 0, 900, 900);
// System.out.print(drawl.length);
if (audioData != null) {
drawl = new int[audioData.length];
for (int i = 0; i < audioData.length; i++) {
ml[0] = audioData[i];
// String s=new String(ml);
drawl[i] = Math.abs((int) ml[0]);
}
System.out.println(drawl[0]);
for (int i = 0; i < drawl.length - 1; i++) {
g.drawLine(i * this.getWidth() / 256, drawl[i] + 100, (i + 1)
* this.getWidth() / 256, drawl[i + 1] + 100);
}
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Auto-generated method stub
while (intBytes != -1) {
try {
synchronized (this) {
this.wait(10);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
repaint();
}
}
public void play() {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(
"F:/perl/key2.wav"));// 获得音频输入流
ais = AudioSystem.getAudioInputStream(
AudioFormat.Encoding.PCM_SIGNED, ais);
AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排
System.out.println("baseFormat=" + baseFormat);
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
baseFormat);
System.out.println("info=" + info);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
// 从混频器获得源数据行
System.out.println("line=" + line);
line.open(baseFormat);// 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作。
line.start();// 允许数据行执行数据 I/O
int BUFFER_SIZE = 256;
audioData = new byte[BUFFER_SIZE];
while (intBytes != -1) {
intBytes = ais.read(audioData, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。
if (intBytes >= 0) {
int outBytes = line.write(audioData, 0, intBytes);// 通过此源数据行将音频数据写入混频器。
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
//