diff --git a/servers/audio/effects/audio_effect_record.cpp b/servers/audio/effects/audio_effect_record.cpp index a0094f66b85..5a583243caa 100644 --- a/servers/audio/effects/audio_effect_record.cpp +++ b/servers/audio/effects/audio_effect_record.cpp @@ -44,20 +44,25 @@ void AudioEffectRecordInstance::process(const AudioFrame *p_src_frames, AudioFra } } +void AudioEffectRecordInstance::_update_buffer() { + //Case: Frames are remaining in the buffer + while (ring_buffer_read_pos < ring_buffer_pos) { + //Read from the buffer into recording_data + _io_store_buffer(); + } +} + +void AudioEffectRecordInstance::_update(void *userdata) { + AudioEffectRecordInstance *ins = (AudioEffectRecordInstance *)userdata; + ins->_update_buffer(); +} + bool AudioEffectRecordInstance::process_silence() const { return true; } void AudioEffectRecordInstance::_io_thread_process() { - - //Reset recorder status thread_active = true; - ring_buffer_pos = 0; - ring_buffer_read_pos = 0; - - //We start a new recording - recording_data.resize(0); //Clear data completely and reset length - is_recording = true; while (is_recording) { //Check: The current recording has been requested to stop @@ -65,13 +70,9 @@ void AudioEffectRecordInstance::_io_thread_process() { is_recording = false; } - //Case: Frames are remaining in the buffer - if (ring_buffer_read_pos < ring_buffer_pos) { - //Read from the buffer into recording_data - _io_store_buffer(); - } - //Case: The buffer is empty - else if (is_recording) { + _update_buffer(); + + if (is_recording) { //Wait to avoid too much busy-wait OS::get_singleton()->delay_usec(500); } @@ -103,7 +104,35 @@ void AudioEffectRecordInstance::_thread_callback(void *_instance) { } void AudioEffectRecordInstance::init() { + //Reset recorder status + ring_buffer_pos = 0; + ring_buffer_read_pos = 0; + + //We start a new recording + recording_data.resize(0); //Clear data completely and reset length + is_recording = true; + +#ifdef NO_THREADS + AudioServer::get_singleton()->add_update_callback(&AudioEffectRecordInstance::_update, this); +#else io_thread = Thread::create(_thread_callback, this); +#endif +} + +void AudioEffectRecordInstance::finish() { + +#ifdef NO_THREADS + AudioServer::get_singleton()->remove_update_callback(&AudioEffectRecordInstance::_update, this); +#else + if (thread_active) { + Thread::wait_to_finish(io_thread); + } +#endif +} + +AudioEffectRecordInstance::~AudioEffectRecordInstance() { + + finish(); } Ref AudioEffectRecord::instance() { @@ -145,8 +174,8 @@ Ref AudioEffectRecord::instance() { void AudioEffectRecord::ensure_thread_stopped() { recording_active = false; - if (current_instance != 0 && current_instance->thread_active) { - Thread::wait_to_finish(current_instance->io_thread); + if (current_instance != 0) { + current_instance->finish(); } } diff --git a/servers/audio/effects/audio_effect_record.h b/servers/audio/effects/audio_effect_record.h index 4b8ee2bcdde..c5e4866f176 100644 --- a/servers/audio/effects/audio_effect_record.h +++ b/servers/audio/effects/audio_effect_record.h @@ -62,14 +62,18 @@ class AudioEffectRecordInstance : public AudioEffectInstance { void _io_store_buffer(); static void _thread_callback(void *_instance); void _init_recording(); + void _update_buffer(); + static void _update(void *userdata); public: void init(); + void finish(); virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count); virtual bool process_silence() const; AudioEffectRecordInstance() : thread_active(false) {} + ~AudioEffectRecordInstance(); }; class AudioEffectRecord : public AudioEffect { diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index d36057b4659..f8e25387924 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -1004,6 +1004,11 @@ void AudioServer::update() { AudioDriver::get_singleton()->reset_profiling_time(); prof_time = 0; #endif + + for (Set::Element *E = update_callbacks.front(); E; E = E->next()) { + + E->get().callback(E->get().userdata); + } } void AudioServer::load_default_bus_layout() { @@ -1129,6 +1134,25 @@ void AudioServer::remove_callback(AudioCallback p_callback, void *p_userdata) { unlock(); } +void AudioServer::add_update_callback(AudioCallback p_callback, void *p_userdata) { + lock(); + CallbackItem ci; + ci.callback = p_callback; + ci.userdata = p_userdata; + update_callbacks.insert(ci); + unlock(); +} + +void AudioServer::remove_update_callback(AudioCallback p_callback, void *p_userdata) { + + lock(); + CallbackItem ci; + ci.callback = p_callback; + ci.userdata = p_userdata; + update_callbacks.erase(ci); + unlock(); +} + void AudioServer::set_bus_layout(const Ref &p_bus_layout) { ERR_FAIL_COND(p_bus_layout.is_null() || p_bus_layout->buses.size() == 0); diff --git a/servers/audio_server.h b/servers/audio_server.h index ba6569eb389..4f981eac2d3 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -262,6 +262,7 @@ private: }; Set callbacks; + Set update_callbacks; friend class AudioDriver; void _driver_process(int p_frames, int32_t *p_buffer); @@ -357,6 +358,9 @@ public: void add_callback(AudioCallback p_callback, void *p_userdata); void remove_callback(AudioCallback p_callback, void *p_userdata); + void add_update_callback(AudioCallback p_callback, void *p_userdata); + void remove_update_callback(AudioCallback p_callback, void *p_userdata); + void set_bus_layout(const Ref &p_bus_layout); Ref generate_bus_layout() const;