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 #include <stdint.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <fcntl.h> 43 #include <unistd.h> 44 45 #include "wav_util.h" 46 #include "btstack_util.h" 47 #include "btstack_debug.h" 48 49 static int wav_reader_fd; 50 static int bytes_per_sample = 2; 51 52 /* Write wav file utils */ 53 typedef struct wav_writer_state { 54 FILE * wav_file; 55 int total_num_samples; 56 int num_channels; 57 int sampling_frequency; 58 int frame_count; 59 } wav_writer_state_t; 60 61 wav_writer_state_t wav_writer_state; 62 63 64 static void little_endian_fstore_16(FILE *wav_file, uint16_t value){ 65 uint8_t buf[2]; 66 little_endian_store_16(buf, 0, value); 67 fwrite(&buf, 1, 2, wav_file); 68 } 69 70 static void little_endian_fstore_32(FILE *wav_file, uint32_t value){ 71 uint8_t buf[4]; 72 little_endian_store_32(buf, 0, value); 73 fwrite(&buf, 1, 4, wav_file); 74 } 75 76 static ssize_t __read(int fd, void *buf, size_t count){ 77 ssize_t len, pos = 0; 78 79 while (count > 0) { 80 len = read(fd, (int8_t * )buf + pos, count); 81 if (len <= 0) 82 return pos; 83 84 count -= len; 85 pos += len; 86 } 87 return pos; 88 } 89 90 static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){ 91 unsigned int write_with_bytes_per_sample = 2; 92 /* write RIFF header */ 93 fwrite("RIFF", 1, 4, wav_file); 94 // num_samples = blocks * subbands 95 uint32_t data_bytes = (uint32_t) (write_with_bytes_per_sample * total_num_samples * num_channels); 96 little_endian_fstore_32(wav_file, data_bytes + 36); 97 fwrite("WAVE", 1, 4, wav_file); 98 99 int byte_rate = sample_rate * num_channels * write_with_bytes_per_sample; 100 int bits_per_sample = 8 * write_with_bytes_per_sample; 101 int block_align = num_channels * bits_per_sample; 102 int fmt_length = 16; 103 int fmt_format_tag = 1; // PCM 104 105 /* write fmt chunk */ 106 fwrite("fmt ", 1, 4, wav_file); 107 little_endian_fstore_32(wav_file, fmt_length); 108 little_endian_fstore_16(wav_file, fmt_format_tag); 109 little_endian_fstore_16(wav_file, num_channels); 110 little_endian_fstore_32(wav_file, sample_rate); 111 little_endian_fstore_32(wav_file, byte_rate); 112 little_endian_fstore_16(wav_file, block_align); 113 little_endian_fstore_16(wav_file, bits_per_sample); 114 115 /* write data chunk */ 116 fwrite("data", 1, 4, wav_file); 117 little_endian_fstore_32(wav_file, data_bytes); 118 } 119 120 int wav_writer_open(const char * filepath, int num_channels, int sampling_frequency){ 121 FILE * wav_file = fopen(filepath, "wb"); 122 if (!wav_file) return 1; 123 124 wav_writer_state.wav_file = wav_file; 125 wav_writer_state.frame_count = 0; 126 wav_writer_state.total_num_samples = 0; 127 wav_writer_state.num_channels = num_channels; 128 wav_writer_state.sampling_frequency = sampling_frequency; 129 write_wav_header(wav_writer_state.wav_file, 0, num_channels, sampling_frequency); 130 return 0; 131 } 132 133 int wav_writer_close(void){ 134 rewind(wav_writer_state.wav_file); 135 write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, 136 wav_writer_state.num_channels, wav_writer_state.sampling_frequency); 137 fclose(wav_writer_state.wav_file); 138 return 0; 139 } 140 141 int wav_writer_write_int8(int num_samples, int8_t * data){ 142 if (data == NULL) return 1; 143 int i = 0; 144 int8_t zero_byte = 0; 145 for (i=0; i<num_samples; i++){ 146 fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file); 147 uint8_t byte_value = (uint8_t)data[i]; 148 fwrite(&byte_value, 1, 1, wav_writer_state.wav_file); 149 } 150 151 wav_writer_state.total_num_samples+=num_samples; 152 wav_writer_state.frame_count++; 153 return 0; 154 } 155 156 int wav_writer_write_le_int16(int num_samples, int16_t * data){ 157 if (data == NULL) return 1; 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 if (btstack_is_little_endian()){ 167 return wav_writer_write_le_int16(num_samples, data); 168 } 169 if (data == NULL) return 1; 170 171 int i; 172 for (i=0;i<num_samples;i++){ 173 uint16_t sample = btstack_flip_16(data[i]); 174 fwrite(&sample, 1, 2, wav_writer_state.wav_file); 175 } 176 177 wav_writer_state.total_num_samples+=num_samples; 178 wav_writer_state.frame_count++; 179 return 0; 180 } 181 182 int wav_reader_open(const char * filepath){ 183 wav_reader_fd = open(filepath, O_RDONLY); 184 if (!wav_reader_fd) { 185 log_error("Can't open file %s", filepath); 186 return 1; 187 } 188 189 uint8_t buf[40]; 190 __read(wav_reader_fd, buf, sizeof(buf)); 191 192 int num_channels = little_endian_read_16(buf, 22); 193 int block_align = little_endian_read_16(buf, 32); 194 if (num_channels != 1 && num_channels != 2) { 195 log_error("Unexpected num channels %d", num_channels); 196 return 1; 197 } 198 bytes_per_sample = block_align/num_channels; 199 if (bytes_per_sample > 2){ 200 bytes_per_sample = bytes_per_sample/8; 201 } 202 return 0; 203 } 204 205 int wav_reader_close(void){ 206 close(wav_reader_fd); 207 return 0; 208 } 209 210 // Wav data: 8bit is uint8_t; 16bit is int16 211 int wav_reader_read_int8(int num_samples, int8_t * data){ 212 if (!wav_reader_fd) return 1; 213 int i; 214 int bytes_read = 0; 215 216 for (i=0; i<num_samples; i++){ 217 if (bytes_per_sample == 2){ 218 uint8_t buf[2]; 219 bytes_read +=__read(wav_reader_fd, &buf, 2); 220 data[i] = buf[1]; 221 } else { 222 uint8_t buf[1]; 223 bytes_read +=__read(wav_reader_fd, &buf, 1); 224 data[i] = buf[0] + 128; 225 } 226 } 227 return bytes_read == num_samples*bytes_per_sample; 228 } 229 230 int wav_reader_read_int16(int num_samples, int16_t * data){ 231 if (!wav_reader_fd) return 1; 232 int i; 233 int bytes_read = 0; 234 for (i=0; i<num_samples; i++){ 235 uint8_t buf[2]; 236 bytes_read +=__read(wav_reader_fd, &buf, 2); 237 data[i] = little_endian_read_16(buf, 0); 238 } 239 return bytes_read == num_samples*bytes_per_sample; 240 } 241 242 243