java绘制音频波形图

时间:2012-08-17 16:28:57
【文件属性】:

文件名称: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. */ // private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE)); pack(); }// /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Musicline msl=new Musicline(); msl.setVisible(true); Thread yh=new Thread(msl); yh.start(); msl.play(); } // 变量声明 - 不进行修改 // 变量声明结束 }


网友评论

  • 还好,有点帮助,但不是很大
  • 我还以为是跟android相关的,虽然是音频绘制波形图,但对我帮助不大。
  • 能用,只有一个java文件,就是贵了点,建议想要资源的同志们直接复制资源简介的代码,一样的
  • 做声音程序,学习一下
  • 好东西,就是想如何录音并画出啊
  • 这个程序对音频的提取是8位的 不知是否对所有的wav格式都适用