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