| 1 | /**************************************************************************/ |
| 2 | /* audio_effect_stereo_enhance.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "audio_effect_stereo_enhance.h" |
| 32 | |
| 33 | #include "servers/audio_server.h" |
| 34 | |
| 35 | void AudioEffectStereoEnhanceInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { |
| 36 | float intensity = base->pan_pullout; |
| 37 | bool surround_mode = base->surround > 0; |
| 38 | float surround_amount = base->surround; |
| 39 | unsigned int delay_frames = (base->time_pullout / 1000.0) * AudioServer::get_singleton()->get_mix_rate(); |
| 40 | |
| 41 | for (int i = 0; i < p_frame_count; i++) { |
| 42 | float l = p_src_frames[i].l; |
| 43 | float r = p_src_frames[i].r; |
| 44 | |
| 45 | float center = (l + r) / 2.0f; |
| 46 | |
| 47 | l = (center + (l - center) * intensity); |
| 48 | r = (center + (r - center) * intensity); |
| 49 | |
| 50 | if (surround_mode) { |
| 51 | float val = (l + r) / 2.0; |
| 52 | |
| 53 | delay_ringbuff[ringbuff_pos & ringbuff_mask] = val; |
| 54 | |
| 55 | float out = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask] * surround_amount; |
| 56 | |
| 57 | l += out; |
| 58 | r += -out; |
| 59 | } else { |
| 60 | float val = r; |
| 61 | |
| 62 | delay_ringbuff[ringbuff_pos & ringbuff_mask] = val; |
| 63 | |
| 64 | //r is delayed |
| 65 | r = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask]; |
| 66 | } |
| 67 | |
| 68 | p_dst_frames[i].l = l; |
| 69 | p_dst_frames[i].r = r; |
| 70 | ringbuff_pos++; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | AudioEffectStereoEnhanceInstance::~AudioEffectStereoEnhanceInstance() { |
| 75 | memdelete_arr(delay_ringbuff); |
| 76 | } |
| 77 | |
| 78 | Ref<AudioEffectInstance> AudioEffectStereoEnhance::instantiate() { |
| 79 | Ref<AudioEffectStereoEnhanceInstance> ins; |
| 80 | ins.instantiate(); |
| 81 | |
| 82 | ins->base = Ref<AudioEffectStereoEnhance>(this); |
| 83 | |
| 84 | float ring_buffer_max_size = AudioEffectStereoEnhanceInstance::MAX_DELAY_MS + 2; |
| 85 | ring_buffer_max_size /= 1000.0; //convert to seconds |
| 86 | ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate(); |
| 87 | |
| 88 | int ringbuff_size = (int)ring_buffer_max_size; |
| 89 | |
| 90 | int bits = 0; |
| 91 | |
| 92 | while (ringbuff_size > 0) { |
| 93 | bits++; |
| 94 | ringbuff_size /= 2; |
| 95 | } |
| 96 | |
| 97 | ringbuff_size = 1 << bits; |
| 98 | ins->ringbuff_mask = ringbuff_size - 1; |
| 99 | ins->ringbuff_pos = 0; |
| 100 | |
| 101 | ins->delay_ringbuff = memnew_arr(float, ringbuff_size); |
| 102 | |
| 103 | return ins; |
| 104 | } |
| 105 | |
| 106 | void AudioEffectStereoEnhance::set_pan_pullout(float p_amount) { |
| 107 | pan_pullout = p_amount; |
| 108 | } |
| 109 | |
| 110 | float AudioEffectStereoEnhance::get_pan_pullout() const { |
| 111 | return pan_pullout; |
| 112 | } |
| 113 | |
| 114 | void AudioEffectStereoEnhance::set_time_pullout(float p_amount) { |
| 115 | time_pullout = p_amount; |
| 116 | } |
| 117 | |
| 118 | float AudioEffectStereoEnhance::get_time_pullout() const { |
| 119 | return time_pullout; |
| 120 | } |
| 121 | |
| 122 | void AudioEffectStereoEnhance::set_surround(float p_amount) { |
| 123 | surround = p_amount; |
| 124 | } |
| 125 | |
| 126 | float AudioEffectStereoEnhance::get_surround() const { |
| 127 | return surround; |
| 128 | } |
| 129 | |
| 130 | void AudioEffectStereoEnhance::_bind_methods() { |
| 131 | ClassDB::bind_method(D_METHOD("set_pan_pullout" , "amount" ), &AudioEffectStereoEnhance::set_pan_pullout); |
| 132 | ClassDB::bind_method(D_METHOD("get_pan_pullout" ), &AudioEffectStereoEnhance::get_pan_pullout); |
| 133 | |
| 134 | ClassDB::bind_method(D_METHOD("set_time_pullout" , "amount" ), &AudioEffectStereoEnhance::set_time_pullout); |
| 135 | ClassDB::bind_method(D_METHOD("get_time_pullout" ), &AudioEffectStereoEnhance::get_time_pullout); |
| 136 | |
| 137 | ClassDB::bind_method(D_METHOD("set_surround" , "amount" ), &AudioEffectStereoEnhance::set_surround); |
| 138 | ClassDB::bind_method(D_METHOD("get_surround" ), &AudioEffectStereoEnhance::get_surround); |
| 139 | |
| 140 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pan_pullout" , PROPERTY_HINT_RANGE, "0,4,0.01" ), "set_pan_pullout" , "get_pan_pullout" ); |
| 141 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_pullout_ms" , PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms" ), "set_time_pullout" , "get_time_pullout" ); |
| 142 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "surround" , PROPERTY_HINT_RANGE, "0,1,0.01" ), "set_surround" , "get_surround" ); |
| 143 | } |
| 144 | |
| 145 | AudioEffectStereoEnhance::AudioEffectStereoEnhance() {} |
| 146 | |