📄 wave.c
字号:
#include <stdio.h>
#include <windows.h>#include "wave.h"
#include "error.h"
struct
{
byte riffheader[4];
byte WAVElen[4];
struct
{
byte fmtheader[8];
byte fmtlen[4];
struct
{
byte FormatTag[2];
byte Channels[2];
byte SamplesPerSec[4];
byte AvgBytesPerSec[4];
byte BlockAlign[2];
byte BitsPerSample[2]; /* format specific for PCM */
} fmt;
struct
{
byte dataheader[4];
byte datalen[4];
/* from here you insert your PCM data */
} data;
} WAVE;
} RIFF =
{ { 'R','I','F','F' } , { sizeof(RIFF.WAVE),0,0,0 } ,
{ { 'W','A','V','E','f','m','t',' ' } , { sizeof(RIFF.WAVE.fmt),0,0,0} ,
{ {1,0} , {0,0},{0,0,0,0},{0,0,0,0},{0,0},{0,0} } ,
{ { 'd','a','t','a' } , {0,0,0,0} }
}
};
long total_byte=0;
static void long2littleendian(long inval,byte *outval,int b)
{
int i;
for(i=0;i<b;i++) {
outval[i] = (inval>>(i*8)) & 0xff;
}
}
/* always big endian */
static void long2bigendian(long inval,byte *outval,int b)
{
int i;
for(i=0;i<b;i++) {
outval[i] = (inval>>((b-i-1)*8)) & 0xff;
}
}
static int testEndian(void)
{
long i,a=0,b=0,c=0;
int ret = 0;
for(i=0;i<sizeof(long);i++) {
((byte *)&a)[i] = i;
b<<=8;
b |= i;
c |= i << (i*8);
}
if(a == b)
ret = 1;
else if(a != c) {
fprintf(stderr,"Strange endianess?? %08lx %08lx %08lx\n",a,b,c);
exit(1);
}
return ret;
}
FILE* wave_open(char *wavfilename,WAVE_INFO *wave_info)
{
int bps;
FILE *wavefile;
int flipendian = 0;
/* standard MS PCM, and its format specific is BitsPerSample */
long2littleendian(1,RIFF.WAVE.fmt.FormatTag,sizeof(RIFF.WAVE.fmt.FormatTag));
long2littleendian(bps=16,RIFF.WAVE.fmt.BitsPerSample,sizeof(RIFF.WAVE.fmt.BitsPerSample));
/* if(wave_info->format == AUDIO_FORMAT_SIGNED_16) {
flipendian = testEndian();
}
else if(wave_info->format == AUDIO_FORMAT_UNSIGNED_8)
long2littleendian(bps=8,RIFF.WAVE.fmt.BitsPerSample,sizeof(RIFF.WAVE.fmt.BitsPerSample));
else
{
fprintf(stderr,"Format not supported.");
return -1;
}*/
if(wave_info->samplerate < 0) wave_info->samplerate = 44100;
long2littleendian(wave_info->channels,RIFF.WAVE.fmt.Channels,sizeof(RIFF.WAVE.fmt.Channels));
long2littleendian(wave_info->samplerate,RIFF.WAVE.fmt.SamplesPerSec,sizeof(RIFF.WAVE.fmt.SamplesPerSec));
long2littleendian((int)(wave_info->channels * wave_info->samplerate * bps)>>3,
RIFF.WAVE.fmt.AvgBytesPerSec,sizeof(RIFF.WAVE.fmt.AvgBytesPerSec));
long2littleendian((int)(wave_info->channels * bps)>>3,
RIFF.WAVE.fmt.BlockAlign,sizeof(RIFF.WAVE.fmt.BlockAlign));
if( (wavefile = fopen(wavfilename, "wb" )) == NULL ) ERROROUT("The file 'data' was not opened\n");
long2littleendian(0,RIFF.WAVE.data.datalen,sizeof(RIFF.WAVE.data.datalen));
long2littleendian(sizeof(RIFF.WAVE),RIFF.WAVElen,sizeof(RIFF.WAVElen));
fwrite(&RIFF, sizeof(RIFF),1,wavefile);
return wavefile;
}
void wave_write(FILE *wavefile,short * buf,int len,WAVE_INFO wave_info)
{
int temp;
// if(!wavefile) return 0;
temp = fwrite(buf, sizeof(short), len, wavefile);
if(temp <= 0) total_byte+=0;
total_byte+=temp;
// printf("temp %d", temp);
// printf("total_bytes %d\n",total_byte );
}
int wave_close(FILE *wavefile,WAVE_INFO wave_info)
{
if(!wavefile) return 0;
if(fseek(wavefile, 0L, SEEK_SET) >= 0) {
long2littleendian(total_byte*2,RIFF.WAVE.data.datalen,sizeof(RIFF.WAVE.data.datalen));
long2littleendian(total_byte*2+sizeof(RIFF.WAVE),RIFF.WAVElen,sizeof(RIFF.WAVElen));
fwrite(&RIFF, sizeof(RIFF),1,wavefile);
// printf("bytes %d", total_byte);
// printf("Samplerate %d", (int)RIFF.WAVE.fmt.SamplesPerSec);
}
else {
fprintf(stderr,"Warning can't rewind WAV file. File-format isn't fully conform now.\n");
}
fclose(wavefile);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -