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