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 "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 fd = open(pklg_path, O_RDONLY); 89 if (fd < 0) { 90 printf("Can't open file %s", pklg_path); 91 return; 92 } 93 94 wav_writer_open(wav_path, 1, 8000); 95 96 btstack_cvsd_plc_init(&plc_state); 97 98 int sco_packet_counter = 0; 99 while (1){ 100 int bytes_read; 101 // get next packet 102 uint8_t header[13]; 103 bytes_read = __read(fd, header, sizeof(header)); 104 if (0 >= bytes_read) break; 105 106 uint8_t packet[256]; 107 uint32_t size = big_endian_read_32(header, 0) - 9; 108 bytes_read = __read(fd, packet, size); 109 110 uint8_t type = header[12]; 111 112 if (type != packet_type) continue; 113 114 sco_packet_counter++; 115 116 int16_t audio_frame_out[128]; // 117 118 if (size > sizeof(audio_frame_out)){ 119 printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n"); 120 break; 121 } 122 123 const int audio_bytes_read = size - 3; 124 const int num_samples = audio_bytes_read / BYTES_PER_FRAME; 125 126 // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO 127 uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff; 128 if (sco_handle == 0) continue; 129 130 // convert into host endian 131 int16_t audio_frame_in[128]; 132 int i; 133 for (i=0;i<num_samples;i++){ 134 audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2); 135 } 136 137 if (plc_enabled){ 138 btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, num_samples, audio_frame_out); 139 } else { 140 memcpy(audio_frame_out, audio_frame_in, audio_bytes_read); 141 } 142 143 wav_writer_write_int16(num_samples, audio_frame_out); 144 } 145 146 wav_writer_close(); 147 close(fd); 148 149 btstack_cvsd_dump_statistics(&plc_state); 150 } 151 152 int main (int argc, const char * argv[]){ 153 154 char wav_path[1000]; 155 char pklg_path[1000]; 156 157 if (argc < 2){ 158 show_usage(); 159 return -1; 160 } 161 162 int argv_pos = 1; 163 const char * filename = argv[argv_pos++]; 164 165 #ifdef OCTAVE_OUTPUT 166 printf("OCTAVE OUTPUT active\n"); 167 btstack_cvsd_plc_octave_set_base_name(filename); 168 #endif 169 170 strcpy(pklg_path, filename); 171 strcat(pklg_path, ".pklg"); 172 173 // in file, no plc 174 strcpy(wav_path, filename); 175 strcat(wav_path, "_in_raw.wav"); 176 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0); 177 178 // in file, plc 179 strcpy(wav_path, filename); 180 strcat(wav_path, "_in_plc.wav"); 181 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1); 182 183 // out file, no plc 184 strcpy(wav_path, filename); 185 strcat(wav_path, "_out.wav"); 186 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0); 187 } 188