diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 78783a0c..6bd4e35a 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -355,7 +355,6 @@ sfz::FileDataHolder sfz::FilePool::loadFile(const FileId& fileId) noexcept *fileInformation }); insertedPair.first->second.status = FileData::Status::Preloaded; - ASSERT(insertedPair.second); return { &insertedPair.first->second }; } } diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index d11c10a0..684662ca 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -595,8 +595,24 @@ void Synth::Impl::finalizeSfzLoad() continue; } - region.hasWavetableSample = fileInformation->wavetable || - fileInformation->end < config::wavetableMaxFrames; + region.hasWavetableSample = fileInformation->wavetable.has_value(); + + if (fileInformation->end < config::wavetableMaxFrames) { + auto sample = resources_.filePool.loadFile(*region.sampleId); + bool allZeros = true; + int numChannels = sample->information.numChannels; + for (int i = 0; i < numChannels; ++i) { + allZeros &= allWithin(sample->preloadedData.getConstSpan(i), + -config::virtuallyZero, config::virtuallyZero); + } + + if (allZeros) { + region.sampleId.reset(new FileId("*silence")); + region.hasWavetableSample = false; + } else { + region.hasWavetableSample |= true; + } + } } if (!region.isOscillator()) { diff --git a/tests/SynthT.cpp b/tests/SynthT.cpp index abc9054a..d9f6a2da 100644 --- a/tests/SynthT.cpp +++ b/tests/SynthT.cpp @@ -1811,3 +1811,20 @@ TEST_CASE("[Keyswitches] Trigger from aftertouch extended CC") synth.renderBlock(buffer); REQUIRE(synth.getNumActiveVoices() == 2); } + +TEST_CASE("[Synth] Short empty files are turned into *silence") +{ + sfz::Synth synth; + std::vector messageList; + sfz::Client client(&messageList); + client.setReceiveCallback(&simpleMessageReceiver); + sfz::AudioBuffer buffer { 2, static_cast(synth.getSamplesPerBlock()) }; + synth.loadSfzString(fs::current_path() / "tests/TestFiles/aftertouch_trigger.sfz", R"( + sample=silence.wav + )"); + synth.dispatchMessage(client, 0, "/region0/sample", "", nullptr); + std::vector expected { + "/region0/sample,s : { *silence }", + }; + REQUIRE(messageList == expected); +} diff --git a/tests/TestFiles/root_key_38.flac b/tests/TestFiles/root_key_38.flac index d8eefb36..d650d48d 100644 Binary files a/tests/TestFiles/root_key_38.flac and b/tests/TestFiles/root_key_38.flac differ diff --git a/tests/TestFiles/root_key_38.wav b/tests/TestFiles/root_key_38.wav index e7a27089..83dcb92c 100644 Binary files a/tests/TestFiles/root_key_38.wav and b/tests/TestFiles/root_key_38.wav differ diff --git a/tests/TestFiles/root_key_62.flac b/tests/TestFiles/root_key_62.flac index 10006b58..9fc63919 100644 Binary files a/tests/TestFiles/root_key_62.flac and b/tests/TestFiles/root_key_62.flac differ diff --git a/tests/TestFiles/root_key_62.wav b/tests/TestFiles/root_key_62.wav index 658d5bd4..51790923 100644 Binary files a/tests/TestFiles/root_key_62.wav and b/tests/TestFiles/root_key_62.wav differ diff --git a/tests/TestFiles/silence.wav b/tests/TestFiles/silence.wav new file mode 100644 index 00000000..6218802a Binary files /dev/null and b/tests/TestFiles/silence.wav differ