From: asterix Date: Tue, 20 Sep 2011 15:42:52 +0000 (+0000) Subject: Add some utility for wav files. X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=137cd684fcc9cbb111f3cf195891415d8c38cf0d;p=bertos.git Add some utility for wav files. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5076 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/algo/wav.h b/bertos/algo/wav.h new file mode 100644 index 00000000..4cd0e31a --- /dev/null +++ b/bertos/algo/wav.h @@ -0,0 +1,127 @@ +/** + * \file + * + * + * \brief WAV audio utils. + * + * \author Daniele Basile + * + * $WIZ$ module_name = "wav" + */ + +#ifndef ALGO_WAV_H +#define ALGO_WAV_H + +#include + +#include +#include + +#include + +typedef struct WavHdr +{ + char chunk_id[4]; + uint32_t chunk_size; + char format[4]; + + char subchunk1_id[4]; + uint32_t subchunk1_size; + uint16_t audio_format; + uint16_t num_channels; + uint32_t sample_rate; + uint32_t byte_rate; + uint16_t block_align; + uint16_t bits_per_sample; + + uint8_t subchunk2[8]; +} WavHdr; + +INLINE int wav_checkHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample) +{ + ASSERT(wav); + + if (strncmp(wav->chunk_id, "RIFF", 4)) + { + kputs("RIFF tag not found\n"); + goto error; + } + + if (strncmp(wav->format, "WAVE", 4)) + { + kputs("WAVE tag not found\n"); + goto error; + } + + if (le16_to_cpu(wav->audio_format) != audio_format) + { + kprintf("Audio format not valid, found [%d]\n", le16_to_cpu(wav->audio_format)); + goto error; + } + + if (le16_to_cpu(wav->num_channels) != num_channels) + { + kprintf("Channels number not valid, found [%d]\n", le16_to_cpu(wav->num_channels)); + goto error; + } + + + if (le32_to_cpu(wav->sample_rate) != sample_rate) + { + kprintf("Sample rate not valid, found [%ld]\n", le32_to_cpu(wav->sample_rate)); + goto error; + } + + if (le16_to_cpu(wav->bits_per_sample) != bits_per_sample) + { + kprintf("Bits per sample not valid, found [%d]\n", le16_to_cpu(wav->bits_per_sample)); + goto error; + } + return 0; + +error: + return -1; +} + + +INLINE void wav_writeHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample) +{ + ASSERT(wav); + + memcpy(&wav->chunk_id, "RIFF", 4); + memcpy(&wav->format, "WAVE", 4); + 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->bits_per_sample = cpu_to_le16(bits_per_sample); +} + +#endif /* ALGO_WAV_H */