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
.
- .pause(channel_id) ⇒ Object
-
.play(channel_id, clip) ⇒ Object
.
- .remove_delay_tap(channel_id, tap_id) ⇒ Object
- .resume(channel_id) ⇒ 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_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
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'ext/audio/audio.c', line 399 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'ext/audio/audio.c', line 161 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
467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'ext/audio/audio.c', line 467 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
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 |
# File 'ext/audio/audio.c', line 92 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'ext/audio/audio.c', line 137 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
303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'ext/audio/audio.c', line 303 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
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 |
# File 'ext/audio/audio.c', line 183 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; } // 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
419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'ext/audio/audio.c', line 419 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
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'ext/audio/audio.c', line 316 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
448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'ext/audio/audio.c', line 448 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
433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'ext/audio/audio.c', line 433 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
381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'ext/audio/audio.c', line 381 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
347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'ext/audio/audio.c', line 347 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
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'ext/audio/audio.c', line 361 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
493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'ext/audio/audio.c', line 493 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
519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'ext/audio/audio.c', line 519 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
480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'ext/audio/audio.c', line 480 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
506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'ext/audio/audio.c', line 506 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
333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'ext/audio/audio.c', line 333 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
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'ext/audio/audio.c', line 289 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_sound_stop(channels[channel]); ma_sound_seek_to_pcm_frame(channels[channel], 0); return Qnil; } |