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