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 btstack_assert(data != NULL); 150 if (wav_writer_state.wav_file == NULL) return 1; 151 152 int i = 0; 153 int8_t zero_byte = 0; 154 for (i=0; i<num_samples; i++){ 155 fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file); 156 uint8_t byte_value = (uint8_t)data[i]; 157 fwrite(&byte_value, 1, 1, wav_writer_state.wav_file); 158 } 159 160 wav_writer_state.total_num_samples+=num_samples; 161 wav_writer_state.frame_count++; 162 return 0; 163 } 164 165 int wav_writer_write_le_int16(int num_samples, int16_t * data){ 166 btstack_assert(data != NULL); 167 if (wav_writer_state.wav_file == NULL) return 1; 168 169 fwrite(data, num_samples, 2, wav_writer_state.wav_file); 170 171 wav_writer_state.total_num_samples+=num_samples; 172 wav_writer_state.frame_count++; 173 return 0; 174 } 175 176 int wav_writer_write_int16(int num_samples, int16_t * data){ 177 btstack_assert(data != NULL); 178 if (wav_writer_state.wav_file == NULL) return 1; 179 180 if (btstack_is_little_endian()){ 181 return wav_writer_write_le_int16(num_samples, data); 182 } 183 184 int i; 185 for (i=0;i<num_samples;i++){ 186 uint16_t sample = btstack_flip_16(data[i]); 187 fwrite(&sample, 1, 2, wav_writer_state.wav_file); 188 } 189 190 wav_writer_state.total_num_samples+=num_samples; 191 wav_writer_state.frame_count++; 192 return 0; 193 } 194 195 /** WAV Reader Implementation */ 196 197 int wav_reader_open(const char * filepath){ 198 memset (&wav_reader_state, 0, sizeof(wav_reader_state)); 199 200 wav_reader_fd = open(filepath, O_RDONLY); 201 if (!wav_reader_fd) { 202 log_error("Can't open file %s", filepath); 203 return 1; 204 } 205 206 uint8_t buf[40]; 207 __read(wav_reader_fd, buf, sizeof(buf)); 208 209 wav_reader_state.num_channels = little_endian_read_16(buf, 22); 210 if ((wav_reader_state.num_channels < 1) || (wav_reader_state.num_channels > 2)) { 211 log_error("Unexpected num channels %d", wav_reader_state.num_channels); 212 return 1; 213 } 214 215 wav_reader_state.sampling_rate = little_endian_read_32(buf, 24); 216 217 int block_align = little_endian_read_16(buf, 32); 218 wav_reader_bytes_per_sample = block_align / wav_reader_state.num_channels; 219 if (wav_reader_bytes_per_sample > 2){ 220 wav_reader_bytes_per_sample = wav_reader_bytes_per_sample / 8; 221 } 222 223 return 0; 224 } 225 226 uint8_t wav_reader_get_num_channels(void){ 227 return wav_reader_state.num_channels; 228 } 229 230 uint32_t wav_reader_get_sampling_rate(void){ 231 return wav_reader_state.sampling_rate; 232 } 233 234 int wav_reader_close(void){ 235 close(wav_reader_fd); 236 return 0; 237 } 238 239 // Wav data: 8bit is uint8_t; 16bit is int16 240 int wav_reader_read_int8(int num_samples, int8_t * data){ 241 if (!wav_reader_fd) return 1; 242 int i; 243 int bytes_read = 0; 244 245 for (i=0; i<num_samples; i++){ 246 if (wav_reader_bytes_per_sample == 2){ 247 uint8_t buf[2]; 248 bytes_read +=__read(wav_reader_fd, &buf, 2); 249 data[i] = buf[1]; 250 } else { 251 uint8_t buf[1]; 252 bytes_read +=__read(wav_reader_fd, &buf, 1); 253 data[i] = buf[0] + 128; 254 } 255 } 256 if (bytes_read == num_samples * wav_reader_bytes_per_sample) { 257 return 0; 258 } else { 259 return 1; 260 } 261 } 262 263 int wav_reader_read_int16(int num_samples, int16_t * data){ 264 if (!wav_reader_fd) return 1; 265 int i; 266 int bytes_read = 0; 267 for (i=0; i<num_samples; i++){ 268 uint8_t buf[2]; 269 bytes_read +=__read(wav_reader_fd, &buf, 2); 270 data[i] = little_endian_read_16(buf, 0); 271 } 272 if (bytes_read == num_samples * wav_reader_bytes_per_sample) { 273 return 0; 274 } else { 275 return 1; 276 } 277 } 278 279 280