// ====================================================================== // W space: Multi star - fractal sound creation tool // (CC) 2009 Jens Koeplinger, http://www.jenskoeplinger.com/P // License: Creative Commons Attribution "Share Alike 3.0 Unported" // http://creativecommons.org/licenses/by-sa/3.0 // See main file "wspacemultistarsound.c" for what this means to you! // ====================================================================== // lib-multistar-util.h // ---------------------------------------------------------------------- // utilities library // // Examples: /* FILE *filePointer; int totalDataLength; filePointer = fopen("soundfile.wav", "wb"); totalDataLength = 3528000; // 20 seconds 16 bit stereo at 44.1kHz ... writeOutputWaveFileHeader(); ... writeTwoByteInteger(filePointer, 16); fprintf(filePointer, "data"); writeFourByteInteger(filePointer, totalDataLength); */ // ---------------------------------------------------------------------- void writeTwoByteInteger(FILE *fp, int byteToWrite) { // only values from negative 0xFFFF to positive 0xFFFF are allowed if (byteToWrite < 0) { // add 0x10000 to negative values fputc((int) (byteToWrite + 0x10000) & 0xFF, fp); fputc((int) ((byteToWrite + 0x10000) >> 8) & 0xFF, fp); } else { fputc((int) byteToWrite & 0xFF, fp); fputc((int) (byteToWrite >> 8) & 0xFF, fp); } } // ---------------------------------------------------------------------- void writeFourByteInteger(FILE *fp, int byteToWrite) { // only positive values between 0 and 0xFFFFFFFF are allowed fputc((int) byteToWrite & 0xFF, fp); fputc((int) (byteToWrite >> 8) & 0xFF, fp); fputc((int) (byteToWrite >> 16) & 0xFF, fp); fputc((int) (byteToWrite >> 24) & 0xFF, fp); } // ---------------------------------------------------------------------- void writeOutputWaveFileHeader() { // opens the output wave file for writing, and // writes header information; currently supported // format: 16 bit stereo at 44.1kHz sampling rate. // // IN: totalDataLength is the length (in bytes) of the sample // fp is a file pointer; closed // OUT: fp (file pointer; file will be open) printf("Open file for output: "); printf(outFileName); printf("\n"); fp = fopen(outFileName, "wb"); printf("... write header\n"); fprintf(fp, "RIFF"); // put text RIFF writeFourByteInteger(fp, totalDataLength + 36); // length of data + header fprintf(fp, "WAVEfmt "); // write the text "WAVEfmt " writeFourByteInteger(fp, 0x10); // write 0x10 (four byte) writeTwoByteInteger(fp, 1); // write 0x01 (two byte) writeTwoByteInteger(fp, 2); // 2 channels (=stereo) writeFourByteInteger(fp, 44100); // sampled at 44.1kHz writeFourByteInteger(fp, 176400); // bytes per second (=sampling rate * 4) writeTwoByteInteger(fp, 4); // bytes per sample: 4 (both channels) writeTwoByteInteger(fp, 16); // write bits per channel per sample: 16 fprintf(fp, "data"); // write the text "data" writeFourByteInteger(fp, totalDataLength); // total length of the data // Header is written, // file is open; // may the data come! printf("... done.\n"); } // ---------------------------------------------------------------------- void closeOutputWaveFile() { printf("Close file\n"); fclose(fp); printf("... done.\n"); } // ----------------------------------------------------------------------