/[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 2698 by schoenebeck, Sun Jan 11 17:47:57 2015 UTC revision 3474 by schoenebeck, Wed Feb 20 16:04:19 2019 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-2019 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 493  namespace DLS { Line 494  namespace DLS {
494       * Generates a new DLSID for the resource.       * Generates a new DLSID for the resource.
495       */       */
496      void Resource::GenerateDLSID() {      void Resource::GenerateDLSID() {
497  #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)          #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
   
498          if (!pDLSID) pDLSID = new dlsid_t;          if (!pDLSID) pDLSID = new dlsid_t;
499            GenerateDLSID(pDLSID);
500            #endif
501        }
502    
503        void Resource::GenerateDLSID(dlsid_t* pDLSID) {
504    #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
505  #ifdef WIN32  #ifdef WIN32
   
506          UUID uuid;          UUID uuid;
507          UuidCreate(&uuid);          UuidCreate(&uuid);
508          pDLSID->ulData1 = uuid.Data1;          pDLSID->ulData1 = uuid.Data1;
# Line 711  namespace DLS { Line 715  namespace DLS {
715       * @param WavePoolOffset - offset of this sample data from wave pool       * @param WavePoolOffset - offset of this sample data from wave pool
716       *                         ('wvpl') list chunk       *                         ('wvpl') list chunk
717       */       */
718      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) {
719          pWaveList = waveList;          pWaveList = waveList;
720          ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE;          ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize());
721          pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);          pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
722          pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);          pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);
723          if (pCkFormat) {          if (pCkFormat) {
# Line 793  namespace DLS { Line 797  namespace DLS {
797          Resize(orig->GetSize());          Resize(orig->GetSize());
798          char* buf = (char*) LoadSampleData();          char* buf = (char*) LoadSampleData();
799          Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now          Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now
800          const unsigned long restorePos = pOrig->pCkData->GetPos();          const file_offset_t restorePos = pOrig->pCkData->GetPos();
801          pOrig->SetPos(0);          pOrig->SetPos(0);
802          for (unsigned long todo = pOrig->GetSize(), i = 0; todo; ) {          for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) {
803              const int iReadAtOnce = 64*1024;              const int iReadAtOnce = 64*1024;
804              unsigned long n = (iReadAtOnce < todo) ? iReadAtOnce : todo;              file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo;
805              n = pOrig->Read(&buf[i], n);              n = pOrig->Read(&buf[i], n);
806              if (!n) break;              if (!n) break;
807              todo -= n;              todo -= n;
# Line 855  namespace DLS { Line 859  namespace DLS {
859       * @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
860       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
861       */       */
862      unsigned long Sample::GetSize() const {      file_offset_t Sample::GetSize() const {
863          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
864          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
865      }      }
# Line 882  namespace DLS { Line 886  namespace DLS {
886       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
887       * other formats will fail!       * other formats will fail!
888       *       *
889       * @param iNewSize - new sample wave data size in sample points (must be       * @param NewSize - new sample wave data size in sample points (must be
890       *                   greater than zero)       *                  greater than zero)
891       * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM       * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM
892       * @throws Exception if \a iNewSize is less than 1       * @throws Exception if \a NewSize is less than 1 or unrealistic large
893       * @see File::Save(), FrameSize, FormatTag       * @see File::Save(), FrameSize, FormatTag
894       */       */
895      void Sample::Resize(int iNewSize) {      void Sample::Resize(file_offset_t NewSize) {
896          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");
897          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");
898          const int iSizeInBytes = iNewSize * FrameSize;          if ((NewSize >> 48) != 0)
899                throw Exception("Unrealistic high DLS sample size detected");
900            const file_offset_t sizeInBytes = NewSize * FrameSize;
901          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
902          if (pCkData) pCkData->Resize(iSizeInBytes);          if (pCkData) pCkData->Resize(sizeInBytes);
903          else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes);          else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes);
904      }      }
905    
906      /**      /**
# Line 913  namespace DLS { Line 919  namespace DLS {
919       * @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
920       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
921       */       */
922      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) {
923          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
924          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");
925          unsigned long orderedBytes = SampleCount * FrameSize;          file_offset_t orderedBytes = SampleCount * FrameSize;
926          unsigned long result = pCkData->SetPos(orderedBytes, Whence);          file_offset_t result = pCkData->SetPos(orderedBytes, Whence);
927          return (result == orderedBytes) ? SampleCount          return (result == orderedBytes) ? SampleCount
928                                          : result / FrameSize;                                          : result / FrameSize;
929      }      }
# Line 931  namespace DLS { Line 937  namespace DLS {
937       * @param pBuffer      destination buffer       * @param pBuffer      destination buffer
938       * @param SampleCount  number of sample points to read       * @param SampleCount  number of sample points to read
939       */       */
940      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {      file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) {
941          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
942          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?
943      }      }
# Line 951  namespace DLS { Line 957  namespace DLS {
957       * @throws Exception if current sample size is too small       * @throws Exception if current sample size is too small
958       * @see LoadSampleData()       * @see LoadSampleData()
959       */       */
960      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {      file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) {
961          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
962          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");
963          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 1000  namespace DLS {
1000      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) {
1001          pCkRegion = rgnList;          pCkRegion = rgnList;
1002    
1003          // articulation informations          // articulation information
1004          RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);          RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);
1005          if (rgnh) {          if (rgnh) {
1006              rgnh->Read(&KeyRange, 2, 2);              rgnh->Read(&KeyRange, 2, 2);
# Line 1016  namespace DLS { Line 1022  namespace DLS {
1022          }          }
1023          SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;          SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;
1024    
1025          // sample informations          // sample information
1026          RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);          RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);
1027          if (wlnk) {          if (wlnk) {
1028              WaveLinkOptionFlags = wlnk->ReadUint16();              WaveLinkOptionFlags = wlnk->ReadUint16();
# Line 1047  namespace DLS { Line 1053  namespace DLS {
1053      Sample* Region::GetSample() {      Sample* Region::GetSample() {
1054          if (pSample) return pSample;          if (pSample) return pSample;
1055          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
1056          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];          uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
1057          Sample* sample = file->GetFirstSample();          Sample* sample = file->GetFirstSample();
1058          while (sample) {          while (sample) {
1059              if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample);              if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample);
1060              sample = file->GetNextSample();              sample = file->GetNextSample();
1061          }          }
1062          return NULL;          return NULL;
# Line 1275  namespace DLS { Line 1281  namespace DLS {
1281          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
1282          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
1283          pRegions->push_back(pNewRegion);          pRegions->push_back(pNewRegion);
1284          Regions = pRegions->size();          Regions = (uint32_t) pRegions->size();
1285          return pNewRegion;          return pNewRegion;
1286      }      }
1287    
# Line 1293  namespace DLS { Line 1299  namespace DLS {
1299          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
1300          if (iter == pRegions->end()) return;          if (iter == pRegions->end()) return;
1301          pRegions->erase(iter);          pRegions->erase(iter);
1302          Regions = pRegions->size();          Regions = (uint32_t) pRegions->size();
1303          delete pRegion;          delete pRegion;
1304      }      }
1305    
# Line 1313  namespace DLS { Line 1319  namespace DLS {
1319          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
1320          uint8_t* pData = (uint8_t*) insh->LoadChunkData();          uint8_t* pData = (uint8_t*) insh->LoadChunkData();
1321          // update 'insh' chunk          // update 'insh' chunk
1322          Regions = (pRegions) ? pRegions->size() : 0;          Regions = (pRegions) ? uint32_t(pRegions->size()) : 0;
1323          midi_locale_t locale;          midi_locale_t locale;
1324          locale.instrument = MIDIProgram;          locale.instrument = MIDIProgram;
1325          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
# Line 1405  namespace DLS { Line 1411  namespace DLS {
1411       */       */
1412      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1413          pRIFF->SetByteOrder(RIFF::endian_little);          pRIFF->SetByteOrder(RIFF::endian_little);
1414            bOwningRiff = true;
1415          pVersion = new version_t;          pVersion = new version_t;
1416          pVersion->major   = 0;          pVersion->major   = 0;
1417          pVersion->minor   = 0;          pVersion->minor   = 0;
# Line 1435  namespace DLS { Line 1442  namespace DLS {
1442      File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {      File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1443          if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");          if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1444          this->pRIFF = pRIFF;          this->pRIFF = pRIFF;
1445            bOwningRiff = false;
1446          RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);          RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1447          if (ckVersion) {          if (ckVersion) {
1448              pVersion = new version_t;              pVersion = new version_t;
# Line 1467  namespace DLS { Line 1474  namespace DLS {
1474                  for (int i = 0 ; i < WavePoolCount ; i++) {                  for (int i = 0 ; i < WavePoolCount ; i++) {
1475                      pWavePoolTableHi[i] = ptbl->ReadUint32();                      pWavePoolTableHi[i] = ptbl->ReadUint32();
1476                      pWavePoolTable[i] = ptbl->ReadUint32();                      pWavePoolTable[i] = ptbl->ReadUint32();
1477                      if (pWavePoolTable[i] & 0x80000000)                      //NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12)
1478                          throw DLS::Exception("Files larger than 2 GB not yet supported");                      //if (pWavePoolTable[i] & 0x80000000)
1479                        //    throw DLS::Exception("Files larger than 2 GB not yet supported");
1480                  }                  }
1481              } else { // conventional 32 bit offsets              } else { // conventional 32 bit offsets
1482                  ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));                  ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
# Line 1506  namespace DLS { Line 1514  namespace DLS {
1514          if (pVersion) delete pVersion;          if (pVersion) delete pVersion;
1515          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++)
1516              delete *i;              delete *i;
1517            if (bOwningRiff)
1518                delete pRIFF;
1519      }      }
1520    
1521      Sample* File::GetFirstSample() {      Sample* File::GetFirstSample() {
# Line 1525  namespace DLS { Line 1535  namespace DLS {
1535          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
1536          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1537          if (wvpl) {          if (wvpl) {
1538              unsigned long wvplFileOffset = wvpl->GetFilePos();              file_offset_t wvplFileOffset = wvpl->GetFilePos();
1539              RIFF::List* wave = wvpl->GetFirstSubList();              RIFF::List* wave = wvpl->GetFirstSubList();
1540              while (wave) {              while (wave) {
1541                  if (wave->GetListType() == LIST_TYPE_WAVE) {                  if (wave->GetListType() == LIST_TYPE_WAVE) {
1542                      unsigned long waveFileOffset = wave->GetFilePos();                      file_offset_t waveFileOffset = wave->GetFilePos();
1543                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1544                  }                  }
1545                  wave = wvpl->GetNextSubList();                  wave = wvpl->GetNextSubList();
# Line 1538  namespace DLS { Line 1548  namespace DLS {
1548          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)
1549              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);              RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1550              if (dwpl) {              if (dwpl) {
1551                  unsigned long dwplFileOffset = dwpl->GetFilePos();                  file_offset_t dwplFileOffset = dwpl->GetFilePos();
1552                  RIFF::List* wave = dwpl->GetFirstSubList();                  RIFF::List* wave = dwpl->GetFirstSubList();
1553                  while (wave) {                  while (wave) {
1554                      if (wave->GetListType() == LIST_TYPE_WAVE) {                      if (wave->GetListType() == LIST_TYPE_WAVE) {
1555                          unsigned long waveFileOffset = wave->GetFilePos();                          file_offset_t waveFileOffset = wave->GetFilePos();
1556                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1557                      }                      }
1558                      wave = dwpl->GetNextSubList();                      wave = dwpl->GetNextSubList();
# Line 1708  namespace DLS { Line 1718  namespace DLS {
1718          }          }
1719    
1720          // update 'colh' chunk          // update 'colh' chunk
1721          Instruments = (pInstruments) ? pInstruments->size() : 0;          Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0;
1722          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1723          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1724          uint8_t* pData = (uint8_t*) colh->LoadChunkData();          uint8_t* pData = (uint8_t*) colh->LoadChunkData();
# Line 1735  namespace DLS { Line 1745  namespace DLS {
1745          }          }
1746    
1747          // update 'ptbl' chunk          // update 'ptbl' chunk
1748          const int iSamples = (pSamples) ? pSamples->size() : 0;          const int iSamples = (pSamples) ? int(pSamples->size()) : 0;
1749          const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1750          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1751          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*/);
1752          const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;          int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1753          ptbl->Resize(iPtblSize);          ptbl->Resize(iPtblSize);
1754          pData = (uint8_t*) ptbl->LoadChunkData();          pData = (uint8_t*) ptbl->LoadChunkData();
1755          WavePoolCount = iSamples;          WavePoolCount = iSamples;
# Line 1767  namespace DLS { Line 1777  namespace DLS {
1777              __notify_progress(&subprogress, 1.0); // notify subprogress done              __notify_progress(&subprogress, 1.0); // notify subprogress done
1778          }          }
1779    
1780            // if there are any extension files, gather which ones are regular
1781            // extension files used as wave pool files (.gx00, .gx01, ... , .gx98)
1782            // and which one is probably a convolution (GigaPulse) file (always to
1783            // be saved as .gx99)
1784            std::list<RIFF::File*> poolFiles;  // < for (.gx00, .gx01, ... , .gx98) files
1785            RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file
1786            if (!ExtensionFiles.empty()) {
1787                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
1788                for (; it != ExtensionFiles.end(); ++it) {
1789                    //FIXME: the .gx99 file is always used by GSt for convolution
1790                    // data (GigaPulse); so we should better detect by subchunk
1791                    // whether the extension file is intended for convolution
1792                    // instead of checkking for a file name, because the latter does
1793                    // not work for saving new gigs created from scratch
1794                    const std::string oldName = (*it)->GetFileName();
1795                    const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
1796                    if (isGigaPulseFile)
1797                        pGigaPulseFile = *it;
1798                    else
1799                        poolFiles.push_back(*it);
1800                }
1801            }
1802    
1803            // update the 'xfil' chunk which describes all extension files (wave
1804            // pool files) except the .gx99 file
1805            if (!poolFiles.empty()) {
1806                const int n = poolFiles.size();
1807                const int iHeaderSize = 4;
1808                const int iEntrySize = 144;
1809    
1810                // make sure chunk exists, and with correct size
1811                RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
1812                if (ckXfil)
1813                    ckXfil->Resize(iHeaderSize + n * iEntrySize);
1814                else
1815                    ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize);
1816    
1817                uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData();
1818    
1819                // re-assemble the chunk's content
1820                store32(pData, n);
1821                std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin();
1822                for (int i = 0, iOffset = 4; i < n;
1823                     ++itExtFile, ++i, iOffset += iEntrySize)
1824                {
1825                    // update the filename string and 5 byte extension of each extension file
1826                    std::string file = lastPathComponent(
1827                        (*itExtFile)->GetFileName()
1828                    );
1829                    if (file.length() + 6 > 128)
1830                        throw Exception("Fatal error, extension filename length exceeds 122 byte maximum");
1831                    uint8_t* pStrings = &pData[iOffset];
1832                    memset(pStrings, 0, 128);
1833                    memcpy(pStrings, file.c_str(), file.length());
1834                    pStrings += file.length() + 1;
1835                    std::string ext = file.substr(file.length()-5);
1836                    memcpy(pStrings, ext.c_str(), 5);
1837                    // update the dlsid of the extension file
1838                    uint8_t* pId = &pData[iOffset + 128];
1839                    dlsid_t id;
1840                    RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID);
1841                    if (ckDLSID) {
1842                        ckDLSID->Read(&id.ulData1, 1, 4);
1843                        ckDLSID->Read(&id.usData2, 1, 2);
1844                        ckDLSID->Read(&id.usData3, 1, 2);
1845                        ckDLSID->Read(id.abData, 8, 1);
1846                    } else {
1847                        ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16);
1848                        Resource::GenerateDLSID(&id);
1849                        uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
1850                        store32(&pData[0], id.ulData1);
1851                        store16(&pData[4], id.usData2);
1852                        store16(&pData[6], id.usData3);
1853                        memcpy(&pData[8], id.abData, 8);
1854                    }
1855                    store32(&pId[0], id.ulData1);
1856                    store16(&pId[4], id.usData2);
1857                    store16(&pId[6], id.usData3);
1858                    memcpy(&pId[8], id.abData, 8);
1859                }
1860            } else {
1861                // in case there was a 'xfil' chunk, remove it
1862                RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
1863                if (ckXfil) pRIFF->DeleteSubChunk(ckXfil);
1864            }
1865    
1866            // update the 'doxf' chunk which describes a .gx99 extension file
1867            // which contains convolution data (GigaPulse)
1868            if (pGigaPulseFile) {
1869                RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
1870                if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148);
1871    
1872                uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData();
1873    
1874                // update the dlsid from the extension file
1875                uint8_t* pId = &pData[132];
1876                RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID);
1877                if (!ckDLSID) { //TODO: auto generate DLS ID if missing
1878                    throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk");
1879                } else {
1880                    dlsid_t id;
1881                    // read DLS ID from extension files's DLS ID chunk
1882                    uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData();
1883                    id.ulData1 = load32(&pData[0]);
1884                    id.usData2 = load16(&pData[4]);
1885                    id.usData3 = load16(&pData[6]);
1886                    memcpy(id.abData, &pData[8], 8);
1887                    // store DLS ID to 'doxf' chunk
1888                    store32(&pId[0], id.ulData1);
1889                    store16(&pId[4], id.usData2);
1890                    store16(&pId[6], id.usData3);
1891                    memcpy(&pId[8], id.abData, 8);
1892                }
1893            } else {
1894                // in case there was a 'doxf' chunk, remove it
1895                RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
1896                if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf);
1897            }
1898    
1899            // the RIFF file to be written might now been grown >= 4GB or might
1900            // been shrunk < 4GB, so we might need to update the wave pool offset
1901            // size and thus accordingly we would need to resize the wave pool
1902            // chunk
1903            const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();
1904            const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file
1905                                         poolFiles.size() > 0;        // < 32 bit gig file where the hi 32 bits are used as extension file nr
1906            if (b64BitWavePoolOffsets != bRequires64Bit) {
1907                b64BitWavePoolOffsets = bRequires64Bit;
1908                iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1909                iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1910                ptbl->Resize(iPtblSize);
1911            }
1912    
1913          __notify_progress(pProgress, 1.0); // notify done          __notify_progress(pProgress, 1.0); // notify done
1914      }      }
1915    
# Line 1785  namespace DLS { Line 1928  namespace DLS {
1928       * @param pProgress - optional: callback function for progress notification       * @param pProgress - optional: callback function for progress notification
1929       */       */
1930      void File::Save(const String& Path, progress_t* pProgress) {      void File::Save(const String& Path, progress_t* pProgress) {
1931            // calculate number of tasks to notify progress appropriately
1932            const size_t nExtFiles = ExtensionFiles.size();
1933            const float tasks = 2.f + nExtFiles;
1934    
1935            // save extension files (if required)
1936            if (!ExtensionFiles.empty()) {
1937                // for assembling path of extension files to be saved to
1938                const std::string folder = parentPath(Path);
1939                const std::string baseName = pathWithoutExtension(Path);
1940                // save the individual extension files
1941                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
1942                for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
1943                    // divide local progress into subprogress
1944                    progress_t subprogress;
1945                    __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
1946                    //FIXME: the .gx99 file is always used by GSt for convolution
1947                    // data (GigaPulse); so we should better detect by subchunk
1948                    // whether the extension file is intended for convolution
1949                    // instead of checkking for a file name, because the latter does
1950                    // not work for saving new gigs created from scratch
1951                    const std::string oldName = (*it)->GetFileName();
1952                    const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
1953                    std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx02d", i+1);
1954                    std::string newPath = concatPath(folder, baseName) + ext;
1955                    // save extension file to its new location
1956                    (*it)->Save(newPath, &subprogress);
1957                }
1958            }
1959    
1960          {          {
1961              // divide local progress into subprogress              // divide local progress into subprogress
1962              progress_t subprogress;              progress_t subprogress;
1963              __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)
1964              // do the actual work              // do the actual work
1965              UpdateChunks(&subprogress);              UpdateChunks(&subprogress);
               
1966          }          }
1967          {          {
1968              // divide local progress into subprogress              // divide local progress into subprogress
1969              progress_t subprogress;              progress_t subprogress;
1970              __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)
1971              // do the actual work              // do the actual work
1972              pRIFF->Save(Path, &subprogress);              pRIFF->Save(Path, &subprogress);
1973          }          }
# Line 1811  namespace DLS { Line 1982  namespace DLS {
1982       * have at the end of the saving process.       * have at the end of the saving process.
1983       *       *
1984       * @param pProgress - optional: callback function for progress notification       * @param pProgress - optional: callback function for progress notification
1985       * @throws RIFF::Exception if any kind of IO error occured       * @throws RIFF::Exception if any kind of IO error occurred
1986       * @throws DLS::Exception  if any kind of DLS specific error occured       * @throws DLS::Exception  if any kind of DLS specific error occurred
1987       */       */
1988      void File::Save(progress_t* pProgress) {      void File::Save(progress_t* pProgress) {
1989            // calculate number of tasks to notify progress appropriately
1990            const size_t nExtFiles = ExtensionFiles.size();
1991            const float tasks = 2.f + nExtFiles;
1992    
1993            // save extension files (if required)
1994            if (!ExtensionFiles.empty()) {
1995                std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
1996                for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
1997                    // divide local progress into subprogress
1998                    progress_t subprogress;
1999                    __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2000                    // save extension file
2001                    (*it)->Save(&subprogress);
2002                }
2003            }
2004    
2005          {          {
2006              // divide local progress into subprogress              // divide local progress into subprogress
2007              progress_t subprogress;              progress_t subprogress;
2008              __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)
2009              // do the actual work              // do the actual work
2010              UpdateChunks(&subprogress);              UpdateChunks(&subprogress);
2011          }          }
2012          {          {
2013              // divide local progress into subprogress              // divide local progress into subprogress
2014              progress_t subprogress;              progress_t subprogress;
2015              __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)
2016              // do the actual work              // do the actual work
2017              pRIFF->Save(&subprogress);              pRIFF->Save(&subprogress);
2018          }          }
# Line 1881  namespace DLS { Line 2068  namespace DLS {
2068          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2069          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2070          // check if 'ptbl' chunk is large enough          // check if 'ptbl' chunk is large enough
2071          WavePoolCount = (pSamples) ? pSamples->size() : 0;          WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2072          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;          const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
2073          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
2074          // save the 'ptbl' chunk's current read/write position          // save the 'ptbl' chunk's current read/write position
2075          unsigned long ulOriginalPos = ptbl->GetPos();          file_offset_t ullOriginalPos = ptbl->GetPos();
2076          // update headers          // update headers
2077          ptbl->SetPos(0);          ptbl->SetPos(0);
2078          uint32_t tmp = WavePoolHeaderSize;          uint32_t tmp = WavePoolHeaderSize;
# Line 1908  namespace DLS { Line 2095  namespace DLS {
2095              }              }
2096          }          }
2097          // restore 'ptbl' chunk's original read/write position          // restore 'ptbl' chunk's original read/write position
2098          ptbl->SetPos(ulOriginalPos);          ptbl->SetPos(ullOriginalPos);
2099      }      }
2100    
2101      /**      /**
# Line 1917  namespace DLS { Line 2104  namespace DLS {
2104       * exists already.       * exists already.
2105       */       */
2106      void File::__UpdateWavePoolTable() {      void File::__UpdateWavePoolTable() {
2107          WavePoolCount = (pSamples) ? pSamples->size() : 0;          WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2108          // resize wave pool table arrays          // resize wave pool table arrays
2109          if (pWavePoolTable)   delete[] pWavePoolTable;          if (pWavePoolTable)   delete[] pWavePoolTable;
2110          if (pWavePoolTableHi) delete[] pWavePoolTableHi;          if (pWavePoolTableHi) delete[] pWavePoolTableHi;
2111          pWavePoolTable   = new uint32_t[WavePoolCount];          pWavePoolTable   = new uint32_t[WavePoolCount];
2112          pWavePoolTableHi = new uint32_t[WavePoolCount];          pWavePoolTableHi = new uint32_t[WavePoolCount];
2113          if (!pSamples) return;          if (!pSamples) return;
2114          // update offsets int wave pool table          // update offsets in wave pool table
2115          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2116          uint64_t wvplFileOffset = wvpl->GetFilePos();          uint64_t wvplFileOffset = wvpl->GetFilePos();
2117          if (b64BitWavePoolOffsets) {          if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ...
             SampleList::iterator iter = pSamples->begin();  
             SampleList::iterator end  = pSamples->end();  
             for (int i = 0 ; iter != end ; ++iter, i++) {  
                 uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;  
                 (*iter)->ulWavePoolOffset = _64BitOffset;  
                 pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);  
                 pWavePoolTable[i]   = (uint32_t) _64BitOffset;  
             }  
         } else { // conventional 32 bit offsets  
2118              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
2119              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
2120              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
2121                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2122                  (*iter)->ulWavePoolOffset = _64BitOffset;                  (*iter)->ullWavePoolOffset = _64BitOffset;
2123                  pWavePoolTable[i] = (uint32_t) _64BitOffset;                  pWavePoolTable[i] = (uint32_t) _64BitOffset;
2124              }              }
2125            } else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ...
2126                if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ...
2127                    SampleList::iterator iter = pSamples->begin();
2128                    SampleList::iterator end  = pSamples->end();
2129                    for (int i = 0 ; iter != end ; ++iter, i++) {
2130                        uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2131                        (*iter)->ullWavePoolOffset = _64BitOffset;
2132                        pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
2133                        pWavePoolTable[i]   = (uint32_t) _64BitOffset;
2134                    }
2135                } else { // 32 bit offsets with extension files (GigaStudio legacy support) ...
2136                    // the main gig and the extension files may contain wave data
2137                    std::vector<RIFF::File*> poolFiles;
2138                    poolFiles.push_back(pRIFF);
2139                    poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end());
2140    
2141                    RIFF::File* pCurPoolFile = NULL;
2142                    int fileNo = 0;
2143                    int waveOffset = 0;
2144                    SampleList::iterator iter = pSamples->begin();
2145                    SampleList::iterator end  = pSamples->end();
2146                    for (int i = 0 ; iter != end ; ++iter, i++) {
2147                        RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile();
2148                        // if this sample is located in the same pool file as the
2149                        // last we reuse the previously computed fileNo and waveOffset
2150                        if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ...
2151                            pCurPoolFile = pPoolFile;
2152    
2153                            std::vector<RIFF::File*>::iterator sIter;
2154                            sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile);
2155                            if (sIter != poolFiles.end())
2156                                fileNo = std::distance(poolFiles.begin(), sIter);
2157                            else
2158                                throw DLS::Exception("Fatal error, unknown pool file");
2159    
2160                            RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL);
2161                            if (!extWvpl)
2162                                throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk");
2163                            waveOffset = extWvpl->GetFilePos() + LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize());
2164                        }
2165                        uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - waveOffset;
2166                        // pWavePoolTableHi stores file number when extension files are in use
2167                        pWavePoolTableHi[i] = (uint32_t) fileNo;
2168                        pWavePoolTable[i]   = (uint32_t) _64BitOffset;
2169                        (*iter)->ullWavePoolOffset = _64BitOffset;
2170                    }
2171                }
2172          }          }
2173      }      }
2174    
2175    
   
2176  // *************** Exception ***************  // *************** Exception ***************
2177  // *  // *
2178    
2179      Exception::Exception(String Message) : RIFF::Exception(Message) {      Exception::Exception() : RIFF::Exception() {
2180        }
2181    
2182        Exception::Exception(String format, ...) : RIFF::Exception() {
2183            va_list arg;
2184            va_start(arg, format);
2185            Message = assemble(format, arg);
2186            va_end(arg);
2187        }
2188    
2189        Exception::Exception(String format, va_list arg) : RIFF::Exception() {
2190            Message = assemble(format, arg);
2191      }      }
2192    
2193      void Exception::PrintMessage() {      void Exception::PrintMessage() {

Legend:
Removed from v.2698  
changed lines
  Added in v.3474

  ViewVC Help
Powered by ViewVC