-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_receiver.c
324 lines (263 loc) · 11.2 KB
/
audio_receiver.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "audio_receiver.h"
#define AUPROC_CAPI_IMPLEMENT_GET_CAPI 1
#include "auproc_capi.h"
#define RECEIVER_CAPI_IMPLEMENT_GET_CAPI 1
#include "receiver_capi.h"
/* ============================================================================================ */
static const char* const AUDIO_RECEIVER_CLASS_NAME = "auproc.audio_receiver";
static const char* ERROR_INVALID_AUDIO_RECEIVER = "invalid auproc.audio_receiver";
/* ============================================================================================ */
typedef struct AudioReceiverUserData AudioReceiverUserData;
struct AudioReceiverUserData
{
const char* className;
auproc_processor* processor;
bool closed;
bool activated;
const auproc_capi* auprocCapi;
auproc_engine* auprocEngine;
auproc_connector* audioInConnector;
const auproc_audiometh* audioMethods;
const receiver_capi* receiverCapi;
receiver_object* receiver;
receiver_writer* receiverWriter;
};
/* ============================================================================================ */
static void setupAudioReceiverMeta(lua_State* L);
static int pushAudioReceiverMeta(lua_State* L)
{
if (luaL_newmetatable(L, AUDIO_RECEIVER_CLASS_NAME)) {
setupAudioReceiverMeta(L);
}
return 1;
}
/* ============================================================================================ */
static AudioReceiverUserData* checkAudioReceiverUdata(lua_State* L, int arg)
{
AudioReceiverUserData* udata = luaL_checkudata(L, arg, AUDIO_RECEIVER_CLASS_NAME);
const auproc_capi* auprocCapi = udata->auprocCapi;
auproc_engine* auprocEngine = udata->auprocEngine;
if (auprocCapi) {
auprocCapi->checkEngineIsNotClosed(L, auprocEngine);
}
if (udata->closed) {
luaL_error(L, ERROR_INVALID_AUDIO_RECEIVER);
return NULL;
}
return udata;
}
/* ============================================================================================ */
static int processCallback(uint32_t nframes, void* processorData)
{
AudioReceiverUserData* udata = (AudioReceiverUserData*) processorData;
const auproc_capi* auprocCapi = udata->auprocCapi;
auproc_engine* auprocEngine = udata->auprocEngine;
const auproc_audiometh* methods = udata->audioMethods;
float* inBuf = methods->getAudioBuffer(udata->audioInConnector, nframes);
const receiver_capi* receiverCapi = udata->receiverCapi;
receiver_object* receiver = udata->receiver;
receiver_writer* writer = udata->receiverWriter;
uint32_t t0 = auprocCapi->getProcessBeginFrameTime(auprocEngine);
if (receiver) {
int rc = receiverCapi->addIntegerToWriter(writer, t0);
unsigned char* data = NULL;
if (rc == 0) {
data = receiverCapi->addArrayToWriter(writer, RECEIVER_FLOAT, nframes);
}
if (data) {
memcpy(data, inBuf, nframes * sizeof(float));
rc = receiverCapi->msgToReceiver(receiver, writer, false /* clear */, false /* nonblock */,
NULL /* error handler */, NULL /* error handler data */);
}
if (!data || rc != 0) {
receiverCapi->clearWriter(writer);
}
}
return 0;
}
/* ============================================================================================ */
static void engineClosedCallback(void* processorData)
{
AudioReceiverUserData* udata = (AudioReceiverUserData*) processorData;
udata->closed = true;
udata->activated = false;
}
static void engineReleasedCallback(void* processorData)
{
AudioReceiverUserData* udata = (AudioReceiverUserData*) processorData;
udata->closed = true;
udata->activated = false;
udata->auprocCapi = NULL;
udata->auprocEngine = NULL;
}
/* ============================================================================================ */
static int AudioReceiver_new(lua_State* L)
{
const int conArg = 1;
const int recvArg = 2;
AudioReceiverUserData* udata = lua_newuserdata(L, sizeof(AudioReceiverUserData));
memset(udata, 0, sizeof(AudioReceiverUserData));
udata->className = AUDIO_RECEIVER_CLASS_NAME;
pushAudioReceiverMeta(L); /* -> udata, meta */
lua_setmetatable(L, -2); /* -> udata */
int versionError = 0;
const auproc_capi* capi = auproc_get_capi(L, conArg, &versionError);
auproc_engine* engine = NULL;
if (capi) {
engine = capi->getEngine(L, conArg, NULL);
}
if (!capi || !engine) {
if (versionError) {
return luaL_argerror(L, conArg, "auproc version mismatch");
} else {
return luaL_argerror(L, conArg, "expected connector object");
}
}
int errReason = 0;
const receiver_capi* receiverCapi = receiver_get_capi(L, recvArg, &errReason);
if (!receiverCapi) {
if (errReason == 1) {
return luaL_argerror(L, recvArg, "receiver capi version mismatch");
} else {
return luaL_argerror(L, recvArg, "expected object with receiver capi");
}
}
receiver_object* receiver = receiverCapi->toReceiver(L, recvArg);
if (!receiver) {
return luaL_argerror(L, recvArg, "expected object with receiver capi");
}
udata->receiverCapi = receiverCapi;
udata->receiver = receiver;
receiverCapi->retainReceiver(receiver);
udata->receiverWriter = receiverCapi->newWriter(16 * 1024, 1);
if (!udata->receiverWriter) {
return luaL_error(L, "out of memory");
}
const char* processorName = lua_pushfstring(L, "%s: %p", AUDIO_RECEIVER_CLASS_NAME, udata); /* -> udata, name */
auproc_con_reg conReg = {AUPROC_AUDIO, AUPROC_IN, NULL};
auproc_con_reg_err regError = {0};
auproc_processor* proc = capi->registerProcessor(L, conArg, 1, engine, processorName, udata,
processCallback, NULL, engineClosedCallback, engineReleasedCallback,
&conReg, ®Error);
lua_pop(L, 1); /* -> udata */
if (!proc)
{
if (regError.errorType == AUPROC_REG_ERR_CONNCTOR_INVALID) {
return luaL_argerror(L, conArg, "invalid connector object");
}
else if (regError.errorType == AUPROC_REG_ERR_ENGINE_MISMATCH)
{
const char* msg = lua_pushfstring(L, "connector belongs to other %s",
capi->engine_category_name);
return luaL_argerror(L, conArg, msg);
}
else if (regError.errorType == AUPROC_REG_ERR_ARG_INVALID
|| regError.errorType == AUPROC_REG_ERR_WRONG_DIRECTION
|| regError.errorType == AUPROC_REG_ERR_WRONG_CONNECTOR_TYPE)
{
return luaL_argerror(L, conArg, "expected AUDIO IN connector");
}
else {
return luaL_error(L, "cannot register processor (err=%d)", regError.errorType);
}
}
udata->processor = proc;
udata->activated = false;
udata->auprocCapi = capi;
udata->auprocEngine = engine;
udata->audioInConnector = conReg.connector;
udata->audioMethods = conReg.audioMethods;
return 1;
}
/* ============================================================================================ */
static int AudioReceiver_release(lua_State* L)
{
AudioReceiverUserData* udata = luaL_checkudata(L, 1, AUDIO_RECEIVER_CLASS_NAME);
udata->closed = true;
udata->activated = false;
if (udata->auprocCapi) {
udata->auprocCapi->unregisterProcessor(L, udata->auprocEngine, udata->processor);
udata->processor = NULL;
udata->auprocCapi = NULL;
udata->auprocEngine = NULL;
}
if (udata->receiver) {
if (udata->receiverWriter) {
udata->receiverCapi->freeWriter(udata->receiverWriter);
udata->receiverWriter = NULL;
}
udata->receiverCapi->releaseReceiver(udata->receiver);
udata->receiver = NULL;
udata->receiverCapi = NULL;
}
return 0;
}
/* ============================================================================================ */
static int AudioReceiver_toString(lua_State* L)
{
AudioReceiverUserData* udata = luaL_checkudata(L, 1, AUDIO_RECEIVER_CLASS_NAME);
lua_pushfstring(L, "%s: %p", AUDIO_RECEIVER_CLASS_NAME, udata);
return 1;
}
/* ============================================================================================ */
static int AudioReceiver_activate(lua_State* L)
{
AudioReceiverUserData* udata = checkAudioReceiverUdata(L, 1);
if (!udata->activated) {
udata->auprocCapi->activateProcessor(L, udata->auprocEngine, udata->processor);
udata->activated = true;
}
return 0;
}
/* ============================================================================================ */
static int AudioReceiver_deactivate(lua_State* L)
{
AudioReceiverUserData* udata = checkAudioReceiverUdata(L, 1);
if (udata->activated) {
udata->auprocCapi->deactivateProcessor(L, udata->auprocEngine, udata->processor);
udata->activated = false;
}
return 0;
}
/* ============================================================================================ */
static const luaL_Reg AudioReceiverMethods[] =
{
{ "activate", AudioReceiver_activate },
{ "deactivate", AudioReceiver_deactivate },
{ "close", AudioReceiver_release },
{ NULL, NULL } /* sentinel */
};
static const luaL_Reg AudioReceiverMetaMethods[] =
{
{ "__tostring", AudioReceiver_toString },
{ "__gc", AudioReceiver_release },
{ NULL, NULL } /* sentinel */
};
static const luaL_Reg ModuleFunctions[] =
{
{ "new_audio_receiver", AudioReceiver_new },
{ NULL, NULL } /* sentinel */
};
/* ============================================================================================ */
static void setupAudioReceiverMeta(lua_State* L)
{ /* -> meta */
lua_pushstring(L, AUDIO_RECEIVER_CLASS_NAME); /* -> meta, className */
lua_setfield(L, -2, "__metatable"); /* -> meta */
luaL_setfuncs(L, AudioReceiverMetaMethods, 0); /* -> meta */
lua_newtable(L); /* -> meta, AudioReceiverClass */
luaL_setfuncs(L, AudioReceiverMethods, 0); /* -> meta, AudioReceiverClass */
lua_setfield (L, -2, "__index"); /* -> meta */
}
/* ============================================================================================ */
int auproc_audio_receiver_init_module(lua_State* L, int module)
{
if (luaL_newmetatable(L, AUDIO_RECEIVER_CLASS_NAME)) {
setupAudioReceiverMeta(L);
}
lua_pop(L, 1);
lua_pushvalue(L, module);
luaL_setfuncs(L, ModuleFunctions, 0);
lua_pop(L, 1);
return 0;
}
/* ============================================================================================ */