Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/audio/effects/audio_effect_amplify.cpp
10278 views
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
33
void 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
47
Ref<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
55
void AudioEffectAmplify::set_volume_db(float p_volume) {
56
volume_db = p_volume;
57
}
58
59
float AudioEffectAmplify::get_volume_db() const {
60
return volume_db;
61
}
62
63
void AudioEffectAmplify::set_volume_linear(float p_volume) {
64
set_volume_db(Math::linear_to_db(p_volume));
65
}
66
67
float AudioEffectAmplify::get_volume_linear() const {
68
return Math::db_to_linear(get_volume_db());
69
}
70
71
void AudioEffectAmplify::_bind_methods() {
72
ClassDB::bind_method(D_METHOD("set_volume_db", "volume"), &AudioEffectAmplify::set_volume_db);
73
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioEffectAmplify::get_volume_db);
74
ClassDB::bind_method(D_METHOD("set_volume_linear", "volume"), &AudioEffectAmplify::set_volume_linear);
75
ClassDB::bind_method(D_METHOD("get_volume_linear"), &AudioEffectAmplify::get_volume_linear);
76
77
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01,suffix:dB"), "set_volume_db", "get_volume_db");
78
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_linear", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_volume_linear", "get_volume_linear");
79
}
80
81
AudioEffectAmplify::AudioEffectAmplify() {
82
volume_db = 0;
83
}
84
85