audio_effect_chorus.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**************************************************************************/
  2. /* audio_effect_chorus.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. #include "audio_effect_chorus.h"
  31. #include "core/math/math_funcs.h"
  32. #include "servers/audio_server.h"
  33. void AudioEffectChorusInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  34. int todo = p_frame_count;
  35. while (todo) {
  36. int to_mix = MIN(todo, 256); //can't mix too much
  37. _process_chunk(p_src_frames, p_dst_frames, to_mix);
  38. p_src_frames += to_mix;
  39. p_dst_frames += to_mix;
  40. todo -= to_mix;
  41. }
  42. }
  43. void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  44. //fill ringbuffer
  45. for (int i = 0; i < p_frame_count; i++) {
  46. audio_buffer.write[(buffer_pos + i) & buffer_mask] = p_src_frames[i];
  47. p_dst_frames[i] = p_src_frames[i] * base->dry;
  48. }
  49. float mix_rate = AudioServer::get_singleton()->get_mix_rate();
  50. /* process voices */
  51. for (int vc = 0; vc < base->voice_count; vc++) {
  52. AudioEffectChorus::Voice &v = base->voice[vc];
  53. double time_to_mix = (float)p_frame_count / mix_rate;
  54. double cycles_to_mix = time_to_mix * v.rate;
  55. unsigned int local_rb_pos = buffer_pos;
  56. AudioFrame *dst_buff = p_dst_frames;
  57. AudioFrame *rb_buff = audio_buffer.ptrw();
  58. double delay_msec = v.delay;
  59. unsigned int delay_frames = Math::fast_ftoi((delay_msec / 1000.0) * mix_rate);
  60. float max_depth_frames = (v.depth / 1000.0) * mix_rate;
  61. uint64_t local_cycles = cycles[vc];
  62. uint64_t increment = llrint(cycles_to_mix / (double)p_frame_count * (double)(1 << AudioEffectChorus::CYCLES_FRAC));
  63. //check the LFO doesn't read ahead of the write pos
  64. if ((((unsigned int)max_depth_frames) + 10) > delay_frames) { //10 as some threshold to avoid precision stuff
  65. delay_frames += (int)max_depth_frames - delay_frames;
  66. delay_frames += 10; //threshold to avoid precision stuff
  67. }
  68. //low pass filter
  69. if (v.cutoff == 0) {
  70. continue;
  71. }
  72. float auxlp = expf(-Math_TAU * v.cutoff / mix_rate);
  73. float c1 = 1.0 - auxlp;
  74. float c2 = auxlp;
  75. AudioFrame h = filter_h[vc];
  76. if (v.cutoff >= AudioEffectChorus::MS_CUTOFF_MAX) {
  77. c1 = 1.0;
  78. c2 = 0.0;
  79. }
  80. //vol modifier
  81. AudioFrame vol_modifier = AudioFrame(base->wet, base->wet) * Math::db_to_linear(v.level);
  82. vol_modifier.l *= CLAMP(1.0 - v.pan, 0, 1);
  83. vol_modifier.r *= CLAMP(1.0 + v.pan, 0, 1);
  84. for (int i = 0; i < p_frame_count; i++) {
  85. /** COMPUTE WAVEFORM **/
  86. float phase = (float)(local_cycles & AudioEffectChorus::CYCLES_MASK) / (float)(1 << AudioEffectChorus::CYCLES_FRAC);
  87. float wave_delay = sinf(phase * Math_TAU) * max_depth_frames;
  88. int wave_delay_frames = lrint(floor(wave_delay));
  89. float wave_delay_frac = wave_delay - (float)wave_delay_frames;
  90. /** COMPUTE RINGBUFFER POS**/
  91. unsigned int rb_source = local_rb_pos;
  92. rb_source -= delay_frames;
  93. rb_source -= wave_delay_frames;
  94. /** READ FROM RINGBUFFER, LINEARLY INTERPOLATE */
  95. AudioFrame val = rb_buff[rb_source & buffer_mask];
  96. AudioFrame val_next = rb_buff[(rb_source - 1) & buffer_mask];
  97. val += (val_next - val) * wave_delay_frac;
  98. val = val * c1 + h * c2;
  99. h = val;
  100. /** MIX VALUE TO OUTPUT **/
  101. dst_buff[i] += val * vol_modifier;
  102. local_cycles += increment;
  103. local_rb_pos++;
  104. }
  105. filter_h[vc] = h;
  106. cycles[vc] += Math::fast_ftoi(cycles_to_mix * (double)(1 << AudioEffectChorus::CYCLES_FRAC));
  107. }
  108. buffer_pos += p_frame_count;
  109. }
  110. Ref<AudioEffectInstance> AudioEffectChorus::instantiate() {
  111. Ref<AudioEffectChorusInstance> ins;
  112. ins.instantiate();
  113. ins->base = Ref<AudioEffectChorus>(this);
  114. for (int i = 0; i < 4; i++) {
  115. ins->filter_h[i] = AudioFrame(0, 0);
  116. ins->cycles[i] = 0;
  117. }
  118. float ring_buffer_max_size = AudioEffectChorus::MAX_DELAY_MS + AudioEffectChorus::MAX_DEPTH_MS + AudioEffectChorus::MAX_WIDTH_MS;
  119. ring_buffer_max_size *= 2; //just to avoid complications
  120. ring_buffer_max_size /= 1000.0; //convert to seconds
  121. ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
  122. int ringbuff_size = ring_buffer_max_size;
  123. int bits = 0;
  124. while (ringbuff_size > 0) {
  125. bits++;
  126. ringbuff_size /= 2;
  127. }
  128. ringbuff_size = 1 << bits;
  129. ins->buffer_mask = ringbuff_size - 1;
  130. ins->buffer_pos = 0;
  131. ins->audio_buffer.resize(ringbuff_size);
  132. for (int i = 0; i < ringbuff_size; i++) {
  133. ins->audio_buffer.write[i] = AudioFrame(0, 0);
  134. }
  135. return ins;
  136. }
  137. void AudioEffectChorus::set_voice_count(int p_voices) {
  138. ERR_FAIL_COND(p_voices < 1 || p_voices > MAX_VOICES);
  139. voice_count = p_voices;
  140. }
  141. int AudioEffectChorus::get_voice_count() const {
  142. return voice_count;
  143. }
  144. void AudioEffectChorus::set_voice_delay_ms(int p_voice, float p_delay_ms) {
  145. ERR_FAIL_INDEX(p_voice, MAX_VOICES);
  146. voice[p_voice].delay = p_delay_ms;
  147. }
  148. float AudioEffectChorus::get_voice_delay_ms(int p_voice) const {
  149. ERR_FAIL_INDEX_V(p_voice, MAX_VOICES, 0);
  150. return voice[p_voice].delay;
  151. }
  152. void AudioEffectChorus::set_voice_rate_hz(int p_voice, float p_rate_hz) {
  153. ERR_FAIL_INDEX(p_voice, MAX_VOICES);
  154. voice[p_voice].rate = p_rate_hz;
  155. }
  156. float AudioEffectChorus::get_voice_rate_hz(int p_voice) const {
  157. ERR_FAIL_INDEX_V(p_voice, MAX_VOICES, 0);
  158. return voice[p_voice].rate;
  159. }
  160. void AudioEffectChorus::set_voice_depth_ms(int p_voice, float p_depth_ms) {
  161. ERR_FAIL_INDEX(p_voice, MAX_VOICES);
  162. voice[p_voice].depth = p_depth_ms;
  163. }
  164. float AudioEffectChorus::get_voice_depth_ms(int p_voice) const {
  165. ERR_FAIL_INDEX_V(p_voice, MAX_VOICES, 0);
  166. return voice[p_voice].depth;
  167. }
  168. void AudioEffectChorus::set_voice_level_db(int p_voice, float p_level_db) {
  169. ERR_FAIL_INDEX(p_voice, MAX_VOICES);
  170. voice[p_voice].level = p_level_db;
  171. }
  172. float AudioEffectChorus::get_voice_level_db(int p_voice) const {
  173. ERR_FAIL_INDEX_V(p_voice, MAX_VOICES, 0);
  174. return voice[p_voice].level;
  175. }
  176. void AudioEffectChorus::set_voice_cutoff_hz(int p_voice, float p_cutoff_hz) {
  177. ERR_FAIL_INDEX(p_voice, MAX_VOICES);
  178. voice[p_voice].cutoff = p_cutoff_hz;
  179. }
  180. float AudioEffectChorus::get_voice_cutoff_hz(int p_voice) const {
  181. ERR_FAIL_INDEX_V(p_voice, MAX_VOICES, 0);
  182. return voice[p_voice].cutoff;
  183. }
  184. void AudioEffectChorus::set_voice_pan(int p_voice, float p_pan) {
  185. ERR_FAIL_INDEX(p_voice, MAX_VOICES);
  186. voice[p_voice].pan = p_pan;
  187. }
  188. float AudioEffectChorus::get_voice_pan(int p_voice) const {
  189. ERR_FAIL_INDEX_V(p_voice, MAX_VOICES, 0);
  190. return voice[p_voice].pan;
  191. }
  192. void AudioEffectChorus::set_wet(float amount) {
  193. wet = amount;
  194. }
  195. float AudioEffectChorus::get_wet() const {
  196. return wet;
  197. }
  198. void AudioEffectChorus::set_dry(float amount) {
  199. dry = amount;
  200. }
  201. float AudioEffectChorus::get_dry() const {
  202. return dry;
  203. }
  204. void AudioEffectChorus::_validate_property(PropertyInfo &p_property) const {
  205. if (p_property.name.begins_with("voice/")) {
  206. int voice_idx = p_property.name.get_slice("/", 1).to_int();
  207. if (voice_idx > voice_count) {
  208. p_property.usage = PROPERTY_USAGE_NONE;
  209. }
  210. }
  211. }
  212. void AudioEffectChorus::_bind_methods() {
  213. ClassDB::bind_method(D_METHOD("set_voice_count", "voices"), &AudioEffectChorus::set_voice_count);
  214. ClassDB::bind_method(D_METHOD("get_voice_count"), &AudioEffectChorus::get_voice_count);
  215. ClassDB::bind_method(D_METHOD("set_voice_delay_ms", "voice_idx", "delay_ms"), &AudioEffectChorus::set_voice_delay_ms);
  216. ClassDB::bind_method(D_METHOD("get_voice_delay_ms", "voice_idx"), &AudioEffectChorus::get_voice_delay_ms);
  217. ClassDB::bind_method(D_METHOD("set_voice_rate_hz", "voice_idx", "rate_hz"), &AudioEffectChorus::set_voice_rate_hz);
  218. ClassDB::bind_method(D_METHOD("get_voice_rate_hz", "voice_idx"), &AudioEffectChorus::get_voice_rate_hz);
  219. ClassDB::bind_method(D_METHOD("set_voice_depth_ms", "voice_idx", "depth_ms"), &AudioEffectChorus::set_voice_depth_ms);
  220. ClassDB::bind_method(D_METHOD("get_voice_depth_ms", "voice_idx"), &AudioEffectChorus::get_voice_depth_ms);
  221. ClassDB::bind_method(D_METHOD("set_voice_level_db", "voice_idx", "level_db"), &AudioEffectChorus::set_voice_level_db);
  222. ClassDB::bind_method(D_METHOD("get_voice_level_db", "voice_idx"), &AudioEffectChorus::get_voice_level_db);
  223. ClassDB::bind_method(D_METHOD("set_voice_cutoff_hz", "voice_idx", "cutoff_hz"), &AudioEffectChorus::set_voice_cutoff_hz);
  224. ClassDB::bind_method(D_METHOD("get_voice_cutoff_hz", "voice_idx"), &AudioEffectChorus::get_voice_cutoff_hz);
  225. ClassDB::bind_method(D_METHOD("set_voice_pan", "voice_idx", "pan"), &AudioEffectChorus::set_voice_pan);
  226. ClassDB::bind_method(D_METHOD("get_voice_pan", "voice_idx"), &AudioEffectChorus::get_voice_pan);
  227. ClassDB::bind_method(D_METHOD("set_wet", "amount"), &AudioEffectChorus::set_wet);
  228. ClassDB::bind_method(D_METHOD("get_wet"), &AudioEffectChorus::get_wet);
  229. ClassDB::bind_method(D_METHOD("set_dry", "amount"), &AudioEffectChorus::set_dry);
  230. ClassDB::bind_method(D_METHOD("get_dry"), &AudioEffectChorus::get_dry);
  231. ADD_PROPERTY(PropertyInfo(Variant::INT, "voice_count", PROPERTY_HINT_RANGE, "1,4,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_voice_count", "get_voice_count");
  232. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dry", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_dry", "get_dry");
  233. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wet", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_wet", "get_wet");
  234. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/1/delay_ms", PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms"), "set_voice_delay_ms", "get_voice_delay_ms", 0);
  235. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/1/rate_hz", PROPERTY_HINT_RANGE, "0.1,20,0.1,suffix:Hz"), "set_voice_rate_hz", "get_voice_rate_hz", 0);
  236. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/1/depth_ms", PROPERTY_HINT_RANGE, "0,20,0.01,suffix:ms"), "set_voice_depth_ms", "get_voice_depth_ms", 0);
  237. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/1/level_db", PROPERTY_HINT_RANGE, "-60,24,0.1,suffix:dB"), "set_voice_level_db", "get_voice_level_db", 0);
  238. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/1/cutoff_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_voice_cutoff_hz", "get_voice_cutoff_hz", 0);
  239. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/1/pan", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_voice_pan", "get_voice_pan", 0);
  240. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/2/delay_ms", PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms"), "set_voice_delay_ms", "get_voice_delay_ms", 1);
  241. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/2/rate_hz", PROPERTY_HINT_RANGE, "0.1,20,0.1,suffix:Hz"), "set_voice_rate_hz", "get_voice_rate_hz", 1);
  242. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/2/depth_ms", PROPERTY_HINT_RANGE, "0,20,0.01,suffix:ms"), "set_voice_depth_ms", "get_voice_depth_ms", 1);
  243. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/2/level_db", PROPERTY_HINT_RANGE, "-60,24,0.1,suffix:dB"), "set_voice_level_db", "get_voice_level_db", 1);
  244. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/2/cutoff_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_voice_cutoff_hz", "get_voice_cutoff_hz", 1);
  245. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/2/pan", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_voice_pan", "get_voice_pan", 1);
  246. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/3/delay_ms", PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms"), "set_voice_delay_ms", "get_voice_delay_ms", 2);
  247. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/3/rate_hz", PROPERTY_HINT_RANGE, "0.1,20,0.1,suffix:Hz"), "set_voice_rate_hz", "get_voice_rate_hz", 2);
  248. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/3/depth_ms", PROPERTY_HINT_RANGE, "0,20,0.01,suffix:ms"), "set_voice_depth_ms", "get_voice_depth_ms", 2);
  249. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/3/level_db", PROPERTY_HINT_RANGE, "-60,24,0.1,suffix:dB"), "set_voice_level_db", "get_voice_level_db", 2);
  250. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/3/cutoff_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_voice_cutoff_hz", "get_voice_cutoff_hz", 2);
  251. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/3/pan", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_voice_pan", "get_voice_pan", 2);
  252. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/4/delay_ms", PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms"), "set_voice_delay_ms", "get_voice_delay_ms", 3);
  253. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/4/rate_hz", PROPERTY_HINT_RANGE, "0.1,20,0.1,suffix:Hz"), "set_voice_rate_hz", "get_voice_rate_hz", 3);
  254. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/4/depth_ms", PROPERTY_HINT_RANGE, "0,20,0.01,suffix:ms"), "set_voice_depth_ms", "get_voice_depth_ms", 3);
  255. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/4/level_db", PROPERTY_HINT_RANGE, "-60,24,0.1,suffix:dB"), "set_voice_level_db", "get_voice_level_db", 3);
  256. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/4/cutoff_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_voice_cutoff_hz", "get_voice_cutoff_hz", 3);
  257. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "voice/4/pan", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_voice_pan", "get_voice_pan", 3);
  258. }
  259. AudioEffectChorus::AudioEffectChorus() {
  260. voice_count = 2;
  261. voice[0].delay = 15;
  262. voice[1].delay = 20;
  263. voice[0].rate = 0.8;
  264. voice[1].rate = 1.2;
  265. voice[0].depth = 2;
  266. voice[1].depth = 3;
  267. voice[0].cutoff = 8000;
  268. voice[1].cutoff = 8000;
  269. voice[0].pan = -0.5;
  270. voice[1].pan = 0.5;
  271. wet = 0.5;
  272. dry = 1.0;
  273. }