--- libgig/trunk/src/gig.cpp 2016/05/21 08:54:32 2923 +++ libgig/trunk/src/gig.cpp 2019/10/02 16:30:29 3623 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2016 by Christian Schoenebeck * + * Copyright (C) 2003-2019 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,9 +399,21 @@ 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) { + pCk3gix->SetPos(0); + uint16_t iSampleGroup = pCk3gix->ReadInt16(); pGroup = pFile->GetGroup(iSampleGroup); } else { // '3gix' chunk missing @@ -361,6 +423,8 @@ pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); if (pCkSmpl) { + pCkSmpl->SetPos(0); + Manufacturer = pCkSmpl->ReadInt32(); Product = pCkSmpl->ReadInt32(); SamplePeriod = pCkSmpl->ReadInt32(); @@ -407,8 +471,10 @@ Dithered = false; TruncatedBits = 0; if (Compressed) { + ewav->SetPos(0); + uint32_t version = ewav->ReadInt32(); - if (version == 3 && BitDepth == 24) { + if (version > 2 && BitDepth == 24) { Dithered = ewav->ReadInt32(); ewav->SetPos(Channels == 2 ? 84 : 64); TruncatedBits = ewav->ReadInt32(); @@ -1281,8 +1347,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 +1408,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) { @@ -1373,6 +1506,8 @@ RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); if (_3ewa) { // if '3ewa' chunk exists + _3ewa->SetPos(0); + _3ewa->ReadInt32(); // unknown, always == chunk size ? LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); @@ -1551,7 +1686,7 @@ EG2Attack = 0.0; EG2Decay1 = 0.005; EG2Sustain = 1000; - EG2Release = 0.3; + EG2Release = 60; LFO2ControlDepth = 0; LFO2Frequency = 1.0; LFO2InternalDepth = 0; @@ -1606,6 +1741,54 @@ memset(DimensionUpperLimits, 127, 8); } + // chunk for own format extensions, these will *NOT* work with Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = _3ewl->GetSubChunk(CHUNK_ID_LSDE); + if (lsde) { // format extension for EG behavior options + lsde->SetPos(0); + + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + for (int i = 0; i < 2; ++i) { // NOTE: we reserved a 3rd byte for a potential future EG3 option + 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); + } + } + // format extension for sustain pedal up effect on release trigger samples + if (lsde && lsde->GetSize() > 3) { // NOTE: we reserved the 3rd byte for a potential future EG3 option + lsde->SetPos(3); + uint8_t byte = lsde->ReadUint8(); + SustainReleaseTrigger = static_cast(byte & 0x03); + NoNoteOffReleaseTrigger = byte >> 7; + } else { + SustainReleaseTrigger = sust_rel_trg_none; + NoNoteOffReleaseTrigger = false; + } + // format extension for LFOs' wave form, phase displacement and for + // LFO3's flip phase + if (lsde && lsde->GetSize() > 4) { + lsde->SetPos(4); + LFO1WaveForm = static_cast( lsde->ReadUint16() ); + LFO2WaveForm = static_cast( lsde->ReadUint16() ); + LFO3WaveForm = static_cast( lsde->ReadUint16() ); + lsde->ReadUint16(); // unused 16 bits, reserved for potential future use + LFO1Phase = (double) GIG_EXP_DECODE( lsde->ReadInt32() ); + LFO2Phase = (double) GIG_EXP_DECODE( lsde->ReadInt32() ); + LFO3Phase = (double) GIG_EXP_DECODE( lsde->ReadInt32() ); + const uint32_t flags = lsde->ReadInt32(); + LFO3FlipPhase = flags & 1; + } else { + LFO1WaveForm = lfo_wave_sine; + LFO2WaveForm = lfo_wave_sine; + LFO3WaveForm = lfo_wave_sine; + LFO1Phase = 0.0; + LFO2Phase = 0.0; + LFO3Phase = 0.0; + LFO3FlipPhase = false; + } + pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, VelocityResponseDepth, VelocityResponseCurveScaling); @@ -1710,6 +1893,106 @@ } } + 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(LFO1WaveForm); + SRLZ(LFO1Frequency); + SRLZ(LFO1Phase); + 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(LFO2WaveForm); + SRLZ(LFO2Frequency); + SRLZ(LFO2Phase); + SRLZ(LFO2InternalDepth); + SRLZ(LFO2ControlDepth); + SRLZ(LFO2Controller); + SRLZ(LFO2FlipPhase); + SRLZ(LFO2Sync); + SRLZ(EG3Attack); + SRLZ(EG3Depth); + SRLZ(LFO3WaveForm); + SRLZ(LFO3Frequency); + SRLZ(LFO3Phase); + SRLZ(LFO3InternalDepth); + SRLZ(LFO3ControlDepth); + SRLZ(LFO3Controller); + SRLZ(LFO3FlipPhase); + 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); + SRLZ(SustainReleaseTrigger); + SRLZ(NoNoteOffReleaseTrigger); + + // derived attributes from DLS::Sampler + SRLZ(FineTune); + SRLZ(Gain); + } + /** * Updates the respective member variable and updates @c SampleAttenuation * which depends on this value. @@ -1743,14 +2026,14 @@ RIFF::Chunk* _3ewa = pParentList->GetSubChunk(CHUNK_ID_3EWA); if (!_3ewa) { File* pFile = (File*) GetParent()->GetParent()->GetParent(); - bool version3 = pFile->pVersion && pFile->pVersion->major == 3; - _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, version3 ? 148 : 140); + bool versiongt2 = pFile->pVersion && pFile->pVersion->major > 2; + _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, versiongt2 ? 148 : 140); } pData = (uint8_t*) _3ewa->LoadChunkData(); // 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 +2293,82 @@ if (chunksize >= 148) { memcpy(&pData[140], DimensionUpperLimits, 8); } + + // chunk for own format extensions, these will *NOT* work with + // Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = pParentList->GetSubChunk(CHUNK_ID_LSDE); + const int lsdeSize = + 3 /* EG cancel options */ + + 1 /* sustain pedal up on release trigger option */ + + 8 /* LFOs' wave forms */ + 12 /* LFOs' phase */ + 4 /* flags (LFO3FlipPhase) */; + if (!lsde && UsesAnyGigFormatExtension()) { + // only add this "LSDE" chunk if there is some (format extension) + // setting effective that would require our "LSDE" format extension + // chunk to be stored + lsde = pParentList->AddSubChunk(CHUNK_ID_LSDE, lsdeSize); + // move LSDE chunk to the end of parent list + pParentList->MoveSubChunk(lsde, (RIFF::Chunk*)NULL); + } + if (lsde) { + if (lsde->GetNewSize() < lsdeSize) + lsde->Resize(lsdeSize); + // format extension for EG behavior options + unsigned char* pData = (unsigned char*) lsde->LoadChunkData(); + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + for (int i = 0; i < 2; ++i) { // NOTE: we reserved the 3rd byte for a potential future EG3 option + 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); + } + // format extension for release trigger options + pData[3] = static_cast(SustainReleaseTrigger) | (NoNoteOffReleaseTrigger ? (1<<7) : 0); + // format extension for LFOs' wave form, phase displacement and for + // LFO3's flip phase + store16(&pData[4], LFO1WaveForm); + store16(&pData[6], LFO2WaveForm); + store16(&pData[8], LFO3WaveForm); + //NOTE: 16 bits reserved here for potential future use ! + const int32_t lfo1Phase = (int32_t) GIG_EXP_ENCODE(LFO1Phase); + const int32_t lfo2Phase = (int32_t) GIG_EXP_ENCODE(LFO2Phase); + const int32_t lfo3Phase = (int32_t) GIG_EXP_ENCODE(LFO3Phase); + store32(&pData[12], lfo1Phase); + store32(&pData[16], lfo2Phase); + store32(&pData[20], lfo3Phase); + const int32_t flags = LFO3FlipPhase ? 1 : 0; + store32(&pData[24], flags); + + // compile time sanity check: is our last store access here + // consistent with the initial lsdeSize value assignment? + static_assert(lsdeSize == 28, "Inconsistency in assumed 'LSDE' RIFF chunk size"); + } + } + + /** + * Returns @c true in case this DimensionRegion object uses any gig format + * extension, that is whether this DimensionRegion object currently has any + * setting effective that would require our "LSDE" RIFF chunk to be stored + * to the gig file. + * + * Right now this is a private method. It is considerable though this method + * to become (in slightly modified form) a public API method in future, i.e. + * to allow instrument editors to visualize and/or warn the user of any + * format extension being used. Right now this method really just serves to + * answer the question whether an LSDE chunk is required, for the public API + * purpose this method would also need to check whether any other setting + * stored to the regular value '3ewa' chunk, is actually a format extension + * as well. + */ + bool DimensionRegion::UsesAnyGigFormatExtension() const { + eg_opt_t defaultOpt; + return memcmp(&EG1Options, &defaultOpt, sizeof(eg_opt_t)) || + memcmp(&EG2Options, &defaultOpt, sizeof(eg_opt_t)) || + SustainReleaseTrigger || NoNoteOffReleaseTrigger || + LFO1WaveForm || LFO2WaveForm || LFO3WaveForm || + LFO1Phase || LFO2Phase || LFO3Phase || + LFO3FlipPhase; } double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) { @@ -2049,6 +2408,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 +2810,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; } @@ -2918,7 +3307,7 @@ } Layers = 1; File* file = (File*) GetParent()->GetParent(); - int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; + int dimensionBits = (file->pVersion && file->pVersion->major > 2) ? 8 : 5; // Actual Loading @@ -2928,6 +3317,8 @@ RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); if (_3lnk) { + _3lnk->SetPos(0); + DimensionRegions = _3lnk->ReadUint32(); for (int i = 0; i < dimensionBits; i++) { dimension_t dimension = static_cast(_3lnk->ReadUint8()); @@ -2962,7 +3353,7 @@ UpdateVelocityTable(); // jump to start of the wave pool indices (if not already there) - if (file->pVersion && file->pVersion->major == 3) + if (file->pVersion && file->pVersion->major > 2) _3lnk->SetPos(68); // version 3 has a different 3lnk structure else _3lnk->SetPos(44); @@ -2971,7 +3362,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 } @@ -3020,14 +3412,14 @@ } File* pFile = (File*) GetParent()->GetParent(); - bool version3 = pFile->pVersion && pFile->pVersion->major == 3; - const int iMaxDimensions = version3 ? 8 : 5; - const int iMaxDimensionRegions = version3 ? 256 : 32; + bool versiongt2 = pFile->pVersion && pFile->pVersion->major > 2; + const int iMaxDimensions = versiongt2 ? 8 : 5; + const int iMaxDimensionRegions = versiongt2 ? 256 : 32; // make sure '3lnk' chunk exists RIFF::Chunk* _3lnk = pCkRegion->GetSubChunk(CHUNK_ID_3LNK); if (!_3lnk) { - const int _3lnkChunkSize = version3 ? 1092 : 172; + const int _3lnkChunkSize = versiongt2 ? 1092 : 172; _3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); @@ -3051,7 +3443,7 @@ } // update wave pool table in '3lnk' chunk - const int iWavePoolOffset = version3 ? 68 : 44; + const int iWavePoolOffset = versiongt2 ? 68 : 44; for (uint i = 0; i < iMaxDimensionRegions; i++) { int iWaveIndex = -1; if (i < DimensionRegions) { @@ -3194,7 +3586,7 @@ // check if max. amount of dimensions reached File* file = (File*) GetParent()->GetParent(); - const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; + const int iMaxDimensions = (file->pVersion && file->pVersion->major > 2) ? 8 : 5; if (Dimensions >= iMaxDimensions) throw gig::Exception("Could not add new dimension, max. amount of " + ToString(iMaxDimensions) + " dimensions already reached"); // check if max. amount of dimension bits reached @@ -3486,6 +3878,7 @@ } // delete temporary region + tempRgn->DeleteChunks(); delete tempRgn; UpdateVelocityTable(); @@ -3625,6 +4018,7 @@ } // delete temporary region + tempRgn->DeleteChunks(); delete tempRgn; UpdateVelocityTable(); @@ -3718,7 +4112,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++) { @@ -3767,7 +4161,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++) { @@ -3852,6 +4246,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) @@ -4102,6 +4497,8 @@ pGroup = group; pChunk = ckScri; if (ckScri) { // object is loaded from file ... + ckScri->SetPos(0); + // read header uint32_t headerSize = ckScri->ReadUint32(); Compression = (Compression_t) ckScri->ReadUint32(); @@ -4116,7 +4513,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(); @@ -4154,6 +4551,18 @@ memcpy(&data[0], &text[0], text.size()); } + /** @brief Remove all RIFF chunks associated with this Script object. + * + * At the moment Script::DeleteChunks() does nothing. It is + * recommended to call this method explicitly though from deriving classes's + * own overridden implementation of this method to avoid potential future + * compatiblity issues. + * + * See DLS::Storage::DeleteChunks() for details. + */ + void Script::DeleteChunks() { + } + /** * Apply this script to the respective RIFF chunks. You have to call * File::Save() to make changes persistent. @@ -4167,15 +4576,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); @@ -4187,7 +4596,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]; @@ -4218,6 +4627,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) { @@ -4253,6 +4678,18 @@ } } + /** @brief Remove all RIFF chunks associated with this ScriptGroup object. + * + * At the moment ScriptGroup::DeleteChunks() does nothing. It is + * recommended to call this method explicitly though from deriving classes's + * own overridden implementation of this method to avoid potential future + * compatiblity issues. + * + * See DLS::Storage::DeleteChunks() for details. + */ + void ScriptGroup::DeleteChunks() { + } + /** * Apply this script group to the respective RIFF chunks. You have to call * File::Save() to make changes persistent. @@ -4364,7 +4801,7 @@ EffectSend = 0; Attenuation = 0; FineTune = 0; - PitchbendRange = 0; + PitchbendRange = 2; PianoReleaseMode = false; DimensionKeyRange.low = 0; DimensionKeyRange.high = 0; @@ -4377,6 +4814,8 @@ if (lart) { RIFF::Chunk* _3ewg = lart->GetSubChunk(CHUNK_ID_3EWG); if (_3ewg) { + _3ewg->SetPos(0); + EffectSend = _3ewg->ReadUint16(); Attenuation = _3ewg->ReadInt32(); FineTune = _3ewg->ReadInt16(); @@ -4421,7 +4860,8 @@ RIFF::List* rgn = lrgn->GetFirstSubList(); while (rgn) { if (rgn->GetListType() == LIST_TYPE_RGN) { - __notify_progress(pProgress, (float) pRegions->size() / (float) Regions); + if (pProgress) + __notify_progress(pProgress, (float) pRegions->size() / (float) Regions); pRegions->push_back(new Region(this, rgn)); } rgn = lrgn->GetNextSubList(); @@ -4436,6 +4876,8 @@ if (lst3LS) { RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); if (ckSCSL) { + ckSCSL->SetPos(0); + int headerSize = ckSCSL->ReadUint32(); int slotCount = ckSCSL->ReadUint32(); if (slotCount) { @@ -4453,7 +4895,8 @@ } } - __notify_progress(pProgress, 1.0f); // notify done + if (pProgress) + __notify_progress(pProgress, 1.0f); // notify done } void Instrument::UpdateRegionKeyTable() { @@ -4462,7 +4905,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; } } @@ -4507,7 +4952,7 @@ File* pFile = (File*) GetParent(); // 3ewg is bigger in gig3, as it includes the iMIDI rules - int size = (pFile->pVersion && pFile->pVersion->major == 3) ? 16416 : 12; + int size = (pFile->pVersion && pFile->pVersion->major > 2) ? 16416 : 12; _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, size); memset(_3ewg->LoadChunkData(), 0, size); } @@ -4539,7 +4984,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; @@ -4575,14 +5020,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); @@ -4642,7 +5088,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 @@ -4788,9 +5234,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; @@ -4915,7 +5363,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 ); } @@ -4937,7 +5385,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. @@ -5061,9 +5509,23 @@ ::LoadString(pNameChunk, Name); } + /** @brief Destructor. + * + * Currently this destructor implementation does nothing. + */ Group::~Group() { - // remove the chunk associated with this group (if any) - if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk); + } + + /** @brief Remove all RIFF chunks associated with this Group object. + * + * See DLS::Storage::DeleteChunks() for details. + */ + void Group::DeleteChunks() { + // handle own RIFF chunks + if (pNameChunk) { + pNameChunk->GetParent()->DeleteSubChunk(pNameChunk); + pNameChunk = NULL; + } } /** @brief Update chunks with current group settings. @@ -5086,7 +5548,7 @@ RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); - if (!pNameChunk && pFile->pVersion && pFile->pVersion->major == 3) { + if (!pNameChunk && pFile->pVersion && pFile->pVersion->major > 2) { // v3 has a fixed list of 128 strings, find a free one for (RIFF::Chunk* ck = _3gnl->GetFirstSubChunk() ; ck ; ck = _3gnl->GetNextSubChunk()) { if (strcmp(static_cast(ck->LoadChunkData()), "") == 0) { @@ -5181,6 +5643,11 @@ 0, 3, 20030331 & 0xffff, 20030331 >> 16 }; + /// Reflects Gigasampler file format version 4.0 (2007-10-12). + const DLS::version_t File::VERSION_4 = { + 0, 4, 20071012 & 0xffff, 20071012 >> 16 + }; + static const DLS::Info::string_length_t _FileFixedStringLengths[] = { { CHUNK_ID_IARL, 256 }, { CHUNK_ID_IART, 128 }, @@ -5277,6 +5744,20 @@ return static_cast( *it ); } + /** + * Returns the total amount of samples of this gig file. + * + * Note that this method might block for a long time in case it is required + * to load the sample info for the first time. + * + * @returns total amount of samples + */ + size_t File::CountSamples() { + if (!pSamples) LoadSamples(); + if (!pSamples) return 0; + return pSamples->size(); + } + /** @brief Add a new sample. * * This will create a new Sample object for the gig file. You have to @@ -5315,6 +5796,7 @@ if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation pSamples->erase(iter); + pSample->DeleteChunks(); delete pSample; SampleList::iterator tmp = SamplesIterator; @@ -5352,50 +5834,103 @@ int iSampleIndex = 0; int iTotalSamples = WavePoolCount; - // check if samples should be loaded from extension files - // (only for old gig files < 2 GB) - int lastFileNo = 0; - if (!file->IsNew() && !(file->GetCurrentFileSize() >> 31)) { - for (int i = 0 ; i < WavePoolCount ; i++) { - if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; - } - } - String name(pRIFF->GetFileName()); - int nameLen = name.length(); - char suffix[6]; - if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; - - for (int fileNo = 0 ; ; ) { + // just for assembling path of optional extension files to be read + const std::string folder = parentPath(pRIFF->GetFileName()); + const std::string baseName = pathWithoutExtension(pRIFF->GetFileName()); + + // the main gig file and the extension files (.gx01, ... , .gx98) may + // contain wave data (wave pool) + std::vector poolFiles; + poolFiles.push_back(pRIFF); + + // get info about all extension files + RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); + if (ckXfil) { // there are extension files (.gx01, ... , .gx98) ... + const uint32_t n = ckXfil->ReadInt32(); + for (int i = 0; i < n; i++) { + // read the filename and load the extension file + std::string name; + ckXfil->ReadString(name, 128); + std::string path = concatPath(folder, name); + RIFF::File* pExtFile = new RIFF::File(path); + // check that the dlsids match + RIFF::Chunk* ckDLSID = pExtFile->GetSubChunk(CHUNK_ID_DLID); + if (ckDLSID) { + ::DLS::dlsid_t idExpected; + idExpected.ulData1 = ckXfil->ReadInt32(); + idExpected.usData2 = ckXfil->ReadInt16(); + idExpected.usData3 = ckXfil->ReadInt16(); + ckXfil->Read(idExpected.abData, 8, 1); + ::DLS::dlsid_t idFound; + ckDLSID->Read(&idFound.ulData1, 1, 4); + ckDLSID->Read(&idFound.usData2, 1, 2); + ckDLSID->Read(&idFound.usData3, 1, 2); + ckDLSID->Read(idFound.abData, 8, 1); + if (memcmp(&idExpected, &idFound, 16) != 0) + throw gig::Exception("dlsid mismatch for extension file: %s", path.c_str()); + } + poolFiles.push_back(pExtFile); + ExtensionFiles.push_back(pExtFile); + } + } + + // check if a .gx99 (GigaPulse) file exists + RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); + if (ckDoxf) { // there is a .gx99 (GigaPulse) file ... + std::string path = baseName + ".gx99"; + RIFF::File* pExtFile = new RIFF::File(path); + + // skip unused int and filename + ckDoxf->SetPos(132, RIFF::stream_curpos); + + // check that the dlsids match + RIFF::Chunk* ckDLSID = pExtFile->GetSubChunk(CHUNK_ID_DLID); + if (ckDLSID) { + ::DLS::dlsid_t idExpected; + idExpected.ulData1 = ckDoxf->ReadInt32(); + idExpected.usData2 = ckDoxf->ReadInt16(); + idExpected.usData3 = ckDoxf->ReadInt16(); + ckDoxf->Read(idExpected.abData, 8, 1); + ::DLS::dlsid_t idFound; + ckDLSID->Read(&idFound.ulData1, 1, 4); + ckDLSID->Read(&idFound.usData2, 1, 2); + ckDLSID->Read(&idFound.usData3, 1, 2); + ckDLSID->Read(idFound.abData, 8, 1); + if (memcmp(&idExpected, &idFound, 16) != 0) + throw gig::Exception("dlsid mismatch for GigaPulse file: %s", path.c_str()); + } + poolFiles.push_back(pExtFile); + ExtensionFiles.push_back(pExtFile); + } + + // load samples from extension files (if required) + for (int i = 0; i < poolFiles.size(); i++) { + RIFF::File* file = poolFiles[i]; RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); if (wvpl) { - file_offset_t wvplFileOffset = wvpl->GetFilePos(); + file_offset_t wvplFileOffset = wvpl->GetFilePos() - + wvpl->GetPos(); // should be zero, but just to be sure RIFF::List* wave = wvpl->GetFirstSubList(); while (wave) { if (wave->GetListType() == LIST_TYPE_WAVE) { // notify current progress - const float subprogress = (float) iSampleIndex / (float) iTotalSamples; - __notify_progress(pProgress, subprogress); + if (pProgress) { + const float subprogress = (float) iSampleIndex / (float) iTotalSamples; + __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, i, iSampleIndex)); iSampleIndex++; } wave = wvpl->GetNextSubList(); } - - if (fileNo == lastFileNo) break; - - // open extension file (*.gx01, *.gx02, ...) - fileNo++; - sprintf(suffix, ".gx%02d", fileNo); - name.replace(nameLen, 5, suffix); - file = new RIFF::File(name); - ExtensionFiles.push_back(file); - } else break; + } } - __notify_progress(pProgress, 1.0); // notify done + if (pProgress) + __notify_progress(pProgress, 1.0); // notify done } Instrument* File::GetFirstInstrument() { @@ -5412,6 +5947,20 @@ } /** + * Returns the total amount of instruments of this gig file. + * + * Note that this method might block for a long time in case it is required + * to load the instruments info for the first time. + * + * @returns total amount of instruments + */ + size_t File::CountInstruments() { + if (!pInstruments) LoadInstruments(); + if (!pInstruments) return 0; + return pInstruments->size(); + } + + /** * Returns the instrument with the given index. * * @param index - number of the sought instrument (0..n) @@ -5422,22 +5971,31 @@ if (!pInstruments) { // TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM) - // sample loading subtask - progress_t subprogress; - __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask - __notify_progress(&subprogress, 0.0f); - if (GetAutoLoad()) - GetFirstSample(&subprogress); // now force all samples to be loaded - __notify_progress(&subprogress, 1.0f); - - // instrument loading subtask - if (pProgress && pProgress->callback) { - subprogress.__range_min = subprogress.__range_max; - subprogress.__range_max = pProgress->__range_max; // schedule remaining percentage for this subtask - } - __notify_progress(&subprogress, 0.0f); - LoadInstruments(&subprogress); - __notify_progress(&subprogress, 1.0f); + if (pProgress) { + // sample loading subtask + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask + __notify_progress(&subprogress, 0.0f); + if (GetAutoLoad()) + GetFirstSample(&subprogress); // now force all samples to be loaded + __notify_progress(&subprogress, 1.0f); + + // instrument loading subtask + if (pProgress->callback) { + subprogress.__range_min = subprogress.__range_max; + subprogress.__range_max = pProgress->__range_max; // schedule remaining percentage for this subtask + } + __notify_progress(&subprogress, 0.0f); + LoadInstruments(&subprogress); + __notify_progress(&subprogress, 1.0f); + } else { + // sample loading subtask + if (GetAutoLoad()) + GetFirstSample(); // now force all samples to be loaded + + // instrument loading subtask + LoadInstruments(); + } } if (!pInstruments) return NULL; InstrumentsIterator = pInstruments->begin(); @@ -5530,7 +6088,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. // @@ -5574,6 +6144,7 @@ InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), (DLS::Instrument*) pInstrument); if (iter == pInstruments->end()) throw gig::Exception("Could not delete instrument, could not find given instrument"); pInstruments->erase(iter); + pInstrument->DeleteChunks(); delete pInstrument; } @@ -5589,21 +6160,26 @@ RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); while (lstInstr) { if (lstInstr->GetListType() == LIST_TYPE_INS) { - // notify current progress - const float localProgress = (float) iInstrumentIndex / (float) Instruments; - __notify_progress(pProgress, localProgress); - - // divide local progress into subprogress for loading current Instrument - progress_t subprogress; - __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex); + if (pProgress) { + // notify current progress + const float localProgress = (float) iInstrumentIndex / (float) Instruments; + __notify_progress(pProgress, localProgress); - pInstruments->push_back(new Instrument(this, lstInstr, &subprogress)); + // divide local progress into subprogress for loading current Instrument + progress_t subprogress; + __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex); + + pInstruments->push_back(new Instrument(this, lstInstr, &subprogress)); + } else { + pInstruments->push_back(new Instrument(this, lstInstr)); + } iInstrumentIndex++; } lstInstr = lstInstruments->GetNextSubList(); } - __notify_progress(pProgress, 1.0); // notify done + if (pProgress) + __notify_progress(pProgress, 1.0); // notify done } } @@ -5615,24 +6191,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 > 2) 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 @@ -5709,6 +6409,7 @@ } // now delete this group object pGroups->erase(iter); + pGroup->DeleteChunks(); delete pGroup; } @@ -5730,6 +6431,7 @@ // move all members of this group to another group pGroup->MoveAll(); pGroups->erase(iter); + pGroup->DeleteChunks(); delete pGroup; } @@ -5743,7 +6445,7 @@ RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); while (ck) { if (ck->GetChunkID() == CHUNK_ID_3GNM) { - if (pVersion && pVersion->major == 3 && + if (pVersion && pVersion->major > 2 && strcmp(static_cast(ck->LoadChunkData()), "") == 0) break; pGroups->push_back(new Group(this, ck)); @@ -5829,6 +6531,7 @@ pScriptGroup->DeleteScript(pScriptGroup->GetScript(i)); if (pScriptGroup->pList) pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList); + pScriptGroup->DeleteChunks(); delete pScriptGroup; } @@ -5919,7 +6622,7 @@ // v3: make sure the file has 128 3gnm chunks // (before updating the Group chunks) - if (pVersion && pVersion->major == 3) { + if (pVersion && pVersion->major > 2) { RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk(); for (int i = 0 ; i < 128 ; i++) { if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64); @@ -5950,7 +6653,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); @@ -6023,7 +6726,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; @@ -6041,25 +6744,43 @@ 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); + if (einf && pVersion && pVersion->major > 2) 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; + } + } } } @@ -6074,8 +6795,8 @@ } /** - * Enable / disable automatic loading. By default this properyt is - * enabled and all informations are loaded automatically. However + * Enable / disable automatic loading. By default this property is + * enabled and every information is loaded automatically. However * loading all Regions, DimensionRegions and especially samples might * take a long time for large .gig files, and sometimes one might only * be interested in retrieving very superficial informations like the @@ -6083,10 +6804,10 @@ * automatic loading to avoid very slow response times. * * @e CAUTION: by disabling this property many pointers (i.e. sample - * references) and informations will have invalid or even undefined + * references) and attributes will have invalid or even undefined * data! This feature is currently only intended for retrieving very - * superficial informations in a very fast way. Don't use it to retrieve - * details like synthesis informations or even to modify .gig files! + * superficial information in a very fast way. Don't use it to retrieve + * details like synthesis information or even to modify .gig files! */ void File::SetAutoLoad(bool b) { bAutoLoad = b; @@ -6105,7 +6826,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() {