/[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 3198 by schoenebeck, Sun May 21 12:46:05 2017 UTC revision 3928 by schoenebeck, Tue Jun 15 11:38:38 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-2017 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 717  namespace DLS { Line 791  namespace DLS {
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 999  namespace DLS { Line 1092  namespace DLS {
1092          // articulation information          // 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 1021  namespace DLS { Line 1116  namespace DLS {
1116          // sample information          // 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 1039  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() {
# Line 1225  namespace DLS { Line 1339  namespace DLS {
1339          midi_locale_t locale;          midi_locale_t locale;
1340          RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);          RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1341          if (insh) {          if (insh) {
1342                insh->SetPos(0);
1343    
1344              Regions = insh->ReadUint32();              Regions = insh->ReadUint32();
1345              insh->Read(&locale, 2, 4);              insh->Read(&locale, 2, 4);
1346          } else { // 'insh' chunk missing          } else { // 'insh' chunk missing
# Line 1242  namespace DLS { Line 1358  namespace DLS {
1358          pRegions = NULL;          pRegions = NULL;
1359      }      }
1360    
1361        /**
1362         * Returns Region at supplied @a pos position within the region list of
1363         * this instrument. If supplied @a pos is out of bounds then @c NULL is
1364         * returned.
1365         *
1366         * @param pos - position of sought Region in region list
1367         * @returns pointer address to requested region or @c NULL if @a pos is
1368         *          out of bounds
1369         */
1370        Region* Instrument::GetRegionAt(size_t pos) {
1371            if (!pRegions) LoadRegions();
1372            if (!pRegions) return NULL;
1373            if (pos >= pRegions->size()) return NULL;
1374            return (*pRegions)[pos];
1375        }
1376    
1377        /**
1378         * Returns the first Region of the instrument. You have to call this
1379         * method once before you use GetNextRegion().
1380         *
1381         * @returns  pointer address to first region or NULL if there is none
1382         * @see      GetNextRegion()
1383         * @deprecated  This method is not reentrant-safe, use GetRegionAt()
1384         *              instead.
1385         */
1386      Region* Instrument::GetFirstRegion() {      Region* Instrument::GetFirstRegion() {
1387          if (!pRegions) LoadRegions();          if (!pRegions) LoadRegions();
1388          if (!pRegions) return NULL;          if (!pRegions) return NULL;
# Line 1249  namespace DLS { Line 1390  namespace DLS {
1390          return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;          return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1391      }      }
1392    
1393        /**
1394         * Returns the next Region of the instrument. You have to call
1395         * GetFirstRegion() once before you can use this method. By calling this
1396         * method multiple times it iterates through the available Regions.
1397         *
1398         * @returns  pointer address to the next region or NULL if end reached
1399         * @see      GetFirstRegion()
1400         * @deprecated  This method is not reentrant-safe, use GetRegionAt()
1401         *              instead.
1402         */
1403      Region* Instrument::GetNextRegion() {      Region* Instrument::GetNextRegion() {
1404          if (!pRegions) return NULL;          if (!pRegions) return NULL;
1405          RegionsIterator++;          RegionsIterator++;
# Line 1260  namespace DLS { Line 1411  namespace DLS {
1411          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1412          if (lrgn) {          if (lrgn) {
1413              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
1414              RIFF::List* rgn = lrgn->GetFirstSubList();              size_t i = 0;
1415              while (rgn) {              for (RIFF::List* rgn = lrgn->GetSubListAt(i); rgn;
1416                     rgn = lrgn->GetSubListAt(++i))
1417                {
1418                  if (rgn->GetListType() == regionCkType) {                  if (rgn->GetListType() == regionCkType) {
1419                      pRegions->push_back(new Region(this, rgn));                      pRegions->push_back(new Region(this, rgn));
1420                  }                  }
                 rgn = lrgn->GetNextSubList();  
1421              }              }
1422          }          }
1423      }      }
# Line 1284  namespace DLS { Line 1436  namespace DLS {
1436      void Instrument::MoveRegion(Region* pSrc, Region* pDst) {      void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
1437          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1438          lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));          lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));
1439            for (size_t i = 0; i < pRegions->size(); ++i) {
1440          pRegions->remove(pSrc);              if ((*pRegions)[i] == pSrc) {
1441          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);                  pRegions->erase(pRegions->begin() + i);
1442          pRegions->insert(iter, pSrc);                  RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
1443                    pRegions->insert(iter, pSrc);
1444                }
1445            }
1446      }      }
1447    
1448      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
# Line 1296  namespace DLS { Line 1451  namespace DLS {
1451          if (iter == pRegions->end()) return;          if (iter == pRegions->end()) return;
1452          pRegions->erase(iter);          pRegions->erase(iter);
1453          Regions = (uint32_t) pRegions->size();          Regions = (uint32_t) pRegions->size();
1454            pRegion->DeleteChunks();
1455          delete pRegion;          delete pRegion;
1456      }      }
1457    
# Line 1329  namespace DLS { Line 1485  namespace DLS {
1485          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
1486          RegionList::iterator end  = pRegions->end();          RegionList::iterator end  = pRegions->end();
1487          for (int i = 0; iter != end; ++iter, ++i) {          for (int i = 0; iter != end; ++iter, ++i) {
1488              // divide local progress into subprogress              if (pProgress) {
1489              progress_t subprogress;                  // divide local progress into subprogress
1490              __divide_progress(pProgress, &subprogress, pRegions->size(), i);                  progress_t subprogress;
1491              // do the actual work                  __divide_progress(pProgress, &subprogress, pRegions->size(), i);
1492              (*iter)->UpdateChunks(&subprogress);                  // do the actual work
1493                    (*iter)->UpdateChunks(&subprogress);
1494                } else
1495                    (*iter)->UpdateChunks(NULL);
1496          }          }
1497          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1498                __notify_progress(pProgress, 1.0); // notify done
1499      }      }
1500    
1501      /** @brief Destructor.      /** @brief Destructor.
1502       *       *
1503       * Removes RIFF chunks associated with this Instrument and frees all       * Frees all memory occupied by this instrument.
      * memory occupied by this instrument.  
1504       */       */
1505      Instrument::~Instrument() {      Instrument::~Instrument() {
1506          if (pRegions) {          if (pRegions) {
# Line 1353  namespace DLS { Line 1512  namespace DLS {
1512              }              }
1513              delete pRegions;              delete pRegions;
1514          }          }
         // remove instrument's chunks  
         RIFF::List* pParent = pCkInstrument->GetParent();  
         pParent->DeleteSubChunk(pCkInstrument);  
