xref: /btstack/test/hfp/pklg_cvsd_test.c (revision 20834b7ab7234d7e2144a2804f9261809c5f8c5e)
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 
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 
110e2cf2490SMatthias Ringwald         uint8_t packet[256];
111e2cf2490SMatthias Ringwald         uint32_t size = big_endian_read_32(header, 0) - 9;
112e2cf2490SMatthias Ringwald 
113e2cf2490SMatthias Ringwald         uint8_t type = header[12];
1148e95b336SMatthias Ringwald 
115*20834b7aSMilanka Ringwald         if (type != packet_type) {
116*20834b7aSMilanka Ringwald             // skip data
117*20834b7aSMilanka Ringwald             while (size){
118*20834b7aSMilanka Ringwald                 int bytes_to_read = btstack_min(sizeof(packet), size);
119*20834b7aSMilanka Ringwald                 __read(fd, packet, bytes_to_read);
120*20834b7aSMilanka Ringwald                 size -= bytes_to_read;
121*20834b7aSMilanka Ringwald             }
122*20834b7aSMilanka Ringwald         } else {
123*20834b7aSMilanka Ringwald             if (size > sizeof(packet) ){
124*20834b7aSMilanka Ringwald                 printf("Error: size %d, packet counter %d \n", size, sco_packet_counter);
125*20834b7aSMilanka Ringwald                 exit(10);
126*20834b7aSMilanka Ringwald             }
127*20834b7aSMilanka Ringwald             bytes_read = __read(fd, packet, size);
1288e95b336SMatthias Ringwald 
129e2cf2490SMatthias Ringwald             sco_packet_counter++;
130e2cf2490SMatthias Ringwald 
131e2cf2490SMatthias Ringwald             int16_t audio_frame_out[128];    //
132e2cf2490SMatthias Ringwald 
133e2cf2490SMatthias Ringwald             if (size > sizeof(audio_frame_out)){
134e2cf2490SMatthias Ringwald                 printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n");
135e2cf2490SMatthias Ringwald                 break;
136e2cf2490SMatthias Ringwald             }
137e2cf2490SMatthias Ringwald 
138e2cf2490SMatthias Ringwald             const int audio_bytes_read = size - 3;
139e2cf2490SMatthias Ringwald             const int num_samples = audio_bytes_read / BYTES_PER_FRAME;
140e2cf2490SMatthias Ringwald 
1413a8dd2bfSMatthias Ringwald             // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO
1423a8dd2bfSMatthias Ringwald             uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff;
1433a8dd2bfSMatthias Ringwald             if (sco_handle == 0) continue;
1443a8dd2bfSMatthias Ringwald 
145e2cf2490SMatthias Ringwald             // convert into host endian
146e2cf2490SMatthias Ringwald             int16_t audio_frame_in[128];
147e2cf2490SMatthias Ringwald             int i;
148e2cf2490SMatthias Ringwald             for (i=0;i<num_samples;i++){
149e2cf2490SMatthias Ringwald                 audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2);
150e2cf2490SMatthias Ringwald             }
151e2cf2490SMatthias Ringwald 
1528e95b336SMatthias Ringwald             if (plc_enabled){
153e2cf2490SMatthias Ringwald                 btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, num_samples, audio_frame_out);
1548e95b336SMatthias Ringwald             } else {
1553a8dd2bfSMatthias Ringwald                 memcpy(audio_frame_out, audio_frame_in, audio_bytes_read);
1568e95b336SMatthias Ringwald             }
1573a8dd2bfSMatthias Ringwald 
158e2cf2490SMatthias Ringwald             wav_writer_write_int16(num_samples, audio_frame_out);
159e2cf2490SMatthias Ringwald         }
160*20834b7aSMilanka Ringwald     }
161e2cf2490SMatthias Ringwald 
162e2cf2490SMatthias Ringwald     wav_writer_close();
163e2cf2490SMatthias Ringwald     close(fd);
1641db30a88SMatthias Ringwald 
1651db30a88SMatthias Ringwald     btstack_cvsd_dump_statistics(&plc_state);
166e2cf2490SMatthias Ringwald }
1678e95b336SMatthias Ringwald 
1688e95b336SMatthias Ringwald int main (int argc, const char * argv[]){
1698e95b336SMatthias Ringwald 
1708e95b336SMatthias Ringwald     char wav_path[1000];
1718e95b336SMatthias Ringwald     char pklg_path[1000];
1728e95b336SMatthias Ringwald 
1738e95b336SMatthias Ringwald     if (argc < 2){
1748e95b336SMatthias Ringwald         show_usage();
1758e95b336SMatthias Ringwald         return -1;
1768e95b336SMatthias Ringwald     }
1778e95b336SMatthias Ringwald 
1788e95b336SMatthias Ringwald     int argv_pos = 1;
1798e95b336SMatthias Ringwald     const char * filename = argv[argv_pos++];
1808e95b336SMatthias Ringwald 
181d1550f90SMilanka Ringwald #ifdef OCTAVE_OUTPUT
182d1550f90SMilanka Ringwald     printf("OCTAVE OUTPUT active\n");
183d1550f90SMilanka Ringwald     btstack_cvsd_plc_octave_set_base_name(filename);
184d1550f90SMilanka Ringwald #endif
185d1550f90SMilanka Ringwald 
1868e95b336SMatthias Ringwald     strcpy(pklg_path, filename);
1878e95b336SMatthias Ringwald     strcat(pklg_path, ".pklg");
1888e95b336SMatthias Ringwald 
1898e95b336SMatthias Ringwald     // in file, no plc
1908e95b336SMatthias Ringwald     strcpy(wav_path, filename);
1918e95b336SMatthias Ringwald     strcat(wav_path, "_in_raw.wav");
1928e95b336SMatthias Ringwald     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0);
1938e95b336SMatthias Ringwald 
1948e95b336SMatthias Ringwald     // in file, plc
1958e95b336SMatthias Ringwald     strcpy(wav_path, filename);
1968e95b336SMatthias Ringwald     strcat(wav_path, "_in_plc.wav");
1978e95b336SMatthias Ringwald     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1);
1988e95b336SMatthias Ringwald 
1998e95b336SMatthias Ringwald     // out file, no plc
2008e95b336SMatthias Ringwald     strcpy(wav_path, filename);
2018e95b336SMatthias Ringwald     strcat(wav_path, "_out.wav");
2028e95b336SMatthias Ringwald     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0);
2038e95b336SMatthias Ringwald }
204