-
Notifications
You must be signed in to change notification settings - Fork 322
/
rtaudio_c.cpp
229 lines (190 loc) · 6.75 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
#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;
rtaudio_error_t errtype;
char errmsg[MAX_ERROR_MESSAGE_LENGTH];
};
const char *rtaudio_version() { return RTAUDIO_VERSION; }
extern "C" const RtAudio::Api rtaudio_compiled_apis[];
const rtaudio_api_t *rtaudio_compiled_api() {
return (rtaudio_api_t *) &rtaudio_compiled_apis[0];
}
extern "C" const unsigned int rtaudio_num_compiled_apis;
unsigned int rtaudio_get_num_compiled_apis(void) {
return rtaudio_num_compiled_apis;
}
extern "C" const char* rtaudio_api_names[][2];
const char *rtaudio_api_name(rtaudio_api_t api) {
if (api < 0 || api >= RTAUDIO_API_NUM)
return NULL;
return rtaudio_api_names[api][0];
}
const char *rtaudio_api_display_name(rtaudio_api_t api)
{
if (api < 0 || api >= RTAUDIO_API_NUM)
return "Unknown";
return rtaudio_api_names[api][1];
}
rtaudio_api_t rtaudio_compiled_api_by_name(const char *name) {
RtAudio::Api api = RtAudio::UNSPECIFIED;
if (name) {
api = RtAudio::getCompiledApiByName(name);
}
return (rtaudio_api_t)api;
}
const char *rtaudio_error(rtaudio_t audio) {
if (audio->errtype == RTAUDIO_ERROR_NONE) {
return NULL;
}
return audio->errmsg;
}
rtaudio_error_t rtaudio_error_type(rtaudio_t audio) {
return audio->errtype;
}
rtaudio_t rtaudio_create(rtaudio_api_t api) {
rtaudio_t audio = new struct rtaudio();
audio->errtype = RTAUDIO_ERROR_NONE;
audio->audio = new RtAudio((RtAudio::Api)api,
[audio](RtAudioErrorType type, const std::string &errorText){
audio->errtype = (rtaudio_error_t)type;
strncpy(audio->errmsg, errorText.c_str(), errorText.size() - 1);
});
return audio;
}
void rtaudio_destroy(rtaudio_t audio) { delete audio->audio; delete 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();
}
unsigned int rtaudio_get_device_id(rtaudio_t audio, int i) {
std::vector<unsigned int> deviceIds = audio->audio->getDeviceIds();
if ( i >= 0 && i < (int) deviceIds.size() )
return deviceIds[i];
else
return 0;
}
rtaudio_device_info_t rtaudio_get_device_info(rtaudio_t audio, unsigned int id) {
rtaudio_device_info_t result;
std::memset(&result, 0, sizeof(result));
audio->errtype = RTAUDIO_ERROR_NONE;
RtAudio::DeviceInfo info = audio->audio->getDeviceInfo(id);
if (audio->errtype != RTAUDIO_ERROR_NONE)
return result;
result.id = info.ID;
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];
}
}
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);
}
rtaudio_error_t 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*/)
{
audio->errtype = RTAUDIO_ERROR_NONE;
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 audio->errtype;
}
void rtaudio_close_stream(rtaudio_t audio) { audio->audio->closeStream(); }
rtaudio_error_t rtaudio_start_stream(rtaudio_t audio) {
audio->errtype = RTAUDIO_ERROR_NONE;
audio->audio->startStream();
return audio->errtype;
}
rtaudio_error_t rtaudio_stop_stream(rtaudio_t audio) {
audio->errtype = RTAUDIO_ERROR_NONE;
audio->audio->stopStream();
return audio->errtype;
}
rtaudio_error_t rtaudio_abort_stream(rtaudio_t audio) {
audio->errtype = RTAUDIO_ERROR_NONE;
audio->audio->abortStream();
return audio->errtype;
}
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) {
audio->errtype = RTAUDIO_ERROR_NONE;
return audio->audio->getStreamTime();
}
void rtaudio_set_stream_time(rtaudio_t audio, double time) {
audio->errtype = RTAUDIO_ERROR_NONE;
audio->audio->setStreamTime(time);
}
long rtaudio_get_stream_latency(rtaudio_t audio) {
audio->errtype = RTAUDIO_ERROR_NONE;
return audio->audio->getStreamLatency();
}
unsigned int rtaudio_get_stream_sample_rate(rtaudio_t audio) {
audio->errtype = RTAUDIO_ERROR_NONE;
return audio->audio->getStreamSampleRate();
}
void rtaudio_show_warnings(rtaudio_t audio, int show) {
audio->audio->showWarnings(!!show);
}