--- libgig/trunk/src/gig.cpp 2018/12/22 18:59:29 3442 +++ libgig/trunk/src/gig.cpp 2020/01/22 15:37:09 3722 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2018 by Christian Schoenebeck * + * Copyright (C) 2003-2020 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -412,6 +412,8 @@ pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); if (pCk3gix) { + pCk3gix->SetPos(0); + uint16_t iSampleGroup = pCk3gix->ReadInt16(); pGroup = pFile->GetGroup(iSampleGroup); } else { // '3gix' chunk missing @@ -421,6 +423,8 @@ pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); if (pCkSmpl) { + pCkSmpl->SetPos(0); + Manufacturer = pCkSmpl->ReadInt32(); Product = pCkSmpl->ReadInt32(); SamplePeriod = pCkSmpl->ReadInt32(); @@ -467,6 +471,8 @@ Dithered = false; TruncatedBits = 0; if (Compressed) { + ewav->SetPos(0); + uint32_t version = ewav->ReadInt32(); if (version > 2 && BitDepth == 24) { Dithered = ewav->ReadInt32(); @@ -1500,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()); @@ -1732,9 +1740,12 @@ VCFType = vcf_type_lowpass; 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(); @@ -1748,8 +1759,35 @@ // 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); - SustainReleaseTrigger = static_cast(lsde->ReadUint8()); - } else SustainReleaseTrigger = sust_rel_trg_none; + 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, @@ -1875,7 +1913,9 @@ SRLZ(EG1ControllerAttackInfluence); SRLZ(EG1ControllerDecayInfluence); SRLZ(EG1ControllerReleaseInfluence); + SRLZ(LFO1WaveForm); SRLZ(LFO1Frequency); + SRLZ(LFO1Phase); SRLZ(LFO1InternalDepth); SRLZ(LFO1ControlDepth); SRLZ(LFO1Controller); @@ -1893,7 +1933,9 @@ SRLZ(EG2ControllerAttackInfluence); SRLZ(EG2ControllerDecayInfluence); SRLZ(EG2ControllerReleaseInfluence); + SRLZ(LFO2WaveForm); SRLZ(LFO2Frequency); + SRLZ(LFO2Phase); SRLZ(LFO2InternalDepth); SRLZ(LFO2ControlDepth); SRLZ(LFO2Controller); @@ -1901,10 +1943,13 @@ 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); @@ -1941,6 +1986,7 @@ SRLZ(EG1Options); SRLZ(EG2Options); SRLZ(SustainReleaseTrigger); + SRLZ(NoNoteOffReleaseTrigger); // derived attributes from DLS::Sampler SRLZ(FineTune); @@ -2251,19 +2297,17 @@ // chunk for own format extensions, these will *NOT* work with // Gigasampler/GigaStudio ! RIFF::Chunk* lsde = pParentList->GetSubChunk(CHUNK_ID_LSDE); - const int lsdeSize = 4; // NOTE: we reserved the 3rd byte for a potential future EG3 option - if (!lsde) { - // only add this "LSDE" chunk if either EG options or sustain - // release trigger option deviate from their default behaviour - eg_opt_t defaultOpt; - if (memcmp(&EG1Options, &defaultOpt, sizeof(eg_opt_t)) || - memcmp(&EG2Options, &defaultOpt, sizeof(eg_opt_t)) || - SustainReleaseTrigger) - { - lsde = pParentList->AddSubChunk(CHUNK_ID_LSDE, lsdeSize); - // move LSDE chunk to the end of parent list - pParentList->MoveSubChunk(lsde, (RIFF::Chunk*)NULL); - } + 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) @@ -2279,11 +2323,54 @@ (pEGOpts[i]->Decay2Cancel ? (1<<3) : 0) | (pEGOpts[i]->ReleaseCancel ? (1<<4) : 0); } - // format extension for effect of sustain pedal up event on release trigger samples - pData[3] = static_cast(SustainReleaseTrigger); + // 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) { curve_type_t curveType = releaseVelocityResponseCurve; uint8_t depth = releaseVelocityResponseDepth; @@ -3230,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()); @@ -3323,7 +3412,7 @@ } File* pFile = (File*) GetParent()->GetParent(); - bool versiongt2 = pFile->pVersion && pFile->pVersion->major > 2; + const bool versiongt2 = pFile->pVersion && pFile->pVersion->major > 2; const int iMaxDimensions = versiongt2 ? 8 : 5; const int iMaxDimensionRegions = versiongt2 ? 256 : 32; @@ -3370,6 +3459,39 @@ } store32(&pData[iWavePoolOffset + i * 4], iWaveIndex); } + + // The following chunks are just added for compatibility with the + // GigaStudio software, which would show a warning if these were + // missing. However currently these chunks don't cover any useful + // data. So if this gig file uses any of our own gig format + // extensions which would cause this gig file to be unloadable + // with GSt software anyway, then just skip these GSt compatibility + // chunks here as well. + if (versiongt2 && !UsesAnyGigFormatExtension()) { + // add 3dnm list which always seems to be empty + RIFF::List* _3dnm = pCkRegion->GetSubList(LIST_TYPE_3DNM); + if (!_3dnm) _3dnm = pCkRegion->AddSubList(LIST_TYPE_3DNM); + + // add 3ddp chunk which always seems to have 16 bytes of 0xFF + RIFF::Chunk* _3ddp = pCkRegion->GetSubChunk(CHUNK_ID_3DDP); + if (!_3ddp) _3ddp = pCkRegion->AddSubChunk(CHUNK_ID_3DDP, 16); + uint8_t* pData = (uint8_t*) _3ddp->LoadChunkData(); + for (int i = 0; i < 16; i += 4) { + store32(&pData[i], 0xFFFFFFFF); + } + + // move 3dnm and 3ddp to the end of the region list + pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3DNM), (RIFF::Chunk*)NULL); + pCkRegion->MoveSubChunk(pCkRegion->GetSubChunk(CHUNK_ID_3DDP), (RIFF::Chunk*)NULL); + } else { + // this is intended for the user switching from GSt >= 3 version + // back to an older format version, delete GSt3 chunks ... + RIFF::List* _3dnm = pCkRegion->GetSubList(LIST_TYPE_3DNM); + if (_3dnm) pCkRegion->DeleteSubChunk(_3dnm); + + RIFF::Chunk* _3ddp = pCkRegion->GetSubChunk(CHUNK_ID_3DDP); + if (_3ddp) pCkRegion->DeleteSubChunk(_3ddp); + } } void Region::LoadDimensionRegions(RIFF::List* rgn) { @@ -3768,9 +3890,23 @@ // now tempRegion's dimensions and DimensionRegions basically reflect // what we wanted to get for this actual Region here, so we now just // delete and recreate the dimension in question with the new amount - // zones and then copy back from tempRegion - DeleteDimension(oldDef); - AddDimension(&newDef); + // zones and then copy back from tempRegion. we're actually deleting and + // recreating all dimensions here, to avoid altering the precise order + // of the dimensions (which would not be an error per se, but it would + // cause usability issues with instrument editors) + { + std::vector oldDefs; + for (int i = 0; i < Dimensions; ++i) + oldDefs.push_back(pDimensionDefinitions[i]); // copy, don't reference + for (int i = Dimensions - 1; i >= 0; --i) + DeleteDimension(&pDimensionDefinitions[i]); + for (int i = 0; i < oldDefs.size(); ++i) { + dimension_def_t& def = oldDefs[i]; + AddDimension( + (def.dimension == newDef.dimension) ? &newDef : &def + ); + } + } for (int iSrc = 0; iSrc < 256; ++iSrc) { DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc]; if (!srcDimRgn) continue; @@ -3789,6 +3925,7 @@ } // delete temporary region + tempRgn->DeleteChunks(); delete tempRgn; UpdateVelocityTable(); @@ -3907,9 +4044,23 @@ // now tempRegion's dimensions and DimensionRegions basically reflect // what we wanted to get for this actual Region here, so we now just // delete and recreate the dimension in question with the new amount - // zones and then copy back from tempRegion - DeleteDimension(oldDef); - AddDimension(&newDef); + // zones and then copy back from tempRegion. we're actually deleting and + // recreating all dimensions here, to avoid altering the precise order + // of the dimensions (which would not be an error per se, but it would + // cause usability issues with instrument editors) + { + std::vector oldDefs; + for (int i = 0; i < Dimensions; ++i) + oldDefs.push_back(pDimensionDefinitions[i]); // copy, don't reference + for (int i = Dimensions - 1; i >= 0; --i) + DeleteDimension(&pDimensionDefinitions[i]); + for (int i = 0; i < oldDefs.size(); ++i) { + dimension_def_t& def = oldDefs[i]; + AddDimension( + (def.dimension == newDef.dimension) ? &newDef : &def + ); + } + } for (int iSrc = 0; iSrc < 256; ++iSrc) { DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc]; if (!srcDimRgn) continue; @@ -3928,6 +4079,7 @@ } // delete temporary region + tempRgn->DeleteChunks(); delete tempRgn; UpdateVelocityTable(); @@ -4232,6 +4384,29 @@ Layers = orig->Layers; } + /** + * Returns @c true in case this Region object uses any gig format + * extension, that is e.g. whether any 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 gig + * format extension being used. See also comments on + * DimensionRegion::UsesAnyGigFormatExtension() for details about such a + * potential public API change in future. + */ + bool Region::UsesAnyGigFormatExtension() const { + for (int i = 0; i < 256; i++) { + if (pDimensionRegions[i]) { + if (pDimensionRegions[i]->UsesAnyGigFormatExtension()) + return true; + } + } + return false; + } + // *************** MidiRule *************** // * @@ -4406,12 +4581,14 @@ 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(); Encoding = (Encoding_t) ckScri->ReadUint32(); Language = (Language_t) ckScri->ReadUint32(); - Bypass = (Language_t) ckScri->ReadUint32() & 1; + Bypass = ckScri->ReadUint32() & 1; crc = ckScri->ReadUint32(); uint32_t nameSize = ckScri->ReadUint32(); Name.resize(nameSize, ' '); @@ -4458,6 +4635,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. @@ -4573,6 +4762,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. @@ -4697,6 +4898,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(); @@ -4741,7 +4944,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(); @@ -4756,6 +4960,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) { @@ -4773,7 +4979,8 @@ } } - __notify_progress(pProgress, 1.0f); // notify done + if (pProgress) + __notify_progress(pProgress, 1.0f); // notify done } void Instrument::UpdateRegionKeyTable() { @@ -5370,6 +5577,31 @@ UpdateRegionKeyTable(); } + /** + * Returns @c true in case this Instrument object uses any gig format + * extension, that is e.g. whether any 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 gig + * format extension being used. See also comments on + * DimensionRegion::UsesAnyGigFormatExtension() for details about such a + * potential public API change in future. + */ + bool Instrument::UsesAnyGigFormatExtension() const { + if (!pRegions) return false; + RegionList::const_iterator iter = pRegions->begin(); + RegionList::const_iterator end = pRegions->end(); + for (; iter != end; ++iter) { + gig::Region* rgn = static_cast(*iter); + if (rgn->UsesAnyGigFormatExtension()) + return true; + } + return false; + } + // *************** Group *************** // * @@ -5386,9 +5618,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. @@ -5659,6 +5905,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; @@ -5696,50 +5943,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 = (int) 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, iSampleIndex)); + 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() { @@ -5780,22 +6080,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(); @@ -5944,6 +6253,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; } @@ -5959,21 +6269,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 } } @@ -6203,6 +6518,7 @@ } // now delete this group object pGroups->erase(iter); + pGroup->DeleteChunks(); delete pGroup; } @@ -6224,6 +6540,7 @@ // move all members of this group to another group pGroup->MoveAll(); pGroups->erase(iter); + pGroup->DeleteChunks(); delete pGroup; } @@ -6323,6 +6640,7 @@ pScriptGroup->DeleteScript(pScriptGroup->GetScript(i)); if (pScriptGroup->pList) pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList); + pScriptGroup->DeleteChunks(); delete pScriptGroup; } @@ -6416,7 +6734,9 @@ 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); + // create 128 empty placeholder strings which will either + // be filled by Group::UpdateChunks below or left empty. + ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64); if (_3gnm) _3gnm = _3gnl->GetNextSubChunk(); } } @@ -6586,8 +6906,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 @@ -6595,10 +6915,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; @@ -6612,6 +6932,30 @@ return bAutoLoad; } + /** + * Returns @c true in case this gig File object uses any gig format + * extension, that is e.g. whether any 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 gig + * format extension being used. See also comments on + * DimensionRegion::UsesAnyGigFormatExtension() for details about such a + * potential public API change in future. + */ + bool File::UsesAnyGigFormatExtension() const { + if (!pInstruments) return false; + InstrumentList::iterator iter = pInstruments->begin(); + InstrumentList::iterator end = pInstruments->end(); + for (; iter != end; ++iter) { + Instrument* pInstrument = static_cast(*iter); + if (pInstrument->UsesAnyGigFormatExtension()) + return true; + } + return false; + } // *************** Exception ***************