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;
401 EG2PreAttackDelay = Gen.GenAmount.shAmount;
402 CheckRange(
"delayModEnv", -12000, 5000, EG2PreAttackDelay);
405 EG2Attack = Gen.GenAmount.shAmount;
406 CheckRange(
"attackModEnv", -12000, 8000, EG2Attack);
409 EG2Hold = Gen.GenAmount.shAmount;
410 CheckRange(
"holdModEnv", -12000, 5000, EG2Hold);
413 EG2Decay = Gen.GenAmount.shAmount;
414 CheckRange(
"decayModEnv", -12000, 8000, EG2Decay);
417 EG2Sustain = Gen.GenAmount.shAmount;
418 CheckRange(
"sustainModEnv", 0, 1000, EG2Sustain);
421 EG2Release = Gen.GenAmount.shAmount;
422 CheckRange(
"releaseModEnv", -12000, 8000, EG2Release);
429 EG1PreAttackDelay = Gen.GenAmount.shAmount;
430 CheckRange(
"delayVolEnv", -12000, 5000, EG1PreAttackDelay);
433 EG1Attack = Gen.GenAmount.shAmount;
434 CheckRange(
"attackVolEnv", -12000, 8000, EG1Attack);
437 EG1Hold = Gen.GenAmount.shAmount;
438 CheckRange(
"holdVolEnv", -12000, 5000, EG1Hold);
441 EG1Decay = Gen.GenAmount.shAmount;
442 CheckRange(
"decayVolEnv", -12000, 8000, EG1Decay);
445 EG1Sustain = Gen.GenAmount.shAmount;
446 CheckRange(
"sustainVolEnv", 0, 1440, EG1Sustain);
449 EG1Release = Gen.GenAmount.shAmount;
450 CheckRange(
"releaseVolEnv", -12000, 8000, EG1Release);
457 uint16_t
id = Gen.GenAmount.wAmount;
458 if (
id >= pFile->Instruments.size()) {
459 throw Exception(
"Broken SF2 file (missing instruments)");
465 loKey = Gen.GenAmount.ranges.byLo;
467 hiKey = Gen.GenAmount.ranges.byHi;
471 minVel = Gen.GenAmount.ranges.byLo;
473 maxVel = Gen.GenAmount.ranges.byHi;
499 uint16_t sid = Gen.GenAmount.wAmount;
500 if (sid >= pFile->Samples.size()) {
501 throw Exception(
"Broken SF2 file (missing samples)");
510 throw Exception(
"Broken SF2 file (invalid loops)");
518 HasLoop = Gen.GenAmount.wAmount & 1;
558 if (pPresetRegion == NULL)
return pan;
559 int p = pPresetRegion->
pan +
pan;
560 if (p < -64) p = -64;
566 if (pPresetRegion == NULL)
return fineTune;
568 if (t < -99) t = -99;
576 if (t < -120) t = -120;
577 if (t > 120) t = 120;
582 int val = (pPresetRegion == NULL || pPresetRegion->EG1PreAttackDelay == NONE) ?
583 EG1PreAttackDelay : pPresetRegion->EG1PreAttackDelay + EG1PreAttackDelay;
588 int val = (pPresetRegion == NULL || pPresetRegion->EG1Attack == NONE) ?
589 EG1Attack : pPresetRegion->EG1Attack + EG1Attack;
594 int val = (pPresetRegion == NULL || pPresetRegion->EG1Hold == NONE) ?
595 EG1Hold : pPresetRegion->EG1Hold + EG1Hold;
600 int val = (pPresetRegion == NULL || pPresetRegion->EG1Decay == NONE) ?
601 EG1Decay : pPresetRegion->EG1Decay + EG1Decay;
606 int val = (pPresetRegion == NULL || pPresetRegion->EG1Sustain == NONE) ?
607 EG1Sustain : pPresetRegion->EG1Sustain + EG1Sustain;
608 return CheckRange(
"GetEG1Sustain()", 0, 1440, val);
612 int val = (pPresetRegion == NULL || pPresetRegion->EG1Release == NONE) ?
613 EG1Release : pPresetRegion->EG1Release + EG1Release;
618 int val = (pPresetRegion == NULL || pPresetRegion->EG2PreAttackDelay == NONE) ?
619 EG2PreAttackDelay : pPresetRegion->EG2PreAttackDelay + EG2PreAttackDelay;
624 int val = (pPresetRegion == NULL || pPresetRegion->EG2Attack == NONE) ?
625 EG2Attack : pPresetRegion->EG2Attack + EG2Attack;
630 int val = (pPresetRegion == NULL || pPresetRegion->EG2Hold == NONE) ?
631 EG2Hold : pPresetRegion->EG2Hold + EG2Hold;
636 int val = (pPresetRegion == NULL || pPresetRegion->EG2Decay == NONE) ?
637 EG2Decay : pPresetRegion->EG2Decay + EG2Decay;
642 int val = (pPresetRegion == NULL || pPresetRegion->EG2Sustain == NONE) ?
643 EG2Sustain : pPresetRegion->EG2Sustain + EG2Sustain;
644 return CheckRange(
"GetEG2Sustain()", 0, 1000, val);
648 int val = (pPresetRegion == NULL || pPresetRegion->EG2Release == NONE) ?
649 EG2Release : pPresetRegion->EG2Release + EG2Release;
654 int val = (pPresetRegion == NULL || pPresetRegion->
modEnvToPitch == NONE) ?
656 return CheckRange(
"GetModEnvToPitch()", -12000, 12000, val);
660 int val = (pPresetRegion == NULL || pPresetRegion->
modLfoToPitch == NONE) ?
662 return CheckRange(
"GetModLfoToPitch()", -12000, 12000, val);
666 int val = (pPresetRegion == NULL || pPresetRegion->
modEnvToFilterFc == NONE) ?
668 return CheckRange(
"GetModEnvToFilterFc()", -12000, +12000, val);
672 int val = (pPresetRegion == NULL || pPresetRegion->
modLfoToFilterFc == NONE) ?
674 return CheckRange(
"GetModLfoToFilterFc()", -12000, +12000, val);
678 int val = (pPresetRegion == NULL || pPresetRegion->
modLfoToVolume == NONE) ?
680 return CheckRange(
"GetModLfoToVolume()", -960, 960, val);
684 int val = (pPresetRegion == NULL || pPresetRegion->
freqModLfo == NONE) ?
690 int val = (pPresetRegion == NULL || pPresetRegion->
delayModLfo == NONE) ?
696 int val = (pPresetRegion == NULL || pPresetRegion->
vibLfoToPitch == NONE) ?
698 return CheckRange(
"GetVibLfoToPitch()", -12000, 12000, val);
702 int val = (pPresetRegion == NULL || pPresetRegion->
freqVibLfo == NONE) ?
708 int val = (pPresetRegion == NULL || pPresetRegion->
delayVibLfo == NONE) ?
716 return CheckRange(
"GetInitialFilterFc()", 1500, 13500, val);
720 int val = (pPresetRegion == NULL || pPresetRegion->
initialFilterQ == NONE) ?
722 return CheckRange(
"GetInitialFilterQ()", 0, 960, val);
732 for (
int i =
regions.size() - 1; i >= 0; i--) {
743 throw Exception(
"Region index out of bounds");
756 if (((r->
loKey == NONE && r->
hiKey == NONE) || (
key >= r->
loKey && key <= r->hiKey)) &&
775 r->pParentInstrument =
this;
833 for (
int i = 0; i <
regions.size(); i++) {
841 std::cerr <<
"Can't remove unknown Region" << std::endl;
845 for (
int i = idx1; i < idx2; i++) {
846 int gIdx1 = pFile->
InstBags[i].InstGenNdx;
847 int gIdx2 = pFile->
InstBags[i + 1].InstGenNdx;
849 if (gIdx1 < 0 || gIdx2 < 0 || gIdx1 > gIdx2 || gIdx2 >= pFile->
InstGenLists.size()) {
850 throw Exception(
"Broken SF2 file (invalid InstGenNdx)");
853 int mIdx1 = pFile->
InstBags[i].InstModNdx;
854 int mIdx2 = pFile->
InstBags[i + 1].InstModNdx;
856 if (mIdx1 < 0 || mIdx2 < 0 || mIdx1 > mIdx2 || mIdx2 >= pFile->
InstModLists.size()) {
857 throw Exception(
"Broken SF2 file (invalid InstModNdx)");
862 for (
int j = gIdx1; j < gIdx2; j++) {
867 for (
int j = mIdx1; j < mIdx2; j++) {
872 if (i == idx1 && idx2 - idx1 > 1) {
875 std::cerr <<
"Ignoring instrument's region without sample" << std::endl;
901 r->EG1PreAttackDelay = r->EG1Attack = r->EG1Hold = r->EG1Decay = r->EG1Sustain = r->EG1Release = NONE;
902 r->EG2PreAttackDelay = r->EG2Attack = r->EG2Hold = r->EG2Decay = r->EG2Sustain = r->EG2Release = NONE;
943 for (
int i = idx1; i < idx2; i++) {
947 if (gIdx1 < 0 || gIdx2 < 0 || gIdx1 > gIdx2 || gIdx2 >= pFile->
PresetGenLists.size()) {
948 throw Exception(
"Broken SF2 file (invalid PresetGenNdx)");
953 for (
int j = gIdx1; j < gIdx2; j++) {
957 if (i == idx1 && idx2 - idx1 > 1) {
960 std::cerr <<
"Ignoring preset's region without instrument" << std::endl;
979 if (!pRIFF)
throw Exception(
"NULL pointer reference to RIFF::File object.");
992 if (lstSDTA == NULL) {
993 throw Exception(
"Broken SF2 file (missing sdta)");
998 if (pCkSmpl != NULL && pCkSm24 != NULL) {
999 long l = pCkSmpl->
GetSize() / 2;
1001 if (pCkSm24->
GetSize() != l) {
1007 if (lstPDTA == NULL) {
1008 throw Exception(
"Broken SF2 file (missing pdta)");
1013 throw Exception(
"Broken SF2 file (broken phdr)");
1016 int count = ck->
GetSize() / 38;
1017 for (
int i = 0; i < count; i++) {
1018 Presets.push_back(
new Preset(
this, ck));
1023 throw Exception(
"Broken SF2 file (broken pbag)");
1027 for (
int i = 0; i < count; i++) {
1037 throw Exception(
"Broken SF2 file (broken pmod)");
1041 for (
int i = 0; i < count; i++) {
1054 throw Exception(
"Broken SF2 file (broken pgen)");
1058 for (
int i = 0; i < count; i++) {
1068 throw Exception(
"Broken SF2 file (broken inst)");
1071 for (
int i = 0; i < count; i++) {
1072 Instruments.push_back(
new Instrument(
this, ck));
1077 throw Exception(
"Broken SF2 file (broken ibag)");
1081 for (
int i = 0; i < count; i++) {
1091 throw Exception(
"Broken SF2 file (broken imod)");
1095 for (
int i = 0; i < count; i++) {
1108 throw Exception(
"Broken SF2 file (broken igen)");
1112 for (
int i = 0; i < count; i++) {
1122 throw Exception(
"Broken SF2 file (broken shdr)");
1125 for (
int i = 0; i < count; i++) {
1126 Samples.push_back(
new Sample(ck, pCkSmpl, pCkSm24));
1130 for (
int i = 0; i < Instruments.size() - 1; i++) {
1133 int x2 = Instruments[i + 1]->InstBagNdx;
1134 if (x1 < 0 || x2 < 0 || x1 > x2 || x2 >=
InstBags.size()) {
1135 throw Exception(
"Broken SF2 file (invalid InstBagNdx)");
1142 for (
int i = 0; i < Presets.size() - 1; i++) {
1143 Preset* preset = Presets[i];
1145 int x2 = Presets[i + 1]->PresetBagNdx;
1146 if (x1 < 0 || x2 < 0 || x1 > x2 || x2 >=
PresetBags.size()) {
1147 throw Exception(
"Broken SF2 file (invalid PresetBagNdx)");
1156 for (
int i = Presets.size() - 1; i >= 0; i--) {
1157 if (Presets[i])
delete (Presets[i]);
1159 for (
int i = Instruments.size() - 1; i >= 0; i--) {
1160 if (Instruments[i])
delete (Instruments[i]);
1162 for (
int i = Samples.size() - 1; i >= 0; i--) {
1163 if (Samples[i])
delete (Samples[i]);
1168 return Presets.size() - 1;
1173 throw Exception(
"Preset index out of bounds");
1176 return Presets[idx];
1180 return Instruments.size() - 1;
1185 throw Exception(
"Instrument index out of bounds");
1188 return Instruments[idx];
1194 if (p == NULL)
continue;
1204 Instruments[i] = NULL;
1211 return Samples.size() - 1;
1216 throw Exception(
"Sample index out of bounds");
1219 return Samples[idx];
1226 if (pInstr == NULL)
continue;
1230 std::cerr <<
"Deleting sample which is still in use" << std::endl;
1237 if (Samples[i] == pSample) {
1249 if (Samples[i] != NULL)
return true;
1344 unsigned long allocationsize = (SampleCount + NullSamplesCount) *
GetFrameSize();
1426 if (SampleCount == 0)
return 0;
1431 uint8_t* pBuf = (uint8_t*)pBuffer;
1434 pCkSm24->
Read(pBuf + SampleCount * 2, SampleCount, 1);
1435 for (
int i = SampleCount - 1; i >= 0; i--) {
1436 pBuf[i*3] = pBuf[(SampleCount * 2) + i];
1437 pBuf[i*3 + 2] = pBuf[i*2 + 1];
1438 pBuf[i*3 + 1] = pBuf[i*2];
1442 pCkSm24->
Read(pBuf + SampleCount * 2, SampleCount, 1);
1443 for (
int i = SampleCount - 1; i >= 0; i--) {
1444 pBuf[i*6] = pBuf[(SampleCount * 2) + i];
1445 pBuf[i*6 + 2] = pBuf[i*2 + 1];
1446 pBuf[i*6 + 1] = pBuf[i*2];
1447 pBuf[i*6 + 3] = pBuf[i*6 + 4] = pBuf[i*6 + 5] = 0;
1451 pCkSm24->
Read(pBuf + SampleCount * 2, SampleCount, 1);
1452 for (
int i = SampleCount - 1; i >= 0; i--) {
1453 pBuf[i*6 + 3] = pBuf[(SampleCount * 2) + i];
1454 pBuf[i*6 + 5] = pBuf[i*2 + 1];
1455 pBuf[i*6 + 4] = pBuf[i*2];
1456 pBuf[i*6] = pBuf[i*6 + 1] = pBuf[i*6 + 2] = 0;
1464 int16_t* pBuf = (int16_t*)pBuffer;
1467 for (
int i = SampleCount - 1; i >= 0; i--) {
1468 pBuf[i*2] = pBuf[i];
1473 for (
int i = SampleCount - 1; i >= 0; i--) {
1475 pBuf[i*2 + 1] = pBuf[i];
1481 std::cerr <<
"Read after the sample end. This is a BUG!" << std::endl;
1482 std::cerr <<
"Current position: " <<
GetPos() << std::endl;
1483 std::cerr <<
"Total number of frames: " <<
GetTotalFrameCount() << std::endl << std::endl;