xref: /btstack/test/lc3/lc3_encoder.c (revision 4a9eead824c50b40e12b6f72611a74a3f57a47f6)
1 /*
2  * Copyright (C) 2022 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 // LC3 decoder EHIMA
41 //
42 // *****************************************************************************
43 
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 
51 #include "wav_util.h"
52 #include "btstack_util.h"
53 #include "btstack_debug.h"
54 
55 #include "btstack_lc3.h"
56 #include "btstack_lc3_google.h"
57 
58 #define MAX_NUM_CHANNELS 2
59 #define MAX_SAMPLES_PER_FRAME 480
60 
61 static uint8_t  write_buffer[200];
62 static int16_t  samples_buffer[MAX_SAMPLES_PER_FRAME + MAX_NUM_CHANNELS];
63 
64 static uint32_t frame_count = 0;
65 
66 static void show_usage(const char * path){
67     printf("Usage: %s input.wav output.lc3 frame_duration_ms octets_per_frame\n", path);
68     printf("- frame_duration_ms: 7.5 or 10\n");
69     printf("- octects_per_frame: 26..155\n");
70     printf("\n\n");
71 }
72 
73 int main (int argc, const char * argv[]){
74     if (argc < 4){
75         show_usage(argv[0]);
76         return -1;
77     }
78 
79     uint8_t argv_pos = 1;
80     const char * wav_filename = argv[argv_pos++];
81     const char * lc3_filename = argv[argv_pos++];
82 
83     btstack_lc3_frame_duration_t frame_duration;
84     if (strcmp(argv[argv_pos], "10") == 0){
85         frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US;
86     } else if (strcmp(argv[argv_pos], "7.5") == 0){
87         frame_duration = BTSTACK_LC3_FRAME_DURATION_7500US;
88     } else {
89         printf("Invalid frame duration %s, must be either 7.5 or 10\n", argv[2]);
90         return -10;
91     }
92     argv_pos++;
93 
94     uint16_t bytes_per_frame = atoi(argv[argv_pos++]);
95     if ((bytes_per_frame < 26) || (bytes_per_frame > 155)){
96         printf("Octets per Frame %u out of range [26..155]\n", bytes_per_frame);
97         return -10;
98     }
99 
100     int status = wav_reader_open(wav_filename);
101     if (status != 0){
102         printf("Could not open wav file %s\n", wav_filename);
103         return -10;
104     }
105 
106     // get wav config
107     uint32_t sampling_frequency_hz = wav_reader_get_sampling_rate();
108     uint8_t  num_channels = wav_reader_get_num_channels();
109 
110     // init decoder
111     uint8_t channel;
112     btstack_lc3_encoder_google_t encoder_contexts[MAX_NUM_CHANNELS];
113     const btstack_lc3_encoder_t * lc3_encoder;
114     for (channel = 0 ; channel < num_channels ; channel++){
115         btstack_lc3_encoder_google_t * encoder_context = &encoder_contexts[channel];
116         lc3_encoder = btstack_lc3_encoder_google_init_instance(encoder_context);
117         lc3_encoder->configure(encoder_context, sampling_frequency_hz, frame_duration);
118     }
119     uint32_t bitrate_per_channel = lc3_encoder->get_bitrate_for_number_of_octets(&encoder_contexts[0], bytes_per_frame);
120     uint32_t bitrate = bitrate_per_channel * num_channels;
121     uint16_t number_samples_per_frame = lc3_encoder->get_number_samples_per_frame(&encoder_contexts[0]);
122 
123     if (number_samples_per_frame > MAX_SAMPLES_PER_FRAME) return -10;
124 
125     // create lc3 file and write header for floating point implementation
126     FILE * lc3_file = fopen(lc3_filename, "wb");
127     if (!lc3_file) return 1;
128 
129     uint16_t frame_duration_100us = (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US) ? 100 : 75;
130 
131     uint8_t header[18];
132     little_endian_store_16(header, 0, 0xcc1c);
133     little_endian_store_16(header, 2, sizeof(header));
134     little_endian_store_16(header, 4, sampling_frequency_hz / 100);
135     little_endian_store_16(header, 6, bitrate / 100);
136     little_endian_store_16(header, 8, num_channels);
137     little_endian_store_16(header, 10, frame_duration_100us * 10);
138     little_endian_store_16(header, 12, 0);
139     little_endian_store_32(header, 14, 0); // num samples need to set later
140     fwrite(header, 1, sizeof(header), lc3_file);
141 
142     // print format
143     printf("WAC file:          %s\n", wav_filename);
144     printf("LC3 file:          %s\n", lc3_filename);
145     printf("Samplerate:        %u Hz\n", sampling_frequency_hz);
146     printf("Channels:          %u\n", num_channels);
147     printf("Frame duration:    %s ms\n", (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US) ? "10" : "7.5");
148     printf("Bitrate:           %u\n", bitrate);
149     printf("Samples per Frame: %u\n", number_samples_per_frame);
150 
151     while (true){
152         // process file frame by frame
153         memset(samples_buffer, 0, sizeof(samples_buffer));
154         // read samples per frame * num channels
155         status = wav_reader_read_int16(number_samples_per_frame * num_channels, samples_buffer);
156 
157         if (status != 0) break;
158 
159         // write len of complete frame
160         uint8_t len[2];
161         little_endian_store_16(len, 0, num_channels * bytes_per_frame);
162         fwrite(len, 1, sizeof(len), lc3_file);
163 
164         // encode frame by frame
165         for (channel = 0; channel < num_channels ; channel++){
166             status = lc3_encoder->encode_signed_16(&encoder_contexts[channel], &samples_buffer[channel], num_channels, write_buffer, bytes_per_frame);
167             if (status != ERROR_CODE_SUCCESS){
168                 printf("Error %u\n", status);
169                 break;
170             }
171             fwrite(write_buffer, 1, bytes_per_frame, lc3_file);
172         }
173 
174         if (status != 0) break;
175 
176         frame_count++;
177     }
178 
179     uint32_t total_samples = frame_count * number_samples_per_frame;
180     printf("Total samples: %u\n", total_samples);
181 
182     // rewind and store num samples
183     little_endian_store_32(header, 14, total_samples); // num samples need to set later
184     rewind(lc3_file);
185     fwrite(header, 1, sizeof(header), lc3_file);
186     fclose(lc3_file);
187 }
188