Change default_path to handle partial paths

This commit is contained in:
Paul Ferrand 2019-12-21 17:37:09 +01:00
parent 68bf0b132d
commit 8d2be44c57
5 changed files with 16 additions and 27 deletions

View file

@ -50,12 +50,9 @@ namespace sfz {
* *
*/ */
struct Region { struct Region {
Region(const MidiState& midiState, std::string defaultPath = "") Region(const MidiState& midiState, absl::string_view defaultPath = "")
: midiState(midiState), defaultPath(std::move(defaultPath)) : midiState(midiState), defaultPath(std::move(defaultPath))
{ {
if (!this->defaultPath.empty() && this->defaultPath.back() != '/')
absl::StrAppend(&this->defaultPath, "/");
ccSwitched.set(); ccSwitched.set();
} }
Region(const Region&) = default; Region(const Region&) = default;
@ -325,7 +322,7 @@ private:
bool aftertouchSwitched { true }; bool aftertouchSwitched { true };
std::bitset<config::numCCs> ccSwitched; std::bitset<config::numCCs> ccSwitched;
bool triggerOnCC { false }; bool triggerOnCC { false };
std::string defaultPath { "" }; absl::string_view defaultPath { "" };
int activeNotesInRange { -1 }; int activeNotesInRange { -1 };
int sequenceCounter { 0 }; int sequenceCounter { 0 };

View file

@ -181,14 +181,10 @@ void sfz::Synth::handleControlOpcodes(const std::vector<Opcode>& members)
break; break;
case hash("Default_path"): case hash("Default_path"):
[[fallthrough]]; [[fallthrough]];
case hash("default_path"): { case hash("default_path"):
auto newPath = absl::StrReplaceAll(trim(member.value), { { "\\", "/" } }); defaultPath = absl::StrReplaceAll(trim(member.value), { { "\\", "/" } });
if (fs::exists(originalDirectory / newPath)) { DBG("Changing default sample path to " << defaultPath);
DBG("Changing default sample path to " << newPath);
defaultPath = std::move(newPath);
}
break; break;
}
default: default:
// Unsupported control opcode // Unsupported control opcode
DBG("Unsupported control opcode: " << member.opcode); DBG("Unsupported control opcode: " << member.opcode);
@ -423,7 +419,7 @@ void sfz::Synth::noteOff(int delay, int channel, int noteNumber, uint8_t velocit
// auto replacedVelocity = (velocity == 0 ? sfz::getNoteVelocity(noteNumber) : velocity); // auto replacedVelocity = (velocity == 0 ? sfz::getNoteVelocity(noteNumber) : velocity);
auto replacedVelocity = midiState.getNoteVelocity(channel, noteNumber); auto replacedVelocity = midiState.getNoteVelocity(channel, noteNumber);
auto randValue = randNoteDistribution(Random::randomGenerator); auto randValue = randNoteDistribution(Random::randomGenerator);
for (auto& voice : voices) for (auto& voice : voices)
voice->registerNoteOff(delay, channel, noteNumber, replacedVelocity); voice->registerNoteOff(delay, channel, noteNumber, replacedVelocity);
@ -452,7 +448,7 @@ void sfz::Synth::cc(int delay, int channel, int ccNumber, uint8_t ccValue) noexc
return; return;
} }
midiState.ccEvent(channel, ccNumber, ccValue); midiState.ccEvent(channel, ccNumber, ccValue);
for (auto& voice : voices) for (auto& voice : voices)
voice->registerCC(delay, channel, ccNumber, ccValue); voice->registerCC(delay, channel, ccNumber, ccValue);
@ -628,4 +624,4 @@ void sfz::Synth::resetAllControllers(int delay, int channel) noexcept
for (int cc = 0; cc < config::numCCs; ++cc) for (int cc = 0; cc < config::numCCs; ++cc)
region->registerCC(channel, cc, 0); region->registerCC(channel, cc, 0);
} }
} }

