--- libgig/trunk/src/DLS.cpp 2013/01/07 23:23:58 2394 +++ libgig/trunk/src/DLS.cpp 2016/12/14 18:55:08 3053 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2013 by Christian Schoenebeck * + * Copyright (C) 2003-2016 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -144,8 +144,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(); @@ -217,13 +219,15 @@ /** * 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() { + void Articulator::UpdateChunks(progress_t* pProgress) { if (pArticulations) { ArticulationList::iterator iter = pArticulations->begin(); ArticulationList::iterator end = pArticulations->end(); for (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + (*iter)->UpdateChunks(pProgress); } } } @@ -336,8 +340,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 @@ -464,9 +470,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 @@ -582,8 +590,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; @@ -701,9 +711,9 @@ * @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) { @@ -744,6 +754,57 @@ RIFF::List* pParent = pWaveList->GetParent(); pParent->DeleteSubChunk(pWaveList); } + + /** + * 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. * @@ -794,7 +855,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; } @@ -821,19 +882,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); } /** @@ -852,11 +915,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; } @@ -870,7 +933,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? } @@ -890,7 +953,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? @@ -900,17 +963,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 @@ -932,7 +996,7 @@ 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->Read(&KeyRange, 2, 2); @@ -954,7 +1018,7 @@ } SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE; - // sample informations + // sample information RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK); if (wlnk) { WaveLinkOptionFlags = wlnk->ReadUint16(); @@ -985,10 +1049,10 @@ 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; @@ -1043,9 +1107,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); @@ -1064,8 +1129,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); @@ -1122,8 +1187,15 @@ PhaseGroup = orig->PhaseGroup; MultiChannel = orig->MultiChannel; Channel = orig->Channel; - WavePoolTableIndex = orig->WavePoolTableIndex; - pSample = orig->pSample; + // 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 @@ -1205,13 +1277,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); @@ -1223,7 +1295,7 @@ 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(); delete pRegion; } @@ -1231,18 +1303,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); @@ -1255,9 +1328,14 @@ 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) { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, pRegions->size(), i); + // do the actual work + (*iter)->UpdateChunks(&subprogress); } + __notify_progress(pProgress, 1.0); // notify done } /** @brief Destructor. @@ -1391,8 +1469,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)); @@ -1449,11 +1528,11 @@ 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(); RIFF::List* wave = wvpl->GetFirstSubList(); while (wave) { if (wave->GetListType() == LIST_TYPE_WAVE) { - unsigned long waveFileOffset = wave->GetFilePos(); + file_offset_t waveFileOffset = wave->GetFilePos(); pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); } wave = wvpl->GetNextSubList(); @@ -1462,11 +1541,11 @@ 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(); RIFF::List* wave = dwpl->GetFirstSubList(); while (wave) { if (wave->GetListType() == LIST_TYPE_WAVE) { - unsigned long waveFileOffset = wave->GetFilePos(); + file_offset_t waveFileOffset = wave->GetFilePos(); pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); } wave = dwpl->GetNextSubList(); @@ -1599,17 +1678,26 @@ 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) { @@ -1623,7 +1711,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(); @@ -1631,19 +1719,30 @@ // update instrument's chunks if (pInstruments) { + // 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 (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + 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 } // 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; @@ -1653,12 +1752,38 @@ // update sample's chunks if (pSamples) { + // 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 (; iter != end; ++iter) { - (*iter)->UpdateChunks(); - } + 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 + } + + // 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; + if (b64BitWavePoolOffsets != bRequires64Bit) { + b64BitWavePoolOffsets = bRequires64Bit; + iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; + iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; + ptbl->Resize(iPtblSize); } + + __notify_progress(pProgress, 1.0); // notify done } /** @brief Save changes to another file. @@ -1673,11 +1798,26 @@ * 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) { + { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress + // do the actual work + UpdateChunks(&subprogress); + + } + { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress + // do the actual work + pRIFF->Save(Path, &subprogress); + } + UpdateFileOffsets(); + __notify_progress(pProgress, 1.0); // notify done } /** @brief Save changes to same file. @@ -1686,12 +1826,40 @@ * 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) { + { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress + // do the actual work + UpdateChunks(&subprogress); + } + { + // divide local progress into subprogress + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress + // do the actual work + pRIFF->Save(&subprogress); + } + UpdateFileOffsets(); + __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(); } @@ -1729,11 +1897,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; @@ -1756,7 +1924,7 @@ } } // restore 'ptbl' chunk's original read/write position - ptbl->SetPos(ulOriginalPos); + ptbl->SetPos(ullOriginalPos); } /** @@ -1765,7 +1933,7 @@ * 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; @@ -1779,8 +1947,8 @@ 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() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); + (*iter)->ullWavePoolOffset = _64BitOffset; pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); pWavePoolTable[i] = (uint32_t) _64BitOffset; } @@ -1788,8 +1956,8 @@ 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() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); + (*iter)->ullWavePoolOffset = _64BitOffset; pWavePoolTable[i] = (uint32_t) _64BitOffset; } }