/[svn]/libgig/trunk/src/DLS.cpp
ViewVC logotype

Diff of /libgig/trunk/src/DLS.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2482 by schoenebeck, Mon Nov 25 02:22:38 2013 UTC revision 2912 by schoenebeck, Tue May 17 14:30:10 2016 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2013 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2016 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 144  namespace DLS { Line 144  namespace DLS {
144      /**      /**
145       * Apply articulation connections to the respective RIFF chunks. You       * Apply articulation connections to the respective RIFF chunks. You
146       * have to call File::Save() to make changes persistent.       * have to call File::Save() to make changes persistent.
147         *
148         * @param pProgress - callback function for progress notification
149       */       */
150      void Articulation::UpdateChunks() {      void Articulation::UpdateChunks(progress_t* pProgress) {
151          const int iEntrySize = 12; // 12 bytes per connection block          const int iEntrySize = 12; // 12 bytes per connection block
152          pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);          pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
153          uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();          uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
# Line 217  namespace DLS { Line 219  namespace DLS {
219      /**      /**
220       * Apply all articulations to the respective RIFF chunks. You have to       * Apply all articulations to the respective RIFF chunks. You have to
221       * call File::Save() to make changes persistent.       * call File::Save() to make changes persistent.
222         *
223         * @param pProgress - callback function for progress notification
224       */       */
225      void Articulator::UpdateChunks() {      void Articulator::UpdateChunks(progress_t* pProgress) {
226          if (pArticulations) {          if (pArticulations) {
227              ArticulationList::iterator iter = pArticulations->begin();              ArticulationList::iterator iter = pArticulations->begin();
228              ArticulationList::iterator end  = pArticulations->end();              ArticulationList::iterator end  = pArticulations->end();
229              for (; iter != end; ++iter) {              for (; iter != end; ++iter) {
230                  (*iter)->UpdateChunks();                  (*iter)->UpdateChunks(pProgress);
231              }              }
232          }          }
233      }      }
# Line 336  namespace DLS { Line 340  namespace DLS {
340       *       *
341       * Apply current INFO field values to the respective INFO chunks. You       * Apply current INFO field values to the respective INFO chunks. You
342       * have to call File::Save() to make changes persistent.       * have to call File::Save() to make changes persistent.
343         *
344         * @param pProgress - callback function for progress notification
345       */       */
346      void Info::UpdateChunks() {      void Info::UpdateChunks(progress_t* pProgress) {
347          if (!pResourceListChunk) return;          if (!pResourceListChunk) return;
348    
349          // make sure INFO list chunk exists          // make sure INFO list chunk exists
# Line 464  namespace DLS { Line 470  namespace DLS {
470       * will not be applied at the moment (yet).       * will not be applied at the moment (yet).
471       *       *
472       * You have to call File::Save() to make changes persistent.       * You have to call File::Save() to make changes persistent.
473         *
474         * @param pProgress - callback function for progress notification
475       */       */
476      void Resource::UpdateChunks() {      void Resource::UpdateChunks(progress_t* pProgress) {
477          pInfo->UpdateChunks();          pInfo->UpdateChunks(pProgress);
478    
479          if (pDLSID) {          if (pDLSID) {
480              // make sure 'dlid' chunk exists              // make sure 'dlid' chunk exists
# Line 582  namespace DLS { Line 590  namespace DLS {
590      /**      /**
591       * Apply all sample player options to the respective RIFF chunk. You       * Apply all sample player options to the respective RIFF chunk. You
592       * have to call File::Save() to make changes persistent.       * have to call File::Save() to make changes persistent.
593         *
594         * @param pProgress - callback function for progress notification
595       */       */
596      void Sampler::UpdateChunks() {      void Sampler::UpdateChunks(progress_t* pProgress) {
597          // make sure 'wsmp' chunk exists          // make sure 'wsmp' chunk exists
598          RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);          RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
599          int wsmpSize = uiHeaderSize + SampleLoops * 16;          int wsmpSize = uiHeaderSize + SampleLoops * 16;
# Line 701  namespace DLS { Line 711  namespace DLS {
711       * @param WavePoolOffset - offset of this sample data from wave pool       * @param WavePoolOffset - offset of this sample data from wave pool
712       *                         ('wvpl') list chunk       *                         ('wvpl') list chunk
713       */       */
714      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) {
715          pWaveList = waveList;          pWaveList = waveList;
716          ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE;          ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize());
717          pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);          pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
718          pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);          pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);
719          if (pCkFormat) {          if (pCkFormat) {
# Line 783  namespace DLS { Line 793  namespace DLS {
793          Resize(orig->GetSize());          Resize(orig->GetSize());
794          char* buf = (char*) LoadSampleData();          char* buf = (char*) LoadSampleData();
795          Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now          Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now
796          const unsigned long restorePos = pOrig->pCkData->GetPos();          const file_offset_t restorePos = pOrig->pCkData->GetPos();
797          pOrig->SetPos(0);          pOrig->SetPos(0);
798          for (unsigned long todo = pOrig->GetSize(), i = 0; todo; ) {          for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) {
799              const int iReadAtOnce = 64*1024;              const int iReadAtOnce = 64*1024;
800              unsigned long n = (iReadAtOnce < todo) ? iReadAtOnce : todo;              file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo;
801              n = pOrig->Read(&buf[i], n);              n = pOrig->Read(&buf[i], n);
802              if (!n) break;              if (!n) break;
803              todo -= n;              todo -= n;
# Line 845  namespace DLS { Line 855  namespace DLS {
855       * @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM       * @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM
856       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
857       */       */
858      unsigned long Sample::GetSize() const {      file_offset_t Sample::GetSize() const {
859          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
860          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
861      }      }
# Line 903  namespace DLS { Line 913  namespace DLS {
913       * @throws Exception if no data RIFF chunk was created for the sample yet       * @throws Exception if no data RIFF chunk was created for the sample yet
914       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
915       */       */
916      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) {
917          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
918          if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one");          if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one");
919          unsigned long orderedBytes = SampleCount * FrameSize;          file_offset_t orderedBytes = SampleCount * FrameSize;
920          unsigned long result = pCkData->SetPos(orderedBytes, Whence);          file_offset_t result = pCkData->SetPos(orderedBytes, Whence);
921          return (result == orderedBytes) ? SampleCount          return (result == orderedBytes) ? SampleCount
922                                          : result / FrameSize;                                          : result / FrameSize;
923      }      }
# Line 921  namespace DLS { Line 931  namespace DLS {
931       * @param pBuffer      destination buffer       * @param pBuffer      destination buffer
932       * @param SampleCount  number of sample points to read       * @param SampleCount  number of sample points to read
933       */       */
934      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {      file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) {
935          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
936          return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?          return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
937      }      }
# Line 941  namespace DLS { Line 951  namespace DLS {
951       * @throws Exception if current sample size is too small       * @throws Exception if current sample size is too small
952       * @see LoadSampleData()       * @see LoadSampleData()
953       */       */
954      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {      file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) {
955          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
956          if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");          if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");
957          return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?          return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
# Line 951  namespace DLS { Line 961  namespace DLS {
961       * Apply sample and its settings to the respective RIFF chunks. You have       * Apply sample and its settings to the respective RIFF chunks. You have
962       * to call File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
963       *       *
964         * @param pProgress - callback function for progress notification
965       * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data       * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data
966       *                   was provided yet       *                   was provided yet
967       */       */
968      void Sample::UpdateChunks() {      void Sample::UpdateChunks(progress_t* pProgress) {
969          if (FormatTag != DLS_WAVE_FORMAT_PCM)          if (FormatTag != DLS_WAVE_FORMAT_PCM)
970              throw Exception("Could not save sample, only PCM format is supported");              throw Exception("Could not save sample, only PCM format is supported");
971          // we refuse to do anything if not sample wave form was provided yet          // we refuse to do anything if not sample wave form was provided yet
972          if (!pCkData)          if (!pCkData)
973              throw Exception("Could not save sample, there is no sample data to save");              throw Exception("Could not save sample, there is no sample data to save");
974          // update chunks of base class as well          // update chunks of base class as well
975          Resource::UpdateChunks();          Resource::UpdateChunks(pProgress);
976          // make sure 'fmt' chunk exists          // make sure 'fmt' chunk exists
977          RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT);          RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT);
978          if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format          if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
# Line 1036  namespace DLS { Line 1047  namespace DLS {
1047      Sample* Region::GetSample() {      Sample* Region::GetSample() {
1048          if (pSample) return pSample;          if (pSample) return pSample;
1049          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
1050          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];          uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
1051          Sample* sample = file->GetFirstSample();          Sample* sample = file->GetFirstSample();
1052          while (sample) {          while (sample) {
1053              if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample);              if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample);
1054              sample = file->GetNextSample();              sample = file->GetNextSample();
1055          }          }
1056          return NULL;          return NULL;
# Line 1094  namespace DLS { Line 1105  namespace DLS {
1105       * Apply Region settings to the respective RIFF chunks. You have to       * Apply Region settings to the respective RIFF chunks. You have to
1106       * call File::Save() to make changes persistent.       * call File::Save() to make changes persistent.
1107       *       *
1108         * @param pProgress - callback function for progress notification
1109       * @throws Exception - if the Region's sample could not be found       * @throws Exception - if the Region's sample could not be found
1110       */       */
1111      void Region::UpdateChunks() {      void Region::UpdateChunks(progress_t* pProgress) {
1112          // make sure 'rgnh' chunk exists          // make sure 'rgnh' chunk exists
1113          RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);          RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);
1114          if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);          if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
# Line 1115  namespace DLS { Line 1127  namespace DLS {
1127    
1128          // update chunks of base classes as well (but skip Resource,          // update chunks of base classes as well (but skip Resource,
1129          // as a rgn doesn't seem to have dlid and INFO chunks)          // as a rgn doesn't seem to have dlid and INFO chunks)
1130          Articulator::UpdateChunks();          Articulator::UpdateChunks(pProgress);
1131          Sampler::UpdateChunks();          Sampler::UpdateChunks(pProgress);
1132    
1133          // make sure 'wlnk' chunk exists          // make sure 'wlnk' chunk exists
1134          RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK);          RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK);
# Line 1269  namespace DLS { Line 1281  namespace DLS {
1281    
1282      void Instrument::MoveRegion(Region* pSrc, Region* pDst) {      void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
1283          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1284          lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0);          lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));
1285    
1286          pRegions->remove(pSrc);          pRegions->remove(pSrc);
1287          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
# Line 1289  namespace DLS { Line 1301  namespace DLS {
1301       * Apply Instrument with all its Regions to the respective RIFF chunks.       * Apply Instrument with all its Regions to the respective RIFF chunks.
1302       * You have to call File::Save() to make changes persistent.       * You have to call File::Save() to make changes persistent.
1303       *       *
1304         * @param pProgress - callback function for progress notification
1305       * @throws Exception - on errors       * @throws Exception - on errors
1306       */       */
1307      void Instrument::UpdateChunks() {      void Instrument::UpdateChunks(progress_t* pProgress) {
1308          // first update base classes' chunks          // first update base classes' chunks
1309          Resource::UpdateChunks();          Resource::UpdateChunks(pProgress);
1310          Articulator::UpdateChunks();          Articulator::UpdateChunks(pProgress);
1311          // make sure 'insh' chunk exists          // make sure 'insh' chunk exists
1312          RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);          RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1313          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
# Line 1313  namespace DLS { Line 1326  namespace DLS {
1326          if (!pRegions) return;          if (!pRegions) return;
1327          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
1328          RegionList::iterator end  = pRegions->end();          RegionList::iterator end  = pRegions->end();
1329          for (; iter != end; ++iter) {          for (int i = 0; iter != end; ++iter, ++i) {
1330              (*iter)->UpdateChunks();              // divide local progress into subprogress
1331                progress_t subprogress;
1332                __divide_progress(pProgress, &subprogress, pRegions->size(), i);
1333                // do the actual work
1334                (*iter)->UpdateChunks(&subprogress);
1335          }          }
1336            __notify_progress(pProgress, 1.0); // notify done
1337      }      }
1338    
1339      /** @brief Destructor.      /** @brief Destructor.
# Line 1449  namespace DLS { Line 1467  namespace DLS {
1467                  for (int i = 0 ; i < WavePoolCount ; i++) {                  for (int i = 0 ; i < WavePoolCount ; i++) {
1468                      pWavePoolTableHi[i] = ptbl->ReadUint32();                      pWavePoolTableHi[i] = ptbl->ReadUint32();
1469                      pWavePoolTable[i] = ptbl->ReadUint32();                      pWavePoolTable[i] = ptbl->ReadUint32();
1470                      if (pWavePoolTable[i] & 0x80000000)                      //NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12)
1471                          throw DLS::Exception("Files larger than 2 GB not yet supported");                      //if (pWavePoolTable[i] & 0x80000000)
1472                        //    throw DLS::Exception("Files larger than 2 GB not yet supported");
1473                  }                  }
1474              } else { // conventional 32 bit offsets              } else { // conventional 32 bit offsets
1475                  ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));                  ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
# Line 1507  namespace DLS { Line 1526  namespace DLS {
1526          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
1527          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1528          if (wvpl) {          if (wvpl) {
1529              unsigned long wvplFileOffset = wvpl->GetFilePos();              file_offset_t wvplFileOffset = wvpl->GetFilePos();
1530              RIFF::List* wave = wvpl->GetFirstSubList();              RIFF::List* wave = wvpl->GetFirstSubList();
1531              while (wave) {              while (wave) {
1532                  if (wave->GetListType() == LIST_TYPE_WAVE) {                  if (wave->GetListType() == LIST_TYPE_WAVE) {
1533                      unsigned long waveFileOffset = wave->GetFilePos();                      file_offset_t waveFileOffset = wave->GetFilePos();
1534                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1535                  }                  }
1536                  wave = wvpl->GetNextSubList();                  wave = wvpl->GetNextSubList();
# Line 1520  namespace DLS { Line 1539  namespace DLS {
1539          else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant)          else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant)
1540              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1541              if (dwpl) {              if (dwpl) {
1542                  unsigned long dwplFileOffset = dwpl->GetFilePos();                  file_offset_t dwplFileOffset = dwpl->GetFilePos();
1543                  RIFF::List* wave = dwpl->GetFirstSubList();                  RIFF::List* wave = dwpl->GetFirstSubList();
1544                  while (wave) {                  while (wave) {
1545                      if (wave->GetListType() == LIST_TYPE_WAVE) {                      if (wave->GetListType() == LIST_TYPE_WAVE) {
1546                          unsigned long waveFileOffset = wave->GetFilePos();                          file_offset_t waveFileOffset = wave->GetFilePos();
1547                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1548                      }                      }
1549                      wave = dwpl->GetNextSubList();                      wave = dwpl->GetNextSubList();
# Line 1671  namespace DLS { Line 1690  namespace DLS {
1690       * the respective RIFF chunks. You have to call Save() to make changes       * the respective RIFF chunks. You have to call Save() to make changes
1691       * persistent.       * persistent.
1692       *       *
1693         * @param pProgress - callback function for progress notification
1694       * @throws Exception - on errors       * @throws Exception - on errors
1695       */       */
1696      void File::UpdateChunks() {      void File::UpdateChunks(progress_t* pProgress) {
1697          // first update base class's chunks          // first update base class's chunks
1698          Resource::UpdateChunks();          Resource::UpdateChunks(pProgress);
1699    
1700          // if version struct exists, update 'vers' chunk          // if version struct exists, update 'vers' chunk
1701          if (pVersion) {          if (pVersion) {
# Line 1697  namespace DLS { Line 1717  namespace DLS {
1717    
1718          // update instrument's chunks          // update instrument's chunks
1719          if (pInstruments) {          if (pInstruments) {
1720                // divide local progress into subprogress
1721                progress_t subprogress;
1722                __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress
1723    
1724                // do the actual work
1725              InstrumentList::iterator iter = pInstruments->begin();              InstrumentList::iterator iter = pInstruments->begin();
1726              InstrumentList::iterator end  = pInstruments->end();              InstrumentList::iterator end  = pInstruments->end();
1727              for (; iter != end; ++iter) {              for (int i = 0; iter != end; ++iter, ++i) {
1728                  (*iter)->UpdateChunks();                  // divide subprogress into sub-subprogress
1729                    progress_t subsubprogress;
1730                    __divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i);
1731                    // do the actual work
1732                    (*iter)->UpdateChunks(&subsubprogress);
1733              }              }
1734    
1735                __notify_progress(&subprogress, 1.0); // notify subprogress done
1736          }          }
1737    
1738          // update 'ptbl' chunk          // update 'ptbl' chunk
1739          const int iSamples = (pSamples) ? pSamples->size() : 0;          const int iSamples = (pSamples) ? pSamples->size() : 0;
1740          const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1741          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1742          if (!ptbl)   ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/);          if (!ptbl)   ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/);
1743          const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;          int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1744          ptbl->Resize(iPtblSize);          ptbl->Resize(iPtblSize);
1745          pData = (uint8_t*) ptbl->LoadChunkData();          pData = (uint8_t*) ptbl->LoadChunkData();
1746          WavePoolCount = iSamples;          WavePoolCount = iSamples;
# Line 1719  namespace DLS { Line 1750  namespace DLS {
1750    
1751          // update sample's chunks          // update sample's chunks
1752          if (pSamples) {          if (pSamples) {
1753                // divide local progress into subprogress
1754                progress_t subprogress;
1755                __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress
1756    
1757                // do the actual work
1758              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
1759              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
1760              for (; iter != end; ++iter) {              for (int i = 0; iter != end; ++iter, ++i) {
1761                  (*iter)->UpdateChunks();                  // divide subprogress into sub-subprogress
1762              }                  progress_t subsubprogress;
1763                    __divide_progress(&subprogress, &subsubprogress, pSamples->size(), i);
1764                    // do the actual work
1765                    (*iter)->UpdateChunks(&subsubprogress);
1766                }
1767    
1768                __notify_progress(&subprogress, 1.0); // notify subprogress done
1769            }
1770    
1771            // the RIFF file to be written might now been grown >= 4GB or might
1772            // been shrunk < 4GB, so we might need to update the wave pool offset
1773            // size and thus accordingly we would need to resize the wave pool
1774            // chunk
1775            const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();
1776            const bool bRequires64Bit = (finalFileSize >> 32) != 0;
1777            if (b64BitWavePoolOffsets != bRequires64Bit) {
1778                b64BitWavePoolOffsets = bRequires64Bit;
1779                iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1780                iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1781                ptbl->Resize(iPtblSize);
1782          }          }
1783    
1784            __notify_progress(pProgress, 1.0); // notify done
1785      }      }
1786    
1787      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1739  namespace DLS { Line 1796  namespace DLS {
1796       * the new file (given by \a Path) afterwards.       * the new file (given by \a Path) afterwards.
1797       *       *
1798       * @param Path - path and file name where everything should be written to       * @param Path - path and file name where everything should be written to
1799         * @param pProgress - optional: callback function for progress notification
1800       */       */
1801      void File::Save(const String& Path) {      void File::Save(const String& Path, progress_t* pProgress) {
1802          UpdateChunks();          {
1803          pRIFF->Save(Path);              // divide local progress into subprogress
1804          __UpdateWavePoolTableChunk();              progress_t subprogress;
1805                __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress
1806                // do the actual work
1807                UpdateChunks(&subprogress);
1808                
1809            }
1810            {
1811                // divide local progress into subprogress
1812                progress_t subprogress;
1813                __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress
1814                // do the actual work
1815                pRIFF->Save(Path, &subprogress);
1816            }
1817            UpdateFileOffsets();
1818            __notify_progress(pProgress, 1.0); // notify done
1819      }      }
1820    
1821      /** @brief Save changes to same file.      /** @brief Save changes to same file.
# Line 1752  namespace DLS { Line 1824  namespace DLS {
1824       * file. The file might temporarily grow to a higher size than it will       * file. The file might temporarily grow to a higher size than it will
1825       * have at the end of the saving process.       * have at the end of the saving process.
1826       *       *
1827         * @param pProgress - optional: callback function for progress notification
1828       * @throws RIFF::Exception if any kind of IO error occured       * @throws RIFF::Exception if any kind of IO error occured
1829       * @throws DLS::Exception  if any kind of DLS specific error occured       * @throws DLS::Exception  if any kind of DLS specific error occured
1830       */       */
1831      void File::Save() {      void File::Save(progress_t* pProgress) {
1832          UpdateChunks();          {
1833          pRIFF->Save();              // divide local progress into subprogress
1834                progress_t subprogress;
1835                __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress
1836                // do the actual work
1837                UpdateChunks(&subprogress);
1838            }
1839            {
1840                // divide local progress into subprogress
1841                progress_t subprogress;
1842                __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress
1843                // do the actual work
1844                pRIFF->Save(&subprogress);
1845            }
1846            UpdateFileOffsets();
1847            __notify_progress(pProgress, 1.0); // notify done
1848        }
1849    
1850        /** @brief Updates all file offsets stored all over the file.
1851         *
1852         * This virtual method is called whenever the overall file layout has been
1853         * changed (i.e. file or individual RIFF chunks have been resized). It is
1854         * then the responsibility of this method to update all file offsets stored
1855         * in the file format. For example samples are referenced by instruments by
1856         * file offsets. The gig format also stores references to instrument
1857         * scripts as file offsets, and thus it overrides this method to update
1858         * those file offsets as well.
1859         */
1860        void File::UpdateFileOffsets() {
1861          __UpdateWavePoolTableChunk();          __UpdateWavePoolTableChunk();
1862      }      }
1863    
# Line 1796  namespace DLS { Line 1896  namespace DLS {
1896          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1897          // check if 'ptbl' chunk is large enough          // check if 'ptbl' chunk is large enough
1898          WavePoolCount = (pSamples) ? pSamples->size() : 0;          WavePoolCount = (pSamples) ? pSamples->size() : 0;
1899          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;          const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
1900          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
1901          // save the 'ptbl' chunk's current read/write position          // save the 'ptbl' chunk's current read/write position
1902          unsigned long ulOriginalPos = ptbl->GetPos();          file_offset_t ullOriginalPos = ptbl->GetPos();
1903          // update headers          // update headers
1904          ptbl->SetPos(0);          ptbl->SetPos(0);
1905          uint32_t tmp = WavePoolHeaderSize;          uint32_t tmp = WavePoolHeaderSize;
# Line 1822  namespace DLS { Line 1922  namespace DLS {
1922              }              }
1923          }          }
1924          // restore 'ptbl' chunk's original read/write position          // restore 'ptbl' chunk's original read/write position
1925          ptbl->SetPos(ulOriginalPos);          ptbl->SetPos(ullOriginalPos);
1926      }      }
1927    
1928      /**      /**
# Line 1845  namespace DLS { Line 1945  namespace DLS {
1945              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
1946              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
1947              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
1948                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
1949                  (*iter)->ulWavePoolOffset = _64BitOffset;                  (*iter)->ullWavePoolOffset = _64BitOffset;
1950                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
1951                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;
1952              }              }
# Line 1854  namespace DLS { Line 1954  namespace DLS {
1954              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
1955              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
1956              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
1957                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
1958                  (*iter)->ulWavePoolOffset = _64BitOffset;                  (*iter)->ullWavePoolOffset = _64BitOffset;
1959                  pWavePoolTable[i] = (uint32_t) _64BitOffset;                  pWavePoolTable[i] = (uint32_t) _64BitOffset;
1960              }              }
1961          }          }

Legend:
Removed from v.2482  
changed lines
  Added in v.2912

  ViewVC Help
Powered by ViewVC