godot/servers/audio/effects/audio_effect_record.cpp

303 lines
9.3 KiB
C++
Raw Normal View History

/*************************************************************************/
/* audio_effect_record.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "audio_effect_record.h"
void AudioEffectRecordInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
if (!is_recording) {
for (int i = 0; i < p_frame_count; i++) {
p_dst_frames[i] = p_src_frames[i];
}
return;
}
//Add incoming audio frames to the IO ring buffer
const AudioFrame *src = p_src_frames;
AudioFrame *rb_buf = ring_buffer.ptrw();
for (int i = 0; i < p_frame_count; i++) {
p_dst_frames[i] = p_src_frames[i];
rb_buf[ring_buffer_pos & ring_buffer_mask] = src[i];
ring_buffer_pos++;
}
}
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();
}
Fix warnings on virtual methods [-Woverloaded-virtual] [-Wdelete-non-virtual-dtor] Fixes the following Clang 7 warnings: ``` editor/editor_help.h:123:7: warning: 'EditorHelpIndex::popup' hides overloaded virtual function [-Woverloaded-virtual] editor/editor_help.h:95:7: warning: 'EditorHelpSearch::popup' hides overloaded virtual function [-Woverloaded-virtual] editor/editor_help.h:96:7: warning: 'EditorHelpSearch::popup' hides overloaded virtual function [-Woverloaded-virtual] editor/plugins/curve_editor_plugin.h:141:15: warning: 'CurvePreviewGenerator::generate' hides overloaded virtual function [-Woverloaded-virtual] editor/plugins/script_editor_plugin.h:70:7: warning: 'ScriptEditorQuickOpen::popup' hides overloaded virtual function [-Woverloaded-virtual] editor/quick_open.h:69:7: warning: 'EditorQuickOpen::popup' hides overloaded virtual function [-Woverloaded-virtual] main/tests/test_io.cpp:53:15: warning: 'TestIO::TestMainLoop::input_event' hides overloaded virtual function [-Woverloaded-virtual] servers/audio/effects/audio_effect_record.h:69:15: warning: 'AudioEffectRecordInstance::process_silence' hides overloaded virtual function [-Woverloaded-virtual] core/os/memory.h:119:2: warning: destructor called on non-final 'ContextGL_X11' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] core/os/memory.h:119:2: warning: destructor called on non-final 'EditorScriptCodeCompletionCache' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] core/os/memory.h:119:2: warning: destructor called on non-final 'Engine' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] core/os/memory.h:119:2: warning: destructor called on non-final 'PhysicalBone::JointData' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] core/os/memory.h:119:2: warning: destructor called on non-final 'VisualServerScene' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] core/os/memory.h:119:2: warning: destructor called on non-final 'VisualServerViewport' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] ```
2018-10-02 10:07:44 +00:00
bool AudioEffectRecordInstance::process_silence() const {
return true;
}
void AudioEffectRecordInstance::_io_thread_process() {
thread_active = true;
while (is_recording) {
//Check: The current recording has been requested to stop
2019-04-08 09:03:37 +00:00
if (!base->recording_active) {
is_recording = false;
}
_update_buffer();
if (is_recording) {
//Wait to avoid too much busy-wait
OS::get_singleton()->delay_usec(500);
}
}
thread_active = false;
}
void AudioEffectRecordInstance::_io_store_buffer() {
int to_read = ring_buffer_pos - ring_buffer_read_pos;
AudioFrame *rb_buf = ring_buffer.ptrw();
while (to_read) {
AudioFrame buffered_frame = rb_buf[ring_buffer_read_pos & ring_buffer_mask];
recording_data.push_back(buffered_frame.l);
recording_data.push_back(buffered_frame.r);
ring_buffer_read_pos++;
to_read--;
}
}
void AudioEffectRecordInstance::_thread_callback(void *_instance) {
AudioEffectRecordInstance *aeri = reinterpret_cast<AudioEffectRecordInstance *>(_instance);
aeri->_io_thread_process();
}
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() {
Ref<AudioEffectRecordInstance> ins;
ins.instance();
ins->base = Ref<AudioEffectRecord>(this);
ins->is_recording = false;
//Re-using the buffer size calculations from audio_effect_delay.cpp
float ring_buffer_max_size = IO_BUFFER_SIZE_MS;
ring_buffer_max_size /= 1000.0; //convert to seconds
ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
int ringbuff_size = ring_buffer_max_size;
int bits = 0;
while (ringbuff_size > 0) {
bits++;
ringbuff_size /= 2;
}
ringbuff_size = 1 << bits;
ins->ring_buffer_mask = ringbuff_size - 1;
ins->ring_buffer_pos = 0;
ins->ring_buffer.resize(ringbuff_size);
ins->ring_buffer_read_pos = 0;
ensure_thread_stopped();
current_instance = ins;
if (recording_active) {
ins->init();
}
return ins;
}
void AudioEffectRecord::ensure_thread_stopped() {
recording_active = false;
if (current_instance != 0) {
current_instance->finish();
}
}
void AudioEffectRecord::set_recording_active(bool p_record) {
if (p_record) {
if (current_instance == 0) {
2019-11-07 08:44:15 +00:00
WARN_PRINT("Recording should not be set as active before Godot has initialized.");
recording_active = false;
return;
}
ensure_thread_stopped();
recording_active = true;
current_instance->init();
} else {
recording_active = false;
}
}
bool AudioEffectRecord::is_recording_active() const {
return recording_active;
}
void AudioEffectRecord::set_format(AudioStreamSample::Format p_format) {
format = p_format;
}
AudioStreamSample::Format AudioEffectRecord::get_format() const {
return format;
}
Ref<AudioStreamSample> AudioEffectRecord::get_recording() const {
AudioStreamSample::Format dst_format = format;
bool stereo = true; //forcing mono is not implemented
Vector<uint8_t> dst_data;
2019-06-22 12:52:51 +00:00
ERR_FAIL_COND_V(current_instance.is_null(), NULL);
2019-08-12 07:10:56 +00:00
ERR_FAIL_COND_V(current_instance->recording_data.size() == 0, NULL);
2019-06-22 12:52:51 +00:00
if (dst_format == AudioStreamSample::FORMAT_8_BITS) {
int data_size = current_instance->recording_data.size();
dst_data.resize(data_size);
uint8_t *w = dst_data.ptrw();
for (int i = 0; i < data_size; i++) {
int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
w[i] = v;
}
} else if (dst_format == AudioStreamSample::FORMAT_16_BITS) {
int data_size = current_instance->recording_data.size();
dst_data.resize(data_size * 2);
uint8_t *w = dst_data.ptrw();
for (int i = 0; i < data_size; i++) {
int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
encode_uint16(v, &w[i * 2]);
}
} else if (dst_format == AudioStreamSample::FORMAT_IMA_ADPCM) {
//byte interleave
Vector<float> left;
Vector<float> right;
int tframes = current_instance->recording_data.size() / 2;
left.resize(tframes);
right.resize(tframes);
for (int i = 0; i < tframes; i++) {
left.set(i, current_instance->recording_data[i * 2 + 0]);
right.set(i, current_instance->recording_data[i * 2 + 1]);
}
Vector<uint8_t> bleft;
Vector<uint8_t> bright;
ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
ResourceImporterWAV::_compress_ima_adpcm(right, bright);
int dl = bleft.size();
dst_data.resize(dl * 2);
uint8_t *w = dst_data.ptrw();
const uint8_t *rl = bleft.ptr();
const uint8_t *rr = bright.ptr();
for (int i = 0; i < dl; i++) {
w[i * 2 + 0] = rl[i];
w[i * 2 + 1] = rr[i];
}
} else {
ERR_PRINT("Format not implemented.");
}
Ref<AudioStreamSample> sample;
sample.instance();
sample->set_data(dst_data);
sample->set_format(dst_format);
sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
sample->set_loop_mode(AudioStreamSample::LOOP_DISABLED);
sample->set_loop_begin(0);
sample->set_loop_end(0);
sample->set_stereo(stereo);
return sample;
}
void AudioEffectRecord::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_recording_active", "record"), &AudioEffectRecord::set_recording_active);
ClassDB::bind_method(D_METHOD("is_recording_active"), &AudioEffectRecord::is_recording_active);
ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioEffectRecord::set_format);
ClassDB::bind_method(D_METHOD("get_format"), &AudioEffectRecord::get_format);
ClassDB::bind_method(D_METHOD("get_recording"), &AudioEffectRecord::get_recording);
ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
}
AudioEffectRecord::AudioEffectRecord() {
format = AudioStreamSample::FORMAT_16_BITS;
2019-11-10 08:49:13 +00:00
recording_active = false;
}