JAVA畫出wav音訊波形圖

來源:互聯網
上載者:User
字型大小:{
function onclick()
{
function onclick()
{
function onclick()
{
changeFont(1);
}
}
}
}">大 {
function onclick()
{
function onclick()
{
function onclick()
{
changeFont(2);
}
}
}
}">中 {
function onclick()
{
function onclick()
{
function onclick()
{
changeFont(3);
}
}
}
}">小

import java.awt.Graphics;
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;

public class Musicline extends javax.swing.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;
//String s=new String(ml);
drawl=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+100,(i+1)*this.getWidth()/256,drawl[i+1]+100);
}
}

}
public void run()
{
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(
"E:/a.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.
*/
// <editor-fold defaultstate="collapsed" desc=" 產生的程式碼 ">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.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();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
Musicline msl=new Musicline();
msl.setVisible(true);
Thread yh=new Thread(msl);
yh.start();
msl.play();

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import java.awt.*;
 import javax.swing.*;
 
 public class Func extends JFrame{
/**
 *<br>方法說明:主方法
 *<br>輸入參數:
 *<br>傳回型別:
 */
  public static void main(String[] args){
    Func db = new Func();
    db.update();
  }
/**
 *<br>方法說明:構造器,顯示表單
 *<br>輸入參數:
 *<br>傳回型別:
 */
  Func(){
    super("Function");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(310,310);
    show();
  }
/**
 *<br>方法說明:更新畫面
 *<br>輸入參數:
 *<br>傳回型別:
 */
  public void update(){
     repaint();
  }
/**
 *<br>方法說明:轉換座標點
 *<br>輸入參數:
 *<br>傳回型別:
 */
  double f(double x) { 
       return (Math.sin(2*x)+Math.cos(3*x));
  }
/**
 *<br>方法說明:繪製座標圖
 *<br>輸入參數:
 *<br>傳回型別:
 */ 
  public void paint(Graphics g) {
       double x0,x1,y0,y1,xd,yd;
       double max=5.0;
       int w,h,hw,hh;
       //擷取螢幕的尺寸
       w=getSize().width;
       h=getSize().height;
       hw=w/2; hh=h/2;
       //在螢幕上輸出字元
       g.drawString("Sin[2x]+Cos[3x]",10,40);
       g.setColor(Color.red); 
       g.drawString("0",hw+5,hh+12);
       g.drawString(""+max,hw-20,40);
       g.drawString(""+max,w-20,hh+12);
       //繪製X軸座標
       g.drawLine(0,hh,w,hh);
       //繪製Y軸座標
       g.drawLine(hw,0,hw,h);
       xd=2*max/w;//計算X增量
       yd=hh/max; //計算y增量
       g.setColor(Color.blue);
       //使用增量繪製一小段線,最終組成曲線圖形。
       for (int x=0 ; x<w-1; x++) {
          x0=-max+x*xd; y0=f(x0)*yd;
          x1=x0+xd;     y1=f(x1)*yd;
          g.drawLine(x,(int)(hh-y0),x+1,(int)(hh-y1));
       }
  }
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.