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