/[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 2909 by schoenebeck, Thu May 12 19:34:17 2016 UTC revision 3929 by schoenebeck, Tue Jun 15 12:22:26 2021 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-2014 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2021 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 24  Line 24 
24  #include "DLS.h"  #include "DLS.h"
25    
26  #include <algorithm>  #include <algorithm>
27    #include <vector>
28  #include <time.h>  #include <time.h>
29    
30  #ifdef __APPLE__  #ifdef __APPLE__
# Line 121  namespace DLS { Line 122  namespace DLS {
122              artl->GetChunkID() != CHUNK_ID_ARTL) {              artl->GetChunkID() != CHUNK_ID_ARTL) {
123                throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected");                throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected");
124          }          }
125    
126            artl->SetPos(0);
127    
128          HeaderSize  = artl->ReadUint32();          HeaderSize  = artl->ReadUint32();
129          Connections = artl->ReadUint32();          Connections = artl->ReadUint32();
130          artl->SetPos(HeaderSize);          artl->SetPos(HeaderSize);
# Line 163  namespace DLS { Line 167  namespace DLS {
167          }          }
168      }      }
169    
170        /** @brief Remove all RIFF chunks associated with this Articulation object.
171         *
172         * At the moment Articulation::DeleteChunks() does nothing. It is
173         * recommended to call this method explicitly though from deriving classes's
174         * own overridden implementation of this method to avoid potential future
175         * compatiblity issues.
176         *
177         * See Storage::DeleteChunks() for details.
178         */
179        void Articulation::DeleteChunks() {
180        }
181    
182    
183    
184  // *************** Articulator  ***************  // *************** Articulator  ***************
# Line 193  namespace DLS { Line 209  namespace DLS {
209          if (lart) {          if (lart) {
210              uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2              uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2
211                                                                           : CHUNK_ID_ARTL;                                                                           : CHUNK_ID_ARTL;
212              RIFF::Chunk* art = lart->GetFirstSubChunk();              size_t i = 0;
213              while (art) {              for (RIFF::Chunk* art = lart->GetSubChunkAt(i); art;
214                     art = lart->GetSubChunkAt(++i))
215                {
216                  if (art->GetChunkID() == artCkType) {                  if (art->GetChunkID() == artCkType) {
217                      if (!pArticulations) pArticulations = new ArticulationList;                      if (!pArticulations) pArticulations = new ArticulationList;
218                      pArticulations->push_back(new Articulation(art));                      pArticulations->push_back(new Articulation(art));
219                  }                  }
                 art = lart->GetNextSubChunk();  
220              }              }
221          }          }
222      }      }
# Line 231  namespace DLS { Line 248  namespace DLS {
248              }              }
249          }          }
250      }      }
251        
252        /** @brief Remove all RIFF chunks associated with this Articulator object.
253         *
254         * See Storage::DeleteChunks() for details.
255         */
256        void Articulator::DeleteChunks() {
257            if (pArticulations) {
258                ArticulationList::iterator iter = pArticulations->begin();
259                ArticulationList::iterator end  = pArticulations->end();
260                for (; iter != end; ++iter) {
261                    (*iter)->DeleteChunks();
262                }
263            }
264        }
265    
266      /**      /**
267       * Not yet implemented in this version, since the .gig format does       * Not yet implemented in this version, since the .gig format does
268       * not need to copy DLS articulators and so far nobody used pure       * not need to copy DLS articulators and so far nobody used pure
# Line 398  namespace DLS { Line 429  namespace DLS {
429          SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));          SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));
430          SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));          SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));
431      }      }
432        
433        /** @brief Remove all RIFF chunks associated with this Info object.
434         *
435         * At the moment Info::DeleteChunks() does nothing. It is
436         * recommended to call this method explicitly though from deriving classes's
437         * own overridden implementation of this method to avoid potential future
438         * compatiblity issues.
439         *
440         * See Storage::DeleteChunks() for details.
441         */
442        void Info::DeleteChunks() {
443        }
444    
445      /**      /**
446       * Make a deep copy of the Info object given by @a orig and assign it to       * Make a deep copy of the Info object given by @a orig and assign it to
447       * this object.       * this object.
# Line 449  namespace DLS { Line 492  namespace DLS {
492    
493          RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);          RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);
494          if (ckDLSID) {          if (ckDLSID) {
495                ckDLSID->SetPos(0);
496    
497              pDLSID = new dlsid_t;              pDLSID = new dlsid_t;
498              ckDLSID->Read(&pDLSID->ulData1, 1, 4);              ckDLSID->Read(&pDLSID->ulData1, 1, 4);
499              ckDLSID->Read(&pDLSID->usData2, 1, 2);              ckDLSID->Read(&pDLSID->usData2, 1, 2);
# Line 463  namespace DLS { Line 508  namespace DLS {
508          if (pInfo)  delete pInfo;          if (pInfo)  delete pInfo;
509      }      }
510    
511        /** @brief Remove all RIFF chunks associated with this Resource object.
512         *
513         * At the moment Resource::DeleteChunks() does nothing. It is recommended
514         * to call this method explicitly though from deriving classes's own
515         * overridden implementation of this method to avoid potential future
516         * compatiblity issues.
517         *
518         * See Storage::DeleteChunks() for details.
519         */
520        void Resource::DeleteChunks() {
521        }
522    
523      /** @brief Update chunks with current Resource data.      /** @brief Update chunks with current Resource data.
524       *       *
525       * Apply Resource data persistently below the previously given resource       * Apply Resource data persistently below the previously given resource
# Line 493  namespace DLS { Line 550  namespace DLS {
550       * Generates a new DLSID for the resource.       * Generates a new DLSID for the resource.
551       */       */
552      void Resource::GenerateDLSID() {      void Resource::GenerateDLSID() {
553  #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)          #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
   
554          if (!pDLSID) pDLSID = new dlsid_t;          if (!pDLSID) pDLSID = new dlsid_t;
555            GenerateDLSID(pDLSID);
556            #endif
557        }
558    
559        void Resource::GenerateDLSID(dlsid_t* pDLSID) {
560  #ifdef WIN32  #ifdef WIN32
   
561          UUID uuid;          UUID uuid;
562          UuidCreate(&uuid);          UuidCreate(&uuid);
563          pDLSID->ulData1 = uuid.Data1;          pDLSID->ulData1 = uuid.Data1;
# Line 522  namespace DLS { Line 581  namespace DLS {
581          pDLSID->abData[5] = uuid.byte13;          pDLSID->abData[5] = uuid.byte13;
582          pDLSID->abData[6] = uuid.byte14;          pDLSID->abData[6] = uuid.byte14;
583          pDLSID->abData[7] = uuid.byte15;          pDLSID->abData[7] = uuid.byte15;
584  #else  #elif defined(HAVE_UUID_GENERATE)
585          uuid_t uuid;          uuid_t uuid;
586          uuid_generate(uuid);          uuid_generate(uuid);
587          pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24;          pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24;
588          pDLSID->usData2 = uuid[4] | uuid[5] << 8;          pDLSID->usData2 = uuid[4] | uuid[5] << 8;
589          pDLSID->usData3 = uuid[6] | uuid[7] << 8;          pDLSID->usData3 = uuid[6] | uuid[7] << 8;
590          memcpy(pDLSID->abData, &uuid[8], 8);          memcpy(pDLSID->abData, &uuid[8], 8);
591  #endif  #else
592    # error "Missing support for uuid generation"
593  #endif  #endif
594      }      }
595            
# Line 551  namespace DLS { Line 611  namespace DLS {
611          pParentList       = ParentList;          pParentList       = ParentList;
612          RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);          RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);
613          if (wsmp) {          if (wsmp) {
614                wsmp->SetPos(0);
615    
616              uiHeaderSize   = wsmp->ReadUint32();              uiHeaderSize   = wsmp->ReadUint32();
617              UnityNote      = wsmp->ReadUint16();              UnityNote      = wsmp->ReadUint16();
618              FineTune       = wsmp->ReadInt16();              FineTune       = wsmp->ReadInt16();
# Line 625  namespace DLS { Line 687  namespace DLS {
687          }          }
688      }      }
689    
690        /** @brief Remove all RIFF chunks associated with this Sampler object.
691         *
692         * At the moment Sampler::DeleteChunks() does nothing. It is
693         * recommended to call this method explicitly though from deriving classes's
694         * own overridden implementation of this method to avoid potential future
695         * compatiblity issues.
696         *
697         * See Storage::DeleteChunks() for details.
698         */
699        void Sampler::DeleteChunks() {
700        }
701    
702      /**      /**
703       * Adds a new sample loop with the provided loop definition.       * Adds a new sample loop with the provided loop definition.
704       *       *
# Line 711  namespace DLS { Line 785  namespace DLS {
785       * @param WavePoolOffset - offset of this sample data from wave pool       * @param WavePoolOffset - offset of this sample data from wave pool
786       *                         ('wvpl') list chunk       *                         ('wvpl') list chunk
787       */       */
788      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) {
789          pWaveList = waveList;          pWaveList = waveList;
790          ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE;          ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize());
791          pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);          pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
792          pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);          pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);
793          if (pCkFormat) {          if (pCkFormat) {
794                pCkFormat->SetPos(0);
795    
796              // common fields              // common fields
797              FormatTag              = pCkFormat->ReadUint16();              FormatTag              = pCkFormat->ReadUint16();
798              Channels               = pCkFormat->ReadUint16();              Channels               = pCkFormat->ReadUint16();
# Line 747  namespace DLS { Line 823  namespace DLS {
823    
824      /** @brief Destructor.      /** @brief Destructor.
825       *       *
826       * Removes RIFF chunks associated with this Sample and frees all       * Frees all memory occupied by this sample.
      * memory occupied by this sample.  
827       */       */
828      Sample::~Sample() {      Sample::~Sample() {
829          RIFF::List* pParent = pWaveList->GetParent();          if (pCkData)
830          pParent->DeleteSubChunk(pWaveList);              pCkData->ReleaseChunkData();
831            if (pCkFormat)
832                pCkFormat->ReleaseChunkData();
833      }      }
834        
835        /** @brief Remove all RIFF chunks associated with this Sample object.
836         *
837         * See Storage::DeleteChunks() for details.
838         */
839        void Sample::DeleteChunks() {
840            // handle base class
841            Resource::DeleteChunks();
842    
843            // handle own RIFF chunks
844            if (pWaveList) {
845                RIFF::List* pParent = pWaveList->GetParent();
846                pParent->DeleteSubChunk(pWaveList);
847                pWaveList = NULL;
848            }
849        }
850    
851      /**      /**
852       * Make a deep copy of the Sample object given by @a orig (without the       * Make a deep copy of the Sample object given by @a orig (without the
853       * actual sample waveform data however) and assign it to this object.       * actual sample waveform data however) and assign it to this object.
# Line 793  namespace DLS { Line 886  namespace DLS {
886          Resize(orig->GetSize());          Resize(orig->GetSize());
887          char* buf = (char*) LoadSampleData();          char* buf = (char*) LoadSampleData();
888          Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now          Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now
889          const unsigned long restorePos = pOrig->pCkData->GetPos();          const file_offset_t restorePos = pOrig->pCkData->GetPos();
890          pOrig->SetPos(0);          pOrig->SetPos(0);
891          for (unsigned long todo = pOrig->GetSize(), i = 0; todo; ) {          for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) {
892              const int iReadAtOnce = 64*1024;              const int iReadAtOnce = 64*1024;
893              unsigned long n = (iReadAtOnce < todo) ? iReadAtOnce : todo;              file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo;
894              n = pOrig->Read(&buf[i], n);              n = pOrig->Read(&buf[i], n);
895              if (!n) break;              if (!n) break;
896              todo -= n;              todo -= n;
# Line 855  namespace DLS { Line 948  namespace DLS {
948       * @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
949       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
950       */       */
951      unsigned long Sample::GetSize() const {      file_offset_t Sample::GetSize() const {
952          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
953          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
954      }      }
# Line 882  namespace DLS { Line 975  namespace DLS {
975       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
976       * other formats will fail!       * other formats will fail!
977       *       *
978       * @param iNewSize - new sample wave data size in sample points (must be       * @param NewSize - new sample wave data size in sample points (must be
979       *                   greater than zero)       *                  greater than zero)
980       * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM       * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM
981       * @throws Exception if \a iNewSize is less than 1       * @throws Exception if \a NewSize is less than 1 or unrealistic large
982       * @see File::Save(), FrameSize, FormatTag       * @see File::Save(), FrameSize, FormatTag
983       */       */
984      void Sample::Resize(int iNewSize) {      void Sample::Resize(file_offset_t NewSize) {
985          if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");          if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");
986          if (iNewSize < 1) throw Exception("Sample size must be at least one sample point");          if (NewSize < 1) throw Exception("Sample size must be at least one sample point");
987          const int iSizeInBytes = iNewSize * FrameSize;          if ((NewSize >> 48) != 0)
988                throw Exception("Unrealistic high DLS sample size detected");
989            const file_offset_t sizeInBytes = NewSize * FrameSize;
990          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
991          if (pCkData) pCkData->Resize(iSizeInBytes);          if (pCkData) pCkData->Resize(sizeInBytes);
992          else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes);          else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes);
993      }      }
994    
995      /**      /**
# Line 913  namespace DLS { Line 1008  namespace DLS {
1008       * @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
1009       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
1010       */       */
1011      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) {
1012          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
1013          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");
1014          unsigned long orderedBytes = SampleCount * FrameSize;          file_offset_t orderedBytes = SampleCount * FrameSize;
1015          unsigned long result = pCkData->SetPos(orderedBytes, Whence);          file_offset_t result = pCkData->SetPos(orderedBytes, Whence);
1016          return (result == orderedBytes) ? SampleCount          return (result == orderedBytes) ? SampleCount
1017                                          : result / FrameSize;                                          : result / FrameSize;
1018      }      }
# Line 931  namespace DLS { Line 1026  namespace DLS {
1026       * @param pBuffer      destination buffer       * @param pBuffer      destination buffer
1027       * @param SampleCount  number of sample points to read       * @param SampleCount  number of sample points to read
1028       */       */
1029      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {      file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) {
1030          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
1031          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?
1032      }      }
# Line 951  namespace DLS { Line 1046  namespace DLS {
1046       * @throws Exception if current sample size is too small       * @throws Exception if current sample size is too small
1047       * @see LoadSampleData()       * @see LoadSampleData()
1048       */       */
1049      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {      file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) {
1050          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
1051          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");
1052          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 994  namespace DLS { Line 1089  namespace DLS {
1089      Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) {      Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) {
1090          pCkRegion = rgnList;          pCkRegion = rgnList;
1091    
1092          // articulation informations          // articulation information
1093          RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);          RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);
1094          if (rgnh) {          if (rgnh) {
1095                rgnh->SetPos(0);
1096    
1097              rgnh->Read(&KeyRange, 2, 2);              rgnh->Read(&KeyRange, 2, 2);
1098              rgnh->Read(&VelocityRange, 2, 2);              rgnh->Read(&VelocityRange, 2, 2);
1099              FormatOptionFlags = rgnh->ReadUint16();              FormatOptionFlags = rgnh->ReadUint16();
# Line 1016  namespace DLS { Line 1113  namespace DLS {
1113          }          }
1114          SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;          SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;
1115    
1116          // sample informations          // sample information
1117          RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);          RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);
1118          if (wlnk) {          if (wlnk) {
1119                wlnk->SetPos(0);
1120    
1121              WaveLinkOptionFlags = wlnk->ReadUint16();              WaveLinkOptionFlags = wlnk->ReadUint16();
1122              PhaseGroup          = wlnk->ReadUint16();              PhaseGroup          = wlnk->ReadUint16();
1123              Channel             = wlnk->ReadUint32();              Channel             = wlnk->ReadUint32();
# Line 1037  namespace DLS { Line 1136  namespace DLS {
1136    
1137      /** @brief Destructor.      /** @brief Destructor.
1138       *       *
1139       * Removes RIFF chunks associated with this Region.       * Intended to free up all memory occupied by this Region object. ATM this
1140         * destructor implementation does nothing though.
1141       */       */
1142      Region::~Region() {      Region::~Region() {
1143          RIFF::List* pParent = pCkRegion->GetParent();      }
1144          pParent->DeleteSubChunk(pCkRegion);  
1145        /** @brief Remove all RIFF chunks associated with this Region object.
1146         *
1147         * See Storage::DeleteChunks() for details.
1148         */
1149        void Region::DeleteChunks() {
1150            // handle base classes
1151            Resource::DeleteChunks();
1152            Articulator::DeleteChunks();
1153            Sampler::DeleteChunks();
1154    
1155            // handle own RIFF chunks
1156            if (pCkRegion) {
1157                RIFF::List* pParent = pCkRegion->GetParent();
1158                pParent->DeleteSubChunk(pCkRegion);
1159                pCkRegion = NULL;
1160            }
1161      }      }
1162    
1163      Sample* Region::GetSample() {      Sample* Region::GetSample() {
1164          if (pSample) return pSample;          if (pSample) return pSample;
1165          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
1166          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];          uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
1167          Sample* sample = file->GetFirstSample();          size_t i = 0;
1168          while (sample) {          for (Sample* sample = file->GetSample(i); sample;
1169              if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample);                       sample = file->GetSample(++i))
1170              sample = file->GetNextSample();          {
1171                if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample);
1172          }          }
1173          return NULL;          return NULL;
1174      }      }
# Line 1223  namespace DLS { Line 1340  namespace DLS {
1340          midi_locale_t locale;          midi_locale_t locale;
1341          RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);          RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1342          if (insh) {          if (insh) {
1343                insh->SetPos(0);
1344    
1345              Regions = insh->ReadUint32();              Regions = insh->ReadUint32();
1346              insh->Read(&locale, 2, 4);              insh->Read(&locale, 2, 4);
1347          } else { // 'insh' chunk missing          } else { // 'insh' chunk missing
# Line 1240  namespace DLS { Line 1359  namespace DLS {
1359          pRegions = NULL;          pRegions = NULL;
1360      }      }
1361    
1362        /**
1363         * Returns Region at supplied @a pos position within the region list of
1364         * this instrument. If supplied @a pos is out of bounds then @c NULL is
1365         * returned.
1366         *
1367         * @param pos - position of sought Region in region list
1368         * @returns pointer address to requested region or @c NULL if @a pos is
1369         *          out of bounds
1370         */
1371        Region* Instrument::GetRegionAt(size_t pos) {
1372            if (!pRegions) LoadRegions();
1373            if (!pRegions) return NULL;
1374            if (pos >= pRegions->size()) return NULL;
1375            return (*pRegions)[pos];
1376        }
1377    
1378        /**
1379         * Returns the first Region of the instrument. You have to call this
1380         * method once before you use GetNextRegion().
1381         *
1382         * @returns  pointer address to first region or NULL if there is none
1383         * @see      GetNextRegion()
1384         * @deprecated  This method is not reentrant-safe, use GetRegionAt()
1385         *              instead.
1386         */
1387      Region* Instrument::GetFirstRegion() {      Region* Instrument::GetFirstRegion() {
1388          if (!pRegions) LoadRegions();          if (!pRegions) LoadRegions();
1389          if (!pRegions) return NULL;          if (!pRegions) return NULL;
# Line 1247  namespace DLS { Line 1391  namespace DLS {
1391          return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;          return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1392      }      }
1393    
1394        /**
1395         * Returns the next Region of the instrument. You have to call
1396         * GetFirstRegion() once before you can use this method. By calling this
1397         * method multiple times it iterates through the available Regions.
1398         *
1399         * @returns  pointer address to the next region or NULL if end reached
1400         * @see      GetFirstRegion()
1401         * @deprecated  This method is not reentrant-safe, use GetRegionAt()
1402         *              instead.
1403         */
1404      Region* Instrument::GetNextRegion() {      Region* Instrument::GetNextRegion() {
1405          if (!pRegions) return NULL;          if (!pRegions) return NULL;
1406          RegionsIterator++;          RegionsIterator++;
# Line 1258  namespace DLS { Line 1412  namespace DLS {
1412          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1413          if (lrgn) {          if (lrgn) {
1414              uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2              uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2
1415              RIFF::List* rgn = lrgn->GetFirstSubList();              size_t i = 0;
1416              while (rgn) {              for (RIFF::List* rgn = lrgn->GetSubListAt(i); rgn;
1417                     rgn = lrgn->GetSubListAt(++i))
1418                {
1419                  if (rgn->GetListType() == regionCkType) {                  if (rgn->GetListType() == regionCkType) {
1420                      pRegions->push_back(new Region(this, rgn));                      pRegions->push_back(new Region(this, rgn));
1421                  }                  }
                 rgn = lrgn->GetNextSubList();  
1422              }              }
1423          }          }
1424      }      }
# Line 1275  namespace DLS { Line 1430  namespace DLS {
1430          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
1431          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
1432          pRegions->push_back(pNewRegion);          pRegions->push_back(pNewRegion);
1433          Regions = pRegions->size();          Regions = (uint32_t) pRegions->size();
1434          return pNewRegion;          return pNewRegion;
1435      }      }
1436    
1437      void Instrument::MoveRegion(Region* pSrc, Region* pDst) {      void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
1438          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1439          lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));          lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));
1440            for (size_t i = 0; i < pRegions->size(); ++i) {
1441          pRegions->remove(pSrc);              if ((*pRegions)[i] == pSrc) {
1442          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);                  pRegions->erase(pRegions->begin() + i);
1443          pRegions->insert(iter, pSrc);                  RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
1444                    pRegions->insert(iter, pSrc);
1445                }
1446            }
1447      }      }
1448    
1449      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
# Line 1293  namespace DLS { Line 1451  namespace DLS {
1451          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
1452          if (iter == pRegions->end()) return;          if (iter == pRegions->end()) return;
1453          pRegions->erase(iter);          pRegions->erase(iter);
1454          Regions = pRegions->size();          Regions = (uint32_t) pRegions->size();
1455            pRegion->DeleteChunks();
1456          delete pRegion;          delete pRegion;
1457      }      }
1458    
# Line 1313  namespace DLS { Line 1472  namespace DLS {
1472          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
1473          uint8_t* pData = (uint8_t*) insh->LoadChunkData();          uint8_t* pData = (uint8_t*) insh->LoadChunkData();
1474          // update 'insh' chunk          // update 'insh' chunk
1475          Regions = (pRegions) ? pRegions->size() : 0;          Regions = (pRegions) ? uint32_t(pRegions->size()) : 0;
1476          midi_locale_t locale;          midi_locale_t locale;
1477          locale.instrument = MIDIProgram;          locale.instrument = MIDIProgram;
1478          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
# Line 1327  namespace DLS { Line 1486  namespace DLS {
1486          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
1487          RegionList::iterator end  = pRegions->end();          RegionList::iterator end  = pRegions->end();
1488          for (int i = 0; iter != end; ++iter, ++i) {          for (int i = 0; iter != end; ++iter, ++i) {
1489              // divide local progress into subprogress              if (pProgress) {
1490              progress_t subprogress;                  // divide local progress into subprogress
1491              __divide_progress(pProgress, &subprogress, pRegions->size(), i);                  progress_t subprogress;
1492              // do the actual work                  __divide_progress(pProgress, &subprogress, pRegions->size(), i);
1493              (*iter)->UpdateChunks(&subprogress);                  // do the actual work
1494                    (*iter)->UpdateChunks(&subprogress);
1495                } else
1496                    (*iter)->UpdateChunks(NULL);
1497          }          }
1498          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1499                __notify_progress(pProgress, 1.0); // notify done
1500      }      }
1501    
1502      /** @brief Destructor.      /** @brief Destructor.
1503       *       *
1504       * Removes RIFF chunks associated with this Instrument and frees all       * Frees all memory occupied by this instrument.
      * memory occupied by this instrument.  
1505       */       */
1506      Instrument::~Instrument() {      Instrument::~Instrument() {
1507          if (pRegions) {          if (pRegions) {
# Line 1351  namespace DLS { Line 1513  namespace DLS {
1513              }              }
1514              delete pRegions;              delete pRegions;
1515          }          }
         // remove instrument's chunks  
         RIFF::List* pParent = pCkInstrument->GetParent();  
         pParent->DeleteSubChunk(pCkInstrument);  
1516      }      }
1517        
1518        /** @brief Remove all RIFF chunks associated with this Instrument object.
1519         *
1520         * See Storage::DeleteChunks() for details.
1521         */
1522        void Instrument::DeleteChunks() {
1523            // handle base classes
1524            Resource::DeleteChunks();
1525            Articulator::DeleteChunks();
1526    
1527            // handle RIFF chunks of members
1528            if (pRegions) {
1529                RegionList::iterator it  = pRegions->begin();
1530                RegionList::iterator end = pRegions->end();
1531                for (; it != end; ++it)
1532                    (*it)->DeleteChunks();
1533            }
1534    
1535            // handle own RIFF chunks
1536            if (pCkInstrument) {
1537                RIFF::List* pParent = pCkInstrument->GetParent();
1538                pParent->DeleteSubChunk(pCkInstrument);
1539                pCkInstrument = NULL;
1540            }
1541        }
1542    
1543      void Instrument::CopyAssignCore(const Instrument* orig) {      void Instrument::CopyAssignCore(const Instrument* orig) {
1544          // handle base classes          // handle base classes
1545          Resource::CopyAssign(orig);          Resource::CopyAssign(orig);
# Line 1381  namespace DLS { Line 1565  namespace DLS {
1565      void Instrument::CopyAssign(const Instrument* orig) {      void Instrument::CopyAssign(const Instrument* orig) {
1566          CopyAssignCore(orig);          CopyAssignCore(orig);
1567          // delete all regions first          // delete all regions first
1568          while (Regions) DeleteRegion(GetFirstRegion());          while (Regions) DeleteRegion(GetRegionAt(0));
1569          // now recreate and copy regions          // now recreate and copy regions
1570          {          {
1571              RegionList::const_iterator it = orig->pRegions->begin();              RegionList::const_iterator it = orig->pRegions->begin();
# Line 1405  namespace DLS { Line 1589  namespace DLS {
1589       */       */
1590      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1591          pRIFF->SetByteOrder(RIFF::endian_little);          pRIFF->SetByteOrder(RIFF::endian_little);
1592            bOwningRiff = true;
1593          pVersion = new version_t;          pVersion = new version_t;
1594          pVersion->major   = 0;          pVersion->major   = 0;
1595          pVersion->minor   = 0;          pVersion->minor   = 0;
# Line 1435  namespace DLS { Line 1620  namespace DLS {
1620      File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {      File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1621          if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");          if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1622          this->pRIFF = pRIFF;          this->pRIFF = pRIFF;
1623            bOwningRiff = false;
1624          RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);          RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1625          if (ckVersion) {          if (ckVersion) {
1626                ckVersion->SetPos(0);
1627    
1628              pVersion = new version_t;              pVersion = new version_t;
1629              ckVersion->Read(pVersion, 4, 2);              ckVersion->Read(pVersion, 4, 2);
1630          }          }
# Line 1445  namespace DLS { Line 1632  namespace DLS {
1632    
1633          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1634          if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found.");          if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found.");
1635            colh->SetPos(0);
1636          Instruments = colh->ReadUint32();          Instruments = colh->ReadUint32();
1637    
1638          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
# Line 1455  namespace DLS { Line 1643  namespace DLS {
1643              WavePoolHeaderSize = 8;              WavePoolHeaderSize = 8;
1644              b64BitWavePoolOffsets = false;              b64BitWavePoolOffsets = false;
1645          } else {          } else {
1646                ptbl->SetPos(0);
1647    
1648              WavePoolHeaderSize = ptbl->ReadUint32();              WavePoolHeaderSize = ptbl->ReadUint32();
1649              WavePoolCount  = ptbl->ReadUint32();              WavePoolCount  = ptbl->ReadUint32();
1650              pWavePoolTable = new uint32_t[WavePoolCount];              pWavePoolTable = new uint32_t[WavePoolCount];
# Line 1507  namespace DLS { Line 1697  namespace DLS {
1697          if (pVersion) delete pVersion;          if (pVersion) delete pVersion;
1698          for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)          for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)
1699              delete *i;              delete *i;
1700            if (bOwningRiff)
1701                delete pRIFF;
1702      }      }
1703    
1704        /**
1705         * Returns Sample object of @a index.
1706         *
1707         * @param index - position of sample in sample list (0..n)
1708         * @returns sample object or NULL if index is out of bounds
1709         */
1710        Sample* File::GetSample(size_t index) {
1711            if (!pSamples) LoadSamples();
1712            if (!pSamples) return NULL;
1713            if (index >= pSamples->size()) return NULL;
1714            return (*pSamples)[index];
1715        }
1716    
1717        /**
1718         * Returns a pointer to the first <i>Sample</i> object of the file,
1719         * <i>NULL</i> otherwise.
1720         *
1721         * @deprecated  This method is not reentrant-safe, use GetSample()
1722         *              instead.
1723         */
1724      Sample* File::GetFirstSample() {      Sample* File::GetFirstSample() {
1725          if (!pSamples) LoadSamples();          if (!pSamples) LoadSamples();
1726          if (!pSamples) return NULL;          if (!pSamples) return NULL;
# Line 1516  namespace DLS { Line 1728  namespace DLS {
1728          return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;          return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1729      }      }
1730    
1731        /**
1732         * Returns a pointer to the next <i>Sample</i> object of the file,
1733         * <i>NULL</i> otherwise.
1734         *
1735         * @deprecated  This method is not reentrant-safe, use GetSample()
1736         *              instead.
1737         */
1738      Sample* File::GetNextSample() {      Sample* File::GetNextSample() {
1739          if (!pSamples) return NULL;          if (!pSamples) return NULL;
1740          SamplesIterator++;          SamplesIterator++;
# Line 1526  namespace DLS { Line 1745  namespace DLS {
1745          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
1746          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1747          if (wvpl) {          if (wvpl) {
1748              unsigned long wvplFileOffset = wvpl->GetFilePos();              file_offset_t wvplFileOffset = wvpl->GetFilePos() -
1749              RIFF::List* wave = wvpl->GetFirstSubList();                                             wvpl->GetPos(); // should be zero, but just to be sure
1750              while (wave) {              size_t i = 0;
1751                for (RIFF::List* wave = wvpl->GetSubListAt(i); wave;
1752                     wave = wvpl->GetSubListAt(++i))
1753                {
1754                  if (wave->GetListType() == LIST_TYPE_WAVE) {                  if (wave->GetListType() == LIST_TYPE_WAVE) {
1755                      unsigned long waveFileOffset = wave->GetFilePos();                      file_offset_t waveFileOffset = wave->GetFilePos() -
1756                                                       wave->GetPos(); // should be zero, but just to be sure
1757                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1758                  }                  }
                 wave = wvpl->GetNextSubList();  
1759              }              }
1760          }          }
1761          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)
1762              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1763              if (dwpl) {              if (dwpl) {
1764                  unsigned long dwplFileOffset = dwpl->GetFilePos();                  file_offset_t dwplFileOffset = dwpl->GetFilePos() -
1765                  RIFF::List* wave = dwpl->GetFirstSubList();                                                 dwpl->GetPos(); // should be zero, but just to be sure
1766                  while (wave) {                  size_t i = 0;
1767                    for (RIFF::List* wave = dwpl->GetSubListAt(i); wave;
1768                         wave = dwpl->GetSubListAt(++i))
1769                    {
1770                      if (wave->GetListType() == LIST_TYPE_WAVE) {                      if (wave->GetListType() == LIST_TYPE_WAVE) {
1771                          unsigned long waveFileOffset = wave->GetFilePos();                          file_offset_t waveFileOffset = wave->GetFilePos() -
1772                                                           wave->GetPos(); // should be zero, but just to be sure
1773                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1774                      }                      }
                     wave = dwpl->GetNextSubList();  
1775                  }                  }
1776              }              }
1777          }          }
# Line 1582  namespace DLS { Line 1807  namespace DLS {
1807          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1808          if (iter == pSamples->end()) return;          if (iter == pSamples->end()) return;
1809          pSamples->erase(iter);          pSamples->erase(iter);
1810            pSample->DeleteChunks();
1811          delete pSample;          delete pSample;
1812      }      }
1813    
# Line 1602  namespace DLS { Line 1828  namespace DLS {
1828          if (!pInstruments) pInstruments = new InstrumentList;          if (!pInstruments) pInstruments = new InstrumentList;
1829          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1830          if (lstInstruments) {          if (lstInstruments) {
1831              RIFF::List* lstInstr = lstInstruments->GetFirstSubList();              size_t i = 0;
1832              while (lstInstr) {              for (RIFF::List* lstInstr = lstInstruments->GetSubListAt(i);
1833                     lstInstr; lstInstr = lstInstruments->GetSubListAt(++i))
1834                {
1835                  if (lstInstr->GetListType() == LIST_TYPE_INS) {                  if (lstInstr->GetListType() == LIST_TYPE_INS) {
1836                      pInstruments->push_back(new Instrument(this, lstInstr));                      pInstruments->push_back(new Instrument(this, lstInstr));
1837                  }                  }
                 lstInstr = lstInstruments->GetNextSubList();  
1838              }              }
1839          }          }
1840      }      }
# Line 1641  namespace DLS { Line 1868  namespace DLS {
1868          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1869          if (iter == pInstruments->end()) return;          if (iter == pInstruments->end()) return;
1870          pInstruments->erase(iter);          pInstruments->erase(iter);
1871            pInstrument->DeleteChunks();
1872          delete pInstrument;          delete pInstrument;
1873      }      }
1874    
1875      /**      /**
1876         * Returns the underlying RIFF::File used for persistency of this DLS::File
1877         * object.
1878         */
1879        RIFF::File* File::GetRiffFile() {
1880            return pRIFF;
1881        }
1882    
1883        /**
1884       * Returns extension file of given index. Extension files are used       * Returns extension file of given index. Extension files are used
1885       * sometimes to circumvent the 2 GB file size limit of the RIFF format and       * sometimes to circumvent the 2 GB file size limit of the RIFF format and
1886       * of certain operating systems in general. In this case, instead of just       * of certain operating systems in general. In this case, instead of just
# Line 1709  namespace DLS { Line 1945  namespace DLS {
1945          }          }
1946    
1947          // update 'colh' chunk          // update 'colh' chunk
1948          Instruments = (pInstruments) ? pInstruments->size() : 0;          Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0;
1949          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1950          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1951          uint8_t* pData = (uint8_t*) colh->LoadChunkData();          uint8_t* pData = (uint8_t*) colh->LoadChunkData();
# Line 1717  namespace DLS { Line 1953  namespace DLS {
1953    
1954          // update instrument's chunks          // update instrument's chunks
1955          if (pInstruments) {          if (pInstruments) {
1956              // divide local progress into subprogress              if (pProgress) {
1957              progress_t subprogress;                  // divide local progress into subprogress
1958              __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress                  progress_t subprogress;
1959                    __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress
1960    
             // 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);  
1961                  // do the actual work                  // do the actual work
1962                  (*iter)->UpdateChunks(&subsubprogress);                  InstrumentList::iterator iter = pInstruments->begin();
1963              }                  InstrumentList::iterator end  = pInstruments->end();
1964                    for (int i = 0; iter != end; ++iter, ++i) {
1965                        // divide subprogress into sub-subprogress
1966                        progress_t subsubprogress;
1967                        __divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i);
1968                        // do the actual work
1969                        (*iter)->UpdateChunks(&subsubprogress);
1970                    }
1971    
1972              __notify_progress(&subprogress, 1.0); // notify subprogress done                  __notify_progress(&subprogress, 1.0); // notify subprogress done
1973                } else {
1974                    InstrumentList::iterator iter = pInstruments->begin();
1975                    InstrumentList::iterator end  = pInstruments->end();
1976                    for (int i = 0; iter != end; ++iter, ++i) {
1977                        (*iter)->UpdateChunks(NULL);
1978                    }
1979                }
1980          }          }
1981    
1982          // update 'ptbl' chunk          // update 'ptbl' chunk
1983          const int iSamples = (pSamples) ? pSamples->size() : 0;          const int iSamples = (pSamples) ? int(pSamples->size()) : 0;
1984          const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1985          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1986          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*/);
1987          const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;          int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1988          ptbl->Resize(iPtblSize);          ptbl->Resize(iPtblSize);
1989          pData = (uint8_t*) ptbl->LoadChunkData();          pData = (uint8_t*) ptbl->LoadChunkData();
1990          WavePoolCount = iSamples;          WavePoolCount = iSamples;
# Line 1750  namespace DLS { Line 1994  namespace DLS {
1994    
1995          // update sample's chunks          // update sample's chunks
1996          if (pSamples) {          if (pSamples) {
1997              // divide local progress into subprogress              if (pProgress) {
1998              progress_t subprogress;                  // divide local progress into subprogress
1999              __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress                  progress_t subprogress;
2000                    __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress
2001    
             // 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);  
2002                  // do the actual work                  // do the actual work
2003                  (*iter)->UpdateChunks(&subsubprogress);                  SampleList::iterator iter = pSamples->begin();
2004                    SampleList::iterator end  = pSamples->end();
2005                    for (int i = 0; iter != end; ++iter, ++i) {
2006                        // divide subprogress into sub-subprogress
2007                        progress_t subsubprogress;
2008                        __divide_progress(&subprogress, &subsubprogress, pSamples->size(), i);
2009                        // do the actual work
2010                        (*iter)->UpdateChunks(&subsubprogress);
2011                    }
2012    
2013                    __notify_progress(&subprogress, 1.0); // notify subprogress done
2014                } else {
2015                    SampleList::iterator iter = pSamples->begin();
2016                    SampleList::iterator end  = pSamples->end();
2017                    for (int i = 0; iter != end; ++iter, ++i) {
2018                        (*iter)->UpdateChunks(NULL);
2019                    }
2020              }              }
2021            }
2022    
2023              __notify_progress(&subprogress, 1.0); // notify subprogress done          // if there are any extension files, gather which ones are regular
2024            // extension files used as wave pool files (.gx00, .gx01, ... , .gx98)
2025            // and which one is probably a convolution (GigaPulse) file (always to
2026            // be saved as .gx99)
2027            std::list<RIFF::File*> poolFiles;  // < for (.gx00, .gx01, ... , .gx98) files
2028            RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file
2029            if (!ExtensionFiles.empty()) {
2030                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2031                for (; it != ExtensionFiles.end(); ++it) {
2032                    //FIXME: the .gx99 file is always used by GSt for convolution
2033                    // data (GigaPulse); so we should better detect by subchunk
2034                    // whether the extension file is intended for convolution
2035                    // instead of checkking for a file name, because the latter does
2036                    // not work for saving new gigs created from scratch
2037                    const std::string oldName = (*it)->GetFileName();
2038                    const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2039                    if (isGigaPulseFile)
2040                        pGigaPulseFile = *it;
2041                    else
2042                        poolFiles.push_back(*it);
2043                }
2044          }          }
2045    
2046          __notify_progress(pProgress, 1.0); // notify done          // update the 'xfil' chunk which describes all extension files (wave
2047            // pool files) except the .gx99 file
2048            if (!poolFiles.empty()) {
2049                const int n = poolFiles.size();
2050                const int iHeaderSize = 4;
2051                const int iEntrySize = 144;
2052    
2053                // make sure chunk exists, and with correct size
2054                RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2055                if (ckXfil)
2056                    ckXfil->Resize(iHeaderSize + n * iEntrySize);
2057                else
2058                    ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize);
2059    
2060                uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData();
2061    
2062                // re-assemble the chunk's content
2063                store32(pData, n);
2064                std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin();
2065                for (int i = 0, iOffset = 4; i < n;
2066                     ++itExtFile, ++i, iOffset += iEntrySize)
2067                {
2068                    // update the filename string and 5 byte extension of each extension file
2069                    std::string file = lastPathComponent(
2070                        (*itExtFile)->GetFileName()
2071                    );
2072                    if (file.length() + 6 > 128)
2073                        throw Exception("Fatal error, extension filename length exceeds 122 byte maximum");
2074                    uint8_t* pStrings = &pData[iOffset];
2075                    memset(pStrings, 0, 128);
2076                    memcpy(pStrings, file.c_str(), file.length());
2077                    pStrings += file.length() + 1;
2078                    std::string ext = file.substr(file.length()-5);
2079                    memcpy(pStrings, ext.c_str(), 5);
2080                    // update the dlsid of the extension file
2081                    uint8_t* pId = &pData[iOffset + 128];
2082                    dlsid_t id;
2083                    RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID);
2084                    if (ckDLSID) {
2085                        ckDLSID->Read(&id.ulData1, 1, 4);
2086                        ckDLSID->Read(&id.usData2, 1, 2);
2087                        ckDLSID->Read(&id.usData3, 1, 2);
2088                        ckDLSID->Read(id.abData, 8, 1);
2089                    } else {
2090                        ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16);
2091                        Resource::GenerateDLSID(&id);
2092                        uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
2093                        store32(&pData[0], id.ulData1);
2094                        store16(&pData[4], id.usData2);
2095                        store16(&pData[6], id.usData3);
2096                        memcpy(&pData[8], id.abData, 8);
2097                    }
2098                    store32(&pId[0], id.ulData1);
2099                    store16(&pId[4], id.usData2);
2100                    store16(&pId[6], id.usData3);
2101                    memcpy(&pId[8], id.abData, 8);
2102                }
2103            } else {
2104                // in case there was a 'xfil' chunk, remove it
2105                RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2106                if (ckXfil) pRIFF->DeleteSubChunk(ckXfil);
2107            }
2108    
2109            // update the 'doxf' chunk which describes a .gx99 extension file
2110            // which contains convolution data (GigaPulse)
2111            if (pGigaPulseFile) {
2112                RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2113                if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148);
2114    
2115                uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData();
2116    
2117                // update the dlsid from the extension file
2118                uint8_t* pId = &pData[132];
2119                RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID);
2120                if (!ckDLSID) { //TODO: auto generate DLS ID if missing
2121                    throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk");
2122                } else {
2123                    dlsid_t id;
2124                    // read DLS ID from extension files's DLS ID chunk
2125                    uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData();
2126                    id.ulData1 = load32(&pData[0]);
2127                    id.usData2 = load16(&pData[4]);
2128                    id.usData3 = load16(&pData[6]);
2129                    memcpy(id.abData, &pData[8], 8);
2130                    // store DLS ID to 'doxf' chunk
2131                    store32(&pId[0], id.ulData1);
2132                    store16(&pId[4], id.usData2);
2133                    store16(&pId[6], id.usData3);
2134                    memcpy(&pId[8], id.abData, 8);
2135                }
2136            } else {
2137                // in case there was a 'doxf' chunk, remove it
2138                RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2139                if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf);
2140            }
2141    
2142            // the RIFF file to be written might now been grown >= 4GB or might
2143            // been shrunk < 4GB, so we might need to update the wave pool offset
2144            // size and thus accordingly we would need to resize the wave pool
2145            // chunk
2146            const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();
2147            const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file
2148                                         poolFiles.size() > 0;        // < 32 bit gig file where the hi 32 bits are used as extension file nr
2149            if (b64BitWavePoolOffsets != bRequires64Bit) {
2150                b64BitWavePoolOffsets = bRequires64Bit;
2151                iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2152                iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
2153                ptbl->Resize(iPtblSize);
2154            }
2155    
2156            if (pProgress)
2157                __notify_progress(pProgress, 1.0); // notify done
2158      }      }
2159    
2160      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1786  namespace DLS { Line 2172  namespace DLS {
2172       * @param pProgress - optional: callback function for progress notification       * @param pProgress - optional: callback function for progress notification
2173       */       */
2174      void File::Save(const String& Path, progress_t* pProgress) {      void File::Save(const String& Path, progress_t* pProgress) {
2175          {          // calculate number of tasks to notify progress appropriately
2176            const size_t nExtFiles = ExtensionFiles.size();
2177            const float tasks = 2.f + nExtFiles;
2178    
2179            // save extension files (if required)
2180            if (!ExtensionFiles.empty()) {
2181                // for assembling path of extension files to be saved to
2182                const std::string baseName = pathWithoutExtension(Path);
2183                // save the individual extension files
2184                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2185                for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2186                    //FIXME: the .gx99 file is always used by GSt for convolution
2187                    // data (GigaPulse); so we should better detect by subchunk
2188                    // whether the extension file is intended for convolution
2189                    // instead of checkking for a file name, because the latter does
2190                    // not work for saving new gigs created from scratch
2191                    const std::string oldName = (*it)->GetFileName();
2192                    const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2193                    std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1);
2194                    std::string newPath = baseName + ext;
2195                    // save extension file to its new location
2196                    if (pProgress) {
2197                         // divide local progress into subprogress
2198                        progress_t subprogress;
2199                        __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2200                        // do the actual work
2201                        (*it)->Save(newPath, &subprogress);
2202                    } else
2203                        (*it)->Save(newPath);
2204                }
2205            }
2206    
2207            if (pProgress) {
2208              // divide local progress into subprogress              // divide local progress into subprogress
2209              progress_t subprogress;              progress_t subprogress;
2210              __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress              __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2211              // do the actual work              // do the actual work
2212              UpdateChunks(&subprogress);              UpdateChunks(&subprogress);
2213                        } else
2214          }              UpdateChunks(NULL);
2215          {  
2216            if (pProgress) {
2217              // divide local progress into subprogress              // divide local progress into subprogress
2218              progress_t subprogress;              progress_t subprogress;
2219              __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress              __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2220              // do the actual work              // do the actual work
2221              pRIFF->Save(Path, &subprogress);              pRIFF->Save(Path, &subprogress);
2222          }          } else
2223                pRIFF->Save(Path);
2224    
2225          UpdateFileOffsets();          UpdateFileOffsets();
2226          __notify_progress(pProgress, 1.0); // notify done  
2227            if (pProgress)
2228                __notify_progress(pProgress, 1.0); // notify done
2229      }      }
2230    
2231      /** @brief Save changes to same file.      /** @brief Save changes to same file.
# Line 1812  namespace DLS { Line 2235  namespace DLS {
2235       * have at the end of the saving process.       * have at the end of the saving process.
2236       *       *
2237       * @param pProgress - optional: callback function for progress notification       * @param pProgress - optional: callback function for progress notification
2238       * @throws RIFF::Exception if any kind of IO error occured       * @throws RIFF::Exception if any kind of IO error occurred
2239       * @throws DLS::Exception  if any kind of DLS specific error occured       * @throws DLS::Exception  if any kind of DLS specific error occurred
2240       */       */
2241      void File::Save(progress_t* pProgress) {      void File::Save(progress_t* pProgress) {
2242          {          // calculate number of tasks to notify progress appropriately
2243            const size_t nExtFiles = ExtensionFiles.size();
2244            const float tasks = 2.f + nExtFiles;
2245    
2246            // save extension files (if required)
2247            if (!ExtensionFiles.empty()) {
2248                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2249                for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2250                    // save extension file
2251                    if (pProgress) {
2252                        // divide local progress into subprogress
2253                        progress_t subprogress;
2254                        __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2255                        // do the actual work
2256                        (*it)->Save(&subprogress);
2257                    } else
2258                        (*it)->Save();
2259                }
2260            }
2261    
2262            if (pProgress) {
2263              // divide local progress into subprogress              // divide local progress into subprogress
2264              progress_t subprogress;              progress_t subprogress;
2265              __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress              __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2266              // do the actual work              // do the actual work
2267              UpdateChunks(&subprogress);              UpdateChunks(&subprogress);
2268          }          } else
2269          {              UpdateChunks(NULL);
2270    
2271            if (pProgress) {
2272              // divide local progress into subprogress              // divide local progress into subprogress
2273              progress_t subprogress;              progress_t subprogress;
2274              __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress              __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2275              // do the actual work              // do the actual work
2276              pRIFF->Save(&subprogress);              pRIFF->Save(&subprogress);
2277          }          } else
2278                pRIFF->Save();
2279    
2280          UpdateFileOffsets();          UpdateFileOffsets();
2281          __notify_progress(pProgress, 1.0); // notify done  
2282            if (pProgress)
2283                __notify_progress(pProgress, 1.0); // notify done
2284      }      }
2285    
2286      /** @brief Updates all file offsets stored all over the file.      /** @brief Updates all file offsets stored all over the file.
# Line 1882  namespace DLS { Line 2331  namespace DLS {
2331          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2332          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2333          // check if 'ptbl' chunk is large enough          // check if 'ptbl' chunk is large enough
2334          WavePoolCount = (pSamples) ? pSamples->size() : 0;          WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2335          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;          const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
2336          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
2337          // save the 'ptbl' chunk's current read/write position          // save the 'ptbl' chunk's current read/write position
2338          unsigned long ulOriginalPos = ptbl->GetPos();          file_offset_t ullOriginalPos = ptbl->GetPos();
2339          // update headers          // update headers
2340          ptbl->SetPos(0);          ptbl->SetPos(0);
2341          uint32_t tmp = WavePoolHeaderSize;          uint32_t tmp = WavePoolHeaderSize;
# Line 1909  namespace DLS { Line 2358  namespace DLS {
2358              }              }
2359          }          }
2360          // restore 'ptbl' chunk's original read/write position          // restore 'ptbl' chunk's original read/write position
2361          ptbl->SetPos(ulOriginalPos);          ptbl->SetPos(ullOriginalPos);
2362      }      }
2363    
2364      /**      /**
# Line 1918  namespace DLS { Line 2367  namespace DLS {
2367       * exists already.       * exists already.
2368       */       */
2369      void File::__UpdateWavePoolTable() {      void File::__UpdateWavePoolTable() {
2370          WavePoolCount = (pSamples) ? pSamples->size() : 0;          WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2371          // resize wave pool table arrays          // resize wave pool table arrays
2372          if (pWavePoolTable)   delete[] pWavePoolTable;          if (pWavePoolTable)   delete[] pWavePoolTable;
2373          if (pWavePoolTableHi) delete[] pWavePoolTableHi;          if (pWavePoolTableHi) delete[] pWavePoolTableHi;
2374          pWavePoolTable   = new uint32_t[WavePoolCount];          pWavePoolTable   = new uint32_t[WavePoolCount];
2375          pWavePoolTableHi = new uint32_t[WavePoolCount];          pWavePoolTableHi = new uint32_t[WavePoolCount];
2376          if (!pSamples) return;          if (!pSamples) return;
2377          // update offsets int wave pool table          // update offsets in wave pool table
2378          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2379          uint64_t wvplFileOffset = wvpl->GetFilePos();          uint64_t wvplFileOffset = wvpl->GetFilePos() -
2380          if (b64BitWavePoolOffsets) {                                    wvpl->GetPos(); // mandatory, since position might have changed
2381              SampleList::iterator iter = pSamples->begin();          if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ...
             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  
2382              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
2383              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
2384              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
2385                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;                  uint64_t _64BitOffset =
2386                  (*iter)->ulWavePoolOffset = _64BitOffset;                      (*iter)->pWaveList->GetFilePos() -
2387                        (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2388                        wvplFileOffset -
2389                        LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2390                    (*iter)->ullWavePoolOffset = _64BitOffset;
2391                  pWavePoolTable[i] = (uint32_t) _64BitOffset;                  pWavePoolTable[i] = (uint32_t) _64BitOffset;
2392              }              }
2393            } else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ...
2394                if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ...
2395                    SampleList::iterator iter = pSamples->begin();
2396                    SampleList::iterator end  = pSamples->end();
2397                    for (int i = 0 ; iter != end ; ++iter, i++) {
2398                        uint64_t _64BitOffset =
2399                            (*iter)->pWaveList->GetFilePos() -
2400                            (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2401                            wvplFileOffset -
2402                            LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2403                        (*iter)->ullWavePoolOffset = _64BitOffset;
2404                        pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
2405                        pWavePoolTable[i]   = (uint32_t) _64BitOffset;
2406                    }
2407                } else { // 32 bit offsets with extension files (GigaStudio legacy support) ...
2408                    // the main gig and the extension files may contain wave data
2409                    std::vector<RIFF::File*> poolFiles;
2410                    poolFiles.push_back(pRIFF);
2411                    poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end());
2412    
2413                    RIFF::File* pCurPoolFile = NULL;
2414                    int fileNo = 0;
2415                    int waveOffset = 0;
2416                    SampleList::iterator iter = pSamples->begin();
2417                    SampleList::iterator end  = pSamples->end();
2418                    for (int i = 0 ; iter != end ; ++iter, i++) {
2419                        RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile();
2420                        // if this sample is located in the same pool file as the
2421                        // last we reuse the previously computed fileNo and waveOffset
2422                        if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ...
2423                            pCurPoolFile = pPoolFile;
2424    
2425                            std::vector<RIFF::File*>::iterator sIter;
2426                            sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile);
2427                            if (sIter != poolFiles.end())
2428                                fileNo = std::distance(poolFiles.begin(), sIter);
2429                            else
2430                                throw DLS::Exception("Fatal error, unknown pool file");
2431    
2432                            RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL);
2433                            if (!extWvpl)
2434                                throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk");
2435                            waveOffset =
2436                                extWvpl->GetFilePos() -
2437                                extWvpl->GetPos() + // mandatory, since position might have changed
2438                                LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize());
2439                        }
2440                        uint64_t _64BitOffset =
2441                            (*iter)->pWaveList->GetFilePos() -
2442                            (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2443                            waveOffset;
2444                        // pWavePoolTableHi stores file number when extension files are in use
2445                        pWavePoolTableHi[i] = (uint32_t) fileNo;
2446                        pWavePoolTable[i]   = (uint32_t) _64BitOffset;
2447                        (*iter)->ullWavePoolOffset = _64BitOffset;
2448                    }
2449                }
2450          }          }
2451      }      }
2452    
2453    
   
2454  // *************** Exception ***************  // *************** Exception ***************
2455  // *  // *
2456    
2457      Exception::Exception(String Message) : RIFF::Exception(Message) {      Exception::Exception() : RIFF::Exception() {
2458        }
2459    
2460        Exception::Exception(String format, ...) : RIFF::Exception() {
2461            va_list arg;
2462            va_start(arg, format);
2463            Message = assemble(format, arg);
2464            va_end(arg);
2465        }
2466    
2467        Exception::Exception(String format, va_list arg) : RIFF::Exception() {
2468            Message = assemble(format, arg);
2469      }      }
2470    
2471      void Exception::PrintMessage() {      void Exception::PrintMessage() {

Legend:
Removed from v.2909  
changed lines
  Added in v.3929

  ViewVC Help
Powered by ViewVC