Module: Audio

Defined in:
ext/audio/audio.c

Class Method Summary collapse

Class Method Details

.add_delay_tap(channel_id, time_ms, volume) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'ext/audio/audio.c', line 443

VALUE audio_add_delay_tap(VALUE self, VALUE channel_id, VALUE time_ms, VALUE volume)
{
    int channel = NUM2INT(channel_id);
    float ms = (float)NUM2DBL(time_ms);
    float vol = (float)NUM2DBL(volume);

    if (channel < 0 || channel >= MAX_CHANNELS || delay_nodes[channel] == NULL) {
        rb_raise(rb_eArgError, "Invalid channel or no delay node: %d", channel);
        return Qnil;
    }

    int tap_id = multi_tap_delay_add_tap(delay_nodes[channel], ms, vol);
    if (tap_id < 0) {
        rb_raise(rb_eRuntimeError, "Failed to add delay tap (max taps reached)");
        return Qnil;
    }

    return rb_int2inum(tap_id);
}

.duration(clip) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/audio/audio.c', line 162

VALUE audio_duration(VALUE self, VALUE clip)
{
    int clip_id = NUM2INT(clip);

    if (clip_id < 0 || clip_id >= sound_count || sounds[clip_id] == NULL) {
        rb_raise(rb_eArgError, "Invalid clip ID: %d", clip_id);
        return Qnil;
    }

    float length;
    ma_result result = ma_sound_get_length_in_seconds(sounds[clip_id], &length);
    if (result != MA_SUCCESS) {
        return Qnil;
    }

    return rb_float_new(length);
}

.enable_reverb(channel_id, enabled) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
# File 'ext/audio/audio.c', line 511

VALUE audio_enable_reverb(VALUE self, VALUE channel_id, VALUE enabled)
{
    int channel = NUM2INT(channel_id);
    ma_bool32 en = RTEST(enabled) ? MA_TRUE : MA_FALSE;

    if (channel < 0 || channel >= MAX_CHANNELS || reverb_nodes[channel] == NULL) {
        return Qnil;
    }

    reverb_set_enabled(reverb_nodes[channel], en);
    return Qnil;
}

.initObject



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
# File 'ext/audio/audio.c', line 93

VALUE audio_init(VALUE self)
{
    if (engine_initialized) {
        return Qnil;
    }

    const char *driver = getenv("NATIVE_AUDIO_DRIVER");
    int use_null = (driver != NULL && strcmp(driver, "null") == 0);

    ma_engine_config config = ma_engine_config_init();
    config.listenerCount = 1;

    if (use_null) {
        ma_backend backends[] = { ma_backend_null };
        ma_result ctx_result = ma_context_init(backends, 1, NULL, &context);
        if (ctx_result != MA_SUCCESS) {
            rb_raise(rb_eRuntimeError, "Failed to initialize null audio context");
            return Qnil;
        }
        context_initialized = 1;
        using_null_backend = 1;
        config.pContext = &context;
    }

    ma_result result = ma_engine_init(&config, &engine);

    if (result != MA_SUCCESS) {
        if (context_initialized) {
            ma_context_uninit(&context);
            context_initialized = 0;
        }
        rb_raise(rb_eRuntimeError, "Failed to initialize audio engine");
        return Qnil;
    }

    engine_initialized = 1;
    rb_set_end_proc(cleanup_audio, Qnil);

    return Qnil;
}

.load(file) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/audio/audio.c', line 138

VALUE audio_load(VALUE self, VALUE file)
{
    const char *path = StringValueCStr(file);

    ma_sound *sound = (ma_sound *)malloc(sizeof(ma_sound));
    if (sound == NULL) {
        rb_raise(rb_eRuntimeError, "Failed to allocate memory for sound");
        return Qnil;
    }

    ma_result result = ma_sound_init_from_file(&engine, path, MA_SOUND_FLAG_DECODE, NULL, NULL, sound);
    if (result != MA_SUCCESS) {
        free(sound);
        rb_raise(rb_eRuntimeError, "Failed to load audio file: %s", path);
        return Qnil;
    }

    int id = sound_count;
    sounds[id] = sound;
    sound_count++;

    return rb_int2inum(id);
}

.pause(channel_id) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
# File 'ext/audio/audio.c', line 347

VALUE audio_pause(VALUE self, VALUE channel_id)
{
    int channel = NUM2INT(channel_id);

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    ma_sound_stop(channels[channel]);

    return Qnil;
}

.play(channel_id, clip) ⇒ Object

Playback



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
# File 'ext/audio/audio.c', line 218

VALUE audio_play(VALUE self, VALUE channel_id, VALUE clip)
{
    int channel = NUM2INT(channel_id);
    int clip_id = NUM2INT(clip);

    if (clip_id < 0 || clip_id >= sound_count || sounds[clip_id] == NULL) {
        rb_raise(rb_eArgError, "Invalid clip ID: %d", clip_id);
        return Qnil;
    }

    if (channel < 0 || channel >= MAX_CHANNELS) {
        rb_raise(rb_eArgError, "Invalid channel ID: %d", channel);
        return Qnil;
    }

    cleanup_finished_channels();

    // Clean up existing resources on this channel
    if (channels[channel] != NULL) {
        ma_sound_stop(channels[channel]);
        ma_sound_uninit(channels[channel]);
        free(channels[channel]);
        channels[channel] = NULL;
    }

    if (delay_nodes[channel] != NULL) {
        multi_tap_delay_uninit(delay_nodes[channel]);
        free(delay_nodes[channel]);
        delay_nodes[channel] = NULL;
    }

    if (reverb_nodes[channel] != NULL) {
        reverb_uninit(reverb_nodes[channel]);
        free(reverb_nodes[channel]);
        reverb_nodes[channel] = NULL;
    }

    // Create sound copy for playback
    ma_sound *playback = (ma_sound *)malloc(sizeof(ma_sound));
    if (playback == NULL) {
        rb_raise(rb_eRuntimeError, "Failed to allocate memory for playback");
        return Qnil;
    }

    ma_result result = ma_sound_init_copy(&engine, sounds[clip_id], MA_SOUND_FLAG_NO_DEFAULT_ATTACHMENT, NULL, playback);
    if (result != MA_SUCCESS) {
        free(playback);
        rb_raise(rb_eRuntimeError, "Failed to create sound copy for playback");
        return Qnil;
    }

    // Create delay node
    ma_uint32 sampleRate = ma_engine_get_sample_rate(&engine);
    ma_uint32 numChannels = ma_engine_get_channels(&engine);

    multi_tap_delay_node *delayNode = (multi_tap_delay_node *)malloc(sizeof(multi_tap_delay_node));
    if (delayNode == NULL) {
        ma_sound_uninit(playback);
        free(playback);
        rb_raise(rb_eRuntimeError, "Failed to allocate memory for delay node");
        return Qnil;
    }

    result = multi_tap_delay_init(delayNode, ma_engine_get_node_graph(&engine), sampleRate, numChannels);
    if (result != MA_SUCCESS) {
        free(delayNode);
        ma_sound_uninit(playback);
        free(playback);
        rb_raise(rb_eRuntimeError, "Failed to initialize delay node");
        return Qnil;
    }

    // Create reverb node
    reverb_node *reverbNode = (reverb_node *)malloc(sizeof(reverb_node));
    if (reverbNode == NULL) {
        multi_tap_delay_uninit(delayNode);
        free(delayNode);
        ma_sound_uninit(playback);
        free(playback);
        rb_raise(rb_eRuntimeError, "Failed to allocate memory for reverb node");
        return Qnil;
    }

    result = reverb_init(reverbNode, ma_engine_get_node_graph(&engine), sampleRate, numChannels);
    if (result != MA_SUCCESS) {
        free(reverbNode);
        multi_tap_delay_uninit(delayNode);
        free(delayNode);
        ma_sound_uninit(playback);
        free(playback);
        rb_raise(rb_eRuntimeError, "Failed to initialize reverb node");
        return Qnil;
    }

    // Route: sound -> delay_node -> reverb_node -> endpoint
    ma_node *endpoint = ma_engine_get_endpoint(&engine);
    ma_node_attach_output_bus(&reverbNode->base, 0, endpoint, 0);
    ma_node_attach_output_bus(&delayNode->base, 0, &reverbNode->base, 0);
    ma_node_attach_output_bus((ma_node *)playback, 0, &delayNode->base, 0);

    delay_nodes[channel] = delayNode;
    reverb_nodes[channel] = reverbNode;
    channels[channel] = playback;
    ma_sound_start(playback);

    return rb_int2inum(channel);
}

.remove_delay_tap(channel_id, tap_id) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'ext/audio/audio.c', line 463

VALUE audio_remove_delay_tap(VALUE self, VALUE channel_id, VALUE tap_id)
{
    int channel = NUM2INT(channel_id);
    int tap = NUM2INT(tap_id);

    if (channel < 0 || channel >= MAX_CHANNELS || delay_nodes[channel] == NULL) {
        return Qnil;
    }

    multi_tap_delay_remove_tap(delay_nodes[channel], tap);

    return Qnil;
}

.resume(channel_id) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
# File 'ext/audio/audio.c', line 360

VALUE audio_resume(VALUE self, VALUE channel_id)
{
    int channel = NUM2INT(channel_id);

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    ma_sound_start(channels[channel]);

    return Qnil;
}

.set_delay_tap_time(channel_id, tap_id, time_ms) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'ext/audio/audio.c', line 492

VALUE audio_set_delay_tap_time(VALUE self, VALUE channel_id, VALUE tap_id, VALUE time_ms)
{
    int channel = NUM2INT(channel_id);
    int tap = NUM2INT(tap_id);
    float ms = (float)NUM2DBL(time_ms);

    if (channel < 0 || channel >= MAX_CHANNELS || delay_nodes[channel] == NULL) {
        return Qnil;
    }

    multi_tap_delay_set_time(delay_nodes[channel], tap, ms);

    return Qnil;
}

.set_delay_tap_volume(channel_id, tap_id, volume) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'ext/audio/audio.c', line 477

VALUE audio_set_delay_tap_volume(VALUE self, VALUE channel_id, VALUE tap_id, VALUE volume)
{
    int channel = NUM2INT(channel_id);
    int tap = NUM2INT(tap_id);
    float vol = (float)NUM2DBL(volume);

    if (channel < 0 || channel >= MAX_CHANNELS || delay_nodes[channel] == NULL) {
        return Qnil;
    }

    multi_tap_delay_set_volume(delay_nodes[channel], tap, vol);

    return Qnil;
}

.set_looping(channel_id, looping) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'ext/audio/audio.c', line 425

VALUE audio_set_looping(VALUE self, VALUE channel_id, VALUE looping)
{
    int channel = NUM2INT(channel_id);
    ma_bool32 loop = RTEST(looping) ? MA_TRUE : MA_FALSE;

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    ma_sound_set_looping(channels[channel], loop);

    return Qnil;
}

.set_pitch(channel_id, pitch) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'ext/audio/audio.c', line 391

VALUE audio_set_pitch(VALUE self, VALUE channel_id, VALUE pitch)
{
    int channel = NUM2INT(channel_id);
    float p = (float)NUM2DBL(pitch);

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    ma_sound_set_pitch(channels[channel], p);

    return Qnil;
}

.set_pos(channel_id, angle, distance) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/audio/audio.c', line 405

VALUE audio_set_pos(VALUE self, VALUE channel_id, VALUE angle, VALUE distance)
{
    int channel = NUM2INT(channel_id);
    int ang = NUM2INT(angle);
    int dist = NUM2INT(distance);

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    float rad = ang * (MA_PI / 180.0f);
    float normalized_dist = dist / 255.0f;
    float x = normalized_dist * sinf(rad);
    float z = -normalized_dist * cosf(rad);

    ma_sound_set_position(channels[channel], x, 0.0f, z);

    return Qnil;
}

.set_reverb_damping(channel_id, damp) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
# File 'ext/audio/audio.c', line 537

VALUE audio_set_reverb_damping(VALUE self, VALUE channel_id, VALUE damp)
{
    int channel = NUM2INT(channel_id);
    float d = (float)NUM2DBL(damp);

    if (channel < 0 || channel >= MAX_CHANNELS || reverb_nodes[channel] == NULL) {
        return Qnil;
    }

    reverb_set_damping(reverb_nodes[channel], d);
    return Qnil;
}

.set_reverb_dry(channel_id, dry) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
# File 'ext/audio/audio.c', line 563

VALUE audio_set_reverb_dry(VALUE self, VALUE channel_id, VALUE dry)
{
    int channel = NUM2INT(channel_id);
    float d = (float)NUM2DBL(dry);

    if (channel < 0 || channel >= MAX_CHANNELS || reverb_nodes[channel] == NULL) {
        return Qnil;
    }

    reverb_set_dry(reverb_nodes[channel], d);
    return Qnil;
}

.set_reverb_room_size(channel_id, size) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
# File 'ext/audio/audio.c', line 524

VALUE audio_set_reverb_room_size(VALUE self, VALUE channel_id, VALUE size)
{
    int channel = NUM2INT(channel_id);
    float s = (float)NUM2DBL(size);

    if (channel < 0 || channel >= MAX_CHANNELS || reverb_nodes[channel] == NULL) {
        return Qnil;
    }

    reverb_set_room_size(reverb_nodes[channel], s);
    return Qnil;
}

.set_reverb_wet(channel_id, wet) ⇒ Object



550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/audio/audio.c', line 550

VALUE audio_set_reverb_wet(VALUE self, VALUE channel_id, VALUE wet)
{
    int channel = NUM2INT(channel_id);
    float w = (float)NUM2DBL(wet);

    if (channel < 0 || channel >= MAX_CHANNELS || reverb_nodes[channel] == NULL) {
        return Qnil;
    }

    reverb_set_wet(reverb_nodes[channel], w);
    return Qnil;
}

.set_volume(channel_id, volume) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'ext/audio/audio.c', line 377

VALUE audio_set_volume(VALUE self, VALUE channel_id, VALUE volume)
{
    int channel = NUM2INT(channel_id);
    int vol = NUM2INT(volume);

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    ma_sound_set_volume(channels[channel], vol / 128.0f);

    return Qnil;
}

.stop(channel_id) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'ext/audio/audio.c', line 326

VALUE audio_stop(VALUE self, VALUE channel_id)
{
    int channel = NUM2INT(channel_id);

    if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) {
        return Qnil;
    }

    ma_uint64 now = ma_engine_get_time_in_pcm_frames(&engine);
    ma_uint32 sample_rate = ma_engine_get_sample_rate(&engine);

    ma_sound_stop(channels[channel]);
    ma_sound_uninit(channels[channel]);
    free(channels[channel]);
    channels[channel] = NULL;

    drain_until_frame[channel] = now + (ma_uint64)(REVERB_DRAIN_SECONDS * sample_rate);

    return Qnil;
}