Add StandardDevice enum to portaudio_io.h header
[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         enum StandardDevices {
41                 DeviceNone = -2,
42                 DeviceDefault = -1
43         };
44
45         bool     initialize_pa ();
46
47         void host_api_list (std::vector<std::string>&);
48         bool set_host_api (const std::string& host_api_name);
49         std::string get_host_api () const { return _host_api_name; }
50         PaHostApiIndex get_host_api_index_from_name (const std::string& name);
51
52         PaDeviceIndex get_default_input_device ();
53         PaDeviceIndex get_default_output_device ();
54
55         void     discover();
56         void     input_device_list (std::map<int, std::string> &devices) const;
57         void     output_device_list (std::map<int, std::string> &devices) const;
58
59         int available_sample_rates (int device_id, std::vector<float>& sample_rates);
60         int available_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes);
61
62         bool get_default_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes);
63 #ifdef WITH_ASIO
64         bool get_asio_buffer_properties (int device_id,
65                                          long& min_size_frames,
66                                          long& max_size_frames,
67                                          long& preferred_size_frames,
68                                          long& granularity);
69
70         bool get_asio_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_size);
71 #endif
72
73         std::string control_app_name (int device_id) const;
74         void launch_control_app (int device_id);
75
76         void     pcm_stop (void);
77         int      pcm_start (void);
78
79         int      pcm_setup (
80                         int device_input,
81                         int device_output,
82                         double   sample_rate,
83                         uint32_t samples_per_period
84                         );
85
86         uint32_t n_playback_channels (void) const { return _playback_channels; }
87         uint32_t n_capture_channels (void) const { return _capture_channels; }
88
89         std::string get_input_channel_name (int device_id, uint32_t channel) const;
90         std::string get_output_channel_name (int device_id, uint32_t channel) const;
91
92         double   sample_rate (void) const { return _cur_sample_rate; }
93         uint32_t capture_latency (void) const { return _cur_input_latency; }
94         uint32_t playback_latency (void) const { return _cur_output_latency; }
95         double   stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }
96
97         int      next_cycle(uint32_t n_samples);
98         int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
99         int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
100
101 private: // Methods
102
103         void clear_device_lists ();
104         void add_default_devices ();
105         void add_devices ();
106         std::string get_host_api_name_from_index (PaHostApiIndex index);
107
108 private: // Data
109         int  _state;
110         bool _initialized;
111
112         uint32_t _capture_channels;
113         uint32_t _playback_channels;
114
115         PaStream *_stream;
116
117         float *_input_buffer;
118         float *_output_buffer;
119
120         double _cur_sample_rate;
121         uint32_t _cur_input_latency;
122         uint32_t _cur_output_latency;
123
124         struct paDevice {
125                 std::string name;
126                 uint32_t n_inputs;
127                 uint32_t n_outputs;
128
129                 paDevice (std::string n, uint32_t i, uint32_t o)
130                         : name (n)
131                         , n_inputs (i)
132                         , n_outputs (o)
133                 {}
134         };
135
136         std::map<int, paDevice *> _input_devices;
137         std::map<int, paDevice *> _output_devices;
138
139         PaHostApiIndex _host_api_index;
140         std::string _host_api_name;
141
142 };
143
144 } // namespace
145
146 #endif /* __libbackend_portaudio_pcmio_h__ */