xref: /btstack/src/btstack_lc3.c (revision 684fe865515eac6d65afb55b82d2e73e3514f228)
165113a0cSMatthias Ringwald /*
265113a0cSMatthias Ringwald  * Copyright (C) 2022 BlueKitchen GmbH
365113a0cSMatthias Ringwald  *
465113a0cSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
565113a0cSMatthias Ringwald  * modification, are permitted provided that the following conditions
665113a0cSMatthias Ringwald  * are met:
765113a0cSMatthias Ringwald  *
865113a0cSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
965113a0cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1065113a0cSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1165113a0cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1265113a0cSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1365113a0cSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1465113a0cSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1565113a0cSMatthias Ringwald  *    from this software without specific prior written permission.
1665113a0cSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1765113a0cSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1865113a0cSMatthias Ringwald  *    monetary gain.
1965113a0cSMatthias Ringwald  *
2065113a0cSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2165113a0cSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2265113a0cSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2365113a0cSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
2465113a0cSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2565113a0cSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2665113a0cSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2765113a0cSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2865113a0cSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2965113a0cSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3065113a0cSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3165113a0cSMatthias Ringwald  * SUCH DAMAGE.
3265113a0cSMatthias Ringwald  *
3365113a0cSMatthias Ringwald  * Please inquire about commercial licensing options at
3465113a0cSMatthias Ringwald  * [email protected]
3565113a0cSMatthias Ringwald  *
3665113a0cSMatthias Ringwald  */
3765113a0cSMatthias Ringwald 
3865113a0cSMatthias Ringwald // *****************************************************************************
3965113a0cSMatthias Ringwald //
4065113a0cSMatthias Ringwald // LC3 Interface
4165113a0cSMatthias Ringwald //
4265113a0cSMatthias Ringwald // *****************************************************************************
4365113a0cSMatthias Ringwald 
4465113a0cSMatthias Ringwald #define BTSTACK_FILE__ "btstack_lc3.c"
4565113a0cSMatthias Ringwald 
4665113a0cSMatthias Ringwald #include "btstack_lc3.h"
4765113a0cSMatthias Ringwald #include "btstack_debug.h"
4865113a0cSMatthias Ringwald 
btstack_lc3_frame_duration_in_us(btstack_lc3_frame_duration_t frame_duration)4965113a0cSMatthias Ringwald uint16_t btstack_lc3_frame_duration_in_us(btstack_lc3_frame_duration_t frame_duration){
5065113a0cSMatthias Ringwald     switch (frame_duration){
5165113a0cSMatthias Ringwald         case BTSTACK_LC3_FRAME_DURATION_7500US:
5265113a0cSMatthias Ringwald             return 7500;
5365113a0cSMatthias Ringwald         case BTSTACK_LC3_FRAME_DURATION_10000US:
5465113a0cSMatthias Ringwald             return 10000;
5565113a0cSMatthias Ringwald         default:
5665113a0cSMatthias Ringwald             return 0;
5765113a0cSMatthias Ringwald     }
5865113a0cSMatthias Ringwald }
5965113a0cSMatthias Ringwald 
btstack_lc3_samples_per_frame(uint32_t sample_rate,btstack_lc3_frame_duration_t frame_duration)6065113a0cSMatthias Ringwald uint16_t btstack_lc3_samples_per_frame(uint32_t sample_rate, btstack_lc3_frame_duration_t frame_duration){
6165113a0cSMatthias Ringwald     // 44.1 kHz uses longer iso interval and same number of samples as 48 khz
6265113a0cSMatthias Ringwald     if (sample_rate == 44100){
6365113a0cSMatthias Ringwald         sample_rate = 48000;
6465113a0cSMatthias Ringwald     }
6565113a0cSMatthias Ringwald     // assume sample rate is x 1000 hz
66*684fe865SMatthias Ringwald     return (sample_rate / 1000) * (btstack_lc3_frame_duration_in_us(frame_duration) / 100) / 10;
6765113a0cSMatthias Ringwald }
68