Merge pull request #362 from jpcima/pitch-keycenter
Set the pitch key center from sample
This commit is contained in:
commit
b0d66f08de
10 changed files with 40 additions and 4 deletions
|
|
@ -216,14 +216,15 @@ absl::optional<sfz::FileInformation> sfz::FilePool::getFileInformation(const Fil
|
|||
returnedValue.numChannels = reader->channels();
|
||||
|
||||
SF_INSTRUMENT instrumentInfo {};
|
||||
bool haveInstrumentInfo = reader->getInstrument(&instrumentInfo);
|
||||
|
||||
FileMetadataReader mdReader;
|
||||
bool mdReaderOpened = mdReader.open(file);
|
||||
|
||||
if (!reader->getInstrument(&instrumentInfo)) {
|
||||
if (!haveInstrumentInfo) {
|
||||
// if no instrument, then try extracting from embedded RIFF chunks (flac)
|
||||
if (mdReaderOpened)
|
||||
mdReader.extractRiffInstrument(instrumentInfo);
|
||||
haveInstrumentInfo = mdReader.extractRiffInstrument(instrumentInfo);
|
||||
}
|
||||
|
||||
if (mdReaderOpened) {
|
||||
|
|
@ -233,7 +234,7 @@ absl::optional<sfz::FileInformation> sfz::FilePool::getFileInformation(const Fil
|
|||
}
|
||||
|
||||
if (!fileId.isReverse()) {
|
||||
if (instrumentInfo.loop_count > 0) {
|
||||
if (haveInstrumentInfo && instrumentInfo.loop_count > 0) {
|
||||
returnedValue.hasLoop = true;
|
||||
returnedValue.loopBegin = instrumentInfo.loops[0].start;
|
||||
returnedValue.loopEnd = min(returnedValue.end, instrumentInfo.loops[0].end - 1);
|
||||
|
|
@ -243,6 +244,9 @@ absl::optional<sfz::FileInformation> sfz::FilePool::getFileInformation(const Fil
|
|||
// prehaps it can make use of SF_LOOP_BACKWARD?
|
||||
}
|
||||
|
||||
if (haveInstrumentInfo)
|
||||
returnedValue.rootKey = clamp<int8_t>(instrumentInfo.basenote, 0, 127);
|
||||
|
||||
return returnedValue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ struct FileInformation {
|
|||
bool hasLoop { false };
|
||||
double sampleRate { config::defaultSampleRate };
|
||||
int numChannels { 0 };
|
||||
int rootKey { 0 };
|
||||
absl::optional<WavetableInfo> wavetable;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -779,7 +779,12 @@ bool sfz::Region::parseOpcode(const Opcode& rawOpcode)
|
|||
|
||||
// Performance parameters: pitch
|
||||
case hash("pitch_keycenter"):
|
||||
setValueFromOpcode(opcode, pitchKeycenter, Default::keyRange);
|
||||
if (opcode.value == "sample")
|
||||
pitchKeycenterFromSample = true;
|
||||
else {
|
||||
pitchKeycenterFromSample = false;
|
||||
setValueFromOpcode(opcode, pitchKeycenter, Default::keyRange);
|
||||
}
|
||||
break;
|
||||
case hash("pitch_keytrack"):
|
||||
setValueFromOpcode(opcode, pitchKeytrack, Default::pitchKeytrackRange);
|
||||
|
|
|
|||
|
|
@ -382,6 +382,7 @@ struct Region {
|
|||
|
||||
// Performance parameters: pitch
|
||||
uint8_t pitchKeycenter { Default::pitchKeycenter }; // pitch_keycenter
|
||||
bool pitchKeycenterFromSample { false };
|
||||
int pitchKeytrack { Default::pitchKeytrack }; // pitch_keytrack
|
||||
int pitchRandom { Default::pitchRandom }; // pitch_random
|
||||
int pitchVeltrack { Default::pitchVeltrack }; // pitch_veltrack
|
||||
|
|
|
|||
|
|
@ -536,6 +536,9 @@ void sfz::Synth::finalizeSfzLoad()
|
|||
if (fileInformation->numChannels == 2)
|
||||
region->hasStereoSample = true;
|
||||
|
||||
if (region->pitchKeycenterFromSample)
|
||||
region->pitchKeycenter = fileInformation->rootKey;
|
||||
|
||||
// TODO: adjust with LFO targets
|
||||
const auto maxOffset = [region]() {
|
||||
uint64_t sumOffsetCC = region->offset + region->offsetRandom;
|
||||
|
|
|
|||
|
|
@ -684,3 +684,25 @@ TEST_CASE("[Files] Duplicate labels")
|
|||
REQUIRE(xmlMidnam.find("<Note Number=\"60\" Name=\"Quux\" />") != xmlMidnam.npos);
|
||||
REQUIRE(xmlMidnam.find("<Control Type=\"7bit\" Number=\"20\" Name=\"Bar\" />") != xmlMidnam.npos);
|
||||
}
|
||||
|
||||
TEST_CASE("[Files] Key center from audio file")
|
||||
{
|
||||
sfz::Synth synth;
|
||||
synth.loadSfzString(fs::current_path() / "tests/TestFiles/sample_keycenter.sfz", R"(
|
||||
<group> pitch_keycenter=sample oscillator=off
|
||||
<region> sample=root_key_38.wav
|
||||
<region> sample=root_key_62.wav
|
||||
<region> sample=root_key_38.flac
|
||||
<region> sample=root_key_62.flac
|
||||
<region> pitch_keycenter=10 sample=root_key_62.flac
|
||||
<region> key=10 sample=root_key_62.flac
|
||||
)");
|
||||
|
||||
REQUIRE(synth.getNumRegions() == 6);
|
||||
REQUIRE(synth.getRegionView(0)->pitchKeycenter == 38);
|
||||
REQUIRE(synth.getRegionView(1)->pitchKeycenter == 62);
|
||||
REQUIRE(synth.getRegionView(2)->pitchKeycenter == 38);
|
||||
REQUIRE(synth.getRegionView(3)->pitchKeycenter == 62);
|
||||
REQUIRE(synth.getRegionView(4)->pitchKeycenter == 10);
|
||||
REQUIRE(synth.getRegionView(5)->pitchKeycenter == 62);
|
||||
}
|
||||
|
|
|
|||
BIN
tests/TestFiles/root_key_38.flac
Normal file
BIN
tests/TestFiles/root_key_38.flac
Normal file
Binary file not shown.
BIN
tests/TestFiles/root_key_38.wav
Normal file
BIN
tests/TestFiles/root_key_38.wav
Normal file
Binary file not shown.
BIN
tests/TestFiles/root_key_62.flac
Normal file
BIN
tests/TestFiles/root_key_62.flac
Normal file
Binary file not shown.
BIN
tests/TestFiles/root_key_62.wav
Normal file
BIN
tests/TestFiles/root_key_62.wav
Normal file
Binary file not shown.
Loading…
Add table
Reference in a new issue