Path: blob/master/servers/audio/effects/audio_effect_stereo_enhance.cpp
10278 views
/**************************************************************************/1/* audio_effect_stereo_enhance.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_stereo_enhance.h"3132#include "servers/audio_server.h"3334void AudioEffectStereoEnhanceInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {35float intensity = base->pan_pullout;36bool surround_mode = base->surround > 0;37float surround_amount = base->surround;38unsigned int delay_frames = (base->time_pullout / 1000.0) * AudioServer::get_singleton()->get_mix_rate();3940for (int i = 0; i < p_frame_count; i++) {41float left = p_src_frames[i].left;42float right = p_src_frames[i].right;4344float center = (left + right) / 2.0f;4546left = (center + (left - center) * intensity);47right = (center + (right - center) * intensity);4849if (surround_mode) {50float val = (left + right) / 2.0;5152delay_ringbuff[ringbuff_pos & ringbuff_mask] = val;5354float out = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask] * surround_amount;5556left += out;57right += -out;58} else {59float val = right;6061delay_ringbuff[ringbuff_pos & ringbuff_mask] = val;6263// The right channel is delayed.64right = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask];65}6667p_dst_frames[i].left = left;68p_dst_frames[i].right = right;69ringbuff_pos++;70}71}7273AudioEffectStereoEnhanceInstance::~AudioEffectStereoEnhanceInstance() {74memdelete_arr(delay_ringbuff);75}7677Ref<AudioEffectInstance> AudioEffectStereoEnhance::instantiate() {78Ref<AudioEffectStereoEnhanceInstance> ins;79ins.instantiate();8081ins->base = Ref<AudioEffectStereoEnhance>(this);8283float ring_buffer_max_size = AudioEffectStereoEnhanceInstance::MAX_DELAY_MS + 2;84ring_buffer_max_size /= 1000.0; //convert to seconds85ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();8687int ringbuff_size = (int)ring_buffer_max_size;8889int bits = 0;9091while (ringbuff_size > 0) {92bits++;93ringbuff_size /= 2;94}9596ringbuff_size = 1 << bits;97ins->ringbuff_mask = ringbuff_size - 1;98ins->ringbuff_pos = 0;99100ins->delay_ringbuff = memnew_arr(float, ringbuff_size);101102return ins;103}104105void AudioEffectStereoEnhance::set_pan_pullout(float p_amount) {106pan_pullout = p_amount;107}108109float AudioEffectStereoEnhance::get_pan_pullout() const {110return pan_pullout;111}112113void AudioEffectStereoEnhance::set_time_pullout(float p_amount) {114time_pullout = p_amount;115}116117float AudioEffectStereoEnhance::get_time_pullout() const {118return time_pullout;119}120121void AudioEffectStereoEnhance::set_surround(float p_amount) {122surround = p_amount;123}124125float AudioEffectStereoEnhance::get_surround() const {126return surround;127}128129void AudioEffectStereoEnhance::_bind_methods() {130ClassDB::bind_method(D_METHOD("set_pan_pullout", "amount"), &AudioEffectStereoEnhance::set_pan_pullout);131ClassDB::bind_method(D_METHOD("get_pan_pullout"), &AudioEffectStereoEnhance::get_pan_pullout);132133ClassDB::bind_method(D_METHOD("set_time_pullout", "amount"), &AudioEffectStereoEnhance::set_time_pullout);134ClassDB::bind_method(D_METHOD("get_time_pullout"), &AudioEffectStereoEnhance::get_time_pullout);135136ClassDB::bind_method(D_METHOD("set_surround", "amount"), &AudioEffectStereoEnhance::set_surround);137ClassDB::bind_method(D_METHOD("get_surround"), &AudioEffectStereoEnhance::get_surround);138139ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pan_pullout", PROPERTY_HINT_RANGE, "0,4,0.01"), "set_pan_pullout", "get_pan_pullout");140ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_pullout_ms", PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms"), "set_time_pullout", "get_time_pullout");141ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "surround", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_surround", "get_surround");142}143144AudioEffectStereoEnhance::AudioEffectStereoEnhance() {}145146147