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 void btstack_audio_audio_played(uint8_t buffer_index){ 72 output_buffer_to_play = (buffer_index + 1) % output_buffer_count; 73 } 74 75 #if 0 76 static void btstack_audio_audio_recorded(const int16_t * samples, uint16_t num_samples){ 77 UNUSED(samples); 78 UNUSED(num_samples); 79 } 80 #endif 81 82 static void driver_timer_handler_sink(btstack_timer_source_t * ts){ 83 84 // playback buffer ready to fill 85 if (output_buffer_to_play != output_buffer_to_fill){ 86 // fill buffer 87 int16_t * buffer = hal_audio_get_output_buffer(output_buffer_to_fill); 88 (*playback_callback)(buffer, output_buffer_samples); 89 90 // next 91 output_buffer_to_fill = (output_buffer_to_fill + 1 ) % output_buffer_count; 92 } 93 94 // re-set timer 95 btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS); 96 btstack_run_loop_add_timer(ts); 97 } 98 99 #if 0 100 static void driver_timer_handler_source(btstack_timer_source_t * ts){ 101 // TODO 102 } 103 #endif 104 105 static int btstack_audio_embedded_sink_init( 106 uint8_t channels, 107 uint32_t samplerate, 108 void (*playback)(int16_t * buffer, uint16_t num_samples) 109 ){ 110 if (!playback){ 111 log_error("No playback callback"); 112 return 1; 113 } 114 115 playback_callback = playback; 116 117 // TODO: split HAL Audio API as well 118 hal_audio_init(channels, samplerate, &btstack_audio_audio_played, NULL); 119 120 return 0; 121 } 122 123 static int btstack_audio_embedded_source_init( 124 uint8_t channels, 125 uint32_t samplerate, 126 void (*recording)(const int16_t * buffer, uint16_t num_samples) 127 ){ 128 if (!recording){ 129 log_error("No recording callback"); 130 return 1; 131 } 132 return 0; 133 } 134 135 static void btstack_audio_embedded_sink_start_stream(void){ 136 137 output_buffer_count = hal_audio_get_num_output_buffers(); 138 output_buffer_samples = hal_audio_get_num_output_buffer_samples(); 139 140 // pre-fill HAL buffers 141 uint16_t i; 142 for (i=0;i<output_buffer_count;i++){ 143 int16_t * buffer = hal_audio_get_output_buffer(i); 144 (*playback_callback)(buffer, output_buffer_samples); 145 } 146 147 output_buffer_to_play = 0; 148 output_buffer_to_fill = 0; 149 150 // start playback 151 hal_audio_start(); 152 153 // start timer 154 btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink); 155 btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS); 156 btstack_run_loop_add_timer(&driver_timer_sink); 157 } 158 159 static void btstack_audio_embedded_source_start_stream(void){ 160 // TODO 161 } 162 163 static void btstack_audio_embedded_sink_close(void){ 164 // stop timer 165 btstack_run_loop_remove_timer(&driver_timer_sink); 166 // close HAL 167 hal_audio_close(); 168 } 169 170 static void btstack_audio_embedded_source_close(void){ 171 // TODO 172 } 173 174 static const btstack_audio_sink_t btstack_audio_embedded_sink = { 175 /* int (*init)(..);*/ &btstack_audio_embedded_sink_init, 176 /* void (*start_stream(void));*/ &btstack_audio_embedded_sink_start_stream, 177 /* void (*close)(void); */ &btstack_audio_embedded_sink_close 178 }; 179 180 static const btstack_audio_source_t btstack_audio_embedded_source = { 181 /* int (*init)(..);*/ &btstack_audio_embedded_source_init, 182 /* void (*start_stream(void));*/ &btstack_audio_embedded_source_start_stream, 183 /* void (*close)(void); */ &btstack_audio_embedded_source_close 184 }; 185 186 const btstack_audio_sink_t * btstack_audio_embedded_sink_get_instance(void){ 187 return &btstack_audio_embedded_sink; 188 } 189 190 const btstack_audio_source_t * btstack_audio_embedded_source_get_instance(void){ 191 return &btstack_audio_embedded_source; 192 } 193 194 #endif 195