日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java wav 波形_java读取wav文件(波形文件)并绘制波形图的方法

發布時間:2025/4/5 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java wav 波形_java读取wav文件(波形文件)并绘制波形图的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了java讀取wav文件(波形文件)并繪制波形圖的方法。分享給大家供大家參考。具體如下:

因為最近有不少網友詢問我波形文件讀寫方面的問題,出于讓大家更方便以及讓代碼能夠得到更好的改進,我將這部分(波形文件的讀寫)代碼開源在github上面。

地址為https://github.com/sintrb/waveaccess/,最新的代碼、例子、文檔都在那上面,我會在我時間精力允許的前提下對該項目進行維護,同時也希望對這方面有興趣的網友能夠加入到該開源項目上。

以下內容基本都過期了,你可以直接去github上面閱讀、下載該項目。

因項目需要讀取.wav文件(波形文件)并繪制波形圖,因此簡單的做了這方面的封裝。

其實主要是對wav文件讀取的封裝,下面是一個wav文件讀取器的封裝:

// filename: wavefilereader.java

// robintang

// 2012-08-23

import java.io.*;

public class wavefilereader {

private string filename = null;

private int[][] data = null;

private int len = 0;

private string chunkdescriptor = null;

static private int lenchunkdescriptor = 4;

private long chunksize = 0;

static private int lenchunksize = 4;

private string waveflag = null;

static private int lenwaveflag = 4;

private string fmtubchunk = null;

static private int lenfmtubchunk = 4;

private long subchunk1size = 0;

static private int lensubchunk1size = 4;

private int audioformat = 0;

static private int lenaudioformat = 2;

private int numchannels = 0;

static private int lennumchannels = 2;

private long samplerate = 0;

static private int lensamplerate = 2;

private long byterate = 0;

static private int lenbyterate = 4;

private int blockalign = 0;

static private int lenblockling = 2;

private int bitspersample = 0;

static private int lenbitspersample = 2;

private string datasubchunk = null;

static private int lendatasubchunk = 4;

private long subchunk2size = 0;

static private int lensubchunk2size = 4;

private fileinputstream fis = null;

private bufferedinputstream bis = null;

private boolean issuccess = false;

public wavefilereader(string filename) {

this.initreader(filename);

}

// 判斷是否創建wav讀取器成功

public boolean issuccess() {

return issuccess;

}

// 獲取每個采樣的編碼長度,8bit或者16bit

public int getbitpersample(){

return this.bitspersample;

}

// 獲取采樣率

public long getsamplerate(){

return this.samplerate;

}

// 獲取聲道個數,1代表單聲道 2代表立體聲

public int getnumchannels(){

return this.numchannels;

}

// 獲取數據長度,也就是一共采樣多少個

public int getdatalen(){

return this.len;

}

// 獲取數據

// 數據是一個二維數組,[n][m]代表第n個聲道的第m個采樣值

public int[][] getdata(){

return this.data;

}

private void initreader(string filename){

this.filename = filename;

try {

fis = new fileinputstream(this.filename);

bis = new bufferedinputstream(fis);

this.chunkdescriptor = readstring(lenchunkdescriptor);

if(!chunkdescriptor.endswith("riff"))

throw new illegalargumentexception("riff miss, " + filename + " is not a wave file.");

this.chunksize = readlong();

this.waveflag = readstring(lenwaveflag);

if(!waveflag.endswith("wave"))

throw new illegalargumentexception("wave miss, " + filename + " is not a wave file.");

this.fmtubchunk = readstring(lenfmtubchunk);

if(!fmtubchunk.endswith("fmt "))

throw new illegalargumentexception("fmt miss, " + filename + " is not a wave file.");

this.subchunk1size = readlong();

this.audioformat = readint();

this.numchannels = readint();

this.samplerate = readlong();

this.byterate = readlong();

this.blockalign = readint();

this.bitspersample = readint();

this.datasubchunk = readstring(lendatasubchunk);

if(!datasubchunk.endswith("data"))

throw new illegalargumentexception("data miss, " + filename + " is not a wave file.");

this.subchunk2size = readlong();

this.len = (int)(this.subchunk2size/(this.bitspersample/8)/this.numchannels);

this.data = new int[this.numchannels][this.len];

for(int i=0; i

for(int n=0; n

if(this.bitspersample == 8){

this.data[n][i] = bis.read();

}

else if(this.bitspersample == 16){

this.data[n][i] = this.readint();

}

}

}

issuccess = true;

} catch (exception e) {

e.printstacktrace();

}

finally{

try{

if(bis != null)

bis.close();

if(fis != null)

fis.close();

}

catch(exception e1){

e1.printstacktrace();

}

}

}

private string readstring(int len){

byte[] buf = new byte[len];

try {

if(bis.read(buf)!=len)

throw new ioexception("no more data!!!");

} catch (ioexception e) {

e.printstacktrace();

}

return new string(buf);

}

private int readint(){

byte[] buf = new byte[2];

int res = 0;

try {

if(bis.read(buf)!=2)

throw new ioexception("no more data!!!");

res = (buf[0]&0x000000ff) | (((int)buf[1])<<8);

} catch (ioexception e) {

e.printstacktrace();

}

return res;

}

private long readlong(){

long res = 0;

try {

long[] l = new long[4];

for(int i=0; i<4; ++i){

l[i] = bis.read();

if(l[i]==-1){

throw new ioexception("no more data!!!");

}

}

res = l[0] | (l[1]<<8) | (l[2]<<16) | (l[3]<<24);

} catch (ioexception e) {

e.printstacktrace();

}

return res;

}

private byte[] readbytes(int len){

byte[] buf = new byte[len];

try {

if(bis.read(buf)!=len)

throw new ioexception("no more data!!!");

} catch (ioexception e) {

e.printstacktrace();

}

return buf;

}

}

