xref: /btstack/platform/posix/btstack_audio_portaudio.c (revision a04735638352d59f4f5a84aad138ca7ff6deb95c)
1a89df2dfSMatthias Ringwald /*
2a89df2dfSMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3a89df2dfSMatthias Ringwald  *
4a89df2dfSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5a89df2dfSMatthias Ringwald  * modification, are permitted provided that the following conditions
6a89df2dfSMatthias Ringwald  * are met:
7a89df2dfSMatthias Ringwald  *
8a89df2dfSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9a89df2dfSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10a89df2dfSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11a89df2dfSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12a89df2dfSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13a89df2dfSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14a89df2dfSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15a89df2dfSMatthias Ringwald  *    from this software without specific prior written permission.
16a89df2dfSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17a89df2dfSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18a89df2dfSMatthias Ringwald  *    monetary gain.
19a89df2dfSMatthias Ringwald  *
20a89df2dfSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21a89df2dfSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a89df2dfSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25a89df2dfSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26a89df2dfSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27a89df2dfSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28a89df2dfSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29a89df2dfSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30a89df2dfSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a89df2dfSMatthias Ringwald  * SUCH DAMAGE.
32a89df2dfSMatthias Ringwald  *
33a89df2dfSMatthias Ringwald  * Please inquire about commercial licensing options at
34a89df2dfSMatthias Ringwald  * [email protected]
35a89df2dfSMatthias Ringwald  *
36a89df2dfSMatthias Ringwald  */
37a89df2dfSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_audio_portaudio.c"
39a89df2dfSMatthias Ringwald 
40a89df2dfSMatthias Ringwald 
41a89df2dfSMatthias Ringwald #include <stdint.h>
42a89df2dfSMatthias Ringwald #include <string.h>
43a89df2dfSMatthias Ringwald #include "btstack_debug.h"
44a89df2dfSMatthias Ringwald #include "btstack_audio.h"
45a89df2dfSMatthias Ringwald #include "btstack_run_loop.h"
46a89df2dfSMatthias Ringwald 
47a89df2dfSMatthias Ringwald #ifdef HAVE_PORTAUDIO
48a89df2dfSMatthias Ringwald 
49a89df2dfSMatthias Ringwald #define PA_SAMPLE_TYPE               paInt16
50a89df2dfSMatthias Ringwald #define NUM_FRAMES_PER_PA_BUFFER       512
51a89df2dfSMatthias Ringwald #define NUM_OUTPUT_BUFFERS               3
52a89df2dfSMatthias Ringwald #define NUM_INPUT_BUFFERS                2
53a89df2dfSMatthias Ringwald #define DRIVER_POLL_INTERVAL_MS          5
54a89df2dfSMatthias Ringwald 
55a89df2dfSMatthias Ringwald #include <portaudio.h>
56a89df2dfSMatthias Ringwald 
57a89df2dfSMatthias Ringwald // config
581a551b1bSMatthias Ringwald static int                    num_channels_sink;
591a551b1bSMatthias Ringwald static int                    num_channels_source;
60bcd51729SMatthias Ringwald static int                    num_bytes_per_sample_sink;
61bcd51729SMatthias Ringwald static int                    num_bytes_per_sample_source;
62a89df2dfSMatthias Ringwald 
63a89df2dfSMatthias Ringwald // portaudio
643d3351f3SMatthias Ringwald static int portaudio_initialized;
653d3351f3SMatthias Ringwald 
663d3351f3SMatthias Ringwald // state
673d3351f3SMatthias Ringwald static int source_initialized;
683d3351f3SMatthias Ringwald static int sink_initialized;
693d3351f3SMatthias Ringwald static int source_active;
703d3351f3SMatthias Ringwald static int sink_active;
713d3351f3SMatthias Ringwald 
721a551b1bSMatthias Ringwald static uint8_t sink_volume;
731a551b1bSMatthias Ringwald 
743d3351f3SMatthias Ringwald static PaStream * stream_source;
753d3351f3SMatthias Ringwald static PaStream * stream_sink;
76a89df2dfSMatthias Ringwald 
77a89df2dfSMatthias Ringwald // client
78a89df2dfSMatthias Ringwald static void (*playback_callback)(int16_t * buffer, uint16_t num_samples);
79a89df2dfSMatthias Ringwald static void (*recording_callback)(const int16_t * buffer, uint16_t num_samples);
80a89df2dfSMatthias Ringwald 
81a89df2dfSMatthias Ringwald // output buffer
82ee37d7ffSMatthias Ringwald static int16_t               output_buffer_storage[NUM_OUTPUT_BUFFERS * NUM_FRAMES_PER_PA_BUFFER * 2];   // stereo
83ee37d7ffSMatthias Ringwald static int16_t             * output_buffers[NUM_OUTPUT_BUFFERS];
84a89df2dfSMatthias Ringwald static int                   output_buffer_to_play;
85a89df2dfSMatthias Ringwald static int                   output_buffer_to_fill;
86a89df2dfSMatthias Ringwald 
87a89df2dfSMatthias Ringwald // input buffer
88a89df2dfSMatthias Ringwald static int16_t               input_buffer_a[NUM_FRAMES_PER_PA_BUFFER * 2];   // stereo
89a89df2dfSMatthias Ringwald static int16_t               input_buffer_b[NUM_FRAMES_PER_PA_BUFFER * 2];   // stereo
90a89df2dfSMatthias Ringwald static int16_t             * input_buffers[NUM_INPUT_BUFFERS] = { input_buffer_a, input_buffer_b};
91a89df2dfSMatthias Ringwald static int                   input_buffer_to_record;
92a89df2dfSMatthias Ringwald static int                   input_buffer_to_fill;
93a89df2dfSMatthias Ringwald 
94a89df2dfSMatthias Ringwald 
95a89df2dfSMatthias Ringwald // timer to fill output ring buffer
963d3351f3SMatthias Ringwald static btstack_timer_source_t  driver_timer_sink;
973d3351f3SMatthias Ringwald static btstack_timer_source_t  driver_timer_source;
98a89df2dfSMatthias Ringwald 
993d3351f3SMatthias Ringwald static int portaudio_callback_sink( const void *                     inputBuffer,
100a89df2dfSMatthias Ringwald                                     void *                           outputBuffer,
101ee37d7ffSMatthias Ringwald                                     unsigned long                    frames_per_buffer,
102a89df2dfSMatthias Ringwald                                     const PaStreamCallbackTimeInfo * timeInfo,
103a89df2dfSMatthias Ringwald                                     PaStreamCallbackFlags            statusFlags,
104a89df2dfSMatthias Ringwald                                     void *                           userData ) {
105a89df2dfSMatthias Ringwald 
106a89df2dfSMatthias Ringwald     /** portaudio_callback is called from different thread, don't use hci_dump / log_info here without additional checks */
107a89df2dfSMatthias Ringwald 
108a89df2dfSMatthias Ringwald     (void) timeInfo; /* Prevent unused variable warnings. */
109a89df2dfSMatthias Ringwald     (void) statusFlags;
110a89df2dfSMatthias Ringwald     (void) userData;
111ee37d7ffSMatthias Ringwald     (void) frames_per_buffer;
1123d3351f3SMatthias Ringwald     (void) inputBuffer;
113a89df2dfSMatthias Ringwald 
1141a551b1bSMatthias Ringwald     // simplified volume control
1151a551b1bSMatthias Ringwald     uint16_t index;
1161a551b1bSMatthias Ringwald     int16_t * from_buffer = output_buffers[output_buffer_to_play];
1171a551b1bSMatthias Ringwald     int16_t * to_buffer = (int16_t *) outputBuffer;
118ee37d7ffSMatthias Ringwald     btstack_assert(frames_per_buffer == NUM_FRAMES_PER_PA_BUFFER);
1191a551b1bSMatthias Ringwald 
1201a551b1bSMatthias Ringwald #if 0
1211a551b1bSMatthias Ringwald     // up to 8 right shifts
1221a551b1bSMatthias Ringwald     int right_shift = 8 - btstack_min(8, ((sink_volume + 15) / 16));
1231a551b1bSMatthias Ringwald     for (index = 0; index < (NUM_FRAMES_PER_PA_BUFFER * num_channels_sink); index++){
1241a551b1bSMatthias Ringwald         *to_buffer++ = (*from_buffer++) >> right_shift;
1251a551b1bSMatthias Ringwald     }
1261a551b1bSMatthias Ringwald #else
1271a551b1bSMatthias Ringwald     // multiply with volume ^ 4
1281a551b1bSMatthias Ringwald     int16_t x2 = sink_volume * sink_volume;
1291a551b1bSMatthias Ringwald     int16_t x4 = (x2 * x2) >> 14;
1301a551b1bSMatthias Ringwald     for (index = 0; index < (NUM_FRAMES_PER_PA_BUFFER * num_channels_sink); index++){
1311a551b1bSMatthias Ringwald         *to_buffer++ = ((*from_buffer++) * x4) >> 14;
1321a551b1bSMatthias Ringwald     }
1331a551b1bSMatthias Ringwald #endif
134a89df2dfSMatthias Ringwald 
135a89df2dfSMatthias Ringwald     // next
136a89df2dfSMatthias Ringwald     output_buffer_to_play = (output_buffer_to_play + 1 ) % NUM_OUTPUT_BUFFERS;
1373d3351f3SMatthias Ringwald 
1383d3351f3SMatthias Ringwald     return 0;
139a89df2dfSMatthias Ringwald }
140a89df2dfSMatthias Ringwald 
1413d3351f3SMatthias Ringwald static int portaudio_callback_source( const void *                     inputBuffer,
1423d3351f3SMatthias Ringwald                                       void *                           outputBuffer,
1433d3351f3SMatthias Ringwald                                       unsigned long                    samples_per_buffer,
1443d3351f3SMatthias Ringwald                                       const PaStreamCallbackTimeInfo * timeInfo,
1453d3351f3SMatthias Ringwald                                       PaStreamCallbackFlags            statusFlags,
1463d3351f3SMatthias Ringwald                                       void *                           userData ) {
1473d3351f3SMatthias Ringwald 
1483d3351f3SMatthias Ringwald     /** portaudio_callback is called from different thread, don't use hci_dump / log_info here without additional checks */
1493d3351f3SMatthias Ringwald 
1503d3351f3SMatthias Ringwald     (void) timeInfo; /* Prevent unused variable warnings. */
1513d3351f3SMatthias Ringwald     (void) statusFlags;
1523d3351f3SMatthias Ringwald     (void) userData;
1533d3351f3SMatthias Ringwald     (void) samples_per_buffer;
1543d3351f3SMatthias Ringwald     (void) outputBuffer;
155a89df2dfSMatthias Ringwald 
156a89df2dfSMatthias Ringwald     // store in one of our buffers
157bcd51729SMatthias Ringwald     memcpy(input_buffers[input_buffer_to_fill], inputBuffer, NUM_FRAMES_PER_PA_BUFFER * num_bytes_per_sample_source);
158a89df2dfSMatthias Ringwald 
159a89df2dfSMatthias Ringwald     // next
160a89df2dfSMatthias Ringwald     input_buffer_to_fill = (input_buffer_to_fill + 1 ) % NUM_INPUT_BUFFERS;
161a89df2dfSMatthias Ringwald 
162a89df2dfSMatthias Ringwald     return 0;
163a89df2dfSMatthias Ringwald }
164a89df2dfSMatthias Ringwald 
1653d3351f3SMatthias Ringwald static void driver_timer_handler_sink(btstack_timer_source_t * ts){
166a89df2dfSMatthias Ringwald 
167a89df2dfSMatthias Ringwald     // playback buffer ready to fill
168*a0473563SMatthias Ringwald     while (output_buffer_to_play != output_buffer_to_fill){
169a89df2dfSMatthias Ringwald         (*playback_callback)(output_buffers[output_buffer_to_fill], NUM_FRAMES_PER_PA_BUFFER);
170a89df2dfSMatthias Ringwald 
171a89df2dfSMatthias Ringwald         // next
172a89df2dfSMatthias Ringwald         output_buffer_to_fill = (output_buffer_to_fill + 1 ) % NUM_OUTPUT_BUFFERS;
173a89df2dfSMatthias Ringwald     }
174a89df2dfSMatthias Ringwald 
1753d3351f3SMatthias Ringwald     // re-set timer
1763d3351f3SMatthias Ringwald     btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS);
1773d3351f3SMatthias Ringwald     btstack_run_loop_add_timer(ts);
1783d3351f3SMatthias Ringwald }
1793d3351f3SMatthias Ringwald 
1803d3351f3SMatthias Ringwald static void driver_timer_handler_source(btstack_timer_source_t * ts){
1813d3351f3SMatthias Ringwald 
182a89df2dfSMatthias Ringwald     // recording buffer ready to process
1833d3351f3SMatthias Ringwald     if (input_buffer_to_record != input_buffer_to_fill){
184a89df2dfSMatthias Ringwald 
185a89df2dfSMatthias Ringwald         (*recording_callback)(input_buffers[input_buffer_to_record], NUM_FRAMES_PER_PA_BUFFER);
186a89df2dfSMatthias Ringwald 
187a89df2dfSMatthias Ringwald         // next
188a89df2dfSMatthias Ringwald         input_buffer_to_record = (input_buffer_to_record + 1 ) % NUM_INPUT_BUFFERS;
189a89df2dfSMatthias Ringwald     }
190a89df2dfSMatthias Ringwald 
191a89df2dfSMatthias Ringwald     // re-set timer
192a89df2dfSMatthias Ringwald     btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS);
193a89df2dfSMatthias Ringwald     btstack_run_loop_add_timer(ts);
194a89df2dfSMatthias Ringwald }
195a89df2dfSMatthias Ringwald 
1963d3351f3SMatthias Ringwald static int btstack_audio_portaudio_sink_init(
197a89df2dfSMatthias Ringwald     uint8_t channels,
198a89df2dfSMatthias Ringwald     uint32_t samplerate,
1993d3351f3SMatthias Ringwald     void (*playback)(int16_t * buffer, uint16_t num_samples)
200a89df2dfSMatthias Ringwald ){
2013d3351f3SMatthias Ringwald     PaError err;
202a89df2dfSMatthias Ringwald 
2031a551b1bSMatthias Ringwald     num_channels_sink = channels;
204bcd51729SMatthias Ringwald     num_bytes_per_sample_sink = 2 * channels;
205a89df2dfSMatthias Ringwald 
206ee37d7ffSMatthias Ringwald     uint8_t i;
207ee37d7ffSMatthias Ringwald     for (i=0;i<NUM_OUTPUT_BUFFERS;i++){
208ee37d7ffSMatthias Ringwald         output_buffers[i] = &output_buffer_storage[i * NUM_FRAMES_PER_PA_BUFFER * 2];
209ee37d7ffSMatthias Ringwald     }
210ee37d7ffSMatthias Ringwald 
2113d3351f3SMatthias Ringwald     if (!playback){
2123d3351f3SMatthias Ringwald         log_error("No playback callback");
2133d3351f3SMatthias Ringwald         return 1;
2143d3351f3SMatthias Ringwald     }
2153d3351f3SMatthias Ringwald 
216a89df2dfSMatthias Ringwald     /* -- initialize PortAudio -- */
2173d3351f3SMatthias Ringwald     if (!portaudio_initialized){
2183d3351f3SMatthias Ringwald         err = Pa_Initialize();
219a89df2dfSMatthias Ringwald         if (err != paNoError){
22024cc6cccSMatthias Ringwald             log_error("Portudio: error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
221a89df2dfSMatthias Ringwald             return err;
222a89df2dfSMatthias Ringwald         }
2233d3351f3SMatthias Ringwald         portaudio_initialized = 1;
2243d3351f3SMatthias Ringwald     }
225a89df2dfSMatthias Ringwald 
226a89df2dfSMatthias Ringwald     /* -- setup output -- */
2273d3351f3SMatthias Ringwald     PaStreamParameters theOutputParameters;
228a89df2dfSMatthias Ringwald     theOutputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
229a89df2dfSMatthias Ringwald     theOutputParameters.channelCount = channels;
230a89df2dfSMatthias Ringwald     theOutputParameters.sampleFormat = PA_SAMPLE_TYPE;
231a89df2dfSMatthias Ringwald     theOutputParameters.suggestedLatency = Pa_GetDeviceInfo( theOutputParameters.device )->defaultHighOutputLatency;
232a89df2dfSMatthias Ringwald     theOutputParameters.hostApiSpecificStreamInfo = NULL;
233a89df2dfSMatthias Ringwald 
234a89df2dfSMatthias Ringwald     const PaDeviceInfo *outputDeviceInfo;
235a89df2dfSMatthias Ringwald     outputDeviceInfo = Pa_GetDeviceInfo( theOutputParameters.device );
23624cc6cccSMatthias Ringwald     log_info("PortAudio: sink device: %s", outputDeviceInfo->name);
237bb90a366SMatthias Ringwald     UNUSED(outputDeviceInfo);
238a89df2dfSMatthias Ringwald 
2393d3351f3SMatthias Ringwald     /* -- setup stream -- */
2403d3351f3SMatthias Ringwald     err = Pa_OpenStream(
2413abb6826SMatthias Ringwald            &stream_sink,
2423d3351f3SMatthias Ringwald            NULL,
2433d3351f3SMatthias Ringwald            &theOutputParameters,
2443d3351f3SMatthias Ringwald            samplerate,
2453d3351f3SMatthias Ringwald            NUM_FRAMES_PER_PA_BUFFER,
2463d3351f3SMatthias Ringwald            paClipOff,           /* we won't output out of range samples so don't bother clipping them */
2473d3351f3SMatthias Ringwald            portaudio_callback_sink,  /* use callback */
2483d3351f3SMatthias Ringwald            NULL );
2493d3351f3SMatthias Ringwald 
2503d3351f3SMatthias Ringwald     if (err != paNoError){
25124cc6cccSMatthias Ringwald         log_error("Portudio: error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
2523d3351f3SMatthias Ringwald         return err;
2533d3351f3SMatthias Ringwald     }
25424cc6cccSMatthias Ringwald     log_info("PortAudio: sink stream created");
2553d3351f3SMatthias Ringwald 
2563abb6826SMatthias Ringwald     const PaStreamInfo * stream_info = Pa_GetStreamInfo(stream_sink);
25724cc6cccSMatthias Ringwald     log_info("PortAudio: sink latency: %f", stream_info->outputLatency);
258894c930cSMatthias Ringwald     UNUSED(stream_info);
2593d3351f3SMatthias Ringwald 
2603d3351f3SMatthias Ringwald     playback_callback  = playback;
2613d3351f3SMatthias Ringwald 
2623d3351f3SMatthias Ringwald     sink_initialized = 1;
2631a551b1bSMatthias Ringwald     sink_volume = 127;
2643d3351f3SMatthias Ringwald 
2653d3351f3SMatthias Ringwald     return 0;
2663d3351f3SMatthias Ringwald }
2673d3351f3SMatthias Ringwald 
2683d3351f3SMatthias Ringwald static int btstack_audio_portaudio_source_init(
2693d3351f3SMatthias Ringwald     uint8_t channels,
2703d3351f3SMatthias Ringwald     uint32_t samplerate,
2713d3351f3SMatthias Ringwald     void (*recording)(const int16_t * buffer, uint16_t num_samples)
2723d3351f3SMatthias Ringwald ){
2733d3351f3SMatthias Ringwald     PaError err;
2743d3351f3SMatthias Ringwald 
2751a551b1bSMatthias Ringwald     num_channels_source = channels;
276bcd51729SMatthias Ringwald     num_bytes_per_sample_source = 2 * channels;
2773d3351f3SMatthias Ringwald 
2783d3351f3SMatthias Ringwald     if (!recording){
2793d3351f3SMatthias Ringwald         log_error("No recording callback");
2803d3351f3SMatthias Ringwald         return 1;
2813d3351f3SMatthias Ringwald     }
2823d3351f3SMatthias Ringwald 
2833d3351f3SMatthias Ringwald     /* -- initialize PortAudio -- */
2843d3351f3SMatthias Ringwald     if (!portaudio_initialized){
2853d3351f3SMatthias Ringwald         err = Pa_Initialize();
2863d3351f3SMatthias Ringwald         if (err != paNoError){
28724cc6cccSMatthias Ringwald             log_error("Portudio: Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
2883d3351f3SMatthias Ringwald             return err;
2893d3351f3SMatthias Ringwald         }
2903d3351f3SMatthias Ringwald         portaudio_initialized = 1;
291a89df2dfSMatthias Ringwald     }
292a89df2dfSMatthias Ringwald 
293a89df2dfSMatthias Ringwald     /* -- setup input -- */
2943d3351f3SMatthias Ringwald     PaStreamParameters theInputParameters;
295a89df2dfSMatthias Ringwald     theInputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
296a89df2dfSMatthias Ringwald     theInputParameters.channelCount = channels;
297a89df2dfSMatthias Ringwald     theInputParameters.sampleFormat = PA_SAMPLE_TYPE;
298a89df2dfSMatthias Ringwald     theInputParameters.suggestedLatency = Pa_GetDeviceInfo( theInputParameters.device )->defaultHighInputLatency;
299a89df2dfSMatthias Ringwald     theInputParameters.hostApiSpecificStreamInfo = NULL;
300a89df2dfSMatthias Ringwald 
301a89df2dfSMatthias Ringwald     const PaDeviceInfo *inputDeviceInfo;
302a89df2dfSMatthias Ringwald     inputDeviceInfo = Pa_GetDeviceInfo( theInputParameters.device );
30324cc6cccSMatthias Ringwald     log_info("PortAudio: source device: %s", inputDeviceInfo->name);
304bb90a366SMatthias Ringwald     UNUSED(inputDeviceInfo);
305a89df2dfSMatthias Ringwald 
306a89df2dfSMatthias Ringwald     /* -- setup stream -- */
307a89df2dfSMatthias Ringwald     err = Pa_OpenStream(
3083abb6826SMatthias Ringwald            &stream_source,
3093d3351f3SMatthias Ringwald            &theInputParameters,
3103d3351f3SMatthias Ringwald            NULL,
311a89df2dfSMatthias Ringwald            samplerate,
312a89df2dfSMatthias Ringwald            NUM_FRAMES_PER_PA_BUFFER,
313a89df2dfSMatthias Ringwald            paClipOff,           /* we won't output out of range samples so don't bother clipping them */
3143d3351f3SMatthias Ringwald            portaudio_callback_source,  /* use callback */
315a89df2dfSMatthias Ringwald            NULL );
316a89df2dfSMatthias Ringwald 
317a89df2dfSMatthias Ringwald     if (err != paNoError){
318a89df2dfSMatthias Ringwald         log_error("Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
319a89df2dfSMatthias Ringwald         return err;
320a89df2dfSMatthias Ringwald     }
32124cc6cccSMatthias Ringwald     log_info("PortAudio: source stream created");
322a89df2dfSMatthias Ringwald 
3233abb6826SMatthias Ringwald     const PaStreamInfo * stream_info = Pa_GetStreamInfo(stream_source);
32424cc6cccSMatthias Ringwald     log_info("PortAudio: source latency: %f", stream_info->inputLatency);
325894c930cSMatthias Ringwald     UNUSED(stream_info);
326a89df2dfSMatthias Ringwald 
327a89df2dfSMatthias Ringwald     recording_callback = recording;
328a89df2dfSMatthias Ringwald 
3293d3351f3SMatthias Ringwald     source_initialized = 1;
3303d3351f3SMatthias Ringwald 
331a89df2dfSMatthias Ringwald     return 0;
332a89df2dfSMatthias Ringwald }
333a89df2dfSMatthias Ringwald 
3341b7f8fa1SMatthias Ringwald static void btstack_audio_portaudio_sink_set_volume(uint8_t volume){
3351a551b1bSMatthias Ringwald     sink_volume = volume;
3361b7f8fa1SMatthias Ringwald }
3371b7f8fa1SMatthias Ringwald 
3381b7f8fa1SMatthias Ringwald static void btstack_audio_portaudio_source_set_gain(uint8_t gain){
3391b7f8fa1SMatthias Ringwald     UNUSED(gain);
3401b7f8fa1SMatthias Ringwald }
3411b7f8fa1SMatthias Ringwald 
3423d3351f3SMatthias Ringwald static void btstack_audio_portaudio_sink_start_stream(void){
3433d3351f3SMatthias Ringwald 
3443d3351f3SMatthias Ringwald     if (!playback_callback) return;
345a89df2dfSMatthias Ringwald 
346ee37d7ffSMatthias Ringwald     // fill buffers once
347ee37d7ffSMatthias Ringwald     uint8_t i;
348ee37d7ffSMatthias Ringwald     for (i=0;i<NUM_OUTPUT_BUFFERS-1;i++){
349ee37d7ffSMatthias Ringwald         (*playback_callback)(&output_buffer_storage[i*NUM_FRAMES_PER_PA_BUFFER*2], NUM_FRAMES_PER_PA_BUFFER);
350ee37d7ffSMatthias Ringwald     }
351a89df2dfSMatthias Ringwald     output_buffer_to_play = 0;
352ee37d7ffSMatthias Ringwald     output_buffer_to_fill = NUM_OUTPUT_BUFFERS-1;
353a89df2dfSMatthias Ringwald 
354a89df2dfSMatthias Ringwald     /* -- start stream -- */
355df991ce9SMatthias Ringwald     PaError err = Pa_StartStream(stream_sink);
356a89df2dfSMatthias Ringwald     if (err != paNoError){
35724cc6cccSMatthias Ringwald         log_error("PortAudio: error starting sink stream: \"%s\"\n",  Pa_GetErrorText(err));
358a89df2dfSMatthias Ringwald         return;
359a89df2dfSMatthias Ringwald     }
360a89df2dfSMatthias Ringwald 
361a89df2dfSMatthias Ringwald     // start timer
3623d3351f3SMatthias Ringwald     btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink);
3633d3351f3SMatthias Ringwald     btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS);
3643d3351f3SMatthias Ringwald     btstack_run_loop_add_timer(&driver_timer_sink);
3653d3351f3SMatthias Ringwald 
3663d3351f3SMatthias Ringwald     sink_active = 1;
367a89df2dfSMatthias Ringwald }
368a89df2dfSMatthias Ringwald 
3693d3351f3SMatthias Ringwald static void btstack_audio_portaudio_source_start_stream(void){
3703d3351f3SMatthias Ringwald 
3713d3351f3SMatthias Ringwald     if (!recording_callback) return;
3723d3351f3SMatthias Ringwald 
3733d3351f3SMatthias Ringwald     /* -- start stream -- */
374df991ce9SMatthias Ringwald     PaError err = Pa_StartStream(stream_source);
3753d3351f3SMatthias Ringwald     if (err != paNoError){
37624cc6cccSMatthias Ringwald         log_error("PortAudio: error starting source stream: \"%s\"\n",  Pa_GetErrorText(err));
3773d3351f3SMatthias Ringwald         return;
3783d3351f3SMatthias Ringwald     }
3793d3351f3SMatthias Ringwald 
3803d3351f3SMatthias Ringwald     // start timer
3813d3351f3SMatthias Ringwald     btstack_run_loop_set_timer_handler(&driver_timer_source, &driver_timer_handler_source);
3823d3351f3SMatthias Ringwald     btstack_run_loop_set_timer(&driver_timer_source, DRIVER_POLL_INTERVAL_MS);
3833d3351f3SMatthias Ringwald     btstack_run_loop_add_timer(&driver_timer_source);
3843d3351f3SMatthias Ringwald 
3853d3351f3SMatthias Ringwald     source_active = 1;
3863d3351f3SMatthias Ringwald }
3873d3351f3SMatthias Ringwald 
3883d3351f3SMatthias Ringwald static void btstack_audio_portaudio_sink_stop_stream(void){
3893d3351f3SMatthias Ringwald 
3903d3351f3SMatthias Ringwald     if (!playback_callback) return;
3913d3351f3SMatthias Ringwald     if (!sink_active)       return;
392a89df2dfSMatthias Ringwald 
393a89df2dfSMatthias Ringwald     // stop timer
3943d3351f3SMatthias Ringwald     btstack_run_loop_remove_timer(&driver_timer_sink);
395a89df2dfSMatthias Ringwald 
3963d3351f3SMatthias Ringwald     PaError err = Pa_StopStream(stream_sink);
397a89df2dfSMatthias Ringwald     if (err != paNoError){
39824cc6cccSMatthias Ringwald         log_error("PortAudio: error stopping sink stream: \"%s\"",  Pa_GetErrorText(err));
399a89df2dfSMatthias Ringwald         return;
400a89df2dfSMatthias Ringwald     }
40124cc6cccSMatthias Ringwald 
40224cc6cccSMatthias Ringwald     sink_active = 0;
4033d3351f3SMatthias Ringwald }
4043d3351f3SMatthias Ringwald 
4053d3351f3SMatthias Ringwald static void btstack_audio_portaudio_source_stop_stream(void){
4063d3351f3SMatthias Ringwald 
4073d3351f3SMatthias Ringwald     if (!recording_callback) return;
4083d3351f3SMatthias Ringwald     if (!source_active)      return;
4093d3351f3SMatthias Ringwald 
4103d3351f3SMatthias Ringwald     // stop timer
4113d3351f3SMatthias Ringwald     btstack_run_loop_remove_timer(&driver_timer_source);
4123d3351f3SMatthias Ringwald 
4133d3351f3SMatthias Ringwald     PaError err = Pa_StopStream(stream_source);
414a89df2dfSMatthias Ringwald     if (err != paNoError){
41524cc6cccSMatthias Ringwald         log_error("PortAudio: error stopping source stream: \"%s\"",  Pa_GetErrorText(err));
416a89df2dfSMatthias Ringwald         return;
417a89df2dfSMatthias Ringwald     }
41824cc6cccSMatthias Ringwald 
41924cc6cccSMatthias Ringwald     source_active = 0;
4203d3351f3SMatthias Ringwald }
4213d3351f3SMatthias Ringwald 
4223d3351f3SMatthias Ringwald static void btstack_audio_portaudio_close_pa_if_not_needed(void){
4233d3351f3SMatthias Ringwald     if (source_initialized) return;
4243d3351f3SMatthias Ringwald     if (sink_initialized) return;
4253d3351f3SMatthias Ringwald     PaError err = Pa_Terminate();
426a89df2dfSMatthias Ringwald     if (err != paNoError){
42724cc6cccSMatthias Ringwald         log_error("Portudio: Error terminating portaudio: \"%s\"",  Pa_GetErrorText(err));
428a89df2dfSMatthias Ringwald         return;
429a89df2dfSMatthias Ringwald     }
4301faf5e55SMatthias Ringwald     portaudio_initialized = 0;
431a89df2dfSMatthias Ringwald }
432a89df2dfSMatthias Ringwald 
4333d3351f3SMatthias Ringwald static void btstack_audio_portaudio_sink_close(void){
4343d3351f3SMatthias Ringwald 
4353d3351f3SMatthias Ringwald     if (!playback_callback) return;
4363d3351f3SMatthias Ringwald 
4373d3351f3SMatthias Ringwald     if (sink_active){
4383d3351f3SMatthias Ringwald         btstack_audio_portaudio_sink_stop_stream();
4393d3351f3SMatthias Ringwald     }
4403d3351f3SMatthias Ringwald 
4411247fb8fSMatthias Ringwald     PaError err = Pa_CloseStream(stream_sink);
4423d3351f3SMatthias Ringwald     if (err != paNoError){
44324cc6cccSMatthias Ringwald         log_error("PortAudio: error closing sink stream: \"%s\"",  Pa_GetErrorText(err));
4443d3351f3SMatthias Ringwald         return;
4453d3351f3SMatthias Ringwald     }
4463d3351f3SMatthias Ringwald 
4473d3351f3SMatthias Ringwald     sink_initialized = 0;
4483d3351f3SMatthias Ringwald     btstack_audio_portaudio_close_pa_if_not_needed();
4493d3351f3SMatthias Ringwald }
4503d3351f3SMatthias Ringwald 
4513d3351f3SMatthias Ringwald static void btstack_audio_portaudio_source_close(void){
4523d3351f3SMatthias Ringwald 
4533d3351f3SMatthias Ringwald     if (!recording_callback) return;
4543d3351f3SMatthias Ringwald 
4553d3351f3SMatthias Ringwald     if (source_active){
4562d66e797SMatthias Ringwald         btstack_audio_portaudio_source_stop_stream();
4573d3351f3SMatthias Ringwald     }
4583d3351f3SMatthias Ringwald 
4591247fb8fSMatthias Ringwald     PaError err = Pa_CloseStream(stream_source);
4603d3351f3SMatthias Ringwald     if (err != paNoError){
46124cc6cccSMatthias Ringwald         log_error("PortAudio: error closing source stream: \"%s\"",  Pa_GetErrorText(err));
4623d3351f3SMatthias Ringwald         return;
4633d3351f3SMatthias Ringwald     }
4643d3351f3SMatthias Ringwald 
4653d3351f3SMatthias Ringwald     source_initialized = 0;
4663d3351f3SMatthias Ringwald     btstack_audio_portaudio_close_pa_if_not_needed();
4673d3351f3SMatthias Ringwald }
4683d3351f3SMatthias Ringwald 
4693d3351f3SMatthias Ringwald static const btstack_audio_sink_t btstack_audio_portaudio_sink = {
4703d3351f3SMatthias Ringwald     /* int (*init)(..);*/                                       &btstack_audio_portaudio_sink_init,
4711b7f8fa1SMatthias Ringwald     /* void (*set_volume)(uint8_t volume); */                   &btstack_audio_portaudio_sink_set_volume,
4723d3351f3SMatthias Ringwald     /* void (*start_stream(void));*/                            &btstack_audio_portaudio_sink_start_stream,
4731247fb8fSMatthias Ringwald     /* void (*stop_stream)(void)  */                            &btstack_audio_portaudio_sink_stop_stream,
4743d3351f3SMatthias Ringwald     /* void (*close)(void); */                                  &btstack_audio_portaudio_sink_close
475a89df2dfSMatthias Ringwald };
476a89df2dfSMatthias Ringwald 
4773d3351f3SMatthias Ringwald static const btstack_audio_source_t btstack_audio_portaudio_source = {
4783d3351f3SMatthias Ringwald     /* int (*init)(..);*/                                       &btstack_audio_portaudio_source_init,
4791b7f8fa1SMatthias Ringwald     /* void (*set_gain)(uint8_t gain); */                       &btstack_audio_portaudio_source_set_gain,
4803d3351f3SMatthias Ringwald     /* void (*start_stream(void));*/                            &btstack_audio_portaudio_source_start_stream,
4811247fb8fSMatthias Ringwald     /* void (*stop_stream)(void)  */                            &btstack_audio_portaudio_source_stop_stream,
4823d3351f3SMatthias Ringwald     /* void (*close)(void); */                                  &btstack_audio_portaudio_source_close
4833d3351f3SMatthias Ringwald };
4843d3351f3SMatthias Ringwald 
4853d3351f3SMatthias Ringwald const btstack_audio_sink_t * btstack_audio_portaudio_sink_get_instance(void){
4863d3351f3SMatthias Ringwald     return &btstack_audio_portaudio_sink;
4873d3351f3SMatthias Ringwald }
4883d3351f3SMatthias Ringwald 
4893d3351f3SMatthias Ringwald const btstack_audio_source_t * btstack_audio_portaudio_source_get_instance(void){
4903d3351f3SMatthias Ringwald     return &btstack_audio_portaudio_source;
491a89df2dfSMatthias Ringwald }
492a89df2dfSMatthias Ringwald 
493a89df2dfSMatthias Ringwald #endif
494