1/**************************************************************************/
2/* audio_effect_amplify.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_amplify.h"
32
33void AudioEffectAmplifyInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
34 //multiply volume interpolating to avoid clicks if this changes
35 float volume_db = base->volume_db;
36 float vol = Math::db_to_linear(mix_volume_db);
37 float vol_inc = (Math::db_to_linear(volume_db) - vol) / float(p_frame_count);
38
39 for (int i = 0; i < p_frame_count; i++) {
40 p_dst_frames[i] = p_src_frames[i] * vol;
41 vol += vol_inc;
42 }
43 //set volume for next mix
44 mix_volume_db = volume_db;
45}
46
47Ref<AudioEffectInstance> AudioEffectAmplify::instantiate() {
48 Ref<AudioEffectAmplifyInstance> ins;
49 ins.instantiate();
50 ins->base = Ref<AudioEffectAmplify>(this);
51 ins->mix_volume_db = volume_db;
52 return ins;
53}
54
55void AudioEffectAmplify::set_volume_db(float p_volume) {
56 volume_db = p_volume;
57}
58
59float AudioEffectAmplify::get_volume_db() const {
60 return volume_db;
61}
62
63void AudioEffectAmplify::_bind_methods() {
64 ClassDB::bind_method(D_METHOD("set_volume_db", "volume"), &AudioEffectAmplify::set_volume_db);
65 ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioEffectAmplify::get_volume_db);
66
67 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01,suffix:dB"), "set_volume_db", "get_volume_db");
68}
69
70AudioEffectAmplify::AudioEffectAmplify() {
71 volume_db = 0;
72}
73