xref: /btstack/platform/embedded/hal_audio.h (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1fa6e8062SMatthias Ringwald /*
2fa6e8062SMatthias Ringwald  * Copyright (C) 2018 BlueKitchen GmbH
3fa6e8062SMatthias Ringwald  *
4fa6e8062SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5fa6e8062SMatthias Ringwald  * modification, are permitted provided that the following conditions
6fa6e8062SMatthias Ringwald  * are met:
7fa6e8062SMatthias Ringwald  *
8fa6e8062SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9fa6e8062SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10fa6e8062SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11fa6e8062SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12fa6e8062SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13fa6e8062SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14fa6e8062SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15fa6e8062SMatthias Ringwald  *    from this software without specific prior written permission.
16fa6e8062SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17fa6e8062SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18fa6e8062SMatthias Ringwald  *    monetary gain.
19fa6e8062SMatthias Ringwald  *
20fa6e8062SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21fa6e8062SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22fa6e8062SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25fa6e8062SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26fa6e8062SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27fa6e8062SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28fa6e8062SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29fa6e8062SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30fa6e8062SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31fa6e8062SMatthias Ringwald  * SUCH DAMAGE.
32fa6e8062SMatthias Ringwald  *
33fa6e8062SMatthias Ringwald  * Please inquire about commercial licensing options at
34fa6e8062SMatthias Ringwald  * [email protected]
35fa6e8062SMatthias Ringwald  *
36fa6e8062SMatthias Ringwald  */
37fa6e8062SMatthias Ringwald 
3880e33422SMatthias Ringwald #ifndef HAL_AUDIO_H
3980e33422SMatthias Ringwald #define HAL_AUDIO_H
40fa6e8062SMatthias Ringwald 
41fa6e8062SMatthias Ringwald #include <stdint.h>
42fa6e8062SMatthias Ringwald 
43ea7d62f8SMatthias Ringwald #if defined __cplusplus
44ea7d62f8SMatthias Ringwald extern "C" {
45ea7d62f8SMatthias Ringwald #endif
46ea7d62f8SMatthias Ringwald 
47fa6e8062SMatthias Ringwald /*
48fa6e8062SMatthias Ringwald  *  hal_audio.h
49fa6e8062SMatthias Ringwald  *
50fa6e8062SMatthias Ringwald  *  Hardware abstraction layer that provides circular audio playback and recording
51fa6e8062SMatthias Ringwald  *
52fa6e8062SMatthias Ringwald  *  Assumptions:
53fa6e8062SMatthias Ringwald  *  - num buffers n >= 2
54fa6e8062SMatthias Ringwald  *  - after start, buffers are played in sequence 0, 1, ... , n-1, 0, 1, .. , n-1, ...
55fa6e8062SMatthias Ringwald  *  - after a buffer is played, the callback is called with tbe index of the played buffer
56fa6e8062SMatthias Ringwald  */
57fa6e8062SMatthias Ringwald 
58fa6e8062SMatthias Ringwald /**
59ff0081eeSMatthias Ringwald  * @brief Setup audio codec for playback using specified samplerate and number of channels
60fa6e8062SMatthias Ringwald  * @param Channels
61fa6e8062SMatthias Ringwald  * @param Sample rate
62fa6e8062SMatthias Ringwald  * @param Buffer played callback
63fa6e8062SMatthias Ringwald  */
64ff0081eeSMatthias Ringwald void hal_audio_sink_init(uint8_t channels,
65fa6e8062SMatthias Ringwald                          uint32_t sample_rate,
66ff0081eeSMatthias Ringwald                          void (*buffer_played_callback)(uint8_t buffer_index));
67fa6e8062SMatthias Ringwald 
68fa6e8062SMatthias Ringwald /**
69fa6e8062SMatthias Ringwald  * @brief Get number of output buffers in HAL
70fa6e8062SMatthias Ringwald  * @returns num buffers
71fa6e8062SMatthias Ringwald  */
72ff0081eeSMatthias Ringwald uint16_t hal_audio_sink_get_num_output_buffers(void);
73fa6e8062SMatthias Ringwald 
74fa6e8062SMatthias Ringwald /**
75fa6e8062SMatthias Ringwald  * @brief Get size of single output buffer in HAL
76fa6e8062SMatthias Ringwald  * @returns buffer size
77fa6e8062SMatthias Ringwald  */
78ff0081eeSMatthias Ringwald uint16_t hal_audio_sink_get_num_output_buffer_samples(void);
79fa6e8062SMatthias Ringwald 
80fa6e8062SMatthias Ringwald /**
81ff0081eeSMatthias Ringwald  * @brief Get output buffer
82fa6e8062SMatthias Ringwald  * @param buffer index
83fa6e8062SMatthias Ringwald  * @returns buffer
84fa6e8062SMatthias Ringwald  */
85ff0081eeSMatthias Ringwald int16_t * hal_audio_sink_get_output_buffer(uint8_t buffer_index);
86fa6e8062SMatthias Ringwald 
87fa6e8062SMatthias Ringwald /**
88fa6e8062SMatthias Ringwald  * @brief Start stream
89fa6e8062SMatthias Ringwald  */
90ff0081eeSMatthias Ringwald void hal_audio_sink_start(void);
91ff0081eeSMatthias Ringwald 
92ff0081eeSMatthias Ringwald /**
93ff0081eeSMatthias Ringwald  * @brief Stop stream
94ff0081eeSMatthias Ringwald  */
95ff0081eeSMatthias Ringwald void hal_audio_sink_stop(void);
96fa6e8062SMatthias Ringwald 
97fa6e8062SMatthias Ringwald /**
98fa6e8062SMatthias Ringwald  * @brief Close audio codec
99fa6e8062SMatthias Ringwald  */
100ff0081eeSMatthias Ringwald void hal_audio_sink_close(void);
101ff0081eeSMatthias Ringwald 
102ff0081eeSMatthias Ringwald 
103ff0081eeSMatthias Ringwald /**
104ff0081eeSMatthias Ringwald  * @brief Setup audio codec for recording using specified samplerate and number of channels
105ff0081eeSMatthias Ringwald  * @param Channels
106ff0081eeSMatthias Ringwald  * @param Sample rate
107ff0081eeSMatthias Ringwald  * @param Buffer recorded callback
108ff0081eeSMatthias Ringwald  */
109ff0081eeSMatthias Ringwald void hal_audio_source_init(uint8_t channels,
110ff0081eeSMatthias Ringwald                            uint32_t sample_rate,
111ff0081eeSMatthias Ringwald                            void (*buffer_recorded_callback)(const int16_t * buffer, uint16_t num_samples));
112ff0081eeSMatthias Ringwald 
113ff0081eeSMatthias Ringwald /**
114ff0081eeSMatthias Ringwald  * @brief Start stream
115ff0081eeSMatthias Ringwald  */
116ff0081eeSMatthias Ringwald void hal_audio_source_start(void);
117ff0081eeSMatthias Ringwald 
118ff0081eeSMatthias Ringwald /**
119ff0081eeSMatthias Ringwald  * @brief Stop stream
120ff0081eeSMatthias Ringwald  */
121ff0081eeSMatthias Ringwald void hal_audio_source_stop(void);
122ff0081eeSMatthias Ringwald 
123ff0081eeSMatthias Ringwald /**
124ff0081eeSMatthias Ringwald  * @brief Close audio codec
125ff0081eeSMatthias Ringwald  */
126ff0081eeSMatthias Ringwald void hal_audio_source_close(void);
127fa6e8062SMatthias Ringwald 
128ea7d62f8SMatthias Ringwald #if defined __cplusplus
129ea7d62f8SMatthias Ringwald }
130ea7d62f8SMatthias Ringwald #endif
131ea7d62f8SMatthias Ringwald 
132fa6e8062SMatthias Ringwald #endif
133