xref: /btstack/platform/posix/wav_util.c (revision a9ec7ee053cb5b64c1651a56c2d80606d96c8a9d)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "wav_util.c"
39 
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "wav_util.h"
46 #include "btstack_util.h"
47 #include "btstack_debug.h"
48 
49 /* wav reader state */
50 static FILE * wav_reader_file;
51 static int wav_reader_bytes_per_sample = 2;
52 static struct {
53     uint8_t num_channels;
54     uint32_t sampling_rate;
55 } wav_reader_state;
56 
57 /* wav writer state */
58 static struct {
59     FILE * wav_file;
60     int total_num_samples;
61     int num_channels;
62     int sampling_frequency;
63     int frame_count;
64 } wav_writer_state;
65 
66 
67 static void little_endian_fstore_16(FILE *wav_file, uint16_t value){
68     uint8_t buf[2];
69     little_endian_store_16(buf, 0, value);
70     fwrite(&buf, 1, 2, wav_file);
71 }
72 
73 static void little_endian_fstore_32(FILE *wav_file, uint32_t value){
74     uint8_t buf[4];
75     little_endian_store_32(buf, 0, value);
76     fwrite(&buf, 1, 4, wav_file);
77 }
78 
79 static void write_wav_header(FILE * wav_file,  int total_num_samples, int num_channels, int sample_rate){
80     unsigned int write_with_bytes_per_sample = 2;
81     /* write RIFF header */
82     fwrite("RIFF", 1, 4, wav_file);
83     // num_samples = blocks * subbands
84     uint32_t data_bytes = (uint32_t) (write_with_bytes_per_sample * total_num_samples * num_channels);
85     little_endian_fstore_32(wav_file, data_bytes + 36);
86     fwrite("WAVE", 1, 4, wav_file);
87 
88     int byte_rate = sample_rate * num_channels * write_with_bytes_per_sample;
89     int bits_per_sample = 8 * write_with_bytes_per_sample;
90     int block_align = num_channels * bits_per_sample;
91     int fmt_length = 16;
92     int fmt_format_tag = 1; // PCM
93 
94     /* write fmt chunk */
95     fwrite("fmt ", 1, 4, wav_file);
96     little_endian_fstore_32(wav_file, fmt_length);
97     little_endian_fstore_16(wav_file, fmt_format_tag);
98     little_endian_fstore_16(wav_file, num_channels);
99     little_endian_fstore_32(wav_file, sample_rate);
100     little_endian_fstore_32(wav_file, byte_rate);
101     little_endian_fstore_16(wav_file, block_align);
102     little_endian_fstore_16(wav_file, bits_per_sample);
103 
104     /* write data chunk */
105     fwrite("data", 1, 4, wav_file);
106     little_endian_fstore_32(wav_file, data_bytes);
107 }
108 
109 int wav_writer_open(const char * filepath, int num_channels, int sampling_frequency){
110     FILE * wav_file = fopen(filepath, "wb");
111     if (!wav_file) return 1;
112 
113     wav_writer_state.wav_file = wav_file;
114     wav_writer_state.frame_count = 0;
115     wav_writer_state.total_num_samples = 0;
116     wav_writer_state.num_channels = num_channels;
117     wav_writer_state.sampling_frequency = sampling_frequency;
118     write_wav_header(wav_writer_state.wav_file, 0, num_channels, sampling_frequency);
119     return 0;
120 }
121 
122 int wav_writer_close(void){
123     if (wav_writer_state.wav_file == NULL) return 0;
124     rewind(wav_writer_state.wav_file);
125     write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples,
126         wav_writer_state.num_channels, wav_writer_state.sampling_frequency);
127     fclose(wav_writer_state.wav_file);
128     wav_writer_state.wav_file = NULL;
129     return 0;
130 }
131 
132 int wav_writer_write_int8(int num_samples, int8_t * data){
133     btstack_assert(data != NULL);
134     if (wav_writer_state.wav_file == NULL) return 1;
135 
136     int i = 0;
137     int8_t zero_byte = 0;
138     for (i=0; i<num_samples; i++){
139         fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file);
140         uint8_t byte_value = (uint8_t)data[i];
141         fwrite(&byte_value, 1, 1, wav_writer_state.wav_file);
142     }
143 
144     wav_writer_state.total_num_samples+=num_samples;
145     wav_writer_state.frame_count++;
146     return 0;
147 }
148 
149 int wav_writer_write_le_int16(int num_samples, int16_t * data){
150     btstack_assert(data != NULL);
151     if (wav_writer_state.wav_file == NULL) return 1;
152 
153     fwrite(data, num_samples, 2, wav_writer_state.wav_file);
154 
155     wav_writer_state.total_num_samples+=num_samples;
156     wav_writer_state.frame_count++;
157     return 0;
158 }
159 
160 int wav_writer_write_int16(int num_samples, int16_t * data){
161     btstack_assert(data != NULL);
162     if (wav_writer_state.wav_file == NULL) return 1;
163 
164     if (btstack_is_little_endian()){
165         return wav_writer_write_le_int16(num_samples, data);
166     }
167 
168     int i;
169     for (i=0;i<num_samples;i++){
170         uint16_t sample = btstack_flip_16(data[i]);
171         fwrite(&sample, 1, 2, wav_writer_state.wav_file);
172     }
173 
174     wav_writer_state.total_num_samples+=num_samples;
175     wav_writer_state.frame_count++;
176     return 0;
177 }
178 
179 /** WAV Reader Implementation */
180 
181 int wav_reader_open(const char * filepath){
182     memset (&wav_reader_state, 0, sizeof(wav_reader_state));
183     wav_reader_file = fopen(filepath, "rb");
184     if (wav_reader_file == NULL) {
185         log_error("Can't open file %s", filepath);
186         return 1;
187     }
188 
189     uint8_t buf[40];
190     fread(buf, 1, sizeof(buf), wav_reader_file);
191 
192     wav_reader_state.num_channels  = (uint8_t) little_endian_read_16(buf, 22);
193     if ((wav_reader_state.num_channels < 1) || (wav_reader_state.num_channels > 2)) {
194         log_error("Unexpected num channels %d", wav_reader_state.num_channels);
195         return 1;
196     }
197 
198     wav_reader_state.sampling_rate = little_endian_read_32(buf, 24);
199 
200     int block_align = little_endian_read_16(buf, 32);
201     wav_reader_bytes_per_sample = block_align / wav_reader_state.num_channels;
202     if (wav_reader_bytes_per_sample > 2){
203         wav_reader_bytes_per_sample = wav_reader_bytes_per_sample / 8;
204     }
205 
206     return 0;
207 }
208 
209 uint8_t wav_reader_get_num_channels(void){
210     return wav_reader_state.num_channels;
211 }
212 
213 uint32_t wav_reader_get_sampling_rate(void){
214     return wav_reader_state.sampling_rate;
215 }
216 
217 int wav_reader_close(void){
218     if (wav_reader_file != NULL){
219         fclose(wav_reader_file);
220     }
221     return 0;
222 }
223 
224 // Wav data: 8bit is uint8_t; 16bit is int16
225 int wav_reader_read_int8(int num_samples, int8_t * data){
226     if (wav_reader_file == NULL) {
227         return 1;
228     }
229     int i;
230     int bytes_read = 0;
231 
232     for (i=0; i<num_samples; i++){
233         if (wav_reader_bytes_per_sample == 2){
234             uint8_t buf[2];
235             bytes_read += (uint16_t) fread(buf, 1, sizeof(buf), wav_reader_file);;
236             data[i] = buf[1];
237         } else {
238             uint8_t buf[1];
239             bytes_read += (uint16_t) fread(buf, 1, sizeof(buf), wav_reader_file);;
240             data[i] = buf[0] + 128;
241         }
242     }
243     if (bytes_read == num_samples * wav_reader_bytes_per_sample) {
244         return 0;
245     } else {
246         return 1;
247     }
248 }
249 
250 int wav_reader_read_int16(int num_samples, int16_t * data){
251     if (wav_reader_file == NULL) {
252         return 1;
253     }
254     int i;
255     int bytes_read = 0;
256     for (i=0; i<num_samples; i++){
257         uint8_t buf[2];
258         bytes_read += (uint16_t) fread(buf, 1, sizeof(buf), wav_reader_file);;
259         data[i] = little_endian_read_16(buf, 0);
260     }
261     if (bytes_read == num_samples * wav_reader_bytes_per_sample) {
262         return 0;
263     } else {
264         return 1;
265     }
266 }
267 
268 
269