forked from thestk/rtaudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtaudio_c.cpp
262 lines (230 loc) · 7.27 KB
/
rtaudio_c.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "rtaudio_c.h"
#include "RtAudio.h"
#include <cstring>
#define MAX_ERROR_MESSAGE_LENGTH 512
struct rtaudio {
RtAudio *audio;
rtaudio_cb_t cb;
void *userdata;
int has_error;
char errmsg[MAX_ERROR_MESSAGE_LENGTH];
};
static const rtaudio_api_t compiled_api[] = {
#if defined(__UNIX_JACK__)
RTAUDIO_API_UNIX_JACK,
#endif
#if defined(__LINUX_ALSA__)
RTAUDIO_API_LINUX_ALSA,
#endif
#if defined(__LINUX_PULSE__)
RTAUDIO_API_LINUX_PULSE,
#endif
#if defined(__LINUX_OSS__)
RTAUDIO_API_LINUX_OSS,
#endif
#if defined(__WINDOWS_ASIO__)
RTAUDIO_API_WINDOWS_ASIO,
#endif
#if defined(__WINDOWS_WASAPI__)
RTAUDIO_API_WINDOWS_WASAPI,
#endif
#if defined(__WINDOWS_DS__)
RTAUDIO_API_WINDOWS_DS,
#endif
#if defined(__MACOSX_CORE__)
RTAUDIO_API_MACOSX_CORE,
#endif
#if defined(__RTAUDIO_DUMMY__)
RTAUDIO_API_DUMMY,
#endif
RTAUDIO_API_UNSPECIFIED,
};
const char *rtaudio_version() { return RTAUDIO_VERSION; }
const rtaudio_api_t *rtaudio_compiled_api() { return compiled_api; }
const char *rtaudio_error(rtaudio_t audio) {
if (audio->has_error) {
return audio->errmsg;
}
return NULL;
}
rtaudio_t rtaudio_create(rtaudio_api_t api) {
rtaudio_t audio = new struct rtaudio();
try {
audio->audio = new RtAudio((RtAudio::Api)api);
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
}
return audio;
}
void rtaudio_destroy(rtaudio_t audio) { delete audio->audio; }
rtaudio_api_t rtaudio_current_api(rtaudio_t audio) {
return (rtaudio_api_t)audio->audio->getCurrentApi();
}
int rtaudio_device_count(rtaudio_t audio) {
return audio->audio->getDeviceCount();
}
rtaudio_device_info_t rtaudio_get_device_info(rtaudio_t audio, int i) {
rtaudio_device_info_t result;
std::memset(&result, 0, sizeof(result));
try {
audio->has_error = 0;
RtAudio::DeviceInfo info = audio->audio->getDeviceInfo(i);
result.probed = info.probed;
result.output_channels = info.outputChannels;
result.input_channels = info.inputChannels;
result.duplex_channels = info.duplexChannels;
result.is_default_output = info.isDefaultOutput;
result.is_default_input = info.isDefaultInput;
result.native_formats = info.nativeFormats;
result.preferred_sample_rate = info.preferredSampleRate;
strncpy(result.name, info.name.c_str(), sizeof(result.name) - 1);
for (unsigned int j = 0; j < info.sampleRates.size(); j++) {
if (j < sizeof(result.sample_rates) / sizeof(result.sample_rates[0])) {
result.sample_rates[j] = info.sampleRates[j];
}
}
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
}
return result;
}
unsigned int rtaudio_get_default_output_device(rtaudio_t audio) {
return audio->audio->getDefaultOutputDevice();
}
unsigned int rtaudio_get_default_input_device(rtaudio_t audio) {
return audio->audio->getDefaultInputDevice();
}
static int proxy_cb_func(void *out, void *in, unsigned int nframes, double time,
RtAudioStreamStatus status, void *userdata) {
rtaudio_t audio = (rtaudio_t)userdata;
return audio->cb(out, in, nframes, time, (rtaudio_stream_status_t)status,
audio->userdata);
}
int rtaudio_open_stream(rtaudio_t audio,
rtaudio_stream_parameters_t *output_params,
rtaudio_stream_parameters_t *input_params,
rtaudio_format_t format, unsigned int sample_rate,
unsigned int *buffer_frames, rtaudio_cb_t cb,
void *userdata, rtaudio_stream_options_t *options,
rtaudio_error_cb_t /*errcb*/) {
try {
audio->has_error = 0;
RtAudio::StreamParameters *in = NULL;
RtAudio::StreamParameters *out = NULL;
RtAudio::StreamOptions *opts = NULL;
RtAudio::StreamParameters inparams;
RtAudio::StreamParameters outparams;
RtAudio::StreamOptions stream_opts;
if (input_params != NULL) {
inparams.deviceId = input_params->device_id;
inparams.nChannels = input_params->num_channels;
inparams.firstChannel = input_params->first_channel;
in = &inparams;
}
if (output_params != NULL) {
outparams.deviceId = output_params->device_id;
outparams.nChannels = output_params->num_channels;
outparams.firstChannel = output_params->first_channel;
out = &outparams;
}
if (options != NULL) {
stream_opts.flags = (RtAudioStreamFlags)options->flags;
stream_opts.numberOfBuffers = options->num_buffers;
stream_opts.priority = options->priority;
if (strlen(options->name) > 0) {
stream_opts.streamName = std::string(options->name);
}
opts = &stream_opts;
}
audio->cb = cb;
audio->userdata = userdata;
audio->audio->openStream(out, in, (RtAudioFormat)format, sample_rate,
buffer_frames, proxy_cb_func, (void *)audio, opts,
NULL);
return 0;
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
return -1;
}
}
void rtaudio_close_stream(rtaudio_t audio) { audio->audio->closeStream(); }
int rtaudio_start_stream(rtaudio_t audio) {
try {
audio->has_error = 0;
audio->audio->startStream();
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
}
return 0;
}
int rtaudio_stop_stream(rtaudio_t audio) {
try {
audio->has_error = 0;
audio->audio->stopStream();
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
}
return 0;
}
int rtaudio_abort_stream(rtaudio_t audio) {
try {
audio->has_error = 0;
audio->audio->abortStream();
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
}
return 0;
}
int rtaudio_is_stream_open(rtaudio_t audio) {
return !!audio->audio->isStreamOpen();
}
int rtaudio_is_stream_running(rtaudio_t audio) {
return !!audio->audio->isStreamRunning();
}
double rtaudio_get_stream_time(rtaudio_t audio) {
try {
audio->has_error = 0;
return audio->audio->getStreamTime();
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
return 0;
}
}
void rtaudio_set_stream_time(rtaudio_t audio, double time) {
try {
audio->has_error = 0;
audio->audio->setStreamTime(time);
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
}
}
int rtaudio_get_stream_latency(rtaudio_t audio) {
try {
audio->has_error = 0;
return audio->audio->getStreamLatency();
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
return -1;
}
}
unsigned int rtaudio_get_stream_sample_rate(rtaudio_t audio) {
try {
return audio->audio->getStreamSampleRate();
} catch (RtAudioError &err) {
audio->has_error = 1;
strncpy(audio->errmsg, err.what(), sizeof(audio->errmsg) - 1);
return -1;
}
}
void rtaudio_show_warnings(rtaudio_t audio, int show) {
audio->audio->showWarnings(!!show);
}