Path: blob/master/servers/audio/effects/audio_effect_capture.cpp
10278 views
/**************************************************************************/1/* audio_effect_capture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_effect_capture.h"3132#include "servers/audio_server.h"3334bool AudioEffectCapture::can_get_buffer(int p_frames) const {35return buffer.data_left() >= p_frames;36}3738PackedVector2Array AudioEffectCapture::get_buffer(int p_frames) {39ERR_FAIL_COND_V(!buffer_initialized, PackedVector2Array());40ERR_FAIL_INDEX_V(p_frames, buffer.size(), PackedVector2Array());41int data_left = buffer.data_left();42if (data_left < p_frames || p_frames == 0) {43return PackedVector2Array();44}4546PackedVector2Array ret;47ret.resize(p_frames);4849Vector<AudioFrame> streaming_data;50streaming_data.resize(p_frames);51buffer.read(streaming_data.ptrw(), p_frames);52for (int32_t i = 0; i < p_frames; i++) {53ret.write[i] = Vector2(streaming_data[i].left, streaming_data[i].right);54}55return ret;56}5758void AudioEffectCapture::clear_buffer() {59const int32_t data_left = buffer.data_left();60buffer.advance_read(data_left);61}6263void AudioEffectCapture::_bind_methods() {64ClassDB::bind_method(D_METHOD("can_get_buffer", "frames"), &AudioEffectCapture::can_get_buffer);65ClassDB::bind_method(D_METHOD("get_buffer", "frames"), &AudioEffectCapture::get_buffer);66ClassDB::bind_method(D_METHOD("clear_buffer"), &AudioEffectCapture::clear_buffer);67ClassDB::bind_method(D_METHOD("set_buffer_length", "buffer_length_seconds"), &AudioEffectCapture::set_buffer_length);68ClassDB::bind_method(D_METHOD("get_buffer_length"), &AudioEffectCapture::get_buffer_length);69ClassDB::bind_method(D_METHOD("get_frames_available"), &AudioEffectCapture::get_frames_available);70ClassDB::bind_method(D_METHOD("get_discarded_frames"), &AudioEffectCapture::get_discarded_frames);71ClassDB::bind_method(D_METHOD("get_buffer_length_frames"), &AudioEffectCapture::get_buffer_length_frames);72ClassDB::bind_method(D_METHOD("get_pushed_frames"), &AudioEffectCapture::get_pushed_frames);7374ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "buffer_length", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), "set_buffer_length", "get_buffer_length");75}7677Ref<AudioEffectInstance> AudioEffectCapture::instantiate() {78if (!buffer_initialized) {79float target_buffer_size = AudioServer::get_singleton()->get_mix_rate() * buffer_length_seconds;80ERR_FAIL_COND_V(target_buffer_size <= 0 || target_buffer_size >= (1 << 27), Ref<AudioEffectInstance>());81buffer.resize(nearest_shift((uint32_t)target_buffer_size));82buffer_initialized = true;83}8485clear_buffer();8687Ref<AudioEffectCaptureInstance> ins;88ins.instantiate();89ins->base = Ref<AudioEffectCapture>(this);9091return ins;92}9394void AudioEffectCapture::set_buffer_length(float p_buffer_length_seconds) {95buffer_length_seconds = p_buffer_length_seconds;96}9798float AudioEffectCapture::get_buffer_length() {99return buffer_length_seconds;100}101102int AudioEffectCapture::get_frames_available() const {103ERR_FAIL_COND_V(!buffer_initialized, 0);104return buffer.data_left();105}106107int64_t AudioEffectCapture::get_discarded_frames() const {108return discarded_frames.get();109}110111int AudioEffectCapture::get_buffer_length_frames() const {112ERR_FAIL_COND_V(!buffer_initialized, 0);113return buffer.size();114}115116int64_t AudioEffectCapture::get_pushed_frames() const {117return pushed_frames.get();118}119120void AudioEffectCaptureInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {121RingBuffer<AudioFrame> &buffer = base->buffer;122123for (int i = 0; i < p_frame_count; i++) {124p_dst_frames[i] = p_src_frames[i];125}126127if (buffer.space_left() >= p_frame_count) {128// Add incoming audio frames to the IO ring buffer129int32_t ret = buffer.write(p_src_frames, p_frame_count);130ERR_FAIL_COND_MSG(ret != p_frame_count, "Failed to add data to effect capture ring buffer despite sufficient space.");131base->pushed_frames.add(p_frame_count);132} else {133base->discarded_frames.add(p_frame_count);134}135}136137bool AudioEffectCaptureInstance::process_silence() const {138return true;139}140141142