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 BLUEKITCHEN
24 * GMBH 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 2048
66 static uint16_t audio_buffer_storage[BUFFER_SAMPLES * NUM_INPUT_CHANNELS];
67 static btstack_ring_buffer_t audio_buffer;
68
69 // transfer buffer
70 #define TRANSFER_SAMPLES 128
71 static int16_t transfer_buffer[TRANSFER_SAMPLES * NUM_INPUT_CHANNELS];
72
73 // playback starts after audio_buffer is half full
74 static bool playback_started;
75
76 // sample couners
77 static int count_recording;
78 static int count_playback;
79
audio_recording(const int16_t * pcm_buffer,uint16_t num_samples_to_write)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
audio_playback(int16_t * pcm_buffer,uint16_t num_samples_to_write)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 == false){
91 if ( num_samples_in_buffer >= (BUFFER_SAMPLES / 2)){
92 playback_started = true;
93 }
94 }
95 count_playback += num_samples_to_write;
96
97 if (playback_started){
98
99 while (num_samples_to_write){
100
101 num_samples_in_buffer = btstack_ring_buffer_bytes_available(&audio_buffer) / BYTES_PER_SAMPLE;
102 int num_samples_ready = btstack_min(num_samples_in_buffer, num_samples_to_write);
103 // limit by transfer_buffer
104 int num_samples_from_buffer = btstack_min(num_samples_ready, TRANSFER_SAMPLES);
105 if (!num_samples_from_buffer) break;
106 uint32_t bytes_read;
107 btstack_ring_buffer_read(&audio_buffer, (uint8_t *) transfer_buffer, num_samples_from_buffer * BYTES_PER_SAMPLE, &bytes_read);
108
109 #if (NUM_INPUT_CHANNELS == 1)
110 // duplicate samples for stereo output
111 int i;
112 for (i=0; i < num_samples_from_buffer;i++) {
113 *pcm_buffer++ = transfer_buffer[i];
114 *pcm_buffer++ = transfer_buffer[i];
115 num_samples_to_write--;
116 }
117 #endif
118
119 #if (NUM_INPUT_CHANNELS == 2)
120 // copy samples
121 int i;
122 int j = 0;
123 for (i=0; i < num_samples_from_buffer;i++) {
124 *pcm_buffer++ = transfer_buffer[j++];
125 *pcm_buffer++ = transfer_buffer[j++];
126 num_samples_to_write--;
127 }
128 #endif
129
130 }
131
132 // warn about underrun
133 if (num_samples_to_write){
134 printf("Buffer underrun - recording %u, playback %u - delta %d!\n", count_recording, count_playback, count_recording - count_playback);
135 }
136
137 }
138
139 // fill rest with silence
140 memset(pcm_buffer, 0, num_samples_to_write * 4);
141 }
142
start_loopback(btstack_timer_source_t * ts)143 static void start_loopback(btstack_timer_source_t * ts){
144 const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance();
145 const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
146
147 // prepare audio buffer
148 btstack_ring_buffer_init(&audio_buffer, (uint8_t*) &audio_buffer_storage[0], sizeof(audio_buffer_storage));
149
150 // setup audio: mono input -> stereo output
151 audio_sink->init(2, samplerate, &audio_playback);
152 audio_source->init(NUM_INPUT_CHANNELS, samplerate, &audio_recording);
153
154 // start duplex
155 audio_sink->start_stream();
156 audio_source->start_stream();
157
158 printf("Start Audio Loopback\n");
159
160 #ifdef TEST_START_STOP_INTERVAL
161 // schedule stop
162 btstack_run_loop_set_timer_handler(ts, &stop_loopback);
163 btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL);
164 btstack_run_loop_add_timer(ts);
165 #else
166 UNUSED(ts);
167 #endif
168 }
169
170 #ifdef TEST_START_STOP_INTERVAL
stop_loopback(btstack_timer_source_t * ts)171 static void stop_loopback(btstack_timer_source_t * ts){
172 const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance();
173 const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
174
175 // stop streams
176 audio_sink->stop_stream();
177 audio_source->stop_stream();
178
179 // close audio
180 audio_sink->close();
181 audio_source->close();
182
183 playback_started = false;
184
185 printf("Stop Audio Loopback\n");
186
187 // schedule stop
188 btstack_run_loop_set_timer_handler(ts, &start_loopback);
189 btstack_run_loop_set_timer(ts, TEST_START_STOP_INTERVAL);
190 btstack_run_loop_add_timer(ts);
191 }
192 #endif
193
194 int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])195 int btstack_main(int argc, const char * argv[]){
196 (void)argc;
197 (void)argv;
198
199 // check audio interface
200 const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance();
201 if (!audio_sink){
202 printf("BTstack Audio Sink not setup\n");
203 return 10;
204 }
205
206 const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance();
207 if (!audio_source){
208 printf("BTstack Audio Source not setup\n");
209 return 10;
210 }
211
212 start_loopback(&start_stop_timer);
213
214 return 0;
215 }
216
217 /* EXAMPLE_END */
218