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 239eddf5e8SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 249eddf5e8SMatthias Ringwald * RINGWALD 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 389eddf5e8SMatthias Ringwald /* 399eddf5e8SMatthias Ringwald * Audio Duplex: forward audio from BTstack audio source to audio sink - test for audio interface 409eddf5e8SMatthias Ringwald * 419eddf5e8SMatthias Ringwald */ 429eddf5e8SMatthias Ringwald 439eddf5e8SMatthias Ringwald #include "btstack.h" 449eddf5e8SMatthias Ringwald 45*04cbc450SMatthias Ringwald 46*04cbc450SMatthias Ringwald // uncomment to test start/stop of loopback / audio driver 47*04cbc450SMatthias Ringwald // #define TEST_START_STOP_INTERVAL 5000 48*04cbc450SMatthias Ringwald 49*04cbc450SMatthias Ringwald #ifdef TEST_START_STOP_INTERVAL 50*04cbc450SMatthias Ringwald static void stop_loopback(btstack_timer_source_t * ts); 51*04cbc450SMatthias Ringwald #endif 52*04cbc450SMatthias Ringwald 53*04cbc450SMatthias Ringwald static btstack_timer_source_t start_stop_timer; 54*04cbc450SMatthias Ringwald 559eddf5e8SMatthias Ringwald // samplerate 569eddf5e8SMatthias Ringwald const uint32_t samplerate = 16000; 579eddf5e8SMatthias Ringwald 589eddf5e8SMatthias Ringwald // ring buffer for audio 599eddf5e8SMatthias Ringwald #define BUFFER_SAMPLES 1024 609eddf5e8SMatthias Ringwald static uint16_t audio_buffer_storage[BUFFER_SAMPLES]; 619eddf5e8SMatthias Ringwald static btstack_ring_buffer_t audio_buffer; 629eddf5e8SMatthias Ringwald 639eddf5e8SMatthias Ringwald // mono buffer 649eddf5e8SMatthias Ringwald #define MONO_BUFFER_LEN 128 659eddf5e8SMatthias Ringwald static int16_t mono_buffer[MONO_BUFFER_LEN]; 669eddf5e8SMatthias Ringwald 679eddf5e8SMatthias Ringwald // playback starts after audio_buffer is half full 689eddf5e8SMatthias Ringwald static int playback_started; 699eddf5e8SMatthias Ringwald 709eddf5e8SMatthias Ringwald // sample couners 719eddf5e8SMatthias Ringwald static int count_recording; 729eddf5e8SMatthias Ringwald static int count_playback; 739eddf5e8SMatthias Ringwald 749eddf5e8SMatthias Ringwald static void audio_recording(const int16_t * pcm_buffer, uint16_t num_samples_to_write){ 759eddf5e8SMatthias Ringwald count_recording += num_samples_to_write; 769eddf5e8SMatthias Ringwald int err = btstack_ring_buffer_write(&audio_buffer, (uint8_t *) pcm_buffer, num_samples_to_write * 2); 779eddf5e8SMatthias Ringwald if (err){ 789eddf5e8SMatthias Ringwald printf("Failed to store %u samples\n", num_samples_to_write); 799eddf5e8SMatthias Ringwald } 809eddf5e8SMatthias Ringwald } 819eddf5e8SMatthias Ringwald 829eddf5e8SMatthias Ringwald static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){ 839eddf5e8SMatthias Ringwald int num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / 2; 849eddf5e8SMatthias Ringwald if (playback_started == 0){ 859eddf5e8SMatthias Ringwald if ( num_samples_in_buffer < (BUFFER_SAMPLES / 2)) return; 869eddf5e8SMatthias Ringwald playback_started = 1; 879eddf5e8SMatthias Ringwald } 889eddf5e8SMatthias Ringwald count_playback += num_samples_to_write; 899eddf5e8SMatthias Ringwald while (num_samples_to_write){ 909eddf5e8SMatthias Ringwald num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / 2; 919eddf5e8SMatthias Ringwald int num_samples_ready = btstack_min(num_samples_in_buffer, num_samples_to_write); 929eddf5e8SMatthias Ringwald // limit by mono_buffer 939eddf5e8SMatthias Ringwald int num_samples_from_buffer = btstack_min(num_samples_ready, MONO_BUFFER_LEN); 949eddf5e8SMatthias Ringwald if (!num_samples_from_buffer) break; 959eddf5e8SMatthias Ringwald uint32_t bytes_read; 969eddf5e8SMatthias Ringwald btstack_ring_buffer_read(&audio_buffer, (uint8_t *) mono_buffer, num_samples_from_buffer * 2, &bytes_read); 979eddf5e8SMatthias Ringwald // duplicate samples for stereo output 989eddf5e8SMatthias Ringwald int i; 999eddf5e8SMatthias Ringwald for (i=0; i < num_samples_from_buffer;i++){ 1009eddf5e8SMatthias Ringwald *pcm_buffer++ = mono_buffer[i]; 1019eddf5e8SMatthias Ringwald *pcm_buffer++ = mono_buffer[i]; 1029eddf5e8SMatthias Ringwald num_samples_to_write--; 1039eddf5e8SMatthias Ringwald } 1049eddf5e8SMatthias Ringwald } 1059eddf5e8SMatthias Ringwald 1069eddf5e8SMatthias Ringwald // warn about underrun 1079eddf5e8SMatthias Ringwald if (num_samples_to_write){ 1089eddf5e8SMatthias Ringwald printf("Buffer underrun - recording %u, playback %u - delta %d!\n", count_recording, count_playback, count_recording - count_playback); 1099eddf5e8SMatthias Ringwald } 1109eddf5e8SMatthias Ringwald 1119eddf5e8SMatthias Ringwald // fill rest with silence 1129eddf5e8SMatthias Ringwald while (num_samples_to_write){ 1139eddf5e8SMatthias Ringwald *pcm_buffer++ = 0; 1149eddf5e8SMatthias Ringwald *pcm_buffer++ = 0; 1159eddf5e8SMatthias Ringwald num_samples_to_write--; 1169eddf5e8SMatthias Ringwald } 1179eddf5e8SMatthias Ringwald } 1189eddf5e8SMatthias Ringwald 119*04cbc450SMatthias Ringwald static void start_loopback(btstack_timer_source_t * ts){ 120*04cbc450SMatthias Ringwald 121*04cbc450SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 122*04cbc450SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 123*04cbc450SMatthias Ringwald 124*04cbc450SMatthias Ringwald // prepare audio buffer 125*04cbc450SMatthias Ringwald btstack_ring_buffer_init(&audio_buffer, (uint8_t*) &audio_buffer_storage[0], sizeof(audio_buffer_storage)); 126*04cbc450SMatthias Ringwald 127*04cbc450SMatthias Ringwald // setup audio: mono input -> stereo output 128*04cbc450SMatthias Ringwald audio_sink->init(2, samplerate, &audio_playback); 129*04cbc450SMatthias Ringwald audio_source->init(1, samplerate, &audio_recording); 130*04cbc450SMatthias Ringwald 131*04cbc450SMatthias Ringwald // start duplex 132*04cbc450SMatthias Ringwald audio_sink->start_stream(); 133*04cbc450SMatthias Ringwald audio_source->start_stream(); 134*04cbc450SMatthias Ringwald 135*04cbc450SMatthias Ringwald printf("Start Audio Loopback\n"); 136*04cbc450SMatthias Ringwald 137*04cbc450SMatthias Ringwald #ifdef TEST_START_STOP_INTERVAL 138*04cbc450SMatthias Ringwald // schedule stop 139*04cbc450SMatthias Ringwald btstack_run_loop_set_timer_handler(ts, &stop_loopback); 140*04cbc450SMatthias Ringwald btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL); 141*04cbc450SMatthias Ringwald btstack_run_loop_add_timer(ts); 142*04cbc450SMatthias Ringwald #endif 143*04cbc450SMatthias Ringwald } 144*04cbc450SMatthias Ringwald 145*04cbc450SMatthias Ringwald #ifdef TEST_START_STOP_INTERVAL 146*04cbc450SMatthias Ringwald static void stop_loopback(btstack_timer_source_t * ts){ 147*04cbc450SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 148*04cbc450SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 149*04cbc450SMatthias Ringwald 150*04cbc450SMatthias Ringwald // stop streams 151*04cbc450SMatthias Ringwald audio_sink->stop_stream(); 152*04cbc450SMatthias Ringwald audio_source->stop_stream(); 153*04cbc450SMatthias Ringwald 154*04cbc450SMatthias Ringwald // close audio 155*04cbc450SMatthias Ringwald audio_sink->close(); 156*04cbc450SMatthias Ringwald audio_source->close(); 157*04cbc450SMatthias Ringwald 158*04cbc450SMatthias Ringwald playback_started = 0; 159*04cbc450SMatthias Ringwald 160*04cbc450SMatthias Ringwald printf("Stop Audio Loopback\n"); 161*04cbc450SMatthias Ringwald 162*04cbc450SMatthias Ringwald // schedule stop 163*04cbc450SMatthias Ringwald btstack_run_loop_set_timer_handler(ts, &start_loopback); 164*04cbc450SMatthias Ringwald btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL); 165*04cbc450SMatthias Ringwald btstack_run_loop_add_timer(ts); 166*04cbc450SMatthias Ringwald } 167*04cbc450SMatthias Ringwald #endif 168*04cbc450SMatthias Ringwald 1699eddf5e8SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 1709eddf5e8SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 1719eddf5e8SMatthias Ringwald (void)argc; 1729eddf5e8SMatthias Ringwald (void)argv; 1739eddf5e8SMatthias Ringwald 1749eddf5e8SMatthias Ringwald // check audio interface 1759eddf5e8SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 1769eddf5e8SMatthias Ringwald if (!audio_sink){ 1779eddf5e8SMatthias Ringwald printf("BTstack Audio Sink not setup\n"); 1789eddf5e8SMatthias Ringwald return 10; 1799eddf5e8SMatthias Ringwald } 1809eddf5e8SMatthias Ringwald 1819eddf5e8SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 1829eddf5e8SMatthias Ringwald if (!audio_source){ 1839eddf5e8SMatthias Ringwald printf("BTstack Audio Source not setup\n"); 1849eddf5e8SMatthias Ringwald return 10; 1859eddf5e8SMatthias Ringwald } 1869eddf5e8SMatthias Ringwald 187*04cbc450SMatthias Ringwald start_loopback(&start_stop_timer); 1889eddf5e8SMatthias Ringwald 1899eddf5e8SMatthias Ringwald return 0; 1909eddf5e8SMatthias Ringwald } 191