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