xref: /btstack/platform/posix/wav_util.c (revision b28dc8004dd8d4fb9020a6dcd2bc81f05d36a008)
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 <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 
47 #include "wav_util.h"
48 #include "btstack_util.h"
49 #include "btstack_debug.h"
50 
51 static int wav_reader_fd;
52 static int bytes_per_sample = 2;
53 
54 /* Write wav file utils */
55 typedef struct wav_writer_state {
56     FILE * wav_file;
57     int total_num_samples;
58     int num_channels;
59     int sampling_frequency;
60     int frame_count;
61 } wav_writer_state_t;
62 
63 wav_writer_state_t wav_writer_state;
64 
65 
66 static void little_endian_fstore_16(FILE *wav_file, uint16_t value){
67     uint8_t buf[2];
68     little_endian_store_16(buf, 0, value);
69     fwrite(&buf, 1, 2, wav_file);
70 }
71 
72 static void little_endian_fstore_32(FILE *wav_file, uint32_t value){
73     uint8_t buf[4];
74     little_endian_store_32(buf, 0, value);
75     fwrite(&buf, 1, 4, wav_file);
76 }
77 
78 static ssize_t __read(int fd, void *buf, size_t count){
79     ssize_t len, pos = 0;
80 
81     while (count > 0) {
82         len = read(fd, (int8_t * )buf + pos, count);
83         if (len <= 0)
84             return pos;
85 
86         count -= len;
87         pos   += len;
88     }
89     return pos;
90 }
91 
92 static void write_wav_header(FILE * wav_file,  int total_num_samples, int num_channels, int sample_rate){
93     unsigned int write_with_bytes_per_sample = 2;
94     /* write RIFF header */
95     fwrite("RIFF", 1, 4, wav_file);
96     // num_samples = blocks * subbands
97     uint32_t data_bytes = (uint32_t) (write_with_bytes_per_sample * total_num_samples * num_channels);
98     little_endian_fstore_32(wav_file, data_bytes + 36);
99     fwrite("WAVE", 1, 4, wav_file);
100 
101     int byte_rate = sample_rate * num_channels * write_with_bytes_per_sample;
102     int bits_per_sample = 8 * write_with_bytes_per_sample;
103     int block_align = num_channels * bits_per_sample;
104     int fmt_length = 16;
105     int fmt_format_tag = 1; // PCM
106 
107     /* write fmt chunk */
108     fwrite("fmt ", 1, 4, wav_file);
109     little_endian_fstore_32(wav_file, fmt_length);
110     little_endian_fstore_16(wav_file, fmt_format_tag);
111     little_endian_fstore_16(wav_file, num_channels);
112     little_endian_fstore_32(wav_file, sample_rate);
113     little_endian_fstore_32(wav_file, byte_rate);
114     little_endian_fstore_16(wav_file, block_align);
115     little_endian_fstore_16(wav_file, bits_per_sample);
116 
117     /* write data chunk */
118     fwrite("data", 1, 4, wav_file);
119     little_endian_fstore_32(wav_file, data_bytes);
120 }
121 
122 int wav_writer_open(const char * filepath, int num_channels, int sampling_frequency){
123     FILE * wav_file = fopen(filepath, "wb");
124     if (!wav_file) return 1;
125 
126     wav_writer_state.wav_file = wav_file;
127     wav_writer_state.frame_count = 0;
128     wav_writer_state.total_num_samples = 0;
129     wav_writer_state.num_channels = num_channels;
130     wav_writer_state.sampling_frequency = sampling_frequency;
131     write_wav_header(wav_writer_state.wav_file, 0, num_channels, sampling_frequency);
132     return 0;
133 }
134 
135 int wav_writer_close(void){
136     if (wav_writer_state.wav_file == NULL) return 0;
137     rewind(wav_writer_state.wav_file);
138     write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples,
139         wav_writer_state.num_channels, wav_writer_state.sampling_frequency);
140     fclose(wav_writer_state.wav_file);
141     wav_writer_state.wav_file = NULL;
142     return 0;
143 }
144 
145 int wav_writer_write_int8(int num_samples, int8_t * data){
146     if (data == NULL) return 1;
147     int i = 0;
148     int8_t zero_byte = 0;
149     for (i=0; i<num_samples; i++){
150         fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file);
151         uint8_t byte_value = (uint8_t)data[i];
152         fwrite(&byte_value, 1, 1, wav_writer_state.wav_file);
153     }
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_le_int16(int num_samples, int16_t * data){
161     if (data == NULL) return 1;
162     fwrite(data, num_samples, 2, wav_writer_state.wav_file);
163 
164     wav_writer_state.total_num_samples+=num_samples;
165     wav_writer_state.frame_count++;
166     return 0;
167 }
168 
169 int wav_writer_write_int16(int num_samples, int16_t * data){
170     if (btstack_is_little_endian()){
171         return wav_writer_write_le_int16(num_samples, data);
172     }
173     if (data == NULL) return 1;
174 
175     int i;
176     for (i=0;i<num_samples;i++){
177         uint16_t sample = btstack_flip_16(data[i]);
178         fwrite(&sample, 1, 2, wav_writer_state.wav_file);
179     }
180 
181     wav_writer_state.total_num_samples+=num_samples;
182     wav_writer_state.frame_count++;
183     return 0;
184 }
185 
186 int wav_reader_open(const char * filepath){
187     wav_reader_fd = open(filepath, O_RDONLY);
188     if (!wav_reader_fd) {
189         log_error("Can't open file %s", filepath);
190         return 1;
191     }
192 
193     uint8_t buf[40];
194     __read(wav_reader_fd, buf, sizeof(buf));
195 
196     int num_channels = little_endian_read_16(buf, 22);
197     int block_align = little_endian_read_16(buf, 32);
198     if (num_channels != 1 && num_channels != 2) {
199         log_error("Unexpected num channels %d", num_channels);
200         return 1;
201     }
202     bytes_per_sample = block_align/num_channels;
203     if (bytes_per_sample > 2){
204         bytes_per_sample = bytes_per_sample/8;
205     }
206     return 0;
207 }
208 
209 int wav_reader_close(void){
210     close(wav_reader_fd);
211     return 0;
212 }
213 
214 // Wav data: 8bit is uint8_t; 16bit is int16
215 int wav_reader_read_int8(int num_samples, int8_t * data){
216     if (!wav_reader_fd) return 1;
217     int i;
218     int bytes_read = 0;
219 
220     for (i=0; i<num_samples; i++){
221         if (bytes_per_sample == 2){
222             uint8_t buf[2];
223             bytes_read +=__read(wav_reader_fd, &buf, 2);
224             data[i] = buf[1];
225         } else {
226             uint8_t buf[1];
227             bytes_read +=__read(wav_reader_fd, &buf, 1);
228             data[i] = buf[0] + 128;
229         }
230     }
231     if (bytes_read == num_samples*bytes_per_sample) {
232         return 0;
233     } else {
234         return 1;
235     }
236 }
237 
238 int wav_reader_read_int16(int num_samples, int16_t * data){
239     if (!wav_reader_fd) return 1;
240     int i;
241     int bytes_read = 0;
242     for (i=0; i<num_samples; i++){
243         uint8_t buf[2];
244         bytes_read +=__read(wav_reader_fd, &buf, 2);
245         data[i] = little_endian_read_16(buf, 0);
246     }
247     if (bytes_read == num_samples*bytes_per_sample) {
248         return 0;
249     } else {
250         return 1;
251     }
252 }
253 
254 
255