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 const uint8_t sine[] = { 49 0, 15, 31, 46, 61, 74, 86, 97, 107, 114, 50 120, 124, 126, 126, 124, 120, 114, 107, 97, 86, 51 74, 61, 46, 31, 15, 0, 241, 225, 210, 195, 52 182, 170, 159, 149, 142, 136, 132, 130, 130, 132, 53 136, 142, 149, 159, 170, 182, 195, 210, 225, 241, 54 }; 55 static int phase = 0; 56 57 static int wav_reader_fd; 58 static int bytes_per_sample = 2; 59 60 /* Write wav file utils */ 61 typedef struct wav_writer_state { 62 FILE * wav_file; 63 int total_num_samples; 64 int num_channels; 65 int sampling_frequency; 66 int frame_count; 67 } wav_writer_state_t; 68 69 wav_writer_state_t 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 ssize_t __read(int fd, void *buf, size_t count){ 85 ssize_t len, pos = 0; 86 87 while (count > 0) { 88 len = read(fd, (int8_t * )buf + pos, count); 89 if (len <= 0) 90 return pos; 91 92 count -= len; 93 pos += len; 94 } 95 return pos; 96 } 97 98 static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){ 99 unsigned int bytes_per_sample = 2; 100 /* write RIFF header */ 101 fwrite("RIFF", 1, 4, wav_file); 102 // num_samples = blocks * subbands 103 uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels); 104 little_endian_fstore_32(wav_file, data_bytes + 36); 105 fwrite("WAVE", 1, 4, wav_file); 106 107 int byte_rate = sample_rate * num_channels * bytes_per_sample; 108 int bits_per_sample = 8 * bytes_per_sample; 109 int block_align = num_channels * bits_per_sample; 110 int fmt_length = 16; 111 int fmt_format_tag = 1; // PCM 112 113 /* write fmt chunk */ 114 fwrite("fmt ", 1, 4, wav_file); 115 little_endian_fstore_32(wav_file, fmt_length); 116 little_endian_fstore_16(wav_file, fmt_format_tag); 117 little_endian_fstore_16(wav_file, num_channels); 118 little_endian_fstore_32(wav_file, sample_rate); 119 little_endian_fstore_32(wav_file, byte_rate); 120 little_endian_fstore_16(wav_file, block_align); 121 little_endian_fstore_16(wav_file, bits_per_sample); 122 123 /* write data chunk */ 124 fwrite("data", 1, 4, wav_file); 125 little_endian_fstore_32(wav_file, data_bytes); 126 } 127 128 int wav_writer_open(const char * filepath, int num_channels, int sampling_frequency){ 129 FILE * wav_file = fopen(filepath, "wb"); 130 if (!wav_file) return 1; 131 132 wav_writer_state.wav_file = wav_file; 133 wav_writer_state.frame_count = 0; 134 wav_writer_state.total_num_samples = 0; 135 wav_writer_state.num_channels = num_channels; 136 wav_writer_state.sampling_frequency = sampling_frequency; 137 write_wav_header(wav_writer_state.wav_file, 0, num_channels, sampling_frequency); 138 return 0; 139 } 140 141 int wav_writer_close(void){ 142 rewind(wav_writer_state.wav_file); 143 write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, 144 wav_writer_state.num_channels, wav_writer_state.sampling_frequency); 145 fclose(wav_writer_state.wav_file); 146 return 0; 147 } 148 149 int wav_writer_write_int8(int num_samples, int8_t * data){ 150 if (data == NULL) return 1; 151 int i = 0; 152 int8_t zero_byte = 0; 153 for (i=0; i<num_samples; i++){ 154 fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file); 155 uint8_t byte_value = (uint8_t)data[i]; 156 fwrite(&byte_value, 1, 1, wav_writer_state.wav_file); 157 } 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 void wav_synthesize_sine_wave_int8(int num_samples, int8_t * data){ 209 int i; 210 for (i=0; i<num_samples; i++){ 211 data[i] = (int8_t)sine[phase]; 212 phase++; 213 if (phase >= sizeof(sine)) phase = 0; 214 } 215 } 216 217