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) {
324 _host_api_index = Pa_GetDefaultHostApi ();
325 _host_api_name = get_host_api_name_from_index (_host_api_index);
332 PortAudioIO::host_api_list (std::vector<std::string>& api_list)
334 if (!initialize_pa()) return;
336 PaHostApiIndex count = Pa_GetHostApiCount();
338 if (count < 0) return;
340 for (int i = 0; i < count; ++i) {
341 const PaHostApiInfo* info = Pa_GetHostApiInfo (i);
342 if (info->name != NULL) { // possible?
343 api_list.push_back (info->name);
349 PortAudioIO::get_host_api_name_from_index (PaHostApiIndex index)
351 std::vector<std::string> api_list;
352 host_api_list(api_list);
353 return api_list[index];
357 PortAudioIO::set_host_api (const std::string& host_api_name)
359 PaHostApiIndex new_index = get_host_api_index_from_name (host_api_name);
362 DEBUG_AUDIO ("Portaudio: Error setting host API\n");
365 _host_api_index = new_index;
366 _host_api_name = host_api_name;
371 PortAudioIO::get_host_api_index_from_name (const std::string& name)
373 if (!initialize_pa()) return -1;
375 PaHostApiIndex count = Pa_GetHostApiCount();
378 DEBUG_AUDIO ("Host API count < 0\n");
382 for (int i = 0; i < count; ++i) {
383 const PaHostApiInfo* info = Pa_GetHostApiInfo (i);
384 if (info != NULL && info->name != NULL) { // possible?
385 if (name == info->name) {
390 DEBUG_AUDIO (string_compose ("Unable to get host API from name: %1\n", name));
396 PortAudioIO::get_default_input_device ()
398 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
399 if (info == NULL) return -1;
400 return info->defaultInputDevice;
404 PortAudioIO::get_default_output_device ()
406 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
407 if (info == NULL) return -1;
408 return info->defaultOutputDevice;
412 PortAudioIO::clear_device_lists ()
414 for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin (); i != _input_devices.end(); ++i) {
417 _input_devices.clear();
419 for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin (); i != _output_devices.end(); ++i) {
422 _output_devices.clear();
426 PortAudioIO::add_default_devices ()
428 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
429 if (info == NULL) return;
431 const PaDeviceInfo* nfo_i = Pa_GetDeviceInfo(get_default_input_device());
432 const PaDeviceInfo* nfo_o = Pa_GetDeviceInfo(get_default_output_device());
433 if (nfo_i && nfo_o) {
434 _input_devices.insert (std::pair<int, paDevice*> (-1,
435 new paDevice("Default",
436 nfo_i->maxInputChannels,
437 nfo_o->maxOutputChannels
439 _output_devices.insert (std::pair<int, paDevice*> (-1,
440 new paDevice("Default",
441 nfo_i->maxInputChannels,
442 nfo_o->maxOutputChannels
448 PortAudioIO::add_devices ()
450 const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
451 if (info == NULL) return;
453 int n_devices = Pa_GetDeviceCount();
455 DEBUG_AUDIO (string_compose ("PortAudio found %1 devices\n", n_devices));
457 for (int i = 0 ; i < n_devices; ++i) {
458 const PaDeviceInfo* nfo = Pa_GetDeviceInfo(i);
461 if (nfo->hostApi != _host_api_index) continue;
463 DEBUG_AUDIO (string_compose (" (%1) '%2' '%3' in: %4 (lat: %5 .. %6) out: %7 "
464 "(lat: %8 .. %9) sr:%10\n",
468 nfo->maxInputChannels,
469 nfo->defaultLowInputLatency * 1e3,
470 nfo->defaultHighInputLatency * 1e3,
471 nfo->maxOutputChannels,
472 nfo->defaultLowOutputLatency * 1e3,
473 nfo->defaultHighOutputLatency * 1e3,
474 nfo->defaultSampleRate));
476 if ( nfo->maxInputChannels == 0 && nfo->maxOutputChannels == 0) {
480 if (nfo->maxInputChannels > 0) {
481 _input_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
483 nfo->maxInputChannels,
484 nfo->maxOutputChannels
487 if (nfo->maxOutputChannels > 0) {
488 _output_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
490 nfo->maxInputChannels,
491 nfo->maxOutputChannels
498 PortAudioIO::discover()
500 DEBUG_AUDIO ("PortAudio: discover\n");
501 if (!initialize_pa()) return;
503 clear_device_lists ();
508 PortAudioIO::pcm_stop ()
511 Pa_CloseStream (_stream);
515 _capture_channels = 0;
516 _playback_channels = 0;
517 _cur_sample_rate = 0;
518 _cur_input_latency = 0;
519 _cur_output_latency = 0;
521 free (_input_buffer); _input_buffer = NULL;
522 free (_output_buffer); _output_buffer = NULL;
527 PortAudioIO::pcm_start()
529 PaError err = Pa_StartStream (_stream);
531 if (err != paNoError) {
539 static uint32_t lower_power_of_two (uint32_t v) {
552 PortAudioIO::pcm_setup (
553 int device_input, int device_output,
554 double sample_rate, uint32_t samples_per_period)
558 PaError err = paNoError;
559 const PaDeviceInfo *nfo_in;
560 const PaDeviceInfo *nfo_out;
561 const PaStreamInfo *nfo_s;
563 if (!initialize_pa()) {
564 DEBUG_AUDIO ("PortAudio Initialization Failed\n");
568 if (device_input == -1) {
569 device_input = get_default_input_device ();
571 if (device_output == -1) {
572 device_output = get_default_output_device ();
575 _capture_channels = 0;
576 _playback_channels = 0;
577 _cur_sample_rate = 0;
578 _cur_input_latency = 0;
579 _cur_output_latency = 0;
581 DEBUG_AUDIO (string_compose (
582 "PortAudio Device IDs: i:%1 o:%2\n", device_input, device_output));
584 nfo_in = Pa_GetDeviceInfo(device_input);
585 nfo_out = Pa_GetDeviceInfo(device_output);
587 if (!nfo_in && ! nfo_out) {
588 DEBUG_AUDIO ("PortAudio Cannot Query Device Info\n");
593 _capture_channels = nfo_in->maxInputChannels;
596 _playback_channels = nfo_out->maxOutputChannels;
599 if(_capture_channels == 0 && _playback_channels == 0) {
600 DEBUG_AUDIO ("PortAudio no input or output channels.\n");
605 // pa_mac_core_blocking.c pa_stable_v19_20140130
606 // BUG: ringbuffer alloc requires power-of-two chn count.
607 if ((_capture_channels & (_capture_channels - 1)) != 0) {
609 "Adjusted capture channels to power of two (portaudio rb bug)\n");
610 _capture_channels = lower_power_of_two (_capture_channels);
613 if ((_playback_channels & (_playback_channels - 1)) != 0) {
615 "Adjusted capture channels to power of two (portaudio rb bug)\n");
616 _playback_channels = lower_power_of_two (_playback_channels);
620 DEBUG_AUDIO (string_compose ("PortAudio Channels: in:%1 out:%2\n",
622 _playback_channels));
624 PaStreamParameters inputParam;
625 PaStreamParameters outputParam;
628 inputParam.device = device_input;
629 inputParam.channelCount = _capture_channels;
630 #ifdef INTERLEAVED_INPUT
631 inputParam.sampleFormat = paFloat32;
633 inputParam.sampleFormat = paFloat32 | paNonInterleaved;
635 inputParam.suggestedLatency = nfo_in->defaultLowInputLatency;
636 inputParam.hostApiSpecificStreamInfo = NULL;
640 outputParam.device = device_output;
641 outputParam.channelCount = _playback_channels;
642 #ifdef INTERLEAVED_OUTPUT
643 outputParam.sampleFormat = paFloat32;
645 outputParam.sampleFormat = paFloat32 | paNonInterleaved;
647 outputParam.suggestedLatency = nfo_out->defaultLowOutputLatency;
648 outputParam.hostApiSpecificStreamInfo = NULL;
651 // XXX re-consider using callback API, testing needed.
652 err = Pa_OpenStream (
654 _capture_channels > 0 ? &inputParam: NULL,
655 _playback_channels > 0 ? &outputParam: NULL,
661 if (err != paNoError) {
662 DEBUG_AUDIO ("PortAudio failed to start stream.\n");
666 nfo_s = Pa_GetStreamInfo (_stream);
668 DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
673 _cur_sample_rate = nfo_s->sampleRate;
674 _cur_input_latency = nfo_s->inputLatency * _cur_sample_rate;
675 _cur_output_latency = nfo_s->outputLatency * _cur_sample_rate;
677 DEBUG_AUDIO (string_compose ("PA Sample Rate %1 SPS\n", _cur_sample_rate));
679 DEBUG_AUDIO (string_compose ("PA Input Latency %1ms, %2 spl\n",
680 1e3 * nfo_s->inputLatency,
681 _cur_input_latency));
683 DEBUG_AUDIO (string_compose ("PA Output Latency %1ms, %2 spl\n",
684 1e3 * nfo_s->outputLatency,
685 _cur_output_latency));
689 if (_capture_channels > 0) {
690 _input_buffer = (float*) malloc (samples_per_period * _capture_channels * sizeof(float));
691 if (!_input_buffer) {
692 DEBUG_AUDIO ("PortAudio failed to allocate input buffer.\n");
698 if (_playback_channels > 0) {
699 _output_buffer = (float*) calloc (samples_per_period * _playback_channels, sizeof(float));
700 if (!_output_buffer) {
701 DEBUG_AUDIO ("PortAudio failed to allocate output buffer.\n");
710 _capture_channels = 0;
711 _playback_channels = 0;
712 free (_input_buffer); _input_buffer = NULL;
713 free (_output_buffer); _output_buffer = NULL;
718 PortAudioIO::next_cycle (uint32_t n_samples)
722 err = Pa_IsStreamActive (_stream);
724 // 0: inactive / aborted
729 // TODO, check drift.. process part with larger capacity first.
730 // Pa_GetStreamReadAvailable(_stream) < Pa_GetStreamWriteAvailable(_stream)
732 if (_playback_channels > 0) {
733 err = Pa_WriteStream (_stream, _output_buffer, n_samples);
734 if (err) xrun = true;
737 if (_capture_channels > 0) {
738 err = Pa_ReadStream (_stream, _input_buffer, n_samples);
740 memset (_input_buffer, 0, sizeof(float) * n_samples * _capture_channels);
750 PortAudioIO::get_input_channel_name (int device_id, uint32_t channel) const
753 const char* channel_name;
755 // This will return an error for non-ASIO devices so no need to check if
756 // the device_id corresponds to an ASIO device.
757 PaError err = PaAsio_GetInputChannelName (device_id, channel, &channel_name);
759 if (err == paNoError) {
761 string_compose ("Input channel name for device %1, channel %2 is %3\n",
768 return std::string();
772 PortAudioIO::get_output_channel_name (int device_id, uint32_t channel) const
775 const char* channel_name;
777 PaError err = PaAsio_GetOutputChannelName (device_id, channel, &channel_name);
779 if (err == paNoError) {
781 string_compose ("Output channel name for device %1, channel %2 is %3\n",
788 return std::string();
791 #ifdef INTERLEAVED_INPUT
794 PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
796 assert(chn < _capture_channels);
797 const uint32_t stride = _capture_channels;
798 float *ptr = _input_buffer + chn;
799 while (n_samples-- > 0) {
809 PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
811 assert(chn < _capture_channels);
812 memcpy((void*)input, &(_input_buffer[chn * n_samples]), n_samples * sizeof(float));
819 #ifdef INTERLEAVED_OUTPUT
822 PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
824 assert(chn < _playback_channels);
825 const uint32_t stride = _playback_channels;
826 float *ptr = _output_buffer + chn;
827 while (n_samples-- > 0) {
837 PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
839 assert(chn < _playback_channels);
840 memcpy((void*)&(_output_buffer[chn * n_samples]), (void*)output, n_samples * sizeof(float));