wav.h
Go to the documentation of this file.
00001
00040 #ifndef  ALGO_WAV_H
00041 #define  ALGO_WAV_H
00042 
00043 #include <cfg/debug.h>
00044
00045 #include <cpu/types.h>
00046 #include <cpu/byteorder.h>
00047
00048 #include <string.h>
00049
00050
00051 typedef struct WavHdr
00052 {
00053     char chunk_id[4];
00054     uint32_t chunk_size;
00055     char format[4];
00056
00057     uint8_t subchunk1_id[4];
00058     uint32_t subchunk1_size;
00059     uint16_t audio_format;
00060     uint16_t num_channels;
00061     uint32_t sample_rate;
00062     uint32_t byte_rate;
00063     uint16_t block_align;
00064     uint16_t bits_per_sample;
00065
00066     uint8_t subchunk2_id[4];
00067     uint32_t subchunk2_size;
00068
00069 } PACKED WavHdr;
00070
00071 INLINE int wav_checkHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample)
00072 {
00073     ASSERT(wav);
00074
00075     if (strncmp(wav->chunk_id, "RIFF", 4))
00076     {
00077         kputs("RIFF tag not found\n");
00078         goto error;
00079     }
00080
00081     if (strncmp(wav->format, "WAVE", 4))
00082     {
00083         kputs("WAVE tag not found\n");
00084         goto error;
00085     }
00086
00087     if (le16_to_cpu(wav->audio_format) != audio_format)
00088     {
00089         kprintf("Audio format not valid, found [%d]\n", le16_to_cpu(wav->audio_format));
00090         goto error;
00091     }
00092
00093     if (le16_to_cpu(wav->num_channels) != num_channels)
00094     {
00095         kprintf("Channels number not valid, found [%d]\n", le16_to_cpu(wav->num_channels));
00096         goto error;
00097     }
00098
00099
00100     if (le32_to_cpu(wav->sample_rate) != sample_rate)
00101     {
00102         kprintf("Sample rate not valid, found [%ld]\n", le32_to_cpu(wav->sample_rate));
00103         goto error;
00104     }
00105
00106     if (le16_to_cpu(wav->bits_per_sample) != bits_per_sample)
00107     {
00108         kprintf("Bits per sample not valid, found [%d]\n", le16_to_cpu(wav->bits_per_sample));
00109         goto error;
00110     }
00111     return 0;
00112
00113 error:
00114     return -1;
00115 }
00116
00117
00118 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)
00119 {
00120     ASSERT(wav);
00121
00122     uint32_t sub2 = sample_num * num_channels * bits_per_sample / 8;
00123
00124     memcpy(&wav->chunk_id, "RIFF", 4);
00125     memcpy(&wav->format, "WAVE", 4);
00126
00127     memcpy(&wav->subchunk1_id, "fmt ", 4);
00128     wav->subchunk1_size = cpu_to_le32(16);
00129
00130     memcpy(&wav->subchunk2_id, "data", 4);
00131     wav->subchunk2_size = cpu_to_le32(sub2);
00132
00133     wav->audio_format = cpu_to_le16(audio_format);
00134     wav->num_channels = cpu_to_le16(num_channels);
00135     wav->sample_rate = cpu_to_le32(sample_rate);
00136     wav->bits_per_sample = cpu_to_le16(bits_per_sample);
00137
00138     wav->chunk_size = cpu_to_le32(36 + sub2);
00139     kprintf("CHUNK_SIZE %ld\n", 36 + sub2);
00140 }
00141
00142 #endif /* ALGO_WAV_H */