31 #define _1200TH_ROOT_OF_2 1.000577789506555
32 #define _200TH_ROOT_OF_10 1.011579454259899
36 if (Timecents == NONE)
return NONE;
37 if (Timecents == 0)
return 1.0;
38 if (Timecents == -32768)
return 0.0;
43 if (Centibels == NONE)
return NONE;
44 if (Centibels == 0)
return 1.0;
49 if (cents == NONE)
return NONE;
50 if (cents == 0)
return 8.176;
56 if(ck == NULL)
throw Exception(
"Mandatory chunk in RIFF list chunk not found: " + ToString(chunkId));
61 if(ck == NULL)
return;
62 char* buf =
new char[strLength];
64 for(
int i = 0; i < strLength; i++) {
66 if(buf[i] == 0 && !len) len = i;
68 if(!len) len = strLength;
78 if (ck == NULL)
throw Exception(
"NULL chunk");
95 ModAmtSrcOper(
Modulator(mod.ModAmtSrcOper))
166 throw Exception(
"Broken SF2 file (invalid sample info)");
180 default:
throw Exception(
"Broken SF2 file (invalid sample type)");
235 unsigned long FrameCount,
240 unsigned long samplestoread = FrameCount, totalreadsamples = 0, readsamples, samplestoloopend;
241 uint8_t* pDst = (uint8_t*) pBuffer;
246 readsamples =
Read(&pDst[totalreadsamples *
GetFrameSize()], Min(samplestoread, samplestoloopend));
247 samplestoread -= readsamples;
248 totalreadsamples += readsamples;
249 if (readsamples == samplestoloopend) {
252 }
while (samplestoread && readsamples);
254 totalreadsamples =
Read(pBuffer, FrameCount);
259 return totalreadsamples;
265 pParentInstrument = NULL;
276 EG1PreAttackDelay = EG1Attack = EG1Hold = EG1Decay = EG1Release = -12000;
278 EG2PreAttackDelay = EG2Attack = EG2Hold = EG2Decay = EG2Release = -12000;
298 int CheckRange(std::string genName,
int min,
int max,
int& gen) {
299 if (gen == NONE)
return gen;
302 std::cerr <<
"sf2: " << genName;
303 std::cerr <<
" is below the minimum allowed value (min=" << min <<
"): " << gen << std::endl;
307 std::cerr <<
"sf2: " << genName;
308 std::cerr <<
" is above the maximum allowed value (max=" << max <<
"): " << gen << std::endl;
316 switch(Gen.GenOper) {
321 if (Gen.GenAmount.shAmount <= 0) {
324 std::cerr <<
"Ignoring invalid endAddrsOffset" << std::endl;
378 pan = Gen.GenAmount.shAmount;
400 EG2PreAttackDelay = Gen.GenAmount.shAmount;
401 CheckRange(
"delayModEnv", -12000, 5000, EG2PreAttackDelay);
404 EG2Attack = Gen.GenAmount.shAmount;
405 CheckRange(
"attackModEnv", -12000, 8000, EG2Attack);
408 EG2Hold = Gen.GenAmount.shAmount;
409 CheckRange(
"holdModEnv", -12000, 5000, EG2Hold);
412 EG2Decay = Gen.GenAmount.shAmount;
413 CheckRange(
"decayModEnv", -12000, 8000, EG2Decay);
416 EG2Sustain = Gen.GenAmount.shAmount;
417 CheckRange(
"sustainModEnv", 0, 1000, EG2Sustain);
420 EG2Release = Gen.GenAmount.shAmount;
421 CheckRange(
"releaseModEnv", -12000, 8000, EG2Release);
428 EG1PreAttackDelay = Gen.GenAmount.shAmount;
429 CheckRange(
"delayVolEnv", -12000, 5000, EG1PreAttackDelay);
432 EG1Attack = Gen.GenAmount.shAmount;
433 CheckRange(
"attackVolEnv", -12000, 8000, EG1Attack);
436 EG1Hold = Gen.GenAmount.shAmount;
437 CheckRange(
"holdVolEnv", -12000, 5000, EG1Hold);
440 EG1Decay = Gen.GenAmount.shAmount;
441 CheckRange(
"decayVolEnv", -12000, 8000, EG1Decay);
444 EG1Sustain = Gen.GenAmount.shAmount;
445 CheckRange(
"sustainVolEnv", 0, 1440, EG1Sustain);
448 EG1Release = Gen.GenAmount.shAmount;
449 CheckRange(
"releaseVolEnv", -12000, 8000, EG1Release);
456 uint16_t
id = Gen.GenAmount.wAmount;
457 if (
id >= pFile->Instruments.size()) {
458 throw Exception(
"Broken SF2 file (missing instruments)");
464 loKey = Gen.GenAmount.ranges.byLo;
466 hiKey = Gen.GenAmount.ranges.byHi;
470 minVel = Gen.GenAmount.ranges.byLo;
472 maxVel = Gen.GenAmount.ranges.byHi;
498 uint16_t sid = Gen.GenAmount.wAmount;
499 if (sid >= pFile->Samples.size()) {
500 throw Exception(
"Broken SF2 file (missing samples)");
509 throw Exception(
"Broken SF2 file (invalid loops)");
517 HasLoop = Gen.GenAmount.wAmount & 1;
557 if (pPresetRegion == NULL)
return pan;
558 int p = pPresetRegion->
pan +
pan;
559 if (p < -64) p = -64;
565 if (pPresetRegion == NULL)
return fineTune;
567 if (t < -99) t = -99;
575 if (t < -120) t = -120;
576 if (t > 120) t = 120;
581 int val = (pPresetRegion == NULL || pPresetRegion->EG1PreAttackDelay == NONE) ?
582 EG1PreAttackDelay : pPresetRegion->EG1PreAttackDelay + EG1PreAttackDelay;
587 int val = (pPresetRegion == NULL || pPresetRegion->EG1Attack == NONE) ?
588 EG1Attack : pPresetRegion->EG1Attack + EG1Attack;
593 int val = (pPresetRegion == NULL || pPresetRegion->EG1Hold == NONE) ?
594 EG1Hold : pPresetRegion->EG1Hold + EG1Hold;
599 int val = (pPresetRegion == NULL || pPresetRegion->EG1Decay == NONE) ?
600 EG1Decay : pPresetRegion->EG1Decay + EG1Decay;
605 int val = (pPresetRegion == NULL || pPresetRegion->EG1Sustain == NONE) ?
606 EG1Sustain : pPresetRegion->EG1Sustain + EG1Sustain;
607 return CheckRange(
"GetEG1Sustain()", 0, 1440, val);
611 int val = (pPresetRegion == NULL || pPresetRegion->EG1Release == NONE) ?
612 EG1Release : pPresetRegion->EG1Release + EG1Release;
617 int val = (pPresetRegion == NULL || pPresetRegion->EG2PreAttackDelay == NONE) ?
618 EG2PreAttackDelay : pPresetRegion->EG2PreAttackDelay + EG2PreAttackDelay;
623 int val = (pPresetRegion == NULL || pPresetRegion->EG2Attack == NONE) ?
624 EG2Attack : pPresetRegion->EG2Attack + EG2Attack;
629 int val = (pPresetRegion == NULL || pPresetRegion->EG2Hold == NONE) ?
630 EG2Hold : pPresetRegion->EG2Hold + EG2Hold;
635 int val = (pPresetRegion == NULL || pPresetRegion->EG2Decay == NONE) ?
636 EG2Decay : pPresetRegion->EG2Decay + EG2Decay;
641 int val = (pPresetRegion == NULL || pPresetRegion->EG2Sustain == NONE) ?
642 EG2Sustain : pPresetRegion->EG2Sustain + EG2Sustain;
643 return CheckRange(
"GetEG2Sustain()", 0, 1000, val);
647 int val = (pPresetRegion == NULL || pPresetRegion->EG2Release == NONE) ?
648 EG2Release : pPresetRegion->EG2Release + EG2Release;
653 int val = (pPresetRegion == NULL || pPresetRegion->
modEnvToPitch == NONE) ?
655 return CheckRange(
"GetModEnvToPitch()", -12000, 12000, val);
659 int val = (pPresetRegion == NULL || pPresetRegion->
modLfoToPitch == NONE) ?
661 return CheckRange(
"GetModLfoToPitch()", -12000, 12000, val);
665 int val = (pPresetRegion == NULL || pPresetRegion->
modEnvToFilterFc == NONE) ?
667 return CheckRange(
"GetModEnvToFilterFc()", -12000, +12000, val);
671 int val = (pPresetRegion == NULL || pPresetRegion->
modLfoToFilterFc == NONE) ?
673 return CheckRange(
"GetModLfoToFilterFc()", -12000, +12000, val);
677 int val = (pPresetRegion == NULL || pPresetRegion->
modLfoToVolume == NONE) ?
679 return CheckRange(
"GetModLfoToVolume()", -960, 960, val);
683 int val = (pPresetRegion == NULL || pPresetRegion->
freqModLfo == NONE) ?
689 int val = (pPresetRegion == NULL || pPresetRegion->
delayModLfo == NONE) ?
695 int val = (pPresetRegion == NULL || pPresetRegion->
vibLfoToPitch == NONE) ?
697 return CheckRange(
"GetVibLfoToPitch()", -12000, 12000, val);
701 int val = (pPresetRegion == NULL || pPresetRegion->
freqVibLfo == NONE) ?
707 int val = (pPresetRegion == NULL || pPresetRegion->
delayVibLfo == NONE) ?
715 return CheckRange(
"GetInitialFilterFc()", 1500, 13500, val);
719 int val = (pPresetRegion == NULL || pPresetRegion->
initialFilterQ == NONE) ?
721 return CheckRange(
"GetInitialFilterQ()", 0, 960, val);
731 for (
int i =
regions.size() - 1; i >= 0; i--) {
742 throw Exception(
"Region index out of bounds");
755 if (((r->
loKey == NONE && r->
hiKey == NONE) || (
key >= r->
loKey && key <= r->hiKey)) &&
774 r->pParentInstrument =
this;
832 for (
int i = 0; i <
regions.size(); i++) {
840 std::cerr <<
"Can't remove unknown Region" << std::endl;
844 for (
int i = idx1; i < idx2; i++) {
845 int gIdx1 = pFile->
InstBags[i].InstGenNdx;
846 int gIdx2 = pFile->
InstBags[i + 1].InstGenNdx;
848 if (gIdx1 < 0 || gIdx2 < 0 || gIdx1 > gIdx2 || gIdx2 >= pFile->
InstGenLists.size()) {
849 throw Exception(
"Broken SF2 file (invalid InstGenNdx)");
852 int mIdx1 = pFile->
InstBags[i].InstModNdx;
853 int mIdx2 = pFile->
InstBags[i + 1].InstModNdx;
855 if (mIdx1 < 0 || mIdx2 < 0 || mIdx1 > mIdx2 || mIdx2 >= pFile->
InstModLists.size()) {
856 throw Exception(
"Broken SF2 file (invalid InstModNdx)");
861 for (
int j = gIdx1; j < gIdx2; j++) {
866 for (
int j = mIdx1; j < mIdx2; j++) {
871 if (i == idx1 && idx2 - idx1 > 1) {
874 std::cerr <<
"Ignoring instrument's region without sample" << std::endl;
900 r->EG1PreAttackDelay = r->EG1Attack = r->EG1Hold = r->EG1Decay = r->EG1Sustain = r->EG1Release = NONE;
901 r->EG2PreAttackDelay = r->EG2Attack = r->EG2Hold = r->EG2Decay = r->EG2Sustain = r->EG2Release = NONE;
942 for (
int i = idx1; i < idx2; i++) {
946 if (gIdx1 < 0 || gIdx2 < 0 || gIdx1 > gIdx2 || gIdx2 >= pFile->
PresetGenLists.size()) {
947 throw Exception(
"Broken SF2 file (invalid PresetGenNdx)");
952 for (
int j = gIdx1; j < gIdx2; j++) {
956 if (i == idx1 && idx2 - idx1 > 1) {
959 std::cerr <<
"Ignoring preset's region without instrument" << std::endl;
978 if (!pRIFF)
throw Exception(
"NULL pointer reference to RIFF::File object.");
991 if (lstSDTA == NULL) {
992 throw Exception(
"Broken SF2 file (missing sdta)");
997 if (pCkSmpl != NULL && pCkSm24 != NULL) {
998 long l = pCkSmpl->
GetSize() / 2;
1000 if (pCkSm24->
GetSize() != l) {
1006 if (lstPDTA == NULL) {
1007 throw Exception(
"Broken SF2 file (missing pdta)");
1012 throw Exception(
"Broken SF2 file (broken phdr)");
1015 int count = ck->
GetSize() / 38;
1016 for (
int i = 0; i < count; i++) {
1017 Presets.push_back(
new Preset(
this, ck));
1022 throw Exception(
"Broken SF2 file (broken pbag)");
1026 for (
int i = 0; i < count; i++) {
1036 throw Exception(
"Broken SF2 file (broken pmod)");
1040 for (
int i = 0; i < count; i++) {
1053 throw Exception(
"Broken SF2 file (broken pgen)");
1057 for (
int i = 0; i < count; i++) {
1067 throw Exception(
"Broken SF2 file (broken inst)");
1070 for (
int i = 0; i < count; i++) {
1071 Instruments.push_back(
new Instrument(
this, ck));
1076 throw Exception(
"Broken SF2 file (broken ibag)");
1080 for (
int i = 0; i < count; i++) {
1090 throw Exception(
"Broken SF2 file (broken imod)");
1094 for (
int i = 0; i < count; i++) {
1107 throw Exception(
"Broken SF2 file (broken igen)");
1111 for (
int i = 0; i < count; i++) {
1121 throw Exception(
"Broken SF2 file (broken shdr)");
1124 for (
int i = 0; i < count; i++) {
1125 Samples.push_back(
new Sample(ck, pCkSmpl, pCkSm24));
1129 for (
int i = 0; i < Instruments.size() - 1; i++) {
1132 int x2 = Instruments[i + 1]->InstBagNdx;
1133 if (x1 < 0 || x2 < 0 || x1 > x2 || x2 >=
InstBags.size()) {
1134 throw Exception(
"Broken SF2 file (invalid InstBagNdx)");
1141 for (
int i = 0; i < Presets.size() - 1; i++) {
1142 Preset* preset = Presets[i];
1144 int x2 = Presets[i + 1]->PresetBagNdx;
1145 if (x1 < 0 || x2 < 0 || x1 > x2 || x2 >=
PresetBags.size()) {
1146 throw Exception(
"Broken SF2 file (invalid PresetBagNdx)");
1155 for (
int i = Presets.size() - 1; i >= 0; i--) {
1156 if (Presets[i])
delete (Presets[i]);
1158 for (
int i = Instruments.size() - 1; i >= 0; i--) {
1159 if (Instruments[i])
delete (Instruments[i]);
1161 for (
int i = Samples.size() - 1; i >= 0; i--) {
1162 if (Samples[i])
delete (Samples[i]);
1167 return Presets.size() - 1;
1172 throw Exception(
"Preset index out of bounds");
1175 return Presets[idx];
1179 return Instruments.size() - 1;
1184 throw Exception(
"Instrument index out of bounds");
1187 return Instruments[idx];
1193 if (p == NULL)
continue;
1203 Instruments[i] = NULL;
1210 return Samples.size() - 1;
1215 throw Exception(
"Sample index out of bounds");
1218 return Samples[idx];
1225 if (pInstr == NULL)
continue;
1229 std::cerr <<
"Deleting sample which is still in use" << std::endl;
1236 if (Samples[i] == pSample) {
1248 if (Samples[i] != NULL)
return true;
1343 unsigned long allocationsize = (SampleCount + NullSamplesCount) *
GetFrameSize();
1425 if (SampleCount == 0)
return 0;
1430 uint8_t* pBuf = (uint8_t*)pBuffer;
1433 pCkSm24->
Read(pBuf + SampleCount * 2, SampleCount, 1);
1434 for (
int i = SampleCount - 1; i >= 0; i--) {
1435 pBuf[i*3] = pBuf[(SampleCount * 2) + i];
1436 pBuf[i*3 + 2] = pBuf[i*2 + 1];
1437 pBuf[i*3 + 1] = pBuf[i*2];
1441 pCkSm24->
Read(pBuf + SampleCount * 2, SampleCount, 1);
1442 for (
int i = SampleCount - 1; i >= 0; i--) {
1443 pBuf[i*6] = pBuf[(SampleCount * 2) + i];
1444 pBuf[i*6 + 2] = pBuf[i*2 + 1];
1445 pBuf[i*6 + 1] = pBuf[i*2];
1446 pBuf[i*6 + 3] = pBuf[i*6 + 4] = pBuf[i*6 + 5] = 0;
1450 pCkSm24->
Read(pBuf + SampleCount * 2, SampleCount, 1);
1451 for (
int i = SampleCount - 1; i >= 0; i--) {
1452 pBuf[i*6 + 3] = pBuf[(SampleCount * 2) + i];
1453 pBuf[i*6 + 5] = pBuf[i*2 + 1];
1454 pBuf[i*6 + 4] = pBuf[i*2];
1455 pBuf[i*6] = pBuf[i*6 + 1] = pBuf[i*6 + 2] = 0;
1463 int16_t* pBuf = (int16_t*)pBuffer;
1466 for (
int i = SampleCount - 1; i >= 0; i--) {
1467 pBuf[i*2] = pBuf[i];
1472 for (
int i = SampleCount - 1; i >= 0; i--) {
1474 pBuf[i*2 + 1] = pBuf[i];
1480 std::cerr <<
"Read after the sample end. This is a BUG!" << std::endl;
1481 std::cerr <<
"Current position: " <<
GetPos() << std::endl;
1482 std::cerr <<
"Total number of frames: " <<
GetTotalFrameCount() << std::endl << std::endl;