2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2016 by Christian Schoenebeck * |
* Copyright (C) 2003-2017 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
269 |
* steps. |
* steps. |
270 |
* |
* |
271 |
* Once the whole data was processed by __calculateCRC(), one should |
* Once the whole data was processed by __calculateCRC(), one should |
272 |
* call __encodeCRC() to get the final CRC result. |
* call __finalizeCRC() to get the final CRC result. |
273 |
* |
* |
274 |
* @param buf - pointer to data the CRC shall be calculated of |
* @param buf - pointer to data the CRC shall be calculated of |
275 |
* @param bufSize - size of the data to be processed |
* @param bufSize - size of the data to be processed |
276 |
* @param crc - variable the CRC sum shall be stored to |
* @param crc - variable the CRC sum shall be stored to |
277 |
*/ |
*/ |
278 |
static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) { |
static void __calculateCRC(unsigned char* buf, size_t bufSize, uint32_t& crc) { |
279 |
for (int i = 0 ; i < bufSize ; i++) { |
for (size_t i = 0 ; i < bufSize ; i++) { |
280 |
crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); |
crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); |
281 |
} |
} |
282 |
} |
} |
286 |
* |
* |
287 |
* @param crc - variable previously passed to __calculateCRC() |
* @param crc - variable previously passed to __calculateCRC() |
288 |
*/ |
*/ |
289 |
inline static uint32_t __encodeCRC(const uint32_t& crc) { |
inline static void __finalizeCRC(uint32_t& crc) { |
290 |
return crc ^ 0xffffffff; |
crc ^= 0xffffffff; |
291 |
} |
} |
292 |
|
|
293 |
|
|
1294 |
// if this is the last write, update the checksum chunk in the |
// if this is the last write, update the checksum chunk in the |
1295 |
// file |
// file |
1296 |
if (pCkData->GetPos() == pCkData->GetSize()) { |
if (pCkData->GetPos() == pCkData->GetSize()) { |
1297 |
|
__finalizeCRC(crc); |
1298 |
File* pFile = static_cast<File*>(GetParent()); |
File* pFile = static_cast<File*>(GetParent()); |
1299 |
pFile->SetSampleChecksum(this, __encodeCRC(crc)); |
pFile->SetSampleChecksum(this, crc); |
1300 |
} |
} |
1301 |
return res; |
return res; |
1302 |
} |
} |
1397 |
* @see GetWaveDataCRC32Checksum() |
* @see GetWaveDataCRC32Checksum() |
1398 |
*/ |
*/ |
1399 |
bool Sample::VerifyWaveData(uint32_t* pActually) { |
bool Sample::VerifyWaveData(uint32_t* pActually) { |
1400 |
File* pFile = static_cast<File*>(GetParent()); |
//File* pFile = static_cast<File*>(GetParent()); |
1401 |
uint32_t crc = CalculateWaveDataChecksum(); |
uint32_t crc = CalculateWaveDataChecksum(); |
1402 |
if (pActually) *pActually = crc; |
if (pActually) *pActually = crc; |
1403 |
return crc == this->crc; |
return crc == this->crc; |
1417 |
if (nRead <= 0) break; |
if (nRead <= 0) break; |
1418 |
__calculateCRC(&buffer[0], nRead * FrameSize, crc); |
__calculateCRC(&buffer[0], nRead * FrameSize, crc); |
1419 |
} |
} |
1420 |
__encodeCRC(crc); |
__finalizeCRC(crc); |
1421 |
return crc; |
return crc; |
1422 |
} |
} |
1423 |
|
|
1830 |
|
|
1831 |
// update '3ewa' chunk with DimensionRegion's current settings |
// update '3ewa' chunk with DimensionRegion's current settings |
1832 |
|
|
1833 |
const uint32_t chunksize = _3ewa->GetNewSize(); |
const uint32_t chunksize = (uint32_t) _3ewa->GetNewSize(); |
1834 |
store32(&pData[0], chunksize); // unknown, always chunk size? |
store32(&pData[0], chunksize); // unknown, always chunk size? |
1835 |
|
|
1836 |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
3798 |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
3799 |
uint8_t bits; |
uint8_t bits; |
3800 |
int veldim = -1; |
int veldim = -1; |
3801 |
int velbitpos; |
int velbitpos = 0; |
3802 |
int bitpos = 0; |
int bitpos = 0; |
3803 |
int dimregidx = 0; |
int dimregidx = 0; |
3804 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
3847 |
int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) { |
int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) { |
3848 |
uint8_t bits; |
uint8_t bits; |
3849 |
int veldim = -1; |
int veldim = -1; |
3850 |
int velbitpos; |
int velbitpos = 0; |
3851 |
int bitpos = 0; |
int bitpos = 0; |
3852 |
int dimregidx = 0; |
int dimregidx = 0; |
3853 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
4196 |
// to handle potential future extensions of the header |
// to handle potential future extensions of the header |
4197 |
ckScri->SetPos(sizeof(int32_t) + headerSize); |
ckScri->SetPos(sizeof(int32_t) + headerSize); |
4198 |
// read actual script data |
// read actual script data |
4199 |
uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos(); |
uint32_t scriptSize = uint32_t(ckScri->GetSize() - ckScri->GetPos()); |
4200 |
data.resize(scriptSize); |
data.resize(scriptSize); |
4201 |
for (int i = 0; i < scriptSize; ++i) |
for (int i = 0; i < scriptSize; ++i) |
4202 |
data[i] = ckScri->ReadUint8(); |
data[i] = ckScri->ReadUint8(); |
4247 |
// recalculate CRC32 check sum |
// recalculate CRC32 check sum |
4248 |
__resetCRC(crc); |
__resetCRC(crc); |
4249 |
__calculateCRC(&data[0], data.size(), crc); |
__calculateCRC(&data[0], data.size(), crc); |
4250 |
__encodeCRC(crc); |
__finalizeCRC(crc); |
4251 |
// make sure chunk exists and has the required size |
// make sure chunk exists and has the required size |
4252 |
const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size(); |
const file_offset_t chunkSize = (file_offset_t) 7*sizeof(int32_t) + Name.size() + data.size(); |
4253 |
if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); |
if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); |
4254 |
else pChunk->Resize(chunkSize); |
else pChunk->Resize(chunkSize); |
4255 |
// fill the chunk data to be written to disk |
// fill the chunk data to be written to disk |
4256 |
uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); |
4257 |
int pos = 0; |
int pos = 0; |
4258 |
store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size |
store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size())); // total header size |
4259 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4260 |
store32(&pData[pos], Compression); |
store32(&pData[pos], Compression); |
4261 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4267 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4268 |
store32(&pData[pos], crc); |
store32(&pData[pos], crc); |
4269 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4270 |
store32(&pData[pos], Name.size()); |
store32(&pData[pos], (uint32_t) Name.size()); |
4271 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4272 |
for (int i = 0; i < Name.size(); ++i, ++pos) |
for (int i = 0; i < Name.size(); ++i, ++pos) |
4273 |
pData[pos] = Name[i]; |
pData[pos] = Name[i]; |
4298 |
return pGroup; |
return pGroup; |
4299 |
} |
} |
4300 |
|
|
4301 |
|
/** |
4302 |
|
* Make a (semi) deep copy of the Script object given by @a orig |
4303 |
|
* and assign it to this object. Note: the ScriptGroup this Script |
4304 |
|
* object belongs to remains untouched by this call. |
4305 |
|
* |
4306 |
|
* @param orig - original Script object to be copied from |
4307 |
|
*/ |
4308 |
|
void Script::CopyAssign(const Script* orig) { |
4309 |
|
Name = orig->Name; |
4310 |
|
Compression = orig->Compression; |
4311 |
|
Encoding = orig->Encoding; |
4312 |
|
Language = orig->Language; |
4313 |
|
Bypass = orig->Bypass; |
4314 |
|
data = orig->data; |
4315 |
|
} |
4316 |
|
|
4317 |
void Script::RemoveAllScriptReferences() { |
void Script::RemoveAllScriptReferences() { |
4318 |
File* pFile = pGroup->pFile; |
File* pFile = pGroup->pFile; |
4319 |
for (int i = 0; pFile->GetInstrument(i); ++i) { |
for (int i = 0; pFile->GetInstrument(i); ++i) { |
4460 |
EffectSend = 0; |
EffectSend = 0; |
4461 |
Attenuation = 0; |
Attenuation = 0; |
4462 |
FineTune = 0; |
FineTune = 0; |
4463 |
PitchbendRange = 0; |
PitchbendRange = 2; |
4464 |
PianoReleaseMode = false; |
PianoReleaseMode = false; |
4465 |
DimensionKeyRange.low = 0; |
DimensionKeyRange.low = 0; |
4466 |
DimensionKeyRange.high = 0; |
DimensionKeyRange.high = 0; |
4635 |
|
|
4636 |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4637 |
if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); |
if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); |
4638 |
const int slotCount = pScriptRefs->size(); |
const int slotCount = (int) pScriptRefs->size(); |
4639 |
const int headerSize = 3 * sizeof(uint32_t); |
const int headerSize = 3 * sizeof(uint32_t); |
4640 |
const int slotSize = 2 * sizeof(uint32_t); |
const int slotSize = 2 * sizeof(uint32_t); |
4641 |
const int totalChunkSize = headerSize + slotCount * slotSize; |
const int totalChunkSize = headerSize + slotCount * slotSize; |
4671 |
if (pScriptRefs && pScriptRefs->size() > 0) { |
if (pScriptRefs && pScriptRefs->size() > 0) { |
4672 |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4673 |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
4674 |
const int slotCount = pScriptRefs->size(); |
const int slotCount = (int) pScriptRefs->size(); |
4675 |
const int headerSize = 3 * sizeof(uint32_t); |
const int headerSize = 3 * sizeof(uint32_t); |
4676 |
ckSCSL->SetPos(headerSize); |
ckSCSL->SetPos(headerSize); |
4677 |
for (int i = 0; i < slotCount; ++i) { |
for (int i = 0; i < slotCount; ++i) { |
4678 |
uint32_t fileOffset = |
uint32_t fileOffset = uint32_t( |
4679 |
(*pScriptRefs)[i].script->pChunk->GetFilePos() - |
(*pScriptRefs)[i].script->pChunk->GetFilePos() - |
4680 |
(*pScriptRefs)[i].script->pChunk->GetPos() - |
(*pScriptRefs)[i].script->pChunk->GetPos() - |
4681 |
CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()); |
CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()) |
4682 |
|
); |
4683 |
ckSCSL->WriteUint32(&fileOffset); |
ckSCSL->WriteUint32(&fileOffset); |
4684 |
// jump over flags entry (containing the bypass flag) |
// jump over flags entry (containing the bypass flag) |
4685 |
ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); |
ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); |
4739 |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
4740 |
Region* pNewRegion = new Region(this, rgn); |
Region* pNewRegion = new Region(this, rgn); |
4741 |
pRegions->push_back(pNewRegion); |
pRegions->push_back(pNewRegion); |
4742 |
Regions = pRegions->size(); |
Regions = (uint32_t) pRegions->size(); |
4743 |
// update Region key table for fast lookup |
// update Region key table for fast lookup |
4744 |
UpdateRegionKeyTable(); |
UpdateRegionKeyTable(); |
4745 |
// done |
// done |
4885 |
for (uint s = 0; group->GetScript(s); ++s) { |
for (uint s = 0; group->GetScript(s); ++s) { |
4886 |
Script* script = group->GetScript(s); |
Script* script = group->GetScript(s); |
4887 |
if (script->pChunk) { |
if (script->pChunk) { |
4888 |
uint32_t offset = script->pChunk->GetFilePos() - |
uint32_t offset = uint32_t( |
4889 |
script->pChunk->GetPos() - |
script->pChunk->GetFilePos() - |
4890 |
CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()); |
script->pChunk->GetPos() - |
4891 |
|
CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()) |
4892 |
|
); |
4893 |
if (offset == soughtOffset) |
if (offset == soughtOffset) |
4894 |
{ |
{ |
4895 |
_ScriptPooolRef ref; |
_ScriptPooolRef ref; |
5014 |
*/ |
*/ |
5015 |
void Instrument::RemoveScript(Script* pScript) { |
void Instrument::RemoveScript(Script* pScript) { |
5016 |
LoadScripts(); |
LoadScripts(); |
5017 |
for (int i = pScriptRefs->size() - 1; i >= 0; --i) { |
for (ssize_t i = pScriptRefs->size() - 1; i >= 0; --i) { |
5018 |
if ((*pScriptRefs)[i].script == pScript) { |
if ((*pScriptRefs)[i].script == pScript) { |
5019 |
pScriptRefs->erase( pScriptRefs->begin() + i ); |
pScriptRefs->erase( pScriptRefs->begin() + i ); |
5020 |
} |
} |
5036 |
* gigedit. |
* gigedit. |
5037 |
*/ |
*/ |
5038 |
uint Instrument::ScriptSlotCount() const { |
uint Instrument::ScriptSlotCount() const { |
5039 |
return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size(); |
return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size()); |
5040 |
} |
} |
5041 |
|
|
5042 |
/** @brief Whether script execution shall be skipped. |
/** @brief Whether script execution shall be skipped. |
5460 |
} |
} |
5461 |
} |
} |
5462 |
String name(pRIFF->GetFileName()); |
String name(pRIFF->GetFileName()); |
5463 |
int nameLen = name.length(); |
int nameLen = (int) name.length(); |
5464 |
char suffix[6]; |
char suffix[6]; |
5465 |
if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; |
if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; |
5466 |
|
|
5629 |
mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s); |
mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s); |
5630 |
mSamples[pFile->GetSample(i)] = s; |
mSamples[pFile->GetSample(i)] = s; |
5631 |
} |
} |
5632 |
|
|
5633 |
|
// clone script groups and their scripts |
5634 |
|
for (int iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { |
5635 |
|
ScriptGroup* sg = pFile->GetScriptGroup(iGroup); |
5636 |
|
ScriptGroup* dg = AddScriptGroup(); |
5637 |
|
dg->Name = "COPY" + ToString(iCallCount) + "_" + sg->Name; |
5638 |
|
for (int iScript = 0; sg->GetScript(iScript); ++iScript) { |
5639 |
|
Script* ss = sg->GetScript(iScript); |
5640 |
|
Script* ds = dg->AddScript(); |
5641 |
|
ds->CopyAssign(ss); |
5642 |
|
} |
5643 |
|
} |
5644 |
|
|
5645 |
//BUG: For some reason this method only works with this additional |
//BUG: For some reason this method only works with this additional |
5646 |
// Save() call in between here. |
// Save() call in between here. |
5647 |
// |
// |
5788 |
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5789 |
if (_3crc->GetNewSize() != pSamples->size() * 8) return false; |
if (_3crc->GetNewSize() != pSamples->size() * 8) return false; |
5790 |
|
|
5791 |
const int n = _3crc->GetNewSize() / 8; |
const file_offset_t n = _3crc->GetNewSize() / 8; |
5792 |
|
|
5793 |
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5794 |
if (!pData) return false; |
if (!pData) return false; |
5795 |
|
|
5796 |
for (int i = 0; i < n; ++i) { |
for (file_offset_t i = 0; i < n; ++i) { |
5797 |
uint32_t one = pData[i*2]; |
uint32_t one = pData[i*2]; |
5798 |
if (one != 1) return false; |
if (one != 1) return false; |
5799 |
} |
} |
6185 |
// Note that there are several fields with unknown use. These |
// Note that there are several fields with unknown use. These |
6186 |
// are set to zero. |
// are set to zero. |
6187 |
|
|
6188 |
int sublen = pSamples->size() / 8 + 49; |
int sublen = int(pSamples->size() / 8 + 49); |
6189 |
int einfSize = (Instruments + 1) * sublen; |
int einfSize = (Instruments + 1) * sublen; |
6190 |
|
|
6191 |
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
6258 |
store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); |
store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); |
6259 |
// next 8 bytes unknown |
// next 8 bytes unknown |
6260 |
store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); |
store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); |
6261 |
store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); |
store32(&pData[(instrumentIdx + 1) * sublen + 40], (uint32_t) pSamples->size()); |
6262 |
// next 4 bytes unknown |
// next 4 bytes unknown |
6263 |
|
|
6264 |
totnbregions += instrument->Regions; |
totnbregions += instrument->Regions; |
6276 |
store32(&pData[24], totnbloops); |
store32(&pData[24], totnbloops); |
6277 |
// next 8 bytes unknown |
// next 8 bytes unknown |
6278 |
// next 4 bytes unknown, not always 0 |
// next 4 bytes unknown, not always 0 |
6279 |
store32(&pData[40], pSamples->size()); |
store32(&pData[40], (uint32_t) pSamples->size()); |
6280 |
// next 4 bytes unknown |
// next 4 bytes unknown |
6281 |
} |
} |
6282 |
|
|