2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* audio_effect_record.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-18 15:03:53 +00:00
|
|
|
|
|
|
|
#include "audio_effect_record.h"
|
|
|
|
|
2022-03-28 13:39:24 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
// FIXME: This file shouldn't depend on editor stuff.
|
|
|
|
#include "editor/import/resource_importer_wav.h"
|
|
|
|
#endif
|
|
|
|
|
2018-01-18 15:03:53 +00:00
|
|
|
void AudioEffectRecordInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
|
|
|
|
if (!is_recording) {
|
2019-05-09 15:03:08 +00:00
|
|
|
for (int i = 0; i < p_frame_count; i++) {
|
|
|
|
p_dst_frames[i] = p_src_frames[i];
|
|
|
|
}
|
2018-01-18 15:03:53 +00:00
|
|
|
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++) {
|
2019-05-09 15:03:08 +00:00
|
|
|
p_dst_frames[i] = p_src_frames[i];
|
2018-01-18 15:03:53 +00:00
|
|
|
rb_buf[ring_buffer_pos & ring_buffer_mask] = src[i];
|
|
|
|
ring_buffer_pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:58:29 +00:00
|
|
|
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) {
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioEffectRecordInstance *ins = static_cast<AudioEffectRecordInstance *>(userdata);
|
2018-10-10 13:58:29 +00:00
|
|
|
ins->_update_buffer();
|
|
|
|
}
|
|
|
|
|
2018-10-02 10:07:44 +00:00
|
|
|
bool AudioEffectRecordInstance::process_silence() const {
|
2018-01-18 15:03:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectRecordInstance::_io_thread_process() {
|
|
|
|
while (is_recording) {
|
|
|
|
//Check: The current recording has been requested to stop
|
2019-04-08 09:03:37 +00:00
|
|
|
if (!base->recording_active) {
|
2018-01-18 15:03:53 +00:00
|
|
|
is_recording = false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:58:29 +00:00
|
|
|
_update_buffer();
|
|
|
|
|
|
|
|
if (is_recording) {
|
2018-01-18 15:03:53 +00:00
|
|
|
//Wait to avoid too much busy-wait
|
|
|
|
OS::get_singleton()->delay_usec(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2018-10-10 13:58:29 +00:00
|
|
|
//Reset recorder status
|
|
|
|
ring_buffer_pos = 0;
|
|
|
|
ring_buffer_read_pos = 0;
|
|
|
|
|
|
|
|
//We start a new recording
|
2022-02-01 18:19:13 +00:00
|
|
|
recording_data.clear(); //Clear data completely and reset length
|
2018-10-10 13:58:29 +00:00
|
|
|
is_recording = true;
|
|
|
|
|
2021-01-19 12:29:41 +00:00
|
|
|
io_thread.start(_thread_callback, this);
|
2018-10-10 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectRecordInstance::finish() {
|
2021-01-19 12:29:41 +00:00
|
|
|
io_thread.wait_to_finish();
|
2018-10-10 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioEffectRecordInstance::~AudioEffectRecordInstance() {
|
|
|
|
finish();
|
2018-01-18 15:03:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
Ref<AudioEffectInstance> AudioEffectRecord::instantiate() {
|
2018-01-18 15:03:53 +00:00
|
|
|
Ref<AudioEffectRecordInstance> ins;
|
2021-06-17 22:03:09 +00:00
|
|
|
ins.instantiate();
|
2018-01-18 15:03:53 +00:00
|
|
|
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;
|
2018-07-27 07:41:40 +00:00
|
|
|
if (recording_active) {
|
2018-01-18 15:03:53 +00:00
|
|
|
ins->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectRecord::ensure_thread_stopped() {
|
2018-07-27 07:41:40 +00:00
|
|
|
recording_active = false;
|
2020-04-01 23:20:12 +00:00
|
|
|
if (current_instance != nullptr) {
|
2018-10-10 13:58:29 +00:00
|
|
|
current_instance->finish();
|
2018-01-18 15:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:41:40 +00:00
|
|
|
void AudioEffectRecord::set_recording_active(bool p_record) {
|
2018-01-18 15:03:53 +00:00
|
|
|
if (p_record) {
|
2020-04-01 23:20:12 +00:00
|
|
|
if (current_instance == nullptr) {
|
2019-11-07 08:44:15 +00:00
|
|
|
WARN_PRINT("Recording should not be set as active before Godot has initialized.");
|
2018-07-27 07:41:40 +00:00
|
|
|
recording_active = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-18 15:03:53 +00:00
|
|
|
ensure_thread_stopped();
|
2019-12-12 14:59:47 +00:00
|
|
|
recording_active = true;
|
2018-01-18 15:03:53 +00:00
|
|
|
current_instance->init();
|
2019-12-12 14:59:47 +00:00
|
|
|
} else {
|
|
|
|
recording_active = false;
|
2018-01-18 15:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:41:40 +00:00
|
|
|
bool AudioEffectRecord::is_recording_active() const {
|
|
|
|
return recording_active;
|
2018-01-18 15:03:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 14:34:36 +00:00
|
|
|
void AudioEffectRecord::set_format(AudioStreamWAV::Format p_format) {
|
2018-01-18 15:03:53 +00:00
|
|
|
format = p_format;
|
|
|
|
}
|
|
|
|
|
2022-07-23 14:34:36 +00:00
|
|
|
AudioStreamWAV::Format AudioEffectRecord::get_format() const {
|
2018-01-18 15:03:53 +00:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2022-07-23 14:34:36 +00:00
|
|
|
Ref<AudioStreamWAV> AudioEffectRecord::get_recording() const {
|
|
|
|
AudioStreamWAV::Format dst_format = format;
|
2018-01-18 15:03:53 +00:00
|
|
|
bool stereo = true; //forcing mono is not implemented
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> dst_data;
|
2018-01-18 15:03:53 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V(current_instance.is_null(), nullptr);
|
|
|
|
ERR_FAIL_COND_V(current_instance->recording_data.size() == 0, nullptr);
|
2019-06-22 12:52:51 +00:00
|
|
|
|
2022-07-23 14:34:36 +00:00
|
|
|
if (dst_format == AudioStreamWAV::FORMAT_8_BITS) {
|
2018-01-18 15:03:53 +00:00
|
|
|
int data_size = current_instance->recording_data.size();
|
|
|
|
dst_data.resize(data_size);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = dst_data.ptrw();
|
2018-01-18 15:03:53 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < data_size; i++) {
|
|
|
|
int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
|
|
|
|
w[i] = v;
|
|
|
|
}
|
2022-07-23 14:34:36 +00:00
|
|
|
} else if (dst_format == AudioStreamWAV::FORMAT_16_BITS) {
|
2018-01-18 15:03:53 +00:00
|
|
|
int data_size = current_instance->recording_data.size();
|
|
|
|
dst_data.resize(data_size * 2);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = dst_data.ptrw();
|
2018-01-18 15:03:53 +00:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
2022-07-23 14:34:36 +00:00
|
|
|
} else if (dst_format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
|
2018-01-18 15:03:53 +00:00
|
|
|
//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]);
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> bleft;
|
|
|
|
Vector<uint8_t> bright;
|
2018-01-18 15:03:53 +00:00
|
|
|
|
2022-03-28 13:39:24 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2018-01-18 15:03:53 +00:00
|
|
|
ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
|
|
|
|
ResourceImporterWAV::_compress_ima_adpcm(right, bright);
|
2022-03-28 13:39:24 +00:00
|
|
|
#else
|
|
|
|
ERR_PRINT("AudioEffectRecord cannot do IMA ADPCM compression at runtime.");
|
|
|
|
#endif
|
2018-01-18 15:03:53 +00:00
|
|
|
|
|
|
|
int dl = bleft.size();
|
|
|
|
dst_data.resize(dl * 2);
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = dst_data.ptrw();
|
|
|
|
const uint8_t *rl = bleft.ptr();
|
|
|
|
const uint8_t *rr = bright.ptr();
|
2018-01-18 15:03:53 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < dl; i++) {
|
|
|
|
w[i * 2 + 0] = rl[i];
|
|
|
|
w[i * 2 + 1] = rr[i];
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-20 11:50:20 +00:00
|
|
|
ERR_PRINT("Format not implemented.");
|
2018-01-18 15:03:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 14:34:36 +00:00
|
|
|
Ref<AudioStreamWAV> sample;
|
2021-06-17 22:03:09 +00:00
|
|
|
sample.instantiate();
|
2018-01-18 15:03:53 +00:00
|
|
|
sample->set_data(dst_data);
|
|
|
|
sample->set_format(dst_format);
|
|
|
|
sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
|
2022-07-23 14:34:36 +00:00
|
|
|
sample->set_loop_mode(AudioStreamWAV::LOOP_DISABLED);
|
2018-01-18 15:03:53 +00:00
|
|
|
sample->set_loop_begin(0);
|
|
|
|
sample->set_loop_end(0);
|
|
|
|
sample->set_stereo(stereo);
|
|
|
|
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectRecord::_bind_methods() {
|
2018-07-27 07:41:40 +00:00
|
|
|
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);
|
2018-01-18 15:03:53 +00:00
|
|
|
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() {
|
2022-07-23 14:34:36 +00:00
|
|
|
format = AudioStreamWAV::FORMAT_16_BITS;
|
2019-11-10 08:49:13 +00:00
|
|
|
recording_active = false;
|
2018-01-18 15:03:53 +00:00
|
|
|
}
|