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 static char wav_filename[1000]; 61 static char pklg_filename[1000]; 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 int main (int argc, const char * argv[]){ 85 if (argc < 2){ 86 show_usage(); 87 return -1; 88 } 89 90 int argv_pos = 1; 91 const char * filename = argv[argv_pos++]; 92 93 strcpy(pklg_filename, filename); 94 strcat(pklg_filename, ".pklg"); 95 96 strcpy(wav_filename, filename); 97 strcat(wav_filename, ".wav"); 98 99 100 int fd = open(pklg_filename, O_RDONLY); 101 if (fd < 0) { 102 printf("Can't open file %s", pklg_filename); 103 return -1; 104 } 105 printf("Open pklg file: %s\n", pklg_filename); 106 107 wav_writer_open(wav_filename, 1, 8000); 108 109 btstack_cvsd_plc_init(&plc_state); 110 111 int sco_packet_counter = 0; 112 while (1){ 113 int bytes_read; 114 // get next packet 115 uint8_t header[13]; 116 bytes_read = __read(fd, header, sizeof(header)); 117 if (0 >= bytes_read) break; 118 119 uint8_t packet[256]; 120 uint32_t size = big_endian_read_32(header, 0) - 9; 121 bytes_read = __read(fd, packet, size); 122 123 uint8_t type = header[12]; 124 if (type != 9) continue; 125 sco_packet_counter++; 126 127 int16_t audio_frame_out[128]; // 128 129 if (size > sizeof(audio_frame_out)){ 130 printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n"); 131 break; 132 } 133 134 const int audio_bytes_read = size - 3; 135 const int num_samples = audio_bytes_read / BYTES_PER_FRAME; 136 137 // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO 138 uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff; 139 if (sco_handle == 0) continue; 140 141 // convert into host endian 142 int16_t audio_frame_in[128]; 143 int i; 144 for (i=0;i<num_samples;i++){ 145 audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2); 146 } 147 148 #if 1 149 btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, num_samples, audio_frame_out); 150 #else 151 memcpy(audio_frame_out, audio_frame_in, audio_bytes_read); 152 #endif 153 154 155 wav_writer_write_int16(num_samples, audio_frame_out); 156 } 157 158 wav_writer_close(); 159 close(fd); 160 161 btstack_cvsd_dump_statistics(&plc_state); 162 } 163