xref: /btstack/test/hfp/pklg_cvsd_test.c (revision e9c5f44ee8add45f6cd4be8b6faa9e09a2804fcc)
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 
113         uint8_t type = header[12];
114 
115         if (type != packet_type) {
116             // skip data
117             while (size){
118                 int bytes_to_read = btstack_min(sizeof(packet), size);
119                 __read(fd, packet, bytes_to_read);
120                 size -= bytes_to_read;
121             }
122         } else {
123             if (size > sizeof(packet) ){
124                 printf("Error: size %d, packet counter %d \n", size, sco_packet_counter);
125                 exit(10);
126             }
127             bytes_read = __read(fd, packet, size);
128 
129             sco_packet_counter++;
130 
131             int16_t audio_frame_out[128];    //
132 
133             if (size > sizeof(audio_frame_out)){
134                 printf("sco_demo_receive_CVSD: SCO packet larger than local output buffer - dropping data.\n");
135                 break;
136             }
137 
138             const int audio_bytes_read = size - 3;
139             const int num_samples = audio_bytes_read / BYTES_PER_FRAME;
140 
141             // check SCO handle -- quick hack, not correct if handle 0x0000 is actually used for SCO
142             uint16_t sco_handle = little_endian_read_16(packet, 0) & 0xfff;
143             if (sco_handle == 0) continue;
144 
145             // convert into host endian
146             int16_t audio_frame_in[128];
147             int i;
148             for (i=0;i<num_samples;i++){
149                 audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2);
150             }
151 
152             if (plc_enabled){
153                 btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, num_samples, audio_frame_out);
154             } else {
155                 memcpy(audio_frame_out, audio_frame_in, audio_bytes_read);
156             }
157 
158             wav_writer_write_int16(num_samples, audio_frame_out);
159         }
160     }
161 
162     wav_writer_close();
163     close(fd);
164 
165     btstack_cvsd_dump_statistics(&plc_state);
166 }
167 
168 int main (int argc, const char * argv[]){
169 
170     char wav_path[1000];
171     char pklg_path[1000];
172 
173     if (argc < 2){
174         show_usage();
175         return -1;
176     }
177 
178     int argv_pos = 1;
179     const char * filename = argv[argv_pos++];
180 
181 #ifdef OCTAVE_OUTPUT
182     printf("OCTAVE OUTPUT active\n");
183     btstack_cvsd_plc_octave_set_base_name(filename);
184 #endif
185 
186     strcpy(pklg_path, filename);
187     strcat(pklg_path, ".pklg");
188 
189     // in file, no plc
190     strcpy(wav_path, filename);
191     strcat(wav_path, "_in_raw.wav");
192     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0);
193 
194     // in file, plc
195     strcpy(wav_path, filename);
196     strcat(wav_path, "_in_plc.wav");
197     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1);
198 
199     // out file, no plc
200     strcpy(wav_path, filename);
201     strcat(wav_path, "_out.wav");
202     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0);
203 }
204