37 #define INITIAL_SAMPLE_BUFFER_SIZE 512000 // 512 kB
40 #define GIG_EXP_DECODE(x) (pow(1.000000008813822, x))
41 #define GIG_EXP_ENCODE(x) (log(x) / log(1.000000008813822))
42 #define GIG_PITCH_TRACK_EXTRACT(x) (!(x & 0x01))
43 #define GIG_PITCH_TRACK_ENCODE(x) ((x) ? 0x00 : 0x01)
44 #define GIG_VCF_RESONANCE_CTRL_EXTRACT(x) ((x >> 4) & 0x03)
45 #define GIG_VCF_RESONANCE_CTRL_ENCODE(x) ((x & 0x03) << 4)
46 #define GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(x) ((x >> 1) & 0x03)
47 #define GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(x) ((x >> 3) & 0x03)
48 #define GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(x) ((x >> 5) & 0x03)
49 #define GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(x) ((x & 0x03) << 1)
50 #define GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(x) ((x & 0x03) << 3)
51 #define GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(x) ((x & 0x03) << 5)
66 static void __notify_progress(
progress_t* pProgress,
float subprogress) {
67 if (pProgress && pProgress->
callback) {
69 const float totalprogress = pProgress->
__range_min + subprogress * totalrange;
70 pProgress->
factor = totalprogress;
76 static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress,
float totalTasks,
float currentTask) {
77 if (pParentProgress && pParentProgress->callback) {
78 const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min;
79 pSubProgress->callback = pParentProgress->callback;
80 pSubProgress->custom = pParentProgress->custom;
81 pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
82 pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
92 inline int get12lo(
const unsigned char* pSrc)
94 const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8;
95 return x & 0x800 ? x - 0x1000 : x;
98 inline int get12hi(
const unsigned char* pSrc)
100 const int x = pSrc[1] >> 4 | pSrc[2] << 4;
101 return x & 0x800 ? x - 0x1000 : x;
104 inline int16_t get16(
const unsigned char* pSrc)
106 return int16_t(pSrc[0] | pSrc[1] << 8);
109 inline int get24(
const unsigned char* pSrc)
111 const int x = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16;
112 return x & 0x800000 ? x - 0x1000000 : x;
115 inline void store24(
unsigned char* pDst,
int x)
122 void Decompress16(
int compressionmode,
const unsigned char* params,
123 int srcStep,
int dstStep,
124 const unsigned char* pSrc, int16_t* pDst,
125 unsigned long currentframeoffset,
126 unsigned long copysamples)
128 switch (compressionmode) {
130 pSrc += currentframeoffset * srcStep;
131 while (copysamples) {
140 int y = get16(params);
141 int dy = get16(params + 2);
142 while (currentframeoffset) {
146 currentframeoffset--;
148 while (copysamples) {
160 void Decompress24(
int compressionmode,
const unsigned char* params,
161 int dstStep,
const unsigned char* pSrc, uint8_t* pDst,
162 unsigned long currentframeoffset,
163 unsigned long copysamples,
int truncatedBits)
165 int y, dy, ddy, dddy;
167 #define GET_PARAMS(params) \
169 dy = y - get24((params) + 3); \
170 ddy = get24((params) + 6); \
171 dddy = get24((params) + 9)
173 #define SKIP_ONE(x) \
179 #define COPY_ONE(x) \
181 store24(pDst, y << truncatedBits); \
184 switch (compressionmode) {
186 pSrc += currentframeoffset * 3;
187 while (copysamples) {
188 store24(pDst, get24(pSrc) << truncatedBits);
197 while (currentframeoffset) {
200 currentframeoffset--;
202 while (copysamples) {
211 while (currentframeoffset > 1) {
215 currentframeoffset -= 2;
217 if (currentframeoffset) {
219 currentframeoffset--;
226 while (copysamples > 1) {
239 while (currentframeoffset) {
241 currentframeoffset--;
243 while (copysamples) {
251 const int bytesPerFrame[] = { 4096, 2052, 768, 524, 396, 268 };
252 const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 };
253 const int headerSize[] = { 0, 4, 0, 12, 12, 12 };
254 const int bitsPerSample[] = { 16, 8, 24, 16, 12, 8 };
262 static uint32_t* __initCRCTable() {
263 static uint32_t res[256];
265 for (
int i = 0 ; i < 256 ; i++) {
267 for (
int j = 0 ; j < 8 ; j++) {
268 c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1;
275 static const uint32_t* __CRCTable = __initCRCTable();
282 inline static void __resetCRC(uint32_t& crc) {
305 static void __calculateCRC(
unsigned char* buf,
int bufSize, uint32_t& crc) {
306 for (
int i = 0 ; i < bufSize ; i++) {
307 crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);
316 inline static uint32_t __encodeCRC(
const uint32_t& crc) {
317 return crc ^ 0xffffffff;
338 static int __resolveZoneSize(dimension_def_t& dimension_definition) {
340 ? int(128.0 / dimension_definition.zones) : 0;
438 if (version == 3 &&
BitDepth == 24) {
443 ScanCompressedSample();
487 store32(&pData[28],
Loops);
491 store32(&pData[36],
LoopID);
502 uint16_t iSampleGroup = 0;
504 if (pFile->pGroups) {
505 std::list<Group*>::iterator iter = pFile->pGroups->begin();
506 std::list<Group*>::iterator end = pFile->pGroups->end();
507 for (
int i = 0; iter != end; i++, iter++) {
516 store16(&pData[0], iSampleGroup);
520 void Sample::ScanCompressedSample() {
523 std::list<unsigned long> frameOffsets;
531 for (
int i = 0 ; ; i++) {
538 if (mode_l > 5 || mode_r > 5)
throw gig::Exception(
"Unknown compression mode");
539 const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r];
544 (bitsPerSample[mode_l] + bitsPerSample[mode_r]);
553 for (
int i = 0 ; ; i++) {
558 const unsigned long frameSize = bytesPerFrame[mode];
574 FrameTable =
new unsigned long[frameOffsets.size()];
575 std::list<unsigned long>::iterator end = frameOffsets.end();
576 std::list<unsigned long>::iterator iter = frameOffsets.begin();
577 for (
int i = 0; iter != end; i++, iter++) {
679 unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->
FrameSize;
795 unsigned long frame = this->
SamplePos / 2048;
801 unsigned long orderedBytes = SampleCount * this->
FrameSize;
802 unsigned long result =
pCkData->
SetPos(orderedBytes, Whence);
803 return (result == orderedBytes) ? SampleCount
852 unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend;
853 uint8_t* pDst = (uint8_t*) pBuffer;
862 if (
GetPos() <= loopEnd) {
870 if (!pPlaybackState->
reverse) {
872 samplestoloopend = loopEnd -
GetPos();
873 readsamples =
Read(&pDst[totalreadsamples * this->
FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
874 samplestoread -= readsamples;
875 totalreadsamples += readsamples;
876 if (readsamples == samplestoloopend) {
877 pPlaybackState->
reverse =
true;
880 }
while (samplestoread && readsamples);
890 unsigned long swapareastart = totalreadsamples;
892 unsigned long samplestoreadinloop = Min(samplestoread, loopoffset);
893 unsigned long reverseplaybackend =
GetPos() - samplestoreadinloop;
895 SetPos(reverseplaybackend);
899 readsamples =
Read(&pDst[totalreadsamples * this->
FrameSize], samplestoreadinloop, pExternalDecompressionBuffer);
900 samplestoreadinloop -= readsamples;
901 samplestoread -= readsamples;
902 totalreadsamples += readsamples;
903 }
while (samplestoreadinloop && readsamples);
905 SetPos(reverseplaybackend);
907 if (reverseplaybackend == loop.
LoopStart) {
909 pPlaybackState->
reverse =
false;
913 if (totalreadsamples > swapareastart)
914 SwapMemoryArea(&pDst[swapareastart * this->
FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
916 }
while (samplestoread && readsamples);
922 if (!pPlaybackState->
reverse)
do {
923 samplestoloopend = loopEnd -
GetPos();
924 readsamples =
Read(&pDst[totalreadsamples * this->
FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
925 samplestoread -= readsamples;
926 totalreadsamples += readsamples;
927 if (readsamples == samplestoloopend) {
928 pPlaybackState->
reverse =
true;
931 }
while (samplestoread && readsamples);
933 if (!samplestoread)
break;
941 unsigned long swapareastart = totalreadsamples;
945 unsigned long reverseplaybackend = loop.
LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.
LoopLength);
947 SetPos(reverseplaybackend);
953 samplestoloopend = loopEnd -
GetPos();
954 readsamples =
Read(&pDst[totalreadsamples * this->
FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer);
955 samplestoreadinloop -= readsamples;
956 samplestoread -= readsamples;
957 totalreadsamples += readsamples;
958 if (readsamples == samplestoloopend) {
962 }
while (samplestoreadinloop && readsamples);
964 SetPos(reverseplaybackend);
967 SwapMemoryArea(&pDst[swapareastart * this->
FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
975 samplestoloopend = loopEnd -
GetPos();
976 readsamples =
Read(&pDst[totalreadsamples * this->
FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
977 samplestoread -= readsamples;
978 totalreadsamples += readsamples;
979 if (readsamples == samplestoloopend) {
983 }
while (samplestoread && readsamples);
991 if (samplestoread)
do {
992 readsamples =
Read(&pDst[totalreadsamples * this->
FrameSize], samplestoread, pExternalDecompressionBuffer);
993 samplestoread -= readsamples;
994 totalreadsamples += readsamples;
995 }
while (readsamples && samplestoread);
1000 return totalreadsamples;
1026 if (SampleCount == 0)
return 0;
1033 return Channels == 2 ?
pCkData->
Read(pBuffer, SampleCount << 1, 2) >> 1
1040 unsigned long assumedsize =
GuessSize(SampleCount),
1042 remainingsamples = SampleCount,
1043 copysamples, skipsamples,
1050 if (pDecompressionBuffer->
Size < assumedsize) {
1051 std::cerr <<
"gig::Read(): WARNING - decompression buffer size too small!" << std::endl;
1053 remainingsamples = SampleCount;
1057 unsigned char* pSrc = (
unsigned char*) pDecompressionBuffer->
pStart;
1058 int16_t* pDst = static_cast<int16_t*>(pBuffer);
1059 uint8_t* pDst24 =
static_cast<uint8_t*
>(pBuffer);
1060 remainingbytes =
pCkData->
Read(pSrc, assumedsize, 1);
1062 while (remainingsamples && remainingbytes) {
1064 unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset;
1066 int mode_l = *pSrc++, mode_r = 0;
1068 if (Channels == 2) {
1070 framebytes = bytesPerFrame[mode_l] + bytesPerFrame[mode_r] + 2;
1071 rightChannelOffset = bytesPerFrameNoHdr[mode_l];
1072 nextFrameOffset = rightChannelOffset + bytesPerFrameNoHdr[mode_r];
1073 if (remainingbytes < framebytes) {
1075 if (mode_l == 4 && (framesamples & 1)) {
1076 rightChannelOffset = ((framesamples + 1) * bitsPerSample[mode_l]) >> 3;
1079 rightChannelOffset = (framesamples * bitsPerSample[mode_l]) >> 3;
1084 framebytes = bytesPerFrame[mode_l] + 1;
1085 nextFrameOffset = bytesPerFrameNoHdr[mode_l];
1086 if (remainingbytes < framebytes) {
1092 if (currentframeoffset + remainingsamples >= framesamples) {
1093 if (currentframeoffset <= framesamples) {
1094 copysamples = framesamples - currentframeoffset;
1095 skipsamples = currentframeoffset;
1099 skipsamples = framesamples;
1106 copysamples = remainingsamples;
1107 skipsamples = currentframeoffset;
1109 this->
FrameOffset = currentframeoffset + copysamples;
1111 remainingsamples -= copysamples;
1113 if (remainingbytes > framebytes) {
1114 remainingbytes -= framebytes;
1115 if (remainingsamples == 0 &&
1116 currentframeoffset + copysamples == framesamples) {
1124 else remainingbytes = 0;
1126 currentframeoffset -= skipsamples;
1128 if (copysamples == 0) {
1133 const unsigned char*
const param_l = pSrc;
1135 if (mode_l != 2) pSrc += 12;
1137 if (Channels == 2) {
1138 const unsigned char*
const param_r = pSrc;
1139 if (mode_r != 2) pSrc += 12;
1141 Decompress24(mode_l, param_l, 6, pSrc, pDst24,
1143 Decompress24(mode_r, param_r, 6, pSrc + rightChannelOffset, pDst24 + 3,
1145 pDst24 += copysamples * 6;
1148 Decompress24(mode_l, param_l, 3, pSrc, pDst24,
1150 pDst24 += copysamples * 3;
1154 if (mode_l) pSrc += 4;
1157 if (Channels == 2) {
1158 const unsigned char*
const param_r = pSrc;
1159 if (mode_r) pSrc += 4;
1161 step = (2 - mode_l) + (2 - mode_r);
1162 Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples);
1163 Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1,
1164 skipsamples, copysamples);
1165 pDst += copysamples << 1;
1169 Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples);
1170 pDst += copysamples;
1173 pSrc += nextFrameOffset;
1177 if (remainingsamples && remainingbytes < WorstCaseFrameSize && pCkData->GetState() ==
RIFF::stream_ready) {
1178 assumedsize =
GuessSize(remainingsamples);
1182 pSrc = (
unsigned char*) pDecompressionBuffer->
pStart;
1186 this->
SamplePos += (SampleCount - remainingsamples);
1188 return (SampleCount - remainingsamples);
1222 if (
GetSize() < SampleCount)
throw Exception(
"Could not write sample data, current sample size to small");
1227 res = Channels == 2 ?
pCkData->
Write(pBuffer, SampleCount << 1, 2) >> 1
1230 __calculateCRC((
unsigned char *)pBuffer, SampleCount *
FrameSize, crc);
1259 const double worstCaseHeaderOverhead =
1260 (256.0 + 12.0 + 2.0 ) / 256.0;
1261 result.
Size = (
unsigned long) (
double(MaxReadSize) * 3.0 * 2.0 * worstCaseHeaderOverhead);
1275 if (DecompressionBuffer.
Size && DecompressionBuffer.
pStart) {
1276 delete[] (int8_t*) DecompressionBuffer.
pStart;
1277 DecompressionBuffer.
pStart = NULL;
1278 DecompressionBuffer.
Size = 0;
1311 uint DimensionRegion::Instances = 0;
1312 DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL;
1323 if (!pVelocityTables) pVelocityTables =
new VelocityTableMap;
1344 uint8_t eg1ctrloptions = _3ewa->
ReadUint8();
1350 uint8_t eg2ctrloptions = _3ewa->
ReadUint8();
1376 uint8_t velocityresponse = _3ewa->
ReadUint8();
1377 if (velocityresponse < 5) {
1380 }
else if (velocityresponse < 10) {
1383 }
else if (velocityresponse < 15) {
1390 uint8_t releasevelocityresponse = _3ewa->
ReadUint8();
1391 if (releasevelocityresponse < 5) {
1394 }
else if (releasevelocityresponse < 10) {
1397 }
else if (releasevelocityresponse < 15) {
1409 uint8_t pitchTrackDimensionBypass = _3ewa->
ReadInt8();
1415 Pan = (pan < 64) ? pan : -((
int)pan - 63);
1427 bool extResonanceCtrl = lfo2ctrl & 0x40;
1435 EG3Depth = (eg3depth <= 1200) ? eg3depth
1436 : (-1) * (int16_t) ((eg3depth ^ 0xfff) + 1);
1439 uint8_t regoptions = _3ewa->
ReadUint8();
1454 uint8_t vcfvelscale = _3ewa->
ReadUint8();
1458 uint8_t vcfresonance = _3ewa->
ReadUint8();
1461 uint8_t vcfbreakpoint = _3ewa->
ReadUint8();
1464 uint8_t vcfvelocity = _3ewa->
ReadUint8();
1469 if (lfo3ctrl & 0x40)
1564 pVelocityReleaseTable = GetReleaseVelocityTable(
1591 for (
int k = 0 ; k < 128 ; k++)
1626 for (
int k = 0 ; k < 128 ; k++)
1674 const uint32_t chunksize = _3ewa->
GetNewSize();
1675 store32(&pData[0], chunksize);
1678 store32(&pData[4], lfo3freq);
1681 store32(&pData[8], eg3attack);
1700 store32(&pData[28], eg1attack);
1703 store32(&pData[32], eg1decay1);
1710 store32(&pData[40], eg1release);
1712 const uint8_t eg1ctl = (uint8_t) EncodeLeverageController(
EG1Controller);
1715 const uint8_t eg1ctrloptions =
1720 pData[45] = eg1ctrloptions;
1722 const uint8_t eg2ctl = (uint8_t) EncodeLeverageController(
EG2Controller);
1725 const uint8_t eg2ctrloptions =
1730 pData[47] = eg2ctrloptions;
1733 store32(&pData[48], lfo1freq);
1736 store32(&pData[52], eg2attack);
1739 store32(&pData[56], eg2decay1);
1746 store32(&pData[64], eg2release);
1753 store32(&pData[72], lfo2freq);
1760 store32(&pData[80], eg1decay2);
1767 store32(&pData[88], eg2decay2);
1780 velocityresponse += 5;
1783 velocityresponse += 10;
1787 throw Exception(
"Could not update DimensionRegion's chunk, unknown VelocityResponseCurve selected");
1789 pData[96] = velocityresponse;
1799 releasevelocityresponse += 5;
1802 releasevelocityresponse += 10;
1806 throw Exception(
"Could not update DimensionRegion's chunk, unknown ReleaseVelocityResponseCurve selected");
1808 pData[97] = releasevelocityresponse;
1825 pitchTrackDimensionBypass |= 0x10;
1828 pitchTrackDimensionBypass |= 0x20;
1834 throw Exception(
"Could not update DimensionRegion's chunk, unknown DimensionBypass selected");
1836 pData[108] = pitchTrackDimensionBypass;
1839 const uint8_t pan = (
Pan >= 0) ?
Pan : ((-
Pan) + 63);
1842 const uint8_t selfmask = (
SelfMask) ? 0x01 : 0x00;
1843 pData[110] = selfmask;
1852 pData[112] = lfo3ctrl;
1856 pData[113] = attenctl;
1863 pData[114] = lfo2ctrl;
1872 pData[115] = lfo1ctrl;
1876 : uint16_t(((-
EG3Depth) - 1) ^ 0xfff);
1877 store16(&pData[116], eg3depth);
1882 pData[120] = channeloffset;
1885 uint8_t regoptions = 0;
1888 pData[121] = regoptions;
1901 const uint8_t eg1hold = (
EG1Hold) ? 0x80 : 0x00;
1902 pData[131] = eg1hold;
1904 const uint8_t vcfcutoff = (
VCFEnabled ? 0x80 : 0x00) |
1906 pData[132] = vcfcutoff;
1912 pData[134] = vcfvelscale;
1918 pData[136] = vcfresonance;
1922 pData[137] = vcfbreakpoint;
1926 pData[138] = vcfvelocity;
1929 pData[139] = vcftype;
1931 if (chunksize >= 148) {
1936 double* DimensionRegion::GetReleaseVelocityTable(
curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) {
1938 uint8_t depth = releaseVelocityResponseDepth;
1947 return GetVelocityTable(curveType, depth, 0);
1950 double* DimensionRegion::GetCutoffVelocityTable(
curve_type_t vcfVelocityCurve,
1951 uint8_t vcfVelocityDynamicRange,
1952 uint8_t vcfVelocityScale,
1956 uint8_t depth = vcfVelocityDynamicRange;
1965 return GetVelocityTable(curveType, depth,
1967 ? vcfVelocityScale : 0);
1971 double* DimensionRegion::GetVelocityTable(
curve_type_t curveType, uint8_t depth, uint8_t scaling)
1974 uint32_t tableKey = (curveType<<16) | (depth<<8) | scaling;
1975 if (pVelocityTables->count(tableKey)) {
1976 table = (*pVelocityTables)[tableKey];
1979 table = CreateVelocityTable(curveType, depth, scaling);
1980 (*pVelocityTables)[tableKey] = table;
1989 leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {
1991 switch (EncodedController) {
1993 case _lev_ctrl_none:
1997 case _lev_ctrl_velocity:
2001 case _lev_ctrl_channelaftertouch:
2007 case _lev_ctrl_modwheel:
2011 case _lev_ctrl_breath:
2015 case _lev_ctrl_foot:
2019 case _lev_ctrl_effect1:
2023 case _lev_ctrl_effect2:
2027 case _lev_ctrl_genpurpose1:
2031 case _lev_ctrl_genpurpose2:
2035 case _lev_ctrl_genpurpose3:
2039 case _lev_ctrl_genpurpose4:
2043 case _lev_ctrl_portamentotime:
2047 case _lev_ctrl_sustainpedal:
2051 case _lev_ctrl_portamento:
2055 case _lev_ctrl_sostenutopedal:
2059 case _lev_ctrl_softpedal:
2063 case _lev_ctrl_genpurpose5:
2067 case _lev_ctrl_genpurpose6:
2071 case _lev_ctrl_genpurpose7:
2075 case _lev_ctrl_genpurpose8:
2079 case _lev_ctrl_effect1depth:
2083 case _lev_ctrl_effect2depth:
2087 case _lev_ctrl_effect3depth:
2091 case _lev_ctrl_effect4depth:
2095 case _lev_ctrl_effect5depth:
2104 return decodedcontroller;
2107 DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {
2108 _lev_ctrl_t encodedcontroller;
2109 switch (DecodedController.type) {
2112 encodedcontroller = _lev_ctrl_none;
2115 encodedcontroller = _lev_ctrl_velocity;
2118 encodedcontroller = _lev_ctrl_channelaftertouch;
2123 switch (DecodedController.controller_number) {
2125 encodedcontroller = _lev_ctrl_modwheel;
2128 encodedcontroller = _lev_ctrl_breath;
2131 encodedcontroller = _lev_ctrl_foot;
2134 encodedcontroller = _lev_ctrl_effect1;
2137 encodedcontroller = _lev_ctrl_effect2;
2140 encodedcontroller = _lev_ctrl_genpurpose1;
2143 encodedcontroller = _lev_ctrl_genpurpose2;
2146 encodedcontroller = _lev_ctrl_genpurpose3;
2149 encodedcontroller = _lev_ctrl_genpurpose4;
2152 encodedcontroller = _lev_ctrl_portamentotime;
2155 encodedcontroller = _lev_ctrl_sustainpedal;
2158 encodedcontroller = _lev_ctrl_portamento;
2161 encodedcontroller = _lev_ctrl_sostenutopedal;
2164 encodedcontroller = _lev_ctrl_softpedal;
2167 encodedcontroller = _lev_ctrl_genpurpose5;
2170 encodedcontroller = _lev_ctrl_genpurpose6;
2173 encodedcontroller = _lev_ctrl_genpurpose7;
2176 encodedcontroller = _lev_ctrl_genpurpose8;
2179 encodedcontroller = _lev_ctrl_effect1depth;
2182 encodedcontroller = _lev_ctrl_effect2depth;
2185 encodedcontroller = _lev_ctrl_effect3depth;
2188 encodedcontroller = _lev_ctrl_effect4depth;
2191 encodedcontroller = _lev_ctrl_effect5depth;
2194 throw gig::Exception(
"leverage controller number is not supported by the gig format");
2200 return encodedcontroller;
2207 VelocityTableMap::iterator iter;
2208 for (iter = pVelocityTables->begin(); iter != pVelocityTables->end(); iter++) {
2209 double* pTable = iter->second;
2210 if (pTable)
delete[] pTable;
2212 pVelocityTables->clear();
2213 delete pVelocityTables;
2214 pVelocityTables = NULL;
2231 return pVelocityAttenuationTable[MIDIKeyVelocity];
2235 return pVelocityReleaseTable[MIDIKeyVelocity];
2239 return pVelocityCutoffTable[MIDIKeyVelocity];
2247 pVelocityAttenuationTable =
2259 pVelocityAttenuationTable =
2271 pVelocityAttenuationTable =
2332 double* DimensionRegion::CreateVelocityTable(
curve_type_t curveType, uint8_t depth, uint8_t scaling) {
2337 const int lin0[] = { 1, 1, 127, 127 };
2338 const int lin1[] = { 1, 21, 127, 127 };
2339 const int lin2[] = { 1, 45, 127, 127 };
2340 const int lin3[] = { 1, 74, 127, 127 };
2341 const int lin4[] = { 1, 127, 127, 127 };
2344 const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 };
2345 const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127,
2347 const int non2[] = { 1, 4, 46, 9, 57, 20, 102, 107, 107, 127,
2349 const int non3[] = { 1, 15, 10, 19, 67, 73, 80, 80, 90, 98, 98, 127,
2351 const int non4[] = { 1, 25, 33, 57, 82, 81, 92, 127, 127, 127 };
2354 const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44,
2355 113, 127, 127, 127 };
2356 const int spe1[] = { 1, 2, 27, 5, 67, 18, 89, 29, 95, 35, 107, 67,
2357 118, 127, 127, 127 };
2358 const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74,
2359 85, 90, 91, 127, 127, 127 };
2360 const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73,
2361 117, 127, 127, 127 };
2362 const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127,
2366 const int spe5[] = { 1, 2, 30, 5, 60, 19, 77, 70, 83, 85, 88, 106,
2367 91, 127, 127, 127 };
2369 const int*
const curves[] = { non0, non1, non2, non3, non4,
2370 lin0, lin1, lin2, lin3, lin4,
2371 spe0, spe1, spe2, spe3, spe4, spe5 };
2373 double*
const table =
new double[128];
2375 const int* curve = curves[curveType * 5 + depth];
2376 const int s = scaling == 0 ? 20 : scaling;
2379 for (
int x = 1 ; x < 128 ; x++) {
2381 if (x > curve[2]) curve += 2;
2382 double y = curve[1] + (x - curve[0]) *
2383 (
double(curve[3] - curve[1]) / (curve[2] - curve[0]));
2388 if (s < 20 && y >= 0.5)
2389 y = y / ((2 - 40.0 / s) * y + 40.0 / s - 1);
2406 for (
int i = 0; i < 256; i++) {
2422 for (
int i = 0; i < dimensionBits; i++) {
2463 uint32_t wavepoolindex = _3lnk->
ReadUint32();
2470 for (
int i = 0 ; i < 8 ; i++) {
2513 const int iMaxDimensions = version3 ? 8 : 5;
2514 const int iMaxDimensionRegions = version3 ? 256 : 32;
2519 const int _3lnkChunkSize = version3 ? 1092 : 172;
2529 store32(&pData[0], DimensionRegions);
2531 for (
int i = 0; i < iMaxDimensions; i++) {
2543 const int iWavePoolOffset = version3 ? 68 : 44;
2544 for (uint i = 0; i < iMaxDimensionRegions; i++) {
2545 int iWaveIndex = -1;
2546 if (i < DimensionRegions) {
2548 File::SampleList::iterator iter = pFile->
pSamples->begin();
2549 File::SampleList::iterator end = pFile->
pSamples->end();
2550 for (
int index = 0; iter != end; ++iter, ++index) {
2557 store32(&pData[iWavePoolOffset + i * 4], iWaveIndex);
2564 int dimensionRegionNr = 0;
2569 dimensionRegionNr++;
2573 if (dimensionRegionNr == 0)
throw gig::Exception(
"No dimension region found.");
2593 if (veldim == -1)
return;
2609 table =
new uint8_t[128];
2613 int velocityZone = 0;
2615 for (
int k = i ; k < end ; k += step) {
2617 for (; tableidx <= d->
DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone;
2621 for (
int k = i ; k < end ; k += step) {
2623 for (; tableidx <= d->
VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2637 if (j == veldim) i += skipveldim;
2650 if (j == Dimensions)
break;
2674 throw gig::Exception(
"Could not add new dimension, max. amount of " + ToString(iMaxDimensions) +
" dimensions already reached");
2676 int iCurrentBits = 0;
2679 if (iCurrentBits >= iMaxDimensions)
2680 throw gig::Exception(
"Could not add new dimension, max. amount of " + ToString(iMaxDimensions) +
" dimension bits already reached");
2681 const int iNewBits = iCurrentBits + pDimDef->
bits;
2682 if (iNewBits > iMaxDimensions)
2683 throw gig::Exception(
"Could not add new dimension, new dimension would exceed max. amount of " + ToString(iMaxDimensions) +
" dimension bits");
2687 throw gig::Exception(
"Could not add new dimension, there is already a dimension of the same type");
2694 for (
int i = 0 ; i < pos ; i++)
2699 for (
int i = 0 ; i < (1 << iCurrentBits) ; i++) {
2700 for (
int j = Dimensions ; j > pos ; j--) {
2720 for (
int i = (1 << iCurrentBits) - (1 << bitpos) ; i >= 0 ; i -= (1 << bitpos)) {
2721 for (
int k = 0 ; k < (1 << bitpos) ; k++) {
2724 for (
int j = 1 ; j < (1 << pDimDef->
bits) ; j++) {
2725 for (
int k = 0 ; k < (1 << bitpos) ; k++) {
2727 if (moveTo) _3prg->
MoveSubChunk(pNewDimRgnListChunk, moveTo);
2740 int mask = (1 << bitpos) - 1;
2741 for (
int z = 0 ; z < pDimDef->
zones ; z++) {
2742 uint8_t upperLimit = uint8_t((z + 1) * 128.0 / pDimDef->
zones - 1);
2743 for (
int i = 0 ; i < 1 << iCurrentBits ; i++) {
2771 int iDimensionNr = -1;
2778 if (iDimensionNr < 0)
throw gig::Exception(
"Invalid dimension_def_t pointer");
2782 for (
int i = 0; i < iDimensionNr; i++)
2787 for (
int i = iDimensionNr + 1; i <
Dimensions; i++)
2794 for (
int iUpperBit = 0; iUpperBit < 1 << iUpperBits; iUpperBit++) {
2796 for (
int iLowerBit = 0; iLowerBit < 1 << iLowerBits; iLowerBit++) {
2798 iObsoleteBit << iLowerBits |
2811 for (
int iFrom = 2, iTo = 1; iFrom < 256 && iTo < 256 - 1; iTo++) {
2813 if (iFrom <= iTo) iFrom = iTo + 1;
2825 for (
int i = iDimensionNr + 1; i <
Dimensions; i++) {
2832 for (
int i = iDimensionNr + 1; i <
Dimensions; i++) {
2846 for (
int i = 0; i < 256; i++) {
2886 if (DimValues[i] <=
pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i])
break;
2895 bits = DimValues[i] & limiter_mask;
2898 dimregidx |= bits << bitpos;
2910 dimregidx |= bits << velbitpos;
2951 if ((int32_t)WavePoolTableIndex == -1)
return NULL;
2989 for (
int i = 0; i < 256; i++) {
3009 for (
int i = 0 ; i <
Triggers ; i++) {
3043 pMidiRules[0] = NULL;
3054 uint8_t dimkeystart = _3ewg->
ReadUint8();
3066 if (id1 == 4 && id2 == 16) {
3071 pMidiRules[i] = NULL;
3083 __notify_progress(pProgress, (
float)
pRegions->size() / (float)
Regions);
3093 __notify_progress(pProgress, 1.0f);
3098 RegionList::iterator iter =
pRegions->begin();
3099 RegionList::iterator end =
pRegions->end();
3100 for (; iter != end; ++iter) {
3102 for (
int iKey = pRegion->
KeyRange.
low; iKey <= pRegion->KeyRange.high; iKey++) {
3109 for (
int i = 0 ; pMidiRules[i] ; i++) {
3110 delete pMidiRules[i];
3112 delete[] pMidiRules;
3130 RegionList::iterator iter =
pRegions->begin();
3131 RegionList::iterator end =
pRegions->end();
3132 for (; iter != end; ++iter)
3133 (*iter)->UpdateChunks();
3157 pData[10] = dimkeystart;
3238 return pMidiRules[i];
3264 for (
int i = 0 ; pMidiRules[i] ; i++) {
3265 delete pMidiRules[i];
3268 pMidiRules[0] = NULL;
3274 RegionList::const_iterator it = orig->
pRegions->begin();
3275 for (
int i = 0; i < orig->
Regions; ++i, ++it) {
3279 static_cast<gig::Region*>(*it)
3299 pNameChunk = ck3gnm;
3329 if (strcmp(static_cast<char*>(ck->LoadChunkData()),
"") == 0) {
3354 if (pSample->GetGroup() ==
this)
return pSample;
3372 if (pSample->GetGroup() ==
this)
return pSample;
3392 Group* pOtherGroup = NULL;
3394 if (pOtherGroup !=
this)
break;
3397 "Could not move samples to another group, since there is no "
3398 "other Group. This is a bug, report it!"
3413 0, 2, 19980628 & 0xffff, 19980628 >> 16
3418 0, 3, 20030331 & 0xffff, 20030331 >> 16
3466 std::list<Group*>::iterator iter = pGroups->begin();
3467 std::list<Group*>::iterator end = pGroups->end();
3468 while (iter != end) {
3524 if (iter ==
pSamples->end())
throw gig::Exception(
"Could not delete sample, could not find given sample");
3533 for (
Region* region = instrument->GetFirstRegion() ; region ;
3534 region = instrument->GetNextRegion()) {
3536 if (region->GetSample() == pSample) region->SetSample(NULL);
3538 for (
int i = 0 ; i < region->DimensionRegions ; i++) {
3561 int iSampleIndex = 0;
3570 int nameLen = name.length();
3572 if (nameLen > 4 && name.substr(nameLen - 4) ==
".gig") nameLen -= 4;
3574 for (
int fileNo = 0 ; ; ) {
3577 unsigned long wvplFileOffset = wvpl->
GetFilePos();
3582 const float subprogress = (float) iSampleIndex / (
float) iTotalSamples;
3583 __notify_progress(pProgress, subprogress);
3585 unsigned long waveFileOffset = wave->
GetFilePos();
3586 pSamples->push_back(
new Sample(
this, wave, waveFileOffset - wvplFileOffset, fileNo));
3593 if (fileNo == lastFileNo)
break;
3597 sprintf(suffix,
".gx%02d", fileNo);
3598 name.replace(nameLen, 5, suffix);
3604 __notify_progress(pProgress, 1.0);
3633 __divide_progress(pProgress, &subprogress, 3.0f, 0.0f);
3634 __notify_progress(&subprogress, 0.0f);
3637 __notify_progress(&subprogress, 1.0f);
3640 if (pProgress && pProgress->
callback) {
3644 __notify_progress(&subprogress, 0.0f);
3646 __notify_progress(&subprogress, 1.0f);
3651 if (i == index)
return static_cast<gig::Instrument*
>( *InstrumentsIterator );
3730 if (lstInstruments) {
3731 int iInstrumentIndex = 0;
3736 const float localProgress = (float) iInstrumentIndex / (
float)
Instruments;
3737 __notify_progress(pProgress, localProgress);
3741 __divide_progress(pProgress, &subprogress,
Instruments, iInstrumentIndex);
3749 __notify_progress(pProgress, 1.0);
3761 int iWaveIndex = -1;
3762 File::SampleList::iterator iter =
pSamples->begin();
3763 File::SampleList::iterator end =
pSamples->end();
3764 for (
int index = 0; iter != end; ++iter, ++index) {
3765 if (*iter == pSample) {
3770 if (iWaveIndex < 0)
throw gig::Exception(
"Could not update crc, could not find sample");
3773 _3crc->
SetPos(iWaveIndex * 8);
3782 GroupsIterator = pGroups->begin();
3783 return *GroupsIterator;
3787 if (!pGroups)
return NULL;
3789 return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator;
3800 GroupsIterator = pGroups->begin();
3801 for (uint i = 0; GroupsIterator != pGroups->end(); i++) {
3802 if (i == index)
return *GroupsIterator;
3813 pGroups->push_back(pGroup);
3828 std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3829 if (iter == pGroups->end())
throw gig::Exception(
"Could not delete group, could not find given group");
3830 if (pGroups->size() == 1)
throw gig::Exception(
"Cannot delete group, there must be at least one default group!");
3836 pGroups->erase(iter);
3852 std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3853 if (iter == pGroups->end())
throw gig::Exception(
"Could not delete group, could not find given group");
3854 if (pGroups->size() == 1)
throw gig::Exception(
"Cannot delete group, there must be at least one default group!");
3857 pGroups->erase(iter);
3862 if (!pGroups) pGroups =
new std::list<Group*>;
3872 strcmp(static_cast<char*>(ck->
LoadChunkData()),
"") == 0)
break;
3874 pGroups->push_back(
new Group(
this, ck));
3881 if (!pGroups->size()) {
3883 pGroup->
Name =
"Default Group";
3884 pGroups->push_back(pGroup);
3911 if (first != info) {
3918 std::list<Group*>::iterator iter = pGroups->begin();
3919 std::list<Group*>::iterator end = pGroups->end();
3920 for (; iter != end; ++iter) {
3921 (*iter)->UpdateChunks();
3928 for (
int i = 0 ; i < 128 ; i++) {
3929 if (i >= pGroups->size()) ::SaveString(
CHUNK_ID_3GNM, _3gnm, _3gnl,
"",
"",
true, 64);
3951 int sublen =
pSamples->size() / 8 + 49;
3956 if (einf->
GetSize() != einfSize) {
3960 }
else if (newFile) {
3966 std::map<gig::Sample*,int> sampleMap;
3969 sampleMap[pSample] = sampleIdx++;
3972 int totnbusedsamples = 0;
3973 int totnbusedchannels = 0;
3974 int totnbregions = 0;
3975 int totnbdimregions = 0;
3977 int instrumentIdx = 0;
3979 memset(&pData[48], 0, sublen - 48);
3983 int nbusedsamples = 0;
3984 int nbusedchannels = 0;
3985 int nbdimregions = 0;
3988 memset(&pData[(instrumentIdx + 1) * sublen + 48], 0, sublen - 48);
3990 for (
Region* region = instrument->GetFirstRegion() ; region ;
3991 region = instrument->GetNextRegion()) {
3992 for (
int i = 0 ; i < region->DimensionRegions ; i++) {
3995 int sampleIdx = sampleMap[d->
pSample];
3996 int byte = 48 + sampleIdx / 8;
3997 int bit = 1 << (sampleIdx & 7);
3998 if ((pData[(instrumentIdx + 1) * sublen + byte] & bit) == 0) {
3999 pData[(instrumentIdx + 1) * sublen + byte] |= bit;
4003 if ((pData[byte] & bit) == 0) {
4012 nbdimregions += region->DimensionRegions;
4016 store32(&pData[(instrumentIdx + 1) * sublen + 4], nbusedchannels);
4017 store32(&pData[(instrumentIdx + 1) * sublen + 8], nbusedsamples);
4018 store32(&pData[(instrumentIdx + 1) * sublen + 12], 1);
4019 store32(&pData[(instrumentIdx + 1) * sublen + 16], instrument->Regions);
4020 store32(&pData[(instrumentIdx + 1) * sublen + 20], nbdimregions);
4021 store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops);
4023 store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx);
4024 store32(&pData[(instrumentIdx + 1) * sublen + 40],
pSamples->size());
4027 totnbregions += instrument->Regions;
4028 totnbdimregions += nbdimregions;
4029 totnbloops += nbloops;
4034 store32(&pData[4], totnbusedchannels);
4035 store32(&pData[8], totnbusedsamples);
4037 store32(&pData[16], totnbregions);
4038 store32(&pData[20], totnbdimregions);
4039 store32(&pData[24], totnbloops);
4042 store32(&pData[40],
pSamples->size());
4055 }
else if (newFile) {
4100 std::cout <<
"gig::Exception: " <<
Message << std::endl;