xref: /btstack/platform/embedded/btstack_audio_embedded.c (revision fb90eb3f628b1084ce21be31e29bf20b65cd1a97)
1 /*
2  * Copyright (C) 2018 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 #define __BTSTACK_FILE__ "btstack_audio_embedded.c"
39 
40 /*
41  *  btstack_audio_embedded.c
42  *
43  *  Implementation of btstack_audio.h using hal_audio.h for embedded run loop
44  *
45  */
46 
47 #include "btstack_config.h"
48 #include "btstack_debug.h"
49 #include "btstack_audio.h"
50 #include "btstack_run_loop_embedded.h"
51 #include "hal_audio.h"
52 
53 // allow to compile all files in embedded folder even if there's no audio support
54 #ifdef HAVE_HAL_AUDIO
55 
56 #define DRIVER_POLL_INTERVAL_MS          5
57 
58 // client
59 static void (*playback_callback)(int16_t * buffer, uint16_t num_samples);
60 static void (*recording_callback)(const int16_t * buffer, uint16_t num_samples);
61 
62 // timer to fill output ring buffer
63 static btstack_timer_source_t  driver_timer_sink;
64 static btstack_timer_source_t  driver_timer_source;
65 
66 static volatile unsigned int output_buffer_to_play;
67 static          unsigned int output_buffer_to_fill;
68 static          unsigned int output_buffer_count;
69 static          unsigned int output_buffer_samples;
70 
71 static volatile int       input_buffer_ready;
72 static volatile const int16_t * input_buffer_samples;
73 static volatile uint16_t  input_buffer_num_samples;
74 
75 static void btstack_audio_audio_played(uint8_t buffer_index){
76     output_buffer_to_play = (buffer_index + 1) % output_buffer_count;
77 }
78 
79 static void btstack_audio_audio_recorded(const int16_t * samples, uint16_t num_samples){
80     input_buffer_samples     = samples;
81     input_buffer_num_samples = num_samples;
82     input_buffer_ready       = 1;
83 }
84 
85 static void driver_timer_handler_sink(btstack_timer_source_t * ts){
86 
87     // playback buffer ready to fill
88     if (output_buffer_to_play != output_buffer_to_fill){
89         int16_t * buffer = hal_audio_sink_get_output_buffer(output_buffer_to_fill);
90         (*playback_callback)(buffer, output_buffer_samples);
91 
92         // next
93         output_buffer_to_fill = (output_buffer_to_fill + 1 ) % output_buffer_count;
94     }
95 
96     // re-set timer
97     btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS);
98     btstack_run_loop_add_timer(ts);
99 }
100 
101 static void driver_timer_handler_source(btstack_timer_source_t * ts){
102     // deliver samples if ready
103     if (input_buffer_ready){
104         (*recording_callback)((const int16_t *)input_buffer_samples, input_buffer_num_samples);
105         input_buffer_ready = 0;
106     }
107 
108     // re-set timer
109     btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS);
110     btstack_run_loop_add_timer(ts);
111 }
112 
113 static int btstack_audio_embedded_sink_init(
114     uint8_t channels,
115     uint32_t samplerate,
116     void (*playback)(int16_t * buffer, uint16_t num_samples)
117 ){
118     if (!playback){
119         log_error("No playback callback");
120         return 1;
121     }
122 
123     playback_callback  = playback;
124 
125     hal_audio_sink_init(channels, samplerate, &btstack_audio_audio_played);
126 
127     return 0;
128 }
129 
130 static int btstack_audio_embedded_source_init(
131     uint8_t channels,
132     uint32_t samplerate,
133     void (*recording)(const int16_t * buffer, uint16_t num_samples)
134 ){
135     if (!recording){
136         log_error("No recording callback");
137         return 1;
138     }
139 
140     recording_callback  = recording;
141 
142     hal_audio_source_init(channels, samplerate, &btstack_audio_audio_recorded);
143 
144     return 0;
145 }
146 
147 static void btstack_audio_embedded_sink_start_stream(void){
148     output_buffer_count   = hal_audio_sink_get_num_output_buffers();
149     output_buffer_samples = hal_audio_sink_get_num_output_buffer_samples();
150 
151     // pre-fill HAL buffers
152     uint16_t i;
153     for (i=0;i<output_buffer_count;i++){
154         int16_t * buffer = hal_audio_sink_get_output_buffer(i);
155         (*playback_callback)(buffer, output_buffer_samples);
156     }
157 
158     output_buffer_to_play = 0;
159     output_buffer_to_fill = 0;
160 
161     // start playback
162     hal_audio_sink_start();
163 
164     // start timer
165     btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink);
166     btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS);
167     btstack_run_loop_add_timer(&driver_timer_sink);
168 }
169 
170 static void btstack_audio_embedded_source_start_stream(void){
171     // just started, no data ready
172     input_buffer_ready = 0;
173 
174     // start recording
175     hal_audio_source_start();
176 
177     // start timer
178     btstack_run_loop_set_timer_handler(&driver_timer_source, &driver_timer_handler_source);
179     btstack_run_loop_set_timer(&driver_timer_source, DRIVER_POLL_INTERVAL_MS);
180     btstack_run_loop_add_timer(&driver_timer_source);
181 }
182 
183 static void btstack_audio_embedded_sink_close(void){
184     // stop timer
185     btstack_run_loop_remove_timer(&driver_timer_sink);
186     // close HAL
187     hal_audio_sink_close();
188 }
189 
190 static void btstack_audio_embedded_source_close(void){
191     // stop timer
192     btstack_run_loop_remove_timer(&driver_timer_source);
193     // close HAL
194     hal_audio_source_close();
195 }
196 
197 static const btstack_audio_sink_t btstack_audio_embedded_sink = {
198     /* int (*init)(..);*/                                       &btstack_audio_embedded_sink_init,
199     /* void (*start_stream(void));*/                            &btstack_audio_embedded_sink_start_stream,
200     /* void (*close)(void); */                                  &btstack_audio_embedded_sink_close
201 };
202 
203 static const btstack_audio_source_t btstack_audio_embedded_source = {
204     /* int (*init)(..);*/                                       &btstack_audio_embedded_source_init,
205     /* void (*start_stream(void));*/                            &btstack_audio_embedded_source_start_stream,
206     /* void (*close)(void); */                                  &btstack_audio_embedded_source_close
207 };
208 
209 const btstack_audio_sink_t * btstack_audio_embedded_sink_get_instance(void){
210     return &btstack_audio_embedded_sink;
211 }
212 
213 const btstack_audio_source_t * btstack_audio_embedded_source_get_instance(void){
214     return &btstack_audio_embedded_source;
215 }
216 
217 #endif
218