xref: /btstack/src/classic/hfp_codec.h (revision 94381a697821095e6e0bfd686d9c2a71486b6ac0)
1 /*
2  * Copyright (C) 2023 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 BLUEKITCHEN
24  * GMBH 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  * @title HFP Audio Encoder
40  * @brief Create SCO packet with H2 Synchronization Header and encoded audio samples
41  */
42 
43 #ifndef HFP_CODEC_H
44 #define HFP_CODEC_H
45 
46 #include "btstack_config.h"
47 #include "hfp.h"    // hfp_h2_framing
48 
49 #include <stdint.h>
50 
51 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
52 #include "btstack_sbc.h"
53 #endif
54 
55 #if defined __cplusplus
56 extern "C" {
57 #endif
58 
59 struct hfp_codec {
60     uint8_t sco_packet[60];
61     uint16_t read_pos;
62     uint16_t write_pos;
63     uint16_t samples_per_frame;
64     hfp_h2_framing_t h2_framing;
65     void (*encode)(struct hfp_codec * hfp_codec, int16_t * pcm_samples);
66 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
67     btstack_sbc_encoder_state_t msbc_state;
68 #endif
69 };
70 
71 /* API_START */
72 
73 typedef struct hfp_codec hfp_codec_t;
74 
75 /**
76  * @brief Initialize HFP Audio Codec
77  * @note  btstack_assert if codec_id is not supported
78  * @param hfp_codec
79  * @param codec_id see HFP_CODEC_xxx in hfp.h
80  * @return status
81  */
82 void hfp_codec_init(hfp_codec_t * hfp_codec, uint8_t codec_id);
83 
84 /**
85  * @brief Get number of audio samples per HFP SCO frame
86  * @param hfp_codec
87  * @return num audio samples per 7.5ms SCO packet
88  */
89 uint16_t hfp_codec_num_audio_samples_per_frame(const hfp_codec_t * hfp_codec);
90 
91 /**
92  * @brief Checks if next frame can be encoded
93  * @param hfp_codec
94  */
95 bool hfp_codec_can_encode_audio_frame_now(const hfp_codec_t * hfp_codec);
96 
97 /**
98  * Get number of bytes ready for sending
99  * @param hfp_codec
100  * @return num bytes ready for current packet
101  */
102 uint16_t hfp_codec_num_bytes_available(const hfp_codec_t * hfp_codec);
103 
104 /**
105  * Encode audio samples for HFP SCO packet
106  * @param hfp_codec
107  * @param pcm_samples - complete audio frame of hfp_msbc_num_audio_samples_per_frame int16 samples
108  */
109 void hfp_codec_encode_audio_frame(hfp_codec_t * hfp_codec, int16_t * pcm_samples);
110 
111 /**
112  * Read from stream into SCO packet buffer
113  * @param hfp_codec
114  * @param buffer to store stream
115  * @param size num bytes to read from stream
116  */
117 void hfp_codec_read_from_stream(hfp_codec_t * hfp_codec, uint8_t * buffer, uint16_t size);
118 
119 /**
120  * @param hfp_codec
121  */
122 void hfp_codec_deinit(hfp_codec_t * hfp_codec);
123 
124 /* API_END */
125 
126 #if defined __cplusplus
127 }
128 #endif
129 
130 #endif
131