diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-04-07 12:49:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-07 12:49:23 +0200 |
commit | 9984ebba173194c68443c9d9a8020800144488e3 (patch) | |
tree | b313d82c3673b89066c575ec8df038af18c10a67 | |
parent | c562a7d149e680a72f7e5dfc9cf4ce3d61c468c4 (diff) | |
parent | 26cc521d5592a7b807688a7ce6c27a2454cbf486 (diff) |
Merge pull request #27645 from theisegeberg/fix-wav-import-trim
Fix end pops when trimming wav files
-rw-r--r-- | editor/import/resource_importer_wav.cpp | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/editor/import/resource_importer_wav.cpp b/editor/import/resource_importer_wav.cpp index 857d8992fd..52dec47343 100644 --- a/editor/import/resource_importer_wav.cpp +++ b/editor/import/resource_importer_wav.cpp @@ -35,6 +35,9 @@ #include "core/os/file_access.h" #include "scene/resources/audio_stream_sample.h" +const float TRIM_DB_LIMIT = -50; +const int TRIM_FADE_OUT_FRAMES = 500; + String ResourceImporterWAV::get_importer_name() const { return "wav"; @@ -393,31 +396,42 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s if (trim && !loop && format_channels > 0) { int first = 0; - int last = (frames * format_channels) - 1; + int last = (frames / format_channels) - 1; bool found = false; - float limit = Math::db2linear((float)-30); - for (int i = 0; i < data.size(); i++) { - float amp = Math::abs(data[i]); + float limit = Math::db2linear(TRIM_DB_LIMIT); + + for (int i = 0; i < data.size() / format_channels; i++) { + float ampChannelSum = 0; + for (int j = 0; j < format_channels; j++) { + ampChannelSum += Math::abs(data[(i * format_channels) + j]); + } + + float amp = Math::abs(ampChannelSum / (float)format_channels); if (!found && amp > limit) { - first = i; + first = i / format_channels; found = true; } if (found && amp > limit) { - last = i; + last = i / format_channels; } } - first /= format_channels; - last /= format_channels; - if (first < last) { - Vector<float> new_data; - new_data.resize((last - first + 1) * format_channels); - for (int i = first * format_channels; i < (last + 1) * format_channels; i++) { - new_data.write[i - first * format_channels] = data[i]; + new_data.resize((last - first) * format_channels); + for (int i = first; i < last; i++) { + + float fadeOutMult = 1; + + if (last - i < TRIM_FADE_OUT_FRAMES) { + fadeOutMult = ((float)(last - i - 1) / (float)TRIM_FADE_OUT_FRAMES); + } + + for (int j = 0; j < format_channels; j++) { + new_data.write[((i - first) * format_channels) + j] = data[(i * format_channels) + j] * fadeOutMult; + } } data = new_data; |