打印
[应用相关]

用fread读wav文件出错

[复制链接]
910|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Edisons|  楼主 | 2019-10-12 20:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
编译可以可以通过 就是运行的时候 文件头就读错了
#include "Wave.h"

// WaveHeader.c


//void main(int argc, char **argv)
void main()
{
    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 = fopen("News Item 1.wav","rb"))!=NULL)
{
  fread(&hwav,sizeof(wav_pcm_header44),1,fp);
// fread(&hwav, 44,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.ChunkID);
  /*
  *  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);

  }
  // printf("DATA:%d\n", (&data);
  fclose(fp);
  fp = NULL;
}
else
{
  //printf("Open wave file %s failed!\n", argv[1]);
  printf("Open wave file %s failed!\n");
}


}




头文件:#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{
signed char ChunkID[4]; // "RIFF"; The "RIFF" the mainchunk;
signed long ChunkSize; // FileSize - 8; The size following this data
signed char Format[4]; // "WAVE"; The "WAVE" format consists of two subchunks: "fmt " and "data"
signed char SubChunk1ID[4]; // "fmt "
signed long SubChunk1Size; // 16 for PCM. This is the size of the rest of the subchunk which follows this data.
signed short AudioFormat; // 1 for PCM. Linear quantization
signed short NumChannels; // 1->Mono, 2->stereo, etc..
signed long SampleRate; // 8000, 11025, 16000, 44100, 48000, etc..
signed long ByteRate; // = SampleRate * NumChannels * BitsPerSample/8
signed short BlockAlign; // = NumChannels * BitsPerSample / 8
signed short BitsPerSample; // 8->8bits, 16->16bits, etc..
signed char SubChunk2ID[4]; // "data"
signed long SubChun2Size; // = NumSamples * NumChannels * BitsPerSample / 8. The size of data
} wav_pcm_header44;

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

使用特权

评论回复
沙发
Listate| | 2019-10-12 20:23 | 只看该作者
把News Item 1.wav去掉空格,改成NewsItem1.wav试试

使用特权

评论回复
板凳
Mozarts| | 2019-10-12 20:25 | 只看该作者

楼主的这个问题似乎手册上似乎有很多例子的。

使用特权

评论回复
地板
21mengnan| | 2019-10-12 23:19 | 只看该作者
好像是不能有空格吧

使用特权

评论回复
5
21mengnan| | 2019-10-12 23:59 | 只看该作者
字母数字下划线嘛

使用特权

评论回复
6
zjq985062714| | 2019-10-13 09:32 | 只看该作者
学到了~

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

58

主题

1426

帖子

0

粉丝