xref: /btstack/platform/posix/wav_util.c (revision 046b44372d25c7bd6fe78e5cf6e5176a1979f437)
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 MATTHIAS
24  * RINGWALD 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) 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     return 0;
142 }
143 
144 int wav_writer_write_int8(int num_samples, int8_t * data){
145     if (data == NULL) return 1;
146     int i = 0;
147     int8_t zero_byte = 0;
148     for (i=0; i<num_samples; i++){
149         fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file);
150         uint8_t byte_value = (uint8_t)data[i];
151         fwrite(&byte_value, 1, 1, wav_writer_state.wav_file);
152     }
153 
154     wav_writer_state.total_num_samples+=num_samples;
155     wav_writer_state.frame_count++;
156     return 0;
157 }
158 
159 int wav_writer_write_le_int16(int num_samples, int16_t * data){
160     if (data == NULL) return 1;
161     fwrite(data, num_samples, 2, wav_writer_state.wav_file);
162 
163     wav_writer_state.total_num_samples+=num_samples;
164     wav_writer_state.frame_count++;
165     return 0;
166 }
167 
168 int wav_writer_write_int16(int num_samples, int16_t * data){
169     if (btstack_is_little_endian()){
170         return wav_writer_write_le_int16(num_samples, data);
171     }
172     if (data == NULL) return 1;
173 
174     int i;
175     for (i=0;i<num_samples;i++){
176         uint16_t sample = btstack_flip_16(data[i]);
177         fwrite(&sample, 1, 2, wav_writer_state.wav_file);
178     }
179 
180     wav_writer_state.total_num_samples+=num_samples;
181     wav_writer_state.frame_count++;
182     return 0;
183 }
184 
185 int wav_reader_open(const char * filepath){
186     wav_reader_fd = open(filepath, O_RDONLY);
187     if (!wav_reader_fd) {
188         log_error("Can't open file %s", filepath);
189         return 1;
190     }
191 
192     uint8_t buf[40];
193     __read(wav_reader_fd, buf, sizeof(buf));
194 
195     int num_channels = little_endian_read_16(buf, 22);
196     int block_align = little_endian_read_16(buf, 32);
197     if (num_channels != 1 && num_channels != 2) {
198         log_error("Unexpected num channels %d", num_channels);
199         return 1;
200     }
201     bytes_per_sample = block_align/num_channels;
202     if (bytes_per_sample > 2){
203         bytes_per_sample = bytes_per_sample/8;
204     }
205     return 0;
206 }
207 
208 int wav_reader_close(void){
209     close(wav_reader_fd);
210     return 0;
211 }
212 
213 // Wav data: 8bit is uint8_t; 16bit is int16
214 int wav_reader_read_int8(int num_samples, int8_t * data){
215     if (!wav_reader_fd) return 1;
216     int i;
217     int bytes_read = 0;
218 
219     for (i=0; i<num_samples; i++){
220         if (bytes_per_sample == 2){
221             uint8_t buf[2];
222             bytes_read +=__read(wav_reader_fd, &buf, 2);
223             data[i] = buf[1];
224         } else {
225             uint8_t buf[1];
226             bytes_read +=__read(wav_reader_fd, &buf, 1);
227             data[i] = buf[0] + 128;
228         }
229     }
230     if (bytes_read == num_samples*bytes_per_sample) {
231         return 0;
232     } else {
233         return 1;
234     }
235 }
236 
237 int wav_reader_read_int16(int num_samples, int16_t * data){
238     if (!wav_reader_fd) return 1;
239     int i;
240     int bytes_read = 0;
241     for (i=0; i<num_samples; i++){
242         uint8_t buf[2];
243         bytes_read +=__read(wav_reader_fd, &buf, 2);
244         data[i] = little_endian_read_16(buf, 0);
245     }
246     if (bytes_read == num_samples*bytes_per_sample) {
247         return 0;
248     } else {
249         return 1;
250     }
251 }
252 
253 
254