3058|0

15

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

用wavread读wav格式的文件老是读错了 求大神帮忙 [复制链接]

#include "Wave.h"

// WaveHeader.c


void main(int argc, char **argv)
{
 FILE *fp=NULL;
 unsigned long size; 
 wav_pcm_header44 hwav;
 short *data;
 int i;
 
 
 /*
 *  Get wave header information
 */
 //fpWav = fopen(argv[1], "rb");
 fp = fopen("music_source.wav", "rb+");
 //fpWav = fopen("abc.txt","r+");
 // printf("Channel number: %d\n",fpWav);
 
 if (fp)
 {
  fread(&hwav, sizeof(wav_pcm_header44), 1, fp);
  
  /* Check wave header */
 // if ( (0==memcmp(hwav.ChunkID, "RIFF", 4)) &&(0==memcmp(hwav.Format, "WAVE", 4)) &&
  // (0==memcmp(hwav.SubChunk1ID, "fmt ", 4)) &&(0==memcmp(hwav.SubChunk2ID, "data", 4)) &&(1==hwav.AudioFormat))
 // {
   printf("Wave audio data format:\n");
   printf("Channel number: %d\n", hwav.NumChannels);
   printf("SampleRate: %dHz\n", hwav.SampleRate);
   printf("BitsPerSample: %dbits\n", hwav.BitsPerSample);
   printf("Audio data size:%d\n", hwav.SubChun2Size);
 // } 
         //printf("hwav.SubChun2Size:%d\n", hwav.SubChun2Size);
  /*
  *  Get wave data
  */
  data=(short*)malloc(sizeof(short)*hwav.SubChun2Size);
  for (i=0;i<hwav.SubChun2Size;i++)
  {
   fread(&data, sizeof(wav_pcm_header44), 1, fp);
  } 
  fclose(fp);
  fp = NULL;
 }
 else
 {
  printf("Open wave file %s failed!\n", argv[1]);
 }
 
 
}

 

 

 

头文件

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

// Microsoft wav pcm sound file format. Normal 44 bytes header
typedef struct _tagMsWavPcmHeader44
{
unsigned char ChunkID[4]; // "RIFF"; The "RIFF" the mainchunk;
unsigned long ChunkSize; // FileSize - 8; The size following this data
unsigned char Format[4]; // "WAVE"; The "WAVE" format consists of two subchunks: "fmt " and "data"

unsigned char SubChunk1ID[4]; // "fmt "
unsigned long SubChunk1Size; // 16 for PCM. This is the size of the rest of the subchunk which follows this data.
unsigned short AudioFormat; // 1 for PCM. Linear quantization
unsigned short NumChannels; // 1->Mono, 2->stereo, etc..
unsigned long SampleRate; // 8000, 11025, 16000, 44100, 48000, etc..
unsigned long ByteRate; // = SampleRate * NumChannels * BitsPerSample/8
unsigned short BlockAlign; // = NumChannels * BitsPerSample / 8
unsigned short BitsPerSample; // 8->8bits, 16->16bits, etc..

unsigned char SubChunk2ID[4]; // "data"
unsigned long SubChun2Size; // = NumSamples * NumChannels * BitsPerSample / 8. The size of data
} wav_pcm_header44;

void wave_generator(FILE *fp, wav_pcm_header44 *phwav);

 

 

编译已经能够通过 就是运行的时候读错了数据    文件头就读错了

点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表