Move Windows MMCSS related utility functions into libpbd
[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         enum ErrorCode {
39                 NoError = 0,
40                 InitializationError,
41                 DeInitializationError,
42                 DeviceConfigNotSupportedError,
43                 StreamOpenError,
44                 StreamStartError,
45                 StreamStopError,
46                 StreamCloseError,
47                 IOError,
48                 BufferUnderrunError,
49                 BufferOverrunError
50         };
51
52         enum StandardDevices {
53                 DeviceNone = -2,
54                 DeviceDefault = -1
55         };
56
57         void host_api_list (std::vector<std::string>&);
58         bool set_host_api (const std::string& host_api_name);
59         std::string get_host_api () const { return _host_api_name; }
60         PaHostApiTypeId get_current_host_api_type () const;
61         PaHostApiIndex get_host_api_index_from_name (const std::string& name);
62
63         PaDeviceIndex get_default_input_device () const;
64         PaDeviceIndex get_default_output_device () const;
65
66         bool     update_devices();
67         void     input_device_list (std::map<int, std::string> &devices) const;
68         void     output_device_list (std::map<int, std::string> &devices) const;
69
70         int available_sample_rates (int device_id, std::vector<float>& sample_rates);
71         int available_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes);
72
73 #ifdef WITH_ASIO
74         bool get_asio_buffer_properties (int device_id,
75                                          long& min_size_frames,
76                                          long& max_size_frames,
77                                          long& preferred_size_frames,
78                                          long& granularity);
79
80         bool get_asio_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_size);
81 #endif
82
83         std::string control_app_name (int device_id) const;
84         void launch_control_app (int device_id);
85
86         ErrorCode open_blocking_stream(int device_input,
87                                        int device_output,
88                                        double sample_rate,
89                                        uint32_t samples_per_period);
90         ErrorCode start_stream(void);
91
92         ErrorCode close_stream(void);
93
94         uint32_t n_playback_channels (void) const { return _playback_channels; }
95         uint32_t n_capture_channels (void) const { return _capture_channels; }
96
97         std::string get_input_channel_name (int device_id, uint32_t channel) const;
98         std::string get_output_channel_name (int device_id, uint32_t channel) const;
99
100         double   sample_rate (void) const { return _cur_sample_rate; }
101         uint32_t capture_latency (void) const { return _cur_input_latency; }
102         uint32_t playback_latency (void) const { return _cur_output_latency; }
103         double   stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }
104
105         int      next_cycle(uint32_t n_samples);
106         int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
107         int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
108
109         float* get_capture_buffer () { return _input_buffer; }
110         float* get_playback_buffer () { return _output_buffer; }
111
112 private: // Methods
113
114         static bool pa_initialize();
115         static bool pa_deinitialize();
116         static bool& pa_initialized();
117
118         void clear_device_lists ();
119         void add_none_devices ();
120         void add_default_devices ();
121         void add_devices ();
122         std::string get_host_api_name_from_index (PaHostApiIndex index);
123
124         bool get_output_stream_params(int device_output,
125                                       PaStreamParameters& outputParam) const;
126         bool get_input_stream_params(int device_input,
127                                      PaStreamParameters& inputParam) const;
128
129         bool set_sample_rate_and_latency_from_stream();
130         bool allocate_buffers_for_blocking_api (uint32_t samples_per_period);
131
132         ErrorCode pre_stream_open(int device_input,
133                                   PaStreamParameters& inputParam,
134                                   int device_output,
135                                   PaStreamParameters& outputParam);
136
137         void reset_stream_dependents ();
138
139         static void get_default_sample_rates(std::vector<float>&);
140         static void get_default_buffer_sizes(std::vector<uint32_t>&);
141
142 private: // Data
143         uint32_t _capture_channels;
144         uint32_t _playback_channels;
145
146         PaStream *_stream;
147
148         float *_input_buffer;
149         float *_output_buffer;
150
151         double _cur_sample_rate;
152         uint32_t _cur_input_latency;
153         uint32_t _cur_output_latency;
154
155         struct paDevice {
156                 std::string name;
157                 uint32_t n_inputs;
158                 uint32_t n_outputs;
159
160                 paDevice (std::string n, uint32_t i, uint32_t o)
161                         : name (n)
162                         , n_inputs (i)
163                         , n_outputs (o)
164                 {}
165         };
166
167         std::map<int, paDevice *> _input_devices;
168         std::map<int, paDevice *> _output_devices;
169
170         PaHostApiIndex _host_api_index;
171         std::string _host_api_name;
172
173 };
174
175 } // namespace
176
177 #endif /* __libbackend_portaudio_pcmio_h__ */