2017-03-05 15:47:28 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* audio_effect_compressor.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-22 22:18:56 +01:00
|
|
|
#include "audio_effect_compressor.h"
|
|
|
|
#include "servers/audio_server.h"
|
|
|
|
|
|
|
|
void AudioEffectCompressorInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
|
2022-08-13 17:45:42 +02:00
|
|
|
float threshold = Math::db_to_linear(base->threshold);
|
2017-01-22 22:18:56 +01:00
|
|
|
float sample_rate = AudioServer::get_singleton()->get_mix_rate();
|
|
|
|
|
|
|
|
float ratatcoef = exp(-1 / (0.00001f * sample_rate));
|
|
|
|
float ratrelcoef = exp(-1 / (0.5f * sample_rate));
|
|
|
|
float attime = base->attack_us / 1000000.0;
|
|
|
|
float reltime = base->release_ms / 1000.0;
|
|
|
|
float atcoef = exp(-1 / (attime * sample_rate));
|
|
|
|
float relcoef = exp(-1 / (reltime * sample_rate));
|
|
|
|
|
2022-08-13 17:45:42 +02:00
|
|
|
float makeup = Math::db_to_linear(base->gain);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
|
|
|
float mix = base->mix;
|
|
|
|
float gr_meter_decay = exp(1 / (1 * sample_rate));
|
|
|
|
|
2017-01-23 00:39:53 +01:00
|
|
|
const AudioFrame *src = p_src_frames;
|
|
|
|
|
|
|
|
if (base->sidechain != StringName() && current_channel != -1) {
|
|
|
|
int bus = AudioServer::get_singleton()->thread_find_bus_index(base->sidechain);
|
|
|
|
if (bus >= 0) {
|
|
|
|
src = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus, current_channel);
|
|
|
|
}
|
|
|
|
}
|
2017-01-22 22:18:56 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < p_frame_count; i++) {
|
2017-01-23 00:39:53 +01:00
|
|
|
AudioFrame s = src[i];
|
2017-01-22 22:18:56 +01:00
|
|
|
//convert to positive
|
2024-01-09 15:01:12 +01:00
|
|
|
s.left = Math::abs(s.left);
|
|
|
|
s.right = Math::abs(s.right);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2024-01-09 15:01:12 +01:00
|
|
|
float peak = MAX(s.left, s.right);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2022-08-13 17:45:42 +02:00
|
|
|
float overdb = 2.08136898f * Math::linear_to_db(peak / threshold);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (overdb < 0.0) { //we only care about what goes over to compress
|
2017-01-22 22:18:56 +01:00
|
|
|
overdb = 0.0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (overdb - rundb > 5) { // diffeence is too large
|
2017-01-22 22:18:56 +01:00
|
|
|
averatio = 4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-22 22:18:56 +01:00
|
|
|
|
|
|
|
if (overdb > rundb) {
|
|
|
|
rundb = overdb + atcoef * (rundb - overdb);
|
|
|
|
runratio = averatio + ratatcoef * (runratio - averatio);
|
|
|
|
} else {
|
|
|
|
rundb = overdb + relcoef * (rundb - overdb);
|
|
|
|
runratio = averatio + ratrelcoef * (runratio - averatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
overdb = rundb;
|
|
|
|
averatio = runratio;
|
|
|
|
|
|
|
|
float cratio;
|
|
|
|
|
|
|
|
if (false) { //rato all-in
|
|
|
|
cratio = 12 + averatio;
|
|
|
|
} else {
|
|
|
|
cratio = base->ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
float gr = -overdb * (cratio - 1) / cratio;
|
2022-08-13 17:45:42 +02:00
|
|
|
float grv = Math::db_to_linear(gr);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
|
|
|
runmax = maxover + relcoef * (runmax - maxover); // highest peak for setting att/rel decays in reltime
|
|
|
|
maxover = runmax;
|
|
|
|
|
|
|
|
if (grv < gr_meter) {
|
|
|
|
gr_meter = grv;
|
|
|
|
} else {
|
|
|
|
gr_meter *= gr_meter_decay;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (gr_meter > 1) {
|
2017-01-22 22:18:56 +01:00
|
|
|
gr_meter = 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-22 22:18:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p_dst_frames[i] = p_src_frames[i] * grv * makeup * mix + p_src_frames[i] * (1.0 - mix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
Ref<AudioEffectInstance> AudioEffectCompressor::instantiate() {
|
2017-01-22 22:18:56 +01:00
|
|
|
Ref<AudioEffectCompressorInstance> ins;
|
2021-06-18 00:03:09 +02:00
|
|
|
ins.instantiate();
|
2017-01-22 22:18:56 +01:00
|
|
|
ins->base = Ref<AudioEffectCompressor>(this);
|
|
|
|
ins->rundb = 0;
|
|
|
|
ins->runratio = 0;
|
|
|
|
ins->averatio = 0;
|
|
|
|
ins->runmax = 0;
|
|
|
|
ins->maxover = 0;
|
|
|
|
ins->gr_meter = 1.0;
|
2017-01-23 00:39:53 +01:00
|
|
|
ins->current_channel = -1;
|
2017-01-22 22:18:56 +01:00
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
2017-07-08 17:12:18 +02:00
|
|
|
void AudioEffectCompressor::set_threshold(float p_threshold) {
|
|
|
|
threshold = p_threshold;
|
2017-01-22 22:18:56 +01:00
|
|
|
}
|
|
|
|
|
2017-07-08 17:12:18 +02:00
|
|
|
float AudioEffectCompressor::get_threshold() const {
|
|
|
|
return threshold;
|
2017-01-22 22:18:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectCompressor::set_ratio(float p_ratio) {
|
|
|
|
ratio = p_ratio;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-22 22:18:56 +01:00
|
|
|
float AudioEffectCompressor::get_ratio() const {
|
|
|
|
return ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectCompressor::set_gain(float p_gain) {
|
|
|
|
gain = p_gain;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-22 22:18:56 +01:00
|
|
|
float AudioEffectCompressor::get_gain() const {
|
|
|
|
return gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectCompressor::set_attack_us(float p_attack_us) {
|
|
|
|
attack_us = p_attack_us;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-22 22:18:56 +01:00
|
|
|
float AudioEffectCompressor::get_attack_us() const {
|
|
|
|
return attack_us;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectCompressor::set_release_ms(float p_release_ms) {
|
|
|
|
release_ms = p_release_ms;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-22 22:18:56 +01:00
|
|
|
float AudioEffectCompressor::get_release_ms() const {
|
|
|
|
return release_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEffectCompressor::set_mix(float p_mix) {
|
|
|
|
mix = p_mix;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-22 22:18:56 +01:00
|
|
|
float AudioEffectCompressor::get_mix() const {
|
|
|
|
return mix;
|
|
|
|
}
|
|
|
|
|
2017-01-23 00:39:53 +01:00
|
|
|
void AudioEffectCompressor::set_sidechain(const StringName &p_sidechain) {
|
|
|
|
AudioServer::get_singleton()->lock();
|
|
|
|
sidechain = p_sidechain;
|
|
|
|
AudioServer::get_singleton()->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringName AudioEffectCompressor::get_sidechain() const {
|
|
|
|
return sidechain;
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:57:11 +02:00
|
|
|
void AudioEffectCompressor::_validate_property(PropertyInfo &p_property) const {
|
|
|
|
if (p_property.name == "sidechain") {
|
2017-01-23 00:39:53 +01:00
|
|
|
String buses = "";
|
|
|
|
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
|
|
|
|
buses += ",";
|
|
|
|
buses += AudioServer::get_singleton()->get_bus_name(i);
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:57:11 +02:00
|
|
|
p_property.hint_string = buses;
|
2017-01-23 00:39:53 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-22 22:18:56 +01:00
|
|
|
|
|
|
|
void AudioEffectCompressor::_bind_methods() {
|
2017-07-08 17:12:18 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_threshold", "threshold"), &AudioEffectCompressor::set_threshold);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_threshold"), &AudioEffectCompressor::get_threshold);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AudioEffectCompressor::set_ratio);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_ratio"), &AudioEffectCompressor::get_ratio);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_gain", "gain"), &AudioEffectCompressor::set_gain);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_gain"), &AudioEffectCompressor::get_gain);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_attack_us", "attack_us"), &AudioEffectCompressor::set_attack_us);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_attack_us"), &AudioEffectCompressor::get_attack_us);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_release_ms", "release_ms"), &AudioEffectCompressor::set_release_ms);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_release_ms"), &AudioEffectCompressor::get_release_ms);
|
2017-01-22 22:18:56 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_mix", "mix"), &AudioEffectCompressor::set_mix);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mix"), &AudioEffectCompressor::get_mix);
|
2017-01-23 00:39:53 +01:00
|
|
|
|
2017-02-12 01:11:37 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_sidechain", "sidechain"), &AudioEffectCompressor::set_sidechain);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_sidechain"), &AudioEffectCompressor::get_sidechain);
|
2017-01-22 22:18:56 +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, "threshold", PROPERTY_HINT_RANGE, "-60,0,0.1"), "set_threshold", "get_threshold");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "1,48,0.1"), "set_ratio", "get_ratio");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gain", PROPERTY_HINT_RANGE, "-20,20,0.1"), "set_gain", "get_gain");
|
2022-05-20 07:24:41 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attack_us", PROPERTY_HINT_RANGE, U"20,2000,1,suffix:\u00B5s"), "set_attack_us", "get_attack_us");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "release_ms", PROPERTY_HINT_RANGE, "20,2000,1,suffix:ms"), "set_release_ms", "get_release_ms");
|
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, "mix", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_mix", "get_mix");
|
2020-02-20 22:58:05 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "sidechain", PROPERTY_HINT_ENUM), "set_sidechain", "get_sidechain");
|
2017-01-22 22:18:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioEffectCompressor::AudioEffectCompressor() {
|
2017-07-08 17:12:18 +02:00
|
|
|
threshold = 0;
|
2017-01-22 22:18:56 +01:00
|
|
|
ratio = 4;
|
|
|
|
gain = 0;
|
|
|
|
attack_us = 20;
|
|
|
|
release_ms = 250;
|
|
|
|
mix = 1;
|
|
|
|
}
|