1 /* 2 * Copyright (C) 2014 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 // ***************************************************************************** 39 // 40 // SBC decoder tests 41 // 42 // ***************************************************************************** 43 44 #include "btstack_config.h" 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <fcntl.h> 51 #include <unistd.h> 52 #include "sbc_decoder.h" 53 #include "oi_assert.h" 54 55 #include "btstack.h" 56 57 static uint8_t read_buffer[6000]; 58 static uint8_t buf[4]; 59 60 typedef struct wav_writer_state { 61 FILE * wav_file; 62 int total_num_samples; 63 int frame_count; 64 } wav_writer_state_t; 65 66 static void show_usage(void){ 67 printf("Usage: ./sbc_decoder_test input.sbc"); 68 } 69 70 static ssize_t __read(int fd, void *buf, size_t count){ 71 ssize_t len, pos = 0; 72 73 while (count > 0) { 74 len = read(fd, buf + pos, count); 75 if (len <= 0) 76 return pos; 77 78 count -= len; 79 pos += len; 80 } 81 return pos; 82 } 83 84 void little_endian_fstore_16(FILE *wav_file, uint16_t value){ 85 little_endian_store_32(buf, 0, value); 86 fwrite(&buf, 1, 2, wav_file); 87 } 88 89 void little_endian_fstore_32(FILE *wav_file, uint32_t value){ 90 little_endian_store_32(buf, 0, value); 91 fwrite(&buf, 1, 4, wav_file); 92 } 93 94 95 static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){ 96 unsigned int 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) (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 * bytes_per_sample; 105 int bits_per_sample = 8 * 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 static void write_wav_data(FILE * wav_file, int num_samples, int num_channels, int16_t * data){ 126 int i; 127 for (i=0; i < num_samples; i++){ 128 little_endian_fstore_16(wav_file, (uint16_t)data[i]); 129 if (num_channels == 2){ 130 little_endian_fstore_16(wav_file, (uint16_t)data); 131 } 132 } 133 } 134 135 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 136 wav_writer_state_t * wav_writer_state = (wav_writer_state_t*) context; 137 write_wav_data(wav_writer_state->wav_file, num_samples, num_channels, data); 138 wav_writer_state->total_num_samples+=num_samples; 139 wav_writer_state->frame_count++; 140 } 141 142 int main (int argc, const char * argv[]){ 143 if (argc < 2){ 144 show_usage(); 145 return -1; 146 } 147 148 const char * sbc_filename = argv[1]; 149 const char * wav_filename = argv[2]; 150 151 152 int fd = open(sbc_filename, O_RDONLY); 153 if (fd < 0) { 154 printf("Can't open file %s", sbc_filename); 155 return -1; 156 } 157 printf("Open sbc file: %s\n", sbc_filename); 158 FILE * wav_file = fopen(wav_filename, "wb"); 159 wav_writer_state_t wav_writer_state; 160 wav_writer_state.wav_file = wav_file; 161 wav_writer_state.frame_count = 0; 162 wav_writer_state.total_num_samples = 0; 163 164 sbc_decoder_state_t state; 165 sbc_decoder_init(&state, &handle_pcm_data, (void*)&wav_writer_state); 166 write_wav_header(wav_writer_state.wav_file, 0, 0, 0); 167 168 while (1){ 169 // get next chunk 170 int bytes_read = __read(fd, read_buffer, sizeof(read_buffer)); 171 if (0 >= bytes_read) break; 172 // process chunk 173 sbc_decoder_process_data(&state, read_buffer, bytes_read); 174 } 175 176 rewind(wav_file); 177 write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, sbc_decoder_num_channels(&state), sbc_decoder_sample_rate(&state)); 178 179 fclose(wav_file); 180 close(fd); 181 182 printf("Write %d frames to wav file: %s\n", wav_writer_state.frame_count, wav_filename); 183 184 } 185