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