mirror of
https://github.com/Redot-Engine/redot-engine.git
synced 2025-12-06 23:31:53 -05:00
Fix AudioEffectRecord circular reference
This commit is contained in:
@@ -72,13 +72,7 @@ bool AudioEffectRecordInstance::process_silence() const {
|
|||||||
|
|
||||||
void AudioEffectRecordInstance::_io_thread_process() {
|
void AudioEffectRecordInstance::_io_thread_process() {
|
||||||
while (is_recording) {
|
while (is_recording) {
|
||||||
//Check: The current recording has been requested to stop
|
|
||||||
if (!base->recording_active) {
|
|
||||||
is_recording = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_update_buffer();
|
_update_buffer();
|
||||||
|
|
||||||
if (is_recording) {
|
if (is_recording) {
|
||||||
//Wait to avoid too much busy-wait
|
//Wait to avoid too much busy-wait
|
||||||
OS::get_singleton()->delay_usec(500);
|
OS::get_singleton()->delay_usec(500);
|
||||||
@@ -120,19 +114,15 @@ void AudioEffectRecordInstance::init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AudioEffectRecordInstance::finish() {
|
void AudioEffectRecordInstance::finish() {
|
||||||
|
is_recording = false;
|
||||||
if (io_thread.is_started()) {
|
if (io_thread.is_started()) {
|
||||||
io_thread.wait_to_finish();
|
io_thread.wait_to_finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioEffectRecordInstance::~AudioEffectRecordInstance() {
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ref<AudioEffectInstance> AudioEffectRecord::instantiate() {
|
Ref<AudioEffectInstance> AudioEffectRecord::instantiate() {
|
||||||
Ref<AudioEffectRecordInstance> ins;
|
Ref<AudioEffectRecordInstance> ins;
|
||||||
ins.instantiate();
|
ins.instantiate();
|
||||||
ins->base = Ref<AudioEffectRecord>(this);
|
|
||||||
ins->is_recording = false;
|
ins->is_recording = false;
|
||||||
|
|
||||||
//Re-using the buffer size calculations from audio_effect_delay.cpp
|
//Re-using the buffer size calculations from audio_effect_delay.cpp
|
||||||
@@ -158,16 +148,19 @@ Ref<AudioEffectInstance> AudioEffectRecord::instantiate() {
|
|||||||
ins->ring_buffer_read_pos = 0;
|
ins->ring_buffer_read_pos = 0;
|
||||||
|
|
||||||
ensure_thread_stopped();
|
ensure_thread_stopped();
|
||||||
current_instance = ins;
|
bool is_currently_recording = false;
|
||||||
if (recording_active) {
|
if (current_instance != nullptr) {
|
||||||
|
is_currently_recording = current_instance->is_recording;
|
||||||
|
}
|
||||||
|
if (is_currently_recording) {
|
||||||
ins->init();
|
ins->init();
|
||||||
}
|
}
|
||||||
|
current_instance = ins;
|
||||||
|
|
||||||
return ins;
|
return ins;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioEffectRecord::ensure_thread_stopped() {
|
void AudioEffectRecord::ensure_thread_stopped() {
|
||||||
recording_active = false;
|
|
||||||
if (current_instance != nullptr) {
|
if (current_instance != nullptr) {
|
||||||
current_instance->finish();
|
current_instance->finish();
|
||||||
}
|
}
|
||||||
@@ -177,20 +170,23 @@ void AudioEffectRecord::set_recording_active(bool p_record) {
|
|||||||
if (p_record) {
|
if (p_record) {
|
||||||
if (current_instance == nullptr) {
|
if (current_instance == nullptr) {
|
||||||
WARN_PRINT("Recording should not be set as active before Godot has initialized.");
|
WARN_PRINT("Recording should not be set as active before Godot has initialized.");
|
||||||
recording_active = false;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure_thread_stopped();
|
ensure_thread_stopped();
|
||||||
recording_active = true;
|
|
||||||
current_instance->init();
|
current_instance->init();
|
||||||
} else {
|
} else {
|
||||||
recording_active = false;
|
if (current_instance != nullptr) {
|
||||||
|
current_instance->is_recording = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AudioEffectRecord::is_recording_active() const {
|
bool AudioEffectRecord::is_recording_active() const {
|
||||||
return recording_active;
|
if (current_instance == nullptr) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return current_instance->is_recording;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioEffectRecord::set_format(AudioStreamWAV::Format p_format) {
|
void AudioEffectRecord::set_format(AudioStreamWAV::Format p_format) {
|
||||||
@@ -292,5 +288,8 @@ void AudioEffectRecord::_bind_methods() {
|
|||||||
|
|
||||||
AudioEffectRecord::AudioEffectRecord() {
|
AudioEffectRecord::AudioEffectRecord() {
|
||||||
format = AudioStreamWAV::FORMAT_16_BITS;
|
format = AudioStreamWAV::FORMAT_16_BITS;
|
||||||
recording_active = false;
|
}
|
||||||
|
|
||||||
|
AudioEffectRecord::~AudioEffectRecord() {
|
||||||
|
ensure_thread_stopped();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ class AudioEffectRecord;
|
|||||||
class AudioEffectRecordInstance : public AudioEffectInstance {
|
class AudioEffectRecordInstance : public AudioEffectInstance {
|
||||||
GDCLASS(AudioEffectRecordInstance, AudioEffectInstance);
|
GDCLASS(AudioEffectRecordInstance, AudioEffectInstance);
|
||||||
friend class AudioEffectRecord;
|
friend class AudioEffectRecord;
|
||||||
Ref<AudioEffectRecord> base;
|
|
||||||
|
|
||||||
bool is_recording;
|
bool is_recording;
|
||||||
Thread io_thread;
|
Thread io_thread;
|
||||||
@@ -68,9 +67,6 @@ public:
|
|||||||
void finish();
|
void finish();
|
||||||
virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
|
virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
|
||||||
virtual bool process_silence() const override;
|
virtual bool process_silence() const override;
|
||||||
|
|
||||||
AudioEffectRecordInstance() {}
|
|
||||||
~AudioEffectRecordInstance();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class AudioEffectRecord : public AudioEffect {
|
class AudioEffectRecord : public AudioEffect {
|
||||||
@@ -82,7 +78,6 @@ class AudioEffectRecord : public AudioEffect {
|
|||||||
IO_BUFFER_SIZE_MS = 1500
|
IO_BUFFER_SIZE_MS = 1500
|
||||||
};
|
};
|
||||||
|
|
||||||
bool recording_active;
|
|
||||||
Ref<AudioEffectRecordInstance> current_instance;
|
Ref<AudioEffectRecordInstance> current_instance;
|
||||||
|
|
||||||
AudioStreamWAV::Format format;
|
AudioStreamWAV::Format format;
|
||||||
@@ -99,8 +94,8 @@ public:
|
|||||||
void set_format(AudioStreamWAV::Format p_format);
|
void set_format(AudioStreamWAV::Format p_format);
|
||||||
AudioStreamWAV::Format get_format() const;
|
AudioStreamWAV::Format get_format() const;
|
||||||
Ref<AudioStreamWAV> get_recording() const;
|
Ref<AudioStreamWAV> get_recording() const;
|
||||||
|
|
||||||
AudioEffectRecord();
|
AudioEffectRecord();
|
||||||
|
~AudioEffectRecord();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // AUDIO_EFFECT_RECORD_H
|
#endif // AUDIO_EFFECT_RECORD_H
|
||||||
|
|||||||
Reference in New Issue
Block a user