Add some utility for wav files.
[bertos.git] / bertos / algo / wav.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief WAV audio utils.
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  *
37  * $WIZ$ module_name = "wav"
38  */
39
40 #ifndef  ALGO_WAV_H
41 #define  ALGO_WAV_H
42
43 #include <cfg/debug.h>
44
45 #include <cpu/types.h>
46 #include <cpu/byteorder.h>
47
48 #include <string.h>
49
50 typedef struct WavHdr
51 {
52         char chunk_id[4];
53         uint32_t chunk_size;
54         char format[4];
55
56         char subchunk1_id[4];
57         uint32_t subchunk1_size;
58         uint16_t audio_format;
59         uint16_t num_channels;
60         uint32_t sample_rate;
61         uint32_t byte_rate;
62         uint16_t block_align;
63         uint16_t bits_per_sample;
64
65         uint8_t subchunk2[8];
66 } WavHdr;
67
68 INLINE int wav_checkHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample)
69 {
70         ASSERT(wav);
71
72         if (strncmp(wav->chunk_id, "RIFF", 4))
73         {
74                 kputs("RIFF tag not found\n");
75                 goto error;
76         }
77
78         if (strncmp(wav->format, "WAVE", 4))
79         {
80                 kputs("WAVE tag not found\n");
81                 goto error;
82         }
83
84         if (le16_to_cpu(wav->audio_format) != audio_format)
85         {
86                 kprintf("Audio format not valid, found [%d]\n", le16_to_cpu(wav->audio_format));
87                 goto error;
88         }
89
90         if (le16_to_cpu(wav->num_channels) != num_channels)
91         {
92                 kprintf("Channels number not valid, found [%d]\n", le16_to_cpu(wav->num_channels));
93                 goto error;
94         }
95
96
97         if (le32_to_cpu(wav->sample_rate) != sample_rate)
98         {
99                 kprintf("Sample rate not valid, found [%ld]\n", le32_to_cpu(wav->sample_rate));
100                 goto error;
101         }
102
103         if (le16_to_cpu(wav->bits_per_sample) != bits_per_sample)
104         {
105                 kprintf("Bits per sample not valid, found [%d]\n", le16_to_cpu(wav->bits_per_sample));
106                 goto error;
107         }
108         return 0;
109
110 error:
111         return -1;
112 }
113
114
115 INLINE void wav_writeHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample)
116 {
117         ASSERT(wav);
118
119         memcpy(&wav->chunk_id, "RIFF", 4);
120         memcpy(&wav->format, "WAVE", 4);
121         wav->audio_format = cpu_to_le16(audio_format);
122         wav->num_channels = cpu_to_le16(num_channels);
123         wav->sample_rate = cpu_to_le16(sample_rate);
124         wav->bits_per_sample = cpu_to_le16(bits_per_sample);
125 }
126
127 #endif /* ALGO_WAV_H */