diff options
author | Marcelo Fernandez <marcelofg55@gmail.com> | 2018-10-10 10:58:29 -0300 |
---|---|---|
committer | Marcelo Fernandez <marcelofg55@gmail.com> | 2018-12-12 21:44:11 -0300 |
commit | 7da55fb739e4f2f2ad50611e78f09caafc7ede76 (patch) | |
tree | fc5fb36f0d5d5fd83f1805ca3023c99f60024571 | |
parent | 4c1a5d9cfe2da761bfe17d52126deabc1f1c1bd3 (diff) |
Fix AudioEffectRecord not working without thread support
-rw-r--r-- | servers/audio/effects/audio_effect_record.cpp | 63 | ||||
-rw-r--r-- | servers/audio/effects/audio_effect_record.h | 4 | ||||
-rw-r--r-- | servers/audio_server.cpp | 24 | ||||
-rw-r--r-- | servers/audio_server.h | 4 |
4 files changed, 78 insertions, 17 deletions
diff --git a/servers/audio/effects/audio_effect_record.cpp b/servers/audio/effects/audio_effect_record.cpp index a0094f66b8..5a583243ca 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<AudioEffectInstance> AudioEffectRecord::instance() { @@ -145,8 +174,8 @@ Ref<AudioEffectInstance> 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 4b8ee2bcdd..c5e4866f17 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 d36057b465..f8e2538792 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<CallbackItem>::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<AudioBusLayout> &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 ba6569eb38..4f981eac2d 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -262,6 +262,7 @@ private: }; Set<CallbackItem> callbacks; + Set<CallbackItem> 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<AudioBusLayout> &p_bus_layout); Ref<AudioBusLayout> generate_bus_layout() const; |