Update preset.
[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
51 typedef struct WavHdr
52 {
53         char chunk_id[4];
54         uint32_t chunk_size;
55         char format[4];
56
57         uint8_t subchunk1_id[4];
58         uint32_t subchunk1_size;
59         uint16_t audio_format;
60         uint16_t num_channels;
61         uint32_t sample_rate;
62         uint32_t byte_rate;
63         uint16_t block_align;
64         uint16_t bits_per_sample;
65
66         uint8_t subchunk2_id[4];
67         uint32_t subchunk2_size;
68
69 } PACKED WavHdr;
70
71 INLINE int wav_checkHdr(WavHdr *wav, uint16_t audio_format, uint16_t num_channels, uint16_t sample_rate, uint16_t bits_per_sample)
72 {
73         ASSERT(wav);
74
75         if (strncmp(wav->chunk_id, "RIFF", 4))
76         {
77                 kputs("RIFF tag not found\n");
78                 goto error;
79         }
80
81         if (strncmp(wav->format, "WAVE", 4))
82         {
83                 kputs("WAVE tag not found\n");
84                 goto error;
85         }
86
87         if (le16_to_cpu(wav->audio_format) != audio_format)
88         {
89                 kprintf("Audio format not valid, found [%d]\n", le16_to_cpu(wav->audio_format));
90                 goto error;
91         }
92
93         if (le16_to_cpu(wav->num_channels) != num_channels)
94         {
95                 kprintf("Channels number not valid, found [%d]\n", le16_to_cpu(wav->num_channels));
96                 goto error;
97         }
98
99
100         if (le32_to_cpu(wav->sample_rate) != sample_rate)
101         {
102                 kprintf("Sample rate not valid, found [%ld]\n", le32_to_cpu(wav->sample_rate));
103                 goto error;
104         }
105
106         if (le16_to_cpu(wav->bits_per_sample) != bits_per_sample)
107         {
108                 kprintf("Bits per sample not valid, found [%d]\n", le16_to_cpu(wav->bits_per_sample));
109                 goto error;
110         }
111         return 0;
112
113 error:
114         return -1;
115 }
116
117
118 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)
119 {
120         ASSERT(wav);
121
122         uint32_t sub2 = sample_num * num_channels * bits_per_sample / 8;
123
124         memcpy(&wav->chunk_id, "RIFF", 4);
125         memcpy(&wav->format, "WAVE", 4);
126
127         memcpy(&wav->subchunk1_id, "fmt ", 4);
128         wav->subchunk1_size = cpu_to_le32(16);
129
130         memcpy(&wav->subchunk2_id, "data", 4);
131         wav->subchunk2_size = cpu_to_le32(sub2);
132
133         wav->audio_format = cpu_to_le16(audio_format);
134         wav->num_channels = cpu_to_le16(num_channels);
135         wav->sample_rate = cpu_to_le32(sample_rate);
136         wav->bits_per_sample = cpu_to_le16(bits_per_sample);
137
138         wav->chunk_size = cpu_to_le32(36 + sub2);
139         kprintf("CHUNK_SIZE %ld\n", 36 + sub2);
140 }
141
142 #endif /* ALGO_WAV_H */