概述
读取wav文件头
void ReadWaveHead(char *file)
{
uchar temp = 0;
uint8 read_bytes = 0;
char *channel_mappings[] = {NULL,"mono","stereo"};
uint32 total_time = 0;
struct PlayTime //播放时间
{
uint8 hour;
uint8 minute;
uint8 second;
} play_time;
if(NULL == (wave.fp=fopen(file, "rb"))) /* open file */
{
printf("file %s open failure!n", file);
}
/* read heade information */
if(1 != fread(wave.header.riff, sizeof(uint8), 1, wave.fp)) /* RIFF chunk */
{
printf("read riff error!n");
return;
}
if(1 != fread(&wave.header.size, sizeof(uint32), 1, wave.fp)) /* SIZE : from here to file end */
{
printf("read size error!n");
return;
}
if(1 != fread(wave.header.wave_flag, sizeof(uint8), 1, wave.fp)) /* wave file flag */
{
printf("read wave_flag error!n");
return;
}
if(1 != fread(wave.header.fmt, sizeof(uint8), 1, wave.fp)) /* fmt chunk */
{
printf("read fmt error!n");
return;
}
if(1 != fread(&wave.header.fmt_len, sizeof(uint32), 1, wave.fp)) /* fmt length */
{
printf("read fmt_len error!n");
return;
}
if(1 != fread(&wave.header.tag, sizeof(uint16), 1, wave.fp)) /* tag : PCM or not */
{
printf("read tag error!n");
return;
}
if(1 != fread(&wave.header.channels, sizeof(uint16), 1, wave.fp)) /* channels */
{
printf("read channels error!n");
return;
}
if(1 != fread(&wave.header.samp_freq, sizeof(uint32), 1, wave.fp)) /* samp_freq */
{
printf("read samp_freq error!n");
return;
}
if(1 != fread(&wave.header.byte_rate, sizeof(uint32), 1, wave.fp)) /* byte_rate : decode how many bytes per second */
{ /* byte_rate = samp_freq * bit_samp */
printf("read byte_rate error!n");
return;
}
if(1 != fread(&wave.header.block_align, sizeof(uint16), 1, wave.fp)) /* quantize bytes for per samp point */
{
printf("read byte_samp error!n");
return;
}
if(1 != fread(&wave.header.bit_samp, sizeof(uint16), 1, wave.fp)) /* quantize bits for per samp point */
{ /* bit_samp = byte_samp * 8 */
printf("read bit_samp error!n");
return;
}
/* jump to "data" for reading data */
if(1 != fread(&wave.data_flag[0], sizeof(uint8), 1, wave.fp)) /* data chunk */
{
printf("read header data error!n");
return;
}
if(1 != fread(&wave.length, sizeof(uint32), 1, wave.fp)) /* data length */
{
printf("read length error!n");
}
/* jduge data chunk flag */
total_time = wave.length / wave.header.byte_rate;
play_time.hour = (uint8)(total_time / 3600);
play_time.minute = (uint8)((total_time / 60) % 60);
play_time.second = (uint8)(total_time % 60);
/* printf file header information */
printf("%s %ldHz %dbit, DataLen: %ld, Rate: %ld, Length: %2ld:%2ld:%2ldn",
channel_mappings[wave.header.channels], //声道
wave.header.samp_freq, //采样频率
wave.header.bit_samp, //每个采样点的量化位数
wave.length,
wave.header.byte_rate,
play_time.hour,play_time.minute,play_time.second);
//fclose(wave.fp); /* close wave file */
}
最后
以上就是满意未来为你收集整理的读取wav文件头的全部内容,希望文章能够帮你解决读取wav文件头所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复