1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2014 Christian Schoenebeck * |
* Copyright (C) 2014-2015 Christian Schoenebeck * |
4 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
5 |
* * |
* * |
6 |
* This program is part of libgig. * |
* This program is part of libgig. * |
34 |
# include <unistd.h> |
# include <unistd.h> |
35 |
#endif |
#endif |
36 |
|
|
37 |
#include "Korg.h" |
#include "../Korg.h" |
38 |
#include "gig.h" |
#include "../gig.h" |
39 |
|
|
40 |
using namespace std; |
using namespace std; |
41 |
|
|
63 |
cout << endl; |
cout << endl; |
64 |
} |
} |
65 |
|
|
66 |
|
static bool beginsWith(const string& haystack, const string& needle) { |
67 |
|
return haystack.substr(0, needle.size()) == needle; |
68 |
|
} |
69 |
|
|
70 |
static bool endsWith(const string& haystack, const string& needle) { |
static bool endsWith(const string& haystack, const string& needle) { |
71 |
return haystack.substr(haystack.size() - needle.size(), needle.size()) == needle; |
return haystack.substr(haystack.size() - needle.size(), needle.size()) == needle; |
72 |
} |
} |
310 |
set<DLS::range_t> velocityRanges; |
set<DLS::range_t> velocityRanges; |
311 |
for (int i = 0; i < group.size(); ++i) { |
for (int i = 0; i < group.size(); ++i) { |
312 |
Korg::KMPInstrument* instr = group[i]; |
Korg::KMPInstrument* instr = group[i]; |
313 |
int iLowerKey = 0; |
uint16_t iLowerKey = 0; |
314 |
for (int k = 0; k < instr->GetRegionCount(); ++k) { |
for (int k = 0; k < instr->GetRegionCount(); ++k) { |
315 |
Korg::KMPRegion* rgn = instr->GetRegion(k); |
Korg::KMPRegion* rgn = instr->GetRegion(k); |
316 |
DLS::range_t keyRange2 = { iLowerKey, rgn->TopKey }; |
DLS::range_t keyRange2 = { iLowerKey, rgn->TopKey }; |
319 |
int from, to; |
int from, to; |
320 |
string baseName = parseNumberRange(instr->Name(), from, to); |
string baseName = parseNumberRange(instr->Name(), from, to); |
321 |
if (baseName != instr->Name()) { // number range like "003-120" found in instrument name ... |
if (baseName != instr->Name()) { // number range like "003-120" found in instrument name ... |
322 |
DLS::range_t velRange = { from, to }; |
DLS::range_t velRange = { uint16_t(from), uint16_t(to) }; |
323 |
velocityRanges.insert(velRange); |
velocityRanges.insert(velRange); |
324 |
} |
} |
325 |
} |
} |
450 |
gigSampleGroup->Name = baseName; |
gigSampleGroup->Name = baseName; |
451 |
} |
} |
452 |
|
|
453 |
|
if (kmpRegion->SampleFileName == "SKIPPEDSAMPL" || |
454 |
|
(beginsWith(kmpRegion->SampleFileName, "INTERNAL") && !endsWith(kmpRegion->SampleFileName, ".KSF"))) |
455 |
|
{ |
456 |
|
return NULL; |
457 |
|
} |
458 |
|
|
459 |
Korg::KSFSample* ksfSample = findKSFSampleWithFileName(kmpRegion->FullSampleFileName()); |
Korg::KSFSample* ksfSample = findKSFSampleWithFileName(kmpRegion->FullSampleFileName()); |
460 |
if (!ksfSample) |
if (!ksfSample) |
461 |
throw Korg::Exception("Internal error: Could not resolve KSFSample object"); |
throw Korg::Exception("Internal error: Could not resolve KSFSample object"); |
476 |
|
|
477 |
for (int i = 0; i < instr->GetRegionCount(); ++i) { |
for (int i = 0; i < instr->GetRegionCount(); ++i) { |
478 |
Korg::KMPRegion* rgn = instr->GetRegion(i); |
Korg::KMPRegion* rgn = instr->GetRegion(i); |
479 |
|
if (rgn->SampleFileName == "SKIPPEDSAMPL") { |
480 |
|
cout << "WARNING: 'SKIPPEDSAMPL' as sample reference found!\n"; |
481 |
|
continue; |
482 |
|
} else if (beginsWith(rgn->SampleFileName, "INTERNAL") && |
483 |
|
!endsWith(rgn->SampleFileName, ".KSF")) { |
484 |
|
cout << "WARNING: One of the KORG instrument's internal samples was referenced as sample!\n"; |
485 |
|
continue; |
486 |
|
} |
487 |
// check if the sample referenced by this region was already |
// check if the sample referenced by this region was already |
488 |
// loaded, if not then load it ... |
// loaded, if not then load it ... |
489 |
if (!findKSFSampleWithFileName(rgn->FullSampleFileName())) |
if (!findKSFSampleWithFileName(rgn->FullSampleFileName())) |
536 |
bool bForce = false; |
bool bForce = false; |
537 |
|
|
538 |
// validate & parse arguments provided to this program |
// validate & parse arguments provided to this program |
|
if (argc < 3) { |
|
|
printUsage(); |
|
|
return EXIT_FAILURE; |
|
|
} |
|
539 |
int iArg; |
int iArg; |
540 |
for (iArg = 1; iArg < argc; ++iArg) { |
for (iArg = 1; iArg < argc; ++iArg) { |
541 |
const string opt = argv[iArg]; |
const string opt = argv[iArg]; |
559 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
560 |
} |
} |
561 |
} |
} |
562 |
|
if (argc < 3) { |
563 |
|
printUsage(); |
564 |
|
return EXIT_FAILURE; |
565 |
|
} |
566 |
|
|
567 |
set<string> inFileNames; |
set<string> inFileNames; |
568 |
string outFileName; |
string outFileName; |
623 |
Korg::KMPInstrument* kmpInstr = *itInstr; |
Korg::KMPInstrument* kmpInstr = *itInstr; |
624 |
cout << " |---> KMP multi sample '" << kmpInstr->Name() << "'" << endl; |
cout << " |---> KMP multi sample '" << kmpInstr->Name() << "'" << endl; |
625 |
|
|
626 |
int iLowKey = 0; |
uint16_t iLowKey = 0; |
627 |
for (int k = 0; k < kmpInstr->GetRegionCount(); ++k) { |
for (int k = 0; k < kmpInstr->GetRegionCount(); ++k) { |
628 |
Korg::KMPRegion* kmpRegion = kmpInstr->GetRegion(k); |
Korg::KMPRegion* kmpRegion = kmpInstr->GetRegion(k); |
629 |
DLS::range_t keyRange = { iLowKey, kmpRegion->TopKey }; |
DLS::range_t keyRange = { iLowKey, kmpRegion->TopKey }; |
686 |
} |
} |
687 |
} |
} |
688 |
|
|
689 |
// get the velocity range for current KORG region |
// only create a velocity dimension if there was really |
690 |
{ |
// more than one velocity split zone defined |
691 |
int from, to; |
if (origVelocitySplits.size() <= 1) { |
692 |
if (parseNumberRange(kmpInstr->Name(), from, to) == kmpInstr->Name()) |
cerr << "WARNING: Velocity split mentioned, but with only one zone, thus ignoring velocity split.\n"; |
693 |
throw Korg::Exception("Internal error: parsing velocity range failed"); |
} else { |
694 |
velRange.low = from; |
// get the velocity range for current KORG region |
695 |
velRange.high = to; |
{ |
696 |
if (velocityCorrectionMap.find(velRange) == velocityCorrectionMap.end()) |
int from, to; |
697 |
throw Korg::Exception("Internal error: inconsistency in velocity split correction map"); |
if (parseNumberRange(kmpInstr->Name(), from, to) == kmpInstr->Name()) |
698 |
velRange = velocityCorrectionMap[velRange]; // corrected |
throw Korg::Exception("Internal error: parsing velocity range failed"); |
699 |
} |
velRange.low = from; |
700 |
|
velRange.high = to; |
701 |
// create velocity split dimension if it doesn't exist already ... |
if (velocityCorrectionMap.find(velRange) == velocityCorrectionMap.end()) |
702 |
iVelocityDimensionIndex = getDimensionIndex(gigRegion, gig::dimension_velocity); |
throw Korg::Exception("Internal error: inconsistency in velocity split correction map"); |
703 |
if (iVelocityDimensionIndex < 0) { |
velRange = velocityCorrectionMap[velRange]; // corrected |
704 |
gig::dimension_def_t dim; |
} |
|
dim.dimension = gig::dimension_velocity; |
|
|
dim.zones = origVelocitySplits.size(); |
|
|
// Find the number of bits required to hold the |
|
|
// specified amount of zones. |
|
|
int zoneBits = dim.zones - 1; |
|
|
for (dim.bits = 0; zoneBits > 1; dim.bits += 2, zoneBits >>= 2); |
|
|
dim.bits += zoneBits; |
|
|
cout << "Adding velocity dimension: zones=" << (int)dim.zones << ", bits=" << (int)dim.bits << endl; |
|
|
gigRegion->AddDimension(&dim); |
|
705 |
|
|
706 |
|
// create velocity split dimension if it doesn't exist already ... |
707 |
iVelocityDimensionIndex = getDimensionIndex(gigRegion, gig::dimension_velocity); |
iVelocityDimensionIndex = getDimensionIndex(gigRegion, gig::dimension_velocity); |
708 |
} |
if (iVelocityDimensionIndex < 0) { |
709 |
|
gig::dimension_def_t dim; |
710 |
|
dim.dimension = gig::dimension_velocity; |
711 |
|
dim.zones = origVelocitySplits.size(); |
712 |
|
// Find the number of bits required to hold the |
713 |
|
// specified amount of zones. |
714 |
|
int zoneBits = dim.zones - 1; |
715 |
|
for (dim.bits = 0; zoneBits > 1; dim.bits += 2, zoneBits >>= 2); |
716 |
|
dim.bits += zoneBits; |
717 |
|
cout << "Adding velocity dimension: zones=" << (int)dim.zones << ", bits=" << (int)dim.bits << endl; |
718 |
|
gigRegion->AddDimension(&dim); |
719 |
|
|
720 |
|
iVelocityDimensionIndex = getDimensionIndex(gigRegion, gig::dimension_velocity); |
721 |
|
} |
722 |
|
|
723 |
if (iVelocityDimensionIndex < 0) |
if (iVelocityDimensionIndex < 0) |
724 |
throw gig::Exception("Internal error: Could not resolve target velocity dimension bit"); |
throw gig::Exception("Internal error: Could not resolve target velocity dimension bit"); |
725 |
|
|
726 |
// find the velocity zone for this one |
// find the velocity zone for this one |
727 |
int iVelocitySplitZone = -1; |
int iVelocitySplitZone = -1; |
|
{ |
|
|
int i = 0; |
|
|
for (map<DLS::range_t,DLS::range_t>::const_iterator itVelSplit = velocityCorrectionMap.begin(); |
|
|
itVelSplit != velocityCorrectionMap.end(); ++itVelSplit, ++i) |
|
728 |
{ |
{ |
729 |
if (itVelSplit->second == velRange) { // already corrected before, thus second, not first |
int i = 0; |
730 |
iVelocitySplitZone = i; |
for (map<DLS::range_t,DLS::range_t>::const_iterator itVelSplit = velocityCorrectionMap.begin(); |
731 |
break; |
itVelSplit != velocityCorrectionMap.end(); ++itVelSplit, ++i) |
732 |
|
{ |
733 |
|
if (itVelSplit->second == velRange) { // already corrected before, thus second, not first |
734 |
|
iVelocitySplitZone = i; |
735 |
|
break; |
736 |
|
} |
737 |
} |
} |
738 |
|
if (iVelocitySplitZone == -1) |
739 |
|
throw gig::Exception("Internal error: Could not resolve target velocity dimension zone"); |
740 |
} |
} |
|
if (iVelocitySplitZone == -1) |
|
|
throw gig::Exception("Internal error: Could not resolve target velocity dimension zone"); |
|
|
} |
|
741 |
|
|
742 |
// select dimension bit for this stereo dimension split |
// select dimension bit for this stereo dimension split |
743 |
iDimBits[iVelocityDimensionIndex] = iVelocitySplitZone; |
iDimBits[iVelocityDimensionIndex] = iVelocitySplitZone; |
744 |
|
} |
745 |
} |
} |
746 |
|
|
747 |
// resolve target gig::DimensionRegion for the left/right and velocity split zone detected above |
// resolve target gig::DimensionRegion for the left/right and velocity split zone detected above |
757 |
|
|
758 |
// assign the respective gig sample to this dimension region |
// assign the respective gig sample to this dimension region |
759 |
gig::Sample* gigSample = findOrCreateGigSampleForKSFRegion(kmpRegion); |
gig::Sample* gigSample = findOrCreateGigSampleForKSFRegion(kmpRegion); |
760 |
dimRgn->pSample = gigSample; // might be NULL (if Korg sample had zero size) |
dimRgn->pSample = gigSample; // might be NULL (if Korg sample had zero size, or if the original instrument's internal samples were used) |
761 |
if (gigSample) { |
if (gigSample) { |
762 |
dimRgn->UnityNote = gigSample->MIDIUnityNote; |
dimRgn->UnityNote = gigSample->MIDIUnityNote; |
763 |
if (gigSample->Loops) { |
if (gigSample->Loops) { |