/
init.c
812 lines (681 loc) · 21.1 KB
/
init.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#include <stdio.h>
#include <portaudio.h>
#include <luaT.h>
#include <lualib.h>
#include "TH.h"
/* TODO
(*) handle callback input as well as output
(*) callback for byte/char/int/float [macroize]
(*) (set)callback function
(*) freeup properly all that stuff
(*) special userdata which is destroyed at the end, such that Pa_Terminate() is called [have it as ref inside streams, so it is called really at the end]
(*) handle upvalues in callback creation?
(*) pourquoi a torch.rand(2,100) segfault si caller a la fin de toto.lua??
*/
typedef struct pa_stream__
{
PaStream *id;
int ninchannel;
int noutchannel;
PaSampleFormat insampleformat;
PaSampleFormat outsampleformat;
lua_State *pa_L;
const char *callbackerror;
} pa_Stream;
static void pa_checkerror(lua_State *L, PaError err)
{
if(err != paNoError)
luaL_error(L, "An error occured while using the portaudio stream: %s", Pa_GetErrorText(err));
}
/*
static const struct luaL_Reg sndfile_SndFile__ [] = {
{NULL, NULL}
};
*/
static int pa_version(lua_State *L)
{
int narg = lua_gettop(L);
if(narg != 0)
luaL_error(L, "invalid arguments: no argument expected");
lua_pushstring(L, Pa_GetVersionText());
return 1;
}
static int pa_hostapicount(lua_State *L)
{
int narg = lua_gettop(L);
if(narg != 0)
luaL_error(L, "invalid arguments: no argument expected");
lua_pushnumber(L, Pa_GetHostApiCount());
return 1;
}
static int pa_defaulthostapi(lua_State *L)
{
int narg = lua_gettop(L);
if(narg != 0)
luaL_error(L, "invalid arguments: no argument expected");
lua_pushnumber(L, Pa_GetDefaultHostApi()+1);
return 1;
}
static int pa_hostapiinfo(lua_State *L)
{
int narg = lua_gettop(L);
PaHostApiIndex apiidx = 0;
const PaHostApiInfo *info;
if(narg == 1 && lua_isnumber(L, 1))
apiidx = (PaHostApiIndex)lua_tonumber(L, 1)-1;
else
luaL_error(L, "expected arguments: number");
info = Pa_GetHostApiInfo(apiidx);
if(info)
{
lua_newtable(L);
lua_pushstring(L, info->name);
lua_setfield(L, -2, "name");
lua_pushnumber(L, info->deviceCount);
lua_setfield(L, -2, "devicecount");
lua_pushnumber(L, info->defaultInputDevice+1);
lua_setfield(L, -2, "defaultinputdevice");
lua_pushnumber(L, info->defaultOutputDevice+1);
lua_setfield(L, -2, "defaultoutputdevice");
return 1;
}
return 0;
}
static int pa_devicecount(lua_State *L)
{
int narg = lua_gettop(L);
if(narg != 0)
luaL_error(L, "invalid arguments: no argument expected");
lua_pushnumber(L, Pa_GetDeviceCount());
return 1;
}
static int pa_defaultinputdevice(lua_State *L)
{
int narg = lua_gettop(L);
PaDeviceIndex dev;
if(narg != 0)
luaL_error(L, "invalid arguments: no argument expected");
dev = Pa_GetDefaultInputDevice();
if(dev == paNoDevice)
return 0;
else
lua_pushnumber(L, dev+1);
return 1;
}
static int pa_defaultoutputdevice(lua_State *L)
{
int narg = lua_gettop(L);
PaDeviceIndex dev;
if(narg != 0)
luaL_error(L, "invalid arguments: no argument expected");
dev = Pa_GetDefaultOutputDevice();
if(dev == paNoDevice)
return 0;
else
lua_pushnumber(L, dev+1);
return 1;
}
static int pa_deviceinfo(lua_State *L)
{
int narg = lua_gettop(L);
PaDeviceIndex devidx = 0;
const PaDeviceInfo *info;
if(narg == 1 && lua_isnumber(L, 1))
devidx = (PaHostApiIndex)lua_tonumber(L, 1)-1;
else
luaL_error(L, "expected arguments: number");
info = Pa_GetDeviceInfo(devidx);
if(info)
{
lua_newtable(L);
lua_pushstring(L, info->name);
lua_setfield(L, -2, "name");
lua_pushnumber(L, info->hostApi+1);
lua_setfield(L, -2, "hostapi");
lua_pushnumber(L, info->maxInputChannels);
lua_setfield(L, -2, "maxinputchannels");
lua_pushnumber(L, info->maxOutputChannels);
lua_setfield(L, -2, "maxoutputchannels");
lua_pushnumber(L, info->defaultLowInputLatency);
lua_setfield(L, -2, "defaultlowinputlatency");
lua_pushnumber(L, info->defaultLowOutputLatency);
lua_setfield(L, -2, "defaultlowoutputlatency");
lua_pushnumber(L, info->defaultHighInputLatency);
lua_setfield(L, -2, "defaulthighinputlatency");
lua_pushnumber(L, info->defaultHighOutputLatency);
lua_setfield(L, -2, "defaulthighoutputlatency");
lua_pushnumber(L, info->defaultSampleRate);
lua_setfield(L, -2, "defaultsamplerate");
return 1;
}
return 0;
}
static void pa_readstreamparameters(lua_State *L, int idx, PaStreamParameters *params)
{
lua_getfield(L, idx, "device");
if(!lua_isnumber(L, -1))
luaL_error(L, "device field must be a number");
params->device = (PaDeviceIndex)(lua_tonumber(L, -1)-1);
lua_pop(L, 1);
lua_getfield(L, idx, "channelcount");
if(!lua_isnumber(L, -1))
luaL_error(L, "channelcount field must be a number");
params->channelCount = (int)(lua_tonumber(L, -1));
lua_pop(L, 1);
lua_getfield(L, idx, "sampleformat");
if(!lua_isnumber(L, -1))
luaL_error(L, "sampleformat field must be a number");
params->sampleFormat = (PaSampleFormat)(lua_tonumber(L, -1));
lua_pop(L, 1);
lua_getfield(L, idx, "suggestedlatency");
if(!lua_isnumber(L, -1))
luaL_error(L, "suggestedlatency field must be a number");
params->suggestedLatency = (PaTime)(lua_tonumber(L, -1));
lua_pop(L, 1);
params->hostApiSpecificStreamInfo = NULL;
}
static int pa_isformatsupported(lua_State *L)
{
int narg = lua_gettop(L);
PaStreamParameters inputparams;
PaStreamParameters outputparams;
double samplerate = 0;
int hasinput = 0;
int hasoutput = 0;
PaError err;
if(narg == 3
&& lua_istable(L, 1) && lua_istable(L, 2) && lua_isnumber(L, 3))
{
hasinput = 1;
hasoutput = 1;
pa_readstreamparameters(L, 1, &inputparams);
pa_readstreamparameters(L, 2, &outputparams);
samplerate = lua_tonumber(L, 3);
}
else if(narg == 3
&& lua_isnil(L, 1) && lua_istable(L, 2) && lua_isnumber(L, 3))
{
hasinput = 0;
hasoutput = 1;
pa_readstreamparameters(L, 2, &outputparams);
samplerate = lua_tonumber(L, 3);
}
else if(narg == 3
&& lua_istable(L, 1) && lua_isnil(L, 2) && lua_isnumber(L, 3))
{
hasinput = 1;
hasoutput = 0;
pa_readstreamparameters(L, 2, &inputparams);
samplerate = lua_tonumber(L, 3);
}
else
luaL_error(L, "expected arguments: (table or nil) (table or nil) number");
err = Pa_IsFormatSupported((hasinput ? &inputparams : NULL), (hasoutput ? &outputparams : NULL), samplerate);
if(err == paNoError)
{
lua_pushboolean(L, 1);
return 1;
}
else
{
lua_pushboolean(L, 0);
lua_pushstring(L, Pa_GetErrorText(err));
return 2;
}
return 0;
}
static int pa_sleep(lua_State *L)
{
int narg = lua_gettop(L);
long msec = 0;
if(narg == 1 && lua_isnumber(L, 1))
msec = (long)lua_tonumber(L, 1);
else
luaL_error(L, "expected arguments: number");
Pa_Sleep(msec);
return 0;
}
/* should be a macro with float, short, char, byte (cannot do the 24 bits right, even though i could convert from 32 bit tensor on the fly) */
static int streamcallbackshort(const void *input_, void *output_,
unsigned long frameCount,
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags,
void *userData_)
{
pa_Stream *stream = userData_;
static THShortStorage outstorage;
static THShortTensor outtensor;
static long size[2];
static long stride[2];
int ret;
size[0] = frameCount;
size[1] = stream->noutchannel;
stride[0] = 2;
stride[1] = 1;
outstorage.data = (short int*)output_;
outstorage.size = frameCount*stream->noutchannel;
outstorage.refcount = 0;
outstorage.flag = 0;
outtensor.size = size;
outtensor.stride = stride;
outtensor.nDimension = 2;
outtensor.storage = &outstorage;
outtensor.storageOffset = 0;
outtensor.refcount = 0;
outtensor.flag = 0;
lua_pushvalue(stream->pa_L, -1);
luaT_pushudata(stream->pa_L, &outtensor, "torch.ShortTensor");
if(lua_pcall(stream->pa_L, 1, 1, 0))
{
stream->callbackerror = lua_tostring(stream->pa_L, -1);
return paComplete;
}
if(lua_toboolean(stream->pa_L, -1))
ret = paContinue;
else
ret = paComplete;
lua_pop(stream->pa_L, 1);
return ret;
}
static int pa_writer(lua_State *L, const void* b, size_t size, void* B)
{
(void)L;
luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
return 0;
}
static void pa_setcallback__(lua_State *L, int idx, pa_Stream *stream)
{
luaL_Buffer b;
size_t l;
const char *str;
lua_State *pa_L = stream->pa_L;
if(!lua_isfunction(L, idx))
luaL_error(L, "internal error: provided callback is not a function");
lua_pushvalue(L, idx);
luaL_buffinit(L, &b);
if(lua_dump(L, pa_writer, &b) != 0)
{
lua_pop(L, 1);
luaL_error(L, "invalid callback function -- cannot be dumped");
}
luaL_pushresult(&b);
str = luaL_checklstring(L, -1, &l);
lua_settop(pa_L, 0);
if(luaL_loadbuffer(pa_L, str, l, NULL))
{
lua_pop(L, 2);
luaL_error(L, "could not load the callback function properly");
}
if(lua_pcall(pa_L, 0, LUA_MULTRET, 0))
{
lua_pop(L, 2);
luaL_error(L, "could not execute the callback function properly: %s", lua_tostring(pa_L, -1));
}
if(!((lua_gettop(pa_L) > 0) && lua_isfunction(pa_L, -1)))
{
lua_pop(L, 2);
luaL_error(L, "the callback function did not return a function");
}
lua_pop(L, 2);
}
static int pa_openstream(lua_State *L)
{
double samplerate = 0;
unsigned long nbufframe = 0;
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
int hascallback = 0;
PaStreamParameters inparams;
PaStreamParameters outparams;
PaStreamFlags flags = 0;
if((narg == 5 || (narg == 6 && lua_isfunction(L, 6)))
&& (lua_istable(L, 1) || lua_isnil(L, 1))
&& (lua_istable(L, 2) || lua_isnil(L, 2))
&& lua_isnumber(L, 3) && lua_isnumber(L, 4) && lua_isnumber(L, 5))
{
if(lua_istable(L, 1))
pa_readstreamparameters(L, 1, &inparams);
if(lua_istable(L, 2))
pa_readstreamparameters(L, 2, &outparams);
samplerate = (double)lua_tonumber(L, 3);
nbufframe = (unsigned long)lua_tonumber(L, 4);
flags = (PaStreamFlags)lua_tonumber(L, 5);
if(narg == 6)
hascallback = 1;
}
else
luaL_error(L, "expected arguments: (table | nil) (table | nil) number number number [function]");
stream = luaT_alloc(L, sizeof(pa_Stream));
stream->id = NULL;
stream->ninchannel = (lua_istable(L, 1) ? inparams.channelCount : 0);
stream->noutchannel = (lua_istable(L, 2) ? outparams.channelCount : 0);
stream->insampleformat = (lua_istable(L, 1) ? inparams.sampleFormat : 0);
stream->outsampleformat = (lua_istable(L, 2) ? outparams.sampleFormat : 0);
if(!(stream->pa_L = luaL_newstate()))
luaL_error(L, "could not allocate new state");
stream->callbackerror = NULL;
luaT_pushudata(L, stream, "pa.Stream");
luaL_openlibs(stream->pa_L);
if(hascallback)
pa_setcallback__(L, 6, stream);
pa_checkerror(L, Pa_OpenStream(&stream->id,
(lua_istable(L, 1) ? &inparams : NULL),
(lua_istable(L, 2) ? &outparams : NULL),
samplerate, nbufframe, flags,
(hascallback ? streamcallbackshort : NULL),
(hascallback ? stream : NULL)));
return 1;
}
static int pa_opendefaultstream(lua_State *L)
{
int numinchan = 0;
int numoutchan = 0;
PaSampleFormat sampleformat = 0;
double samplerate = 0;
unsigned long nbufframe = 0;
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
int hascallback = 0;
if((narg == 5 || (narg == 6 && lua_isfunction(L, 6))) &&
lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4) && lua_isnumber(L, 5))
{
numinchan = (int)lua_tonumber(L, 1);
numoutchan = (int)lua_tonumber(L, 2);
sampleformat = (PaSampleFormat)lua_tonumber(L, 3);
samplerate = (double)lua_tonumber(L, 4);
nbufframe = (unsigned long)lua_tonumber(L, 5);
if(narg == 6)
hascallback = 1;
}
else
luaL_error(L, "expected arguments: number number number number number [function]");
stream = luaT_alloc(L, sizeof(pa_Stream));
stream->id = NULL;
stream->ninchannel = numinchan;
stream->noutchannel = numoutchan;
stream->insampleformat = sampleformat;
stream->outsampleformat = sampleformat;
if(!(stream->pa_L = luaL_newstate()))
luaL_error(L, "could not allocate new state");
stream->callbackerror = NULL;
luaT_pushudata(L, stream, "pa.Stream");
luaL_openlibs(stream->pa_L);
if(hascallback)
pa_setcallback__(L, 6, stream);
pa_checkerror(L, Pa_OpenDefaultStream(&stream->id, numinchan, numoutchan, sampleformat, samplerate, nbufframe,
(hascallback ? streamcallbackshort : NULL),
(hascallback ? stream : NULL)));
return 1;
}
static int pa_stream_close(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
pa_checkerror(L, Pa_CloseStream(stream->id));
stream->id = NULL;
return 0;
}
static int pa_stream_free(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(stream->id)
Pa_CloseStream(stream->id);
/* should also free input/output buffers */
luaT_free(L, stream);
return 0;
}
static int pa_stream_start(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
pa_checkerror(L, Pa_StartStream(stream->id));
return 0;
}
static int pa_stream_abort(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
pa_checkerror(L, Pa_AbortStream(stream->id));
return 0;
}
static int pa_stream_stop(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
pa_checkerror(L, Pa_StopStream(stream->id));
return 0;
}
static int pa_stream_isstopped(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
PaError err = 0;
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
err = Pa_IsStreamStopped(stream->id);
if(err == 1)
lua_pushboolean(L, 1);
else if(err == 0)
lua_pushboolean(L, 0);
else
pa_checkerror(L, err);
return 1;
}
static int pa_stream_isactive(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
PaError err = 0;
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
err = Pa_IsStreamActive(stream->id);
if(err == 1)
lua_pushboolean(L, 1);
else if(err == 0)
lua_pushboolean(L, 0);
else
pa_checkerror(L, err);
return 1;
}
static int pa_stream_readavailable(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
PaError err = 0;
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
err = Pa_GetStreamReadAvailable(stream->id);
if(err >= 0)
lua_pushnumber(L, err);
else
pa_checkerror(L, err);
return 1;
}
static int pa_stream_writeavailable(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
PaError err = 0;
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
err = Pa_GetStreamWriteAvailable(stream->id);
if(err >= 0)
lua_pushnumber(L, err);
else
pa_checkerror(L, err);
return 1;
}
static int pa_stream_cpuload(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
lua_pushnumber(L, Pa_GetStreamCpuLoad(stream->id));
return 1;
}
static int pa_stream_writeShort(lua_State *L)
{
pa_Stream *stream = NULL;
THShortTensor *data = NULL;
long nelem = 0;
PaError err = 0;
int narg = lua_gettop(L);
if(narg == 2 && luaT_isudata(L, 1, "pa.Stream") && luaT_isudata(L, 2, "torch.ShortTensor"))
{
stream = luaT_toudata(L, 1, "pa.Stream");
data = luaT_toudata(L, 2, "torch.ShortTensor");
}
else
luaL_error(L, "expected arguments: Stream ShortTensor");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
nelem = THShortTensor_nElement(data);
luaL_argcheck(L, (nelem > 0) && (nelem % stream->noutchannel == 0), 2, "invalid data: number of elements must be > 0 and divisible by the number of channels");
luaL_argcheck(L, stream->outsampleformat & paInt16, 1, "stream does not support short data");
data = THShortTensor_newContiguous(data);
err = Pa_WriteStream(stream->id, THShortTensor_data(data), nelem/stream->noutchannel);
THShortTensor_free(data);
if(err == paOutputUnderflowed)
lua_pushboolean(L, 0);
else if(err == paNoError)
lua_pushboolean(L, 1);
else
pa_checkerror(L, err);
return 1;
}
static int pa_stream_callbackerror(lua_State *L)
{
pa_Stream *stream = NULL;
int narg = lua_gettop(L);
if(narg == 1 && luaT_isudata(L, 1, "pa.Stream"))
stream = luaT_toudata(L, 1, "pa.Stream");
else
luaL_error(L, "expected arguments: Stream");
if(!stream->id)
luaL_error(L, "attempt to operate on a closed stream");
if(Pa_IsStreamActive(stream->id))
luaL_error(L, "cannot check callback error on an active stream");
if(stream->callbackerror)
{
lua_pushstring(L, stream->callbackerror);
return 1;
}
return 0;
}
static const struct luaL_Reg pa_stream__ [] = {
{"close", pa_stream_close},
{"start", pa_stream_start},
{"abort", pa_stream_abort},
{"stop", pa_stream_stop},
{"isstopped", pa_stream_isstopped},
{"isactive", pa_stream_isactive},
{"readavailable", pa_stream_readavailable},
{"writeavailable", pa_stream_writeavailable},
{"cpuload", pa_stream_cpuload},
{"writeShort", pa_stream_writeShort},
{"callbackerror", pa_stream_callbackerror},
{NULL, NULL}
};
static const struct luaL_Reg pa_global__ [] = {
{"version", pa_version},
{"hostapicount", pa_hostapicount},
{"hostapicount", pa_hostapicount},
{"defaulthostapi", pa_defaulthostapi},
{"hostapiinfo", pa_hostapiinfo},
{"devicecount", pa_devicecount},
{"defaultinputdevice", pa_defaultinputdevice},
{"defaultoutputdevice", pa_defaultoutputdevice},
{"deviceinfo", pa_deviceinfo},
{"isformatsupported", pa_isformatsupported},
{"sleep", pa_sleep},
{"openstream", pa_openstream},
{"opendefaultstream", pa_opendefaultstream},
{NULL, NULL}
};
DLL_EXPORT int luaopen_libpa(lua_State *L)
{
if(sizeof(short) != 2)
luaL_error(L, "your platform has a strange size for short int type");
if(sizeof(int) != 4)
luaL_error(L, "your platform has a strange size for int type");
if(sizeof(char) != 1)
luaL_error(L, "your platform has a strange size for char type");
if(sizeof(float) != 4)
luaL_error(L, "your platform has a strange size for float type");
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, LUA_GLOBALSINDEX, "pa");
luaL_register(L, NULL, pa_global__);
lua_newtable(L);
lua_pushnumber(L, paFloat32);
lua_setfield(L, -2, "float32");
lua_pushnumber(L, paInt16);
lua_setfield(L, -2, "int16");
lua_pushnumber(L, paInt32);
lua_setfield(L, -2, "int32");
lua_pushnumber(L, paInt24);
lua_setfield(L, -2, "int24");
lua_pushnumber(L, paInt8);
lua_setfield(L, -2, "int8");
lua_pushnumber(L, paUInt8);
lua_setfield(L, -2, "uint8");
lua_pushnumber(L, paCustomFormat);
lua_setfield(L, -2, "customformat");
lua_pushnumber(L, paNonInterleaved);
lua_setfield(L, -2, "noninterleaved");
lua_setfield(L, -2, "format");
luaT_newmetatable(L, "pa.Stream", NULL, NULL, pa_stream_free, NULL);
luaL_register(L, NULL, pa_stream__);
lua_pop(L, 1);
pa_checkerror(L, Pa_Initialize());
return 1;
}