// WaveXTest02.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include #include #include struct WaveData{ float * wData; int step; }; void PrepareOutFile(FILE *, unsigned long, int, int , int ); void testWavOut(void); int _tmain(int argc, _TCHAR* argv[]) { testWavOut(); return 0; } void testWavOut(void) { //Create Output File struct WaveData wavs[8]; FILE *fo; _wfopen_s(&fo, L"c:\\temp\\multichanneltest.wav", L"wb"); if (fo == NULL) { printf("Can not create %s\n", "c:\\temp\\multichanneltest.wav"); return; } int TotalDataSize = 0; TotalDataSize = 8 * 44100 * 4 * 3; //8channel, 44100, 4sec, 3byte. PrepareOutFile(fo, TotalDataSize, 8, 44100, 24); for ( int i = 0; i < 8; i++) { wavs[i].step = 100 * (i+1); wavs[i].wData = (float *)malloc(44100*4*sizeof(float)); } for (int i = 0; i < 44100*4; i++) { for(int j = 0; j < 8; j++) { wavs[j].wData[i] = (float)(cos(2*3.141592653589793*i/wavs[j].step) * 0.95); } } unsigned char * wWave_Out_Data;//[ 8 * 44100 * 4 * 3 ]; wWave_Out_Data = (unsigned char *)malloc(8 * 44100 * 4 * 3); long conv; for (int i = 0; i < 44100*4; i++) { for(int j = 0; j < 8; j++) { conv = (long)(wavs[j].wData[i] * 256.0 * 32768.0); wWave_Out_Data[ i * 4 * 2 * 3 + j * 3 ] = (unsigned char)( conv & 0xFF); wWave_Out_Data[ i * 4 * 2 * 3 + j * 3 + 1] = (unsigned char)((conv>>8 ) & 0xFF);; wWave_Out_Data[ i * 4 * 2 * 3 + j * 3 + 2] = (unsigned char)((conv>>16) & 0xFF);; } } fwrite(wWave_Out_Data, sizeof(char), 8 * 44100 * 4 * 3 , fo); fclose(fo); for (int i = 0; i < 8; i++) { free(wavs[i].wData); } free(wWave_Out_Data); } void PrepareOutFile(FILE * fo, unsigned long len, int channels, int Sample_sec, int BitFormat) { unsigned long temp_l; unsigned short temp_s; unsigned char s[20]; //Prepare Output Wave File // RIFF s[0] = 'R'; s[1] = 'I'; s[2] = 'F'; s[3] = 'F'; fwrite(s, 1, 4, fo); //filesize if (channels == 2) { temp_l = len + 36; } else //extended { temp_l = len + 120; } fwrite(&(temp_l), sizeof(long), 1, fo); // WAVE s[0] = 'W'; s[1] = 'A'; s[2] = 'V'; s[3] = 'E'; fwrite(s, 1, 4, fo); // fmt chunk s[0] = 'f'; s[1] = 'm'; s[2] = 't'; s[3] = ' '; fwrite(s, 1, 4, fo); // chunk size if (channels == 2) { temp_l = 16; } else //extended { temp_l = 40; } fwrite(&(temp_l), sizeof(long), 1, fo); // format PCM = 1 if (channels == 2) { temp_s = 1; } else //extended, 0xFFFE { temp_s = 0xFFFE; } fwrite(&(temp_s), sizeof(short), 1, fo); // channel stereo = 2, or Extended temp_s = channels; fwrite(&(temp_s), sizeof(short), 1, fo); // sample 44100, 48000, 88200, 96000 etc temp_l = Sample_sec; fwrite(&(temp_l), sizeof(long), 1, fo); // Bytes per sec temp_l = channels * BitFormat/8 * Sample_sec; fwrite(&(temp_l), sizeof(long), 1, fo); // Block (Bytes per sample&channel) temp_s = channels * BitFormat/8 ; fwrite(&(temp_s), sizeof(short), 1, fo); // Bits per sample temp_s = BitFormat ; fwrite(&(temp_s), sizeof(short), 1, fo); //Extended Format area if (channels > 2) { //22 bytes extended area temp_s = 22; fwrite(&(temp_s), sizeof(short), 1, fo); //bit width temp_s = BitFormat ; fwrite(&(temp_s), sizeof(short), 1, fo); //channel Mask. 2=0x03, 4=0x0F, 6=0x3F, 8=0xFF temp_l = (1<