Revision: 14573
Updated Code
at August 30, 2009 10:19 by Zufolek
Updated Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> /* M_PI is declared in math.h */ #define PI M_PI typedef unsigned int UI; typedef unsigned long int UL; typedef unsigned short int US; typedef unsigned char UC; typedef signed int SI; typedef signed long int SL; typedef signed short int SS; typedef signed char SC; #define attr(a) __attribute__((a)) #define packed attr(packed) /* WAV header, 44-byte total */ typedef struct{ UL riff packed; UL len packed; UL wave packed; UL fmt packed; UL flen packed; US one packed; US chan packed; UL hz packed; UL bpsec packed; US bpsmp packed; US bitpsmp packed; UL dat packed; UL dlen packed; }WAVHDR; int savefile(const char*const s,const void*const m,const int ml){ FILE*f=fopen(s,"wb"); int ok=0; if(f){ ok=fwrite(m,1,ml,f)==ml; fclose(f); } return ok; } /* "converts" 4-char string to long int */ #define dw(a) (*(UL*)(a)) /* Makes 44-byte header for 8-bit WAV in memory usage: wavhdr(pointer,sampleRate,dataLength) */ void wavhdr(void*m,UL hz,UL dlen){ WAVHDR*p=m; p->riff=dw("RIFF"); p->len=dlen+44; p->wave=dw("WAVE"); p->fmt=dw("fmt "); p->flen=0x10; p->one=1; p->chan=1; p->hz=hz; p->bpsec=hz; p->bpsmp=1; p->bitpsmp=8; p->dat=dw("data"); p->dlen=dlen; } /* returns 8-bit sample for a sine wave */ UC sinewave(UL rate,float freq,UC amp,UL z){ return sin(z*((PI*2/rate)*freq))*amp+128; } /* make arbitrary audio data here */ void makeaud(UC*p,const UL rate,UL z){ float freq=500; UC amp=120; while(z--){ *p++=sinewave(rate,freq,amp,z); } } /* makes wav file */ void makewav(const UL rate,const UL dlen){ const UL mlen=dlen+44; UC*const m=malloc(mlen); if(m){ wavhdr(m,rate,dlen); makeaud(m+44,rate,dlen); savefile("out.wav",m,mlen); } } int main(){ if(sizeof(WAVHDR)!=44)puts("bad struct"); makewav(22050,64000); return 0; }
Revision: 14572
Updated Code
at June 7, 2009 09:26 by Zufolek
Updated Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #ifndef PI #define PI 3.141592653589793 #endif typedef unsigned int UI; typedef unsigned long int UL; typedef unsigned short int US; typedef unsigned char UC; typedef signed int SI; typedef signed long int SL; typedef signed short int SS; typedef signed char SC; #define attr(a) __attribute__((a)) #define packed attr(packed) /* WAV header, 44-byte total */ typedef struct{ UL riff packed; UL len packed; UL wave packed; UL fmt packed; UL flen packed; US one packed; US chan packed; UL hz packed; UL bpsec packed; US bpsmp packed; US bitpsmp packed; UL dat packed; UL dlen packed; }WAVHDR; int savefile(const char*const s,const void*const m,const int ml){ FILE*f=fopen(s,"wb"); int ok=0; if(f){ ok=fwrite(m,1,ml,f)==ml; fclose(f); } return ok; } /* "converts" 4-char string to long int */ #define dw(a) (*(UL*)(a)) /* Makes 44-byte header for 8-bit WAV in memory usage: wavhdr(pointer,sampleRate,dataLength) */ void wavhdr(void*m,UL hz,UL dlen){ WAVHDR*p=m; p->riff=dw("RIFF"); p->len=dlen+44; p->wave=dw("WAVE"); p->fmt=dw("fmt "); p->flen=0x10; p->one=1; p->chan=1; p->hz=hz; p->bpsec=hz; p->bpsmp=1; p->bitpsmp=8; p->dat=dw("data"); p->dlen=dlen; } /* returns 8-bit sample for a sine wave */ UC sinewave(UL rate,float freq,UC amp,UL z){ return sin(z*((PI*2/rate)*freq))*amp+128; } /* make arbitrary audio data here */ void makeaud(UC*p,const UL rate,UL z){ float freq=500; UC amp=120; while(z--){ *p++=sinewave(rate,freq,amp,z); } } /* makes wav file */ void makewav(const UL rate,const UL dlen){ const UL mlen=dlen+44; UC*const m=malloc(mlen); if(m){ wavhdr(m,rate,dlen); makeaud(m+44,rate,dlen); savefile("out.wav",m,mlen); } } int main(){ if(sizeof(WAVHDR)!=44)puts("bad struct"); makewav(22050,64000); return 0; }
Revision: 14571
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 7, 2009 09:14 by Zufolek
Initial Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #ifndef PI #define PI 3.141592653589793 #endif typedef unsigned int UI; typedef unsigned long int UL; typedef unsigned short int US; typedef unsigned char UC; typedef signed int SI; typedef signed long int SL; typedef signed short int SS; typedef signed char SC; #define attr(a) __attribute__((a)) #define packed attr(packed) /* WAV header, 44-byte total */ typedef struct{ UL riff packed; UL len packed; UL wave packed; UL fmt packed; UL flen packed; US one packed; US chan packed; UL hz packed; UL bpsec packed; US bpsmp packed; US bitpsmp packed; UL dat packed; UL dlen packed; }WAVHDR; int savefile(const char*const s,const void*const m,const int ml){ FILE*f=fopen(s,"wb"); int ok=0; if(f){ ok=fwrite(m,1,ml,f)==ml; fclose(f); } return ok; } /* "converts" 4-char string to long int */ #define dw(a) (*(UL*)(a)) /* Makes 44-byte header for 8-bit WAV in memory usage: wavhdr(pointer,sampleRate,dataLength) */ void wavhdr(void*m,UL hz,UL dlen){ WAVHDR*p=m; p->riff=dw("RIFF"); p->len=dlen+44; p->wave=dw("WAVE"); p->fmt=dw("fmt "); p->flen=0x10; p->one=1; p->chan=1; p->hz=hz; p->bpsec=hz; p->bpsmp=1; p->bitpsmp=8; p->dat=dw("data"); p->dlen=dlen; } /* returns 8-bit sample for a sine wave */ UC sinewave(UL rate,float freq,UC amp,UL z){ return sin(z*((PI*2/rate)*freq))*amp+128; } /* make arbitrary audio data here */ void makeaud(UC*p,const UL rate,UL z){ float freq=500; UC amp=120; while(z--){ *p++=sinewave(rate,freq,amp,z); } } /* makes wav file */ void makewav(const UL rate,const UL dlen){ const UL mlen=dlen+44; UC*const m=malloc(mlen); if(m){ wavhdr(m,rate,dlen); makeaud(m+44,rate,dlen); savefile("out.wav",m,mlen); } } int main(){ if(sizeof(WAVHDR)!=44)err("bad struct"); makewav(22050,64000); return 0; }
Initial URL
Initial Description
Makes a mono 8-bit (i.e. one byte per sample) wav file, "out.wav" containing a 500 Hertz sine wave signal. 22050 is the sample rate, and 64000 is the total size of audio data in bytes.
Initial Title
Make a Wav file
Initial Tags
file
Initial Language
C