-
Notifications
You must be signed in to change notification settings - Fork 0
/
asound.c
421 lines (379 loc) · 11.2 KB
/
asound.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
#include "ruby.h"
#include <alsa/asoundlib.h>
static VALUE m_snd, m_seq;
static VALUE c_seq, c_event;
static VALUE
seq_allocate(VALUE klass)
{
VALUE result;
snd_seq_t **seq;
result = Data_Make_Struct(klass, snd_seq_t *, 0, free, seq);
snd_seq_open(seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
return result;
}
static VALUE
seq_set_client_name(VALUE self, VALUE name)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
snd_seq_set_client_name(*seq, STR2CSTR(name));
return Qnil;
}
static VALUE
seq_create_simple_port(VALUE self, VALUE name, VALUE caps, VALUE type)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
return INT2NUM(snd_seq_create_simple_port(*seq, STR2CSTR(name), NUM2INT(caps), NUM2INT(type)));
}
static VALUE
seq_connect_from(VALUE self, VALUE port, VALUE other_client, VALUE other_port)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
snd_seq_connect_from(*seq, NUM2INT(port), NUM2INT(other_client), NUM2INT(other_port));
return Qnil;
}
static VALUE
seq_connect_to(VALUE self, VALUE port, VALUE other_client, VALUE other_port)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
snd_seq_connect_to(*seq, NUM2INT(port), NUM2INT(other_client), NUM2INT(other_port));
return Qnil;
}
static VALUE
seq_event_output(VALUE self, VALUE event)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_t *, seq);
Data_Get_Struct(event, snd_seq_event_t, ev);
return INT2NUM(snd_seq_event_output(*seq, ev));
}
static VALUE
seq_event_output_direct(VALUE self, VALUE event)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_t *, seq);
Data_Get_Struct(event, snd_seq_event_t, ev);
return INT2NUM(snd_seq_event_output_direct(*seq, ev));
}
static VALUE
seq_drain_output(VALUE self)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
return INT2NUM(snd_seq_drain_output(*seq));
}
static VALUE
seq_drop_output(VALUE self)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
return INT2NUM(snd_seq_drop_output(*seq));
}
static VALUE
seq_alloc_queue(VALUE self)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
return INT2NUM(snd_seq_alloc_queue(*seq));
}
static VALUE
seq_event_input(VALUE self)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
int result;
Data_Get_Struct(self, snd_seq_t *, seq);
result = INT2NUM(snd_seq_event_input(*seq, &ev));
if (result >= 0) {
return Data_Wrap_Struct(c_event, 0, 0, ev);
}
else {
return Qnil;
}
}
static VALUE
seq_nonblock(VALUE self, VALUE flag)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
snd_seq_nonblock(*seq, RTEST(flag));
return Qnil;
}
static VALUE
seq_change_queue_ppq(VALUE self, VALUE queue, VALUE ppq)
{
snd_seq_t **seq;
Data_Get_Struct(self, snd_seq_t *, seq);
snd_seq_queue_tempo_t *tempo;
snd_seq_queue_tempo_alloca(&tempo);
snd_seq_queue_tempo_set_tempo(tempo, 2000000); // 120 BPM
snd_seq_queue_tempo_set_ppq(tempo, NUM2INT(ppq));
snd_seq_set_queue_tempo(*seq, NUM2INT(queue), tempo);
return Qnil;
}
static VALUE
seq_change_queue_tempo(VALUE self, VALUE queue, VALUE tempo, VALUE event)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_t *, seq);
if (NIL_P(event)) {
ev = NULL;
}
else {
Data_Get_Struct(event, snd_seq_event_t, ev);
}
snd_seq_change_queue_tempo(*seq, NUM2INT(queue), NUM2INT(tempo), ev);
return Qnil;
}
static VALUE
seq_start_queue(VALUE self, VALUE queue, VALUE event)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_t *, seq);
if (NIL_P(event)) {
ev = NULL;
}
else {
Data_Get_Struct(event, snd_seq_event_t, ev);
}
snd_seq_start_queue(*seq, NUM2INT(queue), ev);
return Qnil;
}
static VALUE
seq_stop_queue(VALUE self, VALUE queue, VALUE event)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_t *, seq);
if (NIL_P(event)) {
ev = NULL;
}
else {
Data_Get_Struct(event, snd_seq_event_t, ev);
}
snd_seq_stop_queue(*seq, NUM2INT(queue), ev);
return Qnil;
}
static VALUE
seq_continue_queue(VALUE self, VALUE queue, VALUE event)
{
snd_seq_t **seq;
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_t *, seq);
if (NIL_P(event)) {
ev = NULL;
}
else {
Data_Get_Struct(event, snd_seq_event_t, ev);
}
snd_seq_continue_queue(*seq, NUM2INT(queue), ev);
return Qnil;
}
static VALUE
seq_queue_get_tick_time(VALUE self, VALUE queue)
{
snd_seq_t **seq;
snd_seq_queue_status_t *info;
Data_Get_Struct(self, snd_seq_t *, seq);
snd_seq_queue_status_alloca(&info);
snd_seq_get_queue_status(*seq, NUM2INT(queue), info);
return INT2NUM(snd_seq_queue_status_get_tick_time(info));
}
static VALUE
ev_allocate(VALUE klass)
{
VALUE result;
snd_seq_event_t *ev;
result = Data_Make_Struct(klass, snd_seq_event_t, 0, free, ev);
snd_seq_ev_clear(ev);
return result;
}
static VALUE
ev_set_source(VALUE self, VALUE port)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_source(ev, NUM2INT(port));
return Qnil;
}
static VALUE
ev_set_subs(VALUE self)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_subs(ev);
return Qnil;
}
static VALUE
ev_set_direct(VALUE self)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_direct(ev);
return Qnil;
}
static VALUE
ev_type(VALUE self)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
return INT2NUM(ev->type);
}
static VALUE
ev_set_queue_pos_tick(VALUE self, VALUE queue, VALUE tick)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_queue_pos_tick(ev, NUM2INT(queue), NUM2INT(tick));
return Qnil;
}
static VALUE
ev_set_queue_tempo(VALUE self, VALUE queue, VALUE tempo)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_queue_tempo(ev, NUM2INT(queue), NUM2INT(tempo));
return Qnil;
}
static VALUE
ev_set_note(VALUE self, VALUE ch, VALUE note, VALUE velocity, VALUE duration)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_note(ev, NUM2INT(ch), NUM2INT(note), NUM2INT(velocity), NUM2INT(duration));
return Qnil;
}
static VALUE
ev_set_noteon(VALUE self, VALUE ch, VALUE note, VALUE velocity)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_noteon(ev, NUM2INT(ch), NUM2INT(note), NUM2INT(velocity));
return Qnil;
}
static VALUE
ev_set_noteoff(VALUE self, VALUE ch, VALUE note, VALUE velocity)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_noteoff(ev, NUM2INT(ch), NUM2INT(note), NUM2INT(velocity));
return Qnil;
}
static VALUE
ev_set_controller(VALUE self, VALUE ch, VALUE cc, VALUE value)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_controller(ev, NUM2INT(ch), NUM2INT(cc), NUM2INT(value));
return Qnil;
}
static VALUE
ev_set_pgmchange(VALUE self, VALUE ch, VALUE value)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_pgmchange(ev, NUM2INT(ch), NUM2INT(value));
return Qnil;
}
static VALUE
ev_set_pitchbend(VALUE self, VALUE ch, VALUE value)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_pitchbend(ev, NUM2INT(ch), NUM2INT(value));
return Qnil;
}
static VALUE
ev_set_sysex(VALUE self, VALUE data)
{
snd_seq_event_t *ev;
char *str;
long len;
str = rb_str2cstr(data, &len);
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_set_sysex(ev, len, str);
return INT2NUM(len);
}
static VALUE
ev_get_variable(VALUE self)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
return rb_str_new(ev->data.ext.ptr, ev->data.ext.len);
}
static VALUE
ev_schedule_tick(VALUE self, VALUE queue, VALUE relative, VALUE time)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
snd_seq_ev_schedule_tick(ev, NUM2INT(queue), NUM2INT(relative), NUM2INT(time));
return Qnil;
}
static VALUE
ev_get_type(VALUE self)
{
snd_seq_event_t *ev;
Data_Get_Struct(self, snd_seq_event_t, ev);
return INT2NUM(ev->type);
}
void
Init__asound(void)
{
m_snd = rb_define_module("Snd");
m_seq = rb_define_module_under(m_snd, "Seq");
#define CONST(name) rb_define_const(m_seq, #name, INT2NUM(SND_SEQ_##name))
CONST( PORT_CAP_WRITE );
CONST( PORT_CAP_SUBS_WRITE );
CONST( PORT_CAP_READ );
CONST( PORT_CAP_SUBS_READ );
CONST( PORT_TYPE_MIDI_GENERIC );
CONST( EVENT_NOTEON );
CONST( EVENT_NOTEOFF );
CONST( EVENT_CLOCK );
CONST( EVENT_SYSEX );
#undef CONST
c_seq = rb_define_class_under(m_seq, "Seq", rb_cObject);
rb_define_alloc_func(c_seq, seq_allocate);
rb_define_method(c_seq, "client_name=", seq_set_client_name, 1);
rb_define_method(c_seq, "nonblock=", seq_nonblock, 1);
rb_define_method(c_seq, "_create_simple_port", seq_create_simple_port, 3);
rb_define_method(c_seq, "connect_from", seq_connect_from, 3);
rb_define_method(c_seq, "connect_to", seq_connect_to, 3);
rb_define_method(c_seq, "event_output", seq_event_output, 1);
rb_define_method(c_seq, "event_output_direct", seq_event_output_direct, 1);
rb_define_method(c_seq, "drain_output", seq_drain_output, 0);
rb_define_method(c_seq, "drop_output", seq_drop_output, 0);
rb_define_method(c_seq, "event_input", seq_event_input, 0);
rb_define_method(c_seq, "_alloc_queue", seq_alloc_queue, 0);
rb_define_method(c_seq, "change_queue_ppq", seq_change_queue_ppq, 2);
rb_define_method(c_seq, "_change_queue_tempo", seq_change_queue_tempo, 3);
rb_define_method(c_seq, "_start_queue", seq_start_queue, 2);
rb_define_method(c_seq, "_stop_queue", seq_stop_queue, 2);
rb_define_method(c_seq, "_continue_queue", seq_continue_queue, 2);
rb_define_method(c_seq, "queue_get_tick_time", seq_queue_get_tick_time, 1);
c_event = rb_define_class_under(m_seq, "Event", rb_cObject);
rb_define_alloc_func(c_event, ev_allocate);
rb_define_method(c_event, "source=", ev_set_source, 1);
rb_define_method(c_event, "set_subs", ev_set_subs, 0);
rb_define_method(c_event, "set_direct", ev_set_direct, 0);
rb_define_method(c_event, "type", ev_type, 0);
rb_define_method(c_event, "set_queue_pos_tick", ev_set_queue_pos_tick, 2);
rb_define_method(c_event, "set_queue_tempo", ev_set_queue_tempo, 2);
rb_define_method(c_event, "set_note", ev_set_note, 4);
rb_define_method(c_event, "set_noteon", ev_set_noteon, 3);
rb_define_method(c_event, "set_noteoff", ev_set_noteoff, 3);
rb_define_method(c_event, "set_controller", ev_set_controller, 3);
rb_define_method(c_event, "set_pgmchange", ev_set_pgmchange, 2);
rb_define_method(c_event, "set_pitchbend", ev_set_pitchbend, 2);
rb_define_method(c_event, "set_sysex", ev_set_sysex, 1);
rb_define_method(c_event, "get_variable", ev_get_variable, 0);
rb_define_method(c_event, "schedule_tick", ev_schedule_tick, 3);
rb_define_method(c_event, "get_type", ev_get_type, 0);
}