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 "btstack_sbc.h" 57 58 #include "wav_util.h" 59 60 static uint8_t read_buffer[24]; 61 static int total_num_samples = 0; 62 static int frame_count = 0; 63 64 static void show_usage(void){ 65 printf("\n\nUsage: ./sbc_decoder_test input_file msbc|sbc plc_enabled(0|1) corrupt_frame_period\n\n"); 66 } 67 68 static ssize_t __read(int fd, void *buf, size_t count){ 69 ssize_t len, pos = 0; 70 71 while (count > 0) { 72 len = read(fd, (int8_t * )buf + pos, count); 73 if (len <= 0) 74 return pos; 75 76 count -= len; 77 pos += len; 78 } 79 return pos; 80 } 81 82 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 83 wav_writer_write_int16(num_samples, data); 84 total_num_samples+=num_samples; 85 frame_count++; 86 } 87 88 int main (int argc, const char * argv[]){ 89 if (argc < 4){ 90 show_usage(); 91 return -1; 92 } 93 94 btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 95 96 const char * filename = argv[1]; 97 98 char sbc_filename[1000]; 99 char wav_filename[1000]; 100 101 strcpy(sbc_filename, filename); 102 strcpy(wav_filename, filename); 103 104 const int plc_enabled = atoi(argv[3]); 105 const int corrupt_frame_period = atoi(argv[4]); 106 107 int sample_rate = 8000; 108 109 printf("\n"); 110 if (strncmp(argv[2], "msbc", 4) == 0 ){ 111 mode = SBC_MODE_mSBC; 112 printf("Using SBC_MODE_mSBC mode.\n"); 113 sample_rate = 16000; 114 } else { 115 printf("Using SBC_MODE_STANDARD mode.\n"); 116 } 117 118 if (plc_enabled){ 119 printf("PLC enabled.\n"); 120 } else { 121 printf("PLC disbled.\n"); 122 } 123 124 if (corrupt_frame_period > 0){ 125 printf("Corrupt frame period: every %d frames.\n", corrupt_frame_period); 126 } 127 if (mode == SBC_MODE_mSBC){ 128 strcat(sbc_filename, ".msbc"); 129 } else { 130 strcat(sbc_filename, ".sbc"); 131 } 132 133 if (plc_enabled){ 134 strcat(wav_filename, "_plc.wav"); 135 } else { 136 strcat(wav_filename, ".wav"); 137 } 138 139 140 int fd = open(sbc_filename, O_RDONLY); 141 if (fd < 0) { 142 printf("Can't open file %s", sbc_filename); 143 return -1; 144 } 145 printf("Open sbc file: %s\n", sbc_filename); 146 147 148 btstack_sbc_decoder_state_t state; 149 btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL); 150 wav_writer_open(wav_filename, 1, sample_rate); 151 152 if (!plc_enabled){ 153 btstack_sbc_decoder_test_disable_plc(); 154 } 155 if (corrupt_frame_period > 0){ 156 btstack_sbc_decoder_test_simulate_corrupt_frames(corrupt_frame_period); 157 } 158 159 while (1){ 160 // get next chunk 161 int bytes_read = __read(fd, read_buffer, sizeof(read_buffer)); 162 if (0 >= bytes_read) break; 163 // process chunk 164 btstack_sbc_decoder_process_data(&state, 0, read_buffer, bytes_read); 165 } 166 167 wav_writer_close(); 168 close(fd); 169 int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr; 170 171 printf("\nDecoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n - %d zero frames\n", total_frames_nr, state.good_frames_nr, state.bad_frames_nr, state.zero_frames_nr); 172 printf("Write %d frames to wav file: %s\n\n", frame_count, wav_filename); 173 174 } 175