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 "btstack.h" 54 #include "classic/btstack_cvsd_plc.h" 55 56 #include "wav_util.h" 57 58 #define BYTES_PER_FRAME 2 59 60 #define PAKET_TYPE_SCO_OUT 8 61 #define PAKET_TYPE_SCO_IN 9 62 63 static btstack_cvsd_plc_state_t plc_state; 64 65 static void show_usage(void){ 66 printf("\n\nUsage: ./pklg_cvsd_test input_file\n"); 67 printf("Example: ./pklg_cvsd_test pklg/test1\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, (int8_t * )buf + pos, count); 75 if (len <= 0) 76 return pos; 77 78 count -= len; 79 pos += len; 80 } 81 return pos; 82 } 83 84 static void process_file(const char * pklg_path, const char * wav_path, int packet_type, int plc_enabled){ 85 86 printf("Processing %s -> %s: PLC enabled: %u, direction %s\n", pklg_path, wav_path, plc_enabled, packet_type == PAKET_TYPE_SCO_OUT ? "Out" : "In"); 87 88 int oflags = O_RDONLY; 89 #ifdef _WIN32 90 oflags |= O_BINARY; 91 #endif 92 int fd = open(pklg_path, oflags); 93 if (fd < 0) { 94 printf("Can't open file %s", pklg_path); 95 return; 96 } 97 98 wav_writer_open(wav_path, 1, 8000); 99 100 btstack_cvsd_plc_init(&plc_state); 101 102 int sco_packet_counter = 0; 103 while (1){ 104 int bytes_read; 105 // get next packet 106 uint8_t header[13]; 107 bytes_read = __read(fd, header, sizeof(header)); 108 if (0 >= bytes_read) break; 109 110 uint32_t size = big_endian_read_32(header, 0); 111 112 // auto-detect endianess of size param 113 if (size >0xffff){ 114 size = little_endian_read_32(header, 0); 115 } 116 // subtract header 117 size -= 9; 118 119 uint8_t packet[256]; 120 uint8_t type = header[12]; 121 122 if (type != packet_type) { 123 // skip data 124 while (size){ 125 int bytes_to_read = btstack_min(sizeof(packet), size); 126 __read(fd, packet, bytes_to_read); 127 size -= bytes_to_read; 128 } 129 } else { 130 if (size > sizeof(packet) ){ 131 printf("Error: size %d, packet counter %d \n", size, sco_packet_counter); 132 exit(10); 133 } 134 bytes_read = __read(fd, packet, size); 135 136 sco_packet_counter++; 137 138 int16_t audio_frame_out[128]; // 139 140 if (size > sizeof(audio_frame_out)){ 141 printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n"); 142 break; 143 } 144 145 const int audio_bytes_read = size - 3; 146 const int num_samples = audio_bytes_read / BYTES_PER_FRAME; 147 148 // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO 149 uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff; 150 if (sco_handle == 0) continue; 151 152 // convert into host endian 153 int16_t audio_frame_in[128]; 154 int i; 155 for (i=0;i<num_samples;i++){ 156 audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2); 157 } 158 159 if (plc_enabled){ 160 if (num_samples > 60){ 161 btstack_cvsd_plc_process_data(&plc_state, false, audio_frame_in, 60, audio_frame_out); 162 wav_writer_write_int16(60, audio_frame_out); 163 btstack_cvsd_plc_process_data(&plc_state, false, &audio_frame_in[60], num_samples - 60, audio_frame_out); 164 wav_writer_write_int16(num_samples - 60, audio_frame_out); 165 } else { 166 btstack_cvsd_plc_process_data(&plc_state, false, audio_frame_in, num_samples, audio_frame_out); 167 wav_writer_write_int16(num_samples, audio_frame_out); 168 } 169 } else { 170 wav_writer_write_int16(num_samples, audio_frame_in); 171 } 172 } 173 } 174 175 wav_writer_close(); 176 close(fd); 177 178 btstack_cvsd_dump_statistics(&plc_state); 179 } 180 181 int main (int argc, const char * argv[]){ 182 183 char wav_path[1000]; 184 char pklg_path[1000]; 185 186 if (argc < 2){ 187 show_usage(); 188 return -1; 189 } 190 191 int argv_pos = 1; 192 const char * filename = argv[argv_pos++]; 193 194 #ifdef OCTAVE_OUTPUT 195 printf("OCTAVE OUTPUT active\n"); 196 btstack_cvsd_plc_octave_set_base_name(filename); 197 #endif 198 199 btstack_strcpy(pklg_path, sizeof(pklg_path), filename); 200 btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg"); 201 202 // in file, no plc 203 strcpy(wav_path, filename); 204 strcat(wav_path, "_in_raw.wav"); 205 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0); 206 207 // in file, plc 208 strcpy(wav_path, filename); 209 strcat(wav_path, "_in_plc.wav"); 210 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1); 211 212 // out file, no plc 213 strcpy(wav_path, filename); 214 strcat(wav_path, "_out.wav"); 215 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0); 216 } 217