1dfa32746SMatthias Ringwald /* 2dfa32746SMatthias Ringwald * Copyright (C) 2018 BlueKitchen GmbH 3dfa32746SMatthias Ringwald * 4dfa32746SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5dfa32746SMatthias Ringwald * modification, are permitted provided that the following conditions 6dfa32746SMatthias Ringwald * are met: 7dfa32746SMatthias Ringwald * 8dfa32746SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9dfa32746SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10dfa32746SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11dfa32746SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12dfa32746SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13dfa32746SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14dfa32746SMatthias Ringwald * contributors may be used to endorse or promote products derived 15dfa32746SMatthias Ringwald * from this software without specific prior written permission. 16dfa32746SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17dfa32746SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18dfa32746SMatthias Ringwald * monetary gain. 19dfa32746SMatthias Ringwald * 20dfa32746SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21dfa32746SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22dfa32746SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23dfa32746SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24dfa32746SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25dfa32746SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26dfa32746SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27dfa32746SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28dfa32746SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29dfa32746SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30dfa32746SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31dfa32746SMatthias Ringwald * SUCH DAMAGE. 32dfa32746SMatthias Ringwald * 33dfa32746SMatthias Ringwald * Please inquire about commercial licensing options at 34dfa32746SMatthias Ringwald * [email protected] 35dfa32746SMatthias Ringwald * 36dfa32746SMatthias Ringwald */ 37dfa32746SMatthias Ringwald 38dfa32746SMatthias Ringwald #define __BTSTACK_FILE__ "btstack_audio_embedded.c" 39dfa32746SMatthias Ringwald 40dfa32746SMatthias Ringwald /* 41dfa32746SMatthias Ringwald * btstack_audio_embedded.c 42dfa32746SMatthias Ringwald * 43dfa32746SMatthias Ringwald * Implementation of btstack_audio.h using hal_audio.h for embedded run loop 44dfa32746SMatthias Ringwald * 45dfa32746SMatthias Ringwald */ 46dfa32746SMatthias Ringwald 47dfa32746SMatthias Ringwald #include "btstack_config.h" 48dfa32746SMatthias Ringwald #include "btstack_debug.h" 49dfa32746SMatthias Ringwald #include "btstack_audio.h" 50dfa32746SMatthias Ringwald #include "btstack_run_loop_embedded.h" 51dfa32746SMatthias Ringwald #include "hal_audio.h" 52dfa32746SMatthias Ringwald 53dfa32746SMatthias Ringwald // allow to compile all files in embedded folder even if there's no audio support 54dfa32746SMatthias Ringwald #ifdef HAVE_HAL_AUDIO 55dfa32746SMatthias Ringwald 56dfa32746SMatthias Ringwald #define DRIVER_POLL_INTERVAL_MS 5 57dfa32746SMatthias Ringwald 58dfa32746SMatthias Ringwald // client 59dfa32746SMatthias Ringwald static void (*playback_callback)(int16_t * buffer, uint16_t num_samples); 60fb90eb3fSMatthias Ringwald static void (*recording_callback)(const int16_t * buffer, uint16_t num_samples); 61dfa32746SMatthias Ringwald 62dfa32746SMatthias Ringwald // timer to fill output ring buffer 630402264cSMatthias Ringwald static btstack_timer_source_t driver_timer_sink; 64fb90eb3fSMatthias Ringwald static btstack_timer_source_t driver_timer_source; 65dfa32746SMatthias Ringwald 66dfa32746SMatthias Ringwald static volatile unsigned int output_buffer_to_play; 67dfa32746SMatthias Ringwald static unsigned int output_buffer_to_fill; 68dfa32746SMatthias Ringwald static unsigned int output_buffer_count; 69dfa32746SMatthias Ringwald static unsigned int output_buffer_samples; 70dfa32746SMatthias Ringwald 71fb90eb3fSMatthias Ringwald static volatile int input_buffer_ready; 72fb90eb3fSMatthias Ringwald static volatile const int16_t * input_buffer_samples; 73fb90eb3fSMatthias Ringwald static volatile uint16_t input_buffer_num_samples; 74fb90eb3fSMatthias Ringwald 75*6fd4ca9eSMatthias Ringwald static int source_active; 76*6fd4ca9eSMatthias Ringwald static int sink_active; 77*6fd4ca9eSMatthias Ringwald 78dfa32746SMatthias Ringwald static void btstack_audio_audio_played(uint8_t buffer_index){ 79dfa32746SMatthias Ringwald output_buffer_to_play = (buffer_index + 1) % output_buffer_count; 80dfa32746SMatthias Ringwald } 81dfa32746SMatthias Ringwald 82dfa32746SMatthias Ringwald static void btstack_audio_audio_recorded(const int16_t * samples, uint16_t num_samples){ 83fb90eb3fSMatthias Ringwald input_buffer_samples = samples; 84fb90eb3fSMatthias Ringwald input_buffer_num_samples = num_samples; 85fb90eb3fSMatthias Ringwald input_buffer_ready = 1; 86dfa32746SMatthias Ringwald } 87dfa32746SMatthias Ringwald 880402264cSMatthias Ringwald static void driver_timer_handler_sink(btstack_timer_source_t * ts){ 89dfa32746SMatthias Ringwald 90dfa32746SMatthias Ringwald // playback buffer ready to fill 910402264cSMatthias Ringwald if (output_buffer_to_play != output_buffer_to_fill){ 92ff0081eeSMatthias Ringwald int16_t * buffer = hal_audio_sink_get_output_buffer(output_buffer_to_fill); 93dfa32746SMatthias Ringwald (*playback_callback)(buffer, output_buffer_samples); 94dfa32746SMatthias Ringwald 95dfa32746SMatthias Ringwald // next 96dfa32746SMatthias Ringwald output_buffer_to_fill = (output_buffer_to_fill + 1 ) % output_buffer_count; 97dfa32746SMatthias Ringwald } 98dfa32746SMatthias Ringwald 99dfa32746SMatthias Ringwald // re-set timer 100dfa32746SMatthias Ringwald btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS); 101dfa32746SMatthias Ringwald btstack_run_loop_add_timer(ts); 102dfa32746SMatthias Ringwald } 103dfa32746SMatthias Ringwald 1040402264cSMatthias Ringwald static void driver_timer_handler_source(btstack_timer_source_t * ts){ 105fb90eb3fSMatthias Ringwald // deliver samples if ready 106fb90eb3fSMatthias Ringwald if (input_buffer_ready){ 107fb90eb3fSMatthias Ringwald (*recording_callback)((const int16_t *)input_buffer_samples, input_buffer_num_samples); 108fb90eb3fSMatthias Ringwald input_buffer_ready = 0; 1090402264cSMatthias Ringwald } 110fb90eb3fSMatthias Ringwald 111fb90eb3fSMatthias Ringwald // re-set timer 112fb90eb3fSMatthias Ringwald btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS); 113fb90eb3fSMatthias Ringwald btstack_run_loop_add_timer(ts); 114fb90eb3fSMatthias Ringwald } 1150402264cSMatthias Ringwald 1160402264cSMatthias Ringwald static int btstack_audio_embedded_sink_init( 117dfa32746SMatthias Ringwald uint8_t channels, 118dfa32746SMatthias Ringwald uint32_t samplerate, 1190402264cSMatthias Ringwald void (*playback)(int16_t * buffer, uint16_t num_samples) 120dfa32746SMatthias Ringwald ){ 1210402264cSMatthias Ringwald if (!playback){ 1220402264cSMatthias Ringwald log_error("No playback callback"); 1230402264cSMatthias Ringwald return 1; 1240402264cSMatthias Ringwald } 125dfa32746SMatthias Ringwald 1260402264cSMatthias Ringwald playback_callback = playback; 1270402264cSMatthias Ringwald 128ff0081eeSMatthias Ringwald hal_audio_sink_init(channels, samplerate, &btstack_audio_audio_played); 129dfa32746SMatthias Ringwald 130dfa32746SMatthias Ringwald return 0; 131dfa32746SMatthias Ringwald } 132dfa32746SMatthias Ringwald 1330402264cSMatthias Ringwald static int btstack_audio_embedded_source_init( 1340402264cSMatthias Ringwald uint8_t channels, 1350402264cSMatthias Ringwald uint32_t samplerate, 1360402264cSMatthias Ringwald void (*recording)(const int16_t * buffer, uint16_t num_samples) 1370402264cSMatthias Ringwald ){ 1380402264cSMatthias Ringwald if (!recording){ 1390402264cSMatthias Ringwald log_error("No recording callback"); 1400402264cSMatthias Ringwald return 1; 1410402264cSMatthias Ringwald } 142fb90eb3fSMatthias Ringwald 143fb90eb3fSMatthias Ringwald recording_callback = recording; 144fb90eb3fSMatthias Ringwald 145fb90eb3fSMatthias Ringwald hal_audio_source_init(channels, samplerate, &btstack_audio_audio_recorded); 146fb90eb3fSMatthias Ringwald 1470402264cSMatthias Ringwald return 0; 1480402264cSMatthias Ringwald } 149dfa32746SMatthias Ringwald 1500402264cSMatthias Ringwald static void btstack_audio_embedded_sink_start_stream(void){ 151ff0081eeSMatthias Ringwald output_buffer_count = hal_audio_sink_get_num_output_buffers(); 152ff0081eeSMatthias Ringwald output_buffer_samples = hal_audio_sink_get_num_output_buffer_samples(); 153dfa32746SMatthias Ringwald 154dfa32746SMatthias Ringwald // pre-fill HAL buffers 155dfa32746SMatthias Ringwald uint16_t i; 156dfa32746SMatthias Ringwald for (i=0;i<output_buffer_count;i++){ 157ff0081eeSMatthias Ringwald int16_t * buffer = hal_audio_sink_get_output_buffer(i); 158dfa32746SMatthias Ringwald (*playback_callback)(buffer, output_buffer_samples); 159dfa32746SMatthias Ringwald } 160dfa32746SMatthias Ringwald 161dfa32746SMatthias Ringwald output_buffer_to_play = 0; 162dfa32746SMatthias Ringwald output_buffer_to_fill = 0; 163dfa32746SMatthias Ringwald 164dfa32746SMatthias Ringwald // start playback 165ff0081eeSMatthias Ringwald hal_audio_sink_start(); 166dfa32746SMatthias Ringwald 167dfa32746SMatthias Ringwald // start timer 1680402264cSMatthias Ringwald btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink); 1690402264cSMatthias Ringwald btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS); 1700402264cSMatthias Ringwald btstack_run_loop_add_timer(&driver_timer_sink); 171*6fd4ca9eSMatthias Ringwald 172*6fd4ca9eSMatthias Ringwald // state 173*6fd4ca9eSMatthias Ringwald sink_active = 1; 174dfa32746SMatthias Ringwald } 175dfa32746SMatthias Ringwald 1760402264cSMatthias Ringwald static void btstack_audio_embedded_source_start_stream(void){ 177fb90eb3fSMatthias Ringwald // just started, no data ready 178fb90eb3fSMatthias Ringwald input_buffer_ready = 0; 179fb90eb3fSMatthias Ringwald 180fb90eb3fSMatthias Ringwald // start recording 181fb90eb3fSMatthias Ringwald hal_audio_source_start(); 182fb90eb3fSMatthias Ringwald 183fb90eb3fSMatthias Ringwald // start timer 184fb90eb3fSMatthias Ringwald btstack_run_loop_set_timer_handler(&driver_timer_source, &driver_timer_handler_source); 185fb90eb3fSMatthias Ringwald btstack_run_loop_set_timer(&driver_timer_source, DRIVER_POLL_INTERVAL_MS); 186fb90eb3fSMatthias Ringwald btstack_run_loop_add_timer(&driver_timer_source); 187*6fd4ca9eSMatthias Ringwald 188*6fd4ca9eSMatthias Ringwald // state 189*6fd4ca9eSMatthias Ringwald source_active = 1; 190*6fd4ca9eSMatthias Ringwald } 191*6fd4ca9eSMatthias Ringwald 192*6fd4ca9eSMatthias Ringwald static void btstack_audio_embedded_sink_stop_stream(void){ 193*6fd4ca9eSMatthias Ringwald // stop stream 194*6fd4ca9eSMatthias Ringwald hal_audio_sink_stop(); 195*6fd4ca9eSMatthias Ringwald // stop timer 196*6fd4ca9eSMatthias Ringwald btstack_run_loop_remove_timer(&driver_timer_sink); 197*6fd4ca9eSMatthias Ringwald // state 198*6fd4ca9eSMatthias Ringwald sink_active = 0; 199*6fd4ca9eSMatthias Ringwald } 200*6fd4ca9eSMatthias Ringwald 201*6fd4ca9eSMatthias Ringwald static void btstack_audio_embedded_source_stop_stream(void){ 202*6fd4ca9eSMatthias Ringwald // stop stream 203*6fd4ca9eSMatthias Ringwald hal_audio_source_stop(); 204*6fd4ca9eSMatthias Ringwald // stop timer 205*6fd4ca9eSMatthias Ringwald btstack_run_loop_remove_timer(&driver_timer_source); 206*6fd4ca9eSMatthias Ringwald // state 207*6fd4ca9eSMatthias Ringwald source_active = 0; 2080402264cSMatthias Ringwald } 2090402264cSMatthias Ringwald 2100402264cSMatthias Ringwald static void btstack_audio_embedded_sink_close(void){ 211*6fd4ca9eSMatthias Ringwald // stop stream if needed 212*6fd4ca9eSMatthias Ringwald if (sink_active){ 213*6fd4ca9eSMatthias Ringwald btstack_audio_embedded_sink_stop_stream(); 214*6fd4ca9eSMatthias Ringwald } 215dfa32746SMatthias Ringwald // close HAL 216ff0081eeSMatthias Ringwald hal_audio_sink_close(); 217dfa32746SMatthias Ringwald } 218dfa32746SMatthias Ringwald 2190402264cSMatthias Ringwald static void btstack_audio_embedded_source_close(void){ 220*6fd4ca9eSMatthias Ringwald // stop stream if needed 221*6fd4ca9eSMatthias Ringwald if (source_active){ 222*6fd4ca9eSMatthias Ringwald btstack_audio_embedded_source_stop_stream(); 223*6fd4ca9eSMatthias Ringwald } 224fb90eb3fSMatthias Ringwald // close HAL 225fb90eb3fSMatthias Ringwald hal_audio_source_close(); 2260402264cSMatthias Ringwald } 2270402264cSMatthias Ringwald 2280402264cSMatthias Ringwald static const btstack_audio_sink_t btstack_audio_embedded_sink = { 2290402264cSMatthias Ringwald /* int (*init)(..);*/ &btstack_audio_embedded_sink_init, 2300402264cSMatthias Ringwald /* void (*start_stream(void));*/ &btstack_audio_embedded_sink_start_stream, 231*6fd4ca9eSMatthias Ringwald /* void (*stop_stream)(void) */ &btstack_audio_embedded_sink_stop_stream, 2320402264cSMatthias Ringwald /* void (*close)(void); */ &btstack_audio_embedded_sink_close 233dfa32746SMatthias Ringwald }; 234dfa32746SMatthias Ringwald 2350402264cSMatthias Ringwald static const btstack_audio_source_t btstack_audio_embedded_source = { 2360402264cSMatthias Ringwald /* int (*init)(..);*/ &btstack_audio_embedded_source_init, 2370402264cSMatthias Ringwald /* void (*start_stream(void));*/ &btstack_audio_embedded_source_start_stream, 238*6fd4ca9eSMatthias Ringwald /* void (*stop_stream)(void) */ &btstack_audio_embedded_source_stop_stream, 2390402264cSMatthias Ringwald /* void (*close)(void); */ &btstack_audio_embedded_source_close 2400402264cSMatthias Ringwald }; 2410402264cSMatthias Ringwald 2420402264cSMatthias Ringwald const btstack_audio_sink_t * btstack_audio_embedded_sink_get_instance(void){ 2430402264cSMatthias Ringwald return &btstack_audio_embedded_sink; 2440402264cSMatthias Ringwald } 2450402264cSMatthias Ringwald 2460402264cSMatthias Ringwald const btstack_audio_source_t * btstack_audio_embedded_source_get_instance(void){ 2470402264cSMatthias Ringwald return &btstack_audio_embedded_source; 248dfa32746SMatthias Ringwald } 249dfa32746SMatthias Ringwald 250dfa32746SMatthias Ringwald #endif 251