xref: /btstack/test/sbc/msbc_encoder_test.c (revision 893dbb9455d8018c779f7b9fef03fd5098bfe58e)
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 encoder 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 
55 #include "hfp_msbc.h"
56 #include "btstack_sbc.h"
57 #include "wav_util.h"
58 
59 static int16_t read_buffer[8*16*2];
60 static uint8_t output_buffer[24];
61 
62 int main (int argc, const char * argv[]){
63     if (argc < 3){
64         printf("Usage: %s WAV_FILE mSBC_FILE\n", argv[0]);
65         return -1;
66     }
67 
68     const char * wav_filename = argv[1];
69     const char * sbc_filename = argv[2];
70 
71     if (wav_reader_open(wav_filename) != 0) {
72         printf("Can't open file %s", wav_filename);
73         return -1;
74     }
75 
76     FILE * sbc_fd = fopen(sbc_filename, "wb");
77     if (!sbc_fd) {
78         printf("Can't open file %s", sbc_filename);
79         return -1;
80     }
81 
82     hfp_msbc_init();
83     int num_samples = hfp_msbc_num_audio_samples_per_frame();
84 
85     while (1){
86         if (hfp_msbc_can_encode_audio_frame_now()){
87             int error = wav_reader_read_int16(num_samples, read_buffer);
88             if (error) break;
89 
90             hfp_msbc_encode_audio_frame(read_buffer);
91         }
92         if (hfp_msbc_num_bytes_in_stream() >= sizeof(output_buffer)){
93             hfp_msbc_read_from_stream(output_buffer, sizeof(output_buffer));
94             fwrite(output_buffer, 1, sizeof(output_buffer), sbc_fd);
95         }
96     }
97 
98     printf("Done\n");
99     wav_reader_close();
100     fclose(sbc_fd);
101     return 0;
102 }
103 
104