Path: blob/master/servers/audio/effects/audio_effect_phaser.cpp
10278 views
/**************************************************************************/1/* audio_effect_phaser.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_phaser.h"31#include "servers/audio_server.h"3233void AudioEffectPhaserInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {34float sampling_rate = AudioServer::get_singleton()->get_mix_rate();3536float dmin = base->range_min / (sampling_rate / 2.0);37float dmax = base->range_max / (sampling_rate / 2.0);3839float increment = Math::TAU * (base->rate / sampling_rate);4041for (int i = 0; i < p_frame_count; i++) {42phase += increment;4344while (phase >= Math::TAU) {45phase -= Math::TAU;46}4748float d = dmin + (dmax - dmin) * ((std::sin(phase) + 1.f) / 2.f);4950//update filter coeffs51for (int j = 0; j < 6; j++) {52allpass[0][j].delay(d);53allpass[1][j].delay(d);54}5556//calculate output57float y = allpass[0][0].update(58allpass[0][1].update(59allpass[0][2].update(60allpass[0][3].update(61allpass[0][4].update(62allpass[0][5].update(p_src_frames[i].left + h.left * base->feedback))))));63h.left = y;6465p_dst_frames[i].left = p_src_frames[i].left + y * base->depth;6667y = allpass[1][0].update(68allpass[1][1].update(69allpass[1][2].update(70allpass[1][3].update(71allpass[1][4].update(72allpass[1][5].update(p_src_frames[i].right + h.right * base->feedback))))));73h.right = y;7475p_dst_frames[i].right = p_src_frames[i].right + y * base->depth;76}77}7879Ref<AudioEffectInstance> AudioEffectPhaser::instantiate() {80Ref<AudioEffectPhaserInstance> ins;81ins.instantiate();82ins->base = Ref<AudioEffectPhaser>(this);83ins->phase = 0;84ins->h = AudioFrame(0, 0);8586return ins;87}8889void AudioEffectPhaser::set_range_min_hz(float p_hz) {90range_min = p_hz;91}9293float AudioEffectPhaser::get_range_min_hz() const {94return range_min;95}9697void AudioEffectPhaser::set_range_max_hz(float p_hz) {98range_max = p_hz;99}100101float AudioEffectPhaser::get_range_max_hz() const {102return range_max;103}104105void AudioEffectPhaser::set_rate_hz(float p_hz) {106rate = p_hz;107}108109float AudioEffectPhaser::get_rate_hz() const {110return rate;111}112113void AudioEffectPhaser::set_feedback(float p_fbk) {114feedback = p_fbk;115}116117float AudioEffectPhaser::get_feedback() const {118return feedback;119}120121void AudioEffectPhaser::set_depth(float p_depth) {122depth = p_depth;123}124125float AudioEffectPhaser::get_depth() const {126return depth;127}128129void AudioEffectPhaser::_bind_methods() {130ClassDB::bind_method(D_METHOD("set_range_min_hz", "hz"), &AudioEffectPhaser::set_range_min_hz);131ClassDB::bind_method(D_METHOD("get_range_min_hz"), &AudioEffectPhaser::get_range_min_hz);132133ClassDB::bind_method(D_METHOD("set_range_max_hz", "hz"), &AudioEffectPhaser::set_range_max_hz);134ClassDB::bind_method(D_METHOD("get_range_max_hz"), &AudioEffectPhaser::get_range_max_hz);135136ClassDB::bind_method(D_METHOD("set_rate_hz", "hz"), &AudioEffectPhaser::set_rate_hz);137ClassDB::bind_method(D_METHOD("get_rate_hz"), &AudioEffectPhaser::get_rate_hz);138139ClassDB::bind_method(D_METHOD("set_feedback", "fbk"), &AudioEffectPhaser::set_feedback);140ClassDB::bind_method(D_METHOD("get_feedback"), &AudioEffectPhaser::get_feedback);141142ClassDB::bind_method(D_METHOD("set_depth", "depth"), &AudioEffectPhaser::set_depth);143ClassDB::bind_method(D_METHOD("get_depth"), &AudioEffectPhaser::get_depth);144145ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range_min_hz", PROPERTY_HINT_RANGE, "10,10000,suffix:Hz"), "set_range_min_hz", "get_range_min_hz");146ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range_max_hz", PROPERTY_HINT_RANGE, "10,10000,suffix:Hz"), "set_range_max_hz", "get_range_max_hz");147ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rate_hz", PROPERTY_HINT_RANGE, "0.01,20,suffix:Hz"), "set_rate_hz", "get_rate_hz");148ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "feedback", PROPERTY_HINT_RANGE, "0.1,0.9,0.1"), "set_feedback", "get_feedback");149ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.1,4,0.1"), "set_depth", "get_depth");150}151152AudioEffectPhaser::AudioEffectPhaser() {153range_min = 440;154range_max = 1600;155rate = 0.5;156feedback = 0.7;157depth = 1;158}159160161