為了繪制波形,因此做了一個從jpanel教程而來的波形繪制面板:

// filename: drawpanel.java

// robintang

// 2012-08-23

import java.awt.color;

import java.awt.graphics;

import javax.swing.jpanel;

@suppresswarnings("serial")

public class drawpanel extends jpanel {

private int[] data = null;

public drawpanel(int[] data) {

this.data = data;

}

@override

protected void paintcomponent(graphics g) {

int ww = getwidth();

int hh = getheight();

g.setcolor(color.white);

g.fillrect(0, 0, ww, hh);

int len = data.length;

int step = len/ww;

if(step==0)

step = 1;

int prex = 0, prey = 0; //上一個坐標

int x = 0, y = 0;

g.setcolor(color.red);

double k = hh/2.0/32768.0;

for(int i=0; i

x = i;

// 下面是個三點取出并繪制

// 實際中應該按照采樣率來設置間隔

y = hh-(int)(data[i*3]*k+hh/2);

system.out.print(y);

system.out.print(" ");

if(i!=0){

g.drawline(x, y, prex, prey);

}

prex = x;

prey = y;

}

}

}

有了這些之后就可以調用繪制了,簡單的:

// wavefilereaddemo.java

// robintang

// 2012-08-23

import javax.swing.jframe;

public class wavefilereaddemo {

/**

* @param args

*/

public static void main(string[] args) {

// todo auto-generated method stub

string filename = "file.wav";

jframe frame = new jframe();

wavefilereader reader = new wavefilereader(filename);

if(reader.issuccess()){

int[] data = reader.getdata()[0]; //獲取第一聲道

drawpanel drawpanel = new drawpanel(data); // 創建一個繪制波形的面板

frame.add(drawpanel);

frame.settitle(filename);

frame.setsize(800, 400);

frame.setlocationrelativeto(null);

frame.setdefaultcloseoperation(jframe.exit_on_close);

frame.setvisible(true);

}

else{

system.err.println(filename + "不是一個正常的wav文件");

}

}

}

工程的源代碼可以在我的百度網盤上找到,直接到開源java

放上效果圖一張:

希望本文所述對大家的java程序設計有所幫助。

希望與廣大網友互動??

點此進行留言吧!

總結

以上是生活随笔為你收集整理的java wav 波形_java读取wav文件(波形文件)并绘制波形图的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。