Set the pitch key center from sample

This commit is contained in:
Jean Pierre Cimalando 2020-08-13 20:57:51 +02:00
parent dc7b42fc43
commit 3b3f6d2731
5 changed files with 18 additions and 4 deletions

View file

@ -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;
}

View file

@ -56,6 +56,7 @@ struct FileInformation {
bool hasLoop { false };
double sampleRate { config::defaultSampleRate };
int numChannels { 0 };
int rootKey { 0 };
absl::optional<WavetableInfo> wavetable;
};

View file

@ -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);

View file

@ -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

View file

@ -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;