View file

@ -361,10 +361,10 @@ protected:
private: private:
/** /**
* @brief Reset all CCs; to be used on CC 121 * @brief Reset all CCs; to be used on CC 121
* *
* @param delay the delay for the controller reset * @param delay the delay for the controller reset
* @param channel the channel on which to reset the controller * @param channel the channel on which to reset the controller
* *
*/ */
void resetAllControllers(int delay, int channel) noexcept; void resetAllControllers(int delay, int channel) noexcept;

View file

@ -330,18 +330,17 @@ TEST_CASE("[Files] Default path")
{ {
sfz::Synth synth; sfz::Synth synth;
synth.loadSfzFile(fs::current_path() / "tests/TestFiles/default_path.sfz"); synth.loadSfzFile(fs::current_path() / "tests/TestFiles/default_path.sfz");
REQUIRE(synth.getNumRegions() == 4); REQUIRE(synth.getNumRegions() == 3);
REQUIRE(synth.getRegionView(0)->sample == R"(DefaultPath/SubPath1/sample1.wav)"); REQUIRE(synth.getRegionView(0)->sample == R"(DefaultPath/SubPath1/sample1.wav)");
REQUIRE(synth.getRegionView(1)->sample == R"(DefaultPath/SubPath2/sample2.wav)"); REQUIRE(synth.getRegionView(1)->sample == R"(DefaultPath/SubPath2/sample2.wav)");
REQUIRE(synth.getRegionView(2)->sample == R"(DefaultPath/SubPath1/sample1.wav)"); REQUIRE(synth.getRegionView(2)->sample == R"(DefaultPath/SubPath1/sample1.wav)");
REQUIRE(synth.getRegionView(3)->sample == R"(DefaultPath/SubPath2/sample2.wav)");
} }
TEST_CASE("[Files] Default path reset when calling loadSfzFile again") TEST_CASE("[Files] Default path reset when calling loadSfzFile again")
{ {
sfz::Synth synth; sfz::Synth synth;
synth.loadSfzFile(fs::current_path() / "tests/TestFiles/default_path.sfz"); synth.loadSfzFile(fs::current_path() / "tests/TestFiles/default_path.sfz");
REQUIRE(synth.getNumRegions() == 4); REQUIRE(synth.getNumRegions() == 3);
synth.loadSfzFile(fs::current_path() / "tests/TestFiles/default_path_reset.sfz"); synth.loadSfzFile(fs::current_path() / "tests/TestFiles/default_path_reset.sfz");
REQUIRE(synth.getNumRegions() == 1); REQUIRE(synth.getNumRegions() == 1);
REQUIRE(synth.getRegionView(0)->sample == R"(DefaultPath/SubPath2/sample2.wav)"); REQUIRE(synth.getRegionView(0)->sample == R"(DefaultPath/SubPath2/sample2.wav)");
@ -364,4 +363,4 @@ TEST_CASE("[Files] Set CC applies properly to all channels")
REQUIRE(synth.getMidiState().getCCValue(channel, 142) == 63); REQUIRE(synth.getMidiState().getCCValue(channel, 142) == 63);
REQUIRE(synth.getMidiState().getCCValue(channel, 61) == 122); REQUIRE(synth.getMidiState().getCCValue(channel, 61) == 122);
} }
} }

View file

@ -1,10 +1,7 @@
<region> sample=DefaultPath/SubPath1/sample1.wav <region> sample=DefaultPath/SubPath1/sample1.wav
<control> default_path=DefaultPath\SubPath2 <control> default_path=DefaultPath\SubPath2\
<region> sample=sample2.wav <region> sample=sample2.wav
<control> default_path=DefaultPath/SubPath1 <control> default_path=DefaultPath/SubPath1/sample
<region> sample=sample1.wav <region> sample=1.wav
<control> default_path=DefaultPath/SubPath2/
<region> sample=sample2.wav