xref: /btstack/example/audio_duplex.c (revision 9eddf5e891d3c2342e7a0998f934301e15e032a3)
1 /*
2  * Copyright (C) 2014 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 /*
39  * Audio Duplex: forward audio from BTstack audio source to audio sink - test for audio interface
40  *
41  */
42 
43 #include "btstack.h"
44 
45 // samplerate
46 const uint32_t samplerate = 16000;
47 
48 // ring buffer for audio
49 #define BUFFER_SAMPLES 1024
50 static uint16_t              audio_buffer_storage[BUFFER_SAMPLES];
51 static btstack_ring_buffer_t audio_buffer;
52 
53 // mono buffer
54 #define MONO_BUFFER_LEN 128
55 static int16_t mono_buffer[MONO_BUFFER_LEN];
56 
57 // playback starts after audio_buffer is half full
58 static int playback_started;
59 
60 // sample couners
61 static int count_recording;
62 static int count_playback;
63 
64 static void audio_recording(const int16_t * pcm_buffer, uint16_t num_samples_to_write){
65     count_recording += num_samples_to_write;
66     int err = btstack_ring_buffer_write(&audio_buffer, (uint8_t *) pcm_buffer, num_samples_to_write * 2);
67     if (err){
68         printf("Failed to store %u samples\n", num_samples_to_write);
69     }
70 }
71 
72 static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){
73     int num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / 2;
74     if (playback_started == 0){
75         if ( num_samples_in_buffer < (BUFFER_SAMPLES / 2)) return;
76         playback_started = 1;
77     }
78     count_playback += num_samples_to_write;
79     while (num_samples_to_write){
80         num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / 2;
81         int num_samples_ready = btstack_min(num_samples_in_buffer, num_samples_to_write);
82         // limit by mono_buffer
83         int num_samples_from_buffer = btstack_min(num_samples_ready, MONO_BUFFER_LEN);
84         if (!num_samples_from_buffer) break;
85         uint32_t bytes_read;
86         btstack_ring_buffer_read(&audio_buffer, (uint8_t *) mono_buffer, num_samples_from_buffer * 2, &bytes_read);
87         // duplicate samples for stereo output
88         int i;
89         for (i=0; i < num_samples_from_buffer;i++){
90             *pcm_buffer++ = mono_buffer[i];
91             *pcm_buffer++ = mono_buffer[i];
92             num_samples_to_write--;
93         }
94     }
95 
96     // warn about underrun
97     if (num_samples_to_write){
98         printf("Buffer underrun - recording %u, playback %u - delta %d!\n", count_recording, count_playback, count_recording - count_playback);
99     }
100 
101     // fill rest with silence
102     while (num_samples_to_write){
103         *pcm_buffer++ = 0;
104         *pcm_buffer++ = 0;
105         num_samples_to_write--;
106     }
107 }
108 
109 int btstack_main(int argc, const char * argv[]);
110 int btstack_main(int argc, const char * argv[]){
111     (void)argc;
112     (void)argv;
113 
114     // check audio interface
115     const btstack_audio_sink_t * audio_sink   = btstack_audio_sink_get_instance();
116     if (!audio_sink){
117         printf("BTstack Audio Sink not setup\n");
118         return 10;
119     }
120 
121     const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
122     if (!audio_source){
123         printf("BTstack Audio Source not setup\n");
124         return 10;
125     }
126 
127     // prepare audio buffer
128     btstack_ring_buffer_init(&audio_buffer, (uint8_t*) &audio_buffer_storage[0], sizeof(audio_buffer_storage));
129 
130     // setup audio: mono input -> stereo output
131     audio_source->init(1, samplerate, &audio_recording);
132     audio_sink->init(2, samplerate, &audio_playback);
133 
134     // start duplex
135     audio_source->start_stream();
136     audio_sink->start_stream();
137 
138     return 0;
139 }
140