//■WAVファイル構成 // // バイト位置 バイト数 意味 //----------------------------------------------------------------------------------------- // 00 4 byte R' 'I' 'F' 'F' RIFFヘッダ   // 04 4 byte これ以降のファイルサイズ (ファイルサイズ - 8)     // 08 4 byte W' 'A' 'V' 'E' WAVEヘッダ RIFFの種類がWAVEであることをあらわす // 12 4 byte f' 'm' 't' ' ' (←スペースも含む) fmt チャンク フォーマットの定義 // 16 4 byte バイト数 fmt チャンクのバイト数 リニアPCM のとき 16(10 00 00 00) // 20 2 byte フォーマットID 参照 リニアPCM のとき 1(01 00) // 22 2 byte チャンネル数   モノラル のとき 1(01 00) ステレオ のとき 2(02 00) // 24 4 byte サンプリングレート Hz 44.1kHz のとき 44100(44 AC 00 00) // 28 4 byte データ速度 (Byte/sec)   44.1kHz 16bit ステレオ のとき  // 44100×2×2 = 176400(10 B1 02 00) // 30 2 byte ブロックサイズ (Byte/sample×チャンネル数) // 16bit ステレオ のとき2×2 = 4(04 00) // 32 2 byte サンプルあたりのビット数 (bit/sample) WAV フォーマットでは 8bit か // 16bit。16bit のとき 16(10 00) // 34 2 byte 拡張部分のサイズ   リニアPCMのとき存在しない // 38 n byte 拡張部分   リニアPCMのとき存在しない // 38 + n 4 byte d' 'a' 't' 'a' data チャンク 参照   // 42 + n 4 byte バイト数n 波形データのバイト数   // 46 + n m byte 波形データ //*以下3行は再生用 #include "windows.h" //*再生用宣言 #include "MMSystem.h" //*再生用宣言 #pragma comment(lib,"winmm.lib")//*再生用宣言 #include "stdio.h" #include "stdlib.h" #include "math.h" #include "string.h" // 44100Hz, 8bit, 1ch のWAVデータ #define SAMPLFRQ 44100 #define BIT 8 void intToByte(char Big[], int Little){//32ビット整数格納 Big[0] = Little & 0xff; Big[1] = Little >> 8 & 0xff; Big[2] = Little >> 16 & 0xff; Big[3] = Little >> 24 & 0xff; } void shortToByte(char Big[], int Little){//16整数ビット格納 Big[0] = Little & 0xff; Big[1] = Little >> 8 & 0xff; } int wav_write(const char *filename, char *buf, int size){ int filesize = 44 + size; char *tmp; tmp = (char *) malloc(filesize);//領域確保 if(tmp==NULL) return 1; //■出力データ設定------------------------------------------------------------------------- memcpy(tmp, "RIFF", 4); // ■RIFFヘッダ intToByte(tmp+4,filesize - 8); // これ以降のファイルサイズ (ファイルサイズ - 8) memcpy(tmp+8, "WAVE", 4); // ■WAVEヘッダ memcpy(tmp+12, "fmt ", 4); // ■fmtチャンク intToByte(tmp+16,16 ); // バイト数 fmt チャンクのバイト数(リニアPCM のとき 16(10 00 00 00)) shortToByte(tmp+20, 1); // フォーマットID (リニアPCM のとき 1(01 00)) shortToByte(tmp+22, 1); // チャンネル数 モノラル のとき 1(01 00) intToByte(tmp+24,SAMPLFRQ ); // サンプリングレート intToByte(tmp+28,SAMPLFRQ * (BIT/8)); // データ速度 (Byte/sec) shortToByte(tmp+32, BIT / 8); // ブロックサイズ shortToByte(tmp+34, BIT); // サンプルあたりのビット数 memcpy(tmp+36, "data", 4); // ■data チャンク intToByte(tmp+40,size); // バイト数 memcpy(tmp + 44, buf, size); // 音声複写 //■ファイル出力---------------------------------------------------------------------------- FILE *fp = fopen(filename, "wb"); if (fp == NULL) return 2; fwrite(tmp, filesize, 1, fp); // ファイルへの出力 fclose(fp); // ファイルクローズ free(tmp); // 領域返却 return 0; } int bufSize(int sec){return (SAMPLFRQ*(BIT/8)) * sec;} int main(void){ double PI=3.14159265358979, w; int size = bufSize(1); //1秒間 char *buf=(char *) malloc(size); for(int i=0; i