Mercurial > hg > dmlib
changeset 205:9622c139cb9f
Separate the WAV header / chunk writing functions from mod2wav into dmwav.[ch]
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 07 Oct 2012 14:16:11 +0300 |
parents | d3a9a3804079 |
children | 9b6c0ed66960 |
files | dmwav.c dmwav.h |
diffstat | 2 files changed, 122 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmwav.c Sun Oct 07 14:16:11 2012 +0300 @@ -0,0 +1,62 @@ +/* + * DMLib + * -- Wav file writing + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + */ +#include "dmwav.h" +#include "dmfile.h" + + +BOOL dmWriteWAVChunk(FILE * f, DMWaveChunk *ch) +{ + if (!dm_fwrite_str(f, ch->chunkID, 4)) return FALSE; + return dm_fwrite_le32(f, ch->chunkSize); +} + + +void dmMakeWAVChunk(DMWaveChunk *ch, const char *chunkID, const Uint32 chunkSize) +{ + memcpy(&(ch->chunkID), (const void *) chunkID, 4); + ch->chunkSize = chunkSize; +} + + +void dmWriteWAVHeader(FILE *outFile, int sampBits, int sampFreq, int sampChn, size_t sampLen) +{ + DMWaveFile wav; + + // PCM WAVE chunk + dmMakeWAVChunk(&wav.chFormat, DM_WAVE_FMT_ID, (2 + 2 + 4 + 4 + 2 + 2)); + + wav.wFormatTag = DM_WAVE_FORMAT_PCM; + wav.nChannels = sampChn; + wav.nSamplesPerSec = sampFreq; + wav.nAvgBytesPerSec = (sampBits * sampChn * sampFreq) / 8; + wav.nBlockAlign = (sampBits * sampChn) / 8; + wav.wBitsPerSample = sampBits; + + // Data chunk + dmMakeWAVChunk(&wav.chData, DM_WAVE_DATA_ID, (sampLen * wav.nBlockAlign)); + + // RIFF header + memcpy(&wav.riffID, (const void *) DM_WAVE_RIFF_ID, 4); + memcpy(&wav.riffType, (const void *) DM_WAVE_WAVE_ID, 4); + wav.fileSize = ((4 + 4 + 4) + wav.chFormat.chunkSize + wav.chData.chunkSize); + + // Write header + dm_fwrite_str(outFile, wav.riffID, sizeof(wav.riffID)); + dm_fwrite_le32(outFile, wav.fileSize); + + dm_fwrite_str(outFile, wav.riffType, sizeof(wav.riffType)); + dmWriteWAVChunk(outFile, &wav.chFormat); + + dm_fwrite_le16(outFile, wav.wFormatTag); + dm_fwrite_le16(outFile, wav.nChannels); + dm_fwrite_le32(outFile, wav.nSamplesPerSec); + dm_fwrite_le32(outFile, wav.nAvgBytesPerSec); + dm_fwrite_le16(outFile, wav.nBlockAlign); + dm_fwrite_le16(outFile, wav.wBitsPerSample); + + dmWriteWAVChunk(outFile, &wav.chData); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmwav.h Sun Oct 07 14:16:11 2012 +0300 @@ -0,0 +1,60 @@ +/* + * DMLib + * -- Wav file writing + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + */ +#ifndef DMWAV_H +#define DMWAV_H + +#include "dmlib.h" +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define DM_WAVE_FORMAT_PCM (1) +#define DM_WAVE_RIFF_ID "RIFF" +#define DM_WAVE_WAVE_ID "WAVE" +#define DM_WAVE_FMT_ID "fmt " +#define DM_WAVE_DATA_ID "data" + + +typedef struct +{ + Uint8 chunkID[4]; + Uint32 chunkSize; +} DMWaveChunk; + + +typedef struct +{ + Uint8 riffID[4]; + Uint32 fileSize; + Uint8 riffType[4]; + + DMWaveChunk chFormat; + + Uint16 wFormatTag; + Uint16 nChannels; + Uint32 nSamplesPerSec; + Uint32 nAvgBytesPerSec; + Uint16 nBlockAlign; + Uint16 wBitsPerSample; + + DMWaveChunk chData; + // Data follows here +} DMWaveFile; + + +BOOL dmWriteWAVChunk(FILE * f, DMWaveChunk *ch); +void dmMakeWAVChunk(DMWaveChunk *ch, const char *chunkID, const Uint32 chunkSize); +void dmWriteWAVHeader(FILE *outFile, int sampBits, int sampFreq, int sampChn, size_t sampLen); + + +#ifdef __cplusplus +} +#endif + +#endif // DMWAV_H