最近在做傅里叶变换和小波变换时经常要通过显示波形来检验算法,但通过visual studio之类显示波形又显得麻烦,而且不能跨平台。
CImg是一个跨平台的C++的图像处理库,提供的图像处理等功能十分强大,而且加入项目中十分轻便,只需将头文件包含在项目中即可,十分轻便。
可自行到http://www.cimg.eu/下载
利用它来在linux、Mac OS X中显示波形,再合适不过了,下面是音频波形显示的代码。
主函数
main.cpp
#include <iostream>
#include "wavfile.h"
#include "CImg.h" using namespace std;
using namespace cimg_library;
using namespace AudioUtils; int main(void)
{
WaveFile *wf=new WaveFile();
/*
读取本目录里的audio.wav文件,并生成波形
*/
wf->readwav("audio.wav"); int datalength=wf->size()/wf->bit(); wf->output_WAVfile_info(); int *data=(int*)malloc(sizeof(int)*datalength);
for(int i=;i<datalength;i++)
{
data[i]=(float)(wf->data[i]/);
}
/*
调用CImg显示波形
*/
const unsigned char red[] = { , , }, yellow[] = { , , },
green[] = { , , };
/*
一维信号
*/
int num=wf->size()/(*(wf->bit()));
CImg<float> signal_wav(num,, , , );
for(int i=;i<num;i++)
{
signal_wav[i]=(data[i*])*;
} CImg<unsigned char> signal_visu( , , , , );
/*
画格子
*/
signal_visu.draw_grid( , , , , , , yellow, 0.5 ); signal_visu.draw_graph(signal_wav,red,,,,,-,);
/*
画波形
*/
CImgDisplay main_disp_wav (signal_visu, "signal" ); getchar(); return ;
}
wavfile.h
读取WAV格式文件
#ifndef WAVFILE_H
#define WAVFILE_H
#include <iostream>
#include <fstream>
using namespace std; /*
BYTE=unsigned char(完全等同) WORD=unsigned short(完全等同) DWORD=unsigned long(完全等同) unsigned char是无符号字节型
*/
namespace AudioUtils
{
/*
RIFF WAVE Chunk
==================================
| |所占字节数| 具体内容 |
==================================
| ID | 4 Bytes | 'RIFF' |
----------------------------------
| Size | 4 Bytes | |
----------------------------------
| Type | 4 Bytes | 'WAVE' |
---------------------------------- */
class RiffBlock
{
public:
void ReadRiff(ifstream &infile)
{
int i=;
for(i=;i<;i++)
{
infile.read((char*)&m_RiffID[i],);
}
infile.read((char*)&m_RiffSize,);
for(i=;i<;i++)
{
infile.read((char*)&Format[i],);
}
} /*
此处应当声明为pravte 用函数return, 不安全代码,暂简略处理
*/
unsigned char m_RiffID[];
unsigned long m_RiffSize;
unsigned char Format[];
};
/*
====================================================================
| | 字节数 | 具体内容 |
====================================================================
| ID | 4 Bytes | 'fmt ' |
--------------------------------------------------------------------
| Size | 4 Bytes | 数值为16或18,18则最后又附加信息 |
-------------------------------------------------------------------- ----
| FormatTag | 2 Bytes | 编码方式,一般为0x0001 | |
-------------------------------------------------------------------- |
| Channels | 2 Bytes | 声道数目,1--单声道;2--双声道 | |
-------------------------------------------------------------------- |
| SamplesPerSec | 4 Bytes | 采样频率 | |
-------------------------------------------------------------------- |
| AvgBytesPerSec| 4 Bytes | 每秒所需字节数 | |===> WAVE_FORMAT
-------------------------------------------------------------------- |
| BlockAlign | 2 Bytes | 数据块对齐单位(每个采样需要的字节数) | |
-------------------------------------------------------------------- |
| BitsPerSample | 2 Bytes | 每个采样需要的bit数 | |
-------------------------------------------------------------------- |
| | 2 Bytes | 附加信息(可选,通过Size来判断有无) | |
-------------------------------------------------------------------- ---- */
class FmtBlock
{
public:
//seekg()移动文件指针
void ReadFmt(ifstream &infile)
{
int i=;
for(i=;i<;i++)
{
infile.read((char*)&m_FmtID[i],);
}
infile.read((char*)&m_FmtSize,);
int size=m_FmtSize;
infile.read((char*)&m_FmtTag,);
infile.read((char*)&m_Channels,);
infile.read((char*)&m_SamplesPerSec,);
infile.read((char*)&m_AverageBytesPerSec,);
infile.read((char*)&m_BlockAlign,);
infile.read((char*)&m_BitsPerSample,);
if(size==)
{
infile.read((char*)&m_OtherMeg,);
}
} int BitsPerSample()
{
return (m_BitsPerSample/);
} unsigned char m_FmtID[];
unsigned long m_FmtSize;
unsigned short m_FmtTag;
unsigned short m_Channels;
unsigned long m_SamplesPerSec;
unsigned long m_AverageBytesPerSec;
unsigned short m_BlockAlign;
unsigned short m_BitsPerSample;
unsigned short m_OtherMeg;
}; /*
Data Chunk
==================================
| |所占字节数| 具体内容 |
==================================
| ID | 4 Bytes | 'data' |
----------------------------------
| Size | 4 Bytes | |
----------------------------------
| data | | |
---------------------------------- 1Bytes
---------------------------------------------------------------------
| 单声道 | 取样1 | 取样2 | 取样3 | 取样4 |
| | --------------------------------------------------------
| 8bit量化 | 声道0 | 声道0 | 声道0 | 声道0 |
---------------------------------------------------------------------
| 双声道 | 取样1 | 取样2 |
| |--------------------------------------------------------
| 8bit量化 | 声道0(左) | 声道1(右) | 声道0(左) | 声道1(右) |
1WORD
---------------------------------------------------------------------
| | 取样1 | 取样2 |
| 单声道 |--------------------------------------------------------
| 16bit量化 | 声道0 | 声道0 | 声道0 | 声道0 |
| | (低位字节) | (高位字节) | (低位字节) | (高位字节) |
---------------------------------------------------------------------
| | 取样1 |
| 双声道 |--------------------------------------------------------
| 16bit量化 | 声道0(左) | 声道0(左) | 声道1(右) | 声道1(右) |
| | (低位字节) | (高位字节) | (低位字节) | (高位字节) |
--------------------------------------------------------------------- */
class DataBlock
{
public:
void ReadData(ifstream &infile,int BitsPerSample)//BitsPerSample:1 8bit;2 16bit
{
int i=; bool un_find_data=true;
while(un_find_data)
{
infile.read((char*)&temp,);
if(temp=='d')
{
infile.read((char*)&temp,);
if(temp=='a')
{
infile.read((char*)&temp,);
if(temp=='t')
{
infile.read((char*)&temp,);
if(temp=='a')
{
un_find_data=false;
}
}
} }
} infile.read((char*)&m_DataSize,);
m_NumSamples=m_DataSize/BitsPerSample;
if(BitsPerSample==)
{
m_Data8=new char[m_NumSamples];
for(i=;i<m_NumSamples;i++)
{
infile.read((char*)&m_Data8[i],BitsPerSample);
}
}
else if(BitsPerSample==)
{
m_Data16=new short[m_NumSamples];
for(i=;i<m_NumSamples;i++)
{
infile.read((char*)&m_Data16[i],BitsPerSample);
}
} } short *m_Data16;
char *m_Data8; unsigned char temp;
unsigned char m_DataID[];
unsigned long m_DataSize; int m_NumSamples;//样本数量
};
class WaveFile
{
public:
void readwav(char *path)
{
ifstream infile(path,ios::binary);
m_Riff = new RiffBlock();
m_Fmt = new FmtBlock();
m_Data = new DataBlock(); m_Riff->ReadRiff(infile);
m_Fmt->ReadFmt(infile);
m_Data->ReadData(infile,m_Fmt->BitsPerSample());
int i=;
data=new int[m_Data->m_DataSize/m_Fmt->BitsPerSample()];
for(i=;i<(m_Data->m_DataSize)/(m_Fmt->BitsPerSample());i++)
{ if(m_Fmt->BitsPerSample()==)
{
data[i]=(int)m_Data->m_Data8[i];
}
else if(m_Fmt->BitsPerSample()==)
{
data[i]=(int)m_Data->m_Data16[i];
}
} } void output_WAVfile_info(void)//test
{
cout<<"Audio Msg:"<<endl;
/*
RIFF WAVE Chunk Message
*/
cout<<m_Riff->m_RiffID<<endl;
cout<<m_Riff->m_RiffSize<<endl;
cout<<m_Riff->Format<<endl; /*
Fmt Message
*/
cout<<m_Fmt->m_FmtID<<endl;
cout<<m_Fmt->m_FmtSize<<endl;
cout<<m_Fmt->m_FmtTag<<endl;
cout<<m_Fmt->m_Channels<<endl;
cout<<m_Fmt->m_SamplesPerSec<<endl;
cout<<m_Fmt->m_AverageBytesPerSec<<endl;
cout<<m_Fmt->m_BlockAlign<<endl;
cout<<m_Fmt->m_BitsPerSample<<endl;
cout<<m_Fmt->m_OtherMeg<<endl;
/*
Data Chunk Message
*/
cout<<m_Data->temp<<endl;
cout<<m_Data->m_DataID<<endl;
cout<<m_Data->m_DataSize<<endl; }
int size(void)//音频长度
{
return m_Data->m_DataSize;
}
int channels(void)
{
return m_Fmt->m_Channels;
}
int bit(void)
{
return m_Fmt->BitsPerSample();
}
int *data;
private:
RiffBlock *m_Riff;
FmtBlock *m_Fmt;
DataBlock *m_Data;
};
} #endif
编译时运行如下命令:
g++ -o out main.cpp -O2 -lm -lpthread -L/usr/X11R6/lib -lm -lpthread -lX11
结果: