godot/servers/audio/effects/audio_effect_stereo_enhance.cpp

158 lines
5.7 KiB
C++
Raw Normal View History

/*************************************************************************/
/* audio_effect_stereo_enhance.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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. */
/*************************************************************************/
2017-01-22 10:14:30 +08:00
#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) {
2017-01-22 10:14:30 +08:00
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();
2017-01-22 10:14:30 +08:00
for (int i = 0; i < p_frame_count; i++) {
2017-01-22 10:14:30 +08:00
float l = p_src_frames[i].l;
float r = p_src_frames[i].r;
2017-01-22 10:14:30 +08:00
float center = (l + r) / 2.0f;
2017-01-22 10:14:30 +08:00
l = (center + (l - center) * intensity);
r = (center + (r - center) * intensity);
2017-01-22 10:14:30 +08:00
if (surround_mode) {
float val = (l + r) / 2.0;
2017-01-22 10:14:30 +08:00
delay_ringbuff[ringbuff_pos & ringbuff_mask] = val;
2017-01-22 10:14:30 +08:00
float out = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask] * surround_amount;
2017-01-22 10:14:30 +08:00
l += out;
r += -out;
2017-01-22 10:14:30 +08:00
} else {
float val = r;
2017-01-22 10:14:30 +08:00
delay_ringbuff[ringbuff_pos & ringbuff_mask] = val;
2017-01-22 10:14:30 +08:00
//r is delayed
r = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask];
;
2017-01-22 10:14:30 +08:00
}
p_dst_frames[i].l = l;
p_dst_frames[i].r = r;
2017-01-22 10:14:30 +08:00
ringbuff_pos++;
}
}
AudioEffectStereoEnhanceInstance::~AudioEffectStereoEnhanceInstance() {
memdelete_arr(delay_ringbuff);
}
Ref<AudioEffectInstance> AudioEffectStereoEnhance::instance() {
Ref<AudioEffectStereoEnhanceInstance> ins;
ins.instance();
ins->base = Ref<AudioEffectStereoEnhance>(this);
2017-01-22 10:14:30 +08:00
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();
2017-01-22 10:14:30 +08:00
int ringbuff_size = (int)ring_buffer_max_size;
2017-01-22 10:14:30 +08:00
int bits = 0;
2017-01-22 10:14:30 +08:00
while (ringbuff_size > 0) {
2017-01-22 10:14:30 +08:00
bits++;
ringbuff_size /= 2;
2017-01-22 10:14:30 +08:00
}
ringbuff_size = 1 << bits;
ins->ringbuff_mask = ringbuff_size - 1;
ins->ringbuff_pos = 0;
2017-01-22 10:14:30 +08:00
ins->delay_ringbuff = memnew_arr(float, ringbuff_size);
2017-01-22 10:14:30 +08:00
return ins;
}
void AudioEffectStereoEnhance::set_pan_pullout(float p_amount) {
2017-01-22 10:14:30 +08:00
pan_pullout = p_amount;
2017-01-22 10:14:30 +08:00
}
float AudioEffectStereoEnhance::get_pan_pullout() const {
2017-01-22 10:14:30 +08:00
return pan_pullout;
}
void AudioEffectStereoEnhance::set_time_pullout(float p_amount) {
2017-01-22 10:14:30 +08:00
time_pullout = p_amount;
2017-01-22 10:14:30 +08:00
}
float AudioEffectStereoEnhance::get_time_pullout() const {
2017-01-22 10:14:30 +08:00
return time_pullout;
}
void AudioEffectStereoEnhance::set_surround(float p_amount) {
2017-01-22 10:14:30 +08:00
surround = p_amount;
2017-01-22 10:14:30 +08:00
}
float AudioEffectStereoEnhance::get_surround() const {
2017-01-22 10:14:30 +08:00
return surround;
}
void AudioEffectStereoEnhance::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pan_pullout", "amount"), &AudioEffectStereoEnhance::set_pan_pullout);
ClassDB::bind_method(D_METHOD("get_pan_pullout"), &AudioEffectStereoEnhance::get_pan_pullout);
2017-01-22 10:14:30 +08:00
ClassDB::bind_method(D_METHOD("set_time_pullout", "amount"), &AudioEffectStereoEnhance::set_time_pullout);
ClassDB::bind_method(D_METHOD("get_time_pullout"), &AudioEffectStereoEnhance::get_time_pullout);
2017-01-22 10:14:30 +08:00
ClassDB::bind_method(D_METHOD("set_surround", "amount"), &AudioEffectStereoEnhance::set_surround);
ClassDB::bind_method(D_METHOD("get_surround"), &AudioEffectStereoEnhance::get_surround);
2017-01-22 10:14:30 +08:00
ADD_PROPERTY(PropertyInfo(Variant::REAL, "pan_pullout", PROPERTY_HINT_RANGE, "0,4,0.01"), "set_pan_pullout", "get_pan_pullout");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_pullout_ms", PROPERTY_HINT_RANGE, "0,50,0.01"), "set_time_pullout", "get_time_pullout");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "surround", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_surround", "get_surround");
2017-01-22 10:14:30 +08:00
}
AudioEffectStereoEnhance::AudioEffectStereoEnhance() {
pan_pullout = 1;
time_pullout = 0;
surround = 0;
2017-01-22 10:14:30 +08:00
}