-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtaudio_ffi.lua
291 lines (278 loc) · 9.89 KB
/
rtaudio_ffi.lua
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
local ffi = require"ffi"
--uncomment to debug cdef calls
---[[
local ffi_cdef = function(code)
local ret,err = pcall(ffi.cdef,code)
if not ret then
local lineN = 1
for line in code:gmatch("([^\n\r]*)\r?\n") do
print(lineN, line)
lineN = lineN + 1
end
print(err)
error"bad cdef"
end
end
--]]
ffi_cdef[[
typedef unsigned long rtaudio_format_t;
typedef unsigned int rtaudio_stream_flags_t;
typedef unsigned int rtaudio_stream_status_t;
typedef int (*rtaudio_cb_t)(void *out, void *in, unsigned int nFrames,
double stream_time, rtaudio_stream_status_t status,
void *userdata);
typedef enum rtaudio_error {
RTAUDIO_NO_ERROR = -1,
RTAUDIO_ERROR_WARNING,
RTAUDIO_ERROR_DEBUG_WARNING,
RTAUDIO_ERROR_UNSPECIFIED,
RTAUDIO_ERROR_NO_DEVICES_FOUND,
RTAUDIO_ERROR_INVALID_DEVICE,
RTAUDIO_ERROR_MEMORY_ERROR,
RTAUDIO_ERROR_INVALID_PARAMETER,
RTAUDIO_ERROR_INVALID_USE,
RTAUDIO_ERROR_DRIVER_ERROR,
RTAUDIO_ERROR_SYSTEM_ERROR,
RTAUDIO_ERROR_THREAD_ERROR,
} rtaudio_error_t;
typedef void (*rtaudio_error_cb_t)(rtaudio_error_t err, const char *msg);
typedef enum rtaudio_api {
RTAUDIO_API_UNSPECIFIED,
RTAUDIO_API_LINUX_ALSA,
RTAUDIO_API_LINUX_PULSE,
RTAUDIO_API_LINUX_OSS,
RTAUDIO_API_UNIX_JACK,
RTAUDIO_API_MACOSX_CORE,
RTAUDIO_API_WINDOWS_WASAPI,
RTAUDIO_API_WINDOWS_ASIO,
RTAUDIO_API_WINDOWS_DS,
RTAUDIO_API_DUMMY,
RTAUDIO_API_NUM,
} rtaudio_api_t;
typedef struct rtaudio_device_info {
int probed;
unsigned int output_channels;
unsigned int input_channels;
unsigned int duplex_channels;
int is_default_output;
int is_default_input;
rtaudio_format_t native_formats;
unsigned int preferred_sample_rate;
int sample_rates[16];
char name[512];
} rtaudio_device_info_t;
typedef struct rtaudio_stream_parameters {
unsigned int device_id;
unsigned int num_channels;
unsigned int first_channel;
} rtaudio_stream_parameters_t;
typedef struct rtaudio_stream_options {
rtaudio_stream_flags_t flags;
unsigned int num_buffers;
int priority;
char name[512];
} rtaudio_stream_options_t;
typedef struct rtaudio *rtaudio_t;
const char *rtaudio_version(void);
unsigned int rtaudio_get_num_compiled_apis(void);
const rtaudio_api_t *rtaudio_compiled_api(void);
const char *rtaudio_api_name(rtaudio_api_t api);
const char *rtaudio_api_display_name(rtaudio_api_t api);
rtaudio_api_t rtaudio_compiled_api_by_name(const char *name);
const char *rtaudio_error(rtaudio_t audio);
rtaudio_error_t rtaudio_error_type(rtaudio_t audio);
rtaudio_t rtaudio_create(rtaudio_api_t api);
void rtaudio_destroy(rtaudio_t audio);
rtaudio_api_t rtaudio_current_api(rtaudio_t audio);
int rtaudio_device_count(rtaudio_t audio);
rtaudio_device_info_t rtaudio_get_device_info(rtaudio_t audio,
int i);
unsigned int rtaudio_get_default_output_device(rtaudio_t audio);
unsigned int rtaudio_get_default_input_device(rtaudio_t audio);
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);
void rtaudio_close_stream(rtaudio_t audio);
int rtaudio_start_stream(rtaudio_t audio);
int rtaudio_stop_stream(rtaudio_t audio);
int rtaudio_abort_stream(rtaudio_t audio);
int rtaudio_is_stream_open(rtaudio_t audio);
int rtaudio_is_stream_running(rtaudio_t audio);
double rtaudio_get_stream_time(rtaudio_t audio);
void rtaudio_set_stream_time(rtaudio_t audio, double time);
int rtaudio_get_stream_latency(rtaudio_t audio);
unsigned int rtaudio_get_stream_sample_rate(rtaudio_t audio);
void rtaudio_show_warnings(rtaudio_t audio, int show);]]
ffi_cdef[[static const int RTAUDIO_FORMAT_SINT8 = 0x01;
static const int RTAUDIO_FORMAT_SINT16 = 0x02;
static const int RTAUDIO_FORMAT_SINT24 = 0x04;
static const int RTAUDIO_FORMAT_SINT32 = 0x08;
static const int RTAUDIO_FORMAT_FLOAT32 = 0x10;
static const int RTAUDIO_FORMAT_FLOAT64 = 0x20;
static const int RTAUDIO_FLAGS_NONINTERLEAVED = 0x1;
static const int RTAUDIO_FLAGS_MINIMIZE_LATENCY = 0x2;
static const int RTAUDIO_FLAGS_HOG_DEVICE = 0x4;
static const int RTAUDIO_FLAGS_SCHEDULE_REALTIME = 0x8;
static const int RTAUDIO_FLAGS_ALSA_USE_DEFAULT = 0x10;
static const int RTAUDIO_FLAGS_JACK_DONT_CONNECT = 0x20;
static const int RTAUDIO_STATUS_INPUT_OVERFLOW = 0x1;
static const int RTAUDIO_STATUS_OUTPUT_UNDERFLOW = 0x2;
static const int NUM_SAMPLE_RATES = 16;
static const int MAX_NAME_LENGTH = 512;]]
local lib = ffi.load"rtaudio"
local M = {C=lib}
local rtaudio_t = {}
rtaudio_t.__index = rtaudio_t
function rtaudio_t:__new(api)
local ret = lib.rtaudio_create(api)
ffi.gc(ret,lib.rtaudio_destroy)
return ret
end
function rtaudio_t:destroy()
ffi.gc(self,nil)
return lib.rtaudio_destroy(self)
end
function rtaudio_t:abort_stream()
return lib.rtaudio_abort_stream(self)
end
function rtaudio_t:close_stream()
return lib.rtaudio_close_stream(self)
end
function rtaudio_t:current_api()
return lib.rtaudio_current_api(self)
end
function rtaudio_t:device_count()
return lib.rtaudio_device_count(self)
end
function rtaudio_t:error()
local ret = lib.rtaudio_error(self)
if ret==nil then return nil else return ffi.string(ret) end
end
function rtaudio_t:error_type()
return lib.rtaudio_error_type(self)
end
function rtaudio_t:get_default_input_device()
return lib.rtaudio_get_default_input_device(self)
end
function rtaudio_t:get_default_output_device()
return lib.rtaudio_get_default_output_device(self)
end
function rtaudio_t:get_device_info(i)
return lib.rtaudio_get_device_info(self,i)
end
function rtaudio_t:get_stream_latency()
return lib.rtaudio_get_stream_latency(self)
end
function rtaudio_t:get_stream_sample_rate()
return lib.rtaudio_get_stream_sample_rate(self)
end
function rtaudio_t:get_stream_time()
return lib.rtaudio_get_stream_time(self)
end
function rtaudio_t:is_stream_open()
return lib.rtaudio_is_stream_open(self)
end
function rtaudio_t:is_stream_running()
return lib.rtaudio_is_stream_running(self)
end
function rtaudio_t:open_stream(output_params, input_params, format, sample_rate, buffer_frames, cb, userdata, options, errcb)
return lib.rtaudio_open_stream(self,output_params, input_params, format, sample_rate, buffer_frames, cb, userdata, options, errcb)
end
function rtaudio_t:set_stream_time(time)
return lib.rtaudio_set_stream_time(self,time)
end
function rtaudio_t:show_warnings(show)
return lib.rtaudio_show_warnings(self,show)
end
function rtaudio_t:start_stream()
return lib.rtaudio_start_stream(self)
end
function rtaudio_t:stop_stream()
return lib.rtaudio_stop_stream(self)
end
ffi.cdef"typedef struct rtaudio rtaudio_type"
M.rtaudio = ffi.metatype("rtaudio_type",rtaudio_t)
local callback_t
local callbacks_anchor = {}
function M.MakeAudioCallback(func, ...)
if not callback_t then
local CallbackFactory = require "lj-async.callback"
callback_t = CallbackFactory("int(*)(void*,void*,unsigned int,double,unsigned int,void*)") --"RtAudioCallback"
end
local cb = callback_t(func, ...)
table.insert(callbacks_anchor,cb)
return cb:funcptr() , cb
end
function M.GetAllInfo()
local I = {}
local formats = {
FORMAT_SINT8 = 0x01,
FORMAT_SINT16 = 0x02,
FORMAT_SINT24 = 0x04,
FORMAT_SINT32 = 0x08,
FORMAT_FLOAT32 = 0x10,
FORMAT_FLOAT64 = 0x20,
}
local function formats_tbl(ff)
local str = {}
for k,v in pairs(formats) do
if bit.band(ff,v)~=0 then
table.insert(str,k)
end
end
table.sort(str)
return str
end
local numcompiledapis = M.get_num_compiled_apis()
local compiledapis = M.compiled_api()
I = {APIS={},API={},APIbyNAME={}}
for i=0,numcompiledapis-1 do
I.APIS[i+1] = ffi.string(M.api_name(compiledapis[i]))
I.APIbyNAME[I.APIS[i+1]] = i
end
for i=0,numcompiledapis-1 do
local api = compiledapis[i]
local dac = M.create(api)
local apikey = ffi.string(M.api_name(api))
I.API[apikey] = {}
I.API[apikey].default_output = M.get_default_output_device(dac)
I.API[apikey].default_input = M.get_default_input_device(dac)
I.API[apikey].device_count = M.device_count(dac)
I.API[apikey].devices = {}
for i=0,M.device_count(dac)-1 do
local info = M.get_device_info(dac,i)
I.API[apikey].devices[i] = {}
I.API[apikey].devices[i].probed = info.probed>0
I.API[apikey].devices[i].name = ffi.string(info.name)
I.API[apikey].devices[i].output_channels = info.output_channels
I.API[apikey].devices[i].input_channels = info.input_channels
I.API[apikey].devices[i].duplex_channels = info.duplex_channels
I.API[apikey].devices[i].preferred_sample_rate = info.preferred_sample_rate
I.API[apikey].devices[i].is_default_output = info.is_default_output>0
I.API[apikey].devices[i].is_default_input = info.is_default_input>0
I.API[apikey].devices[i].native_formats = formats_tbl(info.native_formats)
I.API[apikey].devices[i].sample_rates = {}
--sample rates
for k=0,15 do
if info.sample_rates[k]==0 then break end
I.API[apikey].devices[i].sample_rates[k+1] = info.sample_rates[k]
end
end
M.destroy(dac)
end
return I
end
setmetatable(M,{
__index = function(t,k)
local ok,ptr = pcall(function(str) return lib["rtaudio_"..str] end,k)
if not ok then ok,ptr = pcall(function(str) return lib["RTAUDIO_"..str] end,k) end
if not ok then error(k.." not found") end
rawset(M, k, ptr)
return ptr
end
})
return M