blob: 787ff4ee40337f099a7d5ff5714d5d3160e43b32 (
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
DCP-o-matic 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.
DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audio_backend.h"
#include <boost/bind/bind.hpp>
using std::string;
using std::vector;
using boost::optional;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif
AudioBackend* AudioBackend::_instance = nullptr;
#ifdef DCPOMATIC_LINUX
auto constexpr api = RtAudio::LINUX_PULSE;
#endif
#ifdef DCPOMATIC_WINDOWS
auto constexpr api = RtAudio::UNSPECIFIED;
#endif
#ifdef DCPOMATIC_OSX
auto constexpr api = RtAudio::MACOSX_CORE;
#endif
AudioBackend::AudioBackend()
#if (RTAUDIO_VERSION_MAJOR >= 6)
: _rtaudio(api, boost::bind(&AudioBackend::rtaudio_error_callback, this, _2))
#else
: _rtaudio(api)
#endif
{
}
#if (RTAUDIO_VERSION_MAJOR >= 6)
void
AudioBackend::rtaudio_error_callback(string const& error)
{
boost::mutex::scoped_lock lm(_last_error_mutex);
_last_error = error;
}
string
AudioBackend::last_error() const
{
boost::mutex::scoped_lock lm(_last_error_mutex);
return _last_error;
}
#endif
AudioBackend*
AudioBackend::instance()
{
if (!_instance) {
_instance = new AudioBackend();
}
return _instance;
}
vector<string>
AudioBackend::device_names()
{
vector<string> names;
#if (RTAUDIO_VERSION_MAJOR >= 6)
for (auto device_id: _rtaudio.getDeviceIds()) {
auto dev = _rtaudio.getDeviceInfo(device_id);
if (dev.outputChannels > 0) {
names.push_back(dev.name);
}
}
#else
for (unsigned int i = 0; i < _rtaudio.getDeviceCount(); ++i) {
try {
auto dev = _rtaudio.getDeviceInfo(i);
if (dev.probed && dev.outputChannels > 0) {
names.push_back(dev.name);
}
} catch (RtAudioError&) {
/* Something went wrong so let's just ignore that device */
}
}
#endif
return names;
}
optional<string>
AudioBackend::default_device_name()
{
#if (RTAUDIO_VERSION_MAJOR >= 6)
return _rtaudio.getDeviceInfo(_rtaudio.getDefaultOutputDevice()).name;
#else
try {
return _rtaudio.getDeviceInfo(_rtaudio.getDefaultOutputDevice()).name;
} catch (RtAudioError&) {
/* Never mind */
}
#endif
return {};
}
optional<int>
AudioBackend::device_channels(string name)
{
#if (RTAUDIO_VERSION_MAJOR >= 6)
for (auto device_id: _rtaudio.getDeviceIds()) {
auto info = _rtaudio.getDeviceInfo(device_id);
if (info.name == name) {
return info.outputChannels;
}
}
#else
for (unsigned int i = 0; i < _rtaudio.getDeviceCount(); ++i) {
try {
auto info = _rtaudio.getDeviceInfo(i);
if (info.name == name) {
return info.outputChannels;
}
} catch (RtAudioError&) {
/* Never mind */
}
}
#endif
return {};
}
void
AudioBackend::abort_stream_if_running()
{
if (_rtaudio.isStreamRunning()) {
_rtaudio.abortStream();
}
}
optional<string>
AudioBackend::start_stream()
{
#if (RTAUDIO_VERSION_MAJOR >= 6)
if (_rtaudio.startStream() != RTAUDIO_NO_ERROR) {
return last_error();
}
#else
try {
_rtaudio.startStream();
} catch (RtAudioError& e) {
return string(e.what());
}
#endif
return {};
}
|