/********************************************************************************* * Write to the data chunk portion of a WAV file. * Returns bytes written or negative error code. */ int Audio_WAV_WriteShorts( WAV_Writer *writer, short *samples, int numSamples) { unsigned char buffer[2]; unsigned char *bufferPtr; int i; short *p = samples; int numWritten; int bytesWritten; if (numSamples <= 0) { return -1; } for (i=0; i<numSamples; i++) { bufferPtr = buffer; WriteShortLE(&bufferPtr, *p++); numWritten = fwrite(buffer, 1, sizeof(buffer), writer->fid); if (numWritten != sizeof(buffer)) return -1; } bytesWritten = numSamples * sizeof(short); writer->dataSize += bytesWritten; return bytesWritten; }
void CEDPacket::WriteEDString(LPCTSTR psz, BOOL bUnicode) { int nLen; if ( bUnicode ) { nLen = GetStringLenUTF8( psz ); WriteShortLE( nLen ); WriteStringUTF8( psz, FALSE ); } else { nLen = GetStringLen( psz ); WriteShortLE( nLen ); WriteString( psz, FALSE ); } ASSERT( nLen <= 0xFFFF ); }
void CEDPacket::WriteEDString(LPCTSTR psz, DWORD ServerFlags) { int nLen; if ( ServerFlags & ED2K_SERVER_TCP_UNICODE ) { nLen = GetStringLenUTF8( psz ); WriteShortLE( nLen ); WriteStringUTF8( psz, FALSE ); } else { nLen = GetStringLen( psz ); WriteShortLE( nLen ); WriteString( psz, FALSE ); } ASSERT( nLen <= 0xFFFF ); }
/********************************************************************************* * Open named file and write WAV header to the file. * The header includes the DATA chunk type and size. * Returns number of bytes written to file or negative error code. */ int Audio_WAV_OpenWriter(WAV_Writer *writer, const char *fileName, int frameRate, int samplesPerFrame) { unsigned int bytesPerSecond; unsigned char header[WAV_HEADER_SIZE]; unsigned char *addr = header; int numWritten; writer->dataSize = 0; writer->dataSizeOffset = 0; writer->fid = fopen(fileName, "wb"); if (writer->fid == NULL) { return -1; } /* Write RIFF header. */ WriteChunkType(&addr, RIFF_ID); /* Write RIFF size as zero for now. Will patch later. */ WriteLongLE(&addr, 0); /* Write WAVE form ID. */ WriteChunkType(&addr, WAVE_ID); /* Write format chunk based on AudioSample structure. */ WriteChunkType(&addr, FMT_ID); WriteLongLE(&addr, 16); WriteShortLE(&addr, WAVE_FORMAT_PCM); bytesPerSecond = frameRate * samplesPerFrame * sizeof(int16_t); WriteShortLE(&addr, (int16_t) samplesPerFrame); WriteLongLE(&addr, frameRate); WriteLongLE(&addr, bytesPerSecond); WriteShortLE(&addr, (int16_t) (samplesPerFrame * sizeof(int16_t))); /* bytesPerBlock */ WriteShortLE(&addr, (int16_t) 16); /* bits per sample */ /* Write ID and size for 'data' chunk. */ WriteChunkType(&addr, DATA_ID); /* Save offset so we can patch it later. */ writer->dataSizeOffset = static_cast<int>(addr - header); WriteLongLE(&addr, 0); numWritten = fwrite(header, 1, sizeof(header), writer->fid ); if (numWritten != sizeof(header)) return -1; return numWritten; }