--- libgig/trunk/src/gig.cpp 2016/05/18 18:04:49 2922 +++ libgig/trunk/src/gig.cpp 2017/10/03 17:35:02 3350 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2016 by Christian Schoenebeck * + * Copyright (C) 2003-2017 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -24,6 +24,7 @@ #include "gig.h" #include "helper.h" +#include "Serialization.h" #include #include @@ -55,6 +56,9 @@ #define GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(x) ((x & 0x03) << 3) #define GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(x) ((x & 0x03) << 5) +#define SRLZ(member) \ + archive->serializeMember(*this, member, #member); + namespace gig { // *************** Internal functions for sample decompression *************** @@ -269,14 +273,14 @@ * steps. * * Once the whole data was processed by __calculateCRC(), one should - * call __encodeCRC() to get the final CRC result. + * call __finalizeCRC() to get the final CRC result. * * @param buf - pointer to data the CRC shall be calculated of * @param bufSize - size of the data to be processed * @param crc - variable the CRC sum shall be stored to */ - static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) { - for (int i = 0 ; i < bufSize ; i++) { + static void __calculateCRC(unsigned char* buf, size_t bufSize, uint32_t& crc) { + for (size_t i = 0 ; i < bufSize ; i++) { crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); } } @@ -286,8 +290,8 @@ * * @param crc - variable previously passed to __calculateCRC() */ - inline static uint32_t __encodeCRC(const uint32_t& crc) { - return crc ^ 0xffffffff; + inline static void __finalizeCRC(uint32_t& crc) { + crc ^= 0xffffffff; } @@ -315,6 +319,49 @@ +// *************** leverage_ctrl_t *************** +// * + + void leverage_ctrl_t::serialize(Serialization::Archive* archive) { + SRLZ(type); + SRLZ(controller_number); + } + + + +// *************** crossfade_t *************** +// * + + void crossfade_t::serialize(Serialization::Archive* archive) { + SRLZ(in_start); + SRLZ(in_end); + SRLZ(out_start); + SRLZ(out_end); + } + + + +// *************** eg_opt_t *************** +// * + + eg_opt_t::eg_opt_t() { + AttackCancel = true; + AttackHoldCancel = true; + Decay1Cancel = true; + Decay2Cancel = true; + ReleaseCancel = true; + } + + void eg_opt_t::serialize(Serialization::Archive* archive) { + SRLZ(AttackCancel); + SRLZ(AttackHoldCancel); + SRLZ(Decay1Cancel); + SRLZ(Decay2Cancel); + SRLZ(ReleaseCancel); + } + + + // *************** Sample *************** // * @@ -338,8 +385,11 @@ * ('wvpl') list chunk * @param fileNo - number of an extension file where this sample * is located, 0 otherwise + * @param index - wave pool index of sample (may be -1 on new sample) */ - Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { + Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset, unsigned long fileNo, int index) + : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) + { static const DLS::Info::string_length_t fixedStringLengths[] = { { CHUNK_ID_INAM, 64 }, { 0, 0 } @@ -349,6 +399,16 @@ FileNo = fileNo; __resetCRC(crc); + // if this is not a new sample, try to get the sample's already existing + // CRC32 checksum from disk, this checksum will reflect the sample's CRC32 + // checksum of the time when the sample was consciously modified by the + // user for the last time (by calling Sample::Write() that is). + if (index >= 0) { // not a new file ... + try { + uint32_t crc = pFile->GetSampleChecksumByIndex(index); + this->crc = crc; + } catch (...) {} + } pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); if (pCk3gix) { @@ -1281,8 +1341,9 @@ // if this is the last write, update the checksum chunk in the // file if (pCkData->GetPos() == pCkData->GetSize()) { + __finalizeCRC(crc); File* pFile = static_cast(GetParent()); - pFile->SetSampleChecksum(this, __encodeCRC(crc)); + pFile->SetSampleChecksum(this, crc); } return res; } @@ -1341,6 +1402,72 @@ return pGroup; } + /** + * Returns the CRC-32 checksum of the sample's raw wave form data at the + * time when this sample's wave form data was modified for the last time + * by calling Write(). This checksum only covers the raw wave form data, + * not any meta informations like i.e. bit depth or loop points. Since + * this method just returns the checksum stored for this sample i.e. when + * the gig file was loaded, this method returns immediately. So it does no + * recalcuation of the checksum with the currently available sample wave + * form data. + * + * @see VerifyWaveData() + */ + uint32_t Sample::GetWaveDataCRC32Checksum() { + return crc; + } + + /** + * Checks the integrity of this sample's raw audio wave data. Whenever a + * Sample's raw wave data is intentionally modified (i.e. by calling + * Write() and supplying the new raw audio wave form data) a CRC32 checksum + * is calculated and stored/updated for this sample, along to the sample's + * meta informations. + * + * Now by calling this method the current raw audio wave data is checked + * against the already stored CRC32 check sum in order to check whether the + * sample data had been damaged unintentionally for some reason. Since by + * calling this method always the entire raw audio wave data has to be + * read, verifying all samples this way may take a long time accordingly. + * And that's also the reason why the sample integrity is not checked by + * default whenever a gig file is loaded. So this method must be called + * explicitly to fulfill this task. + * + * @param pActually - (optional) if provided, will be set to the actually + * calculated checksum of the current raw wave form data, + * you can get the expected checksum instead by calling + * GetWaveDataCRC32Checksum() + * @returns true if sample is OK or false if the sample is damaged + * @throws Exception if no checksum had been stored to disk for this + * sample yet, or on I/O issues + * @see GetWaveDataCRC32Checksum() + */ + bool Sample::VerifyWaveData(uint32_t* pActually) { + //File* pFile = static_cast(GetParent()); + uint32_t crc = CalculateWaveDataChecksum(); + if (pActually) *pActually = crc; + return crc == this->crc; + } + + uint32_t Sample::CalculateWaveDataChecksum() { + const size_t sz = 20*1024; // 20kB buffer size + std::vector buffer(sz); + buffer.resize(sz); + + const size_t n = sz / FrameSize; + SetPos(0); + uint32_t crc = 0; + __resetCRC(crc); + while (true) { + file_offset_t nRead = Read(&buffer[0], n); + if (nRead <= 0) break; + __calculateCRC(&buffer[0], nRead * FrameSize, crc); + } + __finalizeCRC(crc); + return crc; + } + Sample::~Sample() { Instances--; if (!Instances && InternalDecompressionBuffer.Size) { @@ -1551,7 +1678,7 @@ EG2Attack = 0.0; EG2Decay1 = 0.005; EG2Sustain = 1000; - EG2Release = 0.3; + EG2Release = 60; LFO2ControlDepth = 0; LFO2Frequency = 1.0; LFO2InternalDepth = 0; @@ -1605,6 +1732,20 @@ VCFType = vcf_type_lowpass; memset(DimensionUpperLimits, 127, 8); } + // format extension for EG behavior options, these will *NOT* work with + // Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = _3ewl->GetSubChunk(CHUNK_ID_LSDE); + if (lsde) { + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + for (int i = 0; i < 2; ++i) { + unsigned char byte = lsde->ReadUint8(); + pEGOpts[i]->AttackCancel = byte & 1; + pEGOpts[i]->AttackHoldCancel = byte & (1 << 1); + pEGOpts[i]->Decay1Cancel = byte & (1 << 2); + pEGOpts[i]->Decay2Cancel = byte & (1 << 3); + pEGOpts[i]->ReleaseCancel = byte & (1 << 4); + } + } pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, VelocityResponseDepth, @@ -1710,6 +1851,97 @@ } } + void DimensionRegion::serialize(Serialization::Archive* archive) { + // in case this class will become backward incompatible one day, + // then set a version and minimum version for this class like: + //archive->setVersion(*this, 2); + //archive->setMinVersion(*this, 1); + + SRLZ(VelocityUpperLimit); + SRLZ(EG1PreAttack); + SRLZ(EG1Attack); + SRLZ(EG1Decay1); + SRLZ(EG1Decay2); + SRLZ(EG1InfiniteSustain); + SRLZ(EG1Sustain); + SRLZ(EG1Release); + SRLZ(EG1Hold); + SRLZ(EG1Controller); + SRLZ(EG1ControllerInvert); + SRLZ(EG1ControllerAttackInfluence); + SRLZ(EG1ControllerDecayInfluence); + SRLZ(EG1ControllerReleaseInfluence); + SRLZ(LFO1Frequency); + SRLZ(LFO1InternalDepth); + SRLZ(LFO1ControlDepth); + SRLZ(LFO1Controller); + SRLZ(LFO1FlipPhase); + SRLZ(LFO1Sync); + SRLZ(EG2PreAttack); + SRLZ(EG2Attack); + SRLZ(EG2Decay1); + SRLZ(EG2Decay2); + SRLZ(EG2InfiniteSustain); + SRLZ(EG2Sustain); + SRLZ(EG2Release); + SRLZ(EG2Controller); + SRLZ(EG2ControllerInvert); + SRLZ(EG2ControllerAttackInfluence); + SRLZ(EG2ControllerDecayInfluence); + SRLZ(EG2ControllerReleaseInfluence); + SRLZ(LFO2Frequency); + SRLZ(LFO2InternalDepth); + SRLZ(LFO2ControlDepth); + SRLZ(LFO2Controller); + SRLZ(LFO2FlipPhase); + SRLZ(LFO2Sync); + SRLZ(EG3Attack); + SRLZ(EG3Depth); + SRLZ(LFO3Frequency); + SRLZ(LFO3InternalDepth); + SRLZ(LFO3ControlDepth); + SRLZ(LFO3Controller); + SRLZ(LFO3Sync); + SRLZ(VCFEnabled); + SRLZ(VCFType); + SRLZ(VCFCutoffController); + SRLZ(VCFCutoffControllerInvert); + SRLZ(VCFCutoff); + SRLZ(VCFVelocityCurve); + SRLZ(VCFVelocityScale); + SRLZ(VCFVelocityDynamicRange); + SRLZ(VCFResonance); + SRLZ(VCFResonanceDynamic); + SRLZ(VCFResonanceController); + SRLZ(VCFKeyboardTracking); + SRLZ(VCFKeyboardTrackingBreakpoint); + SRLZ(VelocityResponseCurve); + SRLZ(VelocityResponseDepth); + SRLZ(VelocityResponseCurveScaling); + SRLZ(ReleaseVelocityResponseCurve); + SRLZ(ReleaseVelocityResponseDepth); + SRLZ(ReleaseTriggerDecay); + SRLZ(Crossfade); + SRLZ(PitchTrack); + SRLZ(DimensionBypass); + SRLZ(Pan); + SRLZ(SelfMask); + SRLZ(AttenuationController); + SRLZ(InvertAttenuationController); + SRLZ(AttenuationControllerThreshold); + SRLZ(ChannelOffset); + SRLZ(SustainDefeat); + SRLZ(MSDecode); + //SRLZ(SampleStartOffset); + SRLZ(SampleAttenuation); + SRLZ(EG1Options); + SRLZ(EG2Options); + + // derived attributes from DLS::Sampler + SRLZ(FineTune); + SRLZ(Gain); + } + /** * Updates the respective member variable and updates @c SampleAttenuation * which depends on this value. @@ -1750,7 +1982,7 @@ // update '3ewa' chunk with DimensionRegion's current settings - const uint32_t chunksize = _3ewa->GetNewSize(); + const uint32_t chunksize = (uint32_t) _3ewa->GetNewSize(); store32(&pData[0], chunksize); // unknown, always chunk size? const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); @@ -2010,6 +2242,34 @@ if (chunksize >= 148) { memcpy(&pData[140], DimensionUpperLimits, 8); } + + // format extension for EG behavior options, these will *NOT* work with + // Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = pParentList->GetSubChunk(CHUNK_ID_LSDE); + if (!lsde) { + // only add this "LSDE" chunk if the EG options do not match the + // default EG behavior + eg_opt_t defaultOpt; + if (memcmp(&EG1Options, &defaultOpt, sizeof(eg_opt_t)) || + memcmp(&EG2Options, &defaultOpt, sizeof(eg_opt_t))) + { + lsde = pParentList->AddSubChunk(CHUNK_ID_LSDE, 2); + // move LSDE chunk to the end of parent list + pParentList->MoveSubChunk(lsde, (RIFF::Chunk*)NULL); + } + } + if (lsde) { + unsigned char* pData = (unsigned char*) lsde->LoadChunkData(); + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + for (int i = 0; i < 2; ++i) { + pData[i] = + (pEGOpts[i]->AttackCancel ? 1 : 0) | + (pEGOpts[i]->AttackHoldCancel ? (1<<1) : 0) | + (pEGOpts[i]->Decay1Cancel ? (1<<2) : 0) | + (pEGOpts[i]->Decay2Cancel ? (1<<3) : 0) | + (pEGOpts[i]->ReleaseCancel ? (1<<4) : 0); + } + } } double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) { @@ -2049,6 +2309,33 @@ // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { + // sanity check input parameters + // (fallback to some default parameters on ill input) + switch (curveType) { + case curve_type_nonlinear: + case curve_type_linear: + if (depth > 4) { + printf("Warning: Invalid depth (0x%x) for velocity curve type (0x%x).\n", depth, curveType); + depth = 0; + scaling = 0; + } + break; + case curve_type_special: + if (depth > 5) { + printf("Warning: Invalid depth (0x%x) for velocity curve type 'special'.\n", depth); + depth = 0; + scaling = 0; + } + break; + case curve_type_unknown: + default: + printf("Warning: Unknown velocity curve type (0x%x).\n", curveType); + curveType = curve_type_linear; + depth = 0; + scaling = 0; + break; + } + double* table; uint32_t tableKey = (curveType<<16) | (depth<<8) | scaling; if (pVelocityTables->count(tableKey)) { // if key exists @@ -2424,7 +2711,10 @@ // unknown controller type default: - throw gig::Exception("Unknown leverage controller type."); + decodedcontroller.type = leverage_ctrl_t::type_none; + decodedcontroller.controller_number = 0; + printf("Warning: Unknown leverage controller type (0x%x).\n", EncodedController); + break; } return decodedcontroller; } @@ -2971,7 +3261,8 @@ if (file->GetAutoLoad()) { for (uint i = 0; i < DimensionRegions; i++) { uint32_t wavepoolindex = _3lnk->ReadUint32(); - if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); + if (file->pWavePoolTable && pDimensionRegions[i]) + pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); } GetSample(); // load global region sample reference } @@ -3106,12 +3397,13 @@ int step = 1; for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; - int end = step * pDimensionDefinitions[veldim].zones; // loop through all dimension regions for all dimensions except the velocity dimension int dim[8] = { 0 }; for (int i = 0 ; i < DimensionRegions ; i++) { + const int end = i + step * pDimensionDefinitions[veldim].zones; + // create a velocity table for all cases where the velocity zone is zero if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || pDimensionRegions[i]->VelocityUpperLimit) { // create the velocity table @@ -3142,6 +3434,7 @@ } } + // jump to the next case where the velocity zone is zero int j; int shift = 0; for (j = 0 ; j < Dimensions ; j++) { @@ -3716,7 +4009,7 @@ DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { uint8_t bits; int veldim = -1; - int velbitpos; + int velbitpos = 0; int bitpos = 0; int dimregidx = 0; for (uint i = 0; i < Dimensions; i++) { @@ -3765,7 +4058,7 @@ int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) { uint8_t bits; int veldim = -1; - int velbitpos; + int velbitpos = 0; int bitpos = 0; int dimregidx = 0; for (uint i = 0; i < Dimensions; i++) { @@ -3850,6 +4143,7 @@ if ((int32_t)WavePoolTableIndex == -1) return NULL; File* file = (File*) GetParent()->GetParent(); if (!file->pWavePoolTable) return NULL; + if (WavePoolTableIndex + 1 > file->WavePoolCount) return NULL; // for new files or files >= 2 GB use 64 bit wave pool offsets if (file->pRIFF->IsNew() || (file->pRIFF->GetCurrentFileSize() >> 31)) { // use 64 bit wave pool offsets (treating this as large file) @@ -4114,7 +4408,7 @@ // to handle potential future extensions of the header ckScri->SetPos(sizeof(int32_t) + headerSize); // read actual script data - uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos(); + uint32_t scriptSize = uint32_t(ckScri->GetSize() - ckScri->GetPos()); data.resize(scriptSize); for (int i = 0; i < scriptSize; ++i) data[i] = ckScri->ReadUint8(); @@ -4165,15 +4459,15 @@ // recalculate CRC32 check sum __resetCRC(crc); __calculateCRC(&data[0], data.size(), crc); - __encodeCRC(crc); + __finalizeCRC(crc); // make sure chunk exists and has the required size - 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(); if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); else pChunk->Resize(chunkSize); // fill the chunk data to be written to disk uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); int pos = 0; - 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 pos += sizeof(int32_t); store32(&pData[pos], Compression); pos += sizeof(int32_t); @@ -4185,7 +4479,7 @@ pos += sizeof(int32_t); store32(&pData[pos], crc); pos += sizeof(int32_t); - store32(&pData[pos], Name.size()); + store32(&pData[pos], (uint32_t) Name.size()); pos += sizeof(int32_t); for (int i = 0; i < Name.size(); ++i, ++pos) pData[pos] = Name[i]; @@ -4216,6 +4510,22 @@ return pGroup; } + /** + * Make a (semi) deep copy of the Script object given by @a orig + * and assign it to this object. Note: the ScriptGroup this Script + * object belongs to remains untouched by this call. + * + * @param orig - original Script object to be copied from + */ + void Script::CopyAssign(const Script* orig) { + Name = orig->Name; + Compression = orig->Compression; + Encoding = orig->Encoding; + Language = orig->Language; + Bypass = orig->Bypass; + data = orig->data; + } + void Script::RemoveAllScriptReferences() { File* pFile = pGroup->pFile; for (int i = 0; pFile->GetInstrument(i); ++i) { @@ -4362,7 +4672,7 @@ EffectSend = 0; Attenuation = 0; FineTune = 0; - PitchbendRange = 0; + PitchbendRange = 2; PianoReleaseMode = false; DimensionKeyRange.low = 0; DimensionKeyRange.high = 0; @@ -4460,7 +4770,9 @@ RegionList::iterator end = pRegions->end(); for (; iter != end; ++iter) { gig::Region* pRegion = static_cast(*iter); - for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) { + const int low = std::max(int(pRegion->KeyRange.low), 0); + const int high = std::min(int(pRegion->KeyRange.high), 127); + for (int iKey = low; iKey <= high; iKey++) { RegionKeyTable[iKey] = pRegion; } } @@ -4537,7 +4849,7 @@ RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); - const int slotCount = pScriptRefs->size(); + const int slotCount = (int) pScriptRefs->size(); const int headerSize = 3 * sizeof(uint32_t); const int slotSize = 2 * sizeof(uint32_t); const int totalChunkSize = headerSize + slotCount * slotSize; @@ -4573,14 +4885,15 @@ if (pScriptRefs && pScriptRefs->size() > 0) { RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); - const int slotCount = pScriptRefs->size(); + const int slotCount = (int) pScriptRefs->size(); const int headerSize = 3 * sizeof(uint32_t); ckSCSL->SetPos(headerSize); for (int i = 0; i < slotCount; ++i) { - uint32_t fileOffset = + uint32_t fileOffset = uint32_t( (*pScriptRefs)[i].script->pChunk->GetFilePos() - (*pScriptRefs)[i].script->pChunk->GetPos() - - CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()); + CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()) + ); ckSCSL->WriteUint32(&fileOffset); // jump over flags entry (containing the bypass flag) ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); @@ -4640,7 +4953,7 @@ RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); Region* pNewRegion = new Region(this, rgn); pRegions->push_back(pNewRegion); - Regions = pRegions->size(); + Regions = (uint32_t) pRegions->size(); // update Region key table for fast lookup UpdateRegionKeyTable(); // done @@ -4786,9 +5099,11 @@ for (uint s = 0; group->GetScript(s); ++s) { Script* script = group->GetScript(s); if (script->pChunk) { - uint32_t offset = script->pChunk->GetFilePos() - - script->pChunk->GetPos() - - CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()); + uint32_t offset = uint32_t( + script->pChunk->GetFilePos() - + script->pChunk->GetPos() - + CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()) + ); if (offset == soughtOffset) { _ScriptPooolRef ref; @@ -4913,7 +5228,7 @@ */ void Instrument::RemoveScript(Script* pScript) { LoadScripts(); - for (int i = pScriptRefs->size() - 1; i >= 0; --i) { + for (ssize_t i = pScriptRefs->size() - 1; i >= 0; --i) { if ((*pScriptRefs)[i].script == pScript) { pScriptRefs->erase( pScriptRefs->begin() + i ); } @@ -4935,7 +5250,7 @@ * gigedit. */ uint Instrument::ScriptSlotCount() const { - return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size(); + return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size()); } /** @brief Whether script execution shall be skipped. @@ -5359,7 +5674,7 @@ } } String name(pRIFF->GetFileName()); - int nameLen = name.length(); + int nameLen = (int) name.length(); char suffix[6]; if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; @@ -5375,7 +5690,7 @@ __notify_progress(pProgress, subprogress); file_offset_t waveFileOffset = wave->GetFilePos(); - pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); + pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo, iSampleIndex)); iSampleIndex++; } @@ -5528,7 +5843,19 @@ mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s); mSamples[pFile->GetSample(i)] = s; } - + + // clone script groups and their scripts + for (int iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { + ScriptGroup* sg = pFile->GetScriptGroup(iGroup); + ScriptGroup* dg = AddScriptGroup(); + dg->Name = "COPY" + ToString(iCallCount) + "_" + sg->Name; + for (int iScript = 0; sg->GetScript(iScript); ++iScript) { + Script* ss = sg->GetScript(iScript); + Script* ds = dg->AddScript(); + ds->CopyAssign(ss); + } + } + //BUG: For some reason this method only works with this additional // Save() call in between here. // @@ -5613,24 +5940,148 @@ if (!_3crc) return; // get the index of the sample - int iWaveIndex = -1; - File::SampleList::iterator iter = pSamples->begin(); - File::SampleList::iterator end = pSamples->end(); - for (int index = 0; iter != end; ++iter, ++index) { - if (*iter == pSample) { - iWaveIndex = index; - break; - } - } + int iWaveIndex = GetWaveTableIndexOf(pSample); if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); // write the CRC-32 checksum to disk _3crc->SetPos(iWaveIndex * 8); - uint32_t tmp = 1; - _3crc->WriteUint32(&tmp); // unknown, always 1? + uint32_t one = 1; + _3crc->WriteUint32(&one); // always 1 _3crc->WriteUint32(&crc); } + uint32_t File::GetSampleChecksum(Sample* pSample) { + // get the index of the sample + int iWaveIndex = GetWaveTableIndexOf(pSample); + if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); + + return GetSampleChecksumByIndex(iWaveIndex); + } + + uint32_t File::GetSampleChecksumByIndex(int index) { + if (index < 0) throw gig::Exception("Could not retrieve reference crc of sample, invalid wave pool index of sample"); + + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); + uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); + if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); + + // read the CRC-32 checksum directly from disk + size_t pos = index * 8; + if (pos + 8 > _3crc->GetNewSize()) + throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); + + uint32_t one = load32(&pData[pos]); // always 1 + if (one != 1) + throw gig::Exception("Could not retrieve reference crc of sample, because reference checksum table is damaged"); + + return load32(&pData[pos+4]); + } + + int File::GetWaveTableIndexOf(gig::Sample* pSample) { + if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (int index = 0; iter != end; ++iter, ++index) + if (*iter == pSample) + return index; + return -1; + } + + /** + * Checks whether the file's "3CRC" chunk was damaged. This chunk contains + * the CRC32 check sums of all samples' raw wave data. + * + * @return true if 3CRC chunk is OK, or false if 3CRC chunk is damaged + */ + bool File::VerifySampleChecksumTable() { + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (!_3crc) return false; + if (_3crc->GetNewSize() <= 0) return false; + if (_3crc->GetNewSize() % 8) return false; + if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned + if (_3crc->GetNewSize() != pSamples->size() * 8) return false; + + const file_offset_t n = _3crc->GetNewSize() / 8; + + uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); + if (!pData) return false; + + for (file_offset_t i = 0; i < n; ++i) { + uint32_t one = pData[i*2]; + if (one != 1) return false; + } + + return true; + } + + /** + * Recalculates CRC32 checksums for all samples and rebuilds this gig + * file's checksum table with those new checksums. This might usually + * just be necessary if the checksum table was damaged. + * + * @e IMPORTANT: The current implementation of this method only works + * with files that have not been modified since it was loaded, because + * it expects that no externally caused file structure changes are + * required! + * + * Due to the expectation above, this method is currently protected + * and actually only used by the command line tool "gigdump" yet. + * + * @returns true if Save() is required to be called after this call, + * false if no further action is required + */ + bool File::RebuildSampleChecksumTable() { + // make sure sample chunks were scanned + if (!pSamples) GetFirstSample(); + + bool bRequiresSave = false; + + // make sure "3CRC" chunk exists with required size + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (!_3crc) { + _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); + // the order of einf and 3crc is not the same in v2 and v3 + RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); + if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); + bRequiresSave = true; + } else if (_3crc->GetNewSize() != pSamples->size() * 8) { + _3crc->Resize(pSamples->size() * 8); + bRequiresSave = true; + } + + if (bRequiresSave) { // refill CRC table for all samples in RAM ... + uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); + { + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (; iter != end; ++iter) { + gig::Sample* pSample = (gig::Sample*) *iter; + int index = GetWaveTableIndexOf(pSample); + if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); + pData[index*2] = 1; // always 1 + pData[index*2+1] = pSample->CalculateWaveDataChecksum(); + } + } + } else { // no file structure changes necessary, so directly write to disk and we are done ... + // make sure file is in write mode + pRIFF->SetMode(RIFF::stream_mode_read_write); + { + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (; iter != end; ++iter) { + gig::Sample* pSample = (gig::Sample*) *iter; + int index = GetWaveTableIndexOf(pSample); + if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); + pSample->crc = pSample->CalculateWaveDataChecksum(); + SetSampleChecksum(pSample, pSample->crc); + } + } + } + + return bRequiresSave; + } + Group* File::GetFirstGroup() { if (!pGroups) LoadGroups(); // there must always be at least one group @@ -5948,7 +6399,7 @@ // Note that there are several fields with unknown use. These // are set to zero. - int sublen = pSamples->size() / 8 + 49; + int sublen = int(pSamples->size() / 8 + 49); int einfSize = (Instruments + 1) * sublen; RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); @@ -6021,7 +6472,7 @@ store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); // next 8 bytes unknown store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); - store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); + store32(&pData[(instrumentIdx + 1) * sublen + 40], (uint32_t) pSamples->size()); // next 4 bytes unknown totnbregions += instrument->Regions; @@ -6039,26 +6490,44 @@ store32(&pData[24], totnbloops); // next 8 bytes unknown // next 4 bytes unknown, not always 0 - store32(&pData[40], pSamples->size()); + store32(&pData[40], (uint32_t) pSamples->size()); // next 4 bytes unknown } // update 3crc chunk // The 3crc chunk contains CRC-32 checksums for the - // samples. The actual checksum values will be filled in - // later, by Sample::Write. + // samples. When saving a gig file to disk, we first update the 3CRC + // chunk here (in RAM) with the old crc values which we read from the + // 3CRC chunk when we opened the file (available with gig::Sample::crc + // member variable). This step is required, because samples might have + // been deleted by the user since the file was opened, which in turn + // changes the order of the (i.e. old) checksums within the 3crc chunk. + // If a sample was conciously modified by the user (that is if + // Sample::Write() was called later on) then Sample::Write() will just + // update the respective individual checksum(s) directly on disk and + // leaves all other sample checksums untouched. RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); if (_3crc) { _3crc->Resize(pSamples->size() * 8); - } else if (newFile) { + } else /*if (newFile)*/ { _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); - _3crc->LoadChunkData(); - // the order of einf and 3crc is not the same in v2 and v3 if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); } + { // must be performed in RAM here ... + uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); + if (pData) { + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (int index = 0; iter != end; ++iter, ++index) { + gig::Sample* pSample = (gig::Sample*) *iter; + pData[index*2] = 1; // always 1 + pData[index*2+1] = pSample->crc; + } + } + } } void File::UpdateFileOffsets() { @@ -6103,7 +6572,18 @@ // *************** Exception *************** // * - Exception::Exception(String Message) : DLS::Exception(Message) { + Exception::Exception() : DLS::Exception() { + } + + Exception::Exception(String format, ...) : DLS::Exception() { + va_list arg; + va_start(arg, format); + Message = assemble(format, arg); + va_end(arg); + } + + Exception::Exception(String format, va_list arg) : DLS::Exception() { + Message = assemble(format, arg); } void Exception::PrintMessage() {