2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_effect_filter.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 15:47:28 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
#include "audio_effect_filter.h"
|
|
|
|
#include "servers/audio_server.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <int S>
|
|
|
|
void AudioEffectFilterInstance::_process_filter(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
|
|
|
|
for (int i = 0; i < p_frame_count; i++) {
|
2017-01-21 23:00:25 +01:00
|
|
|
float f = p_src_frames[i].l;
|
|
|
|
filter_process[0][0].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (S > 1) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[0][1].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (S > 2) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[0][2].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (S > 3) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[0][3].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dst_frames[i].l = f;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_frame_count; i++) {
|
2017-01-21 23:00:25 +01:00
|
|
|
float f = p_src_frames[i].r;
|
|
|
|
filter_process[1][0].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (S > 1) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[1][1].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (S > 2) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[1][2].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (S > 3) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[1][3].process_one(f);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dst_frames[i].r = f;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectFilterInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter.set_cutoff(base->cutoff);
|
|
|
|
filter.set_gain(base->gain);
|
|
|
|
filter.set_resonance(base->resonance);
|
|
|
|
filter.set_mode(base->mode);
|
2017-03-05 16:44:50 +01:00
|
|
|
int stages = int(base->db) + 1;
|
2017-01-21 23:00:25 +01:00
|
|
|
filter.set_stages(stages);
|
|
|
|
filter.set_sampling_rate(AudioServer::get_singleton()->get_mix_rate());
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 4; j++) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[i][j].update_coeffs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (stages == 1) {
|
|
|
|
_process_filter<1>(p_src_frames, p_dst_frames, p_frame_count);
|
|
|
|
} else if (stages == 2) {
|
|
|
|
_process_filter<2>(p_src_frames, p_dst_frames, p_frame_count);
|
|
|
|
} else if (stages == 3) {
|
|
|
|
_process_filter<3>(p_src_frames, p_dst_frames, p_frame_count);
|
|
|
|
} else if (stages == 4) {
|
|
|
|
_process_filter<4>(p_src_frames, p_dst_frames, p_frame_count);
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioEffectFilterInstance::AudioEffectFilterInstance() {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 4; j++) {
|
2017-01-21 23:00:25 +01:00
|
|
|
filter_process[i][j].set_filter(&filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioEffectInstance> AudioEffectFilter::instance() {
|
|
|
|
Ref<AudioEffectFilterInstance> ins;
|
|
|
|
ins.instance();
|
2017-03-05 16:44:50 +01:00
|
|
|
ins->base = Ref<AudioEffectFilter>(this);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectFilter::set_cutoff(float p_freq) {
|
2017-03-05 16:44:50 +01:00
|
|
|
cutoff = p_freq;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float AudioEffectFilter::get_cutoff() const {
|
2017-01-21 23:00:25 +01:00
|
|
|
return cutoff;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectFilter::set_resonance(float p_amount) {
|
|
|
|
resonance = p_amount;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float AudioEffectFilter::get_resonance() const {
|
2017-01-21 23:00:25 +01:00
|
|
|
return resonance;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void AudioEffectFilter::set_gain(float p_amount) {
|
|
|
|
gain = p_amount;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
float AudioEffectFilter::get_gain() const {
|
|
|
|
return gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectFilter::set_db(FilterDB p_db) {
|
2017-03-05 16:44:50 +01:00
|
|
|
db = p_db;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioEffectFilter::FilterDB AudioEffectFilter::get_db() const {
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectFilter::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_cutoff", "freq"), &AudioEffectFilter::set_cutoff);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cutoff"), &AudioEffectFilter::get_cutoff);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_resonance", "amount"), &AudioEffectFilter::set_resonance);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_resonance"), &AudioEffectFilter::get_resonance);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_gain", "amount"), &AudioEffectFilter::set_gain);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_gain"), &AudioEffectFilter::get_gain);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_db", "amount"), &AudioEffectFilter::set_db);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_db"), &AudioEffectFilter::get_db);
|
2017-01-21 23:00:25 +01:00
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cutoff_hz", PROPERTY_HINT_RANGE, "1,20500,1"), "set_cutoff", "get_cutoff");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "resonance", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_resonance", "get_resonance");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gain", PROPERTY_HINT_RANGE, "0,4,0.01"), "set_gain", "get_gain");
|
2017-10-22 06:36:43 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "db", PROPERTY_HINT_ENUM, "6 dB,12 dB,18 dB,24 dB"), "set_db", "get_db");
|
2017-09-12 21:09:06 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(FILTER_6DB);
|
|
|
|
BIND_ENUM_CONSTANT(FILTER_12DB);
|
|
|
|
BIND_ENUM_CONSTANT(FILTER_18DB);
|
|
|
|
BIND_ENUM_CONSTANT(FILTER_24DB);
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioEffectFilter::AudioEffectFilter(AudioFilterSW::Mode p_mode) {
|
|
|
|
mode = p_mode;
|
|
|
|
cutoff = 2000;
|
|
|
|
resonance = 0.5;
|
|
|
|
gain = 1.0;
|
|
|
|
db = FILTER_6DB;
|
2017-01-21 23:00:25 +01:00
|
|
|
}
|