Add code to use ASIO supplied min/max buffer sizes but don't use it
[ardour.git] / libs / backends / portaudio / portaudio_io.cc
1 /*
2  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <glibmm.h>
25 #include "portaudio_io.h"
26
27 #ifdef WITH_ASIO
28 #include "pa_asio.h"
29 #endif
30
31 #include "pbd/compose.h"
32
33 #include "debug.h"
34
35 #define INTERLEAVED_INPUT
36 #define INTERLEAVED_OUTPUT
37
38 using namespace ARDOUR;
39
40 PortAudioIO::PortAudioIO ()
41         : _state (-1)
42         , _initialized (false)
43         , _capture_channels (0)
44         , _playback_channels (0)
45         , _stream (0)
46         , _input_buffer (0)
47         , _output_buffer (0)
48         , _cur_sample_rate (0)
49         , _cur_input_latency (0)
50         , _cur_output_latency (0)
51         , _host_api_index(-1)
52 {
53 }
54
55 PortAudioIO::~PortAudioIO ()
56 {
57         if (_state == 0) {
58                 pcm_stop();
59         }
60         if (_initialized) {
61                 Pa_Terminate();
62         }
63
64         clear_device_lists ();
65
66         free (_input_buffer); _input_buffer = NULL;
67         free (_output_buffer); _output_buffer = NULL;
68 }
69
70 std::string
71 PortAudioIO::control_app_name (int device_id) const
72 {
73         const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
74         std::string app_name;
75
76         if (info == NULL) {
77                 DEBUG_AUDIO (string_compose ("Unable to determine Host API from index %1\n",
78                                              _host_api_index));
79                 return app_name;
80         }
81
82         PaHostApiTypeId type_id = info->type;
83
84 #ifdef WITH_ASIO
85         if (type_id == paASIO) {
86                 // is this used for anything, or just acts as a boolean?
87                 return "PortaudioASIO";
88         }
89 #endif
90
91         return app_name;
92 }
93
94 void
95 PortAudioIO::launch_control_app (int device_id)
96 {
97 #ifdef WITH_ASIO
98         PaError err = PaAsio_ShowControlPanel (device_id, NULL);
99
100         if (err != paNoError) {
101                 // error << ?
102                 DEBUG_AUDIO (string_compose (
103                     "Unable to show control panel for device with index %1\n", device_id));
104         }
105 #endif
106 }
107
108 int
109 PortAudioIO::available_sample_rates(int device_id, std::vector<float>& sampleRates)
110 {
111         static const float ardourRates[] = { 8000.0, 22050.0, 24000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0};
112
113         if (!initialize_pa()) return -1;
114
115         // TODO use  separate int device_input, int device_output ?!
116         if (device_id == -1) {
117                 device_id = get_default_input_device ();
118         }
119
120         DEBUG_AUDIO (
121             string_compose ("Querying Samplerates for device %1\n", device_id));
122
123         sampleRates.clear();
124         const PaDeviceInfo* nfo = Pa_GetDeviceInfo(device_id);
125
126         if (nfo) {
127                 PaStreamParameters inputParam;
128                 PaStreamParameters outputParam;
129
130                 inputParam.device = device_id;
131                 inputParam.channelCount = nfo->maxInputChannels;
132                 inputParam.sampleFormat = paFloat32;
133                 inputParam.suggestedLatency = 0;
134                 inputParam.hostApiSpecificStreamInfo = 0;
135
136                 outputParam.device = device_id;
137                 outputParam.channelCount = nfo->maxOutputChannels;
138                 outputParam.sampleFormat = paFloat32;
139                 outputParam.suggestedLatency = 0;
140                 outputParam.hostApiSpecificStreamInfo = 0;
141
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,
146                                                 ardourRates[i])) {
147                                 sampleRates.push_back (ardourRates[i]);
148                         }
149                 }
150         }
151
152         if (sampleRates.empty()) {
153                 // fill in something..
154                 sampleRates.push_back (44100.0);
155                 sampleRates.push_back (48000.0);
156         }
157
158         return 0;
159 }
160
161 #ifdef WITH_ASIO
162 bool
163 PortAudioIO::get_asio_buffer_properties (int device_id,
164                                          long& min_size_frames,
165                                          long& max_size_frames,
166                                          long& preferred_size_frames,
167                                          long& granularity)
168 {
169         // we shouldn't really need all these checks but it shouldn't hurt
170         const PaDeviceInfo* device_info = Pa_GetDeviceInfo(device_id);
171
172         if (!device_info) {
173                 DEBUG_AUDIO (string_compose (
174                     "Unable to get device info from device index %1\n", device_id));
175                 return false;
176         }
177
178         const PaHostApiInfo* info = Pa_GetHostApiInfo (device_info->hostApi);
179
180         if (info == NULL) {
181                 DEBUG_AUDIO (string_compose (
182                     "Unable to determine Host API from device index %1\n", device_id));
183                 return false;
184         }
185
186         if (info->type != paASIO) {
187                 DEBUG_AUDIO (string_compose (
188                     "ERROR device_id %1 is not an ASIO device\n", device_id));
189                 return false;
190         }
191
192         PaError err = PaAsio_GetAvailableBufferSizes (device_id,
193                                                       &min_size_frames,
194                                                       &max_size_frames,
195                                                       &preferred_size_frames,
196                                                       &granularity);
197
198         if (err != paNoError) {
199                 DEBUG_AUDIO (string_compose (
200                     "Unable to determine available buffer sizes for device %1\n", device_id));
201                 return false;
202         }
203         return true;
204 }
205
206 bool
207 PortAudioIO::get_asio_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes)
208 {
209         long min_size_frames = 0;
210         long max_size_frames = 0;
211         long preferred_size_frames = 0;
212         long granularity = 0;
213
214         if (!get_asio_buffer_properties (device_id,
215                                          min_size_frames,
216                                          max_size_frames,
217                                          preferred_size_frames,
218                                          granularity)) {
219                 DEBUG_AUDIO (string_compose (
220                     "Unable to get device buffer properties from device index %1\n", device_id));
221                 return false;
222         }
223
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",
227                                      device_id,
228                                      min_size_frames,
229                                      max_size_frames,
230                                      preferred_size_frames,
231                                      granularity));
232
233 #ifdef USE_ASIO_MIN_MAX_BUFFER_SIZES
234         if (min_size_frames >= max_size_frames) {
235                 buffer_sizes.push_back (preferred_size_frames);
236                 return true;
237         }
238
239         long buffer_size = min_size_frames;
240         while (buffer_size <= max_size_frames) {
241                 buffer_sizes.push_back (buffer_size);
242
243                 if (granularity <= 0) {
244                         // buffer sizes are power of 2
245                         buffer_size = buffer_size * 2;
246                 } else {
247                         buffer_size += granularity;
248                 }
249         }
250 #else
251         buffer_sizes.push_back (preferred_size_frames);
252 #endif
253         return true;
254 }
255 #endif
256
257 bool
258 PortAudioIO::get_default_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes)
259 {
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]);
263         }
264         return true;
265 }
266
267 int
268 PortAudioIO::available_buffer_sizes(int device_id, std::vector<uint32_t>& buffer_sizes)
269 {
270 #ifdef WITH_ASIO
271         const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
272
273         if (info == NULL) {
274                 DEBUG_AUDIO (string_compose ("Unable to determine Host API from index %1\n",
275                                              _host_api_index));
276                 return -1;
277         }
278
279         PaHostApiTypeId type_id = info->type;
280
281         if (type_id == paASIO) {
282                 if (get_asio_buffer_sizes (device_id, buffer_sizes)) {
283                         return 0;
284                 }
285         }
286 #endif
287
288         get_default_buffer_sizes (device_id, buffer_sizes);
289
290         return 0;
291 }
292
293 void
294 PortAudioIO::input_device_list(std::map<int, std::string> &devices) const
295 {
296         for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin ();
297              i != _input_devices.end ();
298              ++i) {
299                 devices.insert (std::pair<int, std::string>(i->first, Glib::locale_to_utf8(i->second->name)));
300         }
301 }
302
303 void
304 PortAudioIO::output_device_list(std::map<int, std::string> &devices) const
305 {
306         for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin ();
307              i != _output_devices.end ();
308              ++i) {
309                 devices.insert (std::pair<int, std::string>(i->first, Glib::locale_to_utf8(i->second->name)));
310         }
311 }
312
313 bool
314 PortAudioIO::initialize_pa ()
315 {
316         PaError err = paNoError;
317
318         if (!_initialized) {
319                 err = Pa_Initialize();
320                 if (err != paNoError) {
321                         return false;
322                 }
323                 _initialized = true;
324         }
325
326         return true;
327 }
328
329 void
330 PortAudioIO::host_api_list (std::vector<std::string>& api_list)
331 {
332         if (!initialize_pa()) return;
333
334         PaHostApiIndex count = Pa_GetHostApiCount();
335
336         if (count < 0) return;
337
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);
342                 }
343         }
344 }
345
346 bool
347 PortAudioIO::set_host_api (const std::string& host_api_name)
348 {
349         PaHostApiIndex new_index = get_host_api_index_from_name (host_api_name);
350
351         if (new_index < 0) {
352                 DEBUG_AUDIO ("Portaudio: Error setting host API\n");
353                 return false;
354         }
355         _host_api_index = new_index;
356         _host_api_name = host_api_name;
357         return true;
358 }
359
360 PaHostApiIndex
361 PortAudioIO::get_host_api_index_from_name (const std::string& name)
362 {
363         if (!initialize_pa()) return -1;
364
365         PaHostApiIndex count = Pa_GetHostApiCount();
366
367         if (count < 0) {
368                 DEBUG_AUDIO ("Host API count < 0\n");
369                 return -1;
370         }
371
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) {
376                                 return i;
377                         }
378                 }
379         }
380         DEBUG_AUDIO (string_compose ("Unable to get host API from name: %1\n", name));
381
382         return -1;
383 }
384
385 PaDeviceIndex
386 PortAudioIO::get_default_input_device ()
387 {
388         const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
389         if (info == NULL) return -1;
390         return info->defaultInputDevice;
391 }
392
393 PaDeviceIndex
394 PortAudioIO::get_default_output_device ()
395 {
396         const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
397         if (info == NULL) return -1;
398         return info->defaultOutputDevice;
399 }
400
401 void
402 PortAudioIO::clear_device_lists ()
403 {
404         for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin (); i != _input_devices.end(); ++i) {
405                 delete i->second;
406         }
407         _input_devices.clear();
408
409         for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin (); i != _output_devices.end(); ++i) {
410                 delete i->second;
411         }
412         _output_devices.clear();
413 }
414
415 void
416 PortAudioIO::add_default_devices ()
417 {
418         const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
419         if (info == NULL) return;
420
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
428                                                 )));
429                 _output_devices.insert (std::pair<int, paDevice*> (-1,
430                                         new paDevice("Default",
431                                                 nfo_i->maxInputChannels,
432                                                 nfo_o->maxOutputChannels
433                                                 )));
434         }
435 }
436
437 void
438 PortAudioIO::add_devices ()
439 {
440         const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
441         if (info == NULL) return;
442
443         int n_devices = Pa_GetDeviceCount();
444
445         DEBUG_AUDIO (string_compose ("PortAudio found %1 devices\n", n_devices));
446
447         for (int i = 0 ; i < n_devices; ++i) {
448                 const PaDeviceInfo* nfo = Pa_GetDeviceInfo(i);
449
450                 if (!nfo) continue;
451                 if (nfo->hostApi != _host_api_index) continue;
452
453                 DEBUG_AUDIO (string_compose (" (%1) '%2' '%3' in: %4 (lat: %5 .. %6) out: %7 "
454                                              "(lat: %8 .. %9) sr:%10\n",
455                                              i,
456                                              info->name,
457                                              nfo->name,
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));
465
466                 if ( nfo->maxInputChannels == 0 && nfo->maxOutputChannels == 0) {
467                         continue;
468                 }
469
470                 if (nfo->maxInputChannels > 0) {
471                         _input_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
472                                                         nfo->name,
473                                                         nfo->maxInputChannels,
474                                                         nfo->maxOutputChannels
475                                                         )));
476                 }
477                 if (nfo->maxOutputChannels > 0) {
478                         _output_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
479                                                         nfo->name,
480                                                         nfo->maxInputChannels,
481                                                         nfo->maxOutputChannels
482                                                         )));
483                 }
484         }
485 }
486
487 void
488 PortAudioIO::discover()
489 {
490         DEBUG_AUDIO ("PortAudio: discover\n");
491         if (!initialize_pa()) return;
492
493         clear_device_lists ();
494         add_default_devices ();
495         add_devices ();
496 }
497
498 void
499 PortAudioIO::pcm_stop ()
500 {
501         if (_stream) {
502                 Pa_CloseStream (_stream);
503         }
504         _stream = NULL;
505
506         _capture_channels = 0;
507         _playback_channels = 0;
508         _cur_sample_rate = 0;
509         _cur_input_latency = 0;
510         _cur_output_latency = 0;
511
512         free (_input_buffer); _input_buffer = NULL;
513         free (_output_buffer); _output_buffer = NULL;
514         _state = -1;
515 }
516
517 int
518 PortAudioIO::pcm_start()
519 {
520         PaError err = Pa_StartStream (_stream);
521
522         if (err != paNoError) {
523                 _state = -1;
524                 return -1;
525         }
526         return 0;
527 }
528
529 #ifdef __APPLE__
530 static uint32_t lower_power_of_two (uint32_t v) {
531         v--;
532         v |= v >> 1;
533         v |= v >> 2;
534         v |= v >> 4;
535         v |= v >> 8;
536         v |= v >> 16;
537         v++;
538         return v >> 1;
539 }
540 #endif
541
542 int
543 PortAudioIO::pcm_setup (
544                 int device_input, int device_output,
545                 double sample_rate, uint32_t samples_per_period)
546 {
547         _state = -2;
548
549         PaError err = paNoError;
550         const PaDeviceInfo *nfo_in;
551         const PaDeviceInfo *nfo_out;
552         const PaStreamInfo *nfo_s;
553                 
554         if (!initialize_pa()) {
555                 DEBUG_AUDIO ("PortAudio Initialization Failed\n");
556                 goto error;
557         }
558
559         if (device_input == -1) {
560                 device_input = get_default_input_device ();
561         }
562         if (device_output == -1) {
563                 device_output = get_default_output_device ();
564         }
565
566         _capture_channels = 0;
567         _playback_channels = 0;
568         _cur_sample_rate = 0;
569         _cur_input_latency = 0;
570         _cur_output_latency = 0;
571
572         DEBUG_AUDIO (string_compose (
573             "PortAudio Device IDs: i:%1 o:%2\n", device_input, device_output));
574
575         nfo_in = Pa_GetDeviceInfo(device_input);
576         nfo_out = Pa_GetDeviceInfo(device_output);
577
578         if (!nfo_in && ! nfo_out) {
579                 DEBUG_AUDIO ("PortAudio Cannot Query Device Info\n");
580                 goto error;
581         }
582
583         if (nfo_in) {
584                 _capture_channels = nfo_in->maxInputChannels;
585         }
586         if (nfo_out) {
587                 _playback_channels = nfo_out->maxOutputChannels;
588         }
589
590         if(_capture_channels == 0 && _playback_channels == 0) {
591                 DEBUG_AUDIO ("PortAudio no input or output channels.\n");
592                 goto error;
593         }
594
595 #ifdef __APPLE__
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) {
599                 DEBUG_AUDIO (
600                     "Adjusted capture channels to power of two (portaudio rb bug)\n");
601                 _capture_channels = lower_power_of_two (_capture_channels);
602         }
603
604         if ((_playback_channels & (_playback_channels - 1)) != 0) {
605                 DEBUG_AUDIO (
606                     "Adjusted capture channels to power of two (portaudio rb bug)\n");
607                 _playback_channels = lower_power_of_two (_playback_channels);
608         }
609 #endif
610
611         DEBUG_AUDIO (string_compose ("PortAudio Channels: in:%1 out:%2\n",
612                                      _capture_channels,
613                                      _playback_channels));
614
615         PaStreamParameters inputParam;
616         PaStreamParameters outputParam;
617
618         if (nfo_in) {
619                 inputParam.device = device_input;
620                 inputParam.channelCount = _capture_channels;
621 #ifdef INTERLEAVED_INPUT
622                 inputParam.sampleFormat = paFloat32;
623 #else
624                 inputParam.sampleFormat = paFloat32 | paNonInterleaved;
625 #endif
626                 inputParam.suggestedLatency = nfo_in->defaultLowInputLatency;
627                 inputParam.hostApiSpecificStreamInfo = NULL;
628         }
629
630         if (nfo_out) {
631                 outputParam.device = device_output;
632                 outputParam.channelCount = _playback_channels;
633 #ifdef INTERLEAVED_OUTPUT
634                 outputParam.sampleFormat = paFloat32;
635 #else
636                 outputParam.sampleFormat = paFloat32 | paNonInterleaved;
637 #endif
638                 outputParam.suggestedLatency = nfo_out->defaultLowOutputLatency;
639                 outputParam.hostApiSpecificStreamInfo = NULL;
640         }
641
642         // XXX re-consider using callback API, testing needed.
643         err = Pa_OpenStream (
644                         &_stream,
645                         _capture_channels > 0 ? &inputParam: NULL,
646                         _playback_channels > 0 ? &outputParam: NULL,
647                         sample_rate,
648                         samples_per_period,
649                         paDitherOff,
650                         NULL, NULL);
651
652         if (err != paNoError) {
653                 DEBUG_AUDIO ("PortAudio failed to start stream.\n");
654                 goto error;
655         }
656
657         nfo_s = Pa_GetStreamInfo (_stream);
658         if (!nfo_s) {
659                 DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
660                 pcm_stop();
661                 goto error;
662         }
663
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;
667
668         DEBUG_AUDIO (string_compose ("PA Sample Rate %1 SPS\n", _cur_sample_rate));
669
670         DEBUG_AUDIO (string_compose ("PA Input Latency %1ms, %2 spl\n",
671                                      1e3 * nfo_s->inputLatency,
672                                      _cur_input_latency));
673
674         DEBUG_AUDIO (string_compose ("PA Output Latency %1ms, %2 spl\n",
675                                      1e3 * nfo_s->outputLatency,
676                                      _cur_output_latency));
677
678         _state = 0;
679
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");
684                         pcm_stop();
685                         goto error;
686                 }
687         }
688
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");
693                         pcm_stop();
694                         goto error;
695                 }
696         }
697
698         return 0;
699
700 error:
701         _capture_channels = 0;
702         _playback_channels = 0;
703         free (_input_buffer); _input_buffer = NULL;
704         free (_output_buffer); _output_buffer = NULL;
705         return -1;
706 }
707
708 int
709 PortAudioIO::next_cycle (uint32_t n_samples)
710 {
711         bool xrun = false;
712         PaError err;
713         err = Pa_IsStreamActive (_stream);
714         if (err != 1) {
715                 //   0: inactive / aborted
716                 // < 0: error
717                 return -1;
718         }
719
720         // TODO, check drift..  process part with larger capacity first.
721         // Pa_GetStreamReadAvailable(_stream) < Pa_GetStreamWriteAvailable(_stream)
722
723         if (_playback_channels > 0) {
724                 err = Pa_WriteStream (_stream, _output_buffer, n_samples);
725                 if (err) xrun = true;
726         }
727
728         if (_capture_channels > 0) {
729                 err = Pa_ReadStream (_stream, _input_buffer, n_samples);
730                 if (err) {
731                         memset (_input_buffer, 0, sizeof(float) * n_samples * _capture_channels);
732                         xrun = true;
733                 }
734         }
735
736
737         return xrun ? 1 : 0;
738 }
739
740
741 #ifdef INTERLEAVED_INPUT
742
743 int
744 PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
745 {
746         assert(chn < _capture_channels);
747         const uint32_t stride = _capture_channels;
748         float *ptr = _input_buffer + chn;
749         while (n_samples-- > 0) {
750                 *input++ = *ptr;
751                 ptr += stride;
752         }
753         return 0;
754 }
755
756 #else
757
758 int
759 PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
760 {
761         assert(chn < _capture_channels);
762         memcpy((void*)input, &(_input_buffer[chn * n_samples]), n_samples * sizeof(float));
763         return 0;
764 }
765
766 #endif
767
768
769 #ifdef INTERLEAVED_OUTPUT
770
771 int
772 PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
773 {
774         assert(chn < _playback_channels);
775         const uint32_t stride = _playback_channels;
776         float *ptr = _output_buffer + chn;
777         while (n_samples-- > 0) {
778                 *ptr = *output++;
779                 ptr += stride;
780         }
781         return 0;
782 }
783
784 #else
785
786 int
787 PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
788 {
789         assert(chn < _playback_channels);
790         memcpy((void*)&(_output_buffer[chn * n_samples]), (void*)output, n_samples * sizeof(float));
791         return 0;
792 }
793
794 #endif