📄 wave.c
字号:
#include <stdio.h>
#include <windows.h>
#include "portableio.h"
#include "error.h"
#include "wave.h"
BOOL checkString(FILE *file,char *string)
{
char temp[1024];
unsigned int length = strlen(string);
if(fread(temp,1,length,file)!=length) ERROROUT("Premature EOF in input !?!");
temp[length] = (char)0;
return !strcmp(temp,string);
}
void wave_close(FILE *wavefile)
{
fclose(wavefile);
}
FILE* wave_open(const char *path, WAVE_INFO *wave_info)//wave_open(FileNames[i], &wave_info);
{
// static char *channel_mappings[] = {NULL,"mono","stereo"};
unsigned short wFormatTag;
unsigned long dAvgBytesPerSec;
unsigned short wBlockAlign;
long filesize;
long header_size;
FILE * wavefile;
if((wavefile = fopen(path,"rb")) == NULL)
ERROROUT("Unable to open wave file");
if(!checkString(wavefile,"RIFF")) ERROROUT("Input not a MS-RIFF file");
filesize = Read32BitsLowHigh(wavefile); /* complete wave chunk size */
printf("Microsoft RIFF, ");
if(!checkString(wavefile,"WAVE")) ERROROUT("Input not WAVE audio");
printf("WAVE audio, ");
/* WAVE FMT format chunk */
if(!checkString(wavefile,"fmt ")) ERROROUT("Can't find format chunk");
/* my total header size calculations don't work, so this is bogus... */
header_size = Read32BitsLowHigh(wavefile); /* chunk size */
wFormatTag = Read16BitsLowHigh(wavefile);
if(wFormatTag!=0x0001) ERROROUT("Not PCM Signal");
wave_info->format = WAVE_RIFF_PCM;
wave_info->channels = Read16BitsLowHigh(wavefile);
wave_info->samplerate = Read32BitsLowHigh(wavefile);
dAvgBytesPerSec = Read32BitsLowHigh(wavefile);
wBlockAlign = Read16BitsLowHigh(wavefile);
/* PCM specific */
// if(waveinfo.channels>2) ERROR("More than 2 channels\n");
/* printf("PCM, %s %ldHz ",
channel_mappings[config.wave.channels], waveinfo.samplerate);*/
wave_info->pcmbitwidth = Read16BitsLowHigh(wavefile);
if(wave_info->pcmbitwidth!=16) ERROROUT("NOT 16 Bit !!!\n");
printf("%dbit, ", wave_info->pcmbitwidth);
if(!checkString(wavefile,"data")) ERROROUT("Can't find data chunk");
header_size = ftell(wavefile);
fseek(wavefile, 0, SEEK_END);
filesize = ftell(wavefile);
fseek(wavefile, header_size, SEEK_SET);
wave_info->samples = (filesize-header_size)/(2*wave_info->channels);
/* config.wave.length = config.wave.total_samples/config.wave.samplerate;
printf("Length: %2ld:%2ld:%2ld\n", (config.wave.length/3600),
(config.wave.length/60)%60,
(config.wave.length%60)); */
return wavefile;
}
static int read_samples(short *sample_buffer,
int frame_size,FILE * wavefile,WAVE_INFO wave_info)
{
int samples_read;
switch(wave_info.format)
{
default :
ERROROUT("[read_samples], wave filetype not supported");
case WAVE_RIFF_PCM :
samples_read = fread(sample_buffer,sizeof(short),frame_size,wavefile);
/* Microsoft PCM Samples are little-endian, */
/* we must swap if this is a big-endian machine */
/* if(config.byte_order==order_bigEndian)
SwapBytesInWords(sample_buffer,samples_read);*/
if(samples_read<frame_size && samples_read>0)
{ /* Pad sample with zero's */
while(samples_read<frame_size) sample_buffer[samples_read++] = 0;
}
break;
}
return samples_read;
}
int wave_get(FILE*wavefile,short* sampleBuffer,int readNumSample,WAVE_INFO wave_info)////noSamples = wave_get(sndfile, sampleBuffer, readNumSample);
/* expects an interleaved 16bit pcm stream from read_samples, which it */
/* de-interleaves into buffer */
{
int samples_read;
samples_read = read_samples(sampleBuffer,readNumSample,wavefile,wave_info);
/* switch(config.mpeg.mode)
{
case MODE_MONO :
case MODE_STEREO :
case MODE_JSTEREO:
case MODE_DUAL :
default :
ERROR("[wav_get], channel mode not supported");
}*/
return samples_read;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -