audio_effect_amplify.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*************************************************************************/
  2. /* audio_effect_amplify.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_amplify.h"
  31. void AudioEffectAmplifyInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  32. //multiply volume interpolating to avoid clicks if this changes
  33. float volume_db = base->volume_db;
  34. float vol = Math::db2linear(mix_volume_db);
  35. float vol_inc = (Math::db2linear(volume_db) - vol) / float(p_frame_count);
  36. for (int i = 0; i < p_frame_count; i++) {
  37. p_dst_frames[i] = p_src_frames[i] * vol;
  38. vol += vol_inc;
  39. }
  40. //set volume for next mix
  41. mix_volume_db = volume_db;
  42. }
  43. Ref<AudioEffectInstance> AudioEffectAmplify::instance() {
  44. Ref<AudioEffectAmplifyInstance> ins;
  45. ins.instance();
  46. ins->base = Ref<AudioEffectAmplify>(this);
  47. ins->mix_volume_db = volume_db;
  48. return ins;
  49. }
  50. void AudioEffectAmplify::set_volume_db(float p_volume) {
  51. volume_db = p_volume;
  52. }
  53. float AudioEffectAmplify::get_volume_db() const {
  54. return volume_db;
  55. }
  56. void AudioEffectAmplify::_bind_methods() {
  57. ClassDB::bind_method(D_METHOD("set_volume_db", "volume"), &AudioEffectAmplify::set_volume_db);
  58. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioEffectAmplify::get_volume_db);
  59. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), "set_volume_db", "get_volume_db");
  60. }
  61. AudioEffectAmplify::AudioEffectAmplify() {
  62. volume_db = 0;
  63. }