Skip to content

Add volume_db to the wav importer #104863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/classes/ResourceImporterWAV.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<member name="edit/trim" type="bool" setter="" getter="" default="false">
If [code]true[/code], automatically trim the beginning and end of the audio if it's lower than -50 dB after normalization (see [member edit/normalize]). This prevents having files with silence at the beginning or end, which increases their size unnecessarily and adds latency to the moment they are played back. A fade-in/fade-out period of 500 samples is also used during trimming to avoid audible pops.
</member>
<member name="edit/volume_db" type="float" setter="" getter="" default="0.0">
Adjusts the audio's volume by this many decibels. Applied after normalization.
[b]Note:[/b] Clipping may occur with positive values. A warning will be shown if this happens.
</member>
<member name="force/8_bit" type="bool" setter="" getter="" default="false">
If [code]true[/code], forces the imported audio to use 8-bit quantization if the source file is 16-bit or higher.
Enabling this is generally not recommended, as 8-bit quantization decreases audio quality significantly. If you need smaller file sizes, consider using Ogg Vorbis or MP3 audio instead.
Expand Down
1 change: 1 addition & 0 deletions editor/import/resource_importer_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void ResourceImporterWAV::get_import_options(const String &p_path, List<ImportOp
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "force/max_rate_hz", PROPERTY_HINT_RANGE, "11025,192000,1,exp"), 44100));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/trim"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/normalize"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "edit/volume_db", PROPERTY_HINT_RANGE, "-24,24,0.001,or_greater,or_less,exp"), 0.0));
// Keep the `edit/loop_mode` enum in sync with AudioStreamWAV::LoopMode (note: +1 offset due to "Detect From WAV").
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "edit/loop_mode", PROPERTY_HINT_ENUM, "Detect From WAV,Disabled,Forward,Ping-Pong,Backward", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "edit/loop_begin"), 0));
Expand Down
14 changes: 14 additions & 0 deletions scene/resources/audio_stream_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,20 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
}
}

float volume_db = p_options["edit/volume_db"];
if (volume_db != 0.0f) {
float volume_linear = Math::db_to_linear(volume_db);
float max = 0.0f;
for (int i = 0; i < data.size(); i++) {
data.write[i] *= volume_linear;
max = MAX(max, abs(data[i]));
data.write[i] = CLAMP(data[i], -1.0f, 1.0f);
}
if (max > 1.0f) {
WARN_PRINT(vformat("%f decibels of clipping occurred when importing WAV. Consider lowering \"Volume dB\" on the WAV import panel.", Math::linear_to_db(max)));
}
}

bool trim = p_options["edit/trim"];

if (trim && (loop_mode == AudioStreamWAV::LOOP_DISABLED) && format_channels > 0) {
Expand Down