怎样使用wavec read函数数

4607人阅读
1.PCM Wave格式详解
WAVE文件格式是微软RIFF(Resource Interchange File Format,资源交换文件标准)的一种,是针对于多媒体文件存储的一种文件格式和标准。 一般而言,RIFF文件由文件头和数据两部分组成,一个WAVE文件由一个“WAVE”数据块组成,这个“WAVE”块又由一个”fmt”子数据块和一个“data”子 数据块组成,也称这种格式为“Canonical form”(权威/牧师格式),如下图所示:
每个字段的涵义如下: ChunkID: 占4个字节,内容为“RIFF”的ASCII码(0x),以大端(big endian)存储。
ChunkSize: 4字节,存储整个文件的字节数(不包含ChunkID和ChunkSize这8个字节),以小端(little endian)方式存储。
Format: 4字节,内容为“WAVE”的ASCII码(0x),以大端存储。
其中bigendian 主要有一个特征,在内存中对操作数的存储方式和从高字节到低字节。例如:0x1234,这样一个数,存储为:
而小尾端littleendian是:
用程序在区别的话,可以考虑:
#include &stdio.h&
#include &stdlib.h&
int main(int argc, char *argv[])
if( c.b==1 )
printf(&little endian\n&);
else printf(&big endian\n&);
system(&PAUSE&);
“WAVE”格式由两个子数据块构成:“fmt”块和“data”块,其中“fmt”块的详细解释如下: Subchunk1ID: 占4个字节,内容为“fmt ”的ASCII码(0x666d7420),以大端存储。
Subchunk1Size: 占4个字节,存储该子块的字节数(不含前面的Subchunk1ID和Subchunk1Size这8个字节),以小端方式存储。
AudioFormat:占2个字节,以小端方式存储,存储音频文件的编码格式,例如若为PCM则其存储值为1,若为其他非PCM格式的则有一定的压缩。
NumChannels: 占2个字节,以小端方式存储,通道数,单通道(Mono)值为1,双通道(Stereo)值为2,等等。
SampleRate: 占4个字节,以小端方式存储,采样率,如8k,44.1k等。
ByteRate: 占4个字节,以小端方式存储,每秒存储的bit数,其值=SampleRate * NumChannels * BitsPerSample/8
BlockAlign: 占2个字节,以小端方式存储,块对齐大小,其值=NumChannels * BitsPerSample/8
BitsPerSample: 占2个字节,以小端方式存储,每个采样点的bit数,一般为8,16,32等。
接下来是两个可选的扩展参数:
ExtraParamSize: 占2个字节,表示扩展段的大小。
ExtraParams: 扩展段其他自定义的一些参数的具体内容,大小由前一个字段给定。
其中,对于每个采样点的bit数,不同的bit数读取数据的方式不同:
// data 为读取到的采样点的值,speech为原始数据流,
//对应于下面的&WAVE&格式文件的第二个子数据块“data”块的“Data”部分。
for(i=0;i&NumSi++){
if(BitsPerSample==8)
data[i] = (int)*((char*)speech+i);
else if(BitsPerSample==16)
data[i] = (int)*((short*)speech+i);
else if(BitsPerSample==32)
data[i] = (int)*((int*)speech+i);
“WAVE”格式文件的第二个子数据块是“data”,其个字段的详细解释如下:
Subchunk2ID: 占4个字节,内容为“data”的ASCII码(0x),以大端存储。
Subchunk2Size: 占4个字节,内容为接下来的正式的数据部分的字节数,其值=NumSamples * NumChannels * BitsPerSample/8
Data: 真正的语音数据部分。
一个Wave文件头的实例
设一个wave文件的前72个字节的十六进制内容如下(可以使用Ultra Edit等工具查看wave文件头):
52 49 46 46 24 08 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00
22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 08 00 00 00 00 00 00
24 17 1e f3 3c 13 3c 14 16 f9 18 f9 34 e7 23 a6 3c f2 24 f2 11 ce 1a 0d
则其个字段的解析如下图:
C语言实现wave文件的读取
这里给出一个用基本的C语言文件操作库函数实现的Wave文件读取的实例代码,可以跨Windows和Linux平台。
#include &stdio.h&
#include &stdlib.h&
#include &string.h&
// define Wave format structure
typedef struct tWAVEFORMATEX
short wFormatT
/* format type */
/* number of channels (i.e. mono, stereo...) */
unsigned int nSamplesPerS
/* sample rate */
unsigned int nAvgBytesPerS
/* for buffer estimation */
short nBlockA
/* block size of data */
short wBitsPerS
/* number of bits per sample of mono data */
/* the count in bytes of the size of */
/* extra information (after cbSize) */
} WAVEFORMATEX, *PWAVEFORMATEX;
char* wavread(char *fname, WAVEFORMATEX *wf);
int main(){
char fname[] = &test.wav&;
WAVEFORMATEX
speech = wavread(fname, &wf);
// afterward processing...
// read wave file
char* wavread(char *fname, WAVEFORMATEX *wf){
char str[32];
unsigned int subchunk1 // head size
unsigned int subchunk2 // speech data size
// check format type
fp = fopen(fname,&r&);
fprintf(stderr,&Can not open the wave file: %s.\n&,fname);
return NULL;
fseek(fp, 8, SEEK_SET);
fread(str, sizeof(char), 7, fp);
str[7] = '\0';
if(strcmp(str,&WAVEfmt&)){
fprintf(stderr,&The file is not in WAVE format!\n&);
return NULL;
// read format header
fseek(fp, 16, SEEK_SET);
fread((unsigned int*)(&subchunk1size),4,1,fp);
fseek(fp, 20, SEEK_SET);
fread(wf, subchunk1size, 1, fp);
// read wave data
fseek(fp, 20+subchunk1size, SEEK_SET);
fread(str, 1, 4, fp);
str[4] = '\0';
if(strcmp(str,&data&)){
fprintf(stderr,&Locating data start point failed!\n&);
return NULL;
fseek(fp, 20+subchunk1size+4, SEEK_SET);
fread((unsigned int*)(&subchunk2size), 4, 1, fp);
speech = (char*)malloc(sizeof(char)*subchunk2size);
if(!speech){
fprintf(stderr, &Memory alloc failed!\n&);
return NULL;
fseek(fp, 20+subchunk1size+8, SEEK_SET);
fread(speech, 1, subchunk2size, fp);
fclose(fp);
[1]WAVE PCM soundfile format: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/&
[2]Resource Interchange File Format: http://en.wikipedia.org/wiki/Resource_Interchange_File_Format&
[3]基于Visual C++6.0的声音文件操作: /3116_1.shtml
Original Link:&
Attribution - NON-Commercial - ShareAlike - Copyright (C)&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2763970次
积分:23879
积分:23879
排名:第272名
原创:158篇
转载:596篇
评论:203条
(1)(1)(2)(2)(1)(2)(1)(42)(17)(15)(15)(3)(6)(2)(13)(7)(14)(14)(18)(16)(32)(29)(14)(25)(22)(24)(10)(23)(27)(21)(21)(21)(28)(33)(51)(20)(29)(33)(26)(31)(2)(3)(10)(2)(3)(6)(11)(5)WaveCapture博客访问: 390008
博文数量: 102
博客积分: 6046
博客等级: 准将
技术积分: 908
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: C/C++
123456789101112131415161718
232425262728293031323334353637383940414243
454647484950
50avistreams
& AVIFileOpen AVIFileAddRef
& AVIFileRelease
AVIFileInfoaviAVIFILEINFO
AVIFileReadDataAVIFileInfofile
AVIFileWriteData
& AVIPutFileOnClipboardavi
& AVIGetFromClipboardavi
& AVIClearClipboard
2数据流操作
& AVIFileGetStream
&AVIStreamOpenFromFileAVIFileOpenAVIFileGetStream
&AVIFileOpenAVIFileGetStream
&AVIStreamAddRefstream
AVIStreamReleasestreams0
AVIStreamInfoAVISTREAMINFObuffersizeratedescription
&& AVIStreamReadDataAVIStreamDataSize
&& AVIStreamReadFormatbufferBIMAPINFOWAVEFORMATEXPCMAVEFORMATAVIStreamReadFormatbufferbufferAVIStreamFormatSize
& AVIStreamReadsampleAVIStreamReadNULLbufferbufferAVIStreamSampleSizebuffer
& AVIAVIStreamBeginStreamingAVIhandleAVIStreamEndStreamming
& AVIStreamReadDrawDib
& AVIFileAVIStreamGetFrameOpenAVIStreamGetFrameBIMAPINFOHEADERAVIStreamGetFrameAVIStreamGetFrameClose
& AVISavebuildAVISaveVAVISaveAVISaveVAVISave
& AVISaveOptions
& AVISaveAVISaveVaviavi
& GetSaveFileNamePreview
& AVIMakeFileFromStreamsaviAVIFileRelease
&AVIFileCreateStreamAVISTREAMINFO
& AVIStreamSetFormatBIMAPINFOWAVEFORMAT
& AVIStreamWriteavi handler
& AVIFileWriteDataAVIStreamWriteData
&AVIStreamRelease
& AVIStreamStartsample numberAVIStreamInfoAVISTREAMINFOdwStartAVIStreamStartTimesample
& AVIStreamLengthsampleAVIStreamInfoAVIStreamLengthTime
&&samplesampleAVIStreamReadNULLAVIStreamFindSampleFIND_ANYsample
AVIStreamFindSamplesample
timesample
&AVIStreamSampleToTimesmaple
&AVIStreamTimeToSample
2.7 streams
&AVIStreamCreate
&AVIMakeCompressedStream
& avistreamcreateavimakecompressedstreamAVIStreamRelease
&CreateEditableStream
阅读(3120) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。嵌入式&PCM&WAVE格式详解及用C语言实现wave文件的读取
1.PCM Wave格式详解
#include &stdio.h&
#include &stdlib.h&
int main(int argc, char *argv[])
if( c.b==1 )
printf("little endian\n");
else printf("big endian\n");
system("PAUSE");
// data 为读取到的采样点的值,speech为原始数据流,
//对应于下面的"WAVE"格式文件的第二个子数据块“data”块的“Data”部分。
for(i=0;i&NumSi++){
if(BitsPerSample==8)
data[i] = (int)*((char*)speech+i);
else if(BitsPerSample==16)
data[i] = (int)*((short*)speech+i);
else if(BitsPerSample==32)
data[i] = (int)*((int*)speech+i);
一个Wave文件头的实例
52 49 46 46 24 08 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00
22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 08 00 00 00 00 00 00
24 17 1e f3 3c 13 3c 14 16 f9 18 f9 34 e7 23 a6 3c f2 24 f2 11 ce 1a 0d
C语言实现wave文件的读取
#include &stdio.h&
#include &stdlib.h&
#include &string.h&
// define Wave format structure
typedef struct tWAVEFORMATEX
short wFormatT
unsigned int nSamplesPerS
unsigned int nAvgBytesPerS
short nBlockA
short wBitsPerS
} WAVEFORMATEX, *PWAVEFORMATEX;
char* wavread(char *fname, WAVEFORMATEX *wf);
int main(){
char fname = "test.wav";
WAVEFORMATEX
speech = wavread(fname, &wf);
// afterward processing...
// read wave file
char* wavread(char *fname, WAVEFORMATEX *wf){
char str[32];
unsigned int subchunk1 // head size
unsigned int subchunk2 // speech data size
// check format type
fp = fopen(fname,"r");
fprintf(stderr,"Can not open the wave file: %s.\n",fname);
return NULL;
fseek(fp, 8, SEEK_SET);
fread(str, sizeof(char), 7, fp);
str[7] = '\0';
if(strcmp(str,"WAVEfmt")){
fprintf(stderr,"The file is not in WAVE format!\n");
return NULL;
// read format header
fseek(fp, 16, SEEK_SET);
fread((unsigned int*)(&subchunk1size),4,1,fp);
fseek(fp, 20, SEEK_SET);
fread(wf, subchunk1size, 1, fp);
// read wave data
fseek(fp, 20+subchunk1size, SEEK_SET);
fread(str, 1, 4, fp);
str[4] = '\0';
if(strcmp(str,"data")){
fprintf(stderr,"Locating data start point failed!\n");
return NULL;
fseek(fp, 20+subchunk1size+4, SEEK_SET);
fread((unsigned int*)(&subchunk2size), 4, 1, fp);
speech = (char*)malloc(sizeof(char)*subchunk2size);
if(!speech){
fprintf(stderr, "Memory alloc failed!\n");
return NULL;
fseek(fp, 20+subchunk1size+8, SEEK_SET);
fread(speech, 1, subchunk2size, fp);
fclose(fp);
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。查看: 20302|回复: 9|关注: 0
请问wavread怎么用?
<h1 style="color:# 麦片财富积分
新手, 积分 5, 距离下一级还需 45 积分
初次接触matlab, 想要读入一段语音。 但是wavread的书写格式是什么呢? 比如说【y,FS】=('bindi.wav')这样对吗? 还有就是这个音频是放在任何地方都可以的吗? 谢谢!!!!!
[ 本帖最后由 cwjy 于
11:43 编辑 ]
论坛优秀回答者
帖子最佳答案
关注者: 586
要放在当前路径下。
《Simulink仿真及代码生成技术入门到精通》已经由北航出版社出版:/.html
<h1 style="color:# 麦片财富积分
关注者: 10
[y,FS]=('c:\bindi.wav')
有时候在写路径名的 时候 请写完整的!!
不然 matlab识别不了!
写好了是那个盘的!!
还有 文件名最好是 字母的&&不要是 汉字的&&matlab识别汉字也有问题
<h1 style="color:# 麦片财富积分
en谢谢了。
用【y,FS】=('bindi.wav')读出来的是两列数, 不知道是什么意思?
&&-0.0015& &-0.0015
& &-0.0010& &-0.0010
& &-0.0007& &-0.0007
& &-0.0007& &-0.0007
& &-0.0014& &-0.0014
& &-0.0025& &-0.0025
& &-0.0039& &-0.0039
& &-0.0051& &-0.0051
& &-0.0057& &-0.0057
& &-0.0057& &-0.0057
& &-0.0052& &-0.0052
& &-0.0043& &-0.0043
& &-0.0033& &-0.0033
& &-0.0021& &-0.0021
& &-0.0007& &-0.0007
& & 0.0009& & 0.0009
& & 0.0025& & 0.0025
& & 0.0039& & 0.0039
& & 0.0045& & 0.0045
& & 0.0044& & 0.0044
& & 0.0034& & 0.0034
& & 0.0019& & 0.0019
& & 0.0003& & 0.0003
& &-0.0011& &-0.0011
<h1 style="color:# 麦片财富积分
<h1 style="color:# 麦片财富积分
不是分别表示样本点频率跟强度吗?
MATLAB 信号处理与通信版块优秀回答者
<h1 style="color:#3 麦片财富积分
关注者: 7
在MATLAB命令窗口输入:
help wavread
doc wavread
<h1 style="color:# 麦片财富积分
[z,fs,nsample]=wavread('XXX.wav');
<h1 style="color:# 麦片财富积分
en谢谢了。
用【y,FS】=('bindi.wav')读出来的是两列数, 不知道是什么意思?
&&-0.0015& &-0.0015
用一列就可以表示这个信号的特征。y(:,1)取第一列
<h1 style="color:# 麦片财富积分
waveread函数只支持脉冲编码调制(PCM)数据格式,所以一般的wav格式matlab是不认的,所以需要用一些格式转换软件,自定义一下输出格式,改为PCM输出即可。然后就是wavread的文件路径必须是完整路径。
站长推荐 /2
利用MATLAB/Simulink开发智能机器人系统
MATLAB中文论坛是全球最大的 MATLAB & Simulink 中文社区。用户免费注册会员后,即可下载代码,讨论问题,请教资深用户及结识书籍作者。立即注册加入我们吧!
MATLAB官方社交平台
MATLAB中文论坛微社区}

我要回帖

更多关于 read函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信