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