xref: /btstack/test/hfp/pklg_cvsd_test.c (revision 54736c11db36f88bdbe80a13bbc1939357e82b68)
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"
5437bb4f7bSMatthias Ringwald #include "classic/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 
show_usage(void)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 
__read(int fd,void * buf,size_t count)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 
process_file(const char * pklg_path,const char * wav_path,int packet_type,int plc_enabled)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 
886cfd76e4SMatthias Ringwald     int oflags = O_RDONLY;
896cfd76e4SMatthias Ringwald #ifdef _WIN32
906cfd76e4SMatthias Ringwald     oflags |= O_BINARY;
916cfd76e4SMatthias Ringwald #endif
926cfd76e4SMatthias 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 
1100d719a1eSMatthias Ringwald         uint32_t size = big_endian_read_32(header, 0);
111e2cf2490SMatthias Ringwald 
1120d719a1eSMatthias Ringwald         // auto-detect endianess of size param
1130d719a1eSMatthias Ringwald         if (size >0xffff){
1140d719a1eSMatthias Ringwald             size = little_endian_read_32(header, 0);
1150d719a1eSMatthias Ringwald         }
1160d719a1eSMatthias Ringwald         // subtract header
1170d719a1eSMatthias Ringwald         size -= 9;
1180d719a1eSMatthias Ringwald 
1190d719a1eSMatthias Ringwald         uint8_t packet[256];
120e2cf2490SMatthias Ringwald         uint8_t type = header[12];
1218e95b336SMatthias Ringwald 
12220834b7aSMilanka Ringwald         if (type != packet_type) {
12320834b7aSMilanka Ringwald             // skip data
12420834b7aSMilanka Ringwald             while (size){
12520834b7aSMilanka Ringwald                 int bytes_to_read = btstack_min(sizeof(packet), size);
12620834b7aSMilanka Ringwald                 __read(fd, packet, bytes_to_read);
12720834b7aSMilanka Ringwald                 size -= bytes_to_read;
12820834b7aSMilanka Ringwald             }
12920834b7aSMilanka Ringwald         } else {
13020834b7aSMilanka Ringwald             if (size > sizeof(packet) ){
13120834b7aSMilanka Ringwald                 printf("Error: size %d, packet counter %d \n", size, sco_packet_counter);
13220834b7aSMilanka Ringwald                 exit(10);
13320834b7aSMilanka Ringwald             }
13420834b7aSMilanka Ringwald             bytes_read = __read(fd, packet, size);
1358e95b336SMatthias Ringwald 
136e2cf2490SMatthias Ringwald             sco_packet_counter++;
137e2cf2490SMatthias Ringwald 
138e2cf2490SMatthias Ringwald             int16_t audio_frame_out[128];    //
139e2cf2490SMatthias Ringwald 
140e2cf2490SMatthias Ringwald             if (size > sizeof(audio_frame_out)){
141e2cf2490SMatthias Ringwald                 printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n");
142e2cf2490SMatthias Ringwald                 break;
143e2cf2490SMatthias Ringwald             }
144e2cf2490SMatthias Ringwald 
145e2cf2490SMatthias Ringwald             const int audio_bytes_read = size - 3;
146e2cf2490SMatthias Ringwald             const int num_samples = audio_bytes_read / BYTES_PER_FRAME;
147e2cf2490SMatthias Ringwald 
1483a8dd2bfSMatthias Ringwald             // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO
1493a8dd2bfSMatthias Ringwald             uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff;
1503a8dd2bfSMatthias Ringwald             if (sco_handle == 0) continue;
1513a8dd2bfSMatthias Ringwald 
152e2cf2490SMatthias Ringwald             // convert into host endian
153e2cf2490SMatthias Ringwald             int16_t audio_frame_in[128];
154e2cf2490SMatthias Ringwald             int i;
155e2cf2490SMatthias Ringwald             for (i=0;i<num_samples;i++){
156e2cf2490SMatthias Ringwald                 audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2);
157e2cf2490SMatthias Ringwald             }
158e2cf2490SMatthias Ringwald 
1598e95b336SMatthias Ringwald             if (plc_enabled){
160163d9175SMatthias Ringwald                 if (num_samples > 60){
1614795fc1aSMatthias Ringwald                     btstack_cvsd_plc_process_data(&plc_state, false, audio_frame_in, 60, audio_frame_out);
162163d9175SMatthias Ringwald                     wav_writer_write_int16(60, audio_frame_out);
1634795fc1aSMatthias Ringwald                     btstack_cvsd_plc_process_data(&plc_state, false, &audio_frame_in[60], num_samples - 60, audio_frame_out);
164163d9175SMatthias Ringwald                     wav_writer_write_int16(num_samples - 60, audio_frame_out);
1658e95b336SMatthias Ringwald                 } else {
1664795fc1aSMatthias Ringwald                     btstack_cvsd_plc_process_data(&plc_state, false, audio_frame_in, num_samples, audio_frame_out);
167e2cf2490SMatthias Ringwald                     wav_writer_write_int16(num_samples, audio_frame_out);
168e2cf2490SMatthias Ringwald                 }
169163d9175SMatthias Ringwald             } else {
170163d9175SMatthias Ringwald                 wav_writer_write_int16(num_samples, audio_frame_in);
171163d9175SMatthias Ringwald             }
172163d9175SMatthias Ringwald         }
17320834b7aSMilanka Ringwald     }
174e2cf2490SMatthias Ringwald 
175e2cf2490SMatthias Ringwald     wav_writer_close();
176e2cf2490SMatthias Ringwald     close(fd);
1771db30a88SMatthias Ringwald 
1781db30a88SMatthias Ringwald     btstack_cvsd_dump_statistics(&plc_state);
179e2cf2490SMatthias Ringwald }
1808e95b336SMatthias Ringwald 
main(int argc,const char * argv[])1818e95b336SMatthias Ringwald int main (int argc, const char * argv[]){
1828e95b336SMatthias Ringwald 
1838e95b336SMatthias Ringwald     char wav_path[1000];
1848e95b336SMatthias Ringwald     char pklg_path[1000];
1858e95b336SMatthias Ringwald 
1868e95b336SMatthias Ringwald     if (argc < 2){
1878e95b336SMatthias Ringwald         show_usage();
1888e95b336SMatthias Ringwald         return -1;
1898e95b336SMatthias Ringwald     }
1908e95b336SMatthias Ringwald 
1918e95b336SMatthias Ringwald     int argv_pos = 1;
1928e95b336SMatthias Ringwald     const char * filename = argv[argv_pos++];
1938e95b336SMatthias Ringwald 
194d1550f90SMilanka Ringwald #ifdef OCTAVE_OUTPUT
195d1550f90SMilanka Ringwald     printf("OCTAVE OUTPUT active\n");
196d1550f90SMilanka Ringwald     btstack_cvsd_plc_octave_set_base_name(filename);
197d1550f90SMilanka Ringwald #endif
198d1550f90SMilanka Ringwald 
199*54736c11SMatthias Ringwald     btstack_strcpy(pklg_path, sizeof(pklg_path), filename);
200*54736c11SMatthias Ringwald     btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg");
2018e95b336SMatthias Ringwald 
2028e95b336SMatthias Ringwald     // in file, no plc
2038e95b336SMatthias Ringwald     strcpy(wav_path, filename);
2048e95b336SMatthias Ringwald     strcat(wav_path, "_in_raw.wav");
2058e95b336SMatthias Ringwald     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0);
2068e95b336SMatthias Ringwald 
2078e95b336SMatthias Ringwald     // in file, plc
2088e95b336SMatthias Ringwald     strcpy(wav_path, filename);
2098e95b336SMatthias Ringwald     strcat(wav_path, "_in_plc.wav");
2108e95b336SMatthias Ringwald     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1);
2118e95b336SMatthias Ringwald 
2128e95b336SMatthias Ringwald     // out file, no plc
2138e95b336SMatthias Ringwald     strcpy(wav_path, filename);
2148e95b336SMatthias Ringwald     strcat(wav_path, "_out.wav");
2158e95b336SMatthias Ringwald     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0);
2168e95b336SMatthias Ringwald }
217