diff options
Diffstat (limited to 'servers')
41 files changed, 5531 insertions, 52 deletions
diff --git a/servers/audio/SCsub b/servers/audio/SCsub index ccc76e823f..afaffcfe93 100644 --- a/servers/audio/SCsub +++ b/servers/audio/SCsub @@ -5,3 +5,5 @@ Import('env') env.add_source_files(env.servers_sources, "*.cpp") Export('env') + +SConscript("effects/SCsub") diff --git a/servers/audio/audio_effect.h b/servers/audio/audio_effect.h index 2fcd22251b..02eb258f99 100644 --- a/servers/audio/audio_effect.h +++ b/servers/audio/audio_effect.h @@ -10,7 +10,7 @@ class AudioEffectInstance : public Reference { public: - virtual void process(AudioFrame *p_frames,int p_frame_count)=0; + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count)=0; }; diff --git a/servers/audio/audio_filter_sw.cpp b/servers/audio/audio_filter_sw.cpp index cdfe1a29f0..e97eb75d04 100644 --- a/servers/audio/audio_filter_sw.cpp +++ b/servers/audio/audio_filter_sw.cpp @@ -142,9 +142,9 @@ void AudioFilterSW::prepare_coefficients(Coeffs *p_coeffs) { //this one is extra tricky double hicutoff=resonance; double centercutoff = (cutoff+resonance)/2.0; - double bandwidth=(Math::log(centercutoff)-Math::log(hicutoff))/Math::log(2); + double bandwidth=(Math::log(centercutoff)-Math::log(hicutoff))/Math::log((double)2); omega=2.0*Math_PI*centercutoff/sampling_rate; - alpha = Math::sin(omega)*Math::sinh( Math::log(2)/2 * bandwidth * omega/Math::sin(omega) ); + alpha = Math::sin(omega)*Math::sinh( Math::log((double)2)/2 * bandwidth * omega/Math::sin(omega) ); a0=1+alpha; p_coeffs->b0 = alpha; diff --git a/servers/audio/audio_filter_sw.h b/servers/audio/audio_filter_sw.h index 0f3e2410fd..b711944ca8 100644 --- a/servers/audio/audio_filter_sw.h +++ b/servers/audio/audio_filter_sw.h @@ -65,7 +65,7 @@ public: void set_filter(AudioFilterSW * p_filter); void process(float *p_samples,int p_amount, int p_stride=1); void update_coeffs(); - inline void process_one(float& p_sample); + _ALWAYS_INLINE_ void process_one(float& p_sample); Processor(); }; diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp new file mode 100644 index 0000000000..f4214838a1 --- /dev/null +++ b/servers/audio/audio_stream.cpp @@ -0,0 +1,86 @@ +/*************************************************************************/ +/* audio_stream.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 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. */ +/*************************************************************************/ +#include "audio_stream.h" + +////////////////////////////// + + + +void AudioStreamPlaybackResampled::_begin_resample() { + + //clear cubic interpolation history + internal_buffer[0]=AudioFrame(0.0,0.0); + internal_buffer[1]=AudioFrame(0.0,0.0); + internal_buffer[2]=AudioFrame(0.0,0.0); + internal_buffer[3]=AudioFrame(0.0,0.0); + //mix buffer + _mix_internal(internal_buffer+4,INTERNAL_BUFFER_LEN); + mix_offset=0; +} + +void AudioStreamPlaybackResampled::mix(AudioFrame* p_buffer,float p_rate_scale,int p_frames) { + + float target_rate = AudioServer::get_singleton()->get_mix_rate() * p_rate_scale; + + uint64_t mix_increment = uint64_t((get_stream_sampling_rate() / double(target_rate)) * double( FP_LEN )); + + for(int i=0;i<p_frames;i++) { + + + + uint32_t idx = CUBIC_INTERP_HISTORY + uint32_t(mix_offset >> FP_BITS); + //standard cubic interpolation (great quality/performance ratio) + //this used to be moved to a LUT for greater performance, but nowadays CPU speed is generally faster than memory. + float mu = (mix_offset&FP_MASK)/float(FP_LEN); + AudioFrame y0 = internal_buffer[idx-3]; + AudioFrame y1 = internal_buffer[idx-2]; + AudioFrame y2 = internal_buffer[idx-1]; + AudioFrame y3 = internal_buffer[idx-0]; + + float mu2 = mu*mu; + AudioFrame a0 = y3 - y2 - y0 + y1; + AudioFrame a1 = y0 - y1 - a0; + AudioFrame a2 = y2 - y0; + AudioFrame a3 = y1; + + p_buffer[i] = (a0*mu*mu2 + a1*mu2 + a2*mu + a3); + + mix_offset+=mix_increment; + + while ( (mix_offset >> FP_BITS) >= INTERNAL_BUFFER_LEN ) { + + internal_buffer[0]=internal_buffer[INTERNAL_BUFFER_LEN+0]; + internal_buffer[1]=internal_buffer[INTERNAL_BUFFER_LEN+1]; + internal_buffer[2]=internal_buffer[INTERNAL_BUFFER_LEN+2]; + internal_buffer[3]=internal_buffer[INTERNAL_BUFFER_LEN+3]; + _mix_internal(internal_buffer+4,INTERNAL_BUFFER_LEN); + mix_offset-=(INTERNAL_BUFFER_LEN<<FP_BITS); + } + } +} diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h new file mode 100644 index 0000000000..d08fedb084 --- /dev/null +++ b/servers/audio/audio_stream.h @@ -0,0 +1,101 @@ +/*************************************************************************/ +/* audio_stream.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 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. */ +/*************************************************************************/ +#ifndef AUDIO_STREAM_H +#define AUDIO_STREAM_H + +#include "resource.h" +#include "servers/audio_server.h" + +class AudioStreamPlayback : public Reference { + + GDCLASS( AudioStreamPlayback, Reference ) + +public: + + virtual void start(float p_from_pos=0.0)=0; + virtual void stop()=0; + virtual bool is_playing() const=0; + + virtual int get_loop_count() const=0; //times it looped + + virtual float get_pos() const=0; + virtual void seek_pos(float p_time)=0; + + virtual void mix(AudioFrame* p_bufer,float p_rate_scale,int p_frames)=0; + + virtual float get_length() const=0; //if supported, otherwise return 0 + + +}; + +class AudioStreamPlaybackResampled : public AudioStreamPlayback { + + GDCLASS( AudioStreamPlaybackResampled, AudioStreamPlayback ) + + + + enum { + FP_BITS=16, //fixed point used for resampling + FP_LEN=(1<<FP_BITS), + FP_MASK=FP_LEN-1, + INTERNAL_BUFFER_LEN=256, + CUBIC_INTERP_HISTORY=4 + }; + + AudioFrame internal_buffer[INTERNAL_BUFFER_LEN+CUBIC_INTERP_HISTORY]; + uint64_t mix_offset; + +protected: + void _begin_resample(); + virtual void _mix_internal(AudioFrame* p_bufer,int p_frames)=0; + virtual float get_stream_sampling_rate()=0; + +public: + + virtual void mix(AudioFrame* p_bufer,float p_rate_scale,int p_frames); + + AudioStreamPlaybackResampled() { mix_offset=0; } +}; + +class AudioStream : public Resource { + + GDCLASS( AudioStream, Resource ) + OBJ_SAVE_TYPE( AudioStream ) //children are all saved as AudioStream, so they can be exchanged + + +public: + + virtual Ref<AudioStreamPlayback> instance_playback()=0; + virtual String get_stream_name() const=0; + + +}; + + +#endif // AUDIO_STREAM_H diff --git a/servers/audio/effects/SCsub b/servers/audio/effects/SCsub new file mode 100644 index 0000000000..ccc76e823f --- /dev/null +++ b/servers/audio/effects/SCsub @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +Import('env') + +env.add_source_files(env.servers_sources, "*.cpp") + +Export('env') diff --git a/servers/audio/effects/audio_effect_amplify.cpp b/servers/audio/effects/audio_effect_amplify.cpp new file mode 100644 index 0000000000..d723f8d2fe --- /dev/null +++ b/servers/audio/effects/audio_effect_amplify.cpp @@ -0,0 +1,50 @@ +#include "audio_effect_amplify.h" + + +void AudioEffectAmplifyInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + + //multiply volume interpolating to avoid clicks if this changes + float volume_db = base->volume_db; + float vol = Math::db2linear(mix_volume_db); + float vol_inc = (Math::db2linear(volume_db) - vol)/float(p_frame_count); + + for(int i=0;i<p_frame_count;i++) { + p_dst_frames[i]=p_src_frames[i]*vol; + vol+=vol_inc; + } + //set volume for next mix + mix_volume_db = volume_db; + +} + + +Ref<AudioEffectInstance> AudioEffectAmplify::instance() { + Ref<AudioEffectAmplifyInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectAmplify>(this); + ins->mix_volume_db=volume_db; + return ins; +} + +void AudioEffectAmplify::set_volume_db(float p_volume) { + volume_db=p_volume; +} + +float AudioEffectAmplify::get_volume_db() const { + + return volume_db; +} + +void AudioEffectAmplify::_bind_methods() { + + ClassDB::bind_method(_MD("set_volume_db","volume"),&AudioEffectAmplify::set_volume_db); + ClassDB::bind_method(_MD("get_volume_db"),&AudioEffectAmplify::get_volume_db); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"volume_db",PROPERTY_HINT_RANGE,"-80,24,0.01"),_SCS("set_volume_db"),_SCS("get_volume_db")); +} + +AudioEffectAmplify::AudioEffectAmplify() +{ + volume_db=0; +} diff --git a/servers/audio/effects/audio_effect_amplify.h b/servers/audio/effects/audio_effect_amplify.h new file mode 100644 index 0000000000..921054e2cd --- /dev/null +++ b/servers/audio/effects/audio_effect_amplify.h @@ -0,0 +1,40 @@ +#ifndef AUDIOEFFECTAMPLIFY_H +#define AUDIOEFFECTAMPLIFY_H + +#include "servers/audio/audio_effect.h" + +class AudioEffectAmplify; + +class AudioEffectAmplifyInstance : public AudioEffectInstance { + GDCLASS(AudioEffectAmplifyInstance,AudioEffectInstance) +friend class AudioEffectAmplify; + Ref<AudioEffectAmplify> base; + + float mix_volume_db; +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectAmplify : public AudioEffect { + GDCLASS(AudioEffectAmplify,AudioEffect) + +friend class AudioEffectAmplifyInstance; + float volume_db; + +protected: + + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + void set_volume_db(float p_volume); + float get_volume_db() const; + + AudioEffectAmplify(); +}; + +#endif // AUDIOEFFECTAMPLIFY_H diff --git a/servers/audio/effects/audio_effect_chorus.cpp b/servers/audio/effects/audio_effect_chorus.cpp new file mode 100644 index 0000000000..d3105343ae --- /dev/null +++ b/servers/audio/effects/audio_effect_chorus.cpp @@ -0,0 +1,365 @@ +#include "audio_effect_chorus.h" +#include "servers/audio_server.h" +#include "math_funcs.h" + +void AudioEffectChorusInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + int todo = p_frame_count; + + while(todo) { + + int to_mix = MIN(todo,256); //can't mix too much + + _process_chunk(p_src_frames,p_dst_frames,to_mix); + + p_src_frames+=to_mix; + p_dst_frames+=to_mix; + + todo-=to_mix; + } +} + +void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + + //fill ringbuffer + for(int i=0;i<p_frame_count;i++) { + audio_buffer[(buffer_pos+i)&buffer_mask]=p_src_frames[i]; + p_dst_frames[i]=p_src_frames[i]*base->dry; + } + + float mix_rate = AudioServer::get_singleton()->get_mix_rate(); + + /* process voices */ + for (int vc=0;vc<base->voice_count;vc++) { + + AudioEffectChorus::Voice &v=base->voice[vc]; + + + double time_to_mix=(float)p_frame_count/mix_rate; + double cycles_to_mix=time_to_mix*v.rate; + + unsigned int local_rb_pos=buffer_pos; + AudioFrame *dst_buff=p_dst_frames; + AudioFrame *rb_buff=audio_buffer.ptr(); + + double delay_msec=v.delay; + unsigned int delay_frames=Math::fast_ftoi((delay_msec/1000.0)*mix_rate); + float max_depth_frames=(v.depth/1000.0)*mix_rate; + + uint64_t local_cycles=cycles[vc]; + uint64_t increment=llrint(cycles_to_mix/(double)p_frame_count*(double)(1<<AudioEffectChorus::CYCLES_FRAC)); + + //check the LFO doesnt read ahead of the write pos + if ((((int)max_depth_frames)+10)>delay_frames) { //10 as some threshold to avoid precision stuff + delay_frames+=(int)max_depth_frames-delay_frames; + delay_frames+=10; //threshold to avoid precision stuff + + } + + + + //low pass filter + if (v.cutoff==0) + continue; + float auxlp=expf(-2.0*Math_PI*v.cutoff/mix_rate); + float c1=1.0-auxlp; + float c2=auxlp; + AudioFrame h=filter_h[vc]; + if (v.cutoff>=AudioEffectChorus::MS_CUTOFF_MAX) { + c1=1.0; c2=0.0; + } + + //vol modifier + + AudioFrame vol_modifier=AudioFrame(base->wet,base->wet) * Math::db2linear(v.level); + vol_modifier.l*=CLAMP( 1.0 - v.pan, 0, 1); + vol_modifier.r*=CLAMP( 1.0 + v.pan, 0, 1); + + + + for (int i=0;i<p_frame_count;i++) { + + /** COMPUTE WAVEFORM **/ + + float phase=(float)(local_cycles&AudioEffectChorus::CYCLES_MASK)/(float)(1<<AudioEffectChorus::CYCLES_FRAC); + + float wave_delay=sinf(phase*2.0*Math_PI)*max_depth_frames; + + int wave_delay_frames=lrint(floor(wave_delay)); + float wave_delay_frac=wave_delay-(float)wave_delay_frames; + + /** COMPUTE RINGBUFFER POS**/ + + unsigned int rb_source=local_rb_pos; + rb_source-=delay_frames; + + rb_source-=wave_delay_frames; + + /** READ FROM RINGBUFFER, LINEARLY INTERPOLATE */ + + AudioFrame val=rb_buff[rb_source&buffer_mask]; + AudioFrame val_next=rb_buff[(rb_source-1)&buffer_mask]; + + val+=(val_next-val)*wave_delay_frac; + + val=val*c1+h*c2; + h=val; + + /** MIX VALUE TO OUTPUT **/ + + dst_buff[i]+=val*vol_modifier; + + local_cycles+=increment; + local_rb_pos++; + + } + + filter_h[vc]=h; + cycles[vc]+=Math::fast_ftoi(cycles_to_mix*(double)(1<<AudioEffectChorus::CYCLES_FRAC)); + } + + buffer_pos+=p_frame_count; +} + + +Ref<AudioEffectInstance> AudioEffectChorus::instance() { + + Ref<AudioEffectChorusInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectChorus>(this); + for(int i=0;i<4;i++) { + ins->filter_h[i]=AudioFrame(0,0); + ins->cycles[i]=0; + } + + float ring_buffer_max_size=AudioEffectChorus::MAX_DELAY_MS+AudioEffectChorus::MAX_DEPTH_MS+AudioEffectChorus::MAX_WIDTH_MS; + + ring_buffer_max_size*=2; //just to avoid complications + 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->buffer_mask=ringbuff_size-1; + ins->buffer_pos=0; + ins->audio_buffer.resize(ringbuff_size); + for(int i=0;i<ringbuff_size;i++) { + ins->audio_buffer[i]=AudioFrame(0,0); + } + + return ins; +} + +void AudioEffectChorus::set_voice_count(int p_voices) { + + ERR_FAIL_COND(p_voices<1 || p_voices>=MAX_VOICES); + voice_count=p_voices; + _change_notify(); +} + + +int AudioEffectChorus::get_voice_count() const{ + + return voice_count; +} + +void AudioEffectChorus::set_voice_delay_ms(int p_voice,float p_delay_ms){ + + ERR_FAIL_INDEX(p_voice,MAX_VOICES); + + voice[p_voice].delay=p_delay_ms; + +} +float AudioEffectChorus::get_voice_delay_ms(int p_voice) const{ + + ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0); + return voice[p_voice].delay; +} + +void AudioEffectChorus::set_voice_rate_hz(int p_voice,float p_rate_hz){ + ERR_FAIL_INDEX(p_voice,MAX_VOICES); + + voice[p_voice].rate=p_rate_hz; +} +float AudioEffectChorus::get_voice_rate_hz(int p_voice) const{ + + ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0); + + return voice[p_voice].rate; +} + +void AudioEffectChorus::set_voice_depth_ms(int p_voice,float p_depth_ms){ + + ERR_FAIL_INDEX(p_voice,MAX_VOICES); + + voice[p_voice].depth=p_depth_ms; +} +float AudioEffectChorus::get_voice_depth_ms(int p_voice) const{ + + ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0); + + return voice[p_voice].depth; +} + +void AudioEffectChorus::set_voice_level_db(int p_voice,float p_level_db){ + + ERR_FAIL_INDEX(p_voice,MAX_VOICES); + + voice[p_voice].level=p_level_db; +} +float AudioEffectChorus::get_voice_level_db(int p_voice) const{ + + ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0); + + return voice[p_voice].level; +} + + +void AudioEffectChorus::set_voice_cutoff_hz(int p_voice,float p_cutoff_hz){ + + ERR_FAIL_INDEX(p_voice,MAX_VOICES); + + voice[p_voice].cutoff=p_cutoff_hz; +} +float AudioEffectChorus::get_voice_cutoff_hz(int p_voice) const{ + + ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0); + + return voice[p_voice].cutoff; +} + +void AudioEffectChorus::set_voice_pan(int p_voice,float p_pan){ + + ERR_FAIL_INDEX(p_voice,MAX_VOICES); + + voice[p_voice].pan=p_pan; +} +float AudioEffectChorus::get_voice_pan(int p_voice) const{ + + ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0); + + return voice[p_voice].pan; +} + + +void AudioEffectChorus::set_wet(float amount){ + + + wet=amount; +} +float AudioEffectChorus::get_wet() const{ + + return wet; +} + +void AudioEffectChorus::set_dry(float amount){ + + dry=amount; + +} +float AudioEffectChorus::get_dry() const{ + + return dry; +} + +void AudioEffectChorus::_validate_property(PropertyInfo& property) const { + + if (property.name.begins_with("voice/")) { + int voice_idx = property.name.get_slice("/",1).to_int(); + if (voice_idx>voice_count) { + property.usage=0; + } + } +} + + +void AudioEffectChorus::_bind_methods() { + + ClassDB::bind_method(_MD("set_voice_count","voices"),&AudioEffectChorus::set_voice_count); + ClassDB::bind_method(_MD("get_voice_count"),&AudioEffectChorus::get_voice_count); + + + ClassDB::bind_method(_MD("set_voice_delay_ms","voice_idx","delay_ms"),&AudioEffectChorus::set_voice_delay_ms); + ClassDB::bind_method(_MD("get_voice_delay_ms","voice_idx"),&AudioEffectChorus::get_voice_delay_ms); + + ClassDB::bind_method(_MD("set_voice_rate_hz","voice_idx","rate_hz"),&AudioEffectChorus::set_voice_rate_hz); + ClassDB::bind_method(_MD("get_voice_rate_hz","voice_idx"),&AudioEffectChorus::get_voice_rate_hz); + + ClassDB::bind_method(_MD("set_voice_depth_ms","voice_idx","depth_ms"),&AudioEffectChorus::set_voice_depth_ms); + ClassDB::bind_method(_MD("get_voice_depth_ms","voice_idx"),&AudioEffectChorus::get_voice_depth_ms); + + ClassDB::bind_method(_MD("set_voice_level_db","voice_idx","level_db"),&AudioEffectChorus::set_voice_level_db); + ClassDB::bind_method(_MD("get_voice_level_db","voice_idx"),&AudioEffectChorus::get_voice_level_db); + + ClassDB::bind_method(_MD("set_voice_cutoff_hz","voice_idx","cutoff_hz"),&AudioEffectChorus::set_voice_cutoff_hz); + ClassDB::bind_method(_MD("get_voice_cutoff_hz","voice_idx"),&AudioEffectChorus::get_voice_cutoff_hz); + + ClassDB::bind_method(_MD("set_voice_pan","voice_idx","pan"),&AudioEffectChorus::set_voice_pan); + ClassDB::bind_method(_MD("get_voice_pan","voice_idx"),&AudioEffectChorus::get_voice_pan); + + ClassDB::bind_method(_MD("set_wet","amount"),&AudioEffectChorus::set_wet); + ClassDB::bind_method(_MD("get_wet"),&AudioEffectChorus::get_wet); + + ClassDB::bind_method(_MD("set_dry","amount"),&AudioEffectChorus::set_dry); + ClassDB::bind_method(_MD("get_dry"),&AudioEffectChorus::get_dry); + + ADD_PROPERTY(PropertyInfo(Variant::INT,"voice_count",PROPERTY_HINT_RANGE,"1,4,1"),_SCS("set_voice_count"),_SCS("get_voice_count")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"dry",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_dry"),_SCS("get_dry")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"wet",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_wet"),_SCS("get_wet")); + + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),_SCS("set_voice_delay_ms"),_SCS("get_voice_delay_ms"),0); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),_SCS("set_voice_rate_hz"),_SCS("get_voice_rate_hz"),0); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),_SCS("set_voice_depth_ms"),_SCS("get_voice_depth_ms"),0); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),_SCS("set_voice_level_db"),_SCS("get_voice_level_db"),0); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),_SCS("set_voice_cutoff_hz"),_SCS("get_voice_cutoff_hz"),0); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_voice_pan"),_SCS("get_voice_pan"),0); + + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),_SCS("set_voice_delay_ms"),_SCS("get_voice_delay_ms"),1); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),_SCS("set_voice_rate_hz"),_SCS("get_voice_rate_hz"),1); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),_SCS("set_voice_depth_ms"),_SCS("get_voice_depth_ms"),1); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),_SCS("set_voice_level_db"),_SCS("get_voice_level_db"),1); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),_SCS("set_voice_cutoff_hz"),_SCS("get_voice_cutoff_hz"),1); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_voice_pan"),_SCS("get_voice_pan"),1); + + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),_SCS("set_voice_delay_ms"),_SCS("get_voice_delay_ms"),2); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),_SCS("set_voice_rate_hz"),_SCS("get_voice_rate_hz"),2); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),_SCS("set_voice_depth_ms"),_SCS("get_voice_depth_ms"),2); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),_SCS("set_voice_level_db"),_SCS("get_voice_level_db"),2); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),_SCS("set_voice_cutoff_hz"),_SCS("get_voice_cutoff_hz"),2); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_voice_pan"),_SCS("get_voice_pan"),2); + + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),_SCS("set_voice_delay_ms"),_SCS("get_voice_delay_ms"),3); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),_SCS("set_voice_rate_hz"),_SCS("get_voice_rate_hz"),3); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),_SCS("set_voice_depth_ms"),_SCS("get_voice_depth_ms"),3); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),_SCS("set_voice_level_db"),_SCS("get_voice_level_db"),3); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),_SCS("set_voice_cutoff_hz"),_SCS("get_voice_cutoff_hz"),3); + ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_voice_pan"),_SCS("get_voice_pan"),3); + +} + +AudioEffectChorus::AudioEffectChorus() +{ + voice_count=2; + voice[0].delay=15; + voice[1].delay=20; + voice[0].rate=0.8; + voice[1].rate=1.2; + voice[0].depth=2; + voice[1].depth=3; + voice[0].cutoff=8000; + voice[1].cutoff=8000; + voice[0].pan=-0.5; + voice[1].pan=0.5; + + wet=0.5; + dry=1.0; +} diff --git a/servers/audio/effects/audio_effect_chorus.h b/servers/audio/effects/audio_effect_chorus.h new file mode 100644 index 0000000000..4cfba5d61a --- /dev/null +++ b/servers/audio/effects/audio_effect_chorus.h @@ -0,0 +1,115 @@ +#ifndef AUDIOEFFECTCHORUS_H +#define AUDIOEFFECTCHORUS_H + + +#include "servers/audio/audio_effect.h" + +class AudioEffectChorus; + +class AudioEffectChorusInstance : public AudioEffectInstance { + GDCLASS(AudioEffectChorusInstance,AudioEffectInstance) +friend class AudioEffectChorus; + Ref<AudioEffectChorus> base; + + Vector<AudioFrame> audio_buffer; + unsigned int buffer_pos; + unsigned int buffer_mask; + + AudioFrame filter_h[4]; + uint64_t cycles[4]; + + void _process_chunk(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectChorus : public AudioEffect { + GDCLASS(AudioEffectChorus,AudioEffect) + +friend class AudioEffectChorusInstance; +public: + enum { + + MAX_DELAY_MS=50, + MAX_DEPTH_MS=20, + MAX_WIDTH_MS=50, + MAX_VOICES=4, + CYCLES_FRAC=16, + CYCLES_MASK=(1<<CYCLES_FRAC)-1, + MAX_CHANNELS=4, + MS_CUTOFF_MAX=16000 + }; + +private: + + struct Voice { + + float delay; + float rate; + float depth; + float level; + float cutoff; + float pan; + + Voice() { + + delay=12.0; + rate=1; + depth=0; + level=0; + cutoff=MS_CUTOFF_MAX; + pan=0; + + } + + } voice[MAX_VOICES]; + + int voice_count; + + float wet; + float dry; + + +protected: + void _validate_property(PropertyInfo& property) const; + + static void _bind_methods(); +public: + + void set_voice_count(int p_voices); + int get_voice_count() const; + + void set_voice_delay_ms(int p_voice,float p_delay_ms); + float get_voice_delay_ms(int p_voice) const; + + void set_voice_rate_hz(int p_voice,float p_rate_hz); + float get_voice_rate_hz(int p_voice) const; + + void set_voice_depth_ms(int p_voice,float p_depth_ms); + float get_voice_depth_ms(int p_voice) const; + + void set_voice_level_db(int p_voice,float p_level_db); + float get_voice_level_db(int p_voice) const; + + void set_voice_cutoff_hz(int p_voice,float p_cutoff_hz); + float get_voice_cutoff_hz(int p_voice) const; + + void set_voice_pan(int p_voice,float p_pan); + float get_voice_pan(int p_voice) const; + + void set_wet(float amount); + float get_wet() const; + + void set_dry(float amount); + float get_dry() const; + + Ref<AudioEffectInstance> instance(); + + AudioEffectChorus(); +}; + +#endif // AUDIOEFFECTCHORUS_H diff --git a/servers/audio/effects/audio_effect_compressor.cpp b/servers/audio/effects/audio_effect_compressor.cpp new file mode 100644 index 0000000000..5d116a9543 --- /dev/null +++ b/servers/audio/effects/audio_effect_compressor.cpp @@ -0,0 +1,227 @@ +#include "audio_effect_compressor.h" +#include "servers/audio_server.h" + +void AudioEffectCompressorInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + + float treshold = Math::db2linear(base->treshold); + float sample_rate=AudioServer::get_singleton()->get_mix_rate(); + + float ratatcoef = exp(-1 / (0.00001f * sample_rate)); + float ratrelcoef = exp(-1 / (0.5f * sample_rate)); + float attime = base->attack_us / 1000000.0; + float reltime = base->release_ms / 1000.0; + float atcoef = exp(-1 / (attime * sample_rate)); + float relcoef = exp(-1 / (reltime * sample_rate)); + + float makeup = Math::db2linear(base->gain); + + float mix = base->mix; + float gr_meter_decay = exp(1 / (1 * sample_rate)); + + const AudioFrame *src = p_src_frames; + + if (base->sidechain!=StringName() && current_channel!=-1) { + + int bus = AudioServer::get_singleton()->thread_find_bus_index(base->sidechain); + if (bus>=0) { + src = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus,current_channel); + } + } + + for(int i=0;i<p_frame_count;i++) { + + AudioFrame s = src[i]; + //convert to positive + s.l = Math::abs(s.l); + s.r = Math::abs(s.r); + + float peak = MAX(s.l,s.r); + + float overdb = 2.08136898f * Math::linear2db(peak/treshold); + + if (overdb<0.0) //we only care about what goes over to compress + overdb=0.0; + + if(overdb-rundb>5) // diffeence is too large + averatio = 4; + + if(overdb > rundb) { + rundb = overdb + atcoef * (rundb - overdb); + runratio = averatio + ratatcoef * (runratio - averatio); + } else { + rundb = overdb + relcoef * (rundb - overdb); + runratio = averatio + ratrelcoef * (runratio - averatio); + } + + overdb = rundb; + averatio = runratio; + + float cratio; + + if(false) { //rato all-in + cratio = 12 + averatio; + } else { + cratio = base->ratio; + } + + float gr = -overdb * (cratio-1)/cratio; + float grv = Math::db2linear(gr); + + runmax = maxover + relcoef * (runmax - maxover); // highest peak for setting att/rel decays in reltime + maxover = runmax; + + if (grv < gr_meter) { + gr_meter=grv; + } else { + gr_meter*=gr_meter_decay; + if(gr_meter>1) + gr_meter=1; + } + + + p_dst_frames[i] = p_src_frames[i] * grv * makeup * mix + p_src_frames[i] * (1.0-mix); + + } + +} + + +Ref<AudioEffectInstance> AudioEffectCompressor::instance() { + Ref<AudioEffectCompressorInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectCompressor>(this); + ins->rundb=0; + ins->runratio=0; + ins->averatio=0; + ins->runmax=0; + ins->maxover=0; + ins->gr_meter=1.0; + ins->current_channel=-1; + return ins; +} + + +void AudioEffectCompressor::set_treshold(float p_treshold) { + + treshold=p_treshold; +} + +float AudioEffectCompressor::get_treshold() const { + + return treshold; +} + +void AudioEffectCompressor::set_ratio(float p_ratio) { + + ratio=p_ratio; +} +float AudioEffectCompressor::get_ratio() const { + + return ratio; +} + +void AudioEffectCompressor::set_gain(float p_gain) { + + gain=p_gain; +} +float AudioEffectCompressor::get_gain() const { + + return gain; +} + +void AudioEffectCompressor::set_attack_us(float p_attack_us) { + + attack_us=p_attack_us; +} +float AudioEffectCompressor::get_attack_us() const { + + return attack_us; +} + +void AudioEffectCompressor::set_release_ms(float p_release_ms) { + + release_ms=p_release_ms; +} +float AudioEffectCompressor::get_release_ms() const { + + return release_ms; +} + +void AudioEffectCompressor::set_mix(float p_mix) { + + mix=p_mix; +} +float AudioEffectCompressor::get_mix() const { + + return mix; +} + +void AudioEffectCompressor::set_sidechain(const StringName& p_sidechain) { + + AudioServer::get_singleton()->lock(); + sidechain=p_sidechain; + AudioServer::get_singleton()->unlock(); +} + +StringName AudioEffectCompressor::get_sidechain() const { + + return sidechain; +} + +void AudioEffectCompressor::_validate_property(PropertyInfo& property) const { + + if (property.name=="sidechain") { + + String buses=""; + for(int i=0;i<AudioServer::get_singleton()->get_bus_count();i++) { + buses+=","; + buses+=AudioServer::get_singleton()->get_bus_name(i); + } + + property.hint_string=buses; + } +} + +void AudioEffectCompressor::_bind_methods() { + + ClassDB::bind_method(_MD("set_treshold","treshold"),&AudioEffectCompressor::set_treshold); + ClassDB::bind_method(_MD("get_treshold"),&AudioEffectCompressor::get_treshold); + + ClassDB::bind_method(_MD("set_ratio","ratio"),&AudioEffectCompressor::set_ratio); + ClassDB::bind_method(_MD("get_ratio"),&AudioEffectCompressor::get_ratio); + + ClassDB::bind_method(_MD("set_gain","gain"),&AudioEffectCompressor::set_gain); + ClassDB::bind_method(_MD("get_gain"),&AudioEffectCompressor::get_gain); + + ClassDB::bind_method(_MD("set_attack_us","attack_us"),&AudioEffectCompressor::set_attack_us); + ClassDB::bind_method(_MD("get_attack_us"),&AudioEffectCompressor::get_attack_us); + + ClassDB::bind_method(_MD("set_release_ms","release_ms"),&AudioEffectCompressor::set_release_ms); + ClassDB::bind_method(_MD("get_release_ms"),&AudioEffectCompressor::get_release_ms); + + ClassDB::bind_method(_MD("set_mix","mix"),&AudioEffectCompressor::set_mix); + ClassDB::bind_method(_MD("get_mix"),&AudioEffectCompressor::get_mix); + + ClassDB::bind_method(_MD("set_sidechain","sidechain"),&AudioEffectCompressor::set_sidechain); + ClassDB::bind_method(_MD("get_sidechain"),&AudioEffectCompressor::get_sidechain); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"treshold",PROPERTY_HINT_RANGE,"-60,0,0.1"),_SCS("set_treshold"),_SCS("get_treshold")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"ratio",PROPERTY_HINT_RANGE,"1,48,0.1"),_SCS("set_ratio"),_SCS("get_ratio")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"gain",PROPERTY_HINT_RANGE,"-20,20,0.1"),_SCS("set_gain"),_SCS("get_gain")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"attack_us",PROPERTY_HINT_RANGE,"20,2000,1"),_SCS("set_attack_us"),_SCS("get_attack_us")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"release_ms",PROPERTY_HINT_RANGE,"20,2000,1"),_SCS("set_release_ms"),_SCS("get_release_ms")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"mix",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_mix"),_SCS("get_mix")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"sidechain",PROPERTY_HINT_ENUM),_SCS("set_sidechain"),_SCS("get_sidechain")); + +} + +AudioEffectCompressor::AudioEffectCompressor() +{ + treshold=0; + ratio=4; + gain=0; + attack_us=20; + release_ms=250; + mix=1; +} diff --git a/servers/audio/effects/audio_effect_compressor.h b/servers/audio/effects/audio_effect_compressor.h new file mode 100644 index 0000000000..eb06b3db77 --- /dev/null +++ b/servers/audio/effects/audio_effect_compressor.h @@ -0,0 +1,70 @@ +#ifndef AUDIOEFFECTCOMPRESSOR_H +#define AUDIOEFFECTCOMPRESSOR_H + + +#include "servers/audio/audio_effect.h" + +class AudioEffectCompressor; + +class AudioEffectCompressorInstance : public AudioEffectInstance { + GDCLASS(AudioEffectCompressorInstance,AudioEffectInstance) +friend class AudioEffectCompressor; + Ref<AudioEffectCompressor> base; + + float rundb,averatio,runratio,runmax,maxover,gr_meter; + int current_channel; +public: + + void set_current_channel(int p_channel) { current_channel=p_channel; } + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectCompressor : public AudioEffect { + GDCLASS(AudioEffectCompressor,AudioEffect) + +friend class AudioEffectCompressorInstance; + float treshold; + float ratio; + float gain; + float attack_us; + float release_ms; + float mix; + StringName sidechain; + + +protected: + void _validate_property(PropertyInfo& property) const; + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + + + void set_treshold(float p_treshold); + float get_treshold() const; + + void set_ratio(float p_ratio); + float get_ratio() const; + + void set_gain(float p_gain); + float get_gain() const; + + void set_attack_us(float p_attack_us); + float get_attack_us() const; + + void set_release_ms(float p_release_ms); + float get_release_ms() const; + + void set_mix(float p_mix); + float get_mix() const; + + void set_sidechain(const StringName& p_sidechain); + StringName get_sidechain() const; + + AudioEffectCompressor(); +}; + +#endif // AUDIOEFFECTCOMPRESSOR_H diff --git a/servers/audio/effects/audio_effect_delay.cpp b/servers/audio/effects/audio_effect_delay.cpp new file mode 100644 index 0000000000..aae2657a63 --- /dev/null +++ b/servers/audio/effects/audio_effect_delay.cpp @@ -0,0 +1,326 @@ +#include "audio_effect_delay.h" +#include "servers/audio_server.h" +#include "math_funcs.h" + +void AudioEffectDelayInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + int todo = p_frame_count; + + while(todo) { + + int to_mix = MIN(todo,256); //can't mix too much + + _process_chunk(p_src_frames,p_dst_frames,to_mix); + + p_src_frames+=to_mix; + p_dst_frames+=to_mix; + + todo-=to_mix; + } +} + +void AudioEffectDelayInstance::_process_chunk(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + + + float main_level_f=base->dry; + + + float mix_rate = AudioServer::get_singleton()->get_mix_rate(); + + float tap_1_level_f=base->tap_1_active?Math::db2linear(base->tap_1_level):0.0; + int tap_1_delay_frames=int((base->tap_1_delay_ms/1000.0)*mix_rate);; + + float tap_2_level_f=base->tap_2_active?Math::db2linear(base->tap_2_level):0.0; + int tap_2_delay_frames=int((base->tap_2_delay_ms/1000.0)*mix_rate);; + + float feedback_level_f=base->feedback_active?Math::db2linear(base->feedback_level):0.0; + unsigned int feedback_delay_frames=int((base->feedback_delay_ms/1000.0)*mix_rate);; + + + AudioFrame tap1_vol=AudioFrame(tap_1_level_f,tap_1_level_f); + + tap1_vol.l*=CLAMP( 1.0 - base->tap_1_pan, 0, 1); + tap1_vol.r*=CLAMP( 1.0 + base->tap_1_pan, 0, 1); + + AudioFrame tap2_vol=AudioFrame(tap_2_level_f,tap_2_level_f); + + tap2_vol.l*=CLAMP( 1.0 - base->tap_2_pan, 0, 1); + tap2_vol.r*=CLAMP( 1.0 + base->tap_2_pan, 0, 1); + + // feedback lowpass here + float lpf_c=expf(-2.0*Math_PI*base->feedback_lowpass/mix_rate); // 0 .. 10khz + float lpf_ic=1.0-lpf_c; + + const AudioFrame *src=p_src_frames; + AudioFrame *dst=p_dst_frames; + AudioFrame *rb_buf=ring_buffer.ptr(); + AudioFrame *fb_buf=feedback_buffer.ptr(); + + + for (int i=0;i<p_frame_count;i++) { + + + rb_buf[ring_buffer_pos&ring_buffer_mask]=src[i]; + + AudioFrame main_val=src[i]*main_level_f; + AudioFrame tap_1_val=rb_buf[(ring_buffer_pos-tap_1_delay_frames)&ring_buffer_mask]*tap1_vol; + AudioFrame tap_2_val=rb_buf[(ring_buffer_pos-tap_2_delay_frames)&ring_buffer_mask]*tap2_vol; + + AudioFrame out=main_val+tap_1_val+tap_2_val; + + out+=fb_buf[ feedback_buffer_pos ]; + + //apply lowpass and feedback gain + AudioFrame fb_in=out*feedback_level_f*lpf_ic+h*lpf_c; + fb_in.undenormalise(); //avoid denormals + + h=fb_in; + fb_buf[ feedback_buffer_pos ]=fb_in; + + dst[i]=out; + + ring_buffer_pos++; + + if ( (++feedback_buffer_pos) >= feedback_delay_frames ) + feedback_buffer_pos=0; + } +} + + + +Ref<AudioEffectInstance> AudioEffectDelay::instance() { + Ref<AudioEffectDelayInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectDelay>(this); + + float ring_buffer_max_size=MAX_DELAY_MS+100; //add 100ms of extra room, just in case + 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->feedback_buffer.resize( ringbuff_size ); + + ins->feedback_buffer_pos=0; + + ins->h=AudioFrame(0,0); + + return ins; +} + + +void AudioEffectDelay::set_dry(float p_dry) { + + dry=p_dry; +} + +float AudioEffectDelay::get_dry(){ + + return dry; +} + +void AudioEffectDelay::set_tap1_active(bool p_active){ + + tap_1_active=p_active; +} +bool AudioEffectDelay::is_tap1_active() const{ + + return tap_1_active; +} + +void AudioEffectDelay::set_tap1_delay_ms(float p_delay_ms){ + + tap_1_delay_ms=p_delay_ms; +} +float AudioEffectDelay::get_tap1_delay_ms() const{ + + return tap_1_delay_ms; +} + +void AudioEffectDelay::set_tap1_level_db(float p_level_db){ + + tap_1_level=p_level_db; +} +float AudioEffectDelay::get_tap1_level_db() const{ + + return tap_1_level; +} + +void AudioEffectDelay::set_tap1_pan(float p_pan){ + + tap_1_pan=p_pan; +} +float AudioEffectDelay::get_tap1_pan() const{ + + return tap_1_pan; +} + + +void AudioEffectDelay::set_tap2_active(bool p_active){ + + tap_2_active=p_active; +} +bool AudioEffectDelay::is_tap2_active() const{ + + return tap_2_active; +} + +void AudioEffectDelay::set_tap2_delay_ms(float p_delay_ms){ + + tap_2_delay_ms=p_delay_ms; +} +float AudioEffectDelay::get_tap2_delay_ms() const{ + + return tap_2_delay_ms; +} + +void AudioEffectDelay::set_tap2_level_db(float p_level_db){ + + tap_2_level=p_level_db; +} +float AudioEffectDelay::get_tap2_level_db() const{ + + return tap_2_level; +} + +void AudioEffectDelay::set_tap2_pan(float p_pan){ + + tap_2_pan=p_pan; +} +float AudioEffectDelay::get_tap2_pan() const{ + + return tap_2_pan; +} + +void AudioEffectDelay::set_feedback_active(bool p_active){ + + feedback_active=p_active; +} +bool AudioEffectDelay::is_feedback_active() const{ + + return feedback_active; +} + +void AudioEffectDelay::set_feedback_delay_ms(float p_delay_ms){ + + feedback_delay_ms=p_delay_ms; +} +float AudioEffectDelay::get_feedback_delay_ms() const{ + + return feedback_delay_ms; +} + +void AudioEffectDelay::set_feedback_level_db(float p_level_db){ + + feedback_level=p_level_db; +} +float AudioEffectDelay::get_feedback_level_db() const{ + + return feedback_level; +} + +void AudioEffectDelay::set_feedback_lowpass(float p_lowpass){ + + feedback_lowpass=p_lowpass; +} +float AudioEffectDelay::get_feedback_lowpass() const{ + + return feedback_lowpass; +} + + +void AudioEffectDelay::_bind_methods() { + + ClassDB::bind_method(_MD("set_dry","amount"),&AudioEffectDelay::set_dry); + ClassDB::bind_method(_MD("get_dry"),&AudioEffectDelay::get_dry); + + ClassDB::bind_method(_MD("set_tap1_active","amount"),&AudioEffectDelay::set_tap1_active); + ClassDB::bind_method(_MD("is_tap1_active"),&AudioEffectDelay::is_tap1_active); + + ClassDB::bind_method(_MD("set_tap1_delay_ms","amount"),&AudioEffectDelay::set_tap1_delay_ms); + ClassDB::bind_method(_MD("get_tap1_delay_ms"),&AudioEffectDelay::get_tap1_delay_ms); + + ClassDB::bind_method(_MD("set_tap1_level_db","amount"),&AudioEffectDelay::set_tap1_level_db); + ClassDB::bind_method(_MD("get_tap1_level_db"),&AudioEffectDelay::get_tap1_level_db); + + ClassDB::bind_method(_MD("set_tap1_pan","amount"),&AudioEffectDelay::set_tap1_pan); + ClassDB::bind_method(_MD("get_tap1_pan"),&AudioEffectDelay::get_tap1_pan); + + ClassDB::bind_method(_MD("set_tap2_active","amount"),&AudioEffectDelay::set_tap2_active); + ClassDB::bind_method(_MD("is_tap2_active"),&AudioEffectDelay::is_tap2_active); + + ClassDB::bind_method(_MD("set_tap2_delay_ms","amount"),&AudioEffectDelay::set_tap2_delay_ms); + ClassDB::bind_method(_MD("get_tap2_delay_ms"),&AudioEffectDelay::get_tap2_delay_ms); + + ClassDB::bind_method(_MD("set_tap2_level_db","amount"),&AudioEffectDelay::set_tap2_level_db); + ClassDB::bind_method(_MD("get_tap2_level_db"),&AudioEffectDelay::get_tap2_level_db); + + ClassDB::bind_method(_MD("set_tap2_pan","amount"),&AudioEffectDelay::set_tap2_pan); + ClassDB::bind_method(_MD("get_tap2_pan"),&AudioEffectDelay::get_tap2_pan); + + + ClassDB::bind_method(_MD("set_feedback_active","amount"),&AudioEffectDelay::set_feedback_active); + ClassDB::bind_method(_MD("is_feedback_active"),&AudioEffectDelay::is_feedback_active); + + ClassDB::bind_method(_MD("set_feedback_delay_ms","amount"),&AudioEffectDelay::set_feedback_delay_ms); + ClassDB::bind_method(_MD("get_feedback_delay_ms"),&AudioEffectDelay::get_feedback_delay_ms); + + ClassDB::bind_method(_MD("set_feedback_level_db","amount"),&AudioEffectDelay::set_feedback_level_db); + ClassDB::bind_method(_MD("get_feedback_level_db"),&AudioEffectDelay::get_feedback_level_db); + + ClassDB::bind_method(_MD("set_feedback_lowpass","amount"),&AudioEffectDelay::set_feedback_lowpass); + ClassDB::bind_method(_MD("get_feedback_lowpass"),&AudioEffectDelay::get_feedback_lowpass); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"dry",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_dry"),_SCS("get_dry")); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL,"tap1/active"),_SCS("set_tap1_active"),_SCS("is_tap1_active")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"tap1/delay_ms",PROPERTY_HINT_RANGE,"0,1500,1"),_SCS("set_tap1_delay_ms"),_SCS("get_tap1_delay_ms")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"tap1/level_db",PROPERTY_HINT_RANGE,"-60,0,0.01"),_SCS("set_tap1_level_db"),_SCS("get_tap1_level_db")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"tap1/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_tap1_pan"),_SCS("get_tap1_pan")); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL,"tap2/active"),_SCS("set_tap2_active"),_SCS("is_tap2_active")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"tap2/delay_ms",PROPERTY_HINT_RANGE,"0,1500,1"),_SCS("set_tap2_delay_ms"),_SCS("get_tap2_delay_ms")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"tap2/level_db",PROPERTY_HINT_RANGE,"-60,0,0.01"),_SCS("set_tap2_level_db"),_SCS("get_tap2_level_db")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"tap2/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_tap2_pan"),_SCS("get_tap2_pan")); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL,"feedback/active"),_SCS("set_feedback_active"),_SCS("is_feedback_active")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"feedback/delay_ms",PROPERTY_HINT_RANGE,"0,1500,1"),_SCS("set_feedback_delay_ms"),_SCS("get_feedback_delay_ms")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"feedback/level_db",PROPERTY_HINT_RANGE,"-60,0,0.01"),_SCS("set_feedback_level_db"),_SCS("get_feedback_level_db")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"feedback/lowpass",PROPERTY_HINT_RANGE,"1,16000,1"),_SCS("set_feedback_lowpass"),_SCS("get_feedback_lowpass")); + + +} + +AudioEffectDelay::AudioEffectDelay() +{ + tap_1_active=true; + tap_1_delay_ms=250; + tap_1_level=-6; + tap_1_pan=0.2; + + tap_2_active=true; + tap_2_delay_ms=500; + tap_2_level=-12; + tap_2_pan=-0.4; + + feedback_active=false; + feedback_delay_ms=340; + feedback_level=-6; + feedback_lowpass=16000; + + dry=1.0; + +} diff --git a/servers/audio/effects/audio_effect_delay.h b/servers/audio/effects/audio_effect_delay.h new file mode 100644 index 0000000000..645561138b --- /dev/null +++ b/servers/audio/effects/audio_effect_delay.h @@ -0,0 +1,112 @@ +#ifndef AUDIOEFFECTECHO_H +#define AUDIOEFFECTECHO_H + +#include "servers/audio/audio_effect.h" + +class AudioEffectDelay; + +class AudioEffectDelayInstance : public AudioEffectInstance { + GDCLASS(AudioEffectDelayInstance,AudioEffectInstance) +friend class AudioEffectDelay; + Ref<AudioEffectDelay> base; + + Vector<AudioFrame> ring_buffer; + + unsigned int ring_buffer_pos; + unsigned int ring_buffer_mask; + + /* feedback buffer */ + Vector<AudioFrame> feedback_buffer; + + unsigned int feedback_buffer_pos; + + AudioFrame h; + void _process_chunk(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectDelay : public AudioEffect { + GDCLASS(AudioEffectDelay,AudioEffect) + +friend class AudioEffectDelayInstance; + enum { + + MAX_DELAY_MS=3000, + MAX_TAPS=2 + }; + + float dry; + + bool tap_1_active; + float tap_1_delay_ms; + float tap_1_level; + float tap_1_pan; + + bool tap_2_active; + float tap_2_delay_ms; + float tap_2_level; + float tap_2_pan; + + bool feedback_active; + float feedback_delay_ms; + float feedback_level; + float feedback_lowpass; + + + +protected: + + static void _bind_methods(); +public: + + void set_dry(float p_dry); + float get_dry(); + + void set_tap1_active(bool p_active); + bool is_tap1_active() const; + + void set_tap1_delay_ms(float p_delay_ms); + float get_tap1_delay_ms() const; + + void set_tap1_level_db(float p_level_db); + float get_tap1_level_db() const; + + void set_tap1_pan(float p_pan); + float get_tap1_pan() const; + + void set_tap2_active(bool p_active); + bool is_tap2_active() const; + + void set_tap2_delay_ms(float p_delay_ms); + float get_tap2_delay_ms() const; + + void set_tap2_level_db(float p_level_db); + float get_tap2_level_db() const; + + void set_tap2_pan(float p_pan); + float get_tap2_pan() const; + + void set_feedback_active(bool p_active); + bool is_feedback_active() const; + + void set_feedback_delay_ms(float p_delay_ms); + float get_feedback_delay_ms() const; + + void set_feedback_level_db(float p_level_db); + float get_feedback_level_db() const; + + void set_feedback_lowpass(float p_lowpass); + float get_feedback_lowpass() const; + + Ref<AudioEffectInstance> instance(); + + AudioEffectDelay(); +}; + + +#endif // AUDIOEFFECTECHO_H diff --git a/servers/audio/effects/audio_effect_distortion.cpp b/servers/audio/effects/audio_effect_distortion.cpp new file mode 100644 index 0000000000..3ba409b0a5 --- /dev/null +++ b/servers/audio/effects/audio_effect_distortion.cpp @@ -0,0 +1,171 @@ +#include "audio_effect_distortion.h" +#include "servers/audio_server.h" +#include "math_funcs.h" + + + +void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + const float *src = (const float*)p_src_frames; + float *dst = (float*)p_dst_frames; + + //float lpf_c=expf(-2.0*Math_PI*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE)); + float lpf_c=expf(-2.0*Math_PI*base->keep_hf_hz/(AudioServer::get_singleton()->get_mix_rate())); + float lpf_ic=1.0-lpf_c; + + float drive_f=base->drive; + float pregain_f=Math::db2linear(base->pre_gain); + float postgain_f=Math::db2linear(base->post_gain); + + float atan_mult=pow(10,drive_f*drive_f*3.0)-1.0+0.001; + float atan_div=1.0/(atanf(atan_mult)*(1.0+drive_f*8)); + + float lofi_mult=powf(2.0,2.0+(1.0-drive_f)*14); //goes from 16 to 2 bits + + for (int i=0;i<p_frame_count*2;i++) { + + float out=undenormalise(src[i]*lpf_ic+lpf_c*h[i&1]); + h[i&1]=out; + float a=out; + float ha=src[i]-out; //high freqs + a*=pregain_f; + + switch (base->mode) { + + case AudioEffectDistortion::MODE_CLIP: { + + a=powf(a,1.0001-drive_f); + if (a>1.0) + a=1.0; + else if (a<(-1.0)) + a=-1.0; + + } break; + case AudioEffectDistortion::MODE_ATAN: { + + + a=atanf(a*atan_mult)*atan_div; + + } break; + case AudioEffectDistortion::MODE_LOFI: { + + a = floorf(a*lofi_mult+0.5)/lofi_mult; + + } break; + case AudioEffectDistortion::MODE_OVERDRIVE: { + + + const double x = a * 0.686306; + const double z = 1 + exp (sqrt (fabs (x)) * -0.75); + a = (expf(x) - expf(-x * z)) / (expf(x) + expf(-x)); + } break; + case AudioEffectDistortion::MODE_WAVESHAPE: { + float x = a; + float k= 2*drive_f/(1.00001-drive_f); + + a = (1.0+k)*x/(1.0+k*fabsf(x)); + + + } break; + } + + dst[i]=a*postgain_f+ha; + + } + + +} + + +Ref<AudioEffectInstance> AudioEffectDistortion::instance() { + Ref<AudioEffectDistortionInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectDistortion>(this); + ins->h[0]=0; + ins->h[1]=0; + + return ins; +} + + +void AudioEffectDistortion::set_mode(Mode p_mode) { + + mode=p_mode; +} + +AudioEffectDistortion::Mode AudioEffectDistortion::get_mode() const{ + + return mode; +} + +void AudioEffectDistortion::set_pre_gain(float p_pre_gain){ + + pre_gain=p_pre_gain; +} +float AudioEffectDistortion::get_pre_gain() const{ + + return pre_gain; +} + +void AudioEffectDistortion::set_keep_hf_hz(float p_keep_hf_hz){ + + keep_hf_hz=p_keep_hf_hz; +} +float AudioEffectDistortion::get_keep_hf_hz() const{ + + return keep_hf_hz; +} + +void AudioEffectDistortion::set_drive(float p_drive){ + + drive=p_drive; +} +float AudioEffectDistortion::get_drive() const{ + + return drive; +} + +void AudioEffectDistortion::set_post_gain(float p_post_gain){ + + post_gain=p_post_gain; +} +float AudioEffectDistortion::get_post_gain() const{ + + return post_gain; +} + + +void AudioEffectDistortion::_bind_methods() { + + ClassDB::bind_method(_MD("set_mode","mode"),&AudioEffectDistortion::set_mode); + ClassDB::bind_method(_MD("get_mode"),&AudioEffectDistortion::get_mode); + + ClassDB::bind_method(_MD("set_pre_gain","pre_gain"),&AudioEffectDistortion::set_pre_gain); + ClassDB::bind_method(_MD("get_pre_gain"),&AudioEffectDistortion::get_pre_gain); + + ClassDB::bind_method(_MD("set_keep_hf_hz","keep_hf_hz"),&AudioEffectDistortion::set_keep_hf_hz); + ClassDB::bind_method(_MD("get_keep_hf_hz"),&AudioEffectDistortion::get_keep_hf_hz); + + ClassDB::bind_method(_MD("set_drive","drive"),&AudioEffectDistortion::set_drive); + ClassDB::bind_method(_MD("get_drive"),&AudioEffectDistortion::get_drive); + + + ClassDB::bind_method(_MD("set_post_gain","post_gain"),&AudioEffectDistortion::set_post_gain); + ClassDB::bind_method(_MD("get_post_gain"),&AudioEffectDistortion::get_post_gain); + + ADD_PROPERTY(PropertyInfo(Variant::INT,"mode",PROPERTY_HINT_ENUM,"Clip,ATan,LoFi,Overdrive,WaveShape"),_SCS("set_mode"),_SCS("get_mode")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"pre_gain",PROPERTY_HINT_RANGE,"-60,60,0.01"),_SCS("set_pre_gain"),_SCS("get_pre_gain")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"keep_hf_hz",PROPERTY_HINT_RANGE,"1,20000,1"),_SCS("set_keep_hf_hz"),_SCS("get_keep_hf_hz")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"drive",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_drive"),_SCS("get_drive")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"post_gain",PROPERTY_HINT_RANGE,"-80,24,0.01"),_SCS("set_post_gain"),_SCS("get_post_gain")); +} + +AudioEffectDistortion::AudioEffectDistortion() +{ + mode=MODE_CLIP; + pre_gain=0; + post_gain=0; + keep_hf_hz=16000; + drive=0; +} + diff --git a/servers/audio/effects/audio_effect_distortion.h b/servers/audio/effects/audio_effect_distortion.h new file mode 100644 index 0000000000..1d2433faeb --- /dev/null +++ b/servers/audio/effects/audio_effect_distortion.h @@ -0,0 +1,69 @@ +#ifndef AUDIOEFFECTDISTORTION_H +#define AUDIOEFFECTDISTORTION_H + +#include "servers/audio/audio_effect.h" + +class AudioEffectDistortion; + +class AudioEffectDistortionInstance : public AudioEffectInstance { + GDCLASS(AudioEffectDistortionInstance,AudioEffectInstance) +friend class AudioEffectDistortion; + Ref<AudioEffectDistortion> base; + float h[2]; +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectDistortion : public AudioEffect { + GDCLASS(AudioEffectDistortion,AudioEffect) +public: + enum Mode { + MODE_CLIP, + MODE_ATAN, + MODE_LOFI, + MODE_OVERDRIVE, + MODE_WAVESHAPE, + }; + +friend class AudioEffectDistortionInstance; + Mode mode; + float pre_gain; + float post_gain; + float keep_hf_hz; + float drive; + +protected: + + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + + + void set_mode(Mode p_mode); + Mode get_mode() const; + + void set_pre_gain(float pre_gain); + float get_pre_gain() const; + + void set_keep_hf_hz(float keep_hf_hz); + float get_keep_hf_hz() const; + + void set_drive(float drive); + float get_drive() const; + + void set_post_gain(float post_gain); + float get_post_gain() const; + + + + AudioEffectDistortion(); +}; + +VARIANT_ENUM_CAST( AudioEffectDistortion::Mode ) + +#endif // AUDIOEFFECTDISTORTION_H diff --git a/servers/audio/effects/audio_effect_eq.cpp b/servers/audio/effects/audio_effect_eq.cpp new file mode 100644 index 0000000000..3c6a684224 --- /dev/null +++ b/servers/audio/effects/audio_effect_eq.cpp @@ -0,0 +1,122 @@ +#include "audio_effect_eq.h" +#include "servers/audio_server.h" + + +void AudioEffectEQInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + int band_count = bands[0].size(); + EQ::BandProcess *proc_l = bands[0].ptr(); + EQ::BandProcess *proc_r = bands[1].ptr(); + float *bgain = gains.ptr(); + for(int i=0;i<band_count;i++) { + bgain[i]=Math::db2linear(base->gain[i]); + } + + + for(int i=0;i<p_frame_count;i++) { + + AudioFrame src = p_src_frames[i]; + AudioFrame dst = AudioFrame(0,0); + + for(int j=0;j<band_count;j++) { + + float l = src.l; + float r = src.r; + + proc_l[j].process_one(l); + proc_r[j].process_one(r); + + dst.l+=l * bgain[j]; + dst.r+=r * bgain[j]; + } + + p_dst_frames[i]=dst; + } + +} + + +Ref<AudioEffectInstance> AudioEffectEQ::instance() { + Ref<AudioEffectEQInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectEQ>(this); + ins->gains.resize(eq.get_band_count()); + for(int i=0;i<2;i++) { + ins->bands[i].resize(eq.get_band_count()); + for(int j=0;j<ins->bands[i].size();j++) { + ins->bands[i][j]=eq.get_band_processor(j); + } + } + + return ins; +} + +void AudioEffectEQ::set_band_gain_db(int p_band,float p_volume) { + ERR_FAIL_INDEX(p_band,gain.size()); + gain[p_band]=p_volume; +} + +float AudioEffectEQ::get_band_gain_db(int p_band) const { + ERR_FAIL_INDEX_V(p_band,gain.size(),0); + + return gain[p_band]; +} +int AudioEffectEQ::get_band_count() const { + return gain.size(); +} + +bool AudioEffectEQ::_set(const StringName& p_name, const Variant& p_value) { + + const Map<StringName,int>::Element *E=prop_band_map.find(p_name); + if (E) { + set_band_gain_db(E->get(),p_value); + return true; + } + + return false; +} + +bool AudioEffectEQ::_get(const StringName& p_name,Variant &r_ret) const{ + + const Map<StringName,int>::Element *E=prop_band_map.find(p_name); + if (E) { + r_ret=get_band_gain_db(E->get()); + return true; + } + + return false; + +} + +void AudioEffectEQ::_get_property_list( List<PropertyInfo> *p_list) const{ + + for(int i=0;i<band_names.size();i++) { + + p_list->push_back(PropertyInfo(Variant::REAL,band_names[i],PROPERTY_HINT_RANGE,"-60,24,0.1")); + } +} + + + +void AudioEffectEQ::_bind_methods() { + + ClassDB::bind_method(_MD("set_band_gain_db","band_idx","volume_db"),&AudioEffectEQ::set_band_gain_db); + ClassDB::bind_method(_MD("get_band_gain_db","band_idx"),&AudioEffectEQ::get_band_gain_db); + ClassDB::bind_method(_MD("get_band_count"),&AudioEffectEQ::get_band_count); + +} + +AudioEffectEQ::AudioEffectEQ(EQ::Preset p_preset) +{ + + + eq.set_mix_rate(AudioServer::get_singleton()->get_mix_rate()); + eq.set_preset_band_mode(p_preset); + gain.resize(eq.get_band_count()); + for(int i=0;i<gain.size();i++) { + gain[i]=0.0; + String name = "band_db/"+itos(eq.get_band_frequency(i))+"_hz"; + prop_band_map[name]=i; + band_names.push_back(name); + } +} diff --git a/servers/audio/effects/audio_effect_eq.h b/servers/audio/effects/audio_effect_eq.h new file mode 100644 index 0000000000..3fcc2c0056 --- /dev/null +++ b/servers/audio/effects/audio_effect_eq.h @@ -0,0 +1,72 @@ +#ifndef AUDIOEFFECTEQ_H +#define AUDIOEFFECTEQ_H + + +#include "servers/audio/audio_effect.h" +#include "servers/audio/effects/eq.h" + +class AudioEffectEQ; + +class AudioEffectEQInstance : public AudioEffectInstance { + GDCLASS(AudioEffectEQInstance,AudioEffectInstance) +friend class AudioEffectEQ; + Ref<AudioEffectEQ> base; + + Vector<EQ::BandProcess> bands[2]; + Vector<float> gains; +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectEQ : public AudioEffect { + GDCLASS(AudioEffectEQ,AudioEffect) + +friend class AudioEffectEQInstance; + + EQ eq; + Vector<float> gain; + Map<StringName,int> prop_band_map; + Vector<String> band_names; + +protected: + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list( List<PropertyInfo> *p_list) const; + + + + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + void set_band_gain_db(int p_band,float p_volume); + float get_band_gain_db(int p_band) const; + int get_band_count() const; + + AudioEffectEQ(EQ::Preset p_preset=EQ::PRESET_6_BANDS); +}; + + +class AudioEffectEQ6 : public AudioEffectEQ { + GDCLASS(AudioEffectEQ6,AudioEffectEQ) +public: + AudioEffectEQ6() : AudioEffectEQ(EQ::PRESET_6_BANDS) {} +}; + +class AudioEffectEQ10 : public AudioEffectEQ { + GDCLASS(AudioEffectEQ10,AudioEffectEQ) +public: + AudioEffectEQ10() : AudioEffectEQ(EQ::PRESET_10_BANDS) {} +}; + +class AudioEffectEQ21 : public AudioEffectEQ { + GDCLASS(AudioEffectEQ21,AudioEffectEQ) +public: + AudioEffectEQ21() : AudioEffectEQ(EQ::PRESET_21_BANDS) {} +}; + +#endif // AUDIOEFFECTEQ_H diff --git a/servers/audio/effects/audio_effect_filter.cpp b/servers/audio/effects/audio_effect_filter.cpp new file mode 100644 index 0000000000..4e54ea1f3e --- /dev/null +++ b/servers/audio/effects/audio_effect_filter.cpp @@ -0,0 +1,151 @@ +#include "audio_effect_filter.h" +#include "servers/audio_server.h" + +template<int S> +void AudioEffectFilterInstance::_process_filter(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + for(int i=0;i<p_frame_count;i++) { + float f = p_src_frames[i].l; + filter_process[0][0].process_one(f); + if (S>1) + filter_process[0][1].process_one(f); + if (S>2) + filter_process[0][2].process_one(f); + if (S>3) + filter_process[0][3].process_one(f); + + p_dst_frames[i].l=f; + } + + for(int i=0;i<p_frame_count;i++) { + float f = p_src_frames[i].r; + filter_process[1][0].process_one(f); + if (S>1) + filter_process[1][1].process_one(f); + if (S>2) + filter_process[1][2].process_one(f); + if (S>3) + filter_process[1][3].process_one(f); + + p_dst_frames[i].r=f; + } + +} + +void AudioEffectFilterInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + filter.set_cutoff(base->cutoff); + filter.set_gain(base->gain); + filter.set_resonance(base->resonance); + filter.set_mode(base->mode); + int stages = int(base->db)+1; + filter.set_stages(stages); + filter.set_sampling_rate(AudioServer::get_singleton()->get_mix_rate()); + + for(int i=0;i<2;i++) { + for(int j=0;j<4;j++) { + filter_process[i][j].update_coeffs(); + } + } + + + if (stages==1) { + _process_filter<1>(p_src_frames,p_dst_frames,p_frame_count); + } else if (stages==2) { + _process_filter<2>(p_src_frames,p_dst_frames,p_frame_count); + } else if (stages==3) { + _process_filter<3>(p_src_frames,p_dst_frames,p_frame_count); + } else if (stages==4) { + _process_filter<4>(p_src_frames,p_dst_frames,p_frame_count); + } + +} + + +AudioEffectFilterInstance::AudioEffectFilterInstance() { + + for(int i=0;i<2;i++) { + for(int j=0;j<4;j++) { + filter_process[i][j].set_filter(&filter); + } + } + +} + + +Ref<AudioEffectInstance> AudioEffectFilter::instance() { + Ref<AudioEffectFilterInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectFilter>(this); + + return ins; +} + +void AudioEffectFilter::set_cutoff(float p_freq) { + + cutoff=p_freq; +} + +float AudioEffectFilter::get_cutoff() const{ + + return cutoff; +} + +void AudioEffectFilter::set_resonance(float p_amount){ + + resonance=p_amount; +} +float AudioEffectFilter::get_resonance() const{ + + return resonance; +} + +void AudioEffectFilter::set_gain(float p_amount){ + + gain=p_amount; +} +float AudioEffectFilter::get_gain() const { + + return gain; +} + + + +void AudioEffectFilter::set_db(FilterDB p_db) { + db=p_db; +} + +AudioEffectFilter::FilterDB AudioEffectFilter::get_db() const { + + return db; +} + +void AudioEffectFilter::_bind_methods() { + + ClassDB::bind_method(_MD("set_cutoff","freq"),&AudioEffectFilter::set_cutoff); + ClassDB::bind_method(_MD("get_cutoff"),&AudioEffectFilter::get_cutoff); + + ClassDB::bind_method(_MD("set_resonance","amount"),&AudioEffectFilter::set_resonance); + ClassDB::bind_method(_MD("get_resonance"),&AudioEffectFilter::get_resonance); + + ClassDB::bind_method(_MD("set_gain","amount"),&AudioEffectFilter::set_gain); + ClassDB::bind_method(_MD("get_gain"),&AudioEffectFilter::get_gain); + + ClassDB::bind_method(_MD("set_db","amount"),&AudioEffectFilter::set_db); + ClassDB::bind_method(_MD("get_db"),&AudioEffectFilter::get_db); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"cutoff_hz",PROPERTY_HINT_RANGE,"1,40000,0.1"),_SCS("set_cutoff"),_SCS("get_cutoff")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"resonance",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_resonance"),_SCS("get_resonance")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"gain",PROPERTY_HINT_RANGE,"0,4,0.01"),_SCS("set_gain"),_SCS("get_gain")); + ADD_PROPERTY(PropertyInfo(Variant::INT,"dB",PROPERTY_HINT_ENUM,"6db,12db,18db,24db"),_SCS("set_db"),_SCS("get_db")); +} + +AudioEffectFilter::AudioEffectFilter(AudioFilterSW::Mode p_mode) +{ + + mode=p_mode; + cutoff=2000; + resonance=0.5; + gain=1.0; + db=FILTER_6DB; +} diff --git a/servers/audio/effects/audio_effect_filter.h b/servers/audio/effects/audio_effect_filter.h new file mode 100644 index 0000000000..d0bc7a446a --- /dev/null +++ b/servers/audio/effects/audio_effect_filter.h @@ -0,0 +1,125 @@ +#ifndef AUDIOEFFECTFILTER_H +#define AUDIOEFFECTFILTER_H + +#include "servers/audio/audio_effect.h" +#include "servers/audio/audio_filter_sw.h" + +class AudioEffectFilter; + +class AudioEffectFilterInstance : public AudioEffectInstance { + GDCLASS(AudioEffectFilterInstance,AudioEffectInstance) +friend class AudioEffectFilter; + + Ref<AudioEffectFilter> base; + + AudioFilterSW filter; + AudioFilterSW::Processor filter_process[2][4]; + + template<int S> + void _process_filter(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + + AudioEffectFilterInstance(); +}; + + +class AudioEffectFilter : public AudioEffect { + GDCLASS(AudioEffectFilter,AudioEffect) +public: + + enum FilterDB { + FILTER_6DB, + FILTER_12DB, + FILTER_18DB, + FILTER_24DB, + }; + friend class AudioEffectFilterInstance; + + AudioFilterSW::Mode mode; + float cutoff; + float resonance; + float gain; + FilterDB db; + + +protected: + + + static void _bind_methods(); +public: + + void set_cutoff(float p_freq); + float get_cutoff() const; + + void set_resonance(float p_amount); + float get_resonance() const; + + void set_gain(float p_amount); + float get_gain() const; + + void set_db(FilterDB p_db); + FilterDB get_db() const; + + Ref<AudioEffectInstance> instance(); + + AudioEffectFilter(AudioFilterSW::Mode p_mode=AudioFilterSW::LOWPASS); +}; + +VARIANT_ENUM_CAST(AudioEffectFilter::FilterDB) + +class AudioEffectLowPassFilter : public AudioEffectFilter { + GDCLASS(AudioEffectLowPassFilter,AudioEffectFilter) +public: + + AudioEffectLowPassFilter() : AudioEffectFilter(AudioFilterSW::LOWPASS) {} +}; + +class AudioEffectHighPassFilter : public AudioEffectFilter { + GDCLASS(AudioEffectHighPassFilter,AudioEffectFilter) +public: + + AudioEffectHighPassFilter() : AudioEffectFilter(AudioFilterSW::HIGHPASS) {} +}; + +class AudioEffectBandPassFilter : public AudioEffectFilter { + GDCLASS(AudioEffectBandPassFilter,AudioEffectFilter) +public: + + AudioEffectBandPassFilter() : AudioEffectFilter(AudioFilterSW::BANDPASS) {} +}; + +class AudioEffectNotchFilter : public AudioEffectFilter { + GDCLASS(AudioEffectNotchFilter,AudioEffectFilter) +public: + + AudioEffectNotchFilter() : AudioEffectFilter(AudioFilterSW::NOTCH) {} +}; + +class AudioEffectBandLimitFilter : public AudioEffectFilter { + GDCLASS(AudioEffectBandLimitFilter,AudioEffectFilter) +public: + + AudioEffectBandLimitFilter() : AudioEffectFilter(AudioFilterSW::BANDLIMIT) {} +}; + + +class AudioEffectLowShelfFilter : public AudioEffectFilter { + GDCLASS(AudioEffectLowShelfFilter,AudioEffectFilter) +public: + + AudioEffectLowShelfFilter() : AudioEffectFilter(AudioFilterSW::LOWSHELF) {} +}; + + +class AudioEffectHighShelfFilter : public AudioEffectFilter { + GDCLASS(AudioEffectHighShelfFilter,AudioEffectFilter) +public: + + AudioEffectHighShelfFilter() : AudioEffectFilter(AudioFilterSW::HIGHSHELF) {} +}; + + + +#endif // AUDIOEFFECTFILTER_H diff --git a/servers/audio/effects/audio_effect_limiter.cpp b/servers/audio/effects/audio_effect_limiter.cpp new file mode 100644 index 0000000000..5cd02682ab --- /dev/null +++ b/servers/audio/effects/audio_effect_limiter.cpp @@ -0,0 +1,124 @@ +#include "audio_effect_limiter.h" + +void AudioEffectLimiterInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + float thresh = Math::db2linear(base->treshold); + float threshdb = base->treshold; + float ceiling = Math::db2linear(base->ceiling); + float ceildb = base->ceiling; + float makeup = Math::db2linear(ceildb - threshdb); + float makeupdb = ceildb - threshdb; + float sc = -base->soft_clip; + float scv = Math::db2linear(sc); + float sccomp = Math::db2linear(-sc); + float peakdb = ceildb + 25; + float peaklvl = Math::db2linear(peakdb); + float scratio = base->soft_clip_ratio; + float scmult = Math::abs((ceildb - sc) / (peakdb - sc)); + + for(int i=0;i<p_frame_count;i++) { + + float spl0 = p_src_frames[i].l; + float spl1 = p_src_frames[i].r; + spl0 = spl0 * makeup; + spl1 = spl1 * makeup; + float sign0 = (spl0 < 0.0 ? -1.0 : 1.0 ); + float sign1 = (spl1 < 0.0 ? -1.0 : 1.0 ); + float abs0 = Math::abs(spl0); + float abs1 = Math::abs(spl1); + float overdb0 = Math::linear2db(abs0) - ceildb; + float overdb1 = Math::linear2db(abs1) - ceildb; + + if (abs0 > scv) + { + spl0 = sign0 * (scv + Math::db2linear(overdb0 * scmult)); + } + if (abs1 > scv) + { + spl1 = sign1 * (scv + Math::db2linear(overdb1 * scmult)); + } + + spl0 = MIN(ceiling, Math::abs(spl0)) * (spl0 < 0.0 ? -1.0 : 1.0); + spl1 = MIN(ceiling, Math::abs(spl1)) * (spl1 < 0.0 ? -1.0 : 1.0); + + p_dst_frames[i].l = spl0; + p_dst_frames[i].r = spl1; + } + +} + + +Ref<AudioEffectInstance> AudioEffectLimiter::instance() { + Ref<AudioEffectLimiterInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectLimiter>(this); + + return ins; +} + + +void AudioEffectLimiter::set_treshold_db(float p_treshold) { + + treshold=p_treshold; +} + +float AudioEffectLimiter::get_treshold_db() const{ + + return treshold; +} + +void AudioEffectLimiter::set_ceiling_db(float p_ceiling){ + + ceiling=p_ceiling; +} +float AudioEffectLimiter::get_ceiling_db() const{ + + return ceiling; +} + +void AudioEffectLimiter::set_soft_clip_db(float p_soft_clip){ + + soft_clip=p_soft_clip; +} +float AudioEffectLimiter::get_soft_clip_db() const{ + + return soft_clip; +} + +void AudioEffectLimiter::set_soft_clip_ratio(float p_soft_clip){ + + soft_clip_ratio=p_soft_clip; +} +float AudioEffectLimiter::get_soft_clip_ratio() const{ + + return soft_clip; +} + + +void AudioEffectLimiter::_bind_methods() { + + ClassDB::bind_method(_MD("set_ceiling_db","ceiling"),&AudioEffectLimiter::set_ceiling_db); + ClassDB::bind_method(_MD("get_ceiling_db"),&AudioEffectLimiter::get_ceiling_db); + + ClassDB::bind_method(_MD("set_treshold_db","treshold"),&AudioEffectLimiter::set_treshold_db); + ClassDB::bind_method(_MD("get_treshold_db"),&AudioEffectLimiter::get_treshold_db); + + ClassDB::bind_method(_MD("set_soft_clip_db","soft_clip"),&AudioEffectLimiter::set_soft_clip_db); + ClassDB::bind_method(_MD("get_soft_clip_db"),&AudioEffectLimiter::get_soft_clip_db); + + ClassDB::bind_method(_MD("set_soft_clip_ratio","soft_clip"),&AudioEffectLimiter::set_soft_clip_ratio); + ClassDB::bind_method(_MD("get_soft_clip_ratio"),&AudioEffectLimiter::get_soft_clip_ratio); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"ceiling_db",PROPERTY_HINT_RANGE,"-20,-0.1,0.1"),_SCS("set_ceiling_db"),_SCS("get_ceiling_db")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"treshold_db",PROPERTY_HINT_RANGE,"-30,0,0.1"),_SCS("set_treshold_db"),_SCS("get_treshold_db")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"soft_clip_db",PROPERTY_HINT_RANGE,"0,6,0.1"),_SCS("set_soft_clip_db"),_SCS("get_soft_clip_db")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"soft_clip_ratio",PROPERTY_HINT_RANGE,"3,20,0.1"),_SCS("set_soft_clip_ratio"),_SCS("get_soft_clip_ratio")); +} + +AudioEffectLimiter::AudioEffectLimiter() +{ + treshold=0; + ceiling=-0.1; + soft_clip=2; + soft_clip_ratio=10; +} diff --git a/servers/audio/effects/audio_effect_limiter.h b/servers/audio/effects/audio_effect_limiter.h new file mode 100644 index 0000000000..b0d7321205 --- /dev/null +++ b/servers/audio/effects/audio_effect_limiter.h @@ -0,0 +1,58 @@ +#ifndef AUDIO_EFFECT_LIMITER_H +#define AUDIO_EFFECT_LIMITER_H + + +#include "servers/audio/audio_effect.h" + +class AudioEffectLimiter; + +class AudioEffectLimiterInstance : public AudioEffectInstance { + GDCLASS(AudioEffectLimiterInstance,AudioEffectInstance) +friend class AudioEffectLimiter; + Ref<AudioEffectLimiter> base; + + float mix_volume_db; +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectLimiter : public AudioEffect { + GDCLASS(AudioEffectLimiter,AudioEffect) + +friend class AudioEffectLimiterInstance; + float treshold; + float ceiling; + float soft_clip; + float soft_clip_ratio; + +protected: + + static void _bind_methods(); +public: + + + void set_treshold_db(float p_treshold); + float get_treshold_db() const; + + void set_ceiling_db(float p_ceiling); + float get_ceiling_db() const; + + void set_soft_clip_db(float p_soft_clip); + float get_soft_clip_db() const; + + void set_soft_clip_ratio(float p_soft_clip); + float get_soft_clip_ratio() const; + + + Ref<AudioEffectInstance> instance(); + void set_volume_db(float p_volume); + float get_volume_db() const; + + AudioEffectLimiter(); +}; + + +#endif // AUDIO_EFFECT_LIMITER_H diff --git a/servers/audio/effects/audio_effect_panner.cpp b/servers/audio/effects/audio_effect_panner.cpp new file mode 100644 index 0000000000..e296b0d998 --- /dev/null +++ b/servers/audio/effects/audio_effect_panner.cpp @@ -0,0 +1,47 @@ +#include "audio_effect_panner.h" + + +void AudioEffectPannerInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + + float lvol = CLAMP( 1.0 - base->pan, 0, 1); + float rvol = CLAMP( 1.0 + base->pan, 0, 1); + + for(int i=0;i<p_frame_count;i++) { + + p_dst_frames[i].l = p_src_frames[i].l * lvol + p_src_frames[i].r * (1.0 - rvol); + p_dst_frames[i].r = p_src_frames[i].r * rvol + p_src_frames[i].l * (1.0 - lvol); + + } + +} + + +Ref<AudioEffectInstance> AudioEffectPanner::instance() { + Ref<AudioEffectPannerInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectPanner>(this); + return ins; +} + +void AudioEffectPanner::set_pan(float p_cpanume) { + pan=p_cpanume; +} + +float AudioEffectPanner::get_pan() const { + + return pan; +} + +void AudioEffectPanner::_bind_methods() { + + ClassDB::bind_method(_MD("set_pan","cpanume"),&AudioEffectPanner::set_pan); + ClassDB::bind_method(_MD("get_pan"),&AudioEffectPanner::get_pan); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_pan"),_SCS("get_pan")); +} + +AudioEffectPanner::AudioEffectPanner() +{ + pan=0; +} diff --git a/servers/audio/effects/audio_effect_panner.h b/servers/audio/effects/audio_effect_panner.h new file mode 100644 index 0000000000..bc1bb00815 --- /dev/null +++ b/servers/audio/effects/audio_effect_panner.h @@ -0,0 +1,40 @@ +#ifndef AUDIOEFFECTPANNER_H +#define AUDIOEFFECTPANNER_H + +#include "servers/audio/audio_effect.h" + +class AudioEffectPanner; + +class AudioEffectPannerInstance : public AudioEffectInstance { + GDCLASS(AudioEffectPannerInstance,AudioEffectInstance) +friend class AudioEffectPanner; + Ref<AudioEffectPanner> base; + +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectPanner : public AudioEffect { + GDCLASS(AudioEffectPanner,AudioEffect) + +friend class AudioEffectPannerInstance; + float pan; + +protected: + + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + void set_pan(float p_volume); + float get_pan() const; + + AudioEffectPanner(); +}; + + +#endif // AUDIOEFFECTPANNER_H diff --git a/servers/audio/effects/audio_effect_phaser.cpp b/servers/audio/effects/audio_effect_phaser.cpp new file mode 100644 index 0000000000..bfce608603 --- /dev/null +++ b/servers/audio/effects/audio_effect_phaser.cpp @@ -0,0 +1,148 @@ +#include "audio_effect_phaser.h" +#include "servers/audio_server.h" +#include "math_funcs.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 * Math_PI * (base->rate / sampling_rate); + + for(int i=0;i<p_frame_count;i++) { + + phase += increment; + + while ( phase >= Math_PI * 2.f ) { + phase -= Math_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<AudioEffectInstance> AudioEffectPhaser::instance() { + Ref<AudioEffectPhaserInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectPhaser>(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 0000000000..53a8ab8bd8 --- /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<AudioEffectPhaser> 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<AudioEffectInstance> 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/audio/effects/audio_effect_pitch_shift.cpp b/servers/audio/effects/audio_effect_pitch_shift.cpp new file mode 100644 index 0000000000..49a14cda04 --- /dev/null +++ b/servers/audio/effects/audio_effect_pitch_shift.cpp @@ -0,0 +1,298 @@ +#include "audio_effect_pitch_shift.h" +#include "servers/audio_server.h" +#include "math_funcs.h" +/**************************************************************************** +* +* NAME: smbPitchShift.cpp +* VERSION: 1.2 +* HOME URL: http://blogs.zynaptiq.com/bernsee +* KNOWN BUGS: none +* +* SYNOPSIS: Routine for doing pitch shifting while maintaining +* duration using the Short Time Fourier Transform. +* +* DESCRIPTION: The routine takes a pitchShift factor value which is between 0.5 +* (one octave down) and 2. (one octave up). A value of exactly 1 does not change +* the pitch. numSampsToProcess tells the routine how many samples in indata[0... +* numSampsToProcess-1] should be pitch shifted and moved to outdata[0 ... +* numSampsToProcess-1]. The two buffers can be identical (ie. it can process the +* data in-place). fftFrameSize defines the FFT frame size used for the +* processing. Typical values are 1024, 2048 and 4096. It may be any value <= +* MAX_FRAME_LENGTH but it MUST be a power of 2. osamp is the STFT +* oversampling factor which also determines the overlap between adjacent STFT +* frames. It should at least be 4 for moderate scaling ratios. A value of 32 is +* recommended for best quality. sampleRate takes the sample rate for the signal +* in unit Hz, ie. 44100 for 44.1 kHz audio. The data passed to the routine in +* indata[] should be in the range [-1.0, 1.0), which is also the output range +* for the data, make sure you scale the data accordingly (for 16bit signed integers +* you would have to divide (and multiply) by 32768). +* +* COPYRIGHT 1999-2015 Stephan M. Bernsee <s.bernsee [AT] zynaptiq [DOT] com> +* +* The Wide Open License (WOL) +* +* Permission to use, copy, modify, distribute and sell this software and its +* documentation for any purpose is hereby granted without fee, provided that +* the above copyright notice and this license appear in all source copies. +* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF +* ANY KIND. See http://www.dspguru.com/wol.htm for more information. +* +*****************************************************************************/ + + +void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata,int stride) { + + + /* + Routine smbPitchShift(). See top of file for explanation + Purpose: doing pitch shifting while maintaining duration using the Short + Time Fourier Transform. + Author: (c)1999-2015 Stephan M. Bernsee <s.bernsee [AT] zynaptiq [DOT] com> + */ + + double magn, phase, tmp, window, real, imag; + double freqPerBin, expct; + long i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2; + + /* set up some handy variables */ + fftFrameSize2 = fftFrameSize/2; + stepSize = fftFrameSize/osamp; + freqPerBin = sampleRate/(double)fftFrameSize; + expct = 2.*Math_PI*(double)stepSize/(double)fftFrameSize; + inFifoLatency = fftFrameSize-stepSize; + if (gRover == 0) gRover = inFifoLatency; + + /* initialize our static arrays */ + + /* main processing loop */ + for (i = 0; i < numSampsToProcess; i++){ + + /* As long as we have not yet collected enough data just read in */ + gInFIFO[gRover] = indata[i*stride]; + outdata[i*stride] = gOutFIFO[gRover-inFifoLatency]; + gRover++; + + /* now we have enough data for processing */ + if (gRover >= fftFrameSize) { + gRover = inFifoLatency; + + /* do windowing and re,im interleave */ + for (k = 0; k < fftFrameSize;k++) { + window = -.5*cos(2.*Math_PI*(double)k/(double)fftFrameSize)+.5; + gFFTworksp[2*k] = gInFIFO[k] * window; + gFFTworksp[2*k+1] = 0.; + } + + + /* ***************** ANALYSIS ******************* */ + /* do transform */ + smbFft(gFFTworksp, fftFrameSize, -1); + + /* this is the analysis step */ + for (k = 0; k <= fftFrameSize2; k++) { + + /* de-interlace FFT buffer */ + real = gFFTworksp[2*k]; + imag = gFFTworksp[2*k+1]; + + /* compute magnitude and phase */ + magn = 2.*sqrt(real*real + imag*imag); + phase = atan2(imag,real); + + /* compute phase difference */ + tmp = phase - gLastPhase[k]; + gLastPhase[k] = phase; + + /* subtract expected phase difference */ + tmp -= (double)k*expct; + + /* map delta phase into +/- Pi interval */ + qpd = tmp/Math_PI; + if (qpd >= 0) qpd += qpd&1; + else qpd -= qpd&1; + tmp -= Math_PI*(double)qpd; + + /* get deviation from bin frequency from the +/- Pi interval */ + tmp = osamp*tmp/(2.*Math_PI); + + /* compute the k-th partials' true frequency */ + tmp = (double)k*freqPerBin + tmp*freqPerBin; + + /* store magnitude and true frequency in analysis arrays */ + gAnaMagn[k] = magn; + gAnaFreq[k] = tmp; + + } + + /* ***************** PROCESSING ******************* */ + /* this does the actual pitch shifting */ + memset(gSynMagn, 0, fftFrameSize*sizeof(float)); + memset(gSynFreq, 0, fftFrameSize*sizeof(float)); + for (k = 0; k <= fftFrameSize2; k++) { + index = k*pitchShift; + if (index <= fftFrameSize2) { + gSynMagn[index] += gAnaMagn[k]; + gSynFreq[index] = gAnaFreq[k] * pitchShift; + } + } + + /* ***************** SYNTHESIS ******************* */ + /* this is the synthesis step */ + for (k = 0; k <= fftFrameSize2; k++) { + + /* get magnitude and true frequency from synthesis arrays */ + magn = gSynMagn[k]; + tmp = gSynFreq[k]; + + /* subtract bin mid frequency */ + tmp -= (double)k*freqPerBin; + + /* get bin deviation from freq deviation */ + tmp /= freqPerBin; + + /* take osamp into account */ + tmp = 2.*Math_PI*tmp/osamp; + + /* add the overlap phase advance back in */ + tmp += (double)k*expct; + + /* accumulate delta phase to get bin phase */ + gSumPhase[k] += tmp; + phase = gSumPhase[k]; + + /* get real and imag part and re-interleave */ + gFFTworksp[2*k] = magn*cos(phase); + gFFTworksp[2*k+1] = magn*sin(phase); + } + + /* zero negative frequencies */ + for (k = fftFrameSize+2; k < 2*fftFrameSize; k++) gFFTworksp[k] = 0.; + + /* do inverse transform */ + smbFft(gFFTworksp, fftFrameSize, 1); + + /* do windowing and add to output accumulator */ + for(k=0; k < fftFrameSize; k++) { + window = -.5*cos(2.*Math_PI*(double)k/(double)fftFrameSize)+.5; + gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp); + } + for (k = 0; k < stepSize; k++) gOutFIFO[k] = gOutputAccum[k]; + + /* shift accumulator */ + memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(float)); + + /* move input FIFO */ + for (k = 0; k < inFifoLatency; k++) gInFIFO[k] = gInFIFO[k+stepSize]; + } + } + + + +} + + +void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign) +/* + FFT routine, (C)1996 S.M.Bernsee. Sign = -1 is FFT, 1 is iFFT (inverse) + Fills fftBuffer[0...2*fftFrameSize-1] with the Fourier transform of the + time domain data in fftBuffer[0...2*fftFrameSize-1]. The FFT array takes + and returns the cosine and sine parts in an interleaved manner, ie. + fftBuffer[0] = cosPart[0], fftBuffer[1] = sinPart[0], asf. fftFrameSize + must be a power of 2. It expects a complex input signal (see footnote 2), + ie. when working with 'common' audio signals our input signal has to be + passed as {in[0],0.,in[1],0.,in[2],0.,...} asf. In that case, the transform + of the frequencies of interest is in fftBuffer[0...fftFrameSize]. +*/ +{ + float wr, wi, arg, *p1, *p2, temp; + float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i; + long i, bitm, j, le, le2, k; + + for (i = 2; i < 2*fftFrameSize-2; i += 2) { + for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1) { + if (i & bitm) j++; + j <<= 1; + } + if (i < j) { + p1 = fftBuffer+i; p2 = fftBuffer+j; + temp = *p1; *(p1++) = *p2; + *(p2++) = temp; temp = *p1; + *p1 = *p2; *p2 = temp; + } + } + for (k = 0, le = 2; k < (long)(log(fftFrameSize)/log(2.)+.5); k++) { + le <<= 1; + le2 = le>>1; + ur = 1.0; + ui = 0.0; + arg = Math_PI / (le2>>1); + wr = cos(arg); + wi = sign*sin(arg); + for (j = 0; j < le2; j += 2) { + p1r = fftBuffer+j; p1i = p1r+1; + p2r = p1r+le2; p2i = p2r+1; + for (i = j; i < 2*fftFrameSize; i += le) { + tr = *p2r * ur - *p2i * ui; + ti = *p2r * ui + *p2i * ur; + *p2r = *p1r - tr; *p2i = *p1i - ti; + *p1r += tr; *p1i += ti; + p1r += le; p1i += le; + p2r += le; p2i += le; + } + tr = ur*wr - ui*wi; + ui = ur*wi + ui*wr; + ur = tr; + } + } +} + + +void AudioEffectPitchShiftInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + float sample_rate = AudioServer::get_singleton()->get_mix_rate(); + + float *in_l = (float*)p_src_frames; + float *in_r = in_l + 1; + + float *out_l = (float*)p_dst_frames; + float *out_r = out_l + 1; + + shift_l.PitchShift(base->pitch_scale,p_frame_count,2048,4,sample_rate,in_l,out_l,2); + shift_r.PitchShift(base->pitch_scale,p_frame_count,2048,4,sample_rate,in_r,out_r,2); + +} + + +Ref<AudioEffectInstance> AudioEffectPitchShift::instance() { + Ref<AudioEffectPitchShiftInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectPitchShift>(this); + + + return ins; +} + +void AudioEffectPitchShift::set_pitch_scale(float p_adjust) { + + pitch_scale=p_adjust; +} + +float AudioEffectPitchShift::get_pitch_scale() const { + + return pitch_scale; +} + + +void AudioEffectPitchShift::_bind_methods() { + + ClassDB::bind_method(_MD("set_pitch_scale","rate"),&AudioEffectPitchShift::set_pitch_scale); + ClassDB::bind_method(_MD("get_pitch_scale"),&AudioEffectPitchShift::get_pitch_scale); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"pitch_scale",PROPERTY_HINT_RANGE,"0.01,16,0.01"),_SCS("set_pitch_scale"),_SCS("get_pitch_scale")); + +} + +AudioEffectPitchShift::AudioEffectPitchShift() { + pitch_scale=1.0; + +} diff --git a/servers/audio/effects/audio_effect_pitch_shift.h b/servers/audio/effects/audio_effect_pitch_shift.h new file mode 100644 index 0000000000..d1343a0745 --- /dev/null +++ b/servers/audio/effects/audio_effect_pitch_shift.h @@ -0,0 +1,89 @@ +#ifndef AUDIO_EFFECT_PITCH_SHIFT_H +#define AUDIO_EFFECT_PITCH_SHIFT_H + + +#include "servers/audio/audio_effect.h" + +class SMBPitchShift { + + enum { + MAX_FRAME_LENGTH=8192 + }; + + float gInFIFO[MAX_FRAME_LENGTH]; + float gOutFIFO[MAX_FRAME_LENGTH]; + float gFFTworksp[2*MAX_FRAME_LENGTH]; + float gLastPhase[MAX_FRAME_LENGTH/2+1]; + float gSumPhase[MAX_FRAME_LENGTH/2+1]; + float gOutputAccum[2*MAX_FRAME_LENGTH]; + float gAnaFreq[MAX_FRAME_LENGTH]; + float gAnaMagn[MAX_FRAME_LENGTH]; + float gSynFreq[MAX_FRAME_LENGTH]; + float gSynMagn[MAX_FRAME_LENGTH]; + long gRover; + + void smbFft(float *fftBuffer, long fftFrameSize, long sign); +public: + void PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata, int stride); + + SMBPitchShift() { + gRover=0; + memset(gInFIFO, 0, MAX_FRAME_LENGTH*sizeof(float)); + memset(gOutFIFO, 0, MAX_FRAME_LENGTH*sizeof(float)); + memset(gFFTworksp, 0, 2*MAX_FRAME_LENGTH*sizeof(float)); + memset(gLastPhase, 0, (MAX_FRAME_LENGTH/2+1)*sizeof(float)); + memset(gSumPhase, 0, (MAX_FRAME_LENGTH/2+1)*sizeof(float)); + memset(gOutputAccum, 0, 2*MAX_FRAME_LENGTH*sizeof(float)); + memset(gAnaFreq, 0, MAX_FRAME_LENGTH*sizeof(float)); + memset(gAnaMagn, 0, MAX_FRAME_LENGTH*sizeof(float)); + } + + +}; + + +class AudioEffectPitchShift; + +class AudioEffectPitchShiftInstance : public AudioEffectInstance { + GDCLASS(AudioEffectPitchShiftInstance,AudioEffectInstance) +friend class AudioEffectPitchShift; + Ref<AudioEffectPitchShift> base; + + SMBPitchShift shift_l; + SMBPitchShift shift_r; + + +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + +}; + + +class AudioEffectPitchShift : public AudioEffect { + GDCLASS(AudioEffectPitchShift,AudioEffect) + +friend class AudioEffectPitchShiftInstance; + + float pitch_scale; + int window_size; + float wet; + float dry; + bool filter; + +protected: + + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + + void set_pitch_scale(float p_adjust); + float get_pitch_scale() const; + + AudioEffectPitchShift(); +}; + + +#endif // AUDIO_EFFECT_PITCH_SHIFT_H diff --git a/servers/audio/effects/audio_effect_reverb.cpp b/servers/audio/effects/audio_effect_reverb.cpp new file mode 100644 index 0000000000..749814fd76 --- /dev/null +++ b/servers/audio/effects/audio_effect_reverb.cpp @@ -0,0 +1,182 @@ +#include "audio_effect_reverb.h" +#include "servers/audio_server.h" +void AudioEffectReverbInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + for(int i=0;i<2;i++) { + Reverb &r=reverb[i]; + + r.set_predelay( base->predelay); + r.set_predelay_feedback( base->predelay_fb ); + r.set_highpass( base->hpf ); + r.set_room_size( base->room_size ); + r.set_damp( base->damping ); + r.set_extra_spread( base->spread ); + r.set_wet( base->wet ); + r.set_dry( base->dry ); + } + + int todo = p_frame_count; + int offset=0; + + while(todo) { + + int to_mix = MIN(todo,Reverb::INPUT_BUFFER_MAX_SIZE); + + for(int j=0;j<to_mix;j++) { + tmp_src[j]=p_src_frames[offset+j].l; + } + + reverb[0].process(tmp_src,tmp_dst,to_mix); + + for(int j=0;j<to_mix;j++) { + p_dst_frames[offset+j].l=tmp_dst[j]; + tmp_src[j]=p_src_frames[offset+j].r; + } + + reverb[1].process(tmp_src,tmp_dst,to_mix); + + for(int j=0;j<to_mix;j++) { + p_dst_frames[offset+j].r=tmp_dst[j]; + } + + offset+=to_mix; + todo-=to_mix; + } +} + +AudioEffectReverbInstance::AudioEffectReverbInstance() { + + reverb[0].set_mix_rate( AudioServer::get_singleton()->get_mix_rate() ); + reverb[0].set_extra_spread_base(0); + reverb[1].set_mix_rate( AudioServer::get_singleton()->get_mix_rate() ); + reverb[1].set_extra_spread_base(0.000521); //for stereo effect + +} + +Ref<AudioEffectInstance> AudioEffectReverb::instance() { + Ref<AudioEffectReverbInstance> ins; + ins.instance(); + ins->base=Ref<AudioEffectReverb>(this); + return ins; +} + +void AudioEffectReverb::set_predelay_msec(float p_msec) { + + predelay=p_msec; +} + +void AudioEffectReverb::set_predelay_feedback(float p_feedback){ + + predelay_fb=p_feedback; +} +void AudioEffectReverb::set_room_size(float p_size){ + + room_size=p_size; +} +void AudioEffectReverb::set_damping(float p_damping){ + + damping=p_damping; +} +void AudioEffectReverb::set_spread(float p_spread){ + + spread=p_spread; +} + +void AudioEffectReverb::set_dry(float p_dry){ + + dry=p_dry; +} +void AudioEffectReverb::set_wet(float p_wet){ + + wet=p_wet; +} +void AudioEffectReverb::set_hpf(float p_hpf) { + + hpf=p_hpf; +} + +float AudioEffectReverb::get_predelay_msec() const { + + return predelay; +} +float AudioEffectReverb::get_predelay_feedback() const { + + return predelay_fb; +} +float AudioEffectReverb::get_room_size() const { + + return room_size; +} +float AudioEffectReverb::get_damping() const { + + return damping; +} +float AudioEffectReverb::get_spread() const { + + return spread; +} +float AudioEffectReverb::get_dry() const { + + return dry; +} +float AudioEffectReverb::get_wet() const { + + return wet; +} +float AudioEffectReverb::get_hpf() const { + + return hpf; +} + + +void AudioEffectReverb::_bind_methods() { + + + ClassDB::bind_method(_MD("set_predelay_msec","msec"),&AudioEffectReverb::set_predelay_msec); + ClassDB::bind_method(_MD("get_predelay_msec"),&AudioEffectReverb::get_predelay_msec); + + ClassDB::bind_method(_MD("set_predelay_feedback","feedback"),&AudioEffectReverb::set_predelay_feedback); + ClassDB::bind_method(_MD("get_predelay_feedback"),&AudioEffectReverb::get_predelay_feedback); + + ClassDB::bind_method(_MD("set_room_size","size"),&AudioEffectReverb::set_room_size); + ClassDB::bind_method(_MD("get_room_size"),&AudioEffectReverb::get_room_size); + + ClassDB::bind_method(_MD("set_damping","amount"),&AudioEffectReverb::set_damping); + ClassDB::bind_method(_MD("get_damping"),&AudioEffectReverb::get_damping); + + ClassDB::bind_method(_MD("set_spread","amount"),&AudioEffectReverb::set_spread); + ClassDB::bind_method(_MD("get_spread"),&AudioEffectReverb::get_spread); + + ClassDB::bind_method(_MD("set_dry","amount"),&AudioEffectReverb::set_dry); + ClassDB::bind_method(_MD("get_dry"),&AudioEffectReverb::get_dry); + + ClassDB::bind_method(_MD("set_wet","amount"),&AudioEffectReverb::set_wet); + ClassDB::bind_method(_MD("get_wet"),&AudioEffectReverb::get_wet); + + ClassDB::bind_method(_MD("set_hpf","amount"),&AudioEffectReverb::set_hpf); + ClassDB::bind_method(_MD("get_hpf"),&AudioEffectReverb::get_hpf); + + + ADD_GROUP("Predelay","predelay_"); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"predelay_msec",PROPERTY_HINT_RANGE,"20,500,1"),_SCS("set_predelay_msec"),_SCS("get_predelay_msec")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"predelay_feedback",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_predelay_msec"),_SCS("get_predelay_msec")); + ADD_GROUP("",""); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"room_size",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_room_size"),_SCS("get_room_size")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"damping",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_damping"),_SCS("get_damping")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"spread",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_spread"),_SCS("get_spread")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"hipass",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_hpf"),_SCS("get_hpf")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"dry",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_dry"),_SCS("get_dry")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"wet",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_wet"),_SCS("get_wet")); +} + +AudioEffectReverb::AudioEffectReverb() { + predelay=150; + predelay_fb=0.4; + hpf=0; + room_size=0.8; + damping=0.5; + spread=1.0; + dry=1.0; + wet=0.5; + +} diff --git a/servers/audio/effects/audio_effect_reverb.h b/servers/audio/effects/audio_effect_reverb.h new file mode 100644 index 0000000000..e05ffe422f --- /dev/null +++ b/servers/audio/effects/audio_effect_reverb.h @@ -0,0 +1,76 @@ +#ifndef AUDIOEFFECTREVERB_H +#define AUDIOEFFECTREVERB_H + + +#include "servers/audio/audio_effect.h" +#include "servers/audio/effects/reverb.h" + +class AudioEffectReverb; + +class AudioEffectReverbInstance : public AudioEffectInstance { + GDCLASS(AudioEffectReverbInstance,AudioEffectInstance) + + Ref<AudioEffectReverb> base; + + float tmp_src[Reverb::INPUT_BUFFER_MAX_SIZE]; + float tmp_dst[Reverb::INPUT_BUFFER_MAX_SIZE]; + +friend class AudioEffectReverb; + + Reverb reverb[2]; + + +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + AudioEffectReverbInstance(); +}; + + +class AudioEffectReverb : public AudioEffect { + GDCLASS(AudioEffectReverb,AudioEffect) + +friend class AudioEffectReverbInstance; + + float predelay; + float predelay_fb; + float hpf; + float room_size; + float damping; + float spread; + float dry; + float wet; + +protected: + + static void _bind_methods(); +public: + + + void set_predelay_msec(float p_msec); + void set_predelay_feedback(float p_feedback); + void set_room_size(float p_size); + void set_damping(float p_damping); + void set_spread(float p_spread); + void set_dry(float p_dry); + void set_wet(float p_wet); + void set_hpf(float p_hpf); + + float get_predelay_msec() const; + float get_predelay_feedback() const; + float get_room_size() const; + float get_damping() const; + float get_spread() const; + float get_dry() const; + float get_wet() const; + float get_hpf() const; + + Ref<AudioEffectInstance> instance(); + void set_volume_db(float p_volume); + float get_volume_db() const; + + AudioEffectReverb(); +}; + + +#endif // AUDIOEFFECTREVERB_H diff --git a/servers/audio/effects/audio_effect_stereo_enhance.cpp b/servers/audio/effects/audio_effect_stereo_enhance.cpp new file mode 100644 index 0000000000..c5968aa772 --- /dev/null +++ b/servers/audio/effects/audio_effect_stereo_enhance.cpp @@ -0,0 +1,135 @@ +#include "audio_effect_stereo_enhance.h" +#include "servers/audio_server.h" +void AudioEffectStereoEnhanceInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) { + + + float intensity=base->pan_pullout; + bool surround_mode=base->surround>0; + float surround_amount=base->surround; + unsigned int delay_frames=(base->time_pullout/1000.0)*AudioServer::get_singleton()->get_mix_rate(); + + for (int i=0;i<p_frame_count;i++) { + + float l=p_src_frames[i].l; + float r=p_src_frames[i].r; + + float center=(l+r)/2.0f; + + l=( center+(l-center)*intensity ); + r=( center+(r-center)*intensity ); + + if (surround_mode) { + + float val=(l+r)/2.0; + + delay_ringbuff[ringbuff_pos&ringbuff_mask]=val; + + float out=delay_ringbuff[(ringbuff_pos-delay_frames)&ringbuff_mask]*surround_amount; + + l+=out; + r+=-out; + } else { + + float val=r; + + delay_ringbuff[ringbuff_pos&ringbuff_mask]=val; + + //r is delayed + r=delay_ringbuff[(ringbuff_pos-delay_frames)&ringbuff_mask];; + + + } + + p_dst_frames[i].l=l; + p_dst_frames[i].r=r; + ringbuff_pos++; + + } + +} + + +AudioEffectStereoEnhanceInstance::~AudioEffectStereoEnhanceInstance() { + + memdelete_arr(delay_ringbuff); +} + +Ref<AudioEffectInstance> AudioEffectStereoEnhance::instance() { + Ref<AudioEffectStereoEnhanceInstance> ins; + ins.instance(); + + ins->base=Ref<AudioEffectStereoEnhance>(this); + + + float ring_buffer_max_size=AudioEffectStereoEnhanceInstance::MAX_DELAY_MS+2; + ring_buffer_max_size/=1000.0;//convert to seconds + ring_buffer_max_size*=AudioServer::get_singleton()->get_mix_rate(); + + int ringbuff_size=(int)ring_buffer_max_size; + + int bits=0; + + while(ringbuff_size>0) { + bits++; + ringbuff_size/=2; + } + + ringbuff_size=1<<bits; + ins->ringbuff_mask=ringbuff_size-1; + ins->ringbuff_pos=0; + + ins->delay_ringbuff = memnew_arr(float,ringbuff_size ); + + return ins; +} + +void AudioEffectStereoEnhance::set_pan_pullout(float p_amount) { + + pan_pullout=p_amount; +} + +float AudioEffectStereoEnhance::get_pan_pullout() const { + + return pan_pullout; +} + +void AudioEffectStereoEnhance::set_time_pullout(float p_amount) { + + time_pullout=p_amount; +} +float AudioEffectStereoEnhance::get_time_pullout() const { + + return time_pullout; +} + +void AudioEffectStereoEnhance::set_surround(float p_amount) { + + surround=p_amount; +} +float AudioEffectStereoEnhance::get_surround() const { + + return surround; +} + +void AudioEffectStereoEnhance::_bind_methods() { + + ClassDB::bind_method(_MD("set_pan_pullout","amount"),&AudioEffectStereoEnhance::set_pan_pullout); + ClassDB::bind_method(_MD("get_pan_pullout"),&AudioEffectStereoEnhance::get_pan_pullout); + + ClassDB::bind_method(_MD("set_time_pullout","amount"),&AudioEffectStereoEnhance::set_time_pullout); + ClassDB::bind_method(_MD("get_time_pullout"),&AudioEffectStereoEnhance::get_time_pullout); + + ClassDB::bind_method(_MD("set_surround","amount"),&AudioEffectStereoEnhance::set_surround); + ClassDB::bind_method(_MD("get_surround"),&AudioEffectStereoEnhance::get_surround); + + ADD_PROPERTY(PropertyInfo(Variant::REAL,"pan_pullout",PROPERTY_HINT_RANGE,"0,4,0.01"),_SCS("set_pan_pullout"),_SCS("get_pan_pullout")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"time_pullout_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),_SCS("set_time_pullout"),_SCS("get_time_pullout")); + ADD_PROPERTY(PropertyInfo(Variant::REAL,"surround",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_surround"),_SCS("get_surround")); +} + +AudioEffectStereoEnhance::AudioEffectStereoEnhance() +{ + pan_pullout=1; + time_pullout=0; + surround=0; +} diff --git a/servers/audio/effects/audio_effect_stereo_enhance.h b/servers/audio/effects/audio_effect_stereo_enhance.h new file mode 100644 index 0000000000..06762acbf3 --- /dev/null +++ b/servers/audio/effects/audio_effect_stereo_enhance.h @@ -0,0 +1,62 @@ +#ifndef AUDIOEFFECTSTEREOENHANCE_H +#define AUDIOEFFECTSTEREOENHANCE_H + + +#include "servers/audio/audio_effect.h" + +class AudioEffectStereoEnhance; + +class AudioEffectStereoEnhanceInstance : public AudioEffectInstance { + GDCLASS(AudioEffectStereoEnhanceInstance,AudioEffectInstance) +friend class AudioEffectStereoEnhance; + Ref<AudioEffectStereoEnhance> base; + + enum { + + MAX_DELAY_MS=50 + }; + + float *delay_ringbuff; + unsigned int ringbuff_pos; + unsigned int ringbuff_mask; + + +public: + + virtual void process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count); + + ~AudioEffectStereoEnhanceInstance(); +}; + + +class AudioEffectStereoEnhance : public AudioEffect { + GDCLASS(AudioEffectStereoEnhance,AudioEffect) + +friend class AudioEffectStereoEnhanceInstance; + float volume_db; + + float pan_pullout; + float time_pullout; + float surround; + +protected: + + static void _bind_methods(); +public: + + + Ref<AudioEffectInstance> instance(); + + void set_pan_pullout(float p_amount); + float get_pan_pullout() const; + + void set_time_pullout(float p_amount); + float get_time_pullout() const; + + void set_surround(float p_amount); + float get_surround() const; + + AudioEffectStereoEnhance(); +}; + +#endif // AUDIOEFFECTSTEREOENHANCE_H diff --git a/servers/audio/effects/eq.cpp b/servers/audio/effects/eq.cpp new file mode 100644 index 0000000000..a6499a66b4 --- /dev/null +++ b/servers/audio/effects/eq.cpp @@ -0,0 +1,219 @@ +// +// C++ Interface: eq +// +// Description: +// +// +// Author: reduzio@gmail.com (C) 2006 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#include "eq.h" +#include <math.h> +#include "error_macros.h" +#include "math_funcs.h" + +#define POW2(v) ((v)*(v)) + +/* Helper */ + static int solve_quadratic(double a,double b,double c,double *r1, double *r2) { +//solves quadractic and returns number of roots + + double base=2*a; + if (base == 0.0f) + return 0; + + double squared=b*b-4*a*c; + if (squared<0.0) + return 0; + + squared=sqrt(squared); + + *r1=(-b+squared)/base; + *r2=(-b-squared)/base; + + if (*r1==*r2) + return 1; + else + return 2; + } + +EQ::BandProcess::BandProcess() { + + c1=c2=c3=history.a1=history.a2=history.a3=0; + history.b1=history.b2=history.b3=0; +} + +void EQ::recalculate_band_coefficients() { + +#define BAND_LOG( m_f ) ( log((m_f)) / log(2) ) + + for (int i=0;i<band.size();i++) { + + double octave_size; + + double frq=band[i].freq; + + if (i==0) { + + octave_size=BAND_LOG(band[1].freq)-BAND_LOG(frq); + } else if (i==(band.size()-1)) { + + octave_size=BAND_LOG(frq)-BAND_LOG(band[i-1].freq); + } else { + + double next=BAND_LOG(band[i+1].freq)-BAND_LOG(frq); + double prev=BAND_LOG(frq)-BAND_LOG(band[i-1].freq); + octave_size=(next+prev)/2.0; + } + + + + double frq_l=round(frq/pow(2.0,octave_size/2.0)); + + + + double side_gain2=POW2(Math_SQRT12); + double th=2.0*Math_PI*frq/mix_rate; + double th_l=2.0*Math_PI*frq_l/mix_rate; + + double c2a=side_gain2 * POW2(cos(th)) + - 2.0 * side_gain2 * cos(th_l) * cos(th) + + side_gain2 + - POW2(sin(th_l)); + + double c2b=2.0 * side_gain2 * POW2(cos(th_l)) + + side_gain2 * POW2(cos(th)) + - 2.0 * side_gain2 * cos(th_l) * cos(th) + - side_gain2 + + POW2(sin(th_l)); + + double c2c=0.25 * side_gain2 * POW2(cos(th)) + - 0.5 * side_gain2 * cos(th_l) * cos(th) + + 0.25 * side_gain2 + - 0.25 * POW2(sin(th_l)); + + //printf("band %i, precoefs = %f,%f,%f\n",i,c2a,c2b,c2c); + + double r1,r2; //roots + int roots=solve_quadratic(c2a,c2b,c2c,&r1,&r2); + + ERR_CONTINUE( roots==0 ); + + band[i].c1=2.0 * ((0.5-r1)/2.0); + band[i].c2=2.0 * r1; + band[i].c3=2.0 * (0.5+r1) * cos(th); + //printf("band %i, coefs = %f,%f,%f\n",i,(float)bands[i].c1,(float)bands[i].c2,(float)bands[i].c3); + + } +} + +void EQ::set_preset_band_mode(Preset p_preset) { + + + band.clear(); + +#define PUSH_BANDS(m_bands) \ + for (int i=0;i<m_bands;i++) { \ + Band b; \ + b.freq=bands[i];\ + band.push_back(b);\ + } + + switch (p_preset) { + + case PRESET_6_BANDS: { + + static const double bands[] = { 32 , 100 , 320 , 1e3, 3200, 10e3 }; + PUSH_BANDS(6); + + } break; + + case PRESET_8_BANDS: { + + static const double bands[] = { 32,72,192,512,1200,3000,7500,16e3 }; + + PUSH_BANDS(8); + } break; + + case PRESET_10_BANDS: { + static const double bands[] = { 31.25, 62.5, 125 , 250 , 500 , 1e3, 2e3, 4e3, 8e3, 16e3 }; + + PUSH_BANDS(10); + + } break; + + case PRESET_21_BANDS: { + + static const double bands[] = { 22 , 32 , 44 , 63 , 90 , 125 , 175 , 250 , 350 , 500 , 700 , 1e3, 1400 , 2e3, 2800 , 4e3, 5600 , 8e3, 11e3, 16e3, 22e3 }; + PUSH_BANDS(21); + + } break; + + case PRESET_31_BANDS: { + + static const double bands[] = { 20, 25, 31.5, 40 , 50 , 63 , 80 , 100 , 125 , 160 , 200 , 250 , 315 , 400 , 500 , 630 , 800 , 1e3 , 1250 , 1600 , 2e3, 2500 , 3150 , 4e3, 5e3, 6300 , 8e3, 10e3, 12500 , 16e3, 20e3 }; + PUSH_BANDS(31); + } break; + + }; + + recalculate_band_coefficients(); +} + +int EQ::get_band_count() const { + + return band.size(); +} +float EQ::get_band_frequency(int p_band) { + + ERR_FAIL_INDEX_V(p_band,band.size(),0); + return band[p_band].freq; +} +void EQ::set_bands(const Vector<float>& p_bands) { + + band.resize(p_bands.size()); + for (int i=0;i<p_bands.size();i++) { + + band[i].freq=p_bands[i]; + } + + recalculate_band_coefficients(); + +} + +void EQ::set_mix_rate(float p_mix_rate) { + + mix_rate=p_mix_rate; + recalculate_band_coefficients(); +} + +EQ::BandProcess EQ::get_band_processor(int p_band) const { + + + EQ::BandProcess band_proc; + + ERR_FAIL_INDEX_V(p_band,band.size(),band_proc); + + band_proc.c1=band[p_band].c1; + band_proc.c2=band[p_band].c2; + band_proc.c3=band[p_band].c3; + + return band_proc; + + +} + + +EQ::EQ() +{ + mix_rate=44100; +} + + +EQ::~EQ() +{ +} + + diff --git a/servers/audio/effects/eq.h b/servers/audio/effects/eq.h new file mode 100644 index 0000000000..2c4668cd0b --- /dev/null +++ b/servers/audio/effects/eq.h @@ -0,0 +1,106 @@ +// +// C++ Interface: eq +// +// Description: +// +// +// Author: reduzio@gmail.com (C) 2006 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#ifndef EQ_FILTER_H +#define EQ_FILTER_H + + +#include "typedefs.h" +#include "vector.h" + + +/** +@author Juan Linietsky +*/ + +class EQ { +public: + + enum Preset { + + PRESET_6_BANDS, + PRESET_8_BANDS, + PRESET_10_BANDS, + PRESET_21_BANDS, + PRESET_31_BANDS + }; + + + + class BandProcess { + + friend class EQ; + float c1,c2,c3; + struct History { + float a1,a2,a3; + float b1,b2,b3; + + } history; + + public: + + inline void process_one(float & p_data); + + BandProcess(); + }; + +private: + struct Band { + + float freq; + float c1,c2,c3; + }; + + Vector<Band> band; + + float mix_rate; + + void recalculate_band_coefficients(); + +public: + + + void set_mix_rate(float p_mix_rate); + + int get_band_count() const; + void set_preset_band_mode(Preset p_preset); + void set_bands(const Vector<float>& p_bands); + BandProcess get_band_processor(int p_band) const; + float get_band_frequency(int p_band); + + EQ(); + ~EQ(); + +}; + + +/* Inline Function */ + +inline void EQ::BandProcess::process_one(float & p_data) { + + + history.a1=p_data; + + history.b1= c1 * ( history.a1 - history.a3 ) + + c3 * history.b2 + - c2 * history.b3; + + p_data = history.b1; + + history.a3=history.a2; + history.a2=history.a1; + history.b3=history.b2; + history.b2=history.b1; + +} + + +#endif diff --git a/servers/audio/effects/reverb.cpp b/servers/audio/effects/reverb.cpp new file mode 100644 index 0000000000..43ea0edb3a --- /dev/null +++ b/servers/audio/effects/reverb.cpp @@ -0,0 +1,364 @@ +// +// C++ Interface: reverb +// +// Description: +// +// +// Author: Juan Linietsky <reduzio@gmail.com>, (C) 2006 +// +// Copyright: See COPYING file that comes with this distribution +// +// + +#include "reverb.h" +#include <math.h> +#include "math_funcs.h" + + +const float Reverb::comb_tunings[MAX_COMBS]={ + //freeverb comb tunings + 0.025306122448979593, + 0.026938775510204082, + 0.028956916099773241, + 0.03074829931972789, + 0.032244897959183672, + 0.03380952380952381, + 0.035306122448979592, + 0.036666666666666667 +}; + +const float Reverb::allpass_tunings[MAX_ALLPASS]={ + //freeverb allpass tunings + 0.0051020408163265302, + 0.007732426303854875, + 0.01, + 0.012607709750566893 +}; + + + +void Reverb::process(float *p_src,float *p_dst,int p_frames) { + + if (p_frames>INPUT_BUFFER_MAX_SIZE) + p_frames=INPUT_BUFFER_MAX_SIZE; + + int predelay_frames=lrint((params.predelay/1000.0)*params.mix_rate); + if (predelay_frames<10) + predelay_frames=10; + if (predelay_frames>=echo_buffer_size) + predelay_frames=echo_buffer_size-1; + + for (int i=0;i<p_frames;i++) { + + if (echo_buffer_pos>=echo_buffer_size) + echo_buffer_pos=0; + + int read_pos=echo_buffer_pos-predelay_frames; + while (read_pos<0) + read_pos+=echo_buffer_size; + + float in=undenormalise(echo_buffer[read_pos]*params.predelay_fb+p_src[i]); + + echo_buffer[echo_buffer_pos]=in; + + input_buffer[i]=in; + + p_dst[i]=0; //take the chance and clear this + + echo_buffer_pos++; + } + + if (params.hpf>0) { + float hpaux=expf(-2.0*Math_PI*params.hpf*6000/params.mix_rate); + float hp_a1=(1.0+hpaux)/2.0; + float hp_a2=-(1.0+hpaux)/2.0; + float hp_b1=hpaux; + + for (int i=0;i<p_frames;i++) { + + float in=input_buffer[i]; + input_buffer[i]=in*hp_a1+hpf_h1*hp_a2+hpf_h2*hp_b1; + hpf_h2=input_buffer[i]; + hpf_h1=in; + } + } + + for (int i=0;i<MAX_COMBS;i++) { + + Comb &c=comb[i]; + + int size_limit=c.size-lrintf((float)c.extra_spread_frames*(1.0-params.extra_spread)); + for (int j=0;j<p_frames;j++) { + + if (c.pos>=size_limit) //reset this now just in case + c.pos=0; + + float out=undenormalise(c.buffer[c.pos]*c.feedback); + out=out*(1.0-c.damp)+c.damp_h*c.damp; //lowpass + c.damp_h=out; + c.buffer[c.pos]=input_buffer[j]+out; + p_dst[j]+=out; + c.pos++; + } + + } + + + static const float allpass_feedback=0.7; + /* this one works, but the other version is just nicer.... + int ap_size_limit[MAX_ALLPASS]; + + for (int i=0;i<MAX_ALLPASS;i++) { + + AllPass &a=allpass[i]; + ap_size_limit[i]=a.size-lrintf((float)a.extra_spread_frames*(1.0-params.extra_spread)); + } + + for (int i=0;i<p_frames;i++) { + + float sample=p_dst[i]; + float aux,in; + float AllPass*ap; + +#define PROCESS_ALLPASS(m_ap) \ + ap=&allpass[m_ap]; \ + if (ap->pos>=ap_size_limit[m_ap]) \ + ap->pos=0; \ + aux=undenormalise(ap->buffer[ap->pos]); \ + in=sample; \ + sample=-in+aux; \ + ap->pos++; + + + PROCESS_ALLPASS(0); + PROCESS_ALLPASS(1); + PROCESS_ALLPASS(2); + PROCESS_ALLPASS(3); + + p_dst[i]=sample; + } + */ + + for (int i=0;i<MAX_ALLPASS;i++) { + + AllPass &a=allpass[i]; + int size_limit=a.size-lrintf((float)a.extra_spread_frames*(1.0-params.extra_spread)); + + for (int j=0;j<p_frames;j++) { + + if (a.pos>=size_limit) + a.pos=0; + + float aux=a.buffer[a.pos]; + a.buffer[a.pos]=undenormalise(allpass_feedback*aux+p_dst[j]); + p_dst[j]=aux-allpass_feedback*a.buffer[a.pos]; + a.pos++; + + } + } + + static const float wet_scale=0.6; + + for (int i=0;i<p_frames;i++) { + + + p_dst[i]=p_dst[i]*params.wet*wet_scale+p_src[i]*params.dry; + } + +} + + +void Reverb::set_room_size(float p_size) { + + params.room_size=p_size; + update_parameters(); + +} +void Reverb::set_damp(float p_damp) { + + params.damp=p_damp; + update_parameters(); + +} +void Reverb::set_wet(float p_wet) { + + params.wet=p_wet; + +} + +void Reverb::set_dry(float p_dry) { + + params.dry=p_dry; + +} + +void Reverb::set_predelay(float p_predelay) { + + params.predelay=p_predelay; +} +void Reverb::set_predelay_feedback(float p_predelay_fb) { + + params.predelay_fb=p_predelay_fb; + +} + +void Reverb::set_highpass(float p_frq) { + + if (p_frq>1) + p_frq=1; + if (p_frq<0) + p_frq=0; + params.hpf=p_frq; +} + +void Reverb::set_extra_spread(float p_spread) { + + params.extra_spread=p_spread; + +} + + +void Reverb::set_mix_rate(float p_mix_rate) { + + params.mix_rate=p_mix_rate; + configure_buffers(); +} + +void Reverb::set_extra_spread_base(float p_sec) { + + params.extra_spread_base=p_sec; + configure_buffers(); +} + + +void Reverb::configure_buffers() { + + clear_buffers(); //clear if necesary + + for (int i=0;i<MAX_COMBS;i++) { + + Comb &c=comb[i]; + + + c.extra_spread_frames=lrint(params.extra_spread_base*params.mix_rate); + + int len=lrint(comb_tunings[i]*params.mix_rate)+c.extra_spread_frames; + if (len<5) + len=5; //may this happen? + + c.buffer = memnew_arr(float,len); + c.pos=0; + for (int j=0;j<len;j++) + c.buffer[j]=0; + c.size=len; + + } + + for (int i=0;i<MAX_ALLPASS;i++) { + + AllPass &a=allpass[i]; + + a.extra_spread_frames=lrint(params.extra_spread_base*params.mix_rate); + + int len=lrint(allpass_tunings[i]*params.mix_rate)+a.extra_spread_frames; + if (len<5) + len=5; //may this happen? + + a.buffer = memnew_arr(float,len); + a.pos=0; + for (int j=0;j<len;j++) + a.buffer[j]=0; + a.size=len; + } + + echo_buffer_size=(int)(((float)MAX_ECHO_MS/1000.0)*params.mix_rate+1.0); + echo_buffer = memnew_arr(float,echo_buffer_size); + for (int i=0;i<echo_buffer_size;i++) { + + echo_buffer[i]=0; + } + + echo_buffer_pos=0; +} + + +void Reverb::update_parameters() { + + //more freeverb derived constants + static const float room_scale = 0.28f; + static const float room_offset = 0.7f; + + for (int i=0;i<MAX_COMBS;i++) { + + Comb &c=comb[i]; + c.feedback=room_offset+params.room_size*room_scale; + if (c.feedback<room_offset) + c.feedback=room_offset; + else if (c.feedback>(room_offset+room_scale)) + c.feedback=(room_offset+room_scale); + + float auxdmp=params.damp/2.0+0.5; //only half the range (0.5 .. 1.0 is enough) + auxdmp*=auxdmp; + + c.damp=expf(-2.0*Math_PI*auxdmp*10000/params.mix_rate); // 0 .. 10khz + } + +} + +void Reverb::clear_buffers() { + + if (echo_buffer) + memdelete_arr(echo_buffer); + + for (int i=0;i<MAX_COMBS;i++) { + + if (comb[i].buffer) + memdelete_arr(comb[i].buffer); + + comb[i].buffer=0; + + } + + for (int i=0;i<MAX_ALLPASS;i++) { + + if (allpass[i].buffer) + memdelete_arr(allpass[i].buffer); + + allpass[i].buffer=0; + } + +} + +Reverb::Reverb() { + + params.room_size=0.8; + params.damp=0.5; + params.dry=1.0; + params.wet=0.0; + params.mix_rate=44100; + params.extra_spread_base=0; + params.extra_spread=1.0; + params.predelay=150; + params.predelay_fb=0.4; + params.hpf=0; + hpf_h1=0; + hpf_h2=0; + + + input_buffer=memnew_arr(float,INPUT_BUFFER_MAX_SIZE); + echo_buffer=0; + + configure_buffers(); + update_parameters(); + + +} + + +Reverb::~Reverb() { + + memdelete_arr(input_buffer); + clear_buffers(); +} + + diff --git a/servers/audio/effects/reverb.h b/servers/audio/effects/reverb.h new file mode 100644 index 0000000000..2c82be9156 --- /dev/null +++ b/servers/audio/effects/reverb.h @@ -0,0 +1,111 @@ +// +// C++ Interface: reverb +// +// Description: +// +// +// Author: Juan Linietsky <reduzio@gmail.com>, (C) 2006 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#ifndef REVERB_H +#define REVERB_H + +#include "typedefs.h" +#include "os/memory.h" +#include "audio_frame.h" + +class Reverb { +public: + enum { + INPUT_BUFFER_MAX_SIZE=1024, + + }; +private: + enum { + + MAX_COMBS=8, + MAX_ALLPASS=4, + MAX_ECHO_MS=500 + + }; + + + + static const float comb_tunings[MAX_COMBS]; + static const float allpass_tunings[MAX_ALLPASS]; + + struct Comb { + + int size; + float *buffer; + float feedback; + float damp; //lowpass + float damp_h; //history + int pos; + int extra_spread_frames; + + Comb() { size=0; buffer=0; feedback=0; damp_h=0; pos=0; } + }; + + struct AllPass { + + int size; + float *buffer; + int pos; + int extra_spread_frames; + AllPass() { size=0; buffer=0; pos=0; } + }; + + Comb comb[MAX_COMBS]; + AllPass allpass[MAX_ALLPASS]; + float *input_buffer; + float *echo_buffer; + int echo_buffer_size; + int echo_buffer_pos; + + float hpf_h1,hpf_h2; + + + struct Parameters { + + float room_size; + float damp; + float wet; + float dry; + float mix_rate; + float extra_spread_base; + float extra_spread; + float predelay; + float predelay_fb; + float hpf; + } params; + + void configure_buffers(); + void update_parameters(); + void clear_buffers(); +public: + + void set_room_size(float p_size); + void set_damp(float p_damp); + void set_wet(float p_wet); + void set_dry(float p_dry); + void set_predelay(float p_predelay); // in ms + void set_predelay_feedback(float p_predelay_fb); // in ms + void set_highpass(float p_frq); + void set_mix_rate(float p_mix_rate); + void set_extra_spread(float p_spread); + void set_extra_spread_base(float p_sec); + + void process(float *p_src,float *p_dst,int p_frames); + + Reverb(); + + ~Reverb(); + +}; + + + +#endif diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 9b938a7f86..8a8b9ebf76 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -29,6 +29,19 @@ #include "audio_server.h" #include "globals.h" #include "os/os.h" +#include "servers/audio/effects/audio_effect_compressor.h" +#include "io/resource_loader.h" +#include "os/file_access.h" +#ifdef TOOLS_ENABLED + +#define MARK_EDITED set_edited(true); + +#else + +#define MARK_EDITED + +#endif + AudioDriver *AudioDriver::singleton=NULL; AudioDriver *AudioDriver::get_singleton() { @@ -42,12 +55,16 @@ void AudioDriver::set_singleton() { void AudioDriver::audio_server_process(int p_frames,int32_t *p_buffer,bool p_update_mix_time) { - AudioServer * audio_server = static_cast<AudioServer*>(AudioServer::get_singleton()); + if (p_update_mix_time) update_mix_time(p_frames); -// audio_server->driver_process(p_frames,p_buffer); + + if (AudioServer::get_singleton()) + AudioServer::get_singleton()->_driver_process(p_frames,p_buffer); } + + void AudioDriver::update_mix_time(int p_frames) { _mix_amount+=p_frames; @@ -74,7 +91,6 @@ AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS]; int AudioDriverManager::driver_count=0; - void AudioDriverManager::add_driver(AudioDriver *p_driver) { ERR_FAIL_COND(driver_count>=MAX_DRIVERS); @@ -97,62 +113,548 @@ AudioDriver *AudioDriverManager::get_driver(int p_driver) { ////////////////////////////////////////////// ////////////////////////////////////////////// +void AudioServer::_driver_process(int p_frames,int32_t* p_buffer) { + + int todo=p_frames; + + while(todo) { + + if (to_mix==0) { + _mix_step(); + } + + int to_copy = MIN(to_mix,todo); + + Bus *master = buses[0]; + + int from = buffer_size-to_mix; + int from_buf=p_frames-todo; + + //master master, send to output + int cs = master->channels.size(); + for(int k=0;k<cs;k++) { + + if (master->channels[k].active) { + + AudioFrame *buf = master->channels[k].buffer.ptr(); + + for(int j=0;j<to_copy;j++) { + + float l = CLAMP(buf[from+j].l,-1.0,1.0); + int32_t vl = l*((1<<20)-1); + p_buffer[(from_buf+j)*(cs*2)+k*2+0]=vl<<11; + + float r = CLAMP(buf[from+j].r,-1.0,1.0); + int32_t vr = r*((1<<20)-1); + p_buffer[(from_buf+j)*(cs*2)+k*2+1]=vr<<11; + } + + } else { + for(int j=0;j<to_copy;j++) { + + p_buffer[(from_buf+j)*(cs*2)+k*2+0]=0; + p_buffer[(from_buf+j)*(cs*2)+k*2+1]=0; + } + } + + } + + todo-=to_copy; + to_mix-=to_copy; + + } + +} + +void AudioServer::_mix_step() { + + for(int i=0;i<buses.size();i++) { + Bus *bus = buses[i]; + bus->index_cache=i; //might be moved around by editor, so.. + for(int k=0;k<bus->channels.size();k++) { + + bus->channels[k].used=false; + } + } + + //make callbacks for mixing the audio + for (Set<CallbackItem>::Element *E=callbacks.front();E;E=E->next()) { + + E->get().callback(E->get().userdata); + } + + for(int i=buses.size()-1;i>=0;i--) { + //go bus by bus + Bus *bus = buses[i]; + + + for(int k=0;k<bus->channels.size();k++) { + + if (bus->channels[k].active && !bus->channels[k].used) { + //buffer was not used, but it's still active, so it must be cleaned + AudioFrame *buf = bus->channels[k].buffer.ptr(); + + for(uint32_t j=0;j<buffer_size;j++) { + + buf[j]=AudioFrame(0,0); + } + } + + } + + + //process effects + for(int j=0;j<bus->effects.size();j++) { + + if (!bus->effects[j].enabled) + continue; + + for(int k=0;k<bus->channels.size();k++) { + + if (!bus->channels[k].active) + continue; + bus->channels[k].effect_instances[j]->process(bus->channels[k].buffer.ptr(),temp_buffer[k].ptr(),buffer_size); + } + + //swap buffers, so internal buffer always has the right data + for(int k=0;k<bus->channels.size();k++) { + + if (!buses[i]->channels[k].active) + continue; + SWAP(bus->channels[k].buffer,temp_buffer[k]); + } + } + + //process send + + Bus *send=NULL; + + if (i>0) { + //everything has a send save for master bus + if (!bus_map.has(bus->send)) { + send=buses[0]; + } else { + send=bus_map[bus->send]; + if (send->index_cache>=bus->index_cache) { //invalid, send to master + send=buses[0]; + } + } + } + + + for(int k=0;k<bus->channels.size();k++) { + + if (!bus->channels[k].active) + continue; + + AudioFrame *buf = bus->channels[k].buffer.ptr(); + + + AudioFrame peak = AudioFrame(0,0); + for(uint32_t j=0;j<buffer_size;j++) { + float l = ABS(buf[j].l); + if (l>peak.l) { + peak.l=l; + } + float r = ABS(buf[j].r); + if (r>peak.r) { + peak.r=r; + } + } + + bus->channels[k].peak_volume=AudioFrame(Math::linear2db(peak.l+0.0000000001),Math::linear2db(peak.r+0.0000000001)); + + + if (!bus->channels[k].used) { + //see if any audio is contained, because channel was not used + + + if (MAX(peak.r,peak.l) > Math::db2linear(channel_disable_treshold_db)) { + bus->channels[k].last_mix_with_audio=mix_frames; + } else if (mix_frames-bus->channels[k].last_mix_with_audio > channel_disable_frames ) { + bus->channels[k].active=false; + continue; //went inactive, dont mix. + } + } + + if (send) { + //if not master bus, send + AudioFrame *target_buf = thread_get_channel_mix_buffer(send->index_cache,k); + + for(uint32_t j=0;j<buffer_size;j++) { + target_buf[j]+=buf[j]; + } + } + + } + + } + + + mix_frames+=buffer_size; + to_mix=buffer_size; + +} + +AudioFrame *AudioServer::thread_get_channel_mix_buffer(int p_bus,int p_buffer) { + + ERR_FAIL_INDEX_V(p_bus,buses.size(),NULL); + ERR_FAIL_INDEX_V(p_buffer,buses[p_bus]->channels.size(),NULL); + + AudioFrame *data = buses[p_bus]->channels[p_buffer].buffer.ptr(); + + + if (!buses[p_bus]->channels[p_buffer].used) { + buses[p_bus]->channels[p_buffer].used=true; + buses[p_bus]->channels[p_buffer].active=true; + buses[p_bus]->channels[p_buffer].last_mix_with_audio=mix_frames; + for(uint32_t i=0;i<buffer_size;i++) { + data[i]=AudioFrame(0,0); + } + } + + return data; +} + +int AudioServer::thread_get_mix_buffer_size() const { + + return buffer_size; +} + +int AudioServer::thread_find_bus_index(const StringName& p_name) { + + if (bus_map.has(p_name)) { + return bus_map[p_name]->index_cache; + } else { + return 0; + } + +} + void AudioServer::set_bus_count(int p_count) { ERR_FAIL_COND(p_count<1); ERR_FAIL_INDEX(p_count,256); + + MARK_EDITED + lock(); + int cb = buses.size(); + + if (p_count<buses.size()) { + for(int i=p_count;i<buses.size();i++) { + bus_map.erase(buses[i]->name); + memdelete(buses[i]); + } + } + buses.resize(p_count); + + for(int i=cb;i<buses.size();i++) { + + String attempt="New Bus"; + int attempts=1; + while(true) { + + bool name_free=true; + for(int j=0;j<i;j++) { + + if (buses[j]->name==attempt) { + name_free=false; + break; + } + } + + if (!name_free) { + attempts++; + attempt="New Bus " +itos(attempts); + } else { + break; + } + + } + + + buses[i]=memnew(Bus); + buses[i]->channels.resize(_get_channel_count()); + for(int j=0;j<_get_channel_count();j++) { + buses[i]->channels[j].buffer.resize(buffer_size); + } + buses[i]->name=attempt; + buses[i]->solo=false; + buses[i]->mute=false; + buses[i]->bypass=false; + buses[i]->volume_db=0; + if (i>0) { + buses[i]->send="Master"; + } + + bus_map[attempt]=buses[i]; + + } + unlock(); + + emit_signal("bus_layout_changed"); } -int AudioServer::get_bus_count() const { - return buses.size(); +void AudioServer::remove_bus(int p_index) { + + ERR_FAIL_INDEX(p_index,buses.size()); + ERR_FAIL_COND(p_index==0); + + MARK_EDITED + + lock(); + bus_map.erase(buses[p_index]->name); + memdelete(buses[p_index]); + buses.remove(p_index); + unlock(); } -void AudioServer::set_bus_mode(int p_bus,BusMode p_mode) { +void AudioServer::add_bus(int p_at_pos) { - ERR_FAIL_INDEX(p_bus,buses.size()); + MARK_EDITED + + if (p_at_pos>=buses.size()) { + p_at_pos=-1; + } else if (p_at_pos==0) { + if (buses.size()>1) + p_at_pos=1; + else + p_at_pos=-1; + } + + String attempt="New Bus"; + int attempts=1; + while(true) { + + bool name_free=true; + for(int j=0;j<buses.size();j++) { + + if (buses[j]->name==attempt) { + name_free=false; + break; + } + } + + if (!name_free) { + attempts++; + attempt="New Bus " +itos(attempts); + } else { + break; + } + + } + + Bus* bus =memnew(Bus); + bus->channels.resize(_get_channel_count()); + for(int j=0;j<_get_channel_count();j++) { + bus->channels[j].buffer.resize(buffer_size); + } + bus->name=attempt; + bus->solo=false; + bus->mute=false; + bus->bypass=false; + bus->volume_db=0; + + bus_map[attempt]=bus; + + if (p_at_pos==-1) + buses.push_back(bus); + else + buses.insert(p_at_pos,bus); } -AudioServer::BusMode AudioServer::get_bus_mode(int p_bus) const { - ERR_FAIL_INDEX_V(p_bus,buses.size(),BUS_MODE_STEREO); +void AudioServer::move_bus(int p_bus,int p_to_pos) { + + ERR_FAIL_COND(p_bus<1 || p_bus>=buses.size()); + ERR_FAIL_COND(p_to_pos!=-1 && (p_to_pos<1 || p_to_pos>buses.size())); + + MARK_EDITED + + if (p_bus==p_to_pos) + return; - return buses[p_bus].mode; + Bus *bus = buses[p_bus]; + buses.remove(p_bus); + + if (p_to_pos==-1) { + buses.push_back(bus); + } else if (p_to_pos<p_bus) { + buses.insert(p_to_pos,bus); + } else { + buses.insert(p_to_pos-1,bus); + } } +int AudioServer::get_bus_count() const { + + return buses.size(); +} + + void AudioServer::set_bus_name(int p_bus,const String& p_name) { ERR_FAIL_INDEX(p_bus,buses.size()); - buses[p_bus].name=p_name; + if (p_bus==0 && p_name!="Master") + return; //bus 0 is always master + + MARK_EDITED + + lock(); + + if (buses[p_bus]->name==p_name) { + unlock(); + return; + } + + String attempt=p_name; + int attempts=1; + + while(true) { + + bool name_free=true; + for(int i=0;i<buses.size();i++) { + + if (buses[i]->name==attempt) { + name_free=false; + break; + } + } + + if (name_free) { + break; + } + + attempts++; + attempt=p_name+" "+itos(attempts); + } + bus_map.erase(buses[p_bus]->name); + buses[p_bus]->name=attempt; + bus_map[attempt]=buses[p_bus]; + unlock(); + + emit_signal("bus_layout_changed"); } String AudioServer::get_bus_name(int p_bus) const { ERR_FAIL_INDEX_V(p_bus,buses.size(),String()); - return buses[p_bus].name; + return buses[p_bus]->name; } void AudioServer::set_bus_volume_db(int p_bus,float p_volume_db) { ERR_FAIL_INDEX(p_bus,buses.size()); - buses[p_bus].volume_db=p_volume_db; + + MARK_EDITED + + buses[p_bus]->volume_db=p_volume_db; } float AudioServer::get_bus_volume_db(int p_bus) const { ERR_FAIL_INDEX_V(p_bus,buses.size(),0); - return buses[p_bus].volume_db; + return buses[p_bus]->volume_db; + +} + +void AudioServer::set_bus_send(int p_bus,const StringName& p_send) { + + ERR_FAIL_INDEX(p_bus,buses.size()); + + MARK_EDITED + + buses[p_bus]->send=p_send; + +} + +StringName AudioServer::get_bus_send(int p_bus) const { + + ERR_FAIL_INDEX_V(p_bus,buses.size(),StringName()); + return buses[p_bus]->send; } + +void AudioServer::set_bus_solo(int p_bus,bool p_enable) { + + ERR_FAIL_INDEX(p_bus,buses.size()); + + MARK_EDITED + + buses[p_bus]->solo=p_enable; + +} + +bool AudioServer::is_bus_solo(int p_bus) const{ + + ERR_FAIL_INDEX_V(p_bus,buses.size(),false); + + return buses[p_bus]->solo; + +} + +void AudioServer::set_bus_mute(int p_bus,bool p_enable){ + + ERR_FAIL_INDEX(p_bus,buses.size()); + + MARK_EDITED + + buses[p_bus]->mute=p_enable; +} +bool AudioServer::is_bus_mute(int p_bus) const{ + + ERR_FAIL_INDEX_V(p_bus,buses.size(),false); + + return buses[p_bus]->mute; + +} + +void AudioServer::set_bus_bypass_effects(int p_bus,bool p_enable){ + + ERR_FAIL_INDEX(p_bus,buses.size()); + + MARK_EDITED + + buses[p_bus]->bypass=p_enable; +} +bool AudioServer::is_bus_bypassing_effects(int p_bus) const{ + + ERR_FAIL_INDEX_V(p_bus,buses.size(),false); + + return buses[p_bus]->bypass; + +} + + +void AudioServer::_update_bus_effects(int p_bus) { + + for(int i=0;i<buses[p_bus]->channels.size();i++) { + buses[p_bus]->channels[i].effect_instances.resize(buses[p_bus]->effects.size()); + for(int j=0;j<buses[p_bus]->effects.size();j++) { + Ref<AudioEffectInstance> fx = buses[p_bus]->effects[j].effect->instance(); + if (fx->cast_to<AudioEffectCompressorInstance>()) { + fx->cast_to<AudioEffectCompressorInstance>()->set_current_channel(i); + } + buses[p_bus]->channels[i].effect_instances[j]=fx; + + } + } +} + + void AudioServer::add_bus_effect(int p_bus,const Ref<AudioEffect>& p_effect,int p_at_pos) { ERR_FAIL_COND(p_effect.is_null()); ERR_FAIL_INDEX(p_bus,buses.size()); + MARK_EDITED + + lock(); Bus::Effect fx; @@ -160,12 +662,14 @@ void AudioServer::add_bus_effect(int p_bus,const Ref<AudioEffect>& p_effect,int //fx.instance=p_effect->instance(); fx.enabled=true; - if (p_at_pos>=buses[p_bus].effects.size() || p_at_pos<0) { - buses[p_bus].effects.push_back(fx); + if (p_at_pos>=buses[p_bus]->effects.size() || p_at_pos<0) { + buses[p_bus]->effects.push_back(fx); } else { - buses[p_bus].effects.insert(p_at_pos,fx); + buses[p_bus]->effects.insert(p_at_pos,fx); } + _update_bus_effects(p_bus); + unlock(); } @@ -174,9 +678,13 @@ void AudioServer::remove_bus_effect(int p_bus,int p_effect) { ERR_FAIL_INDEX(p_bus,buses.size()); + MARK_EDITED + + lock(); - buses[p_bus].effects.remove(p_effect); + buses[p_bus]->effects.remove(p_effect); + _update_bus_effects(p_bus); unlock(); } @@ -185,52 +693,131 @@ int AudioServer::get_bus_effect_count(int p_bus) { ERR_FAIL_INDEX_V(p_bus,buses.size(),0); - return buses[p_bus].effects.size(); + return buses[p_bus]->effects.size(); } Ref<AudioEffect> AudioServer::get_bus_effect(int p_bus,int p_effect) { ERR_FAIL_INDEX_V(p_bus,buses.size(),Ref<AudioEffect>()); - ERR_FAIL_INDEX_V(p_effect,buses[p_bus].effects.size(),Ref<AudioEffect>()); + ERR_FAIL_INDEX_V(p_effect,buses[p_bus]->effects.size(),Ref<AudioEffect>()); - return buses[p_bus].effects[p_effect].effect; + return buses[p_bus]->effects[p_effect].effect; } void AudioServer::swap_bus_effects(int p_bus,int p_effect,int p_by_effect) { ERR_FAIL_INDEX(p_bus,buses.size()); - ERR_FAIL_INDEX(p_effect,buses[p_bus].effects.size()); - ERR_FAIL_INDEX(p_by_effect,buses[p_bus].effects.size()); + ERR_FAIL_INDEX(p_effect,buses[p_bus]->effects.size()); + ERR_FAIL_INDEX(p_by_effect,buses[p_bus]->effects.size()); + + MARK_EDITED + lock(); - SWAP( buses[p_bus].effects[p_effect], buses[p_bus].effects[p_by_effect] ); + SWAP( buses[p_bus]->effects[p_effect], buses[p_bus]->effects[p_by_effect] ); + _update_bus_effects(p_bus); unlock(); } void AudioServer::set_bus_effect_enabled(int p_bus,int p_effect,bool p_enabled) { ERR_FAIL_INDEX(p_bus,buses.size()); - ERR_FAIL_INDEX(p_effect,buses[p_bus].effects.size()); - buses[p_bus].effects[p_effect].enabled=p_enabled; + ERR_FAIL_INDEX(p_effect,buses[p_bus]->effects.size()); + + MARK_EDITED + + + buses[p_bus]->effects[p_effect].enabled=p_enabled; } bool AudioServer::is_bus_effect_enabled(int p_bus,int p_effect) const { ERR_FAIL_INDEX_V(p_bus,buses.size(),false); - ERR_FAIL_INDEX_V(p_effect,buses[p_bus].effects.size(),false); - return buses[p_bus].effects[p_effect].enabled; + ERR_FAIL_INDEX_V(p_effect,buses[p_bus]->effects.size(),false); + return buses[p_bus]->effects[p_effect].enabled; + +} + + +float AudioServer::get_bus_peak_volume_left_db(int p_bus,int p_channel) const { + + ERR_FAIL_INDEX_V(p_bus,buses.size(),0); + ERR_FAIL_INDEX_V(p_channel,buses[p_bus]->channels.size(),0); + + return buses[p_bus]->channels[p_channel].peak_volume.l; + +} +float AudioServer::get_bus_peak_volume_right_db(int p_bus,int p_channel) const { + + ERR_FAIL_INDEX_V(p_bus,buses.size(),0); + ERR_FAIL_INDEX_V(p_channel,buses[p_bus]->channels.size(),0); + + return buses[p_bus]->channels[p_channel].peak_volume.r; + +} + +bool AudioServer::is_bus_channel_active(int p_bus,int p_channel) const { + + ERR_FAIL_INDEX_V(p_bus,buses.size(),false); + ERR_FAIL_INDEX_V(p_channel,buses[p_bus]->channels.size(),false); + + return buses[p_bus]->channels[p_channel].active; } void AudioServer::init() { + channel_disable_treshold_db=GLOBAL_DEF("audio/channel_disable_treshold_db",-60.0); + channel_disable_frames=float(GLOBAL_DEF("audio/channel_disable_time",2.0))*get_mix_rate(); + buffer_size=1024; //harcoded for now + switch( get_speaker_mode() ) { + case SPEAKER_MODE_STEREO: { + temp_buffer.resize(1); + } break; + case SPEAKER_SURROUND_51: { + temp_buffer.resize(3); + } break; + case SPEAKER_SURROUND_71: { + temp_buffer.resize(4); + } break; + } + + for(int i=0;i<temp_buffer.size();i++) { + temp_buffer[i].resize(buffer_size); + } + + mix_count=0; set_bus_count(1);; set_bus_name(0,"Master"); + + + if (AudioDriver::get_singleton()) + AudioDriver::get_singleton()->start(); +#ifdef TOOLS_ENABLED + set_edited(false); //avoid editors from thinking this was edited +#endif + +} + +void AudioServer::load_default_bus_layout() { + + if (FileAccess::exists("res://default_bus_layout.tres")) { + Ref<AudioBusLayout> default_layout = ResourceLoader::load("res://default_bus_layout.tres"); + if (default_layout.is_valid()) { + set_bus_layout(default_layout); + } + } + } + void AudioServer::finish() { + for(int i=0;i<buses.size();i++) { + memdelete(buses[i]); + } + buses.clear(); } void AudioServer::update() { @@ -282,18 +869,353 @@ double AudioServer::get_output_delay() const { AudioServer* AudioServer::singleton=NULL; -void AudioServer::_bind_methods() { + +void* AudioServer::audio_data_alloc(uint32_t p_data_len,const uint8_t *p_from_data) { + + void * ad = memalloc( p_data_len ); + ERR_FAIL_COND_V(!ad,NULL); + if (p_from_data) { + copymem(ad,p_from_data,p_data_len); + } + + audio_data_lock->lock(); + audio_data[ad]=p_data_len; + audio_data_total_mem+=p_data_len; + audio_data_max_mem=MAX(audio_data_total_mem,audio_data_max_mem); + audio_data_lock->unlock(); + + return ad; +} + +void AudioServer::audio_data_free(void* p_data) { + + audio_data_lock->lock(); + if (!audio_data.has(p_data)) { + audio_data_lock->unlock(); + ERR_FAIL(); + } + + audio_data_total_mem-=audio_data[p_data]; + audio_data.erase(p_data); + memfree(p_data); + audio_data_lock->unlock(); + + +} + +size_t AudioServer::audio_data_get_total_memory_usage() const{ + + return audio_data_total_mem; +} +size_t AudioServer::audio_data_get_max_memory_usage() const{ + + return audio_data_max_mem; + +} + +void AudioServer::add_callback(AudioCallback p_callback,void *p_userdata) { + lock(); + CallbackItem ci; + ci.callback=p_callback; + ci.userdata=p_userdata; + callbacks.insert(ci); + unlock(); +} + +void AudioServer::remove_callback(AudioCallback p_callback,void *p_userdata) { + + lock(); + CallbackItem ci; + ci.callback=p_callback; + ci.userdata=p_userdata; + callbacks.erase(ci); + unlock(); + +} + +void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_state) { + + ERR_FAIL_COND(p_state.is_null() || p_state->buses.size()==0); + + lock(); + for(int i=0;i<buses.size();i++) { + memdelete(buses[i]); + } + buses.resize(p_state->buses.size()); + bus_map.clear(); + for(int i=0;i<p_state->buses.size();i++) { + Bus * bus = memnew(Bus); + if (i==0) { + bus->name="Master"; + } else { + bus->name=p_state->buses[i].name; + bus->send=p_state->buses[i].send; + } + + bus->solo=p_state->buses[i].solo; + bus->mute=p_state->buses[i].mute; + bus->bypass=p_state->buses[i].bypass; + bus->volume_db=p_state->buses[i].volume_db; + + for(int j=0;j<p_state->buses[i].effects.size();j++) { + + Ref<AudioEffect> fx = p_state->buses[i].effects[j].effect; + + if (fx.is_valid()) { + + Bus::Effect bfx; + bfx.effect=fx; + bfx.enabled=p_state->buses[i].effects[j].enabled; + bus->effects.push_back(bfx); + } + } + + bus_map[bus->name]=bus; + buses[i]=bus; + + buses[i]->channels.resize(_get_channel_count()); + for(int j=0;j<_get_channel_count();j++) { + buses[i]->channels[j].buffer.resize(buffer_size); + } + _update_bus_effects(i); + } +#ifdef TOOLS_ENABLED + set_edited(false); +#endif + unlock(); + +} + + +Ref<AudioBusLayout> AudioServer::generate_bus_layout() const { + + Ref<AudioBusLayout> state; + state.instance(); + + state->buses.resize( buses.size() ); + + for(int i=0;i<buses.size();i++) { + + state->buses[i].name=buses[i]->name; + state->buses[i].send=buses[i]->send; + state->buses[i].mute=buses[i]->mute; + state->buses[i].solo=buses[i]->solo; + state->buses[i].bypass=buses[i]->bypass; + state->buses[i].volume_db=buses[i]->volume_db; + for(int j=0;j<buses[i]->effects.size();j++) { + AudioBusLayout::Bus::Effect fx; + fx.effect=buses[i]->effects[j].effect; + fx.enabled=buses[i]->effects[j].enabled; + state->buses[i].effects.push_back(fx); + + } + } + + return state; } +void AudioServer::_bind_methods() { + + + ClassDB::bind_method(_MD("set_bus_count","amount"),&AudioServer::set_bus_count); + ClassDB::bind_method(_MD("get_bus_count"),&AudioServer::get_bus_count); + + ClassDB::bind_method(_MD("remove_bus","index"),&AudioServer::remove_bus); + ClassDB::bind_method(_MD("add_bus","at_pos"),&AudioServer::add_bus,DEFVAL(-1)); + ClassDB::bind_method(_MD("move_bus","index","to_index"),&AudioServer::move_bus); + + ClassDB::bind_method(_MD("set_bus_name","bus_idx","name"),&AudioServer::set_bus_name); + ClassDB::bind_method(_MD("get_bus_name","bus_idx"),&AudioServer::get_bus_name); + + ClassDB::bind_method(_MD("set_bus_volume_db","bus_idx","volume_db"),&AudioServer::set_bus_volume_db); + ClassDB::bind_method(_MD("get_bus_volume_db","bus_idx"),&AudioServer::get_bus_volume_db); + + ClassDB::bind_method(_MD("set_bus_send","bus_idx","send"),&AudioServer::set_bus_send); + ClassDB::bind_method(_MD("get_bus_send","bus_idx"),&AudioServer::get_bus_send); + + ClassDB::bind_method(_MD("set_bus_solo","bus_idx","enable"),&AudioServer::set_bus_solo); + ClassDB::bind_method(_MD("is_bus_solo","bus_idx"),&AudioServer::is_bus_solo); + + ClassDB::bind_method(_MD("set_bus_mute","bus_idx","enable"),&AudioServer::set_bus_mute); + ClassDB::bind_method(_MD("is_bus_mute","bus_idx"),&AudioServer::is_bus_mute); + + ClassDB::bind_method(_MD("set_bus_bypass_effects","bus_idx","enable"),&AudioServer::set_bus_bypass_effects); + ClassDB::bind_method(_MD("is_bus_bypassing_effects","bus_idx"),&AudioServer::is_bus_bypassing_effects); + + ClassDB::bind_method(_MD("add_bus_effect","bus_idx","effect:AudioEffect"),&AudioServer::add_bus_effect,DEFVAL(-1)); + ClassDB::bind_method(_MD("remove_bus_effect","bus_idx","effect_idx"),&AudioServer::remove_bus_effect); + + ClassDB::bind_method(_MD("get_bus_effect_count","bus_idx"),&AudioServer::add_bus_effect); + ClassDB::bind_method(_MD("get_bus_effect:AudioEffect","bus_idx","effect_idx"),&AudioServer::get_bus_effect); + ClassDB::bind_method(_MD("swap_bus_effects","bus_idx","effect_idx","by_effect_idx"),&AudioServer::swap_bus_effects); + + ClassDB::bind_method(_MD("set_bus_effect_enabled","bus_idx","effect_idx","enabled"),&AudioServer::set_bus_effect_enabled); + ClassDB::bind_method(_MD("is_bus_effect_enabled","bus_idx","effect_idx"),&AudioServer::is_bus_effect_enabled); + + ClassDB::bind_method(_MD("get_bus_peak_volume_left_db","bus_idx","channel"),&AudioServer::get_bus_peak_volume_left_db); + ClassDB::bind_method(_MD("get_bus_peak_volume_right_db","bus_idx","channel"),&AudioServer::get_bus_peak_volume_right_db); + + ClassDB::bind_method(_MD("lock"),&AudioServer::lock); + ClassDB::bind_method(_MD("unlock"),&AudioServer::unlock); + + ClassDB::bind_method(_MD("get_speaker_mode"),&AudioServer::get_speaker_mode); + ClassDB::bind_method(_MD("get_mix_rate"),&AudioServer::get_mix_rate); + + ClassDB::bind_method(_MD("set_state","state:AudioServerState"),&AudioServer::set_bus_layout); + ClassDB::bind_method(_MD("generate_state:AudioServerState"),&AudioServer::generate_bus_layout); + + ADD_SIGNAL(MethodInfo("bus_layout_changed") ); +} + AudioServer::AudioServer() { singleton=this; + audio_data_total_mem=0; + audio_data_max_mem=0; + audio_data_lock=Mutex::create(); + mix_frames=0; + to_mix=0; + } AudioServer::~AudioServer() { + memdelete(audio_data_lock); +} + +///////////////////////////////// + + + +bool AudioBusLayout::_set(const StringName& p_name, const Variant& p_value) { + + String s = p_name; + if (s.begins_with("bus/")) { + int index = s.get_slice("/",1).to_int(); + if (buses.size()<=index) { + buses.resize(index+1); + } + + Bus &bus = buses[index]; + + String what = s.get_slice("/",2); + + if (what=="name") { + bus.name=p_value; + } else if (what=="solo") { + bus.solo=p_value; + } else if (what=="mute") { + bus.mute=p_value; + } else if (what=="bypass_fx") { + bus.bypass=p_value; + } else if (what=="volume_db") { + bus.volume_db=p_value; + } else if (what=="send") { + bus.send=p_value; + } else if (what=="effect") { + int which = s.get_slice("/",3).to_int(); + if (bus.effects.size()<=which) { + bus.effects.resize(which+1); + } + + Bus::Effect &fx = bus.effects[which]; + + String fxwhat = s.get_slice("/",4); + if (fxwhat=="effect") { + fx.effect=p_value; + } else if (fxwhat=="enabled") { + fx.enabled=p_value; + } else { + return false; + } + + return true; + } else { + return false; + } + + return true; + } + + return false; } +bool AudioBusLayout::_get(const StringName& p_name,Variant &r_ret) const{ + + String s = p_name; + if (s.begins_with("bus/")) { + + int index = s.get_slice("/",1).to_int(); + if (index<0 || index>=buses.size()) + return false; + + const Bus &bus = buses[index]; + + String what = s.get_slice("/",2); + + if (what=="name") { + r_ret=bus.name; + } else if (what=="solo") { + r_ret=bus.solo; + } else if (what=="mute") { + r_ret=bus.mute; + } else if (what=="bypass_fx") { + r_ret=bus.bypass; + } else if (what=="volume_db") { + r_ret=bus.volume_db; + } else if (what=="send") { + r_ret=bus.send; + } else if (what=="effect") { + int which = s.get_slice("/",3).to_int(); + if (which<0 || which>=bus.effects.size()) { + return false; + } + + const Bus::Effect &fx = bus.effects[which]; + + String fxwhat = s.get_slice("/",4); + if (fxwhat=="effect") { + r_ret=fx.effect; + } else if (fxwhat=="enabled") { + r_ret=fx.enabled; + } else { + return false; + } + + return true; + } else { + return false; + } + + return true; + } + + return false; + +} +void AudioBusLayout::_get_property_list( List<PropertyInfo> *p_list) const{ + + for(int i=0;i<buses.size();i++) { + p_list->push_back(PropertyInfo(Variant::STRING,"bus/"+itos(i)+"/name",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + p_list->push_back(PropertyInfo(Variant::BOOL,"bus/"+itos(i)+"/solo",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + p_list->push_back(PropertyInfo(Variant::BOOL,"bus/"+itos(i)+"/mute",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + p_list->push_back(PropertyInfo(Variant::BOOL,"bus/"+itos(i)+"/bypass_fx",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + p_list->push_back(PropertyInfo(Variant::REAL,"bus/"+itos(i)+"/volume_db",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + p_list->push_back(PropertyInfo(Variant::REAL,"bus/"+itos(i)+"/send",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + + for(int j=0;j<buses[i].effects.size();j++) { + p_list->push_back(PropertyInfo(Variant::OBJECT,"bus/"+itos(i)+"/effect/"+itos(j)+"/effect",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + p_list->push_back(PropertyInfo(Variant::BOOL,"bus/"+itos(i)+"/effect/"+itos(j)+"/enabled",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); + } + } +} + + +AudioBusLayout::AudioBusLayout() { + + buses.resize(1); + buses[0].name="Master"; +} diff --git a/servers/audio_server.h b/servers/audio_server.h index 77aca39760..88849bb591 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -100,56 +100,131 @@ public: }; +class AudioBusLayout; + class AudioServer : public Object { GDCLASS( AudioServer, Object ) public: - enum BusMode { - BUS_MODE_STEREO, - BUS_MODE_SURROUND - }; - //re-expose this her, as AudioDriver is not exposed to script enum SpeakerMode { SPEAKER_MODE_STEREO, SPEAKER_SURROUND_51, SPEAKER_SURROUND_71, }; + + enum { + AUDIO_DATA_INVALID_ID=-1 + }; + + typedef void (*AudioCallback)(void* p_userdata); + private: uint32_t buffer_size; + uint64_t mix_count; + uint64_t mix_frames; + + float channel_disable_treshold_db; + uint32_t channel_disable_frames; + + int to_mix; struct Bus { - String name; - BusMode mode; - Vector<AudioFrame> buffer; + StringName name; + bool solo; + bool mute; + bool bypass; + + //Each channel is a stereo pair. + struct Channel { + bool used; + bool active; + AudioFrame peak_volume; + Vector<AudioFrame> buffer; + Vector<Ref<AudioEffectInstance> > effect_instances; + uint64_t last_mix_with_audio; + Channel() { last_mix_with_audio=0; used=false; active=false; peak_volume=AudioFrame(0,0); } + }; + + Vector<Channel> channels; + struct Effect { Ref<AudioEffect> effect; - Ref<AudioEffectInstance> instance; bool enabled; }; Vector<Effect> effects; - float volume_db; + StringName send; + int index_cache; }; - Vector<Bus> buses; + Vector< Vector<AudioFrame> >temp_buffer; //temp_buffer for each level + Vector<Bus*> buses; + Map<StringName,Bus*> bus_map; + _FORCE_INLINE_ int _get_channel_count() const { + switch (AudioDriver::get_singleton()->get_speaker_mode()) { + case AudioDriver::SPEAKER_MODE_STEREO: return 1; + case AudioDriver::SPEAKER_SURROUND_51: return 3; + case AudioDriver::SPEAKER_SURROUND_71: return 4; + + } + ERR_FAIL_V(1); + } + + + void _update_bus_effects(int p_bus); - static void _bind_methods(); static AudioServer* singleton; + + // TODO create an audiodata pool to optimize memory + + + Map<void*,uint32_t> audio_data; + size_t audio_data_total_mem; + size_t audio_data_max_mem; + + Mutex *audio_data_lock; + + void _mix_step(); + + struct CallbackItem { + + AudioCallback callback; + void *userdata; + + bool operator<(const CallbackItem& p_item) const { + return (callback==p_item.callback ? userdata < p_item.userdata : callback < p_item.callback); + } + }; + + Set<CallbackItem> callbacks; + +friend class AudioDriver; + void _driver_process(int p_frames, int32_t *p_buffer); +protected: + + static void _bind_methods(); public: + //do not use from outside audio thread + AudioFrame *thread_get_channel_mix_buffer(int p_bus,int p_buffer); + int thread_get_mix_buffer_size() const; + int thread_find_bus_index(const StringName& p_name); + void set_bus_count(int p_count); int get_bus_count() const; - void set_bus_mode(int p_bus,BusMode p_mode); - BusMode get_bus_mode(int p_bus) const; + void remove_bus(int p_index); + void add_bus(int p_at_pos=-1); + + void move_bus(int p_bus,int p_to_pos); void set_bus_name(int p_bus,const String& p_name); String get_bus_name(int p_bus) const; @@ -157,6 +232,19 @@ public: void set_bus_volume_db(int p_bus,float p_volume_db); float get_bus_volume_db(int p_bus) const; + + void set_bus_send(int p_bus,const StringName& p_send); + StringName get_bus_send(int p_bus) const; + + void set_bus_solo(int p_bus,bool p_enable); + bool is_bus_solo(int p_bus) const; + + void set_bus_mute(int p_bus,bool p_enable); + bool is_bus_mute(int p_bus) const; + + void set_bus_bypass_effects(int p_bus,bool p_enable); + bool is_bus_bypassing_effects(int p_bus) const; + void add_bus_effect(int p_bus,const Ref<AudioEffect>& p_effect,int p_at_pos=-1); void remove_bus_effect(int p_bus,int p_effect); @@ -168,9 +256,15 @@ public: void set_bus_effect_enabled(int p_bus,int p_effect,bool p_enabled); bool is_bus_effect_enabled(int p_bus,int p_effect) const; + float get_bus_peak_volume_left_db(int p_bus,int p_channel) const; + float get_bus_peak_volume_right_db(int p_bus,int p_channel) const; + + bool is_bus_channel_active(int p_bus,int p_channel) const; + virtual void init(); virtual void finish(); virtual void update(); + virtual void load_default_bus_layout(); /* MISC config */ @@ -188,13 +282,74 @@ public: virtual double get_mix_time() const; //useful for video -> audio sync virtual double get_output_delay() const; + void* audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data=NULL); + void audio_data_free(void* p_data); + + size_t audio_data_get_total_memory_usage() const; + size_t audio_data_get_max_memory_usage() const; + + + void add_callback(AudioCallback p_callback,void *p_userdata); + void remove_callback(AudioCallback p_callback,void *p_userdata); + + void set_bus_layout(const Ref<AudioBusLayout>& p_state); + Ref<AudioBusLayout> generate_bus_layout() const; + AudioServer(); virtual ~AudioServer(); }; -VARIANT_ENUM_CAST( AudioServer::BusMode ) VARIANT_ENUM_CAST( AudioServer::SpeakerMode ) +class AudioBusLayout : public Resource { + + GDCLASS(AudioBusLayout,Resource) + +friend class AudioServer; + + struct Bus { + + StringName name; + bool solo; + bool mute; + bool bypass; + + struct Effect { + Ref<AudioEffect> effect; + bool enabled; + }; + + Vector<Effect> effects; + + float volume_db; + StringName send; + + Bus() { + solo=false; + mute=false; + bypass=false; + volume_db=0; + } + }; + + Vector<Bus> buses; + +protected: + + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list( List<PropertyInfo> *p_list) const; + +public: + + AudioBusLayout(); +}; + + + + + + typedef AudioServer AS; diff --git a/servers/physics_2d/broad_phase_2d_hash_grid.cpp b/servers/physics_2d/broad_phase_2d_hash_grid.cpp index d2b37319ad..8b7a410b3d 100644 --- a/servers/physics_2d/broad_phase_2d_hash_grid.cpp +++ b/servers/physics_2d/broad_phase_2d_hash_grid.cpp @@ -539,14 +539,14 @@ int BroadPhase2DHashGrid::cull_segment(const Vector2& p_from, const Vector2& p_t Vector2 max; if (dir.x<0) - max.x= (Math::floor(pos.x)*cell_size - p_from.x) / dir.x; + max.x= (Math::floor((double)pos.x)*cell_size - p_from.x) / dir.x; else - max.x= (Math::floor(pos.x + 1)*cell_size - p_from.x) / dir.x; + max.x= (Math::floor((double)pos.x + 1)*cell_size - p_from.x) / dir.x; if (dir.y<0) - max.y= (Math::floor(pos.y)*cell_size - p_from.y) / dir.y; + max.y= (Math::floor((double)pos.y)*cell_size - p_from.y) / dir.y; else - max.y= (Math::floor(pos.y + 1)*cell_size - p_from.y) / dir.y; + max.y= (Math::floor((double)pos.y + 1)*cell_size - p_from.y) / dir.y; int cullcount=0; _cull<false,true>(pos,Rect2(),p_from,p_to,p_results,p_max_results,p_result_indices,cullcount); diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp index 8b831f4ff6..01f8ffa504 100644 --- a/servers/register_server_types.cpp +++ b/servers/register_server_types.cpp @@ -35,6 +35,21 @@ #include "physics_2d_server.h" #include "script_debugger_remote.h" #include "visual/shader_types.h" +#include "audio/audio_stream.h" +#include "audio/audio_effect.h" +#include "audio/effects/audio_effect_amplify.h" +#include "audio/effects/audio_effect_reverb.h" +#include "audio/effects/audio_effect_filter.h" +#include "audio/effects/audio_effect_eq.h" +#include "audio/effects/audio_effect_distortion.h" +#include "audio/effects/audio_effect_stereo_enhance.h" +#include "audio/effects/audio_effect_panner.h" +#include "audio/effects/audio_effect_chorus.h" +#include "audio/effects/audio_effect_delay.h" +#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<ScriptDebuggerRemote::ResourceUsage>* r_usage) { @@ -67,6 +82,42 @@ void register_server_types() { shader_types = memnew( ShaderTypes ); + ClassDB::register_virtual_class<AudioStream>(); + ClassDB::register_virtual_class<AudioStreamPlayback>(); + ClassDB::register_virtual_class<AudioEffect>(); + ClassDB::register_class<AudioBusLayout>(); + + { + //audio effects + ClassDB::register_class<AudioEffectAmplify>(); + + ClassDB::register_class<AudioEffectReverb>(); + + ClassDB::register_class<AudioEffectLowPassFilter>(); + ClassDB::register_class<AudioEffectHighPassFilter>(); + ClassDB::register_class<AudioEffectBandPassFilter>(); + ClassDB::register_class<AudioEffectNotchFilter>(); + ClassDB::register_class<AudioEffectBandLimitFilter>(); + ClassDB::register_class<AudioEffectLowShelfFilter>(); + ClassDB::register_class<AudioEffectHighShelfFilter>(); + + ClassDB::register_class<AudioEffectEQ6>(); + ClassDB::register_class<AudioEffectEQ10>(); + ClassDB::register_class<AudioEffectEQ21>(); + + ClassDB::register_class<AudioEffectDistortion>(); + + ClassDB::register_class<AudioEffectStereoEnhance>(); + + ClassDB::register_class<AudioEffectPanner>(); + ClassDB::register_class<AudioEffectChorus>(); + ClassDB::register_class<AudioEffectDelay>(); + ClassDB::register_class<AudioEffectCompressor>(); + ClassDB::register_class<AudioEffectLimiter>(); + ClassDB::register_class<AudioEffectPitchShift>(); + ClassDB::register_class<AudioEffectPhaser>(); + } + ClassDB::register_virtual_class<Physics2DDirectBodyState>(); ClassDB::register_virtual_class<Physics2DDirectSpaceState>(); |