mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-14 01:27:41 +00:00
portaudio: split audio interface
This commit is contained in:
parent
9eddf5e891
commit
3d3351f352
@ -59,7 +59,16 @@ static int num_channels;
|
||||
static int num_bytes_per_sample;
|
||||
|
||||
// portaudio
|
||||
static PaStream * stream;
|
||||
static int portaudio_initialized;
|
||||
|
||||
// state
|
||||
static int source_initialized;
|
||||
static int sink_initialized;
|
||||
static int source_active;
|
||||
static int sink_active;
|
||||
|
||||
static PaStream * stream_source;
|
||||
static PaStream * stream_sink;
|
||||
|
||||
// client
|
||||
static void (*playback_callback)(int16_t * buffer, uint16_t num_samples);
|
||||
@ -82,14 +91,15 @@ static int input_buffer_to_fill;
|
||||
|
||||
|
||||
// timer to fill output ring buffer
|
||||
static btstack_timer_source_t driver_timer;
|
||||
static btstack_timer_source_t driver_timer_sink;
|
||||
static btstack_timer_source_t driver_timer_source;
|
||||
|
||||
static int portaudio_callback( const void * inputBuffer,
|
||||
void * outputBuffer,
|
||||
unsigned long samples_per_buffer,
|
||||
const PaStreamCallbackTimeInfo * timeInfo,
|
||||
PaStreamCallbackFlags statusFlags,
|
||||
void * userData ) {
|
||||
static int portaudio_callback_sink( const void * inputBuffer,
|
||||
void * outputBuffer,
|
||||
unsigned long samples_per_buffer,
|
||||
const PaStreamCallbackTimeInfo * timeInfo,
|
||||
PaStreamCallbackFlags statusFlags,
|
||||
void * userData ) {
|
||||
|
||||
/** portaudio_callback is called from different thread, don't use hci_dump / log_info here without additional checks */
|
||||
|
||||
@ -97,42 +107,60 @@ static int portaudio_callback( const void * inputBuffer,
|
||||
(void) statusFlags;
|
||||
(void) userData;
|
||||
(void) samples_per_buffer;
|
||||
(void) inputBuffer;
|
||||
|
||||
// -- playback / output
|
||||
if (playback_callback){
|
||||
// fill from one of our buffers
|
||||
memcpy(outputBuffer, output_buffers[output_buffer_to_play], NUM_FRAMES_PER_PA_BUFFER * num_bytes_per_sample);
|
||||
|
||||
// fill from one of our buffers
|
||||
memcpy(outputBuffer, output_buffers[output_buffer_to_play], NUM_FRAMES_PER_PA_BUFFER * num_bytes_per_sample);
|
||||
|
||||
// next
|
||||
output_buffer_to_play = (output_buffer_to_play + 1 ) % NUM_OUTPUT_BUFFERS;
|
||||
}
|
||||
|
||||
// -- recording / input
|
||||
if (recording_callback){
|
||||
|
||||
// store in one of our buffers
|
||||
memcpy(input_buffers[input_buffer_to_fill], inputBuffer, NUM_FRAMES_PER_PA_BUFFER * num_bytes_per_sample);
|
||||
|
||||
// next
|
||||
input_buffer_to_fill = (input_buffer_to_fill + 1 ) % NUM_INPUT_BUFFERS;
|
||||
}
|
||||
// next
|
||||
output_buffer_to_play = (output_buffer_to_play + 1 ) % NUM_OUTPUT_BUFFERS;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void driver_timer_handler(btstack_timer_source_t * ts){
|
||||
static int portaudio_callback_source( const void * inputBuffer,
|
||||
void * outputBuffer,
|
||||
unsigned long samples_per_buffer,
|
||||
const PaStreamCallbackTimeInfo * timeInfo,
|
||||
PaStreamCallbackFlags statusFlags,
|
||||
void * userData ) {
|
||||
|
||||
/** portaudio_callback is called from different thread, don't use hci_dump / log_info here without additional checks */
|
||||
|
||||
(void) timeInfo; /* Prevent unused variable warnings. */
|
||||
(void) statusFlags;
|
||||
(void) userData;
|
||||
(void) samples_per_buffer;
|
||||
(void) outputBuffer;
|
||||
|
||||
// store in one of our buffers
|
||||
memcpy(input_buffers[input_buffer_to_fill], inputBuffer, NUM_FRAMES_PER_PA_BUFFER * num_bytes_per_sample);
|
||||
|
||||
// next
|
||||
input_buffer_to_fill = (input_buffer_to_fill + 1 ) % NUM_INPUT_BUFFERS;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void driver_timer_handler_sink(btstack_timer_source_t * ts){
|
||||
|
||||
// playback buffer ready to fill
|
||||
if (playback_callback && output_buffer_to_play != output_buffer_to_fill){
|
||||
if (output_buffer_to_play != output_buffer_to_fill){
|
||||
(*playback_callback)(output_buffers[output_buffer_to_fill], NUM_FRAMES_PER_PA_BUFFER);
|
||||
|
||||
// next
|
||||
output_buffer_to_fill = (output_buffer_to_fill + 1 ) % NUM_OUTPUT_BUFFERS;
|
||||
}
|
||||
|
||||
// re-set timer
|
||||
btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS);
|
||||
btstack_run_loop_add_timer(ts);
|
||||
}
|
||||
|
||||
static void driver_timer_handler_source(btstack_timer_source_t * ts){
|
||||
|
||||
// recording buffer ready to process
|
||||
if (recording_callback && input_buffer_to_record != input_buffer_to_fill){
|
||||
if (input_buffer_to_record != input_buffer_to_fill){
|
||||
|
||||
(*recording_callback)(input_buffers[input_buffer_to_record], NUM_FRAMES_PER_PA_BUFFER);
|
||||
|
||||
@ -145,68 +173,52 @@ static void driver_timer_handler(btstack_timer_source_t * ts){
|
||||
btstack_run_loop_add_timer(ts);
|
||||
}
|
||||
|
||||
static int btstack_audio_portaudio_init(
|
||||
static int btstack_audio_portaudio_sink_init(
|
||||
uint8_t channels,
|
||||
uint32_t samplerate,
|
||||
void (*playback)(int16_t * buffer, uint16_t num_samples),
|
||||
void (*recording)(const int16_t * buffer, uint16_t num_samples)
|
||||
void (*playback)(int16_t * buffer, uint16_t num_samples)
|
||||
){
|
||||
PaError err;
|
||||
|
||||
num_channels = channels;
|
||||
num_bytes_per_sample = 2 * channels;
|
||||
|
||||
if (!playback){
|
||||
log_error("No playback callback");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* -- initialize PortAudio -- */
|
||||
PaError err = Pa_Initialize();
|
||||
if (err != paNoError){
|
||||
log_error("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
PaStreamParameters theInputParameters;
|
||||
PaStreamParameters theOutputParameters;
|
||||
|
||||
PaStreamParameters * inputParameters = NULL;
|
||||
PaStreamParameters * outputParameters = NULL;
|
||||
if (!portaudio_initialized){
|
||||
err = Pa_Initialize();
|
||||
if (err != paNoError){
|
||||
log_error("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err));
|
||||
return err;
|
||||
}
|
||||
portaudio_initialized = 1;
|
||||
}
|
||||
|
||||
/* -- setup output -- */
|
||||
if (playback){
|
||||
theOutputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
|
||||
theOutputParameters.channelCount = channels;
|
||||
theOutputParameters.sampleFormat = PA_SAMPLE_TYPE;
|
||||
theOutputParameters.suggestedLatency = Pa_GetDeviceInfo( theOutputParameters.device )->defaultHighOutputLatency;
|
||||
theOutputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
PaStreamParameters theOutputParameters;
|
||||
theOutputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
|
||||
theOutputParameters.channelCount = channels;
|
||||
theOutputParameters.sampleFormat = PA_SAMPLE_TYPE;
|
||||
theOutputParameters.suggestedLatency = Pa_GetDeviceInfo( theOutputParameters.device )->defaultHighOutputLatency;
|
||||
theOutputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
const PaDeviceInfo *outputDeviceInfo;
|
||||
outputDeviceInfo = Pa_GetDeviceInfo( theOutputParameters.device );
|
||||
log_info("PortAudio: Output device: %s", outputDeviceInfo->name);
|
||||
|
||||
outputParameters = &theOutputParameters;
|
||||
}
|
||||
|
||||
/* -- setup input -- */
|
||||
if (recording){
|
||||
theInputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
|
||||
theInputParameters.channelCount = channels;
|
||||
theInputParameters.sampleFormat = PA_SAMPLE_TYPE;
|
||||
theInputParameters.suggestedLatency = Pa_GetDeviceInfo( theInputParameters.device )->defaultHighInputLatency;
|
||||
theInputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
const PaDeviceInfo *inputDeviceInfo;
|
||||
inputDeviceInfo = Pa_GetDeviceInfo( theInputParameters.device );
|
||||
log_info("PortAudio: Input device: %s", inputDeviceInfo->name);
|
||||
|
||||
inputParameters = &theInputParameters;
|
||||
}
|
||||
const PaDeviceInfo *outputDeviceInfo;
|
||||
outputDeviceInfo = Pa_GetDeviceInfo( theOutputParameters.device );
|
||||
log_info("PortAudio: Output device: %s", outputDeviceInfo->name);
|
||||
|
||||
/* -- setup stream -- */
|
||||
err = Pa_OpenStream(
|
||||
&stream,
|
||||
inputParameters,
|
||||
outputParameters,
|
||||
&stream_source,
|
||||
NULL,
|
||||
&theOutputParameters,
|
||||
samplerate,
|
||||
NUM_FRAMES_PER_PA_BUFFER,
|
||||
paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
||||
portaudio_callback, /* use callback */
|
||||
portaudio_callback_sink, /* use callback */
|
||||
NULL );
|
||||
|
||||
if (err != paNoError){
|
||||
@ -215,17 +227,83 @@ static int btstack_audio_portaudio_init(
|
||||
}
|
||||
log_info("PortAudio: stream opened");
|
||||
|
||||
const PaStreamInfo * stream_info = Pa_GetStreamInfo(stream);
|
||||
log_info("PortAudio: Input latency: %f", stream_info->inputLatency);
|
||||
const PaStreamInfo * stream_info = Pa_GetStreamInfo(stream_source);
|
||||
log_info("PortAudio: Output latency: %f", stream_info->outputLatency);
|
||||
|
||||
playback_callback = playback;
|
||||
recording_callback = recording;
|
||||
|
||||
sink_initialized = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void btstack_audio_portaudio_start_stream(void){
|
||||
static int btstack_audio_portaudio_source_init(
|
||||
uint8_t channels,
|
||||
uint32_t samplerate,
|
||||
void (*recording)(const int16_t * buffer, uint16_t num_samples)
|
||||
){
|
||||
PaError err;
|
||||
|
||||
num_channels = channels;
|
||||
num_bytes_per_sample = 2 * channels;
|
||||
|
||||
if (!recording){
|
||||
log_error("No recording callback");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* -- initialize PortAudio -- */
|
||||
if (!portaudio_initialized){
|
||||
err = Pa_Initialize();
|
||||
if (err != paNoError){
|
||||
log_error("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err));
|
||||
return err;
|
||||
}
|
||||
portaudio_initialized = 1;
|
||||
}
|
||||
|
||||
/* -- setup input -- */
|
||||
PaStreamParameters theInputParameters;
|
||||
theInputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
|
||||
theInputParameters.channelCount = channels;
|
||||
theInputParameters.sampleFormat = PA_SAMPLE_TYPE;
|
||||
theInputParameters.suggestedLatency = Pa_GetDeviceInfo( theInputParameters.device )->defaultHighInputLatency;
|
||||
theInputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
|
||||
const PaDeviceInfo *inputDeviceInfo;
|
||||
inputDeviceInfo = Pa_GetDeviceInfo( theInputParameters.device );
|
||||
log_info("PortAudio: Input device: %s", inputDeviceInfo->name);
|
||||
|
||||
/* -- setup stream -- */
|
||||
err = Pa_OpenStream(
|
||||
&stream_sink,
|
||||
&theInputParameters,
|
||||
NULL,
|
||||
samplerate,
|
||||
NUM_FRAMES_PER_PA_BUFFER,
|
||||
paClipOff, /* we won't output out of range samples so don't bother clipping them */
|
||||
portaudio_callback_source, /* use callback */
|
||||
NULL );
|
||||
|
||||
if (err != paNoError){
|
||||
log_error("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err));
|
||||
return err;
|
||||
}
|
||||
log_info("PortAudio: stream opened");
|
||||
|
||||
const PaStreamInfo * stream_info = Pa_GetStreamInfo(stream_sink);
|
||||
log_info("PortAudio: Input latency: %f", stream_info->inputLatency);
|
||||
|
||||
recording_callback = recording;
|
||||
|
||||
source_initialized = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void btstack_audio_portaudio_sink_start_stream(void){
|
||||
|
||||
if (!playback_callback) return;
|
||||
|
||||
// fill buffer once
|
||||
(*playback_callback)(output_buffer_a, NUM_FRAMES_PER_PA_BUFFER);
|
||||
@ -234,49 +312,138 @@ static void btstack_audio_portaudio_start_stream(void){
|
||||
output_buffer_to_fill = 2;
|
||||
|
||||
/* -- start stream -- */
|
||||
PaError err = Pa_StartStream(stream);
|
||||
PaError err = Pa_StartStream(stream_source);
|
||||
if (err != paNoError){
|
||||
log_error("Error starting the stream: \"%s\"\n", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// start timer
|
||||
btstack_run_loop_set_timer_handler(&driver_timer, &driver_timer_handler);
|
||||
btstack_run_loop_set_timer(&driver_timer, DRIVER_POLL_INTERVAL_MS);
|
||||
btstack_run_loop_add_timer(&driver_timer);
|
||||
btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink);
|
||||
btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS);
|
||||
btstack_run_loop_add_timer(&driver_timer_sink);
|
||||
|
||||
sink_active = 1;
|
||||
}
|
||||
|
||||
static void btstack_audio_portaudio_close(void){
|
||||
static void btstack_audio_portaudio_source_start_stream(void){
|
||||
|
||||
if (!recording_callback) return;
|
||||
|
||||
/* -- start stream -- */
|
||||
PaError err = Pa_StartStream(stream_sink);
|
||||
if (err != paNoError){
|
||||
log_error("Error starting the stream: \"%s\"\n", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// start timer
|
||||
btstack_run_loop_set_timer_handler(&driver_timer_source, &driver_timer_handler_source);
|
||||
btstack_run_loop_set_timer(&driver_timer_source, DRIVER_POLL_INTERVAL_MS);
|
||||
btstack_run_loop_add_timer(&driver_timer_source);
|
||||
|
||||
source_active = 1;
|
||||
}
|
||||
|
||||
static void btstack_audio_portaudio_sink_stop_stream(void){
|
||||
|
||||
if (!playback_callback) return;
|
||||
if (!sink_active) return;
|
||||
|
||||
// stop timer
|
||||
btstack_run_loop_remove_timer(&driver_timer);
|
||||
btstack_run_loop_remove_timer(&driver_timer_sink);
|
||||
|
||||
log_info("PortAudio: Stream closed");
|
||||
PaError err = Pa_StopStream(stream);
|
||||
PaError err = Pa_StopStream(stream_sink);
|
||||
if (err != paNoError){
|
||||
log_error("Error stopping the stream: \"%s\"", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
err = Pa_CloseStream(stream);
|
||||
}
|
||||
|
||||
static void btstack_audio_portaudio_source_stop_stream(void){
|
||||
|
||||
if (!recording_callback) return;
|
||||
if (!source_active) return;
|
||||
|
||||
// stop timer
|
||||
btstack_run_loop_remove_timer(&driver_timer_source);
|
||||
|
||||
log_info("PortAudio: Stream closed");
|
||||
PaError err = Pa_StopStream(stream_source);
|
||||
if (err != paNoError){
|
||||
log_error("Error closing the stream: \"%s\"", Pa_GetErrorText(err));
|
||||
log_error("Error stopping the stream: \"%s\"", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
err = Pa_Terminate();
|
||||
}
|
||||
|
||||
static void btstack_audio_portaudio_close_pa_if_not_needed(void){
|
||||
if (source_initialized) return;
|
||||
if (sink_initialized) return;
|
||||
PaError err = Pa_Terminate();
|
||||
if (err != paNoError){
|
||||
log_error("Error terminating portaudio: \"%s\"", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static const btstack_audio_t btstack_audio_portaudio = {
|
||||
/* int (*init)(..);*/ &btstack_audio_portaudio_init,
|
||||
/* void (*start_stream(void));*/ &btstack_audio_portaudio_start_stream,
|
||||
/* void (*close)(void); */ &btstack_audio_portaudio_close
|
||||
static void btstack_audio_portaudio_sink_close(void){
|
||||
|
||||
if (!playback_callback) return;
|
||||
|
||||
if (sink_active){
|
||||
btstack_audio_portaudio_sink_stop_stream();
|
||||
}
|
||||
|
||||
err = Pa_CloseStream(stream_sink);
|
||||
if (err != paNoError){
|
||||
log_error("Error closing the stream: \"%s\"", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
|
||||
sink_initialized = 0;
|
||||
btstack_audio_portaudio_close_pa_if_not_needed();
|
||||
}
|
||||
|
||||
|
||||
static void btstack_audio_portaudio_source_close(void){
|
||||
|
||||
if (!recording_callback) return;
|
||||
|
||||
if (source_active){
|
||||
btstack_audio_portaudio_sink_stop_stream();
|
||||
}
|
||||
|
||||
err = Pa_CloseStream(stream_source);
|
||||
if (err != paNoError){
|
||||
log_error("Error closing the stream: \"%s\"", Pa_GetErrorText(err));
|
||||
return;
|
||||
}
|
||||
|
||||
source_initialized = 0;
|
||||
btstack_audio_portaudio_close_pa_if_not_needed();
|
||||
}
|
||||
|
||||
static const btstack_audio_sink_t btstack_audio_portaudio_sink = {
|
||||
/* int (*init)(..);*/ &btstack_audio_portaudio_sink_init,
|
||||
/* void (*start_stream(void));*/ &btstack_audio_portaudio_sink_start_stream,
|
||||
/* void (*stop_stream)(void) */ &btstack_audio_portaudio_sink_stop_stream;
|
||||
/* void (*close)(void); */ &btstack_audio_portaudio_sink_close
|
||||
};
|
||||
|
||||
const btstack_audio_t * btstack_audio_portaudio_get_instance(void){
|
||||
return &btstack_audio_portaudio;
|
||||
static const btstack_audio_source_t btstack_audio_portaudio_source = {
|
||||
/* int (*init)(..);*/ &btstack_audio_portaudio_source_init,
|
||||
/* void (*start_stream(void));*/ &btstack_audio_portaudio_source_start_stream,
|
||||
/* void (*stop_stream)(void) */ &btstack_audio_portaudio_source_stop_stream;
|
||||
/* void (*close)(void); */ &btstack_audio_portaudio_source_close
|
||||
};
|
||||
|
||||
const btstack_audio_sink_t * btstack_audio_portaudio_sink_get_instance(void){
|
||||
return &btstack_audio_portaudio_sink;
|
||||
}
|
||||
|
||||
const btstack_audio_source_t * btstack_audio_portaudio_source_get_instance(void){
|
||||
return &btstack_audio_portaudio_source;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -170,7 +170,8 @@ int main(int argc, const char * argv[]){
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_PORTAUDIO
|
||||
btstack_audio_set_instance(btstack_audio_portaudio_get_instance());
|
||||
btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
|
||||
btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
|
||||
#endif
|
||||
|
||||
// inform about BTstack state
|
||||
|
Loading…
x
Reference in New Issue
Block a user