Fix function to fill the needed header fields, for valid wav file.
authorasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Mon, 26 Sep 2011 08:50:49 +0000 (08:50 +0000)
committerasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Mon, 26 Sep 2011 08:50:49 +0000 (08:50 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5088 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/algo/wav.h

index 4cd0e31acca87a0dd773915a9e2b9aec2a0175c3..c9274d356fdfb43edaefab20bb906c00162b6459 100644 (file)
 
 #include <string.h>
 
+
 typedef struct WavHdr
 {
        char chunk_id[4];
        uint32_t chunk_size;
        char format[4];
 
-       char subchunk1_id[4];
+       uint8_t subchunk1_id[4];
        uint32_t subchunk1_size;
        uint16_t audio_format;
        uint16_t num_channels;
@@ -62,8 +63,10 @@ typedef struct WavHdr
        uint16_t block_align;
        uint16_t bits_per_sample;
 
-       uint8_t subchunk2[8];
-} WavHdr;
+       uint8_t subchunk2_id[4];
+       uint32_t subchunk2_size;
+
+} PACKED WavHdr;
 
 INLINE int wav_checkHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample)
 {
@@ -112,16 +115,28 @@ error:
 }
 
 
-INLINE void wav_writeHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample)
+INLINE void wav_writeHdr(WavHdr *wav, size_t sample_num, uint16_t audio_format, uint16_t num_channels, uint32_t sample_rate, uint16_t bits_per_sample)
 {
        ASSERT(wav);
 
+       uint32_t sub2 = sample_num * num_channels * bits_per_sample / 8;
+
        memcpy(&wav->chunk_id, "RIFF", 4);
        memcpy(&wav->format, "WAVE", 4);
+
+       memcpy(&wav->subchunk1_id, "fmt ", 4);
+       wav->subchunk1_size = cpu_to_le32(16);
+
+       memcpy(&wav->subchunk2_id, "data", 4);
+       wav->subchunk2_size = cpu_to_le32(sub2);
+
        wav->audio_format = cpu_to_le16(audio_format);
        wav->num_channels = cpu_to_le16(num_channels);
-       wav->sample_rate = cpu_to_le16(sample_rate);
+       wav->sample_rate = cpu_to_le32(sample_rate);
        wav->bits_per_sample = cpu_to_le16(bits_per_sample);
+
+       wav->chunk_size = cpu_to_le32(36 + sub2);
+       kprintf("CHUNK_SIZE %ld\n", 36 + sub2);
 }
 
 #endif /* ALGO_WAV_H */