2 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "portaudio_io.h"
31 #include "pbd/compose.h"
35 #define INTERLEAVED_INPUT
36 #define INTERLEAVED_OUTPUT
38 using namespace ARDOUR;
40 PortAudioIO::PortAudioIO ()
42 , _initialized (false)
43 , _capture_channels (0)
44 , _playback_channels (0)
48 , _cur_sample_rate (0)
49 , _cur_input_latency (0)
50 , _cur_output_latency (0)
55 PortAudioIO::~PortAudioIO ()
64 clear_device_lists ();
66 free (_input_buffer); _input_buffer = NULL;
67 free (_output_buffer); _output_buffer = NULL;
71 PortAudioIO::control_app_name (int device_id) const
73 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
77 DEBUG_AUDIO (string_compose ("Unable to determine Host API from index %1\n",
82 PaHostApiTypeId type_id = info->type;
85 if (type_id == paASIO) {
86 // is this used for anything, or just acts as a boolean?
87 return "PortaudioASIO";
95 PortAudioIO::launch_control_app (int device_id)
98 PaError err = PaAsio_ShowControlPanel (device_id, NULL);
100 if (err != paNoError) {
102 DEBUG_AUDIO (string_compose (
103 "Unable to show control panel for device with index %1\n", device_id));
109 PortAudioIO::available_sample_rates(int device_id, std::vector<float>& sampleRates)
111 static const float ardourRates[] = { 8000.0, 22050.0, 24000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0};
113 if (!initialize_pa()) return -1;
115 // TODO use separate int device_input, int device_output ?!
116 if (device_id == -1) {
117 device_id = get_default_input_device ();
121 string_compose ("Querying Samplerates for device %1\n", device_id));
124 const PaDeviceInfo* nfo = Pa_GetDeviceInfo(device_id);
127 PaStreamParameters inputParam;
128 PaStreamParameters outputParam;
130 inputParam.device = device_id;
131 inputParam.channelCount = nfo->maxInputChannels;
132 inputParam.sampleFormat = paFloat32;
133 inputParam.suggestedLatency = 0;
134 inputParam.hostApiSpecificStreamInfo = 0;
136 outputParam.device = device_id;
137 outputParam.channelCount = nfo->maxOutputChannels;
138 outputParam.sampleFormat = paFloat32;
139 outputParam.suggestedLatency = 0;
140 outputParam.hostApiSpecificStreamInfo = 0;
142 for (uint32_t i = 0; i < sizeof(ardourRates)/sizeof(float); ++i) {
143 if (paFormatIsSupported == Pa_IsFormatSupported(
144 nfo->maxInputChannels > 0 ? &inputParam : NULL,
145 nfo->maxOutputChannels > 0 ? &outputParam : NULL,
147 sampleRates.push_back (ardourRates[i]);
152 if (sampleRates.empty()) {
153 // fill in something..
154 sampleRates.push_back (44100.0);
155 sampleRates.push_back (48000.0);
163 PortAudioIO::get_asio_buffer_properties (int device_id,
164 long& min_size_frames,
165 long& max_size_frames,
166 long& preferred_size_frames,
169 // we shouldn't really need all these checks but it shouldn't hurt
170 const PaDeviceInfo* device_info = Pa_GetDeviceInfo(device_id);
173 DEBUG_AUDIO (string_compose (
174 "Unable to get device info from device index %1\n", device_id));
178 const PaHostApiInfo* info = Pa_GetHostApiInfo (device_info->hostApi);
181 DEBUG_AUDIO (string_compose (
182 "Unable to determine Host API from device index %1\n", device_id));
186 if (info->type != paASIO) {
187 DEBUG_AUDIO (string_compose (
188 "ERROR device_id %1 is not an ASIO device\n", device_id));
192 PaError err = PaAsio_GetAvailableBufferSizes (device_id,
195 &preferred_size_frames,
198 if (err != paNoError) {
199 DEBUG_AUDIO (string_compose (
200 "Unable to determine available buffer sizes for device %1\n", device_id));
207 PortAudioIO::get_asio_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes)
209 long min_size_frames = 0;
210 long max_size_frames = 0;
211 long preferred_size_frames = 0;
212 long granularity = 0;
214 if (!get_asio_buffer_properties (device_id,
217 preferred_size_frames,
219 DEBUG_AUDIO (string_compose (
220 "Unable to get device buffer properties from device index %1\n", device_id));
224 DEBUG_AUDIO (string_compose ("ASIO buffer properties for device %1, "
225 "min_size_frames: %2, max_size_frames: %3, "
226 "preferred_size_frames: %4, granularity: %5\n",
230 preferred_size_frames,
233 #ifdef USE_ASIO_MIN_MAX_BUFFER_SIZES
234 if (min_size_frames >= max_size_frames) {
235 buffer_sizes.push_back (preferred_size_frames);
239 long buffer_size = min_size_frames;
240 while (buffer_size <= max_size_frames) {
241 buffer_sizes.push_back (buffer_size);
243 if (granularity <= 0) {
244 // buffer sizes are power of 2
245 buffer_size = buffer_size * 2;
247 buffer_size += granularity;
251 buffer_sizes.push_back (preferred_size_frames);
258 PortAudioIO::get_default_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes)
260 static const uint32_t ardourSizes[] = { 64, 128, 256, 512, 1024, 2048, 4096 };
261 for(uint32_t i = 0; i < sizeof(ardourSizes)/sizeof(uint32_t); ++i) {
262 buffer_sizes.push_back (ardourSizes[i]);
268 PortAudioIO::available_buffer_sizes(int device_id, std::vector<uint32_t>& buffer_sizes)
271 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
274 DEBUG_AUDIO (string_compose ("Unable to determine Host API from index %1\n",
279 PaHostApiTypeId type_id = info->type;
281 if (type_id == paASIO) {
282 if (get_asio_buffer_sizes (device_id, buffer_sizes)) {
288 get_default_buffer_sizes (device_id, buffer_sizes);
294 PortAudioIO::input_device_list(std::map<int, std::string> &devices) const
296 for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin ();
297 i != _input_devices.end ();
299 devices.insert (std::pair<int, std::string>(i->first, Glib::locale_to_utf8(i->second->name)));
304 PortAudioIO::output_device_list(std::map<int, std::string> &devices) const
306 for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin ();
307 i != _output_devices.end ();
309 devices.insert (std::pair<int, std::string>(i->first, Glib::locale_to_utf8(i->second->name)));
314 PortAudioIO::initialize_pa ()
316 PaError err = paNoError;
319 err = Pa_Initialize();
320 if (err != paNoError) {
330 PortAudioIO::host_api_list (std::vector<std::string>& api_list)
332 if (!initialize_pa()) return;
334 PaHostApiIndex count = Pa_GetHostApiCount();
336 if (count < 0) return;
338 for (int i = 0; i < count; ++i) {
339 const PaHostApiInfo* info = Pa_GetHostApiInfo (i);
340 if (info->name != NULL) { // possible?
341 api_list.push_back (info->name);
347 PortAudioIO::set_host_api (const std::string& host_api_name)
349 PaHostApiIndex new_index = get_host_api_index_from_name (host_api_name);
352 DEBUG_AUDIO ("Portaudio: Error setting host API\n");
355 _host_api_index = new_index;
356 _host_api_name = host_api_name;
361 PortAudioIO::get_host_api_index_from_name (const std::string& name)
363 if (!initialize_pa()) return -1;
365 PaHostApiIndex count = Pa_GetHostApiCount();
368 DEBUG_AUDIO ("Host API count < 0\n");
372 for (int i = 0; i < count; ++i) {
373 const PaHostApiInfo* info = Pa_GetHostApiInfo (i);
374 if (info != NULL && info->name != NULL) { // possible?
375 if (name == info->name) {
380 DEBUG_AUDIO (string_compose ("Unable to get host API from name: %1\n", name));
386 PortAudioIO::get_default_input_device ()
388 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
389 if (info == NULL) return -1;
390 return info->defaultInputDevice;
394 PortAudioIO::get_default_output_device ()
396 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
397 if (info == NULL) return -1;
398 return info->defaultOutputDevice;
402 PortAudioIO::clear_device_lists ()
404 for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin (); i != _input_devices.end(); ++i) {
407 _input_devices.clear();
409 for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin (); i != _output_devices.end(); ++i) {
412 _output_devices.clear();
416 PortAudioIO::add_default_devices ()
418 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
419 if (info == NULL) return;
421 const PaDeviceInfo* nfo_i = Pa_GetDeviceInfo(get_default_input_device());
422 const PaDeviceInfo* nfo_o = Pa_GetDeviceInfo(get_default_output_device());
423 if (nfo_i && nfo_o) {
424 _input_devices.insert (std::pair<int, paDevice*> (-1,
425 new paDevice("Default",
426 nfo_i->maxInputChannels,
427 nfo_o->maxOutputChannels
429 _output_devices.insert (std::pair<int, paDevice*> (-1,
430 new paDevice("Default",
431 nfo_i->maxInputChannels,
432 nfo_o->maxOutputChannels
438 PortAudioIO::add_devices ()
440 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
441 if (info == NULL) return;
443 int n_devices = Pa_GetDeviceCount();
445 DEBUG_AUDIO (string_compose ("PortAudio found %1 devices\n", n_devices));
447 for (int i = 0 ; i < n_devices; ++i) {
448 const PaDeviceInfo* nfo = Pa_GetDeviceInfo(i);
451 if (nfo->hostApi != _host_api_index) continue;
453 DEBUG_AUDIO (string_compose (" (%1) '%2' '%3' in: %4 (lat: %5 .. %6) out: %7 "
454 "(lat: %8 .. %9) sr:%10\n",
458 nfo->maxInputChannels,
459 nfo->defaultLowInputLatency * 1e3,
460 nfo->defaultHighInputLatency * 1e3,
461 nfo->maxOutputChannels,
462 nfo->defaultLowOutputLatency * 1e3,
463 nfo->defaultHighOutputLatency * 1e3,
464 nfo->defaultSampleRate));
466 if ( nfo->maxInputChannels == 0 && nfo->maxOutputChannels == 0) {
470 if (nfo->maxInputChannels > 0) {
471 _input_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
473 nfo->maxInputChannels,
474 nfo->maxOutputChannels
477 if (nfo->maxOutputChannels > 0) {
478 _output_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
480 nfo->maxInputChannels,
481 nfo->maxOutputChannels
488 PortAudioIO::discover()
490 DEBUG_AUDIO ("PortAudio: discover\n");
491 if (!initialize_pa()) return;
493 clear_device_lists ();
494 add_default_devices ();
499 PortAudioIO::pcm_stop ()
502 Pa_CloseStream (_stream);
506 _capture_channels = 0;
507 _playback_channels = 0;
508 _cur_sample_rate = 0;
509 _cur_input_latency = 0;
510 _cur_output_latency = 0;
512 free (_input_buffer); _input_buffer = NULL;
513 free (_output_buffer); _output_buffer = NULL;
518 PortAudioIO::pcm_start()
520 PaError err = Pa_StartStream (_stream);
522 if (err != paNoError) {
530 static uint32_t lower_power_of_two (uint32_t v) {
543 PortAudioIO::pcm_setup (
544 int device_input, int device_output,
545 double sample_rate, uint32_t samples_per_period)
549 PaError err = paNoError;
550 const PaDeviceInfo *nfo_in;
551 const PaDeviceInfo *nfo_out;
552 const PaStreamInfo *nfo_s;
554 if (!initialize_pa()) {
555 DEBUG_AUDIO ("PortAudio Initialization Failed\n");
559 if (device_input == -1) {
560 device_input = get_default_input_device ();
562 if (device_output == -1) {
563 device_output = get_default_output_device ();
566 _capture_channels = 0;
567 _playback_channels = 0;
568 _cur_sample_rate = 0;
569 _cur_input_latency = 0;
570 _cur_output_latency = 0;
572 DEBUG_AUDIO (string_compose (
573 "PortAudio Device IDs: i:%1 o:%2\n", device_input, device_output));
575 nfo_in = Pa_GetDeviceInfo(device_input);
576 nfo_out = Pa_GetDeviceInfo(device_output);
578 if (!nfo_in && ! nfo_out) {
579 DEBUG_AUDIO ("PortAudio Cannot Query Device Info\n");
584 _capture_channels = nfo_in->maxInputChannels;
587 _playback_channels = nfo_out->maxOutputChannels;
590 if(_capture_channels == 0 && _playback_channels == 0) {
591 DEBUG_AUDIO ("PortAudio no input or output channels.\n");
596 // pa_mac_core_blocking.c pa_stable_v19_20140130
597 // BUG: ringbuffer alloc requires power-of-two chn count.
598 if ((_capture_channels & (_capture_channels - 1)) != 0) {
600 "Adjusted capture channels to power of two (portaudio rb bug)\n");
601 _capture_channels = lower_power_of_two (_capture_channels);
604 if ((_playback_channels & (_playback_channels - 1)) != 0) {
606 "Adjusted capture channels to power of two (portaudio rb bug)\n");
607 _playback_channels = lower_power_of_two (_playback_channels);
611 DEBUG_AUDIO (string_compose ("PortAudio Channels: in:%1 out:%2\n",
613 _playback_channels));
615 PaStreamParameters inputParam;
616 PaStreamParameters outputParam;
619 inputParam.device = device_input;
620 inputParam.channelCount = _capture_channels;
621 #ifdef INTERLEAVED_INPUT
622 inputParam.sampleFormat = paFloat32;
624 inputParam.sampleFormat = paFloat32 | paNonInterleaved;
626 inputParam.suggestedLatency = nfo_in->defaultLowInputLatency;
627 inputParam.hostApiSpecificStreamInfo = NULL;
631 outputParam.device = device_output;
632 outputParam.channelCount = _playback_channels;
633 #ifdef INTERLEAVED_OUTPUT
634 outputParam.sampleFormat = paFloat32;
636 outputParam.sampleFormat = paFloat32 | paNonInterleaved;
638 outputParam.suggestedLatency = nfo_out->defaultLowOutputLatency;
639 outputParam.hostApiSpecificStreamInfo = NULL;
642 // XXX re-consider using callback API, testing needed.
643 err = Pa_OpenStream (
645 _capture_channels > 0 ? &inputParam: NULL,
646 _playback_channels > 0 ? &outputParam: NULL,
652 if (err != paNoError) {
653 DEBUG_AUDIO ("PortAudio failed to start stream.\n");
657 nfo_s = Pa_GetStreamInfo (_stream);
659 DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
664 _cur_sample_rate = nfo_s->sampleRate;
665 _cur_input_latency = nfo_s->inputLatency * _cur_sample_rate;
666 _cur_output_latency = nfo_s->outputLatency * _cur_sample_rate;
668 DEBUG_AUDIO (string_compose ("PA Sample Rate %1 SPS\n", _cur_sample_rate));
670 DEBUG_AUDIO (string_compose ("PA Input Latency %1ms, %2 spl\n",
671 1e3 * nfo_s->inputLatency,
672 _cur_input_latency));
674 DEBUG_AUDIO (string_compose ("PA Output Latency %1ms, %2 spl\n",
675 1e3 * nfo_s->outputLatency,
676 _cur_output_latency));
680 if (_capture_channels > 0) {
681 _input_buffer = (float*) malloc (samples_per_period * _capture_channels * sizeof(float));
682 if (!_input_buffer) {
683 DEBUG_AUDIO ("PortAudio failed to allocate input buffer.\n");
689 if (_playback_channels > 0) {
690 _output_buffer = (float*) calloc (samples_per_period * _playback_channels, sizeof(float));
691 if (!_output_buffer) {
692 DEBUG_AUDIO ("PortAudio failed to allocate output buffer.\n");
701 _capture_channels = 0;
702 _playback_channels = 0;
703 free (_input_buffer); _input_buffer = NULL;
704 free (_output_buffer); _output_buffer = NULL;
709 PortAudioIO::next_cycle (uint32_t n_samples)
713 err = Pa_IsStreamActive (_stream);
715 // 0: inactive / aborted
720 // TODO, check drift.. process part with larger capacity first.
721 // Pa_GetStreamReadAvailable(_stream) < Pa_GetStreamWriteAvailable(_stream)
723 if (_playback_channels > 0) {
724 err = Pa_WriteStream (_stream, _output_buffer, n_samples);
725 if (err) xrun = true;
728 if (_capture_channels > 0) {
729 err = Pa_ReadStream (_stream, _input_buffer, n_samples);
731 memset (_input_buffer, 0, sizeof(float) * n_samples * _capture_channels);
741 #ifdef INTERLEAVED_INPUT
744 PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
746 assert(chn < _capture_channels);
747 const uint32_t stride = _capture_channels;
748 float *ptr = _input_buffer + chn;
749 while (n_samples-- > 0) {
759 PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
761 assert(chn < _capture_channels);
762 memcpy((void*)input, &(_input_buffer[chn * n_samples]), n_samples * sizeof(float));
769 #ifdef INTERLEAVED_OUTPUT
772 PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
774 assert(chn < _playback_channels);
775 const uint32_t stride = _playback_channels;
776 float *ptr = _output_buffer + chn;
777 while (n_samples-- > 0) {
787 PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
789 assert(chn < _playback_channels);
790 memcpy((void*)&(_output_buffer[chn * n_samples]), (void*)output, n_samples * sizeof(float));