diff --git a/servers/audio/effects/audio_effect_phaser.cpp b/servers/audio/effects/audio_effect_phaser.cpp new file mode 100644 index 00000000000..b687e9f7087 --- /dev/null +++ b/servers/audio/effects/audio_effect_phaser.cpp @@ -0,0 +1,147 @@ +#include "audio_effect_phaser.h" +#include "servers/audio_server.h" + +void AudioEffectPhaserInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + float sampling_rate = AudioServer::get_singleton()->get_mix_rate(); + + float dmin = base->range_min / (sampling_rate/2.0); + float dmax = base->range_max / (sampling_rate/2.0); + + float increment = 2.f * M_PI * (base->rate / sampling_rate); + + for(int i=0;i= M_PI * 2.f ) { + phase -= M_PI * 2.f; + } + + float d = dmin + (dmax-dmin) * ((sin( phase ) + 1.f)/2.f); + + + //update filter coeffs + for( int j=0; j<6; j++ ) { + allpass[0][j].delay( d ); + allpass[1][j].delay( d ); + } + + //calculate output + float y = allpass[0][0].update( + allpass[0][1].update( + allpass[0][2].update( + allpass[0][3].update( + allpass[0][4].update( + allpass[0][5].update( p_src_frames[i].l + h.l * base->feedback )))))); + h.l=y; + + p_dst_frames[i].l = p_src_frames[i].l + y * base->depth; + + y = allpass[1][0].update( + allpass[1][1].update( + allpass[1][2].update( + allpass[1][3].update( + allpass[1][4].update( + allpass[1][5].update( p_src_frames[i].r + h.r * base->feedback )))))); + h.r=y; + + p_dst_frames[i].r = p_src_frames[i].r + y * base->depth; + + + } + +} + + +Ref AudioEffectPhaser::instance() { + Ref ins; + ins.instance(); + ins->base=Ref(this); + ins->phase=0; + ins->h=AudioFrame(0,0); + + return ins; +} + + +void AudioEffectPhaser::set_range_min_hz(float p_hz) { + + range_min=p_hz; +} + +float AudioEffectPhaser::get_range_min_hz() const{ + + return range_min; +} + +void AudioEffectPhaser::set_range_max_hz(float p_hz){ + + range_max=p_hz; +} +float AudioEffectPhaser::get_range_max_hz() const{ + + return range_max; +} + +void AudioEffectPhaser::set_rate_hz(float p_hz){ + + rate=p_hz; +} +float AudioEffectPhaser::get_rate_hz() const{ + + return rate; +} + +void AudioEffectPhaser::set_feedback(float p_fbk){ + + feedback=p_fbk; +} +float AudioEffectPhaser::get_feedback() const{ + + return feedback; +} + +void AudioEffectPhaser::set_depth(float p_depth) { + + depth=p_depth; +} + +float AudioEffectPhaser::get_depth() const { + + return depth; +} + +void AudioEffectPhaser::_bind_methods() { + + ClassDB::bind_method(_MD("set_range_min_hz","hz"),&AudioEffectPhaser::set_range_min_hz); + ClassDB::bind_method(_MD("get_range_min_hz"),&AudioEffectPhaser::get_range_min_hz); + + ClassDB::bind_method(_MD("set_range_max_hz","hz"),&AudioEffectPhaser::set_range_max_hz); + ClassDB::bind_method(_MD("get_range_max_hz"),&AudioEffectPhaser::get_range_max_hz); + + ClassDB::bind_method(_MD("set_rate_hz","hz"),&AudioEffectPhaser::set_rate_hz); + ClassDB::bind_method(_MD("get_rate_hz"),&AudioEffectPhaser::get_rate_hz); + + ClassDB::bind_method(_MD("set_feedback","fbk"),&AudioEffectPhaser::set_feedback); + ClassDB::bind_method(_MD("get_feedback"),&AudioEffectPhaser::get_feedback); + + ClassDB::bind_method(_MD("set_depth","depth"),&AudioEffectPhaser::set_depth); + ClassDB::bind_method(_MD("get_depth"),&AudioEffectPhaser::get_depth); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"range_min_hz",PROPERTY_HINT_RANGE,"10,10000"),_SCS("set_range_min_hz"),_SCS("get_range_min_hz")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"range_max_hz",PROPERTY_HINT_RANGE,"10,10000"),_SCS("set_range_max_hz"),_SCS("get_range_max_hz")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"rate_hz",PROPERTY_HINT_RANGE,"0.01,20"),_SCS("set_rate_hz"),_SCS("get_rate_hz")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"feedback",PROPERTY_HINT_RANGE,"0.1,0.9,0.1"),_SCS("set_feedback"),_SCS("get_feedback")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"depth",PROPERTY_HINT_RANGE,"0.1,4,0.1"),_SCS("set_depth"),_SCS("get_depth")); + +} + +AudioEffectPhaser::AudioEffectPhaser() +{ + range_min=440; + range_max=1600; + rate=0.5; + feedback=0.7; + depth=1; +} diff --git a/servers/audio/effects/audio_effect_phaser.h b/servers/audio/effects/audio_effect_phaser.h new file mode 100644 index 00000000000..53a8ab8bd8a --- /dev/null +++ b/servers/audio/effects/audio_effect_phaser.h @@ -0,0 +1,81 @@ +#ifndef AUDIO_EFFECT_PHASER_H +#define AUDIO_EFFECT_PHASER_H + + + +#include "servers/audio/audio_effect.h" + +class AudioEffectPhaser; + +class AudioEffectPhaserInstance : public AudioEffectInstance { + GDCLASS(AudioEffectPhaserInstance,AudioEffectInstance) +friend class AudioEffectPhaser; + Ref base; + + float phase; + AudioFrame h; + + class AllpassDelay{ + float a, h; + public: + + _ALWAYS_INLINE_ void delay( float d ) { + a = (1.f - d) / (1.f + d); + } + + _ALWAYS_INLINE_ float update( float s ){ + float y = s * -a + h; + h = y * a + s; + return y; + } + + AllpassDelay() { a =0; h = 0;} + + }; + + AllpassDelay allpass[2][6]; +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectPhaser : public AudioEffect { + GDCLASS(AudioEffectPhaser,AudioEffect) + +friend class AudioEffectPhaserInstance; + float range_min; + float range_max; + float rate; + float feedback; + float depth; + +protected: + + static void _bind_methods(); +public: + + + Ref instance(); + + void set_range_min_hz(float p_hz); + float get_range_min_hz() const; + + void set_range_max_hz(float p_hz); + float get_range_max_hz() const; + + void set_rate_hz(float p_hz); + float get_rate_hz() const; + + void set_feedback(float p_fbk); + float get_feedback() const; + + void set_depth(float p_depth); + float get_depth() const; + + AudioEffectPhaser(); +}; + + +#endif // AUDIO_EFFECT_PHASER_H diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp index 251db182df6..56bb0d32b21 100644 --- a/servers/register_server_types.cpp +++ b/servers/register_server_types.cpp @@ -49,6 +49,7 @@ #include "audio/effects/audio_effect_compressor.h" #include "audio/effects/audio_effect_limiter.h" #include "audio/effects/audio_effect_pitch_shift.h" +#include "audio/effects/audio_effect_phaser.h" static void _debugger_get_resource_usage(List* r_usage) { @@ -113,6 +114,7 @@ void register_server_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); }