forked from warhammerkid/ruby-audio
/
ra_sound.c
506 lines (438 loc) · 15.5 KB
/
ra_sound.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
#include "ra_sound.h"
extern VALUE eRubyAudioError;
/*
* Class <code>CSound</code> is a very light wrapper around the
* <code>SNDFILE</code> struct exposed by libsndfile.
*/
void Init_ra_sound() {
VALUE mRubyAudio = rb_define_module("RubyAudio");
VALUE cRASound = rb_define_class_under(mRubyAudio, "CSound", rb_cObject);
rb_define_alloc_func(cRASound, ra_sound_allocate);
rb_define_singleton_method(cRASound, "open", ra_sound_s_open, -1);
rb_define_method(cRASound, "initialize", ra_sound_init, 3);
rb_define_method(cRASound, "info", ra_sound_info, 0);
rb_define_method(cRASound, "seek", ra_sound_seek, 2);
rb_define_method(cRASound, "read", ra_sound_read, 2);
rb_define_method(cRASound, "write", ra_sound_write, 1);
rb_define_method(cRASound, "<<", ra_sound_addbuf, 1);
rb_define_method(cRASound, "close", ra_sound_close, 0);
rb_define_method(cRASound, "closed?", ra_sound_closed, 0);
}
static VALUE ra_sound_allocate(VALUE klass) {
RA_SOUND *snd = ALLOC(RA_SOUND);
memset(snd, 0, sizeof(RA_SOUND));
VALUE self = Data_Wrap_Struct(klass, ra_sound_mark, ra_sound_free, snd);
return self;
}
static void ra_sound_mark(RA_SOUND *snd) {
if(snd) {
rb_gc_mark(snd->info);
}
}
static void ra_sound_free(RA_SOUND *snd) {
if(!snd->closed && snd->snd != NULL) sf_close(snd->snd);
xfree(snd);
}
/*
* call-seq:
* CSound.open(...) => snd
* CSound.open(...) {|snd| block } => obj
*
* With no associated block, <code>open</code> is a synonym for
* <code>CSound.new</code>. If the optional code block is given, it will be
* passed <i>snd</i> as an argument, and the CSound object will automatically be
* closed when the block terminates. In this instance, <code>CSound.open</code>
* returns the value of the block.
*/
static VALUE ra_sound_s_open(int argc, VALUE *argv, VALUE klass) {
VALUE obj = rb_class_new_instance(argc, argv, klass);
if(!rb_block_given_p()) return obj;
return rb_ensure(rb_yield, obj, ra_sound_close_safe, obj);
}
/*
* call-seq:
* CSound.new(path, mode, info) => snd
*
* Returns a new <code>CSound</code> object for the audio file at the given path
* with the given mode. Valid modes are <code>"r"</code>, <code>"w"</code>, or
* <code>"rw"</code>.
*/
static VALUE ra_sound_init(VALUE self, VALUE path, VALUE mode, VALUE info) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
// Get mode
const char *m = StringValueCStr(mode);
if(strcmp(m, "rw") == 0) snd->mode = SFM_RDWR;
else if(strcmp(m, "r") == 0) snd->mode = SFM_READ;
else if(strcmp(m, "w") == 0) snd->mode = SFM_WRITE;
else rb_raise(rb_eArgError, "invalid access mode %s", m);
// Set info
snd->info = info;
// Open sound file
const char *p = StringValueCStr(path);
SF_INFO *sf_info;
Data_Get_Struct(info, SF_INFO, sf_info);
snd->snd = sf_open(p, snd->mode, sf_info);
if(snd->snd == NULL) rb_raise(eRubyAudioError, sf_strerror(snd->snd));
snd->closed = 0;
return self;
}
/*
* call-seq:
* snd.info => CSoundInfo
*
* Returns the info object associated with the sound.
*/
static VALUE ra_sound_info(VALUE self) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
return snd->info;
}
/*
* call-seq:
* snd.seek(frames, whence) => 0
*
* Seeks to a given offset <i>anInteger</i> in the sound according to the value
* of <i>whence</i>:
*
* IO::SEEK_CUR | Seeks to _frames_ plus current position
* --------------+----------------------------------------------------
* IO::SEEK_END | Seeks to _frames_ plus end of stream (you probably
* | want a negative value for _frames_)
* --------------+----------------------------------------------------
* IO::SEEK_SET | Seeks to the absolute location given by _frames_
*/
static VALUE ra_sound_seek(VALUE self, VALUE frames, VALUE whence) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
if(snd->closed) rb_raise(eRubyAudioError, "closed sound");
if(sf_seek(snd->snd, (sf_count_t)NUM2OFFT(frames), FIX2INT(whence)) == -1) {
rb_raise(eRubyAudioError, "invalid seek");
}
return INT2FIX(0);
}
static void ra_sound_read_short(RA_SOUND *snd, RA_BUFFER *buf, sf_count_t frames) {
static short temp[1024];
int temp_len = 1024;
short *data = (short*)buf->data;
short mix_sum;
// Get info struct
SF_INFO *info;
Data_Get_Struct(snd->info, SF_INFO, info);
// Up/Downmix based on channel matching
sf_count_t read = 0, r, amount;
int i, k;
if(buf->channels == info->channels) { // Simply read data without mix
read = sf_readf_short(snd->snd, data, frames);
} else if(buf->channels == 1) { // Downmix to mono
sf_count_t max = temp_len / info->channels;
int channels;
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > max) amount = max;
r = sf_readf_short(snd->snd, temp, amount);
if(r == 0) break;
// Mix channels together by averaging all channels and store to buffer
for(i = 0; i < r; i++) {
mix_sum = 0;
for(k = 0; k < info->channels; k++) mix_sum += temp[i * info->channels + k];
data[read] = mix_sum/info->channels;
read++;
}
}
} else if(info->channels == 1) { // Upmix from mono by copying channel
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > temp_len) amount = temp_len;
r = sf_readf_short(snd->snd, temp, amount);
if(r == 0) break;
// Write every frame channel times to the buffer
for(i = 0; i < r; i++) {
for(k = 0; k < buf->channels; k++) {
data[read * buf->channels + k] = temp[i];
}
read++;
}
}
} else {
rb_raise(eRubyAudioError, "unsupported mix from %d to %d", buf->channels, info->channels);
}
buf->real_size = read;
}
static void ra_sound_read_int(RA_SOUND *snd, RA_BUFFER *buf, sf_count_t frames) {
static int temp[1024];
int temp_len = 1024;
int *data = (int*)buf->data;
int mix_sum;
// Get info struct
SF_INFO *info;
Data_Get_Struct(snd->info, SF_INFO, info);
// Up/Downmix based on channel matching
sf_count_t read = 0, r, amount;
int i, k;
if(buf->channels == info->channels) { // Simply read data without mix
read = sf_readf_int(snd->snd, data, frames);
} else if(buf->channels == 1) { // Downmix to mono
sf_count_t max = temp_len / info->channels;
int channels;
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > max) amount = max;
r = sf_readf_int(snd->snd, temp, amount);
if(r == 0) break;
// Mix channels together by averaging all channels and store to buffer
for(i = 0; i < r; i++) {
mix_sum = 0;
for(k = 0; k < info->channels; k++) mix_sum += temp[i * info->channels + k];
data[read] = mix_sum/info->channels;
read++;
}
}
} else if(info->channels == 1) { // Upmix from mono by copying channel
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > temp_len) amount = temp_len;
r = sf_readf_int(snd->snd, temp, amount);
if(r == 0) break;
// Write every frame channel times to the buffer
for(i = 0; i < r; i++) {
for(k = 0; k < buf->channels; k++) {
data[read * buf->channels + k] = temp[i];
}
read++;
}
}
} else {
rb_raise(eRubyAudioError, "unsupported mix from %d to %d", buf->channels, info->channels);
}
buf->real_size = read;
}
static void ra_sound_read_float(RA_SOUND *snd, RA_BUFFER *buf, sf_count_t frames) {
static float temp[1024];
int temp_len = 1024;
float *data = (float*)buf->data;
float mix_sum;
// Get info struct
SF_INFO *info;
Data_Get_Struct(snd->info, SF_INFO, info);
// Up/Downmix based on channel matching
sf_count_t read = 0, r, amount;
int i, k;
if(buf->channels == info->channels) { // Simply read data without mix
read = sf_readf_float(snd->snd, data, frames);
} else if(buf->channels == 1) { // Downmix to mono
sf_count_t max = temp_len / info->channels;
int channels;
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > max) amount = max;
r = sf_readf_float(snd->snd, temp, amount);
if(r == 0) break;
// Mix channels together by averaging all channels and store to buffer
for(i = 0; i < r; i++) {
mix_sum = 0;
for(k = 0; k < info->channels; k++) mix_sum += temp[i * info->channels + k];
data[read] = mix_sum/info->channels;
read++;
}
}
} else if(info->channels == 1) { // Upmix from mono by copying channel
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > temp_len) amount = temp_len;
r = sf_readf_float(snd->snd, temp, amount);
if(r == 0) break;
// Write every frame channel times to the buffer
for(i = 0; i < r; i++) {
for(k = 0; k < buf->channels; k++) {
data[read * buf->channels + k] = temp[i];
}
read++;
}
}
} else {
rb_raise(eRubyAudioError, "unsupported mix from %d to %d", buf->channels, info->channels);
}
buf->real_size = read;
}
static void ra_sound_read_double(RA_SOUND *snd, RA_BUFFER *buf, sf_count_t frames) {
static double temp[1024];
int temp_len = 1024;
double *data = (double*)buf->data;
double mix_sum;
// Get info struct
SF_INFO *info;
Data_Get_Struct(snd->info, SF_INFO, info);
// Up/Downmix based on channel matching
sf_count_t read = 0, r, amount;
int i, k;
if(buf->channels == info->channels) { // Simply read data without mix
read = sf_readf_double(snd->snd, data, frames);
} else if(buf->channels == 1) { // Downmix to mono
sf_count_t max = temp_len / info->channels;
int channels;
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > max) amount = max;
r = sf_readf_double(snd->snd, temp, amount);
if(r == 0) break;
// Mix channels together by averaging all channels and store to buffer
for(i = 0; i < r; i++) {
mix_sum = 0;
for(k = 0; k < info->channels; k++) mix_sum += temp[i * info->channels + k];
data[read] = mix_sum/info->channels;
read++;
}
}
} else if(info->channels == 1) { // Upmix from mono by copying channel
while(read < frames) {
// Calculate # of frames to read
amount = frames - read;
if(amount > temp_len) amount = temp_len;
r = sf_readf_double(snd->snd, temp, amount);
if(r == 0) break;
// Write every frame channel times to the buffer
for(i = 0; i < r; i++) {
for(k = 0; k < buf->channels; k++) {
data[read * buf->channels + k] = temp[i];
}
read++;
}
}
} else {
rb_raise(eRubyAudioError, "unsupported mix from %d to %d", buf->channels, info->channels);
}
buf->real_size = read;
}
/*
* call-seq:
* snd.read(buf, frames) => integer
*
* Tries to read the given number of frames into the buffer and returns the
* number of frames actually read.
*/
static VALUE ra_sound_read(VALUE self, VALUE buf, VALUE frames) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
if(snd->closed) rb_raise(eRubyAudioError, "closed sound");
// Get buffer struct
RA_BUFFER *b;
Data_Get_Struct(buf, RA_BUFFER, b);
// Double-check frame count against buffer size
sf_count_t f = (sf_count_t)NUM2OFFT(frames);
if(f < 0 || f > b->size) {
rb_raise(eRubyAudioError, "frame count invalid");
}
// Shortcut for 0 frame reads
if(f == 0) {
b->real_size = 0;
return INT2FIX(b->real_size);;
}
// Read based on type
switch(b->type) {
case RA_BUFFER_TYPE_SHORT:
ra_sound_read_short(snd, b, f);
break;
case RA_BUFFER_TYPE_INT:
ra_sound_read_int(snd, b, f);
break;
case RA_BUFFER_TYPE_FLOAT:
ra_sound_read_float(snd, b, f);
break;
case RA_BUFFER_TYPE_DOUBLE:
ra_sound_read_double(snd, b, f);
break;
}
// Return read
return INT2FIX(b->real_size);
}
/*
* call-seq:
* snd.write(buf) => integer
*
* Writes the entire contents of the given buffer to the sound and returns the
* number of frames written.
*/
static VALUE ra_sound_write(VALUE self, VALUE buf) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
if(snd->closed) rb_raise(eRubyAudioError, "closed sound");
// Get buffer struct
RA_BUFFER *b;
Data_Get_Struct(buf, RA_BUFFER, b);
// Get info struct
SF_INFO *info;
Data_Get_Struct(snd->info, SF_INFO, info);
// Check buffer channels matches actual channels
if(b->channels != info->channels) {
rb_raise(eRubyAudioError, "channel count mismatch: %d vs %d", b->channels, info->channels);
}
// Write data
sf_count_t written = 0;
switch(b->type) {
case RA_BUFFER_TYPE_SHORT:
written = sf_writef_short(snd->snd, b->data, b->real_size);
break;
case RA_BUFFER_TYPE_INT:
written = sf_writef_int(snd->snd, b->data, b->real_size);
break;
case RA_BUFFER_TYPE_FLOAT:
written = sf_writef_float(snd->snd, b->data, b->real_size);
break;
case RA_BUFFER_TYPE_DOUBLE:
written = sf_writef_double(snd->snd, b->data, b->real_size);
break;
}
return OFFT2NUM(written);
}
/*
* call-seq:
* snd << buf => snd
*
* Writes the given buffer to the string.
*
* snd << buf1 << buf2
*/
static VALUE ra_sound_addbuf(VALUE self, VALUE buf) {
ra_sound_write(self, buf);
return self;
}
/*
* call-seq:
* snd.close => nil
*
* Closes <i>snd</i> and frees up all memory associated with the sound. The
* sound is unavailable for any further data operations; an error is raised if
* such an attempt is made. Sounds are automatically closed when they are claimed
* by the garbage collector.
*/
static VALUE ra_sound_close(VALUE self) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
if(snd->closed) rb_raise(eRubyAudioError, "closed sound");
sf_close(snd->snd);
snd->snd = NULL;
snd->closed = 1;
return Qnil;
}
static VALUE ra_sound_close_safe(VALUE self) {
return rb_rescue(ra_sound_close, self, 0, 0);
}
/*
* call-seq:
* snd.closed? => true or false
*
* Whether or not the current sound is closed to further operations.
*/
static VALUE ra_sound_closed(VALUE self) {
RA_SOUND *snd;
Data_Get_Struct(self, RA_SOUND, snd);
return snd->closed ? Qtrue : Qfalse;
}