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[24]; 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("\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 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 fwrite(data, num_samples, 2, wav_file); 127 } 128 129 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 130 wav_writer_state_t * wav_writer_state = (wav_writer_state_t*) context; 131 write_wav_data(wav_writer_state->wav_file, num_samples, num_channels, data); 132 wav_writer_state->total_num_samples+=num_samples; 133 wav_writer_state->frame_count++; 134 } 135 136 int main (int argc, const char * argv[]){ 137 if (argc < 4){ 138 show_usage(); 139 return -1; 140 } 141 142 sbc_mode_t mode = SBC_MODE_STANDARD; 143 144 const char * sbc_filename = argv[1]; 145 const char * wav_filename = argv[2]; 146 147 if (strncmp(argv[3], "msbc", 4) == 0 ){ 148 mode = SBC_MODE_mSBC; 149 printf("Using SBC_MODE_mSBC mode\n"); 150 } else { 151 printf("Using SBC_MODE_STANDARD mode\n"); 152 } 153 154 int fd = open(sbc_filename, O_RDONLY); 155 if (fd < 0) { 156 printf("Can't open file %s", sbc_filename); 157 return -1; 158 } 159 printf("Open sbc file: %s\n", sbc_filename); 160 FILE * wav_file = fopen(wav_filename, "wb"); 161 wav_writer_state_t wav_writer_state; 162 wav_writer_state.wav_file = wav_file; 163 wav_writer_state.frame_count = 0; 164 wav_writer_state.total_num_samples = 0; 165 166 sbc_decoder_state_t state; 167 sbc_decoder_init(&state, mode, &handle_pcm_data, (void*)&wav_writer_state); 168 write_wav_header(wav_writer_state.wav_file, 0, 0, 0); 169 170 while (1){ 171 // get next chunk 172 int bytes_read = __read(fd, read_buffer, sizeof(read_buffer)); 173 if (0 >= bytes_read) break; 174 // process chunk 175 sbc_decoder_process_data(&state, read_buffer, bytes_read); 176 } 177 178 rewind(wav_file); 179 write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, sbc_decoder_num_channels(&state), sbc_decoder_sample_rate(&state)); 180 181 fclose(wav_file); 182 close(fd); 183 184 printf("Write %d frames to wav file: %s\n", wav_writer_state.frame_count, wav_filename); 185 186 } 187