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 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 #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 49 // allow to compile all files in embedded folder even if there's no audio support 50 #ifdef HAVE_HAL_AUDIO 51 52 #include "btstack_debug.h" 53 #include "btstack_audio.h" 54 #include "btstack_run_loop_embedded.h" 55 #include "hal_audio.h" 56 57 #include <stddef.h> 58 59 #define DRIVER_POLL_INTERVAL_MS 5 60 61 // client 62 static void (*playback_callback)(int16_t * buffer, uint16_t num_samples); 63 static void (*recording_callback)(const int16_t * buffer, uint16_t num_samples); 64 65 // timer to fill output ring buffer 66 static btstack_timer_source_t driver_timer_sink; 67 static btstack_timer_source_t driver_timer_source; 68 69 static volatile unsigned int output_buffer_to_play; 70 static unsigned int output_buffer_to_fill; 71 static unsigned int output_buffer_count; 72 static unsigned int output_buffer_samples; 73 74 static volatile int input_buffer_ready; 75 static volatile const int16_t * input_buffer_samples; 76 static volatile uint16_t input_buffer_num_samples; 77 78 static int source_active; 79 static int sink_active; 80 81 // Support for stereo-only audio hal 82 #ifdef HAVE_HAL_AUDIO_SINK_STEREO_ONLY 83 static bool output_duplicate_samples; 84 #endif 85 86 static void btstack_audio_audio_played(uint8_t buffer_index){ 87 output_buffer_to_play = (buffer_index + 1) % output_buffer_count; 88 } 89 90 static void btstack_audio_audio_recorded(const int16_t * samples, uint16_t num_samples){ 91 input_buffer_samples = samples; 92 input_buffer_num_samples = num_samples; 93 input_buffer_ready = 1; 94 } 95 96 static void driver_timer_handler_sink(btstack_timer_source_t * ts){ 97 98 // playback buffer ready to fill 99 if (output_buffer_to_play != output_buffer_to_fill){ 100 int16_t * buffer = hal_audio_sink_get_output_buffer(output_buffer_to_fill); 101 (*playback_callback)(buffer, output_buffer_samples); 102 103 #ifdef HAVE_HAL_AUDIO_SINK_STEREO_ONLY 104 if (output_duplicate_samples){ 105 unsigned int i = output_buffer_samples; 106 do { 107 i--; 108 int16_t sample = buffer[i]; 109 buffer[2*i + 0] = sample; 110 buffer[2*i + 1] = sample; 111 } while ( i > 0 ); 112 } 113 #endif 114 115 // next 116 output_buffer_to_fill = (output_buffer_to_fill + 1 ) % output_buffer_count; 117 } 118 119 // re-set timer 120 btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS); 121 btstack_run_loop_add_timer(ts); 122 } 123 124 static void driver_timer_handler_source(btstack_timer_source_t * ts){ 125 // deliver samples if ready 126 if (input_buffer_ready){ 127 (*recording_callback)((const int16_t *)input_buffer_samples, input_buffer_num_samples); 128 input_buffer_ready = 0; 129 } 130 131 // re-set timer 132 btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS); 133 btstack_run_loop_add_timer(ts); 134 } 135 136 static int btstack_audio_embedded_sink_init( 137 uint8_t channels, 138 uint32_t samplerate, 139 void (*playback)(int16_t * buffer, uint16_t num_samples) 140 ){ 141 btstack_assert(playback != NULL); 142 btstack_assert(channels != 0); 143 144 #ifdef HAVE_HAL_AUDIO_SINK_STEREO_ONLY 145 // always use stereo from hal, duplicate samples if needed 146 output_duplicate_samples = channels != 2; 147 channels = 2; 148 #endif 149 150 playback_callback = playback; 151 152 hal_audio_sink_init(channels, samplerate, &btstack_audio_audio_played); 153 154 return 0; 155 } 156 157 static int btstack_audio_embedded_source_init( 158 uint8_t channels, 159 uint32_t samplerate, 160 void (*recording)(const int16_t * buffer, uint16_t num_samples) 161 ){ 162 if (!recording){ 163 log_error("No recording callback"); 164 return 1; 165 } 166 167 recording_callback = recording; 168 169 hal_audio_source_init(channels, samplerate, &btstack_audio_audio_recorded); 170 171 return 0; 172 } 173 174 static void btstack_audio_embedded_sink_set_volume(uint8_t volume){ 175 UNUSED(volume); 176 } 177 178 static void btstack_audio_embedded_source_set_gain(uint8_t gain){ 179 UNUSED(gain); 180 } 181 182 static void btstack_audio_embedded_sink_start_stream(void){ 183 output_buffer_count = hal_audio_sink_get_num_output_buffers(); 184 output_buffer_samples = hal_audio_sink_get_num_output_buffer_samples(); 185 186 btstack_assert(output_buffer_samples > 0); 187 188 // pre-fill HAL buffers 189 uint16_t i; 190 for (i=0;i<output_buffer_count;i++){ 191 int16_t * buffer = hal_audio_sink_get_output_buffer(i); 192 (*playback_callback)(buffer, output_buffer_samples); 193 } 194 195 output_buffer_to_play = 0; 196 output_buffer_to_fill = 0; 197 198 // start playback 199 hal_audio_sink_start(); 200 201 // start timer 202 btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink); 203 btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS); 204 btstack_run_loop_add_timer(&driver_timer_sink); 205 206 // state 207 sink_active = 1; 208 } 209 210 static void btstack_audio_embedded_source_start_stream(void){ 211 // just started, no data ready 212 input_buffer_ready = 0; 213 214 // start recording 215 hal_audio_source_start(); 216 217 // start timer 218 btstack_run_loop_set_timer_handler(&driver_timer_source, &driver_timer_handler_source); 219 btstack_run_loop_set_timer(&driver_timer_source, DRIVER_POLL_INTERVAL_MS); 220 btstack_run_loop_add_timer(&driver_timer_source); 221 222 // state 223 source_active = 1; 224 } 225 226 static void btstack_audio_embedded_sink_stop_stream(void){ 227 // stop stream 228 hal_audio_sink_stop(); 229 // stop timer 230 btstack_run_loop_remove_timer(&driver_timer_sink); 231 // state 232 sink_active = 0; 233 } 234 235 static void btstack_audio_embedded_source_stop_stream(void){ 236 // stop stream 237 hal_audio_source_stop(); 238 // stop timer 239 btstack_run_loop_remove_timer(&driver_timer_source); 240 // state 241 source_active = 0; 242 } 243 244 static void btstack_audio_embedded_sink_close(void){ 245 // stop stream if needed 246 if (sink_active){ 247 btstack_audio_embedded_sink_stop_stream(); 248 } 249 // close HAL 250 hal_audio_sink_close(); 251 } 252 253 static void btstack_audio_embedded_source_close(void){ 254 // stop stream if needed 255 if (source_active){ 256 btstack_audio_embedded_source_stop_stream(); 257 } 258 // close HAL 259 hal_audio_source_close(); 260 } 261 262 static const btstack_audio_sink_t btstack_audio_embedded_sink = { 263 /* int (*init)(..);*/ &btstack_audio_embedded_sink_init, 264 /* void (*set_volume)(uint8_t volume); */ &btstack_audio_embedded_sink_set_volume, 265 /* void (*start_stream(void));*/ &btstack_audio_embedded_sink_start_stream, 266 /* void (*stop_stream)(void) */ &btstack_audio_embedded_sink_stop_stream, 267 /* void (*close)(void); */ &btstack_audio_embedded_sink_close 268 }; 269 270 static const btstack_audio_source_t btstack_audio_embedded_source = { 271 /* int (*init)(..);*/ &btstack_audio_embedded_source_init, 272 /* void (*set_gain)(uint8_t gain); */ &btstack_audio_embedded_source_set_gain, 273 /* void (*start_stream(void));*/ &btstack_audio_embedded_source_start_stream, 274 /* void (*stop_stream)(void) */ &btstack_audio_embedded_source_stop_stream, 275 /* void (*close)(void); */ &btstack_audio_embedded_source_close 276 }; 277 278 const btstack_audio_sink_t * btstack_audio_embedded_sink_get_instance(void){ 279 return &btstack_audio_embedded_sink; 280 } 281 282 const btstack_audio_source_t * btstack_audio_embedded_source_get_instance(void){ 283 return &btstack_audio_embedded_source; 284 } 285 286 #endif 287