call me Mr. 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         void     discover();
41         void     device_list (std::map<int, std::string> &devices) const;
42
43         int      available_sample_rates (int device_id, std::vector<float>& sampleRates);
44         int      available_buffer_sizes (int device_id, std::vector<uint32_t>& sampleRates);
45
46
47         void     pcm_stop (void);
48         int      pcm_start (void);
49
50         int      pcm_setup (
51                         int device_input,
52                         int device_output,
53                         double   sample_rate,
54                         uint32_t samples_per_period
55                         );
56
57         uint32_t n_playback_channels (void) const { return _playback_channels; }
58         uint32_t n_capture_channels (void) const { return _capture_channels; }
59
60         double   sample_rate (void) const { return _cur_sample_rate; }
61         uint32_t capture_latency (void) const { return _cur_input_latency; }
62         uint32_t playback_latency (void) const { return _cur_output_latency; }
63         double   stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }
64
65         int      next_cycle(uint32_t n_samples);
66         int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
67         int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
68
69 private:
70         int  _state;
71         bool _initialized;
72
73         uint32_t _capture_channels;
74         uint32_t _playback_channels;
75
76         PaStream *_stream;
77
78         float *_input_buffer;
79         float *_output_buffer;
80
81         double _cur_sample_rate;
82         uint32_t _cur_input_latency;
83         uint32_t _cur_output_latency;
84
85
86         struct paDevice {
87                 std::string name;
88                 uint32_t n_inputs;
89                 uint32_t n_outputs;
90
91                 paDevice (std::string n, uint32_t i, uint32_t o)
92                         : name (n)
93                         , n_inputs (i)
94                         , n_outputs (o)
95                 {}
96         };
97
98         std::map<int, paDevice *> _devices;
99 };
100
101 } // namespace
102
103 #endif /* __libbackend_portaudio_pcmio_h__ */