| 1 | /**************************************************************************/ |
| 2 | /* audio_driver_web.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "audio_driver_web.h" |
| 32 | |
| 33 | #include "godot_audio.h" |
| 34 | |
| 35 | #include "core/config/project_settings.h" |
| 36 | #include "core/object/object.h" |
| 37 | #include "scene/main/node.h" |
| 38 | #include "servers/audio/audio_stream.h" |
| 39 | |
| 40 | #include <emscripten.h> |
| 41 | |
| 42 | AudioDriverWeb::AudioContext AudioDriverWeb::audio_context; |
| 43 | |
| 44 | bool AudioDriverWeb::is_available() { |
| 45 | return godot_audio_is_available() != 0; |
| 46 | } |
| 47 | |
| 48 | void AudioDriverWeb::_state_change_callback(int p_state) { |
| 49 | AudioDriverWeb::audio_context.state = p_state; |
| 50 | } |
| 51 | |
| 52 | void AudioDriverWeb::_latency_update_callback(float p_latency) { |
| 53 | AudioDriverWeb::audio_context.output_latency = p_latency; |
| 54 | } |
| 55 | |
| 56 | void AudioDriverWeb::_sample_playback_finished_callback(const char *p_playback_object_id) { |
| 57 | const ObjectID playback_id = ObjectID(String::to_int(p_playback_object_id)); |
| 58 | |
| 59 | Object *playback_object = ObjectDB::get_instance(playback_id); |
| 60 | if (playback_object == nullptr) { |
| 61 | return; |
| 62 | } |
| 63 | Ref<AudioSamplePlayback> playback = Object::cast_to<AudioSamplePlayback>(playback_object); |
| 64 | if (playback.is_null()) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | AudioServer::get_singleton()->stop_sample_playback(playback); |
| 69 | } |
| 70 | |
| 71 | void AudioDriverWeb::_audio_driver_process(int p_from, int p_samples) { |
| 72 | int32_t *stream_buffer = reinterpret_cast<int32_t *>(output_rb); |
| 73 | const int max_samples = memarr_len(output_rb); |
| 74 | |
| 75 | int write_pos = p_from; |
| 76 | int to_write = p_samples; |
| 77 | if (to_write == 0) { |
| 78 | to_write = max_samples; |
| 79 | } |
| 80 | // High part |
| 81 | if (write_pos + to_write > max_samples) { |
| 82 | const int samples_high = max_samples - write_pos; |
| 83 | audio_server_process(samples_high / channel_count, &stream_buffer[write_pos]); |
| 84 | for (int i = write_pos; i < max_samples; i++) { |
| 85 | output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f; |
| 86 | } |
| 87 | to_write -= samples_high; |
| 88 | write_pos = 0; |
| 89 | } |
| 90 | // Leftover |
| 91 | audio_server_process(to_write / channel_count, &stream_buffer[write_pos]); |
| 92 | for (int i = write_pos; i < write_pos + to_write; i++) { |
| 93 | output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void AudioDriverWeb::_audio_driver_capture(int p_from, int p_samples) { |
| 98 | if (get_input_buffer().is_empty()) { |
| 99 | return; // Input capture stopped. |
| 100 | } |
| 101 | const int max_samples = memarr_len(input_rb); |
| 102 | |
| 103 | int read_pos = p_from; |
| 104 | int to_read = p_samples; |
| 105 | if (to_read == 0) { |
| 106 | to_read = max_samples; |
| 107 | } |
| 108 | // High part |
| 109 | if (read_pos + to_read > max_samples) { |
| 110 | const int samples_high = max_samples - read_pos; |
| 111 | for (int i = read_pos; i < max_samples; i++) { |
| 112 | input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16)); |
| 113 | } |
| 114 | to_read -= samples_high; |
| 115 | read_pos = 0; |
| 116 | } |
| 117 | // Leftover |
| 118 | for (int i = read_pos; i < read_pos + to_read; i++) { |
| 119 | input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16)); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | Error AudioDriverWeb::init() { |
| 124 | int latency = Engine::get_singleton()->get_audio_output_latency(); |
| 125 | if (!audio_context.inited) { |
| 126 | audio_context.mix_rate = _get_configured_mix_rate(); |
| 127 | audio_context.channel_count = godot_audio_init(&audio_context.mix_rate, latency, &_state_change_callback, &_latency_update_callback); |
| 128 | audio_context.inited = true; |
| 129 | } |
| 130 | mix_rate = audio_context.mix_rate; |
| 131 | channel_count = audio_context.channel_count; |
| 132 | buffer_length = closest_power_of_2(uint32_t(latency * mix_rate / 1000)); |
| 133 | Error err = create(buffer_length, channel_count); |
| 134 | if (err != OK) { |
| 135 | return err; |
| 136 | } |
| 137 | if (output_rb) { |
| 138 | memdelete_arr(output_rb); |
| 139 | } |
| 140 | const size_t array_size = buffer_length * (size_t)channel_count; |
| 141 | output_rb = memnew_arr(float, array_size); |
| 142 | if (!output_rb) { |
| 143 | return ERR_OUT_OF_MEMORY; |
| 144 | } |
| 145 | if (input_rb) { |
| 146 | memdelete_arr(input_rb); |
| 147 | } |
| 148 | input_rb = memnew_arr(float, array_size); |
| 149 | if (!input_rb) { |
| 150 | return ERR_OUT_OF_MEMORY; |
| 151 | } |
| 152 | |
| 153 | godot_audio_sample_set_finished_callback(&_sample_playback_finished_callback); |
| 154 | |
| 155 | return OK; |
| 156 | } |
| 157 | |
| 158 | void AudioDriverWeb::start() { |
| 159 | start(output_rb, memarr_len(output_rb), input_rb, memarr_len(input_rb)); |
| 160 | } |
| 161 | |
| 162 | void AudioDriverWeb::resume() { |
| 163 | if (audio_context.state == 0) { // 'suspended' |
| 164 | godot_audio_resume(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | float AudioDriverWeb::get_latency() { |
| 169 | return audio_context.output_latency + (float(buffer_length) / mix_rate); |
| 170 | } |
| 171 | |
| 172 | int AudioDriverWeb::get_mix_rate() const { |
| 173 | return mix_rate; |
| 174 | } |
| 175 | |
| 176 | AudioDriver::SpeakerMode AudioDriverWeb::get_speaker_mode() const { |
| 177 | return get_speaker_mode_by_total_channels(channel_count); |
| 178 | } |
| 179 | |
| 180 | void AudioDriverWeb::finish() { |
| 181 | finish_driver(); |
| 182 | if (output_rb) { |
| 183 | memdelete_arr(output_rb); |
| 184 | output_rb = nullptr; |
| 185 | } |
| 186 | if (input_rb) { |
| 187 | memdelete_arr(input_rb); |
| 188 | input_rb = nullptr; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | Error AudioDriverWeb::input_start() { |
| 193 | lock(); |
| 194 | input_buffer_init(buffer_length); |
| 195 | unlock(); |
| 196 | if (godot_audio_input_start()) { |
| 197 | return FAILED; |
| 198 | } |
| 199 | return OK; |
| 200 | } |
| 201 | |
| 202 | Error AudioDriverWeb::input_stop() { |
| 203 | godot_audio_input_stop(); |
| 204 | lock(); |
| 205 | input_buffer.clear(); |
| 206 | unlock(); |
| 207 | return OK; |
| 208 | } |
| 209 | |
| 210 | bool AudioDriverWeb::is_stream_registered_as_sample(const Ref<AudioStream> &p_stream) const { |
| 211 | ERR_FAIL_COND_V_MSG(p_stream.is_null(), false, "Parameter p_stream is null."); |
| 212 | return godot_audio_sample_stream_is_registered(itos(p_stream->get_instance_id()).utf8().get_data()) != 0; |
| 213 | } |
| 214 | |
| 215 | void AudioDriverWeb::register_sample(const Ref<AudioSample> &p_sample) { |
| 216 | ERR_FAIL_COND_MSG(p_sample.is_null(), "Parameter p_sample is null."); |
| 217 | ERR_FAIL_COND_MSG(p_sample->stream.is_null(), "Parameter p_sample->stream is null."); |
| 218 | |
| 219 | String loop_mode; |
| 220 | switch (p_sample->loop_mode) { |
| 221 | case AudioSample::LoopMode::LOOP_DISABLED: { |
| 222 | loop_mode = "disabled"; |
| 223 | } break; |
| 224 | |
| 225 | case AudioSample::LoopMode::LOOP_FORWARD: { |
| 226 | loop_mode = "forward"; |
| 227 | } break; |
| 228 | |
| 229 | case AudioSample::LoopMode::LOOP_PINGPONG: { |
| 230 | loop_mode = "pingpong"; |
| 231 | } break; |
| 232 | |
| 233 | case AudioSample::LoopMode::LOOP_BACKWARD: { |
| 234 | loop_mode = "backward"; |
| 235 | } break; |
| 236 | } |
| 237 | |
| 238 | double length = p_sample->stream->get_length(); |
| 239 | |
| 240 | Vector<AudioFrame> frames; |
| 241 | int frames_total = mix_rate * length; |
| 242 | { |
| 243 | Ref<AudioStreamPlayback> stream_playback = p_sample->stream->instantiate_playback(); |
| 244 | frames.resize(frames_total); |
| 245 | AudioFrame *frames_ptr = frames.ptrw(); |
| 246 | stream_playback->start(); |
| 247 | stream_playback->mix(frames_ptr, 1.0f, frames_total); |
| 248 | } |
| 249 | |
| 250 | PackedFloat32Array data; |
| 251 | data.resize(frames_total * 2); |
| 252 | float *data_ptrw = data.ptrw(); |
| 253 | for (int i = 0; i < frames_total; i++) { |
| 254 | data_ptrw[i] = frames[i].left; |
| 255 | data_ptrw[i + frames_total] = frames[i].right; |
| 256 | } |
| 257 | |
| 258 | godot_audio_sample_register_stream( |
| 259 | itos(p_sample->stream->get_instance_id()).utf8().get_data(), |
| 260 | data_ptrw, |
| 261 | frames_total, |
| 262 | loop_mode.utf8().get_data(), |
| 263 | p_sample->loop_begin, |
| 264 | p_sample->loop_end); |
| 265 | } |
| 266 | |
| 267 | void AudioDriverWeb::unregister_sample(const Ref<AudioSample> &p_sample) { |
| 268 | ERR_FAIL_COND_MSG(p_sample.is_null(), "Parameter p_sample is null."); |
| 269 | ERR_FAIL_COND_MSG(p_sample->stream.is_null(), "Parameter p_sample->stream is null."); |
| 270 | |
| 271 | godot_audio_sample_unregister_stream(itos(p_sample->stream->get_instance_id()).utf8().get_data()); |
| 272 | } |
| 273 | |
| 274 | void AudioDriverWeb::start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { |
| 275 | ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); |
| 276 | ERR_FAIL_COND_MSG(p_playback->stream.is_null(), "Parameter p_playback->stream is null."); |
| 277 | |
| 278 | constexpr int real_max_channels = AudioServer::MAX_CHANNELS_PER_BUS * 2; |
| 279 | PackedFloat32Array volume; |
| 280 | volume.resize(real_max_channels); |
| 281 | float *volume_ptrw = volume.ptrw(); |
| 282 | for (int i = 0; i < real_max_channels; i += 2) { |
| 283 | if (p_playback->volume_vector.is_empty()) { |
| 284 | volume_ptrw[i] = 0; |
| 285 | volume_ptrw[i + 1] = 0; |
| 286 | } else { |
| 287 | const AudioFrame &frame = p_playback->volume_vector[i / 2]; |
| 288 | volume_ptrw[i] = frame.left; |
| 289 | volume_ptrw[i + 1] = frame.right; |
| 290 | } |
| 291 | } |
| 292 | godot_audio_sample_start( |
| 293 | itos(p_playback->get_instance_id()).utf8().get_data(), |
| 294 | itos(p_playback->stream->get_instance_id()).utf8().get_data(), |
| 295 | AudioServer::get_singleton()->get_bus_index(p_playback->bus), |
| 296 | p_playback->offset, |
| 297 | p_playback->pitch_scale, |
| 298 | volume_ptrw); |
| 299 | } |
| 300 | |
| 301 | void AudioDriverWeb::stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { |
| 302 | ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); |
| 303 | godot_audio_sample_stop(itos(p_playback->get_instance_id()).utf8().get_data()); |
| 304 | } |
| 305 | |
| 306 | void AudioDriverWeb::set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) { |
| 307 | ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); |
| 308 | godot_audio_sample_set_pause(itos(p_playback->get_instance_id()).utf8().get_data(), p_paused); |
| 309 | } |
| 310 | |
| 311 | bool AudioDriverWeb::is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) { |
| 312 | ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null."); |
| 313 | return godot_audio_sample_is_active(itos(p_playback->get_instance_id()).utf8().get_data()) != 0; |
| 314 | } |
| 315 | |
| 316 | double AudioDriverWeb::get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) { |
| 317 | ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null."); |
| 318 | return godot_audio_get_sample_playback_position(itos(p_playback->get_instance_id()).utf8().get_data()); |
| 319 | } |
| 320 | |
| 321 | void AudioDriverWeb::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) { |
| 322 | ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); |
| 323 | godot_audio_sample_update_pitch_scale( |
| 324 | itos(p_playback->get_instance_id()).utf8().get_data(), |
| 325 | p_pitch_scale); |
| 326 | } |
| 327 | |
| 328 | void AudioDriverWeb::set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) { |
| 329 | ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); |
| 330 | |
| 331 | constexpr int real_max_channels = AudioServer::MAX_CHANNELS_PER_BUS * 2; |
| 332 | |
| 333 | PackedInt32Array buses; |
| 334 | buses.resize(p_bus_volumes.size()); |
| 335 | int32_t *buses_ptrw = buses.ptrw(); |
| 336 | PackedFloat32Array values; |
| 337 | values.resize(p_bus_volumes.size() * AudioServer::MAX_CHANNELS_PER_BUS * 2); |
| 338 | float *values_ptrw = values.ptrw(); |
| 339 | int idx = 0; |
| 340 | for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) { |
| 341 | int bus_index = AudioServer::get_singleton()->get_bus_index(pair.key); |
| 342 | buses_ptrw[idx] = bus_index; |
| 343 | ERR_FAIL_COND(pair.value.size() != AudioServer::MAX_CHANNELS_PER_BUS); |
| 344 | for (int i = 0; i < real_max_channels; i += 2) { |
| 345 | const AudioFrame &frame = pair.value[i / 2]; |
| 346 | values_ptrw[(idx * real_max_channels) + i] = frame.left; |
| 347 | values_ptrw[(idx * real_max_channels) + i + 1] = frame.right; |
| 348 | } |
| 349 | idx++; |
| 350 | } |
| 351 | godot_audio_sample_set_volumes_linear( |
| 352 | itos(p_playback->get_instance_id()).utf8().get_data(), |
| 353 | buses_ptrw, |
| 354 | buses.size(), |
| 355 | values_ptrw, |
| 356 | values.size()); |
| 357 | } |
| 358 | |
| 359 | void AudioDriverWeb::set_sample_bus_count(int p_count) { |
| 360 | godot_audio_sample_bus_set_count(p_count); |
| 361 | } |
| 362 | |
| 363 | void AudioDriverWeb::remove_sample_bus(int p_index) { |
| 364 | godot_audio_sample_bus_remove(p_index); |
| 365 | } |
| 366 | |
| 367 | void AudioDriverWeb::add_sample_bus(int p_at_pos) { |
| 368 | godot_audio_sample_bus_add(p_at_pos); |
| 369 | } |
| 370 | |
| 371 | void AudioDriverWeb::move_sample_bus(int p_bus, int p_to_pos) { |
| 372 | godot_audio_sample_bus_move(p_bus, p_to_pos); |
| 373 | } |
| 374 | |
| 375 | void AudioDriverWeb::set_sample_bus_send(int p_bus, const StringName &p_send) { |
| 376 | godot_audio_sample_bus_set_send(p_bus, AudioServer::get_singleton()->get_bus_index(p_send)); |
| 377 | } |
| 378 | |
| 379 | void AudioDriverWeb::set_sample_bus_volume_db(int p_bus, float p_volume_db) { |
| 380 | godot_audio_sample_bus_set_volume_db(p_bus, p_volume_db); |
| 381 | } |
| 382 | |
| 383 | void AudioDriverWeb::set_sample_bus_solo(int p_bus, bool p_enable) { |
| 384 | godot_audio_sample_bus_set_solo(p_bus, p_enable); |
| 385 | } |
| 386 | |
| 387 | void AudioDriverWeb::set_sample_bus_mute(int p_bus, bool p_enable) { |
| 388 | godot_audio_sample_bus_set_mute(p_bus, p_enable); |
| 389 | } |
| 390 | |
| 391 | #ifdef THREADS_ENABLED |
| 392 | |
| 393 | /// AudioWorkletNode implementation (threads) |
| 394 | void AudioDriverWorklet::_audio_thread_func(void *p_data) { |
| 395 | AudioDriverWorklet *driver = static_cast<AudioDriverWorklet *>(p_data); |
| 396 | const int out_samples = memarr_len(driver->get_output_rb()); |
| 397 | const int in_samples = memarr_len(driver->get_input_rb()); |
| 398 | int wpos = 0; |
| 399 | int to_write = out_samples; |
| 400 | int rpos = 0; |
| 401 | int to_read = 0; |
| 402 | int32_t step = 0; |
| 403 | while (!driver->quit) { |
| 404 | if (to_read) { |
| 405 | driver->lock(); |
| 406 | driver->_audio_driver_capture(rpos, to_read); |
| 407 | godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_IN, -to_read); |
| 408 | driver->unlock(); |
| 409 | rpos += to_read; |
| 410 | if (rpos >= in_samples) { |
| 411 | rpos -= in_samples; |
| 412 | } |
| 413 | } |
| 414 | if (to_write) { |
| 415 | driver->lock(); |
| 416 | driver->_audio_driver_process(wpos, to_write); |
| 417 | godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_OUT, to_write); |
| 418 | driver->unlock(); |
| 419 | wpos += to_write; |
| 420 | if (wpos >= out_samples) { |
| 421 | wpos -= out_samples; |
| 422 | } |
| 423 | } |
| 424 | step = godot_audio_worklet_state_wait(driver->state, STATE_PROCESS, step, 1); |
| 425 | to_write = out_samples - godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_OUT); |
| 426 | to_read = godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_IN); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) { |
| 431 | if (!godot_audio_has_worklet()) { |
| 432 | return ERR_UNAVAILABLE; |
| 433 | } |
| 434 | return (Error)godot_audio_worklet_create(p_channels); |
| 435 | } |
| 436 | |
| 437 | void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) { |
| 438 | godot_audio_worklet_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, state); |
| 439 | thread.start(_audio_thread_func, this); |
| 440 | } |
| 441 | |
| 442 | void AudioDriverWorklet::lock() { |
| 443 | mutex.lock(); |
| 444 | } |
| 445 | |
| 446 | void AudioDriverWorklet::unlock() { |
| 447 | mutex.unlock(); |
| 448 | } |
| 449 | |
| 450 | void AudioDriverWorklet::finish_driver() { |
| 451 | quit = true; // Ask thread to quit. |
| 452 | thread.wait_to_finish(); |
| 453 | } |
| 454 | |
| 455 | #else // No threads. |
| 456 | |
| 457 | /// AudioWorkletNode implementation (no threads) |
| 458 | AudioDriverWorklet *AudioDriverWorklet::singleton = nullptr; |
| 459 | |
| 460 | Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) { |
| 461 | if (!godot_audio_has_worklet()) { |
| 462 | return ERR_UNAVAILABLE; |
| 463 | } |
| 464 | return (Error)godot_audio_worklet_create(p_channels); |
| 465 | } |
| 466 | |
| 467 | void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) { |
| 468 | _audio_driver_process(); |
| 469 | godot_audio_worklet_start_no_threads(p_out_buf, p_out_buf_size, &_process_callback, p_in_buf, p_in_buf_size, &_capture_callback); |
| 470 | } |
| 471 | |
| 472 | void AudioDriverWorklet::_process_callback(int p_pos, int p_samples) { |
| 473 | AudioDriverWorklet *driver = AudioDriverWorklet::get_singleton(); |
| 474 | driver->_audio_driver_process(p_pos, p_samples); |
| 475 | } |
| 476 | |
| 477 | void AudioDriverWorklet::_capture_callback(int p_pos, int p_samples) { |
| 478 | AudioDriverWorklet *driver = AudioDriverWorklet::get_singleton(); |
| 479 | driver->_audio_driver_capture(p_pos, p_samples); |
| 480 | } |
| 481 | |
| 482 | #endif // THREADS_ENABLED |
| 483 | |
| 484 | /// ScriptProcessorNode implementation |
| 485 | AudioDriverScriptProcessor *AudioDriverScriptProcessor::singleton = nullptr; |
| 486 | |
| 487 | void AudioDriverScriptProcessor::_process_callback() { |
| 488 | AudioDriverScriptProcessor::get_singleton()->_audio_driver_capture(); |
| 489 | AudioDriverScriptProcessor::get_singleton()->_audio_driver_process(); |
| 490 | } |
| 491 | |
| 492 | Error AudioDriverScriptProcessor::create(int &p_buffer_samples, int p_channels) { |
| 493 | if (!godot_audio_has_script_processor()) { |
| 494 | return ERR_UNAVAILABLE; |
| 495 | } |
| 496 | return (Error)godot_audio_script_create(&p_buffer_samples, p_channels); |
| 497 | } |
| 498 | |
| 499 | void AudioDriverScriptProcessor::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) { |
| 500 | godot_audio_script_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, &_process_callback); |
| 501 | } |