summaryrefslogtreecommitdiff
path: root/libs/backends/portaudio/portaudio_io.h
blob: 991c5425748247bb179d73cf03fae339c3122176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __libbackend_portaudio_pcmio_h__
#define __libbackend_portaudio_pcmio_h__

#include <map>
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>

#include <stdint.h>

#include <portaudio.h>

namespace ARDOUR {

class PortAudioIO {
public:
	PortAudioIO (void);
	~PortAudioIO (void);

	int      state (void) const { return _state; }

	bool     initialize_pa ();

	void host_api_list (std::vector<std::string>&);
	bool set_host_api (const std::string& host_api_name);
	std::string get_host_api () const { return _host_api_name; }
	PaHostApiIndex get_host_api_index_from_name (const std::string& name);

	PaDeviceIndex get_default_input_device ();
	PaDeviceIndex get_default_output_device ();

	void     discover();
	void     input_device_list (std::map<int, std::string> &devices) const;
	void     output_device_list (std::map<int, std::string> &devices) const;

	int      available_sample_rates (int device_id, std::vector<float>& sampleRates);
	int      available_buffer_sizes (int device_id, std::vector<uint32_t>& sampleRates);

	std::string control_app_name (int device_id) const;
	void launch_control_app (int device_id);

	void     pcm_stop (void);
	int      pcm_start (void);

	int      pcm_setup (
			int device_input,
			int device_output,
			double   sample_rate,
			uint32_t samples_per_period
			);

	uint32_t n_playback_channels (void) const { return _playback_channels; }
	uint32_t n_capture_channels (void) const { return _capture_channels; }

	double   sample_rate (void) const { return _cur_sample_rate; }
	uint32_t capture_latency (void) const { return _cur_input_latency; }
	uint32_t playback_latency (void) const { return _cur_output_latency; }
	double   stream_time(void) const { if (_stream) return Pa_GetStreamTime (_stream); return 0; }

	int      next_cycle(uint32_t n_samples);
	int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
	int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);

private: // Methods

	void clear_device_lists ();
	void add_default_devices ();
	void add_devices ();

private: // Data
	int  _state;
	bool _initialized;

	uint32_t _capture_channels;
	uint32_t _playback_channels;

	PaStream *_stream;

	float *_input_buffer;
	float *_output_buffer;

	double _cur_sample_rate;
	uint32_t _cur_input_latency;
	uint32_t _cur_output_latency;

	struct paDevice {
		std::string name;
		uint32_t n_inputs;
		uint32_t n_outputs;

		paDevice (std::string n, uint32_t i, uint32_t o)
			: name (n)
			, n_inputs (i)
			, n_outputs (o)
		{}
	};

	std::map<int, paDevice *> _input_devices;
	std::map<int, paDevice *> _output_devices;

	PaHostApiIndex _host_api_index;
	std::string _host_api_name;

};

} // namespace

#endif /* __libbackend_portaudio_pcmio_h__ */