1515      }      }
1516        
1517        /** @brief Remove all RIFF chunks associated with this Instrument object.
1518         *
1519         * See Storage::DeleteChunks() for details.
1520         */
1521        void Instrument::DeleteChunks() {
1522            // handle base classes
1523            Resource::DeleteChunks();
1524            Articulator::DeleteChunks();
1525    
1526            // handle RIFF chunks of members
1527            if (pRegions) {
1528                RegionList::iterator it  = pRegions->begin();
1529                RegionList::iterator end = pRegions->end();
1530                for (; it != end; ++it)
1531                    (*it)->DeleteChunks();
1532            }
1533    
1534            // handle own RIFF chunks
1535            if (pCkInstrument) {
1536                RIFF::List* pParent = pCkInstrument->GetParent();
1537                pParent->DeleteSubChunk(pCkInstrument);
1538                pCkInstrument = NULL;
1539            }
1540        }
1541    
1542      void Instrument::CopyAssignCore(const Instrument* orig) {      void Instrument::CopyAssignCore(const Instrument* orig) {
1543          // handle base classes          // handle base classes
1544          Resource::CopyAssign(orig);          Resource::CopyAssign(orig);
# Line 1383  namespace DLS { Line 1564  namespace DLS {
1564      void Instrument::CopyAssign(const Instrument* orig) {      void Instrument::CopyAssign(const Instrument* orig) {
1565          CopyAssignCore(orig);          CopyAssignCore(orig);
1566          // delete all regions first          // delete all regions first
1567          while (Regions) DeleteRegion(GetFirstRegion());          while (Regions) DeleteRegion(GetRegionAt(0));
1568          // now recreate and copy regions          // now recreate and copy regions
1569          {          {
1570              RegionList::const_iterator it = orig->pRegions->begin();              RegionList::const_iterator it = orig->pRegions->begin();
# Line 1407  namespace DLS { Line 1588  namespace DLS {
1588       */       */
1589      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1590          pRIFF->SetByteOrder(RIFF::endian_little);          pRIFF->SetByteOrder(RIFF::endian_little);
1591            bOwningRiff = true;
1592          pVersion = new version_t;          pVersion = new version_t;
1593          pVersion->major   = 0;          pVersion->major   = 0;
1594          pVersion->minor   = 0;          pVersion->minor   = 0;
# Line 1437  namespace DLS { Line 1619  namespace DLS {
1619      File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {      File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1620          if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");          if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1621          this->pRIFF = pRIFF;          this->pRIFF = pRIFF;
1622            bOwningRiff = false;
1623          RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);          RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1624          if (ckVersion) {          if (ckVersion) {
1625                ckVersion->SetPos(0);
1626    
1627              pVersion = new version_t;              pVersion = new version_t;
1628              ckVersion->Read(pVersion, 4, 2);              ckVersion->Read(pVersion, 4, 2);
1629          }          }
# Line 1447  namespace DLS { Line 1631  namespace DLS {
1631    
1632          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1633          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.");
1634            colh->SetPos(0);
1635          Instruments = colh->ReadUint32();          Instruments = colh->ReadUint32();
1636    
1637          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
# Line 1457  namespace DLS { Line 1642  namespace DLS {
1642              WavePoolHeaderSize = 8;              WavePoolHeaderSize = 8;
1643              b64BitWavePoolOffsets = false;              b64BitWavePoolOffsets = false;
1644          } else {          } else {
1645                ptbl->SetPos(0);
1646    
1647              WavePoolHeaderSize = ptbl->ReadUint32();              WavePoolHeaderSize = ptbl->ReadUint32();
1648              WavePoolCount  = ptbl->ReadUint32();              WavePoolCount  = ptbl->ReadUint32();
1649              pWavePoolTable = new uint32_t[WavePoolCount];              pWavePoolTable = new uint32_t[WavePoolCount];
# Line 1509  namespace DLS { Line 1696  namespace DLS {
1696          if (pVersion) delete pVersion;          if (pVersion) delete pVersion;
1697          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++)
1698              delete *i;              delete *i;
1699            if (bOwningRiff)
1700                delete pRIFF;
1701        }
1702    
1703        /**
1704         * Returns Sample object of @a index.
1705         *
1706         * @param index - position of sample in sample list (0..n)
1707         * @returns sample object or NULL if index is out of bounds
1708         */
1709        Sample* File::GetSample(size_t index) {
1710            if (!pSamples) LoadSamples();
1711            if (!pSamples) return NULL;
1712            if (index >= pSamples->size()) return NULL;
1713            return (*pSamples)[index];
1714      }      }
1715    
1716        /**
1717         * Returns a pointer to the first <i>Sample</i> object of the file,
1718         * <i>NULL</i> otherwise.
1719         *
1720         * @deprecated  This method is not reentrant-safe, use GetSample()
1721         *              instead.
1722         */
1723      Sample* File::GetFirstSample() {      Sample* File::GetFirstSample() {
1724          if (!pSamples) LoadSamples();          if (!pSamples) LoadSamples();
1725          if (!pSamples) return NULL;          if (!pSamples) return NULL;
# Line 1518  namespace DLS { Line 1727  namespace DLS {
1727          return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;          return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1728      }      }
1729    
1730        /**
1731         * Returns a pointer to the next <i>Sample</i> object of the file,
1732         * <i>NULL</i> otherwise.
1733         *
1734         * @deprecated  This method is not reentrant-safe, use GetSample()
1735         *              instead.
1736         */
1737      Sample* File::GetNextSample() {      Sample* File::GetNextSample() {
1738          if (!pSamples) return NULL;          if (!pSamples) return NULL;
1739          SamplesIterator++;          SamplesIterator++;
# Line 1528  namespace DLS { Line 1744  namespace DLS {
1744          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
1745          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1746          if (wvpl) {          if (wvpl) {
1747              file_offset_t wvplFileOffset = wvpl->GetFilePos();              file_offset_t wvplFileOffset = wvpl->GetFilePos() -
1748              RIFF::List* wave = wvpl->GetFirstSubList();                                             wvpl->GetPos(); // should be zero, but just to be sure
1749              while (wave) {              size_t i = 0;
1750                for (RIFF::List* wave = wvpl->GetSubListAt(i); wave;
1751                     wave = wvpl->GetSubListAt(++i))
1752                {
1753                  if (wave->GetListType() == LIST_TYPE_WAVE) {                  if (wave->GetListType() == LIST_TYPE_WAVE) {
1754                      file_offset_t waveFileOffset = wave->GetFilePos();                      file_offset_t waveFileOffset = wave->GetFilePos() -
1755                                                       wave->GetPos(); // should be zero, but just to be sure
1756                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1757                  }                  }
                 wave = wvpl->GetNextSubList();  
1758              }              }
1759          }          }
1760          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)
1761              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1762              if (dwpl) {              if (dwpl) {
1763                  file_offset_t dwplFileOffset = dwpl->GetFilePos();                  file_offset_t dwplFileOffset = dwpl->GetFilePos() -
1764                  RIFF::List* wave = dwpl->GetFirstSubList();                                                 dwpl->GetPos(); // should be zero, but just to be sure
1765                  while (wave) {                  size_t i = 0;
1766                    for (RIFF::List* wave = dwpl->GetSubListAt(i); wave;
1767                         wave = dwpl->GetSubListAt(++i))
1768                    {
1769                      if (wave->GetListType() == LIST_TYPE_WAVE) {                      if (wave->GetListType() == LIST_TYPE_WAVE) {
1770                          file_offset_t waveFileOffset = wave->GetFilePos();                          file_offset_t waveFileOffset = wave->GetFilePos() -
1771                                                           wave->GetPos(); // should be zero, but just to be sure
1772                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1773                      }                      }
                     wave = dwpl->GetNextSubList();  
1774                  }                  }
1775              }              }
1776          }          }
# Line 1584  namespace DLS { Line 1806  namespace DLS {
1806          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1807          if (iter == pSamples->end()) return;          if (iter == pSamples->end()) return;
1808          pSamples->erase(iter);          pSamples->erase(iter);
1809            pSample->DeleteChunks();
1810          delete pSample;          delete pSample;
1811      }      }
1812    
# Line 1604  namespace DLS { Line 1827  namespace DLS {
1827          if (!pInstruments) pInstruments = new InstrumentList;          if (!pInstruments) pInstruments = new InstrumentList;
1828          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1829          if (lstInstruments) {          if (lstInstruments) {
1830              RIFF::List* lstInstr = lstInstruments->GetFirstSubList();              size_t i = 0;
1831              while (lstInstr) {              for (RIFF::List* lstInstr = lstInstruments->GetSubListAt(i);
1832                     lstInstr; lstInstr = lstInstruments->GetSubListAt(++i))
1833                {
1834                  if (lstInstr->GetListType() == LIST_TYPE_INS) {                  if (lstInstr->GetListType() == LIST_TYPE_INS) {
1835                      pInstruments->push_back(new Instrument(this, lstInstr));                      pInstruments->push_back(new Instrument(this, lstInstr));
1836                  }                  }
                 lstInstr = lstInstruments->GetNextSubList();  
1837              }              }
1838          }          }
1839      }      }
# Line 1643  namespace DLS { Line 1867  namespace DLS {
1867          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1868          if (iter == pInstruments->end()) return;          if (iter == pInstruments->end()) return;
1869          pInstruments->erase(iter);          pInstruments->erase(iter);
1870            pInstrument->DeleteChunks();
1871          delete pInstrument;          delete pInstrument;
1872      }      }
1873    
1874      /**      /**
1875         * Returns the underlying RIFF::File used for persistency of this DLS::File
1876         * object.
1877         */
1878        RIFF::File* File::GetRiffFile() {
1879            return pRIFF;
1880        }
1881    
1882        /**
1883       * Returns extension file of given index. Extension files are used       * Returns extension file of given index. Extension files are used
1884       * 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
1885       * 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 1719  namespace DLS { Line 1952  namespace DLS {
1952    
1953          // update instrument's chunks          // update instrument's chunks
1954          if (pInstruments) {          if (pInstruments) {
1955              // divide local progress into subprogress              if (pProgress) {
1956              progress_t subprogress;                  // divide local progress into subprogress
1957              __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress                  progress_t subprogress;
1958                    __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress
1959    
             // 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);  
1960                  // do the actual work                  // do the actual work
1961                  (*iter)->UpdateChunks(&subsubprogress);                  InstrumentList::iterator iter = pInstruments->begin();
1962              }                  InstrumentList::iterator end  = pInstruments->end();
1963                    for (int i = 0; iter != end; ++iter, ++i) {
1964                        // divide subprogress into sub-subprogress
1965                        progress_t subsubprogress;
1966                        __divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i);
1967                        // do the actual work
1968                        (*iter)->UpdateChunks(&subsubprogress);
1969                    }
1970    
1971              __notify_progress(&subprogress, 1.0); // notify subprogress done                  __notify_progress(&subprogress, 1.0); // notify subprogress done
1972                } else {
1973                    InstrumentList::iterator iter = pInstruments->begin();
1974                    InstrumentList::iterator end  = pInstruments->end();
1975                    for (int i = 0; iter != end; ++iter, ++i) {
1976                        (*iter)->UpdateChunks(NULL);
1977                    }
1978                }
1979          }          }
1980    
1981          // update 'ptbl' chunk          // update 'ptbl' chunk
# Line 1752  namespace DLS { Line 1993  namespace DLS {
1993    
1994          // update sample's chunks          // update sample's chunks
1995          if (pSamples) {          if (pSamples) {
1996              // divide local progress into subprogress              if (pProgress) {
1997              progress_t subprogress;                  // divide local progress into subprogress
1998              __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress                  progress_t subprogress;
1999                    __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress
2000    
             // 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);  
2001                  // do the actual work                  // do the actual work
2002                  (*iter)->UpdateChunks(&subsubprogress);                  SampleList::iterator iter = pSamples->begin();
2003                    SampleList::iterator end  = pSamples->end();
2004                    for (int i = 0; iter != end; ++iter, ++i) {
2005                        // divide subprogress into sub-subprogress
2006                        progress_t subsubprogress;
2007                        __divide_progress(&subprogress, &subsubprogress, pSamples->size(), i);
2008                        // do the actual work
2009                        (*iter)->UpdateChunks(&subsubprogress);
2010                    }
2011    
2012                    __notify_progress(&subprogress, 1.0); // notify subprogress done
2013                } else {
2014                    SampleList::iterator iter = pSamples->begin();
2015                    SampleList::iterator end  = pSamples->end();
2016                    for (int i = 0; iter != end; ++iter, ++i) {
2017                        (*iter)->UpdateChunks(NULL);
2018                    }
2019                }
2020            }
2021    
2022            // if there are any extension files, gather which ones are regular
2023            // extension files used as wave pool files (.gx00, .gx01, ... , .gx98)
2024            // and which one is probably a convolution (GigaPulse) file (always to
2025            // be saved as .gx99)
2026            std::list<RIFF::File*> poolFiles;  // < for (.gx00, .gx01, ... , .gx98) files
2027            RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file
2028            if (!ExtensionFiles.empty()) {
2029                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2030                for (; it != ExtensionFiles.end(); ++it) {
2031                    //FIXME: the .gx99 file is always used by GSt for convolution
2032                    // data (GigaPulse); so we should better detect by subchunk
2033                    // whether the extension file is intended for convolution
2034                    // instead of checkking for a file name, because the latter does
2035                    // not work for saving new gigs created from scratch
2036                    const std::string oldName = (*it)->GetFileName();
2037                    const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2038                    if (isGigaPulseFile)
2039                        pGigaPulseFile = *it;
2040                    else
2041                        poolFiles.push_back(*it);
2042              }              }
2043            }
2044    
2045              __notify_progress(&subprogress, 1.0); // notify subprogress done          // update the 'xfil' chunk which describes all extension files (wave
2046            // pool files) except the .gx99 file
2047            if (!poolFiles.empty()) {
2048                const int n = poolFiles.size();
2049                const int iHeaderSize = 4;
2050                const int iEntrySize = 144;
2051    
2052                // make sure chunk exists, and with correct size
2053                RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2054                if (ckXfil)
2055                    ckXfil->Resize(iHeaderSize + n * iEntrySize);
2056                else
2057                    ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize);
2058    
2059                uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData();
2060    
2061                // re-assemble the chunk's content
2062                store32(pData, n);
2063                std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin();
2064                for (int i = 0, iOffset = 4; i < n;
2065                     ++itExtFile, ++i, iOffset += iEntrySize)
2066                {
2067                    // update the filename string and 5 byte extension of each extension file
2068                    std::string file = lastPathComponent(
2069                        (*itExtFile)->GetFileName()
2070                    );
2071                    if (file.length() + 6 > 128)
2072                        throw Exception("Fatal error, extension filename length exceeds 122 byte maximum");
2073                    uint8_t* pStrings = &pData[iOffset];
2074                    memset(pStrings, 0, 128);
2075                    memcpy(pStrings, file.c_str(), file.length());
2076                    pStrings += file.length() + 1;
2077                    std::string ext = file.substr(file.length()-5);
2078                    memcpy(pStrings, ext.c_str(), 5);
2079                    // update the dlsid of the extension file
2080                    uint8_t* pId = &pData[iOffset + 128];
2081                    dlsid_t id;
2082                    RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID);
2083                    if (ckDLSID) {
2084                        ckDLSID->Read(&id.ulData1, 1, 4);
2085                        ckDLSID->Read(&id.usData2, 1, 2);
2086                        ckDLSID->Read(&id.usData3, 1, 2);
2087                        ckDLSID->Read(id.abData, 8, 1);
2088                    } else {
2089                        ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16);
2090                        Resource::GenerateDLSID(&id);
2091                        uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
2092                        store32(&pData[0], id.ulData1);
2093                        store16(&pData[4], id.usData2);
2094                        store16(&pData[6], id.usData3);
2095                        memcpy(&pData[8], id.abData, 8);
2096                    }
2097                    store32(&pId[0], id.ulData1);
2098                    store16(&pId[4], id.usData2);
2099                    store16(&pId[6], id.usData3);
2100                    memcpy(&pId[8], id.abData, 8);
2101                }
2102            } else {
2103                // in case there was a 'xfil' chunk, remove it
2104                RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2105                if (ckXfil) pRIFF->DeleteSubChunk(ckXfil);
2106            }
2107    
2108            // update the 'doxf' chunk which describes a .gx99 extension file
2109            // which contains convolution data (GigaPulse)
2110            if (pGigaPulseFile) {
2111                RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2112                if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148);
2113    
2114                uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData();
2115    
2116                // update the dlsid from the extension file
2117                uint8_t* pId = &pData[132];
2118                RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID);
2119                if (!ckDLSID) { //TODO: auto generate DLS ID if missing
2120                    throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk");
2121                } else {
2122                    dlsid_t id;
2123                    // read DLS ID from extension files's DLS ID chunk
2124                    uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData();
2125                    id.ulData1 = load32(&pData[0]);
2126                    id.usData2 = load16(&pData[4]);
2127                    id.usData3 = load16(&pData[6]);
2128                    memcpy(id.abData, &pData[8], 8);
2129                    // store DLS ID to 'doxf' chunk
2130                    store32(&pId[0], id.ulData1);
2131                    store16(&pId[4], id.usData2);
2132                    store16(&pId[6], id.usData3);
2133                    memcpy(&pId[8], id.abData, 8);
2134                }
2135            } else {
2136                // in case there was a 'doxf' chunk, remove it
2137                RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2138                if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf);
2139          }          }
2140    
2141          // the RIFF file to be written might now been grown >= 4GB or might          // the RIFF file to be written might now been grown >= 4GB or might
# Line 1775  namespace DLS { Line 2143  namespace DLS {
2143          // size and thus accordingly we would need to resize the wave pool          // size and thus accordingly we would need to resize the wave pool
2144          // chunk          // chunk
2145          const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();          const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();
2146          const bool bRequires64Bit = (finalFileSize >> 32) != 0;          const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file
2147                                         poolFiles.size() > 0;        // < 32 bit gig file where the hi 32 bits are used as extension file nr
2148          if (b64BitWavePoolOffsets != bRequires64Bit) {          if (b64BitWavePoolOffsets != bRequires64Bit) {
2149              b64BitWavePoolOffsets = bRequires64Bit;              b64BitWavePoolOffsets = bRequires64Bit;
2150              iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;              iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
# Line 1783  namespace DLS { Line 2152  namespace DLS {
2152              ptbl->Resize(iPtblSize);              ptbl->Resize(iPtblSize);
2153          }          }
2154    
2155          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2156                __notify_progress(pProgress, 1.0); // notify done
2157      }      }
2158    
2159      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1801  namespace DLS { Line 2171  namespace DLS {
2171       * @param pProgress - optional: callback function for progress notification       * @param pProgress - optional: callback function for progress notification
2172       */       */
2173      void File::Save(const String& Path, progress_t* pProgress) {      void File::Save(const String& Path, progress_t* pProgress) {
2174          {          // calculate number of tasks to notify progress appropriately
2175            const size_t nExtFiles = ExtensionFiles.size();
2176            const float tasks = 2.f + nExtFiles;
2177    
2178            // save extension files (if required)
2179            if (!ExtensionFiles.empty()) {
2180                // for assembling path of extension files to be saved to
2181                const std::string baseName = pathWithoutExtension(Path);
2182                // save the individual extension files
2183                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2184                for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2185                    //FIXME: the .gx99 file is always used by GSt for convolution
2186                    // data (GigaPulse); so we should better detect by subchunk
2187                    // whether the extension file is intended for convolution
2188                    // instead of checkking for a file name, because the latter does
2189                    // not work for saving new gigs created from scratch
2190                    const std::string oldName = (*it)->GetFileName();
2191                    const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2192                    std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1);
2193                    std::string newPath = baseName + ext;
2194                    // save extension file to its new location
2195                    if (pProgress) {
2196                         // divide local progress into subprogress
2197                        progress_t subprogress;
2198                        __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2199                        // do the actual work
2200                        (*it)->Save(newPath, &subprogress);
2201                    } else
2202                        (*it)->Save(newPath);
2203                }
2204            }
2205    
2206            if (pProgress) {
2207              // divide local progress into subprogress              // divide local progress into subprogress
2208              progress_t subprogress;              progress_t subprogress;
2209              __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)
2210              // do the actual work              // do the actual work
2211              UpdateChunks(&subprogress);              UpdateChunks(&subprogress);
2212                        } else
2213          }              UpdateChunks(NULL);
2214          {  
2215            if (pProgress) {
2216              // divide local progress into subprogress              // divide local progress into subprogress
2217              progress_t subprogress;              progress_t subprogress;
2218              __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)
2219              // do the actual work              // do the actual work
2220              pRIFF->Save(Path, &subprogress);              pRIFF->Save(Path, &subprogress);
2221          }          } else
2222                pRIFF->Save(Path);
2223    
2224          UpdateFileOffsets();          UpdateFileOffsets();
2225          __notify_progress(pProgress, 1.0); // notify done  
2226            if (pProgress)
2227                __notify_progress(pProgress, 1.0); // notify done
2228      }      }
2229    
2230      /** @brief Save changes to same file.      /** @brief Save changes to same file.
# Line 1831  namespace DLS { Line 2238  namespace DLS {
2238       * @throws DLS::Exception  if any kind of DLS specific error occurred       * @throws DLS::Exception  if any kind of DLS specific error occurred
2239       */       */
2240      void File::Save(progress_t* pProgress) {      void File::Save(progress_t* pProgress) {
2241          {          // calculate number of tasks to notify progress appropriately
2242            const size_t nExtFiles = ExtensionFiles.size();
2243            const float tasks = 2.f + nExtFiles;
2244    
2245            // save extension files (if required)
2246            if (!ExtensionFiles.empty()) {
2247                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2248                for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2249                    // save extension file
2250                    if (pProgress) {
2251                        // divide local progress into subprogress
2252                        progress_t subprogress;
2253                        __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2254                        // do the actual work
2255                        (*it)->Save(&subprogress);
2256                    } else
2257                        (*it)->Save();
2258                }
2259            }
2260    
2261            if (pProgress) {
2262              // divide local progress into subprogress              // divide local progress into subprogress
2263              progress_t subprogress;              progress_t subprogress;
2264              __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)
2265              // do the actual work              // do the actual work
2266              UpdateChunks(&subprogress);              UpdateChunks(&subprogress);
2267          }          } else
2268          {              UpdateChunks(NULL);
2269    
2270            if (pProgress) {
2271              // divide local progress into subprogress              // divide local progress into subprogress
2272              progress_t subprogress;              progress_t subprogress;
2273              __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)
2274              // do the actual work              // do the actual work
2275              pRIFF->Save(&subprogress);              pRIFF->Save(&subprogress);
2276          }          } else
2277                pRIFF->Save();
2278    
2279          UpdateFileOffsets();          UpdateFileOffsets();
2280          __notify_progress(pProgress, 1.0); // notify done  
2281            if (pProgress)
2282                __notify_progress(pProgress, 1.0); // notify done
2283      }      }
2284    
2285      /** @brief Updates all file offsets stored all over the file.      /** @brief Updates all file offsets stored all over the file.
# Line 1940  namespace DLS { Line 2373  namespace DLS {
2373          pWavePoolTable   = new uint32_t[WavePoolCount];          pWavePoolTable   = new uint32_t[WavePoolCount];
2374          pWavePoolTableHi = new uint32_t[WavePoolCount];          pWavePoolTableHi = new uint32_t[WavePoolCount];
2375          if (!pSamples) return;          if (!pSamples) return;
2376          // update offsets int wave pool table          // update offsets in wave pool table
2377          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2378          uint64_t wvplFileOffset = wvpl->GetFilePos();          uint64_t wvplFileOffset = wvpl->GetFilePos() -
2379          if (b64BitWavePoolOffsets) {                                    wvpl->GetPos(); // mandatory, since position might have changed
2380            if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ...
2381              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
2382              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
2383              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
2384                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());                  uint64_t _64BitOffset =
2385                  (*iter)->ullWavePoolOffset = _64BitOffset;                      (*iter)->pWaveList->GetFilePos() -
2386                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);                      (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2387                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;                      wvplFileOffset -
2388              }                      LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
         } else { // conventional 32 bit offsets  
             SampleList::iterator iter = pSamples->begin();  
             SampleList::iterator end  = pSamples->end();  
             for (int i = 0 ; iter != end ; ++iter, i++) {  
                 uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());  
2389                  (*iter)->ullWavePoolOffset = _64BitOffset;                  (*iter)->ullWavePoolOffset = _64BitOffset;
2390                  pWavePoolTable[i] = (uint32_t) _64BitOffset;                  pWavePoolTable[i] = (uint32_t) _64BitOffset;
2391              }              }
2392            } else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ...
2393                if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ...
2394                    SampleList::iterator iter = pSamples->begin();
2395                    SampleList::iterator end  = pSamples->end();
2396                    for (int i = 0 ; iter != end ; ++iter, i++) {
2397                        uint64_t _64BitOffset =
2398                            (*iter)->pWaveList->GetFilePos() -
2399                            (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2400                            wvplFileOffset -
2401                            LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2402                        (*iter)->ullWavePoolOffset = _64BitOffset;
2403                        pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
2404                        pWavePoolTable[i]   = (uint32_t) _64BitOffset;
2405                    }
2406                } else { // 32 bit offsets with extension files (GigaStudio legacy support) ...
2407                    // the main gig and the extension files may contain wave data
2408                    std::vector<RIFF::File*> poolFiles;
2409                    poolFiles.push_back(pRIFF);
2410                    poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end());
2411    
2412                    RIFF::File* pCurPoolFile = NULL;
2413                    int fileNo = 0;
2414                    int waveOffset = 0;
2415                    SampleList::iterator iter = pSamples->begin();
2416                    SampleList::iterator end  = pSamples->end();
2417                    for (int i = 0 ; iter != end ; ++iter, i++) {
2418                        RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile();
2419                        // if this sample is located in the same pool file as the
2420                        // last we reuse the previously computed fileNo and waveOffset
2421                        if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ...
2422                            pCurPoolFile = pPoolFile;
2423    
2424                            std::vector<RIFF::File*>::iterator sIter;
2425                            sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile);
2426                            if (sIter != poolFiles.end())
2427                                fileNo = std::distance(poolFiles.begin(), sIter);
2428                            else
2429                                throw DLS::Exception("Fatal error, unknown pool file");
2430    
2431                            RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL);
2432                            if (!extWvpl)
2433                                throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk");
2434                            waveOffset =
2435                                extWvpl->GetFilePos() -
2436                                extWvpl->GetPos() + // mandatory, since position might have changed
2437                                LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize());
2438                        }
2439                        uint64_t _64BitOffset =
2440                            (*iter)->pWaveList->GetFilePos() -
2441                            (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2442                            waveOffset;
2443                        // pWavePoolTableHi stores file number when extension files are in use
2444                        pWavePoolTableHi[i] = (uint32_t) fileNo;
2445                        pWavePoolTable[i]   = (uint32_t) _64BitOffset;
2446                        (*iter)->ullWavePoolOffset = _64BitOffset;
2447                    }
2448                }
2449          }          }
2450      }      }
2451    
2452    
   
2453  // *************** Exception ***************  // *************** Exception ***************
2454  // *  // *
2455    

Legend:
Removed from v.3198  
changed lines
  Added in v.3928

  ViewVC Help
Powered by ViewVC