Pas de dépendances
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define FREQUENCE 44100 #define BITPERSAMPLE 16 typedef int int32; typedef short int16; typedef char int8; struct WavHeader { int8 FileTypeBlocID[4]; int32 FileSize; int8 FileFormatID[4]; }; struct WavFmt { int8 FormatBlocID[4]; int32 BlocSize; int16 AudioFormat; int16 NbrCanaux; int32 Frequence; int32 BytePerSec; int16 BytePerBloc; int16 BitsPerSample; }; FILE * wavfile_open( const char *filename ) { struct WavHeader head = {{'R','I','F','F'},0,{'W','A','V','E'}}; struct WavFmt Fmt = {{'f','m','t',' '},16,1,1,FREQUENCE,FREQUENCE*BITPERSAMPLE/8,BITPERSAMPLE/8,BITPERSAMPLE}; FILE* F; F = fopen(filename,"wb"); if(!F) return 0; fwrite(&head,sizeof(head),1,F); fwrite(&Fmt,sizeof(Fmt),1,F); return F; } void wavfile_write(FILE *F, int16* data, int32 length ) { char subhead[5] = "data"; fwrite(subhead,4,1,F); fwrite(&length,sizeof(length),1,F); fwrite(data,sizeof(short),length,F); } void wavfile_close( FILE *file ) { int file_length = ftell(file); int32 FileSize = file_length - 8; fseek(file,4,SEEK_SET); fwrite(&FileSize,sizeof(FileSize),1,file); fclose(file); } int main() { FILE* F; int i; int nbsecondes = 10; int updown = 1; int16* datas = malloc(nbsecondes*FREQUENCE*sizeof(int16)); for(i=0;i<nbsecondes*FREQUENCE;i++) { int largeurcarre = ((i/FREQUENCE)+1)*100; if (i%largeurcarre==0) updown= -1*updown; datas[i] = updown*16384; } F = wavfile_open("sound.wav"); if (!F) return -1; wavfile_write(F,datas,nbsecondes*FREQUENCE); free(datas); wavfile_close(F); return 0; }
No explanations yet.