--- libgig/trunk/src/gig.cpp 2020/01/22 15:37:09 3722 +++ libgig/trunk/src/gig.cpp 2021/06/19 10:44:14 3957 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2020 by Christian Schoenebeck * + * Copyright (C) 2003-2021 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -482,8 +482,8 @@ ScanCompressedSample(); } - // we use a buffer for decompression and for truncating 24 bit samples to 16 bit - if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) { + // we use a buffer for decompression only + if (Compressed && !InternalDecompressionBuffer.Size) { InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE]; InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE; } @@ -609,8 +609,8 @@ uint16_t iSampleGroup = 0; // 0 refers to default sample group File* pFile = static_cast(pParent); if (pFile->pGroups) { - std::list::iterator iter = pFile->pGroups->begin(); - std::list::iterator end = pFile->pGroups->end(); + std::vector::iterator iter = pFile->pGroups->begin(); + std::vector::iterator end = pFile->pGroups->end(); for (int i = 0; iter != end; i++, iter++) { if (*iter == pGroup) { iSampleGroup = i; @@ -3498,13 +3498,14 @@ RIFF::List* _3prg = rgn->GetSubList(LIST_TYPE_3PRG); if (_3prg) { int dimensionRegionNr = 0; - RIFF::List* _3ewl = _3prg->GetFirstSubList(); - while (_3ewl) { + size_t i = 0; + for (RIFF::List* _3ewl = _3prg->GetSubListAt(i); _3ewl; + _3ewl = _3prg->GetSubListAt(++i)) + { if (_3ewl->GetListType() == LIST_TYPE_3EWL) { pDimensionRegions[dimensionRegionNr] = new DimensionRegion(this, _3ewl); dimensionRegionNr++; } - _3ewl = _3prg->GetNextSubList(); } if (dimensionRegionNr == 0) throw gig::Exception("No dimension region found."); } @@ -3807,6 +3808,8 @@ * @throws gig::Exception if requested zone could not be deleted */ void Region::DeleteDimensionZone(dimension_t type, int zone) { + if (!Dimensions) + throw gig::Exception("Could not delete dimension zone, because there is no dimension at all."); dimension_def_t* oldDef = GetDimensionDefinition(type); if (!oldDef) throw gig::Exception("Could not delete dimension zone, no such dimension of given type"); @@ -3835,7 +3838,7 @@ // requested by the arguments of this method call) to the temporary // region, and don't use Region::CopyAssign() here for this task, since // it would also alter fast lookup helper variables here and there - dimension_def_t newDef; + dimension_def_t newDef = {}; for (int i = 0; i < Dimensions; ++i) { dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference // is this the dimension requested by the method arguments? ... @@ -3846,6 +3849,9 @@ } tempRgn->AddDimension(&def); } + // silence clang sanitizer warning + if (newDef.dimension == dimension_none) + throw gig::Exception("Unexpected internal failure resolving dimension in DeleteDimensionZone() [this is a bug]."); // find the dimension index in the tempRegion which is the dimension // type passed to this method (paranoidly expecting different order) @@ -3946,6 +3952,8 @@ * @throws gig::Exception if requested zone could not be splitted */ void Region::SplitDimensionZone(dimension_t type, int zone) { + if (!Dimensions) + throw gig::Exception("Could not split dimension zone, because there is no dimension at all."); dimension_def_t* oldDef = GetDimensionDefinition(type); if (!oldDef) throw gig::Exception("Could not split dimension zone, no such dimension of given type"); @@ -3972,7 +3980,7 @@ // requested by the arguments of this method call) to the temporary // region, and don't use Region::CopyAssign() here for this task, since // it would also alter fast lookup helper variables here and there - dimension_def_t newDef; + dimension_def_t newDef = {}; for (int i = 0; i < Dimensions; ++i) { dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference // is this the dimension requested by the method arguments? ... @@ -3983,6 +3991,9 @@ } tempRgn->AddDimension(&def); } + // silence clang sanitizer warning + if (newDef.dimension == dimension_none) + throw gig::Exception("Unexpected internal failure resolving dimension in SplitDimensionZone() [this is a bug]."); // find the dimension index in the tempRegion which is the dimension // type passed to this method (paranoidly expecting different order) @@ -4314,21 +4325,23 @@ uint64_t soughtoffset = uint64_t(file->pWavePoolTable[WavePoolTableIndex]) | uint64_t(file->pWavePoolTableHi[WavePoolTableIndex]) << 32; - Sample* sample = file->GetFirstSample(pProgress); - while (sample) { + size_t i = 0; + for (Sample* sample = file->GetSample(i, pProgress); sample; + sample = file->GetSample(++i)) + { if (sample->ullWavePoolOffset == soughtoffset) - return static_cast(sample); - sample = file->GetNextSample(); + return sample; } } else { // use extension files and 32 bit wave pool offsets file_offset_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; file_offset_t soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; - Sample* sample = file->GetFirstSample(pProgress); - while (sample) { + size_t i = 0; + for (Sample* sample = file->GetSample(i, pProgress); sample; + sample = file->GetSample(++i)) + { if (sample->ullWavePoolOffset == soughtoffset && - sample->FileNo == soughtfileno) return static_cast(sample); - sample = file->GetNextSample(); + sample->FileNo == soughtfileno) return sample; } } return NULL; @@ -4594,6 +4607,14 @@ Name.resize(nameSize, ' '); for (int i = 0; i < nameSize; ++i) Name[i] = ckScri->ReadUint8(); + // check if an uuid was already stored along with this script + if (headerSize >= 6*sizeof(int32_t) + nameSize + 16) { // yes ... + for (uint i = 0; i < 16; ++i) { + Uuid[i] = ckScri->ReadUint8(); + } + } else { // no uuid yet, generate one now ... + GenerateUuid(); + } // to handle potential future extensions of the header ckScri->SetPos(sizeof(int32_t) + headerSize); // read actual script data @@ -4608,6 +4629,7 @@ Bypass = false; crc = 0; Name = "Unnamed Script"; + GenerateUuid(); } } @@ -4662,13 +4684,14 @@ __calculateCRC(&data[0], data.size(), crc); __finalizeCRC(crc); // make sure chunk exists and has the required size - const file_offset_t chunkSize = (file_offset_t) 7*sizeof(int32_t) + Name.size() + data.size(); + const file_offset_t chunkSize = + (file_offset_t) 7*sizeof(int32_t) + Name.size() + 16 + 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], uint32_t(6*sizeof(int32_t) + Name.size())); // total header size + store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size() + 16)); // total header size pos += sizeof(int32_t); store32(&pData[pos], Compression); pos += sizeof(int32_t); @@ -4684,11 +4707,37 @@ pos += sizeof(int32_t); for (int i = 0; i < Name.size(); ++i, ++pos) pData[pos] = Name[i]; + for (int i = 0; i < 16; ++i, ++pos) + pData[pos] = Uuid[i]; for (int i = 0; i < data.size(); ++i, ++pos) pData[pos] = data[i]; } /** + * Generate a new Universally Unique Identifier (UUID) for this script. + */ + void Script::GenerateUuid() { + DLS::dlsid_t dlsid; + DLS::Resource::GenerateDLSID(&dlsid); + Uuid[0] = dlsid.ulData1 & 0xff; + Uuid[1] = dlsid.ulData1 >> 8 & 0xff; + Uuid[2] = dlsid.ulData1 >> 16 & 0xff; + Uuid[3] = dlsid.ulData1 >> 24 & 0xff; + Uuid[4] = dlsid.usData2 & 0xff; + Uuid[5] = dlsid.usData2 >> 8 & 0xff; + Uuid[6] = dlsid.usData3 & 0xff; + Uuid[7] = dlsid.usData3 >> 8 & 0xff; + Uuid[8] = dlsid.abData[0]; + Uuid[9] = dlsid.abData[1]; + Uuid[10] = dlsid.abData[2]; + Uuid[11] = dlsid.abData[3]; + Uuid[12] = dlsid.abData[4]; + Uuid[13] = dlsid.abData[5]; + Uuid[14] = dlsid.abData[6]; + Uuid[15] = dlsid.abData[7]; + } + + /** * Move this script from its current ScriptGroup to another ScriptGroup * given by @a pGroup. * @@ -4752,8 +4801,8 @@ ScriptGroup::~ScriptGroup() { if (pScripts) { - std::list::iterator iter = pScripts->begin(); - std::list::iterator end = pScripts->end(); + std::vector::iterator iter = pScripts->begin(); + std::vector::iterator end = pScripts->end(); while (iter != end) { delete *iter; ++iter; @@ -4791,7 +4840,7 @@ // now store the name of this group as chunk as subchunk of the list chunk ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64); - for (std::list::iterator it = pScripts->begin(); + for (std::vector::iterator it = pScripts->begin(); it != pScripts->end(); ++it) { (*it)->UpdateChunks(pProgress); @@ -4806,12 +4855,10 @@ * @param index - number of the sought script (0..n) * @returns sought script or NULL if there's no such script */ - Script* ScriptGroup::GetScript(uint index) { + Script* ScriptGroup::GetScript(size_t index) { if (!pScripts) LoadScripts(); - std::list::iterator it = pScripts->begin(); - for (uint i = 0; it != pScripts->end(); ++i, ++it) - if (i == index) return *it; - return NULL; + if (index >= pScripts->size()) return NULL; + return (*pScripts)[index]; } /** @brief Add new instrument script. @@ -4844,7 +4891,7 @@ */ void ScriptGroup::DeleteScript(Script* pScript) { if (!pScripts) LoadScripts(); - std::list::iterator iter = + std::vector::iterator iter = find(pScripts->begin(), pScripts->end(), pScript); if (iter == pScripts->end()) throw gig::Exception("Could not delete script, could not find given script"); @@ -4857,11 +4904,12 @@ void ScriptGroup::LoadScripts() { if (pScripts) return; - pScripts = new std::list; + pScripts = new std::vector; if (!pList) return; - for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck; - ck = pList->GetNextSubChunk()) + size_t i = 0; + for (RIFF::Chunk* ck = pList->GetSubChunkAt(i); ck; + ck = pList->GetSubChunkAt(++i)) { if (ck->GetChunkID() == CHUNK_ID_SCRI) { pScripts->push_back(new Script(this, ck)); @@ -4941,14 +4989,15 @@ if (!pRegions) pRegions = new RegionList; RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); if (lrgn) { - RIFF::List* rgn = lrgn->GetFirstSubList(); - while (rgn) { + size_t i = 0; + for (RIFF::List* rgn = lrgn->GetSubListAt(i); rgn; + rgn = lrgn->GetSubListAt(++i)) + { if (rgn->GetListType() == LIST_TYPE_RGN) { if (pProgress) __notify_progress(pProgress, (float) pRegions->size() / (float) Regions); pRegions->push_back(new Region(this, rgn)); } - rgn = lrgn->GetNextSubList(); } // Creating Region Key Table for fast lookup UpdateRegionKeyTable(); @@ -4958,6 +5007,7 @@ // own gig format extensions RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS); if (lst3LS) { + // script slots (that is references to instrument scripts) RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); if (ckSCSL) { ckSCSL->SetPos(0); @@ -4977,6 +5027,44 @@ } } } + + // overridden script 'patch' variables + RIFF::Chunk* ckSCPV = lst3LS->GetSubChunk(CHUNK_ID_SCPV); + if (ckSCPV) { + ckSCPV->SetPos(0); + + int nScripts = ckSCPV->ReadUint32(); + for (int iScript = 0; iScript < nScripts; ++iScript) { + _UUID uuid; + for (int i = 0; i < 16; ++i) + uuid[i] = ckSCPV->ReadUint8(); + uint slot = ckSCPV->ReadUint32(); + ckSCPV->ReadUint32(); // unused, reserved 32 bit + int nVars = ckSCPV->ReadUint32(); + for (int iVar = 0; iVar < nVars; ++iVar) { + uint8_t type = ckSCPV->ReadUint8(); + ckSCPV->ReadUint8(); // unused, reserved byte + int blobSize = ckSCPV->ReadUint16(); + RIFF::file_offset_t pos = ckSCPV->GetPos(); + // assuming 1st bit is set in 'type', otherwise blob not + // supported for decoding + if (type & 1) { + String name, value; + int len = ckSCPV->ReadUint16(); + for (int i = 0; i < len; ++i) + name += (char) ckSCPV->ReadUint8(); + len = ckSCPV->ReadUint16(); + for (int i = 0; i < len; ++i) + value += (char) ckSCPV->ReadUint8(); + if (!name.empty()) // 'name' should never be empty, but just to be sure + scriptVars[uuid][slot][name] = value; + } + // also for potential future extensions: seek forward + // according to blob size + ckSCPV->SetPos(pos + blobSize); + } + } + } } if (pProgress) @@ -5068,6 +5156,8 @@ RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); + + // save script slots (that is references to instrument scripts) const int slotCount = (int) pScriptRefs->size(); const int headerSize = 3 * sizeof(uint32_t); const int slotSize = 2 * sizeof(uint32_t); @@ -5092,6 +5182,78 @@ store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0); pos += sizeof(uint32_t); } + + // save overridden script 'patch' variables ... + + // the actual 'scriptVars' member variable might contain variables of + // scripts which are currently no longer assigned to any script slot + // of this instrument, we need to get rid of these variables here to + // prevent saving those persistently, however instead of touching the + // member variable 'scriptVars' directly, rather strip a separate + // copy such that the overridden values are not lost during an + // instrument editor session (i.e. if script might be re-assigned) + _VarsByScript vars = stripScriptVars(); + if (!vars.empty()) { + // determine total size required for 'SCPV' RIFF chunk, and the + // total amount of scripts being overridden (the latter is + // required because a script might be used on several script + // slots, hence vars.size() could then not be used here instead) + size_t totalChunkSize = 4; + size_t totalScriptsOverridden = 0; + for (const auto& script : vars) { + for (const auto& slot : script.second) { + totalScriptsOverridden++; + totalChunkSize += 16 + 4 + 4 + 4; + for (const auto& var : slot.second) { + totalChunkSize += 4 + 2 + var.first.length() + + 2 + var.second.length(); + } + } + } + + // ensure 'SCPV' RIFF chunk exists (with required size) + RIFF::Chunk* ckSCPV = lst3LS->GetSubChunk(CHUNK_ID_SCPV); + if (!ckSCPV) ckSCPV = lst3LS->AddSubChunk(CHUNK_ID_SCPV, totalChunkSize); + else ckSCPV->Resize(totalChunkSize); + + // store the actual data to 'SCPV' RIFF chunk + uint8_t* pData = (uint8_t*) ckSCPV->LoadChunkData(); + int pos = 0; + store32(&pData[pos], (uint32_t) totalScriptsOverridden); // scripts count + pos += 4; + for (const auto& script : vars) { + for (const auto& slot : script.second) { + for (int i = 0; i < 16; ++i) + pData[pos+i] = script.first[i]; // uuid + pos += 16; + store32(&pData[pos], (uint32_t) slot.first); // slot index + pos += 4; + store32(&pData[pos], (uint32_t) 0); // unused, reserved 32 bit + pos += 4; + store32(&pData[pos], (uint32_t) slot.second.size()); // variables count + pos += 4; + for (const auto& var : slot.second) { + pData[pos++] = 1; // type + pData[pos++] = 0; // reserved byte + store16(&pData[pos], 2 + var.first.size() + 2 + var.second.size()); // blob size + pos += 2; + store16(&pData[pos], var.first.size()); // variable name length + pos += 2; + for (int i = 0; i < var.first.size(); ++i) + pData[pos++] = var.first[i]; + store16(&pData[pos], var.second.size()); // variable value length + pos += 2; + for (int i = 0; i < var.second.size(); ++i) + pData[pos++] = var.second[i]; + } + } + } + } else { + // no script variable overridden by this instrument, so get rid + // of 'SCPV' RIFF chunk (if any) + RIFF::Chunk* ckSCPV = lst3LS->GetSubChunk(CHUNK_ID_SCPV); + if (ckSCPV) lst3LS->DeleteSubChunk(ckSCPV); + } } else { // no script slots, so get rid of any LS custom RIFF chunks (if any) RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); @@ -5139,11 +5301,28 @@ } /** + * Returns Region at supplied @a pos position within the region list of + * this instrument. If supplied @a pos is out of bounds then @c NULL is + * returned. + * + * @param pos - position of sought Region in region list + * @returns pointer address to requested region or @c NULL if @a pos is + * out of bounds + */ + Region* Instrument::GetRegionAt(size_t pos) { + if (!pRegions) return NULL; + if (pos >= pRegions->size()) return NULL; + return static_cast( (*pRegions)[pos] ); + } + + /** * Returns the first Region of the instrument. You have to call this * method once before you use GetNextRegion(). * * @returns pointer address to first region or NULL if there is none * @see GetNextRegion() + * @deprecated This method is not reentrant-safe, use GetRegionAt() + * instead. */ Region* Instrument::GetFirstRegion() { if (!pRegions) return NULL; @@ -5158,6 +5337,8 @@ * * @returns pointer address to the next region or NULL if end reached * @see GetFirstRegion() + * @deprecated This method is not reentrant-safe, use GetRegionAt() + * instead. */ Region* Instrument::GetNextRegion() { if (!pRegions) return NULL; @@ -5211,7 +5392,8 @@ * @param dst - destination instrument at which this instrument will be * moved to, or pass NULL for moving to end of list * @throw gig::Exception if this instrument and target instrument are not - * part of the same file + * part of the same file, as well as on unexpected + * internal error */ void Instrument::MoveTo(Instrument* dst) { if (dst && GetParent() != dst->GetParent()) @@ -5228,11 +5410,17 @@ File::InstrumentList::iterator itFrom = std::find(list.begin(), list.end(), static_cast(this)); + if (itFrom == list.end()) + throw Exception( + "gig::Instrument::MoveTo(): unexpected missing membership " + "of this instrument." + ); + list.erase(itFrom); File::InstrumentList::iterator itTo = std::find(list.begin(), list.end(), static_cast(dst)); - list.splice(itTo, list, itFrom); + list.insert(itTo, this); } // move the instrument's actual list RIFF chunk appropriately @@ -5313,7 +5501,7 @@ File* pFile = (File*) GetParent(); for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) { uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset; - for (uint i = 0; pFile->GetScriptGroup(i); ++i) { + for (size_t i = 0; pFile->GetScriptGroup(i); ++i) { ScriptGroup* group = pFile->GetScriptGroup(i); for (uint s = 0; group->GetScript(s); ++s) { Script* script = group->GetScript(s); @@ -5351,7 +5539,7 @@ * @param index - instrument script slot index * @returns script or NULL if index is out of bounds */ - Script* Instrument::GetScriptOfSlot(uint index) { + Script* Instrument::GetScriptOfSlot(size_t index) { LoadScripts(); if (index >= pScriptRefs->size()) return NULL; return pScriptRefs->at(index).script; @@ -5412,7 +5600,7 @@ * @param index1 - index of the first script slot to swap * @param index2 - index of the second script slot to swap */ - void Instrument::SwapScriptSlots(uint index1, uint index2) { + void Instrument::SwapScriptSlots(size_t index1, size_t index2) { LoadScripts(); if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size()) return; @@ -5427,7 +5615,7 @@ * * @param index - index of script slot to remove */ - void Instrument::RemoveScriptSlot(uint index) { + void Instrument::RemoveScriptSlot(size_t index) { LoadScripts(); if (index >= pScriptRefs->size()) return; pScriptRefs->erase( pScriptRefs->begin() + index ); @@ -5468,8 +5656,8 @@ * GigaStudio 4 software. It will currently only work with LinuxSampler and * gigedit. */ - uint Instrument::ScriptSlotCount() const { - return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size()); + size_t Instrument::ScriptSlotCount() const { + return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size(); } /** @brief Whether script execution shall be skipped. @@ -5488,7 +5676,7 @@ * @param index - index of the script slot on this instrument * @see Script::Bypass */ - bool Instrument::IsScriptSlotBypassed(uint index) { + bool Instrument::IsScriptSlotBypassed(size_t index) { if (index >= ScriptSlotCount()) return false; return pScriptRefs ? pScriptRefs->at(index).bypass : scriptPoolFileOffsets.at(index).bypass; @@ -5508,7 +5696,7 @@ * @param bBypass - if true, the script slot will be skipped by the sampler * @see Script::Bypass */ - void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) { + void Instrument::SetScriptSlotBypassed(size_t index, bool bBypass) { if (index >= ScriptSlotCount()) return; if (pScriptRefs) pScriptRefs->at(index).bypass = bBypass; @@ -5516,6 +5704,265 @@ scriptPoolFileOffsets.at(index).bypass = bBypass; } + /// type cast (by copy) uint8_t[16] -> std::array + inline std::array _UUIDFromCArray(const uint8_t* pData) { + std::array uuid; + memcpy(&uuid[0], pData, 16); + return uuid; + } + + /** + * Returns true if this @c Instrument has any script slot which references + * the @c Script identified by passed @p uuid. + */ + bool Instrument::ReferencesScriptWithUuid(const _UUID& uuid) { + const size_t nSlots = ScriptSlotCount(); + for (size_t iSlot = 0; iSlot < nSlots; ++iSlot) + if (_UUIDFromCArray(&GetScriptOfSlot(iSlot)->Uuid[0]) == uuid) + return true; + return false; + } + + /** @brief Checks whether a certain script 'patch' variable value is set. + * + * Returns @c true if the initial value for the requested script variable is + * currently overridden by this instrument. + * + * @remarks Real-time instrument scripts allow to declare special 'patch' + * variables, which essentially behave like regular variables of their data + * type, however their initial value may optionally be overridden on a per + * instrument basis. That allows to share scripts between instruments while + * still being able to fine tune certain aspects of the script for each + * instrument individually. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * Gigedit. + * + * @param slot - script slot index of the variable to be retrieved + * @param variable - name of the 'patch' variable in that script + */ + bool Instrument::IsScriptPatchVariableSet(int slot, String variable) { + if (variable.empty()) return false; + Script* script = GetScriptOfSlot(slot); + if (!script) return false; + const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]); + if (!scriptVars.count(uuid)) return false; + const _VarsBySlot& slots = scriptVars.find(uuid)->second; + if (slots.empty()) return false; + if (slots.count(slot)) + return slots.find(slot)->second.count(variable); + else + return slots.begin()->second.count(variable); + } + + /** @brief Get all overridden script 'patch' variables. + * + * Returns map of key-value pairs reflecting all patch variables currently + * being overridden by this instrument for the given script @p slot, where + * key is the variable name and value is the hereby currently overridden + * value for that variable. + * + * @remarks Real-time instrument scripts allow to declare special 'patch' + * variables, which essentially behave like regular variables of their data + * type, however their initial value may optionally be overridden on a per + * instrument basis. That allows to share scripts between instruments while + * still being able to fine tune certain aspects of the script for each + * instrument individually. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * Gigedit. + * + * @param slot - script slot index of the variable to be retrieved + */ + std::map Instrument::GetScriptPatchVariables(int slot) { + Script* script = GetScriptOfSlot(slot); + if (!script) return std::map(); + const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]); + if (!scriptVars.count(uuid)) return std::map(); + const _VarsBySlot& slots = scriptVars.find(uuid)->second; + if (slots.empty()) return std::map(); + const _PatchVars& vars = + (slots.count(slot)) ? + slots.find(slot)->second : slots.begin()->second; + return vars; + } + + /** @brief Get overridden initial value for 'patch' variable. + * + * Returns current initial value for the requested script variable being + * overridden by this instrument. + * + * @remarks Real-time instrument scripts allow to declare special 'patch' + * variables, which essentially behave like regular variables of their data + * type, however their initial value may optionally be overridden on a per + * instrument basis. That allows to share scripts between instruments while + * still being able to fine tune certain aspects of the script for each + * instrument individually. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * Gigedit. + * + * @param slot - script slot index of the variable to be retrieved + * @param variable - name of the 'patch' variable in that script + */ + String Instrument::GetScriptPatchVariable(int slot, String variable) { + std::map vars = GetScriptPatchVariables(slot); + return (vars.count(variable)) ? vars.find(variable)->second : ""; + } + + /** @brief Override initial value for 'patch' variable. + * + * Overrides initial value for the requested script variable for this + * instrument with the passed value. + * + * @remarks Real-time instrument scripts allow to declare special 'patch' + * variables, which essentially behave like regular variables of their data + * type, however their initial value may optionally be overridden on a per + * instrument basis. That allows to share scripts between instruments while + * still being able to fine tune certain aspects of the script for each + * instrument individually. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * Gigedit. + * + * @param slot - script slot index of the variable to be set + * @param variable - name of the 'patch' variable in that script + * @param value - overridden initial value for that script variable + * @throws gig::Exception if given script @p slot index is invalid or given + * @p variable name is empty + */ + void Instrument::SetScriptPatchVariable(int slot, String variable, String value) { + if (variable.empty()) + throw Exception("Variable name must not be empty"); + Script* script = GetScriptOfSlot(slot); + if (!script) + throw Exception("No script slot with index " + ToString(slot)); + const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]); + scriptVars[uuid][slot][variable] = value; + } + + /** @brief Drop overridden initial value(s) for 'patch' variable(s). + * + * Reverts initial value(s) for requested script variable(s) back to their + * default initial value(s) defined in the script itself. + * + * Both arguments of this method are optional. The most obvious use case of + * this method would be passing a valid script @p slot index and a + * (non-emtpy string as) @p variable name to this method, which would cause + * that single variable to be unset for that specific script slot (on this + * @c Instrument level). + * + * Not passing a value (or @c -1 for @p slot and/or empty string for + * @p variable) means 'wildcard'. So accordingly absence of argument(s) will + * cause all variables and/or for all script slots being unset. Hence this + * method serves 2^2 = 4 possible use cases in total and accordingly covers + * 4 different behaviours in one method. + * + * @remarks Real-time instrument scripts allow to declare special 'patch' + * variables, which essentially behave like regular variables of their data + * type, however their initial value may optionally be overridden on a per + * instrument basis. That allows to share scripts between instruments while + * still being able to fine tune certain aspects of the script for each + * instrument individually. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * Gigedit. + * + * @param slot - script slot index of the variable to be unset + * @param variable - name of the 'patch' variable in that script + */ + void Instrument::UnsetScriptPatchVariable(int slot, String variable) { + Script* script = GetScriptOfSlot(slot); + + // option 1: unset a particular variable of one particular script slot + if (slot != -1 && !variable.empty()) { + if (!script) return; + const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]); + if (!scriptVars.count(uuid)) return; + if (!scriptVars[uuid].count(slot)) return; + if (scriptVars[uuid][slot].count(variable)) + scriptVars[uuid][slot].erase( + scriptVars[uuid][slot].find(variable) + ); + if (scriptVars[uuid][slot].empty()) + scriptVars[uuid].erase( scriptVars[uuid].find(slot) ); + if (scriptVars[uuid].empty()) + scriptVars.erase( scriptVars.find(uuid) ); + return; + } + + // option 2: unset all variables of all script slots + if (slot == -1 && variable.empty()) { + scriptVars.clear(); + return; + } + + // option 3: unset all variables of one particular script slot only + if (slot != -1) { + if (!script) return; + const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]); + if (scriptVars.count(uuid)) + scriptVars.erase( scriptVars.find(uuid) ); + return; + } + + // option 4: unset a particular variable of all script slots + _VarsByScript::iterator itScript = scriptVars.begin(); + _VarsByScript::iterator endScript = scriptVars.end(); + while (itScript != endScript) { + _VarsBySlot& slots = itScript->second; + _VarsBySlot::iterator itSlot = slots.begin(); + _VarsBySlot::iterator endSlot = slots.end(); + while (itSlot != endSlot) { + _PatchVars& vars = itSlot->second; + if (vars.count(variable)) + vars.erase( vars.find(variable) ); + if (vars.empty()) + slots.erase(itSlot++); // postfix increment to avoid iterator invalidation + else + ++itSlot; + } + if (slots.empty()) + scriptVars.erase(itScript++); // postfix increment to avoid iterator invalidation + else + ++itScript; + } + } + + /** + * Returns stripped version of member variable @c scriptVars, where scripts + * no longer referenced by this @c Instrument are filtered out, and so are + * variables of meanwhile obsolete slots (i.e. a script still being + * referenced, but previously overridden on a script slot which either no + * longer exists or is hosting another script now). + */ + Instrument::_VarsByScript Instrument::stripScriptVars() { + _VarsByScript vars; + _VarsByScript::const_iterator itScript = scriptVars.begin(); + _VarsByScript::const_iterator endScript = scriptVars.end(); + for (; itScript != endScript; ++itScript) { + const _UUID& uuid = itScript->first; + if (!ReferencesScriptWithUuid(uuid)) + continue; + const _VarsBySlot& slots = itScript->second; + _VarsBySlot::const_iterator itSlot = slots.begin(); + _VarsBySlot::const_iterator endSlot = slots.end(); + for (; itSlot != endSlot; ++itSlot) { + Script* script = GetScriptOfSlot(itSlot->first); + if (!script) continue; + if (_UUIDFromCArray(&script->Uuid[0]) != uuid) continue; + if (itSlot->second.empty()) continue; + vars[uuid][itSlot->first] = itSlot->second; + } + } + return vars; + } + /** * Make a (semi) deep copy of the Instrument object given by @a orig * and assign it to this object. @@ -5550,7 +5997,12 @@ PianoReleaseMode = orig->PianoReleaseMode; DimensionKeyRange = orig->DimensionKeyRange; scriptPoolFileOffsets = orig->scriptPoolFileOffsets; - pScriptRefs = orig->pScriptRefs; + // deep copy of pScriptRefs required (to avoid undefined behaviour) + if (pScriptRefs) delete pScriptRefs; + pScriptRefs = new std::vector<_ScriptPooolRef>; + if (orig->pScriptRefs) + *pScriptRefs = *orig->pScriptRefs; + scriptVars = orig->scriptVars; // free old midi rules for (int i = 0 ; pMidiRules[i] ; i++) { @@ -5560,7 +6012,7 @@ pMidiRules[0] = NULL; // delete all old regions - while (Regions) DeleteRegion(GetFirstRegion()); + while (Regions) DeleteRegion(GetRegionAt(0)); // create new regions and copy them from original { RegionList::const_iterator it = orig->pRegions->begin(); @@ -5592,6 +6044,7 @@ */ bool Instrument::UsesAnyGigFormatExtension() const { if (!pRegions) return false; + if (!scriptVars.empty()) return true; RegionList::const_iterator iter = pRegions->begin(); RegionList::const_iterator end = pRegions->end(); for (; iter != end; ++iter) { @@ -5615,6 +6068,7 @@ Group::Group(File* file, RIFF::Chunk* ck3gnm) { pFile = file; pNameChunk = ck3gnm; + SamplesIterator = 0; ::LoadString(pNameChunk, Name); } @@ -5659,7 +6113,8 @@ 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()) { + size_t i = 0; + for (RIFF::Chunk* ck = _3gnl->GetSubChunkAt(i); ck; ck = _3gnl->GetSubChunkAt(++i)) { if (strcmp(static_cast(ck->LoadChunkData()), "") == 0) { pNameChunk = ck; break; @@ -5672,6 +6127,26 @@ } /** + * Returns Sample object at @a index of this sample group. + * + * @param index - position of sample in this sample group's sample list + * (0..n) + * @returns sample object or NULL if index is out of bounds + */ + Sample* Group::GetSample(size_t index) { + if (pFile->pSamples && index >= pFile->pSamples->size()) return NULL; + size_t indexInFile = 0; + size_t indexInGroup = 0; + for (Sample* pSample = pFile->GetSample(indexInFile); pSample; + pSample = pFile->GetSample(++indexInFile)) + { + if (pSample->GetGroup() != this) continue; + if (indexInGroup++ == index) return pSample; + } + return NULL; + } + + /** * Returns the first Sample of this Group. You have to call this method * once before you use GetNextSample(). * @@ -5681,11 +6156,17 @@ * @returns pointer address to first Sample or NULL if there is none * applied to this Group * @see GetNextSample() + * @deprecated This method is not reentrant-safe, use GetSample() + * instead. */ Sample* Group::GetFirstSample() { - // FIXME: lazy und unsafe implementation, should be an autonomous iterator - for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) { - if (pSample->GetGroup() == this) return pSample; + size_t& i = this->SamplesIterator; + i = 0; + for (Sample* pSample = pFile->GetSample(i); pSample; + pSample = pFile->GetSample(++i)) + { + if (pSample->GetGroup() == this) + return pSample; } return NULL; } @@ -5699,11 +6180,16 @@ * @returns pointer address to the next Sample of this Group or NULL if * end reached * @see GetFirstSample() + * @deprecated This method is not reentrant-safe, use GetSample() + * instead. */ Sample* Group::GetNextSample() { - // FIXME: lazy und unsafe implementation, should be an autonomous iterator - for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) { - if (pSample->GetGroup() == this) return pSample; + size_t& i = this->SamplesIterator; + for (Sample* pSample = pFile->GetSample(++i); pSample; + pSample = pFile->GetSample(++i)) + { + if (pSample->GetGroup() == this) + return pSample; } return NULL; } @@ -5723,8 +6209,11 @@ */ void Group::MoveAll() { // get "that" other group first + size_t i = 0; Group* pOtherGroup = NULL; - for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) { + for (pOtherGroup = pFile->GetGroup(i); pOtherGroup; + pOtherGroup = pFile->GetGroup(++i)) + { if (pOtherGroup != this) break; } if (!pOtherGroup) throw Exception( @@ -5732,7 +6221,8 @@ "other Group. This is a bug, report it!" ); // now move all samples of this group to the other group - for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { + Sample* pSample; + while ((pSample = GetSample(0))) { pOtherGroup->AddSample(pSample); } } @@ -5804,8 +6294,8 @@ File::~File() { if (pGroups) { - std::list::iterator iter = pGroups->begin(); - std::list::iterator end = pGroups->end(); + std::vector::iterator iter = pGroups->begin(); + std::vector::iterator end = pGroups->end(); while (iter != end) { delete *iter; ++iter; @@ -5813,8 +6303,8 @@ delete pGroups; } if (pScriptGroups) { - std::list::iterator iter = pScriptGroups->begin(); - std::list::iterator end = pScriptGroups->end(); + std::vector::iterator iter = pScriptGroups->begin(); + std::vector::iterator end = pScriptGroups->end(); while (iter != end) { delete *iter; ++iter; @@ -5823,6 +6313,14 @@ } } + /** + * Returns a pointer to the first Sample object of the file, + * NULL otherwise. + * + * @param pProgress - optional: callback function for progress notification + * @deprecated This method is not reentrant-safe, use GetSample() + * instead. + */ Sample* File::GetFirstSample(progress_t* pProgress) { if (!pSamples) LoadSamples(pProgress); if (!pSamples) return NULL; @@ -5830,6 +6328,13 @@ return static_cast( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); } + /** + * Returns a pointer to the next Sample object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetSample() + * instead. + */ Sample* File::GetNextSample() { if (!pSamples) return NULL; SamplesIterator++; @@ -5839,18 +6344,15 @@ /** * Returns Sample object of @a index. * + * @param index - position of sample in sample list (0..n) + * @param pProgress - optional: callback function for progress notification * @returns sample object or NULL if index is out of bounds */ - Sample* File::GetSample(uint index) { - if (!pSamples) LoadSamples(); + Sample* File::GetSample(size_t index, progress_t* pProgress) { + if (!pSamples) LoadSamples(pProgress); if (!pSamples) return NULL; - DLS::File::SampleList::iterator it = pSamples->begin(); - for (int i = 0; i < index; ++i) { - ++it; - if (it == pSamples->end()) return NULL; - } - if (it == pSamples->end()) return NULL; - return static_cast( *it ); + if (index >= pSamples->size()) return NULL; + return static_cast( (*pSamples)[index] ); } /** @@ -5908,13 +6410,15 @@ pSample->DeleteChunks(); delete pSample; - SampleList::iterator tmp = SamplesIterator; // remove all references to the sample - for (Instrument* instrument = GetFirstInstrument() ; instrument ; - instrument = GetNextInstrument()) { - for (Region* region = instrument->GetFirstRegion() ; region ; - region = instrument->GetNextRegion()) { - + size_t iIns = 0; + for (Instrument* instrument = GetInstrument(iIns); instrument; + instrument = GetInstrument(++iIns)) + { + size_t iRgn = 0; + for (Region* region = instrument->GetRegionAt(iRgn); region; + region = instrument->GetRegionAt(++iRgn)) + { if (region->GetSample() == pSample) region->SetSample(NULL); for (int i = 0 ; i < region->DimensionRegions ; i++) { @@ -5923,7 +6427,6 @@ } } } - SamplesIterator = tmp; // restore iterator } void File::LoadSamples() { @@ -5937,8 +6440,6 @@ if (!pSamples) pSamples = new SampleList; - RIFF::File* file = pRIFF; - // just for progress calculation int iSampleIndex = 0; int iTotalSamples = WavePoolCount; @@ -6019,8 +6520,10 @@ if (wvpl) { file_offset_t wvplFileOffset = wvpl->GetFilePos() - wvpl->GetPos(); // should be zero, but just to be sure - RIFF::List* wave = wvpl->GetFirstSubList(); - while (wave) { + size_t i = 0; + for (RIFF::List* wave = wvpl->GetSubListAt(i); wave; + wave = wvpl->GetSubListAt(++i)) + { if (wave->GetListType() == LIST_TYPE_WAVE) { // notify current progress if (pProgress) { @@ -6033,7 +6536,6 @@ iSampleIndex++; } - wave = wvpl->GetNextSubList(); } } } @@ -6042,6 +6544,13 @@ __notify_progress(pProgress, 1.0); // notify done } + /** + * Returns a pointer to the first Instrument object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetInstrument() + * instead. + */ Instrument* File::GetFirstInstrument() { if (!pInstruments) LoadInstruments(); if (!pInstruments) return NULL; @@ -6049,6 +6558,13 @@ return static_cast( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL ); } + /** + * Returns a pointer to the next Instrument object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetInstrument() + * instead. + */ Instrument* File::GetNextInstrument() { if (!pInstruments) return NULL; InstrumentsIterator++; @@ -6076,7 +6592,7 @@ * @param pProgress - optional: callback function for progress notification * @returns sought instrument or NULL if there's no such instrument */ - Instrument* File::GetInstrument(uint index, progress_t* pProgress) { + Instrument* File::GetInstrument(size_t index, progress_t* pProgress) { if (!pInstruments) { // TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM) @@ -6086,7 +6602,7 @@ __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 + GetSample(0, &subprogress); // now force all samples to be loaded __notify_progress(&subprogress, 1.0f); // instrument loading subtask @@ -6100,19 +6616,15 @@ } else { // sample loading subtask if (GetAutoLoad()) - GetFirstSample(); // now force all samples to be loaded + GetSample(0); // now force all samples to be loaded // instrument loading subtask LoadInstruments(); } } if (!pInstruments) return NULL; - InstrumentsIterator = pInstruments->begin(); - for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { - if (i == index) return static_cast( *InstrumentsIterator ); - InstrumentsIterator++; - } - return NULL; + if (index >= pInstruments->size()) return NULL; + return static_cast( (*pInstruments)[index] ); } /** @brief Add a new instrument definition. @@ -6199,7 +6711,7 @@ } // clone script groups and their scripts - for (int iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { + for (size_t iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { ScriptGroup* sg = pFile->GetScriptGroup(iGroup); ScriptGroup* dg = AddScriptGroup(); dg->Name = "COPY" + ToString(iCallCount) + "_" + sg->Name; @@ -6266,8 +6778,10 @@ RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); if (lstInstruments) { int iInstrumentIndex = 0; - RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); - while (lstInstr) { + size_t i = 0; + for (RIFF::List* lstInstr = lstInstruments->GetSubListAt(i); + lstInstr; lstInstr = lstInstruments->GetSubListAt(++i)) + { if (lstInstr->GetListType() == LIST_TYPE_INS) { if (pProgress) { // notify current progress @@ -6285,7 +6799,6 @@ iInstrumentIndex++; } - lstInstr = lstInstruments->GetNextSubList(); } if (pProgress) __notify_progress(pProgress, 1.0); // notify done @@ -6339,7 +6852,7 @@ } int File::GetWaveTableIndexOf(gig::Sample* pSample) { - if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned + if (!pSamples) GetSample(0); // 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) @@ -6359,7 +6872,7 @@ 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 (!pSamples) GetSample(0); // make sure sample chunks were scanned if (_3crc->GetNewSize() != pSamples->size() * 8) return false; const file_offset_t n = _3crc->GetNewSize() / 8; @@ -6393,7 +6906,7 @@ */ bool File::RebuildSampleChecksumTable() { // make sure sample chunks were scanned - if (!pSamples) GetFirstSample(); + if (!pSamples) GetSample(0); bool bRequiresSave = false; @@ -6442,6 +6955,12 @@ return bRequiresSave; } + /** + * Returns a pointer to the first Group object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetGroup() instead. + */ Group* File::GetFirstGroup() { if (!pGroups) LoadGroups(); // there must always be at least one group @@ -6449,6 +6968,12 @@ return *GroupsIterator; } + /** + * Returns a pointer to the next Group object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetGroup() instead. + */ Group* File::GetNextGroup() { if (!pGroups) return NULL; ++GroupsIterator; @@ -6461,14 +6986,10 @@ * @param index - number of the sought group (0..n) * @returns sought group or NULL if there's no such group */ - Group* File::GetGroup(uint index) { + Group* File::GetGroup(size_t index) { if (!pGroups) LoadGroups(); - GroupsIterator = pGroups->begin(); - for (uint i = 0; GroupsIterator != pGroups->end(); i++) { - if (i == index) return *GroupsIterator; - ++GroupsIterator; - } - return NULL; + if (index >= pGroups->size()) return NULL; + return (*pGroups)[index]; } /** @@ -6483,9 +7004,9 @@ */ Group* File::GetGroup(String name) { if (!pGroups) LoadGroups(); - GroupsIterator = pGroups->begin(); - for (uint i = 0; GroupsIterator != pGroups->end(); ++GroupsIterator, ++i) - if ((*GroupsIterator)->Name == name) return *GroupsIterator; + size_t i = 0; + for (Group* pGroup = GetGroup(i); pGroup; pGroup = GetGroup(++i)) + if (pGroup->Name == name) return pGroup; return NULL; } @@ -6509,11 +7030,13 @@ */ void File::DeleteGroup(Group* pGroup) { if (!pGroups) LoadGroups(); - std::list::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); + std::vector::iterator iter = + find(pGroups->begin(), pGroups->end(), pGroup); if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); // delete all members of this group - for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) { + Sample* pSample; + while ((pSample = pGroup->GetSample(0))) { DeleteSample(pSample); } // now delete this group object @@ -6534,7 +7057,8 @@ */ void File::DeleteGroupOnly(Group* pGroup) { if (!pGroups) LoadGroups(); - std::list::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); + std::vector::iterator iter = + find(pGroups->begin(), pGroups->end(), pGroup); if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); // move all members of this group to another group @@ -6545,21 +7069,22 @@ } void File::LoadGroups() { - if (!pGroups) pGroups = new std::list; + if (!pGroups) pGroups = new std::vector; // try to read defined groups from file RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); if (lst3gri) { RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); if (lst3gnl) { - RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); - while (ck) { + size_t i = 0; + for (RIFF::Chunk* ck = lst3gnl->GetSubChunkAt(i); ck; + ck = lst3gnl->GetSubChunkAt(++i)) + { if (ck->GetChunkID() == CHUNK_ID_3GNM) { if (pVersion && pVersion->major > 2 && strcmp(static_cast(ck->LoadChunkData()), "") == 0) break; pGroups->push_back(new Group(this, ck)); } - ck = lst3gnl->GetNextSubChunk(); } } } @@ -6578,12 +7103,10 @@ * @param index - number of the sought group (0..n) * @returns sought script group or NULL if there's no such group */ - ScriptGroup* File::GetScriptGroup(uint index) { + ScriptGroup* File::GetScriptGroup(size_t index) { if (!pScriptGroups) LoadScriptGroups(); - std::list::iterator it = pScriptGroups->begin(); - for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) - if (i == index) return *it; - return NULL; + if (index >= pScriptGroups->size()) return NULL; + return (*pScriptGroups)[index]; } /** @brief Get instrument script group (by name). @@ -6596,9 +7119,10 @@ */ ScriptGroup* File::GetScriptGroup(const String& name) { if (!pScriptGroups) LoadScriptGroups(); - std::list::iterator it = pScriptGroups->begin(); - for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) - if ((*it)->Name == name) return *it; + for (size_t i = 0; i < pScriptGroups->size(); ++i) { + ScriptGroup* pGroup = (*pScriptGroups)[i]; + if (pGroup->Name == name) return pGroup; + } return NULL; } @@ -6631,7 +7155,7 @@ */ void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) { if (!pScriptGroups) LoadScriptGroups(); - std::list::iterator iter = + std::vector::iterator iter = find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup); if (iter == pScriptGroups->end()) throw gig::Exception("Could not delete script group, could not find given script group"); @@ -6646,11 +7170,12 @@ void File::LoadScriptGroups() { if (pScriptGroups) return; - pScriptGroups = new std::list; + pScriptGroups = new std::vector; RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS); if (lstLS) { - for (RIFF::List* lst = lstLS->GetFirstSubList(); lst; - lst = lstLS->GetNextSubList()) + size_t i = 0; + for (RIFF::List* lst = lstLS->GetSubListAt(i); lst; + lst = lstLS->GetSubListAt(++i)) { if (lst->GetListType() == LIST_TYPE_RTIS) { pScriptGroups->push_back(new ScriptGroup(this, lst)); @@ -6690,7 +7215,7 @@ // of the respective instrument script chunk as reference. if (pScriptGroups) { // Update instrument script (group) chunks. - for (std::list::iterator it = pScriptGroups->begin(); + for (std::vector::iterator it = pScriptGroups->begin(); it != pScriptGroups->end(); ++it) { (*it)->UpdateChunks(pProgress); @@ -6711,7 +7236,7 @@ // INFO was added by Resource::UpdateChunks - make sure it // is placed first in file RIFF::Chunk* info = pRIFF->GetSubList(LIST_TYPE_INFO); - RIFF::Chunk* first = pRIFF->GetFirstSubChunk(); + RIFF::Chunk* first = pRIFF->GetSubChunkAt(0); if (first != info) { pRIFF->MoveSubChunk(info, first); } @@ -6732,17 +7257,18 @@ // v3: make sure the file has 128 3gnm chunks // (before updating the Group chunks) if (pVersion && pVersion->major > 2) { - RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk(); - for (int i = 0 ; i < 128 ; i++) { + size_t i = 0; + for (RIFF::Chunk* _3gnm = _3gnl->GetSubChunkAt(i); i < 128; + _3gnm = _3gnl->GetSubChunkAt(++i)) + { // 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(); } } - std::list::iterator iter = pGroups->begin(); - std::list::iterator end = pGroups->end(); + std::vector::iterator iter = pGroups->begin(); + std::vector::iterator end = pGroups->end(); for (; iter != end; ++iter) { (*iter)->UpdateChunks(pProgress); } @@ -6780,9 +7306,11 @@ uint8_t* pData = (uint8_t*) einf->LoadChunkData(); std::map sampleMap; - int sampleIdx = 0; - for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { - sampleMap[pSample] = sampleIdx++; + size_t sampleIdx = 0; + for (Sample* pSample = GetSample(0); pSample; + pSample = GetSample(++sampleIdx)) + { + sampleMap[pSample] = sampleIdx; } int totnbusedsamples = 0; @@ -6794,8 +7322,10 @@ memset(&pData[48], 0, sublen - 48); - for (Instrument* instrument = GetFirstInstrument() ; instrument ; - instrument = GetNextInstrument()) { + size_t iIns = 0; + for (Instrument* instrument = GetInstrument(iIns); instrument; + instrument = GetInstrument(++iIns)) + { int nbusedsamples = 0; int nbusedchannels = 0; int nbdimregions = 0; @@ -6803,8 +7333,10 @@ memset(&pData[(instrumentIdx + 1) * sublen + 48], 0, sublen - 48); - for (Region* region = instrument->GetFirstRegion() ; region ; - region = instrument->GetNextRegion()) { + size_t iRgn = 0; + for (Region* region = instrument->GetRegionAt(iRgn); region; + region = instrument->GetRegionAt(++iRgn)) + { for (int i = 0 ; i < region->DimensionRegions ; i++) { gig::DimensionRegion *d = region->pDimensionRegions[i]; if (d->pSample) { @@ -6898,8 +7430,9 @@ void File::UpdateFileOffsets() { DLS::File::UpdateFileOffsets(); - for (Instrument* instrument = GetFirstInstrument(); instrument; - instrument = GetNextInstrument()) + size_t i = 0; + for (Instrument* instrument = GetInstrument(i); instrument; + instrument = GetInstrument(++i)) { instrument->UpdateScriptFileOffsets(); }