1*a602f23dSMatthias Ringwald /* 2*a602f23dSMatthias Ringwald * Copyright (C) 2023 BlueKitchen GmbH 3*a602f23dSMatthias Ringwald * 4*a602f23dSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*a602f23dSMatthias Ringwald * modification, are permitted provided that the following conditions 6*a602f23dSMatthias Ringwald * are met: 7*a602f23dSMatthias Ringwald * 8*a602f23dSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*a602f23dSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*a602f23dSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*a602f23dSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*a602f23dSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*a602f23dSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*a602f23dSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*a602f23dSMatthias Ringwald * from this software without specific prior written permission. 16*a602f23dSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*a602f23dSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*a602f23dSMatthias Ringwald * monetary gain. 19*a602f23dSMatthias Ringwald * 20*a602f23dSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*a602f23dSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*a602f23dSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*a602f23dSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24*a602f23dSMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*a602f23dSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*a602f23dSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*a602f23dSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*a602f23dSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*a602f23dSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*a602f23dSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*a602f23dSMatthias Ringwald * SUCH DAMAGE. 32*a602f23dSMatthias Ringwald * 33*a602f23dSMatthias Ringwald * Please inquire about commercial licensing options at 34*a602f23dSMatthias Ringwald * [email protected] 35*a602f23dSMatthias Ringwald * 36*a602f23dSMatthias Ringwald */ 37*a602f23dSMatthias Ringwald 38*a602f23dSMatthias Ringwald /** 39*a602f23dSMatthias Ringwald * @title HFP Audio Encoder 40*a602f23dSMatthias Ringwald * @brief Create SCO packet with H2 Synchronization Header and encoded audio samples 41*a602f23dSMatthias Ringwald */ 42*a602f23dSMatthias Ringwald 43*a602f23dSMatthias Ringwald #ifndef HFP_CODEC_H 44*a602f23dSMatthias Ringwald #define HFP_CODEC_H 45*a602f23dSMatthias Ringwald 46*a602f23dSMatthias Ringwald #include "btstack_config.h" 47*a602f23dSMatthias Ringwald #include "hfp.h" // hfp_h2_framing 48*a602f23dSMatthias Ringwald 49*a602f23dSMatthias Ringwald #include <stdint.h> 50*a602f23dSMatthias Ringwald 51*a602f23dSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 52*a602f23dSMatthias Ringwald #include "btstack_sbc.h" 53*a602f23dSMatthias Ringwald #endif 54*a602f23dSMatthias Ringwald 55*a602f23dSMatthias Ringwald #if defined __cplusplus 56*a602f23dSMatthias Ringwald extern "C" { 57*a602f23dSMatthias Ringwald #endif 58*a602f23dSMatthias Ringwald 59*a602f23dSMatthias Ringwald struct hfp_codec { 60*a602f23dSMatthias Ringwald uint8_t sco_packet[60]; 61*a602f23dSMatthias Ringwald uint16_t read_pos; 62*a602f23dSMatthias Ringwald uint16_t write_pos; 63*a602f23dSMatthias Ringwald uint16_t samples_per_frame; 64*a602f23dSMatthias Ringwald hfp_h2_framing_t h2_framing; 65*a602f23dSMatthias Ringwald void (*encode)(struct hfp_codec * hfp_codec, int16_t * pcm_samples); 66*a602f23dSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 67*a602f23dSMatthias Ringwald btstack_sbc_encoder_state_t msbc_state; 68*a602f23dSMatthias Ringwald #endif 69*a602f23dSMatthias Ringwald }; 70*a602f23dSMatthias Ringwald 71*a602f23dSMatthias Ringwald /* API_START */ 72*a602f23dSMatthias Ringwald 73*a602f23dSMatthias Ringwald typedef struct hfp_codec hfp_codec_t; 74*a602f23dSMatthias Ringwald 75*a602f23dSMatthias Ringwald /** 76*a602f23dSMatthias Ringwald * @brief Initialize HFP Audio Codec 77*a602f23dSMatthias Ringwald * @note btstack_assert if codec_id is not supported 78*a602f23dSMatthias Ringwald * @param hfp_codec 79*a602f23dSMatthias Ringwald * @param codec_id see HFP_CODEC_xxx in hfp.h 80*a602f23dSMatthias Ringwald * @return status 81*a602f23dSMatthias Ringwald */ 82*a602f23dSMatthias Ringwald void hfp_codec_init(hfp_codec_t * hfp_codec, uint8_t codec_id); 83*a602f23dSMatthias Ringwald 84*a602f23dSMatthias Ringwald /** 85*a602f23dSMatthias Ringwald * @brief Get number of audio samples per HFP SCO frame 86*a602f23dSMatthias Ringwald * @param hfp_codec 87*a602f23dSMatthias Ringwald * @return num audio samples per 7.5ms SCO packet 88*a602f23dSMatthias Ringwald */ 89*a602f23dSMatthias Ringwald uint16_t hfp_codec_num_audio_samples_per_frame(const hfp_codec_t * hfp_codec); 90*a602f23dSMatthias Ringwald 91*a602f23dSMatthias Ringwald /** 92*a602f23dSMatthias Ringwald * @brief Checks if next frame can be encoded 93*a602f23dSMatthias Ringwald * @param hfp_codec 94*a602f23dSMatthias Ringwald */ 95*a602f23dSMatthias Ringwald bool hfp_codec_can_encode_audio_frame_now(const hfp_codec_t * hfp_codec); 96*a602f23dSMatthias Ringwald 97*a602f23dSMatthias Ringwald /** 98*a602f23dSMatthias Ringwald * Get number of bytes ready for sending 99*a602f23dSMatthias Ringwald * @param hfp_codec 100*a602f23dSMatthias Ringwald * @return num bytes ready for current packet 101*a602f23dSMatthias Ringwald */ 102*a602f23dSMatthias Ringwald uint16_t hfp_codec_num_bytes_available(const hfp_codec_t * hfp_codec); 103*a602f23dSMatthias Ringwald 104*a602f23dSMatthias Ringwald /** 105*a602f23dSMatthias Ringwald * Encode audio samples for HFP SCO packet 106*a602f23dSMatthias Ringwald * @param hfp_codec 107*a602f23dSMatthias Ringwald * @param pcm_samples - complete audio frame of hfp_msbc_num_audio_samples_per_frame int16 samples 108*a602f23dSMatthias Ringwald */ 109*a602f23dSMatthias Ringwald void hfp_codec_encode_audio_frame(hfp_codec_t * hfp_codec, int16_t * pcm_samples); 110*a602f23dSMatthias Ringwald 111*a602f23dSMatthias Ringwald /** 112*a602f23dSMatthias Ringwald * Read from stream into SCO packet buffer 113*a602f23dSMatthias Ringwald * @param hfp_codec 114*a602f23dSMatthias Ringwald * @param buffer to store stream 115*a602f23dSMatthias Ringwald * @param size num bytes to read from stream 116*a602f23dSMatthias Ringwald */ 117*a602f23dSMatthias Ringwald void hfp_codec_read_from_stream(hfp_codec_t * hfp_codec, uint8_t * buffer, uint16_t size); 118*a602f23dSMatthias Ringwald 119*a602f23dSMatthias Ringwald /** 120*a602f23dSMatthias Ringwald * @param hfp_codec 121*a602f23dSMatthias Ringwald */ 122*a602f23dSMatthias Ringwald void hfp_codec_deinit(hfp_codec_t * hfp_codec); 123*a602f23dSMatthias Ringwald 124*a602f23dSMatthias Ringwald /* API_END */ 125*a602f23dSMatthias Ringwald 126*a602f23dSMatthias Ringwald #if defined __cplusplus 127*a602f23dSMatthias Ringwald } 128*a602f23dSMatthias Ringwald #endif 129*a602f23dSMatthias Ringwald 130*a602f23dSMatthias Ringwald #endif 131