1e2cf2490SMatthias Ringwald /* 2e2cf2490SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3e2cf2490SMatthias Ringwald * 4e2cf2490SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5e2cf2490SMatthias Ringwald * modification, are permitted provided that the following conditions 6e2cf2490SMatthias Ringwald * are met: 7e2cf2490SMatthias Ringwald * 8e2cf2490SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9e2cf2490SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10e2cf2490SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11e2cf2490SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12e2cf2490SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13e2cf2490SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14e2cf2490SMatthias Ringwald * contributors may be used to endorse or promote products derived 15e2cf2490SMatthias Ringwald * from this software without specific prior written permission. 16e2cf2490SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17e2cf2490SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18e2cf2490SMatthias Ringwald * monetary gain. 19e2cf2490SMatthias Ringwald * 20e2cf2490SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21e2cf2490SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22e2cf2490SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23e2cf2490SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24e2cf2490SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25e2cf2490SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26e2cf2490SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27e2cf2490SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28e2cf2490SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29e2cf2490SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30e2cf2490SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31e2cf2490SMatthias Ringwald * SUCH DAMAGE. 32e2cf2490SMatthias Ringwald * 33e2cf2490SMatthias Ringwald * Please inquire about commercial licensing options at 34e2cf2490SMatthias Ringwald * [email protected] 35e2cf2490SMatthias Ringwald * 36e2cf2490SMatthias Ringwald */ 37e2cf2490SMatthias Ringwald 38e2cf2490SMatthias Ringwald // ***************************************************************************** 39e2cf2490SMatthias Ringwald // 40e2cf2490SMatthias Ringwald // SBC decoder tests 41e2cf2490SMatthias Ringwald // 42e2cf2490SMatthias Ringwald // ***************************************************************************** 43e2cf2490SMatthias Ringwald 44e2cf2490SMatthias Ringwald #include "btstack_config.h" 45e2cf2490SMatthias Ringwald 46e2cf2490SMatthias Ringwald #include <stdint.h> 47e2cf2490SMatthias Ringwald #include <stdio.h> 48e2cf2490SMatthias Ringwald #include <stdlib.h> 49e2cf2490SMatthias Ringwald #include <string.h> 50e2cf2490SMatthias Ringwald #include <fcntl.h> 51e2cf2490SMatthias Ringwald #include <unistd.h> 52e2cf2490SMatthias Ringwald 53e2cf2490SMatthias Ringwald #include "btstack.h" 54e2cf2490SMatthias Ringwald #include "btstack_cvsd_plc.h" 55e2cf2490SMatthias Ringwald 56e2cf2490SMatthias Ringwald #include "wav_util.h" 57e2cf2490SMatthias Ringwald 58e2cf2490SMatthias Ringwald #define BYTES_PER_FRAME 2 59e2cf2490SMatthias Ringwald 60*8e95b336SMatthias Ringwald #define PAKET_TYPE_SCO_OUT 8 61*8e95b336SMatthias Ringwald #define PAKET_TYPE_SCO_IN 9 62e2cf2490SMatthias Ringwald 63e2cf2490SMatthias Ringwald static btstack_cvsd_plc_state_t plc_state; 64e2cf2490SMatthias Ringwald 65e2cf2490SMatthias Ringwald static void show_usage(void){ 66e2cf2490SMatthias Ringwald printf("\n\nUsage: ./pklg_cvsd_test input_file\n"); 67e2cf2490SMatthias Ringwald printf("Example: ./pklg_cvsd_test pklg/test1\n"); 68e2cf2490SMatthias Ringwald } 69e2cf2490SMatthias Ringwald 70e2cf2490SMatthias Ringwald static ssize_t __read(int fd, void *buf, size_t count){ 71e2cf2490SMatthias Ringwald ssize_t len, pos = 0; 72e2cf2490SMatthias Ringwald 73e2cf2490SMatthias Ringwald while (count > 0) { 74e2cf2490SMatthias Ringwald len = read(fd, (int8_t * )buf + pos, count); 75e2cf2490SMatthias Ringwald if (len <= 0) 76e2cf2490SMatthias Ringwald return pos; 77e2cf2490SMatthias Ringwald 78e2cf2490SMatthias Ringwald count -= len; 79e2cf2490SMatthias Ringwald pos += len; 80e2cf2490SMatthias Ringwald } 81e2cf2490SMatthias Ringwald return pos; 82e2cf2490SMatthias Ringwald } 83e2cf2490SMatthias Ringwald 84*8e95b336SMatthias Ringwald static void process_file(const char * pklg_path, const char * wav_path, int packet_type, int plc_enabled){ 85e2cf2490SMatthias Ringwald 86*8e95b336SMatthias Ringwald printf("Processing %s -> %s: PLC enabled: %u, direction %s\n", pklg_path, wav_path, plc_enabled, packet_type == PAKET_TYPE_SCO_OUT ? "Out" : "In"); 87e2cf2490SMatthias Ringwald 88*8e95b336SMatthias Ringwald int fd = open(pklg_path, O_RDONLY); 89e2cf2490SMatthias Ringwald if (fd < 0) { 90*8e95b336SMatthias Ringwald printf("Can't open file %s", pklg_path); 91*8e95b336SMatthias Ringwald return; 92e2cf2490SMatthias Ringwald } 93e2cf2490SMatthias Ringwald 94*8e95b336SMatthias Ringwald wav_writer_open(wav_path, 1, 8000); 95e2cf2490SMatthias Ringwald 96e2cf2490SMatthias Ringwald btstack_cvsd_plc_init(&plc_state); 97e2cf2490SMatthias Ringwald 98e2cf2490SMatthias Ringwald int sco_packet_counter = 0; 99e2cf2490SMatthias Ringwald while (1){ 100e2cf2490SMatthias Ringwald int bytes_read; 101e2cf2490SMatthias Ringwald // get next packet 102e2cf2490SMatthias Ringwald uint8_t header[13]; 103e2cf2490SMatthias Ringwald bytes_read = __read(fd, header, sizeof(header)); 104e2cf2490SMatthias Ringwald if (0 >= bytes_read) break; 105e2cf2490SMatthias Ringwald 106e2cf2490SMatthias Ringwald uint8_t packet[256]; 107e2cf2490SMatthias Ringwald uint32_t size = big_endian_read_32(header, 0) - 9; 108e2cf2490SMatthias Ringwald bytes_read = __read(fd, packet, size); 109e2cf2490SMatthias Ringwald 110e2cf2490SMatthias Ringwald uint8_t type = header[12]; 111*8e95b336SMatthias Ringwald 112*8e95b336SMatthias Ringwald if (type != packet_type) continue; 113*8e95b336SMatthias Ringwald 114e2cf2490SMatthias Ringwald sco_packet_counter++; 115e2cf2490SMatthias Ringwald 116e2cf2490SMatthias Ringwald int16_t audio_frame_out[128]; // 117e2cf2490SMatthias Ringwald 118e2cf2490SMatthias Ringwald if (size > sizeof(audio_frame_out)){ 119e2cf2490SMatthias Ringwald printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n"); 120e2cf2490SMatthias Ringwald break; 121e2cf2490SMatthias Ringwald } 122e2cf2490SMatthias Ringwald 123e2cf2490SMatthias Ringwald const int audio_bytes_read = size - 3; 124e2cf2490SMatthias Ringwald const int num_samples = audio_bytes_read / BYTES_PER_FRAME; 125e2cf2490SMatthias Ringwald 1263a8dd2bfSMatthias Ringwald // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO 1273a8dd2bfSMatthias Ringwald uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff; 1283a8dd2bfSMatthias Ringwald if (sco_handle == 0) continue; 1293a8dd2bfSMatthias Ringwald 130e2cf2490SMatthias Ringwald // convert into host endian 131e2cf2490SMatthias Ringwald int16_t audio_frame_in[128]; 132e2cf2490SMatthias Ringwald int i; 133e2cf2490SMatthias Ringwald for (i=0;i<num_samples;i++){ 134e2cf2490SMatthias Ringwald audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2); 135e2cf2490SMatthias Ringwald } 136e2cf2490SMatthias Ringwald 137*8e95b336SMatthias Ringwald if (plc_enabled){ 138e2cf2490SMatthias Ringwald btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, num_samples, audio_frame_out); 139*8e95b336SMatthias Ringwald } else { 1403a8dd2bfSMatthias Ringwald memcpy(audio_frame_out, audio_frame_in, audio_bytes_read); 141*8e95b336SMatthias Ringwald } 1423a8dd2bfSMatthias Ringwald 143e2cf2490SMatthias Ringwald wav_writer_write_int16(num_samples, audio_frame_out); 144e2cf2490SMatthias Ringwald } 145e2cf2490SMatthias Ringwald 146e2cf2490SMatthias Ringwald wav_writer_close(); 147e2cf2490SMatthias Ringwald close(fd); 1481db30a88SMatthias Ringwald 1491db30a88SMatthias Ringwald btstack_cvsd_dump_statistics(&plc_state); 150e2cf2490SMatthias Ringwald } 151*8e95b336SMatthias Ringwald 152*8e95b336SMatthias Ringwald int main (int argc, const char * argv[]){ 153*8e95b336SMatthias Ringwald 154*8e95b336SMatthias Ringwald char wav_path[1000]; 155*8e95b336SMatthias Ringwald char pklg_path[1000]; 156*8e95b336SMatthias Ringwald 157*8e95b336SMatthias Ringwald if (argc < 2){ 158*8e95b336SMatthias Ringwald show_usage(); 159*8e95b336SMatthias Ringwald return -1; 160*8e95b336SMatthias Ringwald } 161*8e95b336SMatthias Ringwald 162*8e95b336SMatthias Ringwald int argv_pos = 1; 163*8e95b336SMatthias Ringwald const char * filename = argv[argv_pos++]; 164*8e95b336SMatthias Ringwald 165*8e95b336SMatthias Ringwald strcpy(pklg_path, filename); 166*8e95b336SMatthias Ringwald strcat(pklg_path, ".pklg"); 167*8e95b336SMatthias Ringwald 168*8e95b336SMatthias Ringwald // in file, no plc 169*8e95b336SMatthias Ringwald strcpy(wav_path, filename); 170*8e95b336SMatthias Ringwald strcat(wav_path, "_in_raw.wav"); 171*8e95b336SMatthias Ringwald process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0); 172*8e95b336SMatthias Ringwald 173*8e95b336SMatthias Ringwald // in file, plc 174*8e95b336SMatthias Ringwald strcpy(wav_path, filename); 175*8e95b336SMatthias Ringwald strcat(wav_path, "_in_plc.wav"); 176*8e95b336SMatthias Ringwald process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1); 177*8e95b336SMatthias Ringwald 178*8e95b336SMatthias Ringwald // out file, no plc 179*8e95b336SMatthias Ringwald strcpy(wav_path, filename); 180*8e95b336SMatthias Ringwald strcat(wav_path, "_out.wav"); 181*8e95b336SMatthias Ringwald process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0); 182*8e95b336SMatthias Ringwald } 183