xref: /btstack/example/audio_duplex.c (revision f11fd9a990fedbf11b7c70e38b9da44019506e13)
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 #define BTSTACK_FILE__ "audio_duplex.c"
39 
40 /* EXAMPLE_START(audio_duplex): Audio Driver - Forward Audio from Source to Sink
41  *
42  */
43 
44 #include "btstack.h"
45 #include <stdio.h>
46 
47 // uncomment to test start/stop of loopback / audio driver
48 // #define TEST_START_STOP_INTERVAL 5000
49 
50 // change to 1 for mono input and 2 for stereo input
51 #define NUM_INPUT_CHANNELS 1
52 
53 #define BYTES_PER_SAMPLE (NUM_INPUT_CHANNELS * 2)
54 
55 #ifdef TEST_START_STOP_INTERVAL
56 static void stop_loopback(btstack_timer_source_t * ts);
57 #endif
58 
59 static btstack_timer_source_t start_stop_timer;
60 
61 // samplerate
62 const uint32_t samplerate = 16000;
63 
64 // ring buffer for audio
65 #define BUFFER_SAMPLES 1024
66 static uint16_t              audio_buffer_storage[BUFFER_SAMPLES];
67 static btstack_ring_buffer_t audio_buffer;
68 
69 // transfer buffer
70 #define TRANSFER_BUFFER_LEN 128
71 static int16_t transfer_buffer[TRANSFER_BUFFER_LEN];
72 
73 // playback starts after audio_buffer is half full
74 static int playback_started;
75 
76 // sample couners
77 static int count_recording;
78 static int count_playback;
79 
80 static void audio_recording(const int16_t * pcm_buffer, uint16_t num_samples_to_write){
81     count_recording += num_samples_to_write;
82     int err = btstack_ring_buffer_write(&audio_buffer, (uint8_t *) pcm_buffer, num_samples_to_write * BYTES_PER_SAMPLE);
83     if (err){
84         printf("Failed to store %u samples\n", num_samples_to_write);
85     }
86 }
87 
88 static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){
89     int num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / BYTES_PER_SAMPLE;
90     if (playback_started == 0){
91         if ( num_samples_in_buffer < (BUFFER_SAMPLES / 2)) return;
92         playback_started = 1;
93     }
94     count_playback += num_samples_to_write;
95     while (num_samples_to_write){
96         num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / BYTES_PER_SAMPLE;
97         int num_samples_ready = btstack_min(num_samples_in_buffer, num_samples_to_write);
98         // limit by transfer_buffer
99         int num_samples_from_buffer = btstack_min(num_samples_ready, TRANSFER_BUFFER_LEN);
100         if (!num_samples_from_buffer) break;
101         uint32_t bytes_read;
102         btstack_ring_buffer_read(&audio_buffer, (uint8_t *) transfer_buffer, num_samples_from_buffer * BYTES_PER_SAMPLE, &bytes_read);
103 #if (NUM_INPUT_CHANNELS == 1)
104         // duplicate samples for stereo output
105         int i;
106         for (i=0; i < num_samples_from_buffer;i++) {
107             *pcm_buffer++ = transfer_buffer[i];
108             *pcm_buffer++ = transfer_buffer[i];
109             num_samples_to_write--;
110         }
111 #endif
112 #if (NUM_INPUT_CHANNELS == 2)
113         // copy samples
114         int i;
115         int j = 0;
116         for (i=0; i < num_samples_from_buffer;i++) {
117             *pcm_buffer++ = transfer_buffer[j++];
118             *pcm_buffer++ = transfer_buffer[j++];
119             num_samples_to_write--;
120         }
121 #endif
122     }
123 
124     // warn about underrun
125     if (num_samples_to_write){
126         printf("Buffer underrun - recording %u, playback %u - delta %d!\n", count_recording, count_playback, count_recording - count_playback);
127     }
128 
129     // fill rest with silence
130     while (num_samples_to_write){
131         *pcm_buffer++ = 0;
132         *pcm_buffer++ = 0;
133         num_samples_to_write--;
134     }
135 }
136 
137 static void start_loopback(btstack_timer_source_t * ts){
138     const btstack_audio_sink_t   * audio_sink   = btstack_audio_sink_get_instance();
139     const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
140 
141     // prepare audio buffer
142     btstack_ring_buffer_init(&audio_buffer, (uint8_t*) &audio_buffer_storage[0], sizeof(audio_buffer_storage));
143 
144     // setup audio: mono input -> stereo output
145     audio_sink->init(2, samplerate, &audio_playback);
146     audio_source->init(NUM_INPUT_CHANNELS, samplerate, &audio_recording);
147 
148     // start duplex
149     audio_sink->start_stream();
150     audio_source->start_stream();
151 
152     printf("Start Audio Loopback\n");
153 
154 #ifdef TEST_START_STOP_INTERVAL
155     // schedule stop
156     btstack_run_loop_set_timer_handler(ts, &stop_loopback);
157     btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL);
158     btstack_run_loop_add_timer(ts);
159 #else
160     UNUSED(ts);
161 #endif
162 }
163 
164 #ifdef TEST_START_STOP_INTERVAL
165 static void stop_loopback(btstack_timer_source_t * ts){
166     const btstack_audio_sink_t   * audio_sink   = btstack_audio_sink_get_instance();
167     const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
168 
169     // stop streams
170     audio_sink->stop_stream();
171     audio_source->stop_stream();
172 
173     // close audio
174     audio_sink->close();
175     audio_source->close();
176 
177     playback_started = 0;
178 
179     printf("Stop Audio Loopback\n");
180 
181     // schedule stop
182     btstack_run_loop_set_timer_handler(ts, &start_loopback);
183     btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL);
184     btstack_run_loop_add_timer(ts);
185 }
186 #endif
187 
188 int btstack_main(int argc, const char * argv[]);
189 int btstack_main(int argc, const char * argv[]){
190     (void)argc;
191     (void)argv;
192 
193     // check audio interface
194     const btstack_audio_sink_t * audio_sink   = btstack_audio_sink_get_instance();
195     if (!audio_sink){
196         printf("BTstack Audio Sink not setup\n");
197         return 10;
198     }
199 
200     const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
201     if (!audio_source){
202         printf("BTstack Audio Source not setup\n");
203         return 10;
204     }
205 
206     start_loopback(&start_stop_timer);
207 
208     return 0;
209 }
210 
211 /* EXAMPLE_END */