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 53 #include "oi_assert.h" 54 55 #include "btstack.h" 56 #include "sbc_decoder.h" 57 58 static uint8_t read_buffer[24]; 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("\n\nUsage: ./sbc_decoder_test input.sbc output.wav msbc|sbc\n\n"); 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 uint8_t buf[2]; 86 little_endian_store_32(buf, 0, value); 87 fwrite(&buf, 1, 2, wav_file); 88 } 89 90 void little_endian_fstore_32(FILE *wav_file, uint32_t value){ 91 uint8_t buf[4]; 92 little_endian_store_32(buf, 0, value); 93 fwrite(&buf, 1, 4, wav_file); 94 } 95 96 97 static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){ 98 unsigned int bytes_per_sample = 2; 99 /* write RIFF header */ 100 fwrite("RIFF", 1, 4, wav_file); 101 // num_samples = blocks * subbands 102 uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels); 103 little_endian_fstore_32(wav_file, data_bytes + 36); 104 fwrite("WAVE", 1, 4, wav_file); 105 106 int byte_rate = sample_rate * num_channels * bytes_per_sample; 107 int bits_per_sample = 8 * bytes_per_sample; 108 int block_align = num_channels * bits_per_sample; 109 int fmt_length = 16; 110 int fmt_format_tag = 1; // PCM 111 112 /* write fmt chunk */ 113 fwrite("fmt ", 1, 4, wav_file); 114 little_endian_fstore_32(wav_file, fmt_length); 115 little_endian_fstore_16(wav_file, fmt_format_tag); 116 little_endian_fstore_16(wav_file, num_channels); 117 little_endian_fstore_32(wav_file, sample_rate); 118 little_endian_fstore_32(wav_file, byte_rate); 119 little_endian_fstore_16(wav_file, block_align); 120 little_endian_fstore_16(wav_file, bits_per_sample); 121 122 /* write data chunk */ 123 fwrite("data", 1, 4, wav_file); 124 little_endian_fstore_32(wav_file, data_bytes); 125 } 126 127 static void write_wav_data(FILE * wav_file, int num_samples, int num_channels, int16_t * data){ 128 fwrite(data, num_samples, 2, wav_file); 129 } 130 131 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 132 wav_writer_state_t * wav_writer_state = (wav_writer_state_t*) context; 133 write_wav_data(wav_writer_state->wav_file, num_samples, num_channels, data); 134 wav_writer_state->total_num_samples+=num_samples; 135 wav_writer_state->frame_count++; 136 } 137 138 int main (int argc, const char * argv[]){ 139 if (argc < 4){ 140 show_usage(); 141 return -1; 142 } 143 144 sbc_mode_t mode = SBC_MODE_STANDARD; 145 146 const char * sbc_filename = argv[1]; 147 const char * wav_filename = argv[2]; 148 149 if (strncmp(argv[3], "msbc", 4) == 0 ){ 150 mode = SBC_MODE_mSBC; 151 printf("Using SBC_MODE_mSBC mode\n"); 152 } else { 153 printf("Using SBC_MODE_STANDARD mode\n"); 154 } 155 156 int fd = open(sbc_filename, O_RDONLY); 157 if (fd < 0) { 158 printf("Can't open file %s", sbc_filename); 159 return -1; 160 } 161 printf("Open sbc file: %s\n", sbc_filename); 162 FILE * wav_file = fopen(wav_filename, "wb"); 163 wav_writer_state_t wav_writer_state; 164 wav_writer_state.wav_file = wav_file; 165 wav_writer_state.frame_count = 0; 166 wav_writer_state.total_num_samples = 0; 167 168 sbc_decoder_state_t state; 169 sbc_decoder_init(&state, mode, &handle_pcm_data, (void*)&wav_writer_state); 170 write_wav_header(wav_writer_state.wav_file, 0, 0, 0); 171 172 while (1){ 173 // get next chunk 174 int bytes_read = __read(fd, read_buffer, sizeof(read_buffer)); 175 if (0 >= bytes_read) break; 176 // process chunk 177 sbc_decoder_process_data(&state, read_buffer, bytes_read); 178 } 179 180 rewind(wav_file); 181 write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, sbc_decoder_num_channels(&state), sbc_decoder_sample_rate(&state)); 182 183 fclose(wav_file); 184 close(fd); 185 186 printf("Write %d frames to wav file: %s\n", wav_writer_state.frame_count, wav_filename); 187 188 } 189