fccda4f05e13f4e9d884ae035a554b5c1b8edc92
[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     device_list (std::map<int, std::string> &devices) const;
51
52         int      available_sample_rates (int device_id, std::vector<float>& sampleRates);
53         int      available_buffer_sizes (int device_id, std::vector<uint32_t>& sampleRates);
54
55
56         void     pcm_stop (void);
57         int      pcm_start (void);
58
59         int      pcm_setup (
60                         int device_input,
61                         int device_output,
62                         double   sample_rate,
63                         uint32_t samples_per_period
64                         );
65
66         uint32_t n_playback_channels (void) const { return _playback_channels; }
67         uint32_t n_capture_channels (void) const { return _capture_channels; }
68
69         double   sample_rate (void) const { return _cur_sample_rate; }
70         uint32_t capture_latency (void) const { return _cur_input_latency; }
71         uint32_t playback_latency (void) const { return _cur_output_latency; }
72         double   stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }
73
74         int      next_cycle(uint32_t n_samples);
75         int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
76         int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
77
78 private: // Methods
79
80         void clear_device_list ();
81         void add_default_device ();
82         void add_devices ();
83
84 private: // Data
85         int  _state;
86         bool _initialized;
87
88         uint32_t _capture_channels;
89         uint32_t _playback_channels;
90
91         PaStream *_stream;
92
93         float *_input_buffer;
94         float *_output_buffer;
95
96         double _cur_sample_rate;
97         uint32_t _cur_input_latency;
98         uint32_t _cur_output_latency;
99
100         struct paDevice {
101                 std::string name;
102                 uint32_t n_inputs;
103                 uint32_t n_outputs;
104
105                 paDevice (std::string n, uint32_t i, uint32_t o)
106                         : name (n)
107                         , n_inputs (i)
108                         , n_outputs (o)
109                 {}
110         };
111
112         std::map<int, paDevice *> _devices;
113
114         PaHostApiIndex _host_api_index;
115
116 };
117
118 } // namespace
119
120 #endif /* __libbackend_portaudio_pcmio_h__ */