下载安装

使用音频分析工具audacity分析wave文件_#include

audacity也有ubuntu版本,通过命令sudo apt install audacity安装

startup.wav文件内容

使用音频分析工具audacity分析wave文件_音视频_02

波形

使用音频分析工具audacity分析wave文件_ci_03

使用音频分析工具audacity分析wave文件_#include_04

wave格式:

wav 格式,是微软开发的一种文件格式规范,整个文件分为两部分,第一部分是“文件头”,记录重要的参数信息,对于音频而言,就包括:采样率、通道数、位宽等等;第二部分是“数据块”,即一帧一帧的二进制数据,对于音频而言,就是原始的 PCM 数据。所以wav格式 = Header(44 bytes) + data

下图的前44个字节表示wave文件头

使用音频分析工具audacity分析wave文件_ci_05

wave文件头的数据类型是:

使用音频分析工具audacity分析wave文件_#include_06

data数据区PCM数据格式:

使用音频分析工具audacity分析wave文件_音视频_07

解析代码:

使用音频分析工具audacity分析wave文件_采样率_08

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct WaveHeader{
int riff_id;
int riff_sz;
int riff_fmt;
int fmt_id;
int fmt_sz;
short audio_fmt;
short num_chn;
int sample_rate;
int byte_rate;
short block_align;
short bits_per_sample;
int data_id;
int data_sz;
} WaveHeader;

int main(void)
{
FILE *stream;
WaveHeader wh;

memset(&wh, 0x00, sizeof(wh));

printf("sizeof(WaveHeader) = %ld.\n", sizeof(WaveHeader));

if((stream=fopen("startup.wav","ro"))==NULL)
{
fprintf(stderr,"Can not open file.\n");
return 0;
}
printf("open success.\n");

fseek(stream, 0, SEEK_SET);

fread(&wh,1,sizeof(wh),stream);

fclose(stream);

printf("riff_id %d,\n" \
"riff_size %d\n" \
"num_chn %d.\n" \
"sample_rate %d.\n" \
"byte_rate %d.\n" \
"block_align %d.\n" \
, wh.riff_id, wh.riff_sz, wh.num_chn, wh.sample_rate, wh.byte_rate, wh.block_align);

return 0;
}

同理,将PCM文件转换为WAVE格式,其实就是在PCM前面加上了44个字节的头.

结束!