Ardour  9.0-pre0-582-g084a23a80d
portaudio_io.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __libbackend_portaudio_pcmio_h__
20 #define __libbackend_portaudio_pcmio_h__
21 
22 #include <cstdint>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 #include <string>
27 
28 #include <portaudio.h>
29 
30 namespace ARDOUR {
31 
32 class PortAudioIO {
33 public:
34  PortAudioIO (void);
35  ~PortAudioIO (void);
36 
38  DeviceNone = -2,
39  DeviceDefault = -1
40  };
41 
42  void host_api_list (std::vector<std::string>&);
43  bool set_host_api (const std::string& host_api_name);
44  std::string get_host_api () const { return _host_api_name; }
45  PaHostApiTypeId get_current_host_api_type () const;
46  PaHostApiIndex get_host_api_index_from_name (const std::string& name);
47 
48  PaDeviceIndex get_default_input_device () const;
49  PaDeviceIndex get_default_output_device () const;
50 
52  void input_device_list (std::map<int, std::string> &devices) const;
53  void output_device_list (std::map<int, std::string> &devices) const;
54 
55  int available_sample_rates (int device_id, std::vector<float>& sample_rates);
56  int available_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes);
57 
58 #ifdef WITH_ASIO
59  bool get_asio_buffer_properties (int device_id,
60  long& min_size_samples,
61  long& max_size_samples,
62  long& preferred_size_samples,
63  long& granularity);
64 
65  bool get_asio_buffer_sizes(int device_id,
66  std::vector<uint32_t>& buffer_size,
67  bool preferred_only);
68 #endif
69 
70  std::string control_app_name (int device_id) const;
71  void launch_control_app (int device_id);
72 
73  PaErrorCode open_blocking_stream(int device_input,
74  int device_output,
75  double sample_rate,
76  uint32_t samples_per_period);
77 
78  PaErrorCode open_callback_stream(int device_input,
79  int device_output,
80  double sample_rate,
81  uint32_t samples_per_period,
82  PaStreamCallback* callback,
83  void* data);
84 
85  PaErrorCode start_stream(void);
86 
87  PaErrorCode close_stream(void);
88 
89  uint32_t n_playback_channels (void) const { return _playback_channels; }
90  uint32_t n_capture_channels (void) const { return _capture_channels; }
91 
92  std::string get_input_channel_name (int device_id, uint32_t channel) const;
93  std::string get_output_channel_name (int device_id, uint32_t channel) const;
94 
95  double sample_rate (void) const { return _cur_sample_rate; }
96  uint32_t capture_latency (void) const { return _cur_input_latency; }
97  uint32_t playback_latency (void) const { return _cur_output_latency; }
98  double stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }
99 
100  int next_cycle(uint32_t n_samples);
101  int get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
102  int set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
103 
104  float* get_capture_buffer () { return _input_buffer; }
105  float* get_playback_buffer () { return _output_buffer; }
106 
107 private: // Methods
108 
109  static bool pa_initialize();
110  static bool pa_deinitialize();
111  static bool& pa_initialized();
112 
116  void add_devices ();
117  std::string get_host_api_name_from_index (PaHostApiIndex index);
118 
119  bool get_output_stream_params(int device_output,
120  PaStreamParameters& outputParam) const;
121  bool get_input_stream_params(int device_input,
122  PaStreamParameters& inputParam) const;
123 
125  bool allocate_buffers_for_blocking_api (uint32_t samples_per_period);
126 
127  PaErrorCode pre_stream_open(int device_input,
128  PaStreamParameters& inputParam,
129  int device_output,
130  PaStreamParameters& outputParam,
131  uint32_t sample_rate,
132  uint32_t samples_per_period);
133 
135 
136  static void get_default_sample_rates(std::vector<float>&);
137  static void get_default_buffer_sizes(std::vector<uint32_t>&);
138 
139 private: // Data
142 
143  PaStream *_stream;
144 
147 
151 
152  struct paDevice {
153  std::string name;
154  uint32_t n_inputs;
155  uint32_t n_outputs;
156 
157  paDevice (std::string n, uint32_t i, uint32_t o)
158  : name (n)
159  , n_inputs (i)
160  , n_outputs (o)
161  {}
162  };
163 
164  std::map<int, paDevice *> _input_devices;
165  std::map<int, paDevice *> _output_devices;
166 
167  PaHostApiIndex _host_api_index;
168  std::string _host_api_name;
169 
170 };
171 
172 } // namespace
173 
174 #endif /* __libbackend_portaudio_pcmio_h__ */
static void get_default_buffer_sizes(std::vector< uint32_t > &)
uint32_t _playback_channels
Definition: portaudio_io.h:141
std::string _host_api_name
Definition: portaudio_io.h:168
PaHostApiIndex _host_api_index
Definition: portaudio_io.h:167
double sample_rate(void) const
Definition: portaudio_io.h:95
std::string get_output_channel_name(int device_id, uint32_t channel) const
PaErrorCode open_blocking_stream(int device_input, int device_output, double sample_rate, uint32_t samples_per_period)
static void get_default_sample_rates(std::vector< float > &)
uint32_t capture_latency(void) const
Definition: portaudio_io.h:96
std::string get_input_channel_name(int device_id, uint32_t channel) const
static bool pa_initialize()
PaErrorCode pre_stream_open(int device_input, PaStreamParameters &inputParam, int device_output, PaStreamParameters &outputParam, uint32_t sample_rate, uint32_t samples_per_period)
std::string control_app_name(int device_id) const
int available_sample_rates(int device_id, std::vector< float > &sample_rates)
PaDeviceIndex get_default_output_device() const
bool get_output_stream_params(int device_output, PaStreamParameters &outputParam) const
float * get_playback_buffer()
Definition: portaudio_io.h:105
PaHostApiIndex get_host_api_index_from_name(const std::string &name)
uint32_t playback_latency(void) const
Definition: portaudio_io.h:97
void host_api_list(std::vector< std::string > &)
void reset_stream_dependents()
std::map< int, paDevice * > _input_devices
Definition: portaudio_io.h:164
PaHostApiTypeId get_current_host_api_type() const
uint32_t _capture_channels
Definition: portaudio_io.h:140
PaDeviceIndex get_default_input_device() const
bool get_input_stream_params(int device_input, PaStreamParameters &inputParam) const
uint32_t n_capture_channels(void) const
Definition: portaudio_io.h:90
int set_playback_channel(uint32_t chn, const float *input, uint32_t n_samples)
PaErrorCode close_stream(void)
float * get_capture_buffer()
Definition: portaudio_io.h:104
int get_capture_channel(uint32_t chn, float *input, uint32_t n_samples)
int available_buffer_sizes(int device_id, std::vector< uint32_t > &buffer_sizes)
bool set_host_api(const std::string &host_api_name)
bool allocate_buffers_for_blocking_api(uint32_t samples_per_period)
static bool & pa_initialized()
bool set_sample_rate_and_latency_from_stream()
void input_device_list(std::map< int, std::string > &devices) const
void launch_control_app(int device_id)
PaErrorCode open_callback_stream(int device_input, int device_output, double sample_rate, uint32_t samples_per_period, PaStreamCallback *callback, void *data)
static bool pa_deinitialize()
std::string get_host_api_name_from_index(PaHostApiIndex index)
uint32_t _cur_input_latency
Definition: portaudio_io.h:149
uint32_t _cur_output_latency
Definition: portaudio_io.h:150
double stream_time(void) const
Definition: portaudio_io.h:98
std::string get_host_api() const
Definition: portaudio_io.h:44
std::map< int, paDevice * > _output_devices
Definition: portaudio_io.h:165
void output_device_list(std::map< int, std::string > &devices) const
PaErrorCode start_stream(void)
int next_cycle(uint32_t n_samples)
uint32_t n_playback_channels(void) const
Definition: portaudio_io.h:89
GtkImageIconNameData name
Definition: gtkimage.h:6
paDevice(std::string n, uint32_t i, uint32_t o)
Definition: portaudio_io.h:157