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 608e95b336SMatthias Ringwald #define PAKET_TYPE_SCO_OUT 8 618e95b336SMatthias 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 848e95b336SMatthias Ringwald static void process_file(const char * pklg_path, const char * wav_path, int packet_type, int plc_enabled){ 85e2cf2490SMatthias Ringwald 868e95b336SMatthias 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*6cfd76e4SMatthias Ringwald int oflags = O_RDONLY; 89*6cfd76e4SMatthias Ringwald #ifdef _WIN32 90*6cfd76e4SMatthias Ringwald oflags |= O_BINARY; 91*6cfd76e4SMatthias Ringwald #endif 92*6cfd76e4SMatthias Ringwald int fd = open(pklg_path, oflags); 93e2cf2490SMatthias Ringwald if (fd < 0) { 948e95b336SMatthias Ringwald printf("Can't open file %s", pklg_path); 958e95b336SMatthias Ringwald return; 96e2cf2490SMatthias Ringwald } 97e2cf2490SMatthias Ringwald 988e95b336SMatthias Ringwald wav_writer_open(wav_path, 1, 8000); 99e2cf2490SMatthias Ringwald 100e2cf2490SMatthias Ringwald btstack_cvsd_plc_init(&plc_state); 101e2cf2490SMatthias Ringwald 102e2cf2490SMatthias Ringwald int sco_packet_counter = 0; 103e2cf2490SMatthias Ringwald while (1){ 104e2cf2490SMatthias Ringwald int bytes_read; 105e2cf2490SMatthias Ringwald // get next packet 106e2cf2490SMatthias Ringwald uint8_t header[13]; 107e2cf2490SMatthias Ringwald bytes_read = __read(fd, header, sizeof(header)); 108e2cf2490SMatthias Ringwald if (0 >= bytes_read) break; 109e2cf2490SMatthias Ringwald 110e2cf2490SMatthias Ringwald uint8_t packet[256]; 111e2cf2490SMatthias Ringwald uint32_t size = big_endian_read_32(header, 0) - 9; 112e2cf2490SMatthias Ringwald bytes_read = __read(fd, packet, size); 113e2cf2490SMatthias Ringwald 114e2cf2490SMatthias Ringwald uint8_t type = header[12]; 1158e95b336SMatthias Ringwald 1168e95b336SMatthias Ringwald if (type != packet_type) continue; 1178e95b336SMatthias Ringwald 118e2cf2490SMatthias Ringwald sco_packet_counter++; 119e2cf2490SMatthias Ringwald 120e2cf2490SMatthias Ringwald int16_t audio_frame_out[128]; // 121e2cf2490SMatthias Ringwald 122e2cf2490SMatthias Ringwald if (size > sizeof(audio_frame_out)){ 123e2cf2490SMatthias Ringwald printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n"); 124e2cf2490SMatthias Ringwald break; 125e2cf2490SMatthias Ringwald } 126e2cf2490SMatthias Ringwald 127e2cf2490SMatthias Ringwald const int audio_bytes_read = size - 3; 128e2cf2490SMatthias Ringwald const int num_samples = audio_bytes_read / BYTES_PER_FRAME; 129e2cf2490SMatthias Ringwald 1303a8dd2bfSMatthias Ringwald // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO 1313a8dd2bfSMatthias Ringwald uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff; 1323a8dd2bfSMatthias Ringwald if (sco_handle == 0) continue; 1333a8dd2bfSMatthias Ringwald 134e2cf2490SMatthias Ringwald // convert into host endian 135e2cf2490SMatthias Ringwald int16_t audio_frame_in[128]; 136e2cf2490SMatthias Ringwald int i; 137e2cf2490SMatthias Ringwald for (i=0;i<num_samples;i++){ 138e2cf2490SMatthias Ringwald audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2); 139e2cf2490SMatthias Ringwald } 140e2cf2490SMatthias Ringwald 1418e95b336SMatthias Ringwald if (plc_enabled){ 142e2cf2490SMatthias Ringwald btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, num_samples, audio_frame_out); 1438e95b336SMatthias Ringwald } else { 1443a8dd2bfSMatthias Ringwald memcpy(audio_frame_out, audio_frame_in, audio_bytes_read); 1458e95b336SMatthias Ringwald } 1463a8dd2bfSMatthias Ringwald 147e2cf2490SMatthias Ringwald wav_writer_write_int16(num_samples, audio_frame_out); 148e2cf2490SMatthias Ringwald } 149e2cf2490SMatthias Ringwald 150e2cf2490SMatthias Ringwald wav_writer_close(); 151e2cf2490SMatthias Ringwald close(fd); 1521db30a88SMatthias Ringwald 1531db30a88SMatthias Ringwald btstack_cvsd_dump_statistics(&plc_state); 154e2cf2490SMatthias Ringwald } 1558e95b336SMatthias Ringwald 1568e95b336SMatthias Ringwald int main (int argc, const char * argv[]){ 1578e95b336SMatthias Ringwald 1588e95b336SMatthias Ringwald char wav_path[1000]; 1598e95b336SMatthias Ringwald char pklg_path[1000]; 1608e95b336SMatthias Ringwald 1618e95b336SMatthias Ringwald if (argc < 2){ 1628e95b336SMatthias Ringwald show_usage(); 1638e95b336SMatthias Ringwald return -1; 1648e95b336SMatthias Ringwald } 1658e95b336SMatthias Ringwald 1668e95b336SMatthias Ringwald int argv_pos = 1; 1678e95b336SMatthias Ringwald const char * filename = argv[argv_pos++]; 1688e95b336SMatthias Ringwald 169d1550f90SMilanka Ringwald #ifdef OCTAVE_OUTPUT 170d1550f90SMilanka Ringwald printf("OCTAVE OUTPUT active\n"); 171d1550f90SMilanka Ringwald btstack_cvsd_plc_octave_set_base_name(filename); 172d1550f90SMilanka Ringwald #endif 173d1550f90SMilanka Ringwald 1748e95b336SMatthias Ringwald strcpy(pklg_path, filename); 1758e95b336SMatthias Ringwald strcat(pklg_path, ".pklg"); 1768e95b336SMatthias Ringwald 1778e95b336SMatthias Ringwald // in file, no plc 1788e95b336SMatthias Ringwald strcpy(wav_path, filename); 1798e95b336SMatthias Ringwald strcat(wav_path, "_in_raw.wav"); 1808e95b336SMatthias Ringwald process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0); 1818e95b336SMatthias Ringwald 1828e95b336SMatthias Ringwald // in file, plc 1838e95b336SMatthias Ringwald strcpy(wav_path, filename); 1848e95b336SMatthias Ringwald strcat(wav_path, "_in_plc.wav"); 1858e95b336SMatthias Ringwald process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1); 1868e95b336SMatthias Ringwald 1878e95b336SMatthias Ringwald // out file, no plc 1888e95b336SMatthias Ringwald strcpy(wav_path, filename); 1898e95b336SMatthias Ringwald strcat(wav_path, "_out.wav"); 1908e95b336SMatthias Ringwald process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0); 1918e95b336SMatthias Ringwald } 192