Module: Audio
- Defined in:
- ext/audio/audio.c
Class Method Summary collapse
-
.add_delay_tap(channel_id, time_ms, volume) ⇒ Object
.
- .duration(clip) ⇒ Object
-
.enable_reverb(channel_id, enabled) ⇒ Object
.
-
.init ⇒ Object
.
-
.load(file) ⇒ Object
.
-
.next_free_channel ⇒ Object
.
- .on_channel_freed(callback) ⇒ Object
- .pause(channel_id) ⇒ Object
-
.play(channel_id, clip) ⇒ Object
Playback.
- .remove_delay_tap(channel_id, tap_id) ⇒ Object
- .reset_all_channels ⇒ Object
- .resume(channel_id) ⇒ Object
- .seek(channel_id, seconds) ⇒ Object
- .set_delay_tap_time(channel_id, tap_id, time_ms) ⇒ Object
- .set_delay_tap_volume(channel_id, tap_id, volume) ⇒ Object
- .set_looping(channel_id, looping) ⇒ Object
- .set_pan(channel_id, pan) ⇒ Object
- .set_pitch(channel_id, pitch) ⇒ Object
- .set_pos(channel_id, angle, distance) ⇒ Object
- .set_reverb_damping(channel_id, damp) ⇒ Object
- .set_reverb_dry(channel_id, dry) ⇒ Object
- .set_reverb_room_size(channel_id, size) ⇒ Object
- .set_reverb_wet(channel_id, wet) ⇒ Object
-
.set_volume(channel_id, volume) ⇒ Object
.
- .stop(channel_id) ⇒ Object
Class Method Details
.add_delay_tap(channel_id, time_ms, volume) ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'ext/audio/audio.c', line 480 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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/audio/audio.c', line 163 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
548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'ext/audio/audio.c', line 548 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; } |
.init ⇒ Object
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 |
# File 'ext/audio/audio.c', line 94 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'ext/audio/audio.c', line 139 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); } |
.next_free_channel ⇒ Object
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'ext/audio/audio.c', line 617 VALUE audio_next_free_channel(VALUE self) { cleanup_finished_channels(-1); // Prefer fully drained channels to preserve reverb tails for (int i = 0; i < MAX_CHANNELS; i++) { if (channels[i] == NULL && drain_until_frame[i] == 0) { return rb_int2inum(i); } } // Fall back to draining channels if all else is exhausted for (int i = 0; i < MAX_CHANNELS; i++) { if (channels[i] == NULL) { return rb_int2inum(i); } } return rb_int2inum(-1); } |
.on_channel_freed(callback) ⇒ Object
666 667 668 669 670 671 672 673 674 675 676 |
# File 'ext/audio/audio.c', line 666 VALUE audio_on_channel_freed(VALUE self, VALUE callback) { if (channel_freed_callback != Qnil) { rb_gc_unregister_address(&channel_freed_callback); } channel_freed_callback = callback; rb_gc_register_address(&channel_freed_callback); return Qnil; } |
.pause(channel_id) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'ext/audio/audio.c', line 356 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
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 |
# File 'ext/audio/audio.c', line 224 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(channel); // Cancel any pending drain timer for this channel drain_until_frame[channel] = 0; // 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
500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'ext/audio/audio.c', line 500 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; } |
.reset_all_channels ⇒ Object
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 |
# File 'ext/audio/audio.c', line 638 VALUE audio_reset_all_channels(VALUE self) { for (int i = 0; i < MAX_CHANNELS; i++) { if (channels[i] != NULL) { ma_sound_stop(channels[i]); ma_sound_uninit(channels[i]); free(channels[i]); channels[i] = NULL; } if (delay_nodes[i] != NULL) { multi_tap_delay_uninit(delay_nodes[i]); free(delay_nodes[i]); delay_nodes[i] = NULL; } if (reverb_nodes[i] != NULL) { reverb_uninit(reverb_nodes[i]); free(reverb_nodes[i]); reverb_nodes[i] = NULL; } drain_until_frame[i] = 0; } return Qnil; } |
.resume(channel_id) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'ext/audio/audio.c', line 369 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; } |
.seek(channel_id, seconds) ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'ext/audio/audio.c', line 448 VALUE audio_seek(VALUE self, VALUE channel_id, VALUE seconds) { int channel = NUM2INT(channel_id); float s = (float)NUM2DBL(seconds); if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) { return Qnil; } ma_sound_seek_to_second(channels[channel], s); return Qnil; } |
.set_delay_tap_time(channel_id, tap_id, time_ms) ⇒ Object
529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'ext/audio/audio.c', line 529 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
514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'ext/audio/audio.c', line 514 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
462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'ext/audio/audio.c', line 462 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_pan(channel_id, pan) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'ext/audio/audio.c', line 434 VALUE audio_set_pan(VALUE self, VALUE channel_id, VALUE pan) { int channel = NUM2INT(channel_id); float p = (float)NUM2DBL(pan); if (channel < 0 || channel >= MAX_CHANNELS || channels[channel] == NULL) { return Qnil; } ma_sound_set_pan(channels[channel], p); return Qnil; } |
.set_pitch(channel_id, pitch) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'ext/audio/audio.c', line 400 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
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'ext/audio/audio.c', line 414 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
574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'ext/audio/audio.c', line 574 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
600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'ext/audio/audio.c', line 600 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
561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'ext/audio/audio.c', line 561 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
587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'ext/audio/audio.c', line 587 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
386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'ext/audio/audio.c', line 386 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
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'ext/audio/audio.c', line 335 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; } |