diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 78783a0ca..6bd4e35ae 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 2d7f2a92a..fc4ad988b 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 abc9054a4..d9f6a2daa 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 d8eefb36a..d650d48d9 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 e7a27089c..83dcb92c0 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 10006b581..9fc639190 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 658d5bd4d..51790923c 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 000000000..6218802a6 Binary files /dev/null and b/tests/TestFiles/silence.wav differ