Support selecting separate input and output devices in portaudio backend
[ardour.git] / libs / backends / portaudio / portaudio_io.h
1 /*
2  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ifndef __libbackend_portaudio_pcmio_h__
20 #define __libbackend_portaudio_pcmio_h__
21
22 #include <map>
23 #include <vector>
24 #include <string>
25 #include <boost/shared_ptr.hpp>
26
27 #include <stdint.h>
28
29 #include <portaudio.h>
30
31 namespace ARDOUR {
32
33 class PortAudioIO {
34 public:
35         PortAudioIO (void);
36         ~PortAudioIO (void);
37
38         int      state (void) const { return _state; }
39
40         bool     initialize_pa ();
41
42         void     host_api_list (std::vector<std::string>&);
43         void     set_host_api (const std::string& host_api_name);
44         PaHostApiIndex get_host_api_index_from_name (const std::string& name);
45
46         PaDeviceIndex get_default_input_device ();
47         PaDeviceIndex get_default_output_device ();
48
49         void     discover();
50         void     input_device_list (std::map<int, std::string> &devices) const;
51         void     output_device_list (std::map<int, std::string> &devices) const;
52
53         int      available_sample_rates (int device_id, std::vector<float>& sampleRates);
54         int      available_buffer_sizes (int device_id, std::vector<uint32_t>& sampleRates);
55
56
57         void     pcm_stop (void);
58         int      pcm_start (void);
59
60         int      pcm_setup (
61                         int device_input,
62                         int device_output,
63                         double   sample_rate,
64                         uint32_t samples_per_period
65                         );
66
67         uint32_t n_playback_channels (void) const { return _playback_channels; }
68         uint32_t n_capture_channels (void) const { return _capture_channels; }
69
70         double   sample_rate (void) const { return _cur_sample_rate; }
71         uint32_t capture_latency (void) const { return _cur_input_latency; }
72         uint32_t playback_latency (void) const { return _cur_output_latency; }
73         double   stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }
74
75         int      next_cycle(uint32_t n_samples);
76         int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
77         int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
78
79 private: // Methods
80
81         void clear_device_lists ();
82         void add_default_devices ();
83         void add_devices ();
84
85 private: // Data
86         int  _state;
87         bool _initialized;
88
89         uint32_t _capture_channels;
90         uint32_t _playback_channels;
91
92         PaStream *_stream;
93
94         float *_input_buffer;
95         float *_output_buffer;
96
97         double _cur_sample_rate;
98         uint32_t _cur_input_latency;
99         uint32_t _cur_output_latency;
100
101         struct paDevice {
102                 std::string name;
103                 uint32_t n_inputs;
104                 uint32_t n_outputs;
105
106                 paDevice (std::string n, uint32_t i, uint32_t o)
107                         : name (n)
108                         , n_inputs (i)
109                         , n_outputs (o)
110                 {}
111         };
112
113         std::map<int, paDevice *> _input_devices;
114         std::map<int, paDevice *> _output_devices;
115
116         PaHostApiIndex _host_api_index;
117
118 };
119
120 } // namespace
121
122 #endif /* __libbackend_portaudio_pcmio_h__ */