19eddf5e8SMatthias Ringwald /* 29eddf5e8SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 39eddf5e8SMatthias Ringwald * 49eddf5e8SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 59eddf5e8SMatthias Ringwald * modification, are permitted provided that the following conditions 69eddf5e8SMatthias Ringwald * are met: 79eddf5e8SMatthias Ringwald * 89eddf5e8SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 99eddf5e8SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 109eddf5e8SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 119eddf5e8SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 129eddf5e8SMatthias Ringwald * documentation and/or other materials provided with the distribution. 139eddf5e8SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 149eddf5e8SMatthias Ringwald * contributors may be used to endorse or promote products derived 159eddf5e8SMatthias Ringwald * from this software without specific prior written permission. 169eddf5e8SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 179eddf5e8SMatthias Ringwald * personal benefit and not for any commercial purpose or for 189eddf5e8SMatthias Ringwald * monetary gain. 199eddf5e8SMatthias Ringwald * 209eddf5e8SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 219eddf5e8SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 229eddf5e8SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 259eddf5e8SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 269eddf5e8SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 279eddf5e8SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 289eddf5e8SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 299eddf5e8SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 309eddf5e8SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319eddf5e8SMatthias Ringwald * SUCH DAMAGE. 329eddf5e8SMatthias Ringwald * 339eddf5e8SMatthias Ringwald * Please inquire about commercial licensing options at 349eddf5e8SMatthias Ringwald * [email protected] 359eddf5e8SMatthias Ringwald * 369eddf5e8SMatthias Ringwald */ 379eddf5e8SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "audio_duplex.c" 39bb2a7656SMatthias Ringwald 40ec8ae085SMilanka Ringwald /* EXAMPLE_START(audio_duplex): Audio Driver - Forward Audio from Source to Sink 419eddf5e8SMatthias Ringwald * 429eddf5e8SMatthias Ringwald */ 439eddf5e8SMatthias Ringwald 449eddf5e8SMatthias Ringwald #include "btstack.h" 45b0e4fcd5SMatthias Ringwald #include <stdio.h> 4604cbc450SMatthias Ringwald 4704cbc450SMatthias Ringwald // uncomment to test start/stop of loopback / audio driver 4804cbc450SMatthias Ringwald // #define TEST_START_STOP_INTERVAL 5000 4904cbc450SMatthias Ringwald 50a7996a1dSMatthias Ringwald // change to 1 for mono input and 2 for stereo input 51a7996a1dSMatthias Ringwald #define NUM_INPUT_CHANNELS 1 52a7996a1dSMatthias Ringwald 53a7996a1dSMatthias Ringwald #define BYTES_PER_SAMPLE (NUM_INPUT_CHANNELS * 2) 54a7996a1dSMatthias Ringwald 5504cbc450SMatthias Ringwald #ifdef TEST_START_STOP_INTERVAL 5604cbc450SMatthias Ringwald static void stop_loopback(btstack_timer_source_t * ts); 5704cbc450SMatthias Ringwald #endif 5804cbc450SMatthias Ringwald 5904cbc450SMatthias Ringwald static btstack_timer_source_t start_stop_timer; 6004cbc450SMatthias Ringwald 619eddf5e8SMatthias Ringwald // samplerate 629eddf5e8SMatthias Ringwald const uint32_t samplerate = 16000; 639eddf5e8SMatthias Ringwald 649eddf5e8SMatthias Ringwald // ring buffer for audio 659eddf5e8SMatthias Ringwald #define BUFFER_SAMPLES 1024 66f5cfc39fSMatthias Ringwald static uint16_t audio_buffer_storage[BUFFER_SAMPLES * NUM_INPUT_CHANNELS]; 679eddf5e8SMatthias Ringwald static btstack_ring_buffer_t audio_buffer; 689eddf5e8SMatthias Ringwald 69a7996a1dSMatthias Ringwald // transfer buffer 70f5cfc39fSMatthias Ringwald #define TRANSFER_SAMPLES 128 71f5cfc39fSMatthias Ringwald static int16_t transfer_buffer[TRANSFER_SAMPLES * NUM_INPUT_CHANNELS]; 729eddf5e8SMatthias Ringwald 739eddf5e8SMatthias Ringwald // playback starts after audio_buffer is half full 74*82b4f8a5SMatthias Ringwald static bool playback_started; 759eddf5e8SMatthias Ringwald 769eddf5e8SMatthias Ringwald // sample couners 779eddf5e8SMatthias Ringwald static int count_recording; 789eddf5e8SMatthias Ringwald static int count_playback; 799eddf5e8SMatthias Ringwald 809eddf5e8SMatthias Ringwald static void audio_recording(const int16_t * pcm_buffer, uint16_t num_samples_to_write){ 819eddf5e8SMatthias Ringwald count_recording += num_samples_to_write; 82a7996a1dSMatthias Ringwald int err = btstack_ring_buffer_write(&audio_buffer, (uint8_t *) pcm_buffer, num_samples_to_write * BYTES_PER_SAMPLE); 839eddf5e8SMatthias Ringwald if (err){ 849eddf5e8SMatthias Ringwald printf("Failed to store %u samples\n", num_samples_to_write); 859eddf5e8SMatthias Ringwald } 869eddf5e8SMatthias Ringwald } 879eddf5e8SMatthias Ringwald 889eddf5e8SMatthias Ringwald static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){ 89a7996a1dSMatthias Ringwald int num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / BYTES_PER_SAMPLE; 90*82b4f8a5SMatthias Ringwald if (playback_started == false){ 91*82b4f8a5SMatthias Ringwald if ( num_samples_in_buffer >= (BUFFER_SAMPLES / 2)){ 92*82b4f8a5SMatthias Ringwald playback_started = true; 93*82b4f8a5SMatthias Ringwald } 949eddf5e8SMatthias Ringwald } 959eddf5e8SMatthias Ringwald count_playback += num_samples_to_write; 96*82b4f8a5SMatthias Ringwald 97*82b4f8a5SMatthias Ringwald if (playback_started){ 98*82b4f8a5SMatthias Ringwald 999eddf5e8SMatthias Ringwald while (num_samples_to_write){ 100*82b4f8a5SMatthias Ringwald 101a7996a1dSMatthias Ringwald num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / BYTES_PER_SAMPLE; 1029eddf5e8SMatthias Ringwald int num_samples_ready = btstack_min(num_samples_in_buffer, num_samples_to_write); 103a7996a1dSMatthias Ringwald // limit by transfer_buffer 104f5cfc39fSMatthias Ringwald int num_samples_from_buffer = btstack_min(num_samples_ready, TRANSFER_SAMPLES); 1059eddf5e8SMatthias Ringwald if (!num_samples_from_buffer) break; 1069eddf5e8SMatthias Ringwald uint32_t bytes_read; 107a7996a1dSMatthias Ringwald btstack_ring_buffer_read(&audio_buffer, (uint8_t *) transfer_buffer, num_samples_from_buffer * BYTES_PER_SAMPLE, &bytes_read); 108*82b4f8a5SMatthias Ringwald 109a7996a1dSMatthias Ringwald #if (NUM_INPUT_CHANNELS == 1) 1109eddf5e8SMatthias Ringwald // duplicate samples for stereo output 1119eddf5e8SMatthias Ringwald int i; 1129eddf5e8SMatthias Ringwald for (i=0; i < num_samples_from_buffer;i++) { 113a7996a1dSMatthias Ringwald *pcm_buffer++ = transfer_buffer[i]; 114a7996a1dSMatthias Ringwald *pcm_buffer++ = transfer_buffer[i]; 1159eddf5e8SMatthias Ringwald num_samples_to_write--; 1169eddf5e8SMatthias Ringwald } 117a7996a1dSMatthias Ringwald #endif 118*82b4f8a5SMatthias Ringwald 119a7996a1dSMatthias Ringwald #if (NUM_INPUT_CHANNELS == 2) 120a7996a1dSMatthias Ringwald // copy samples 121a7996a1dSMatthias Ringwald int i; 122a7996a1dSMatthias Ringwald int j = 0; 123a7996a1dSMatthias Ringwald for (i=0; i < num_samples_from_buffer;i++) { 124a7996a1dSMatthias Ringwald *pcm_buffer++ = transfer_buffer[j++]; 125a7996a1dSMatthias Ringwald *pcm_buffer++ = transfer_buffer[j++]; 126a7996a1dSMatthias Ringwald num_samples_to_write--; 127a7996a1dSMatthias Ringwald } 128a7996a1dSMatthias Ringwald #endif 129*82b4f8a5SMatthias Ringwald 1309eddf5e8SMatthias Ringwald } 1319eddf5e8SMatthias Ringwald 1329eddf5e8SMatthias Ringwald // warn about underrun 1339eddf5e8SMatthias Ringwald if (num_samples_to_write){ 1349eddf5e8SMatthias Ringwald printf("Buffer underrun - recording %u, playback %u - delta %d!\n", count_recording, count_playback, count_recording - count_playback); 1359eddf5e8SMatthias Ringwald } 1369eddf5e8SMatthias Ringwald 1379eddf5e8SMatthias Ringwald } 138*82b4f8a5SMatthias Ringwald 139*82b4f8a5SMatthias Ringwald // fill rest with silence 140*82b4f8a5SMatthias Ringwald memset(pcm_buffer, 0, num_samples_to_write * 4); 1419eddf5e8SMatthias Ringwald } 1429eddf5e8SMatthias Ringwald 14304cbc450SMatthias Ringwald static void start_loopback(btstack_timer_source_t * ts){ 14404cbc450SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 14504cbc450SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 14604cbc450SMatthias Ringwald 14704cbc450SMatthias Ringwald // prepare audio buffer 14804cbc450SMatthias Ringwald btstack_ring_buffer_init(&audio_buffer, (uint8_t*) &audio_buffer_storage[0], sizeof(audio_buffer_storage)); 14904cbc450SMatthias Ringwald 15004cbc450SMatthias Ringwald // setup audio: mono input -> stereo output 15104cbc450SMatthias Ringwald audio_sink->init(2, samplerate, &audio_playback); 152a7996a1dSMatthias Ringwald audio_source->init(NUM_INPUT_CHANNELS, samplerate, &audio_recording); 15304cbc450SMatthias Ringwald 15404cbc450SMatthias Ringwald // start duplex 15504cbc450SMatthias Ringwald audio_sink->start_stream(); 15604cbc450SMatthias Ringwald audio_source->start_stream(); 15704cbc450SMatthias Ringwald 15804cbc450SMatthias Ringwald printf("Start Audio Loopback\n"); 15904cbc450SMatthias Ringwald 16004cbc450SMatthias Ringwald #ifdef TEST_START_STOP_INTERVAL 16104cbc450SMatthias Ringwald // schedule stop 16204cbc450SMatthias Ringwald btstack_run_loop_set_timer_handler(ts, &stop_loopback); 16304cbc450SMatthias Ringwald btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL); 16404cbc450SMatthias Ringwald btstack_run_loop_add_timer(ts); 1658ade2a8bSMatthias Ringwald #else 1668ade2a8bSMatthias Ringwald UNUSED(ts); 16704cbc450SMatthias Ringwald #endif 16804cbc450SMatthias Ringwald } 16904cbc450SMatthias Ringwald 17004cbc450SMatthias Ringwald #ifdef TEST_START_STOP_INTERVAL 17104cbc450SMatthias Ringwald static void stop_loopback(btstack_timer_source_t * ts){ 17204cbc450SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 17304cbc450SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 17404cbc450SMatthias Ringwald 17504cbc450SMatthias Ringwald // stop streams 17604cbc450SMatthias Ringwald audio_sink->stop_stream(); 17704cbc450SMatthias Ringwald audio_source->stop_stream(); 17804cbc450SMatthias Ringwald 17904cbc450SMatthias Ringwald // close audio 18004cbc450SMatthias Ringwald audio_sink->close(); 18104cbc450SMatthias Ringwald audio_source->close(); 18204cbc450SMatthias Ringwald 183*82b4f8a5SMatthias Ringwald playback_started = false; 18404cbc450SMatthias Ringwald 18504cbc450SMatthias Ringwald printf("Stop Audio Loopback\n"); 18604cbc450SMatthias Ringwald 18704cbc450SMatthias Ringwald // schedule stop 18804cbc450SMatthias Ringwald btstack_run_loop_set_timer_handler(ts, &start_loopback); 18904cbc450SMatthias Ringwald btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL); 19004cbc450SMatthias Ringwald btstack_run_loop_add_timer(ts); 19104cbc450SMatthias Ringwald } 19204cbc450SMatthias Ringwald #endif 19304cbc450SMatthias Ringwald 1949eddf5e8SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 1959eddf5e8SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 1969eddf5e8SMatthias Ringwald (void)argc; 1979eddf5e8SMatthias Ringwald (void)argv; 1989eddf5e8SMatthias Ringwald 1999eddf5e8SMatthias Ringwald // check audio interface 2009eddf5e8SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 2019eddf5e8SMatthias Ringwald if (!audio_sink){ 2029eddf5e8SMatthias Ringwald printf("BTstack Audio Sink not setup\n"); 2039eddf5e8SMatthias Ringwald return 10; 2049eddf5e8SMatthias Ringwald } 2059eddf5e8SMatthias Ringwald 2069eddf5e8SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 2079eddf5e8SMatthias Ringwald if (!audio_source){ 2089eddf5e8SMatthias Ringwald printf("BTstack Audio Source not setup\n"); 2099eddf5e8SMatthias Ringwald return 10; 2109eddf5e8SMatthias Ringwald } 2119eddf5e8SMatthias Ringwald 21204cbc450SMatthias Ringwald start_loopback(&start_stop_timer); 2139eddf5e8SMatthias Ringwald 2149eddf5e8SMatthias Ringwald return 0; 2159eddf5e8SMatthias Ringwald } 216ec8ae085SMilanka Ringwald 217ec8ae085SMilanka Ringwald /* EXAMPLE_END */ 218