| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/os/thread.h" |
| 34 |
#include "scene/resources/audio_stream_wav.h" |
| 35 |
#include "servers/audio/audio_effect.h" |
| 36 |
#include "servers/audio/audio_server.h" |
| 37 |
|
| 38 |
class AudioEffectRecord; |
| 39 |
|
| 40 |
class AudioEffectRecordInstance : public AudioEffectInstance { |
| 41 |
GDCLASS(AudioEffectRecordInstance, AudioEffectInstance); |
| 42 |
friend class AudioEffectRecord; |
| 43 |
|
| 44 |
bool is_recording; |
| 45 |
Thread io_thread; |
| 46 |
|
| 47 |
Vector<AudioFrame> ring_buffer; |
| 48 |
Vector<float> recording_data; |
| 49 |
|
| 50 |
unsigned int ring_buffer_pos; |
| 51 |
unsigned int ring_buffer_mask; |
| 52 |
unsigned int ring_buffer_read_pos; |
| 53 |
|
| 54 |
void _io_thread_process(); |
| 55 |
void _io_store_buffer(); |
| 56 |
static void _thread_callback(void *_instance); |
| 57 |
void _init_recording(); |
| 58 |
void _update_buffer(); |
| 59 |
static void _update(void *userdata); |
| 60 |
|
| 61 |
public: |
| 62 |
void init(); |
| 63 |
void finish(); |
| 64 |
virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override; |
| 65 |
virtual bool process_silence() const override; |
| 66 |
}; |
| 67 |
|
| 68 |
class AudioEffectRecord : public AudioEffect { |
| 69 |
GDCLASS(AudioEffectRecord, AudioEffect); |
| 70 |
|
| 71 |
friend class AudioEffectRecordInstance; |
| 72 |
|
| 73 |
enum { |
| 74 |
IO_BUFFER_SIZE_MS = 1500 |
| 75 |
}; |
| 76 |
|
| 77 |
Ref<AudioEffectRecordInstance> current_instance; |
| 78 |
|
| 79 |
AudioStreamWAV::Format format; |
| 80 |
|
| 81 |
void ensure_thread_stopped(); |
| 82 |
|
| 83 |
protected: |
| 84 |
static void _bind_methods(); |
| 85 |
|
| 86 |
public: |
| 87 |
Ref<AudioEffectInstance> instantiate() override; |
| 88 |
void set_recording_active(bool p_record); |
| 89 |
bool is_recording_active() const; |
| 90 |
void set_format(AudioStreamWAV::Format p_format); |
| 91 |
AudioStreamWAV::Format get_format() const; |
| 92 |
Ref<AudioStreamWAV> get_recording() const; |
| 93 |
AudioEffectRecord(); |
| 94 |
~AudioEffectRecord(); |
| 95 |
}; |