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