--- libgig/trunk/src/DLS.cpp 2011/09/19 21:41:34 2274 +++ libgig/trunk/src/DLS.cpp 2020/01/22 15:48:32 3723 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2009 by Christian Schoenebeck * + * Copyright (C) 2003-2020 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -24,6 +24,7 @@ #include "DLS.h" #include +#include #include #ifdef __APPLE__ @@ -121,6 +122,9 @@ artl->GetChunkID() != CHUNK_ID_ARTL) { throw DLS::Exception(" or chunk expected"); } + + artl->SetPos(0); + HeaderSize = artl->ReadUint32(); Connections = artl->ReadUint32(); artl->SetPos(HeaderSize); @@ -144,8 +148,10 @@ /** * Apply articulation connections to the respective RIFF chunks. You * have to call File::Save() to make changes persistent. + * + * @param pProgress - callback function for progress notification */ - void Articulation::UpdateChunks() { + void Articulation::UpdateChunks(progress_t* pProgress) { const int iEntrySize = 12; // 12 bytes per connection block pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); @@ -161,6 +167,18 @@ } } + /** @brief Remove all RIFF chunks associated with this Articulation object. + * + * At the moment Articulation::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 Storage::DeleteChunks() for details. + */ + void Articulation::DeleteChunks() { + } + // *************** Articulator *************** @@ -217,17 +235,42 @@ /** * Apply all articulations to the respective RIFF chunks. You have to * call File::Save() to make changes persistent. + * + * @param pProgress - callback function for progress notification + */ + void Articulator::UpdateChunks(progress_t* pProgress) { + if (pArticulations) { + ArticulationList::iterator iter = pArticulations->begin(); + ArticulationList::iterator end = pArticulations->end(); + for (; iter != end; ++iter) { + (*iter)->UpdateChunks(pProgress); + } + } + } + + /** @brief Remove all RIFF chunks associated with this Articulator object. + * + * See Storage::DeleteChunks() for details. */ - void Articulator::UpdateChunks() { + void Articulator::DeleteChunks() { if (pArticulations) { ArticulationList::iterator iter = pArticulations->begin(); ArticulationList::iterator end = pArticulations->end(); for (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + (*iter)->DeleteChunks(); } } } + /** + * Not yet implemented in this version, since the .gig format does + * not need to copy DLS articulators and so far nobody used pure + * DLS instrument AFAIK. + */ + void Articulator::CopyAssign(const Articulator* orig) { + //TODO: implement deep copy assignment for this class + } + // *************** Info *************** @@ -327,8 +370,10 @@ * * Apply current INFO field values to the respective INFO chunks. You * have to call File::Save() to make changes persistent. + * + * @param pProgress - callback function for progress notification */ - void Info::UpdateChunks() { + void Info::UpdateChunks(progress_t* pProgress) { if (!pResourceListChunk) return; // make sure INFO list chunk exists @@ -384,6 +429,46 @@ SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); } + /** @brief Remove all RIFF chunks associated with this Info object. + * + * At the moment Info::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 Storage::DeleteChunks() for details. + */ + void Info::DeleteChunks() { + } + + /** + * Make a deep copy of the Info object given by @a orig and assign it to + * this object. + * + * @param orig - original Info object to be copied from + */ + void Info::CopyAssign(const Info* orig) { + Name = orig->Name; + ArchivalLocation = orig->ArchivalLocation; + CreationDate = orig->CreationDate; + Comments = orig->Comments; + Product = orig->Product; + Copyright = orig->Copyright; + Artists = orig->Artists; + Genre = orig->Genre; + Keywords = orig->Keywords; + Engineer = orig->Engineer; + Technician = orig->Technician; + Software = orig->Software; + Medium = orig->Medium; + Source = orig->Source; + SourceForm = orig->SourceForm; + Commissioned = orig->Commissioned; + Subject = orig->Subject; + //FIXME: hmm, is copying this pointer a good idea? + pFixedStringLengths = orig->pFixedStringLengths; + } + // *************** Resource *************** @@ -406,6 +491,8 @@ RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID); if (ckDLSID) { + ckDLSID->SetPos(0); + pDLSID = new dlsid_t; ckDLSID->Read(&pDLSID->ulData1, 1, 4); ckDLSID->Read(&pDLSID->usData2, 1, 2); @@ -420,6 +507,18 @@ if (pInfo) delete pInfo; } + /** @brief Remove all RIFF chunks associated with this Resource object. + * + * At the moment Resource::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 Storage::DeleteChunks() for details. + */ + void Resource::DeleteChunks() { + } + /** @brief Update chunks with current Resource data. * * Apply Resource data persistently below the previously given resource @@ -427,9 +526,11 @@ * will not be applied at the moment (yet). * * You have to call File::Save() to make changes persistent. + * + * @param pProgress - callback function for progress notification */ - void Resource::UpdateChunks() { - pInfo->UpdateChunks(); + void Resource::UpdateChunks(progress_t* pProgress) { + pInfo->UpdateChunks(pProgress); if (pDLSID) { // make sure 'dlid' chunk exists @@ -448,12 +549,14 @@ * Generates a new DLSID for the resource. */ void Resource::GenerateDLSID() { -#if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE) - + #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE) if (!pDLSID) pDLSID = new dlsid_t; + GenerateDLSID(pDLSID); + #endif + } + void Resource::GenerateDLSID(dlsid_t* pDLSID) { #ifdef WIN32 - UUID uuid; UuidCreate(&uuid); pDLSID->ulData1 = uuid.Data1; @@ -477,16 +580,27 @@ pDLSID->abData[5] = uuid.byte13; pDLSID->abData[6] = uuid.byte14; pDLSID->abData[7] = uuid.byte15; -#else +#elif defined(HAVE_UUID_GENERATE) uuid_t uuid; uuid_generate(uuid); pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24; pDLSID->usData2 = uuid[4] | uuid[5] << 8; pDLSID->usData3 = uuid[6] | uuid[7] << 8; memcpy(pDLSID->abData, &uuid[8], 8); -#endif +#else +# error "Missing support for uuid generation" #endif } + + /** + * Make a deep copy of the Resource object given by @a orig and assign it + * to this object. + * + * @param orig - original Resource object to be copied from + */ + void Resource::CopyAssign(const Resource* orig) { + pInfo->CopyAssign(orig->pInfo); + } // *************** Sampler *************** @@ -496,6 +610,8 @@ pParentList = ParentList; RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP); if (wsmp) { + wsmp->SetPos(0); + uiHeaderSize = wsmp->ReadUint32(); UnityNote = wsmp->ReadUint16(); FineTune = wsmp->ReadInt16(); @@ -535,8 +651,10 @@ /** * Apply all sample player options to the respective RIFF chunk. You * have to call File::Save() to make changes persistent. + * + * @param pProgress - callback function for progress notification */ - void Sampler::UpdateChunks() { + void Sampler::UpdateChunks(progress_t* pProgress) { // make sure 'wsmp' chunk exists RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); int wsmpSize = uiHeaderSize + SampleLoops * 16; @@ -568,6 +686,18 @@ } } + /** @brief Remove all RIFF chunks associated with this Sampler object. + * + * At the moment Sampler::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 Storage::DeleteChunks() for details. + */ + void Sampler::DeleteChunks() { + } + /** * Adds a new sample loop with the provided loop definition. * @@ -600,8 +730,10 @@ // copy old loops array (skipping given loop) for (int i = 0, o = 0; i < SampleLoops; i++) { if (&pSampleLoops[i] == pLoopDef) continue; - if (o == SampleLoops - 1) + if (o == SampleLoops - 1) { + delete[] pNewLoops; throw Exception("Could not delete Sample Loop, because it does not exist"); + } pNewLoops[o] = pSampleLoops[i]; o++; } @@ -610,7 +742,28 @@ pSampleLoops = pNewLoops; SampleLoops--; } - + + /** + * Make a deep copy of the Sampler object given by @a orig and assign it + * to this object. + * + * @param orig - original Sampler object to be copied from + */ + void Sampler::CopyAssign(const Sampler* orig) { + // copy trivial scalars + UnityNote = orig->UnityNote; + FineTune = orig->FineTune; + Gain = orig->Gain; + NoSampleDepthTruncation = orig->NoSampleDepthTruncation; + NoSampleCompression = orig->NoSampleCompression; + SamplerOptions = orig->SamplerOptions; + + // copy sample loops + if (SampleLoops) delete[] pSampleLoops; + pSampleLoops = new sample_loop_t[orig->SampleLoops]; + memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t)); + SampleLoops = orig->SampleLoops; + } // *************** Sample *************** @@ -631,12 +784,14 @@ * @param WavePoolOffset - offset of this sample data from wave pool * ('wvpl') list chunk */ - Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : Resource(pFile, waveList) { + Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset) : Resource(pFile, waveList) { pWaveList = waveList; - ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE; + ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize()); pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT); pCkData = waveList->GetSubChunk(CHUNK_ID_DATA); if (pCkFormat) { + pCkFormat->SetPos(0); + // common fields FormatTag = pCkFormat->ReadUint16(); Channels = pCkFormat->ReadUint16(); @@ -667,12 +822,80 @@ /** @brief Destructor. * - * Removes RIFF chunks associated with this Sample and frees all - * memory occupied by this sample. + * Frees all memory occupied by this sample. */ Sample::~Sample() { - RIFF::List* pParent = pWaveList->GetParent(); - pParent->DeleteSubChunk(pWaveList); + if (pCkData) + pCkData->ReleaseChunkData(); + if (pCkFormat) + pCkFormat->ReleaseChunkData(); + } + + /** @brief Remove all RIFF chunks associated with this Sample object. + * + * See Storage::DeleteChunks() for details. + */ + void Sample::DeleteChunks() { + // handle base class + Resource::DeleteChunks(); + + // handle own RIFF chunks + if (pWaveList) { + RIFF::List* pParent = pWaveList->GetParent(); + pParent->DeleteSubChunk(pWaveList); + pWaveList = NULL; + } + } + + /** + * Make a deep copy of the Sample object given by @a orig (without the + * actual sample waveform data however) and assign it to this object. + * + * This is a special internal variant of CopyAssign() which only copies the + * most mandatory member variables. It will be called by gig::Sample + * descendent instead of CopyAssign() since gig::Sample has its own + * implementation to access and copy the actual sample waveform data. + * + * @param orig - original Sample object to be copied from + */ + void Sample::CopyAssignCore(const Sample* orig) { + // handle base classes + Resource::CopyAssign(orig); + // handle actual own attributes of this class + FormatTag = orig->FormatTag; + Channels = orig->Channels; + SamplesPerSecond = orig->SamplesPerSecond; + AverageBytesPerSecond = orig->AverageBytesPerSecond; + BlockAlign = orig->BlockAlign; + BitDepth = orig->BitDepth; + SamplesTotal = orig->SamplesTotal; + FrameSize = orig->FrameSize; + } + + /** + * Make a deep copy of the Sample object given by @a orig and assign it to + * this object. + * + * @param orig - original Sample object to be copied from + */ + void Sample::CopyAssign(const Sample* orig) { + CopyAssignCore(orig); + + // copy sample waveform data (reading directly from disc) + Resize(orig->GetSize()); + char* buf = (char*) LoadSampleData(); + Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now + const file_offset_t restorePos = pOrig->pCkData->GetPos(); + pOrig->SetPos(0); + for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) { + const int iReadAtOnce = 64*1024; + file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo; + n = pOrig->Read(&buf[i], n); + if (!n) break; + todo -= n; + i += (n * pOrig->FrameSize); + } + pOrig->pCkData->SetPos(restorePos); } /** @brief Load sample data into RAM. @@ -724,7 +947,7 @@ * @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM * @see FrameSize, FormatTag */ - unsigned long Sample::GetSize() { + file_offset_t Sample::GetSize() const { if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; return (pCkData) ? pCkData->GetSize() / FrameSize : 0; } @@ -751,19 +974,21 @@ * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with * other formats will fail! * - * @param iNewSize - new sample wave data size in sample points (must be - * greater than zero) - * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM - * @throws Exception if \a iNewSize is less than 1 + * @param NewSize - new sample wave data size in sample points (must be + * greater than zero) + * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM + * @throws Exception if \a NewSize is less than 1 or unrealistic large * @see File::Save(), FrameSize, FormatTag */ - void Sample::Resize(int iNewSize) { + void Sample::Resize(file_offset_t NewSize) { if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); - if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); - const int iSizeInBytes = iNewSize * FrameSize; + if (NewSize < 1) throw Exception("Sample size must be at least one sample point"); + if ((NewSize >> 48) != 0) + throw Exception("Unrealistic high DLS sample size detected"); + const file_offset_t sizeInBytes = NewSize * FrameSize; pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); - if (pCkData) pCkData->Resize(iSizeInBytes); - else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes); + if (pCkData) pCkData->Resize(sizeInBytes); + else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes); } /** @@ -782,11 +1007,11 @@ * @throws Exception if no data RIFF chunk was created for the sample yet * @see FrameSize, FormatTag */ - unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { + file_offset_t Sample::SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence) { if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); - unsigned long orderedBytes = SampleCount * FrameSize; - unsigned long result = pCkData->SetPos(orderedBytes, Whence); + file_offset_t orderedBytes = SampleCount * FrameSize; + file_offset_t result = pCkData->SetPos(orderedBytes, Whence); return (result == orderedBytes) ? SampleCount : result / FrameSize; } @@ -800,7 +1025,7 @@ * @param pBuffer destination buffer * @param SampleCount number of sample points to read */ - unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { + file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) { if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? } @@ -820,7 +1045,7 @@ * @throws Exception if current sample size is too small * @see LoadSampleData() */ - unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { + file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) { if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? @@ -830,17 +1055,18 @@ * Apply sample and its settings to the respective RIFF chunks. You have * to call File::Save() to make changes persistent. * + * @param pProgress - callback function for progress notification * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data * was provided yet */ - void Sample::UpdateChunks() { + void Sample::UpdateChunks(progress_t* pProgress) { if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Could not save sample, only PCM format is supported"); // we refuse to do anything if not sample wave form was provided yet if (!pCkData) throw Exception("Could not save sample, there is no sample data to save"); // update chunks of base class as well - Resource::UpdateChunks(); + Resource::UpdateChunks(pProgress); // make sure 'fmt' chunk exists RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT); if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format @@ -862,9 +1088,11 @@ Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) { pCkRegion = rgnList; - // articulation informations + // articulation information RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH); if (rgnh) { + rgnh->SetPos(0); + rgnh->Read(&KeyRange, 2, 2); rgnh->Read(&VelocityRange, 2, 2); FormatOptionFlags = rgnh->ReadUint16(); @@ -884,9 +1112,11 @@ } SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE; - // sample informations + // sample information RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK); if (wlnk) { + wlnk->SetPos(0); + WaveLinkOptionFlags = wlnk->ReadUint16(); PhaseGroup = wlnk->ReadUint16(); Channel = wlnk->ReadUint32(); @@ -905,20 +1135,37 @@ /** @brief Destructor. * - * Removes RIFF chunks associated with this Region. + * Intended to free up all memory occupied by this Region object. ATM this + * destructor implementation does nothing though. */ Region::~Region() { - RIFF::List* pParent = pCkRegion->GetParent(); - pParent->DeleteSubChunk(pCkRegion); + } + + /** @brief Remove all RIFF chunks associated with this Region object. + * + * See Storage::DeleteChunks() for details. + */ + void Region::DeleteChunks() { + // handle base classes + Resource::DeleteChunks(); + Articulator::DeleteChunks(); + Sampler::DeleteChunks(); + + // handle own RIFF chunks + if (pCkRegion) { + RIFF::List* pParent = pCkRegion->GetParent(); + pParent->DeleteSubChunk(pCkRegion); + pCkRegion = NULL; + } } Sample* Region::GetSample() { if (pSample) return pSample; File* file = (File*) GetParent()->GetParent(); - unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; + uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; Sample* sample = file->GetFirstSample(); while (sample) { - if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample); + if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample); sample = file->GetNextSample(); } return NULL; @@ -973,9 +1220,10 @@ * Apply Region settings to the respective RIFF chunks. You have to * call File::Save() to make changes persistent. * + * @param pProgress - callback function for progress notification * @throws Exception - if the Region's sample could not be found */ - void Region::UpdateChunks() { + void Region::UpdateChunks(progress_t* pProgress) { // make sure 'rgnh' chunk exists RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); @@ -994,8 +1242,8 @@ // update chunks of base classes as well (but skip Resource, // as a rgn doesn't seem to have dlid and INFO chunks) - Articulator::UpdateChunks(); - Sampler::UpdateChunks(); + Articulator::UpdateChunks(pProgress); + Sampler::UpdateChunks(pProgress); // make sure 'wlnk' chunk exists RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK); @@ -1027,7 +1275,45 @@ store32(&pData[4], Channel); store32(&pData[8], WavePoolTableIndex); } - + + /** + * Make a (semi) deep copy of the Region object given by @a orig and assign + * it to this object. + * + * Note that the sample pointer referenced by @a orig is simply copied as + * memory address. Thus the respective sample is shared, not duplicated! + * + * @param orig - original Region object to be copied from + */ + void Region::CopyAssign(const Region* orig) { + // handle base classes + Resource::CopyAssign(orig); + Articulator::CopyAssign(orig); + Sampler::CopyAssign(orig); + // handle actual own attributes of this class + // (the trivial ones) + VelocityRange = orig->VelocityRange; + KeyGroup = orig->KeyGroup; + Layer = orig->Layer; + SelfNonExclusive = orig->SelfNonExclusive; + PhaseMaster = orig->PhaseMaster; + PhaseGroup = orig->PhaseGroup; + MultiChannel = orig->MultiChannel; + Channel = orig->Channel; + // only take the raw sample reference if the two Region objects are + // part of the same file + if (GetParent()->GetParent() == orig->GetParent()->GetParent()) { + WavePoolTableIndex = orig->WavePoolTableIndex; + pSample = orig->pSample; + } else { + WavePoolTableIndex = -1; + pSample = NULL; + } + FormatOptionFlags = orig->FormatOptionFlags; + WaveLinkOptionFlags = orig->WaveLinkOptionFlags; + // handle the last, a bit sensible attribute + SetKeyRange(orig->KeyRange.low, orig->KeyRange.high); + } // *************** Instrument *************** @@ -1052,6 +1338,8 @@ midi_locale_t locale; RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); if (insh) { + insh->SetPos(0); + Regions = insh->ReadUint32(); insh->Read(&locale, 2, 4); } else { // 'insh' chunk missing @@ -1104,13 +1392,13 @@ 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(); return pNewRegion; } void Instrument::MoveRegion(Region* pSrc, Region* pDst) { RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); - lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); + lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0)); pRegions->remove(pSrc); RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); @@ -1122,7 +1410,8 @@ RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); if (iter == pRegions->end()) return; pRegions->erase(iter); - Regions = pRegions->size(); + Regions = (uint32_t) pRegions->size(); + pRegion->DeleteChunks(); delete pRegion; } @@ -1130,18 +1419,19 @@ * Apply Instrument with all its Regions to the respective RIFF chunks. * You have to call File::Save() to make changes persistent. * + * @param pProgress - callback function for progress notification * @throws Exception - on errors */ - void Instrument::UpdateChunks() { + void Instrument::UpdateChunks(progress_t* pProgress) { // first update base classes' chunks - Resource::UpdateChunks(); - Articulator::UpdateChunks(); + Resource::UpdateChunks(pProgress); + Articulator::UpdateChunks(pProgress); // make sure 'insh' chunk exists RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); uint8_t* pData = (uint8_t*) insh->LoadChunkData(); // update 'insh' chunk - Regions = (pRegions) ? pRegions->size() : 0; + Regions = (pRegions) ? uint32_t(pRegions->size()) : 0; midi_locale_t locale; locale.instrument = MIDIProgram; locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); @@ -1154,15 +1444,23 @@ if (!pRegions) return; RegionList::iterator iter = pRegions->begin(); RegionList::iterator end = pRegions->end(); - for (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + for (int i = 0; iter != end; ++iter, ++i) { + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, pRegions->size(), i); + // do the actual work + (*iter)->UpdateChunks(&subprogress); + } else + (*iter)->UpdateChunks(NULL); } + if (pProgress) + __notify_progress(pProgress, 1.0); // notify done } /** @brief Destructor. * - * Removes RIFF chunks associated with this Instrument and frees all - * memory occupied by this instrument. + * Frees all memory occupied by this instrument. */ Instrument::~Instrument() { if (pRegions) { @@ -1174,11 +1472,69 @@ } delete pRegions; } - // remove instrument's chunks - RIFF::List* pParent = pCkInstrument->GetParent(); - pParent->DeleteSubChunk(pCkInstrument); } + /** @brief Remove all RIFF chunks associated with this Instrument object. + * + * See Storage::DeleteChunks() for details. + */ + void Instrument::DeleteChunks() { + // handle base classes + Resource::DeleteChunks(); + Articulator::DeleteChunks(); + + // handle RIFF chunks of members + if (pRegions) { + RegionList::iterator it = pRegions->begin(); + RegionList::iterator end = pRegions->end(); + for (; it != end; ++it) + (*it)->DeleteChunks(); + } + + // handle own RIFF chunks + if (pCkInstrument) { + RIFF::List* pParent = pCkInstrument->GetParent(); + pParent->DeleteSubChunk(pCkInstrument); + pCkInstrument = NULL; + } + } + + void Instrument::CopyAssignCore(const Instrument* orig) { + // handle base classes + Resource::CopyAssign(orig); + Articulator::CopyAssign(orig); + // handle actual own attributes of this class + // (the trivial ones) + IsDrum = orig->IsDrum; + MIDIBank = orig->MIDIBank; + MIDIBankCoarse = orig->MIDIBankCoarse; + MIDIBankFine = orig->MIDIBankFine; + MIDIProgram = orig->MIDIProgram; + } + + /** + * Make a (semi) deep copy of the Instrument object given by @a orig and assign + * it to this object. + * + * Note that all sample pointers referenced by @a orig are simply copied as + * memory address. Thus the respective samples are shared, not duplicated! + * + * @param orig - original Instrument object to be copied from + */ + void Instrument::CopyAssign(const Instrument* orig) { + CopyAssignCore(orig); + // delete all regions first + while (Regions) DeleteRegion(GetFirstRegion()); + // now recreate and copy regions + { + RegionList::const_iterator it = orig->pRegions->begin(); + for (int i = 0; i < orig->Regions; ++i, ++it) { + Region* dstRgn = AddRegion(); + //NOTE: Region does semi-deep copy ! + dstRgn->CopyAssign(*it); + } + } + } // *************** File *************** @@ -1192,6 +1548,7 @@ */ File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { pRIFF->SetByteOrder(RIFF::endian_little); + bOwningRiff = true; pVersion = new version_t; pVersion->major = 0; pVersion->minor = 0; @@ -1222,9 +1579,11 @@ File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) { if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object."); this->pRIFF = pRIFF; - + bOwningRiff = false; RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); if (ckVersion) { + ckVersion->SetPos(0); + pVersion = new version_t; ckVersion->Read(pVersion, 4, 2); } @@ -1232,6 +1591,7 @@ RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found."); + colh->SetPos(0); Instruments = colh->ReadUint32(); RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); @@ -1242,6 +1602,8 @@ WavePoolHeaderSize = 8; b64BitWavePoolOffsets = false; } else { + ptbl->SetPos(0); + WavePoolHeaderSize = ptbl->ReadUint32(); WavePoolCount = ptbl->ReadUint32(); pWavePoolTable = new uint32_t[WavePoolCount]; @@ -1254,8 +1616,9 @@ for (int i = 0 ; i < WavePoolCount ; i++) { pWavePoolTableHi[i] = ptbl->ReadUint32(); pWavePoolTable[i] = ptbl->ReadUint32(); - if (pWavePoolTable[i] & 0x80000000) - throw DLS::Exception("Files larger than 2 GB not yet supported"); + //NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12) + //if (pWavePoolTable[i] & 0x80000000) + // throw DLS::Exception("Files larger than 2 GB not yet supported"); } } else { // conventional 32 bit offsets ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); @@ -1293,6 +1656,8 @@ if (pVersion) delete pVersion; for (std::list::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) delete *i; + if (bOwningRiff) + delete pRIFF; } Sample* File::GetFirstSample() { @@ -1312,11 +1677,13 @@ if (!pSamples) pSamples = new SampleList; RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); if (wvpl) { - unsigned long 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) { - unsigned long waveFileOffset = wave->GetFilePos(); + file_offset_t waveFileOffset = wave->GetFilePos() - + wave->GetPos(); // should be zero, but just to be sure pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); } wave = wvpl->GetNextSubList(); @@ -1325,11 +1692,13 @@ else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant) RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL); if (dwpl) { - unsigned long dwplFileOffset = dwpl->GetFilePos(); + file_offset_t dwplFileOffset = dwpl->GetFilePos() - + dwpl->GetPos(); // should be zero, but just to be sure RIFF::List* wave = dwpl->GetFirstSubList(); while (wave) { if (wave->GetListType() == LIST_TYPE_WAVE) { - unsigned long waveFileOffset = wave->GetFilePos(); + file_offset_t waveFileOffset = wave->GetFilePos() - + wave->GetPos(); // should be zero, but just to be sure pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); } wave = dwpl->GetNextSubList(); @@ -1368,6 +1737,7 @@ SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); if (iter == pSamples->end()) return; pSamples->erase(iter); + pSample->DeleteChunks(); delete pSample; } @@ -1427,30 +1797,70 @@ InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); if (iter == pInstruments->end()) return; pInstruments->erase(iter); + pInstrument->DeleteChunks(); delete pInstrument; } + /** + * Returns the underlying RIFF::File used for persistency of this DLS::File + * object. + */ + RIFF::File* File::GetRiffFile() { + return pRIFF; + } + + /** + * Returns extension file of given index. Extension files are used + * sometimes to circumvent the 2 GB file size limit of the RIFF format and + * of certain operating systems in general. In this case, instead of just + * using one file, the content is spread among several files with similar + * file name scheme. This is especially used by some GigaStudio sound + * libraries. + * + * @param index - index of extension file + * @returns sought extension file, NULL if index out of bounds + * @see GetFileName() + */ + RIFF::File* File::GetExtensionFile(int index) { + if (index < 0 || index >= ExtensionFiles.size()) return NULL; + std::list::iterator iter = ExtensionFiles.begin(); + for (int i = 0; iter != ExtensionFiles.end(); ++iter, ++i) + if (i == index) return *iter; + return NULL; + } + /** @brief File name of this DLS file. * * This method returns the file name as it was provided when loading * the respective DLS file. However in case the File object associates * an empty, that is new DLS file, which was not yet saved to disk, * this method will return an empty string. + * + * @see GetExtensionFile() */ String File::GetFileName() { return pRIFF->GetFileName(); } + + /** + * You may call this method store a future file name, so you don't have to + * to pass it to the Save() call later on. + */ + void File::SetFileName(const String& name) { + pRIFF->SetFileName(name); + } /** * Apply all the DLS file's current instruments, samples and settings to * the respective RIFF chunks. You have to call Save() to make changes * persistent. * + * @param pProgress - callback function for progress notification * @throws Exception - on errors */ - void File::UpdateChunks() { + void File::UpdateChunks(progress_t* pProgress) { // first update base class's chunks - Resource::UpdateChunks(); + Resource::UpdateChunks(pProgress); // if version struct exists, update 'vers' chunk if (pVersion) { @@ -1464,7 +1874,7 @@ } // update 'colh' chunk - Instruments = (pInstruments) ? pInstruments->size() : 0; + Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0; RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); uint8_t* pData = (uint8_t*) colh->LoadChunkData(); @@ -1472,19 +1882,38 @@ // update instrument's chunks if (pInstruments) { - InstrumentList::iterator iter = pInstruments->begin(); - InstrumentList::iterator end = pInstruments->end(); - for (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress + + // do the actual work + InstrumentList::iterator iter = pInstruments->begin(); + InstrumentList::iterator end = pInstruments->end(); + for (int i = 0; iter != end; ++iter, ++i) { + // divide subprogress into sub-subprogress + progress_t subsubprogress; + __divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i); + // do the actual work + (*iter)->UpdateChunks(&subsubprogress); + } + + __notify_progress(&subprogress, 1.0); // notify subprogress done + } else { + InstrumentList::iterator iter = pInstruments->begin(); + InstrumentList::iterator end = pInstruments->end(); + for (int i = 0; iter != end; ++iter, ++i) { + (*iter)->UpdateChunks(NULL); + } } } // update 'ptbl' chunk - const int iSamples = (pSamples) ? pSamples->size() : 0; - const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; + const int iSamples = (pSamples) ? int(pSamples->size()) : 0; + int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/); - const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; + int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; ptbl->Resize(iPtblSize); pData = (uint8_t*) ptbl->LoadChunkData(); WavePoolCount = iSamples; @@ -1494,12 +1923,167 @@ // update sample's chunks if (pSamples) { - SampleList::iterator iter = pSamples->begin(); - SampleList::iterator end = pSamples->end(); - for (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress + + // do the actual work + SampleList::iterator iter = pSamples->begin(); + SampleList::iterator end = pSamples->end(); + for (int i = 0; iter != end; ++iter, ++i) { + // divide subprogress into sub-subprogress + progress_t subsubprogress; + __divide_progress(&subprogress, &subsubprogress, pSamples->size(), i); + // do the actual work + (*iter)->UpdateChunks(&subsubprogress); + } + + __notify_progress(&subprogress, 1.0); // notify subprogress done + } else { + SampleList::iterator iter = pSamples->begin(); + SampleList::iterator end = pSamples->end(); + for (int i = 0; iter != end; ++iter, ++i) { + (*iter)->UpdateChunks(NULL); + } + } + } + + // if there are any extension files, gather which ones are regular + // extension files used as wave pool files (.gx00, .gx01, ... , .gx98) + // and which one is probably a convolution (GigaPulse) file (always to + // be saved as .gx99) + std::list poolFiles; // < for (.gx00, .gx01, ... , .gx98) files + RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file + if (!ExtensionFiles.empty()) { + std::list::iterator it = ExtensionFiles.begin(); + for (; it != ExtensionFiles.end(); ++it) { + //FIXME: the .gx99 file is always used by GSt for convolution + // data (GigaPulse); so we should better detect by subchunk + // whether the extension file is intended for convolution + // instead of checkking for a file name, because the latter does + // not work for saving new gigs created from scratch + const std::string oldName = (*it)->GetFileName(); + const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99"); + if (isGigaPulseFile) + pGigaPulseFile = *it; + else + poolFiles.push_back(*it); + } + } + + // update the 'xfil' chunk which describes all extension files (wave + // pool files) except the .gx99 file + if (!poolFiles.empty()) { + const int n = poolFiles.size(); + const int iHeaderSize = 4; + const int iEntrySize = 144; + + // make sure chunk exists, and with correct size + RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); + if (ckXfil) + ckXfil->Resize(iHeaderSize + n * iEntrySize); + else + ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize); + + uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData(); + + // re-assemble the chunk's content + store32(pData, n); + std::list::iterator itExtFile = poolFiles.begin(); + for (int i = 0, iOffset = 4; i < n; + ++itExtFile, ++i, iOffset += iEntrySize) + { + // update the filename string and 5 byte extension of each extension file + std::string file = lastPathComponent( + (*itExtFile)->GetFileName() + ); + if (file.length() + 6 > 128) + throw Exception("Fatal error, extension filename length exceeds 122 byte maximum"); + uint8_t* pStrings = &pData[iOffset]; + memset(pStrings, 0, 128); + memcpy(pStrings, file.c_str(), file.length()); + pStrings += file.length() + 1; + std::string ext = file.substr(file.length()-5); + memcpy(pStrings, ext.c_str(), 5); + // update the dlsid of the extension file + uint8_t* pId = &pData[iOffset + 128]; + dlsid_t id; + RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID); + if (ckDLSID) { + ckDLSID->Read(&id.ulData1, 1, 4); + ckDLSID->Read(&id.usData2, 1, 2); + ckDLSID->Read(&id.usData3, 1, 2); + ckDLSID->Read(id.abData, 8, 1); + } else { + ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16); + Resource::GenerateDLSID(&id); + uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData(); + store32(&pData[0], id.ulData1); + store16(&pData[4], id.usData2); + store16(&pData[6], id.usData3); + memcpy(&pData[8], id.abData, 8); + } + store32(&pId[0], id.ulData1); + store16(&pId[4], id.usData2); + store16(&pId[6], id.usData3); + memcpy(&pId[8], id.abData, 8); + } + } else { + // in case there was a 'xfil' chunk, remove it + RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); + if (ckXfil) pRIFF->DeleteSubChunk(ckXfil); + } + + // update the 'doxf' chunk which describes a .gx99 extension file + // which contains convolution data (GigaPulse) + if (pGigaPulseFile) { + RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); + if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148); + + uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData(); + + // update the dlsid from the extension file + uint8_t* pId = &pData[132]; + RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID); + if (!ckDLSID) { //TODO: auto generate DLS ID if missing + throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk"); + } else { + dlsid_t id; + // read DLS ID from extension files's DLS ID chunk + uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData(); + id.ulData1 = load32(&pData[0]); + id.usData2 = load16(&pData[4]); + id.usData3 = load16(&pData[6]); + memcpy(id.abData, &pData[8], 8); + // store DLS ID to 'doxf' chunk + store32(&pId[0], id.ulData1); + store16(&pId[4], id.usData2); + store16(&pId[6], id.usData3); + memcpy(&pId[8], id.abData, 8); } + } else { + // in case there was a 'doxf' chunk, remove it + RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); + if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf); + } + + // the RIFF file to be written might now been grown >= 4GB or might + // been shrunk < 4GB, so we might need to update the wave pool offset + // size and thus accordingly we would need to resize the wave pool + // chunk + const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize(); + const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file + poolFiles.size() > 0; // < 32 bit gig file where the hi 32 bits are used as extension file nr + if (b64BitWavePoolOffsets != bRequires64Bit) { + b64BitWavePoolOffsets = bRequires64Bit; + iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; + iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; + ptbl->Resize(iPtblSize); } + + if (pProgress) + __notify_progress(pProgress, 1.0); // notify done } /** @brief Save changes to another file. @@ -1514,11 +2098,63 @@ * the new file (given by \a Path) afterwards. * * @param Path - path and file name where everything should be written to + * @param pProgress - optional: callback function for progress notification */ - void File::Save(const String& Path) { - UpdateChunks(); - pRIFF->Save(Path); - __UpdateWavePoolTableChunk(); + void File::Save(const String& Path, progress_t* pProgress) { + // calculate number of tasks to notify progress appropriately + const size_t nExtFiles = ExtensionFiles.size(); + const float tasks = 2.f + nExtFiles; + + // save extension files (if required) + if (!ExtensionFiles.empty()) { + // for assembling path of extension files to be saved to + const std::string baseName = pathWithoutExtension(Path); + // save the individual extension files + std::list::iterator it = ExtensionFiles.begin(); + for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) { + //FIXME: the .gx99 file is always used by GSt for convolution + // data (GigaPulse); so we should better detect by subchunk + // whether the extension file is intended for convolution + // instead of checkking for a file name, because the latter does + // not work for saving new gigs created from scratch + const std::string oldName = (*it)->GetFileName(); + const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99"); + std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1); + std::string newPath = baseName + ext; + // save extension file to its new location + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files + // do the actual work + (*it)->Save(newPath, &subprogress); + } else + (*it)->Save(newPath); + } + } + + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) + // do the actual work + UpdateChunks(&subprogress); + } else + UpdateChunks(NULL); + + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) + // do the actual work + pRIFF->Save(Path, &subprogress); + } else + pRIFF->Save(Path); + + UpdateFileOffsets(); + + if (pProgress) + __notify_progress(pProgress, 1.0); // notify done } /** @brief Save changes to same file. @@ -1527,12 +2163,66 @@ * file. The file might temporarily grow to a higher size than it will * have at the end of the saving process. * - * @throws RIFF::Exception if any kind of IO error occured - * @throws DLS::Exception if any kind of DLS specific error occured + * @param pProgress - optional: callback function for progress notification + * @throws RIFF::Exception if any kind of IO error occurred + * @throws DLS::Exception if any kind of DLS specific error occurred + */ + void File::Save(progress_t* pProgress) { + // calculate number of tasks to notify progress appropriately + const size_t nExtFiles = ExtensionFiles.size(); + const float tasks = 2.f + nExtFiles; + + // save extension files (if required) + if (!ExtensionFiles.empty()) { + std::list::iterator it = ExtensionFiles.begin(); + for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) { + // save extension file + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files + // do the actual work + (*it)->Save(&subprogress); + } else + (*it)->Save(); + } + } + + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) + // do the actual work + UpdateChunks(&subprogress); + } else + UpdateChunks(NULL); + + if (pProgress) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) + // do the actual work + pRIFF->Save(&subprogress); + } else + pRIFF->Save(); + + UpdateFileOffsets(); + + if (pProgress) + __notify_progress(pProgress, 1.0); // notify done + } + + /** @brief Updates all file offsets stored all over the file. + * + * This virtual method is called whenever the overall file layout has been + * changed (i.e. file or individual RIFF chunks have been resized). It is + * then the responsibility of this method to update all file offsets stored + * in the file format. For example samples are referenced by instruments by + * file offsets. The gig format also stores references to instrument + * scripts as file offsets, and thus it overrides this method to update + * those file offsets as well. */ - void File::Save() { - UpdateChunks(); - pRIFF->Save(); + void File::UpdateFileOffsets() { __UpdateWavePoolTableChunk(); } @@ -1570,11 +2260,11 @@ RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; // check if 'ptbl' chunk is large enough - WavePoolCount = (pSamples) ? pSamples->size() : 0; - const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; + WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0; + const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); // save the 'ptbl' chunk's current read/write position - unsigned long ulOriginalPos = ptbl->GetPos(); + file_offset_t ullOriginalPos = ptbl->GetPos(); // update headers ptbl->SetPos(0); uint32_t tmp = WavePoolHeaderSize; @@ -1597,7 +2287,7 @@ } } // restore 'ptbl' chunk's original read/write position - ptbl->SetPos(ulOriginalPos); + ptbl->SetPos(ullOriginalPos); } /** @@ -1606,42 +2296,105 @@ * exists already. */ void File::__UpdateWavePoolTable() { - WavePoolCount = (pSamples) ? pSamples->size() : 0; + WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0; // resize wave pool table arrays if (pWavePoolTable) delete[] pWavePoolTable; if (pWavePoolTableHi) delete[] pWavePoolTableHi; pWavePoolTable = new uint32_t[WavePoolCount]; pWavePoolTableHi = new uint32_t[WavePoolCount]; if (!pSamples) return; - // update offsets int wave pool table + // update offsets in wave pool table RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); - uint64_t wvplFileOffset = wvpl->GetFilePos(); - if (b64BitWavePoolOffsets) { + uint64_t wvplFileOffset = wvpl->GetFilePos() - + wvpl->GetPos(); // mandatory, since position might have changed + if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ... SampleList::iterator iter = pSamples->begin(); SampleList::iterator end = pSamples->end(); for (int i = 0 ; iter != end ; ++iter, i++) { - uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; - (*iter)->ulWavePoolOffset = _64BitOffset; - pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); - pWavePoolTable[i] = (uint32_t) _64BitOffset; - } - } else { // conventional 32 bit offsets - SampleList::iterator iter = pSamples->begin(); - SampleList::iterator end = pSamples->end(); - for (int i = 0 ; iter != end ; ++iter, i++) { - uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; - (*iter)->ulWavePoolOffset = _64BitOffset; + uint64_t _64BitOffset = + (*iter)->pWaveList->GetFilePos() - + (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure + wvplFileOffset - + LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); + (*iter)->ullWavePoolOffset = _64BitOffset; pWavePoolTable[i] = (uint32_t) _64BitOffset; } + } else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ... + if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ... + SampleList::iterator iter = pSamples->begin(); + SampleList::iterator end = pSamples->end(); + for (int i = 0 ; iter != end ; ++iter, i++) { + uint64_t _64BitOffset = + (*iter)->pWaveList->GetFilePos() - + (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure + wvplFileOffset - + LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); + (*iter)->ullWavePoolOffset = _64BitOffset; + pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); + pWavePoolTable[i] = (uint32_t) _64BitOffset; + } + } else { // 32 bit offsets with extension files (GigaStudio legacy support) ... + // the main gig and the extension files may contain wave data + std::vector poolFiles; + poolFiles.push_back(pRIFF); + poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end()); + + RIFF::File* pCurPoolFile = NULL; + int fileNo = 0; + int waveOffset = 0; + SampleList::iterator iter = pSamples->begin(); + SampleList::iterator end = pSamples->end(); + for (int i = 0 ; iter != end ; ++iter, i++) { + RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile(); + // if this sample is located in the same pool file as the + // last we reuse the previously computed fileNo and waveOffset + if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ... + pCurPoolFile = pPoolFile; + + std::vector::iterator sIter; + sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile); + if (sIter != poolFiles.end()) + fileNo = std::distance(poolFiles.begin(), sIter); + else + throw DLS::Exception("Fatal error, unknown pool file"); + + RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL); + if (!extWvpl) + throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk"); + waveOffset = + extWvpl->GetFilePos() - + extWvpl->GetPos() + // mandatory, since position might have changed + LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize()); + } + uint64_t _64BitOffset = + (*iter)->pWaveList->GetFilePos() - + (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure + waveOffset; + // pWavePoolTableHi stores file number when extension files are in use + pWavePoolTableHi[i] = (uint32_t) fileNo; + pWavePoolTable[i] = (uint32_t) _64BitOffset; + (*iter)->ullWavePoolOffset = _64BitOffset; + } + } } } - // *************** Exception *************** // * - Exception::Exception(String Message) : RIFF::Exception(Message) { + Exception::Exception() : RIFF::Exception() { + } + + Exception::Exception(String format, ...) : RIFF::Exception() { + va_list arg; + va_start(arg, format); + Message = assemble(format, arg); + va_end(arg); + } + + Exception::Exception(String format, va_list arg) : RIFF::Exception() { + Message = assemble(format, arg); } void Exception::PrintMessage() {