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