/[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 802 by schoenebeck, Thu Nov 10 19:53:34 2005 UTC revision 839 by persson, Fri Feb 10 19:23:59 2006 UTC
# Line 212  namespace DLS { Line 212  namespace DLS {
212       * call File::Save() to make changes persistent.       * call File::Save() to make changes persistent.
213       */       */
214      void Articulator::UpdateChunks() {      void Articulator::UpdateChunks() {
215          ArticulationList::iterator iter = pArticulations->begin();          if (pArticulations) {
216          ArticulationList::iterator end  = pArticulations->end();              ArticulationList::iterator iter = pArticulations->begin();
217          for (; iter != end; ++iter) {              ArticulationList::iterator end  = pArticulations->end();
218              (*iter)->UpdateChunks();              for (; iter != end; ++iter) {
219                    (*iter)->UpdateChunks();
220                }
221          }          }
222      }      }
223    
# Line 255  namespace DLS { Line 257  namespace DLS {
257          }          }
258      }      }
259    
260        Info::~Info() {
261        }
262    
263      /** @brief Load given INFO field.      /** @brief Load given INFO field.
264       *       *
265       * Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO       * Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO
# Line 263  namespace DLS { Line 268  namespace DLS {
268      void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {      void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
269          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
270          if (ck) {          if (ck) {
271              // TODO: no check for ZSTR terminated strings yet              const char* str = (char*)ck->LoadChunkData();
272              s = (char*) ck->LoadChunkData();              int size = ck->GetSize();
273                int len;
274                for (len = 0 ; len < size ; len++)
275                    if (str[len] == '\0') break;
276                s.assign(str, len);
277              ck->ReleaseChunkData();              ck->ReleaseChunkData();
278          }          }
279      }      }
# Line 273  namespace DLS { Line 282  namespace DLS {
282       *       *
283       * Apply given info value to info chunk with ID \a ChunkID, which is a       * Apply given info value to info chunk with ID \a ChunkID, which is a
284       * subchunk of INFO list chunk \a lstINFO. If the given chunk already       * subchunk of INFO list chunk \a lstINFO. If the given chunk already
285       * exists, value \a s will be applied, otherwise if it doesn't exist yet       * exists, value \a s will be applied. Otherwise if it doesn't exist yet
286       * and \a sDefault is not an empty string, such a chunk will be created       * and either \a s or \a sDefault is not an empty string, such a chunk
287       * and \a sDefault will be applied.       * will be created and either \a s or \a sDefault will be applied
288         * (depending on which one is not an empty string, if both are not an
289         * empty string \a s will be preferred).
290       *       *
291       * @param ChunkID  - 32 bit RIFF chunk ID of INFO subchunk       * @param ChunkID  - 32 bit RIFF chunk ID of INFO subchunk
292       * @param lstINFO  - parent (INFO) RIFF list chunk       * @param lstINFO  - parent (INFO) RIFF list chunk
# Line 288  namespace DLS { Line 299  namespace DLS {
299              ck->Resize(s.size() + 1);              ck->Resize(s.size() + 1);
300              char* pData = (char*) ck->LoadChunkData();              char* pData = (char*) ck->LoadChunkData();
301              memcpy(pData, s.c_str(), s.size() + 1);              memcpy(pData, s.c_str(), s.size() + 1);
302          } else if (sDefault != "") { // create chunk and use default value          } else if (s != "" || sDefault != "") { // create chunk
303              ck = lstINFO->AddSubChunk(ChunkID, sDefault.size() + 1);              const String& sToSave = (s != "") ? s : sDefault;
304                ck = lstINFO->AddSubChunk(ChunkID, sToSave.size() + 1);
305              char* pData = (char*) ck->LoadChunkData();              char* pData = (char*) ck->LoadChunkData();
306              memcpy(pData, sDefault.c_str(), sDefault.size() + 1);              memcpy(pData, sToSave.c_str(), sToSave.size() + 1);
307          }          }
308      }      }
309    
# Line 312  namespace DLS { Line 324  namespace DLS {
324          // get current date          // get current date
325          time_t now = time(NULL);          time_t now = time(NULL);
326          tm* pNowBroken = localtime(&now);          tm* pNowBroken = localtime(&now);
327          String defaultCreationDate = ToString(pNowBroken->tm_year) + "-" +          String defaultCreationDate = ToString(1900 + pNowBroken->tm_year) + "-" +
328                                       ToString(pNowBroken->tm_mon)  + "-" +                                       ToString(pNowBroken->tm_mon + 1)  + "-" +
329                                       ToString(pNowBroken->tm_mday);                                       ToString(pNowBroken->tm_mday);
330          String defaultSoftware = libraryName() + " " + libraryVersion();          String defaultSoftware = libraryName() + " " + libraryVersion();
331          String defaultComments = "Created with " + libraryName() + " " + libraryVersion();          String defaultComments = "Created with " + libraryName() + " " + libraryVersion();
# Line 816  namespace DLS { Line 828  namespace DLS {
828          // get sample's wave pool table index          // get sample's wave pool table index
829          int index = -1;          int index = -1;
830          File* pFile = (File*) GetParent()->GetParent();          File* pFile = (File*) GetParent()->GetParent();
831          File::SampleList::iterator iter = pFile->pSamples->begin();          if (pFile->pSamples) {
832          File::SampleList::iterator end  = pFile->pSamples->end();              File::SampleList::iterator iter = pFile->pSamples->begin();
833          for (int i = 0; iter != end; ++iter, i++) {              File::SampleList::iterator end  = pFile->pSamples->end();
834              if (*iter == pSample) {              for (int i = 0; iter != end; ++iter, i++) {
835                  index = i;                  if (*iter == pSample) {
836                  break;                      index = i;
837                        break;
838                    }
839              }              }
840          }          }
841          if (index < 0) throw Exception("Could not save Region, could not find Region's sample");          if (index < 0) throw Exception("Could not save Region, could not find Region's sample");
# Line 888  namespace DLS { Line 902  namespace DLS {
902      }      }
903    
904      void Instrument::LoadRegions() {      void Instrument::LoadRegions() {
905            if (!pRegions) pRegions = new RegionList;
906          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
907          if (!lrgn) throw DLS::Exception("Mandatory chunks in <ins > chunk not found.");          if (lrgn) {
908          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
909          RIFF::List* rgn = lrgn->GetFirstSubList();              RIFF::List* rgn = lrgn->GetFirstSubList();
910          while (rgn) {              while (rgn) {
911              if (rgn->GetListType() == regionCkType) {                  if (rgn->GetListType() == regionCkType) {
912                  if (!pRegions) pRegions = new RegionList;                      pRegions->push_back(new Region(this, rgn));
913                  pRegions->push_back(new Region(this, rgn));                  }
914                    rgn = lrgn->GetNextSubList();
915              }              }
             rgn = lrgn->GetNextSubList();  
916          }          }
917      }      }
918    
919      Region* Instrument::AddRegion() {      Region* Instrument::AddRegion() {
920          if (!pRegions) pRegions = new RegionList;          if (!pRegions) LoadRegions();
921          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
922          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
923          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
924          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
925          pRegions->push_back(pNewRegion);          pRegions->push_back(pNewRegion);
926            Regions = pRegions->size();
927          return pNewRegion;          return pNewRegion;
928      }      }
929    
# Line 916  namespace DLS { Line 932  namespace DLS {
932          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
933          if (iter == pRegions->end()) return;          if (iter == pRegions->end()) return;
934          pRegions->erase(iter);          pRegions->erase(iter);
935            Regions = pRegions->size();
936          delete pRegion;          delete pRegion;
937      }      }
938    
# Line 934  namespace DLS { Line 951  namespace DLS {
951          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
952          uint8_t* pData = (uint8_t*) insh->LoadChunkData();          uint8_t* pData = (uint8_t*) insh->LoadChunkData();
953          // update 'insh' chunk          // update 'insh' chunk
954          Regions = pRegions->size();          Regions = (pRegions) ? pRegions->size() : 0;
955          midi_locale_t locale;          midi_locale_t locale;
956          locale.instrument = MIDIProgram;          locale.instrument = MIDIProgram;
957          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
# Line 943  namespace DLS { Line 960  namespace DLS {
960          memccpy(&pData[0], &Regions, 1, 4);          memccpy(&pData[0], &Regions, 1, 4);
961          memccpy(&pData[4], &locale, 2, 4);          memccpy(&pData[4], &locale, 2, 4);
962          // update Region's chunks          // update Region's chunks
963            if (!pRegions) return;
964          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
965          RegionList::iterator end  = pRegions->end();          RegionList::iterator end  = pRegions->end();
966          for (; iter != end; ++iter) {          for (; iter != end; ++iter) {
# Line 1074  namespace DLS { Line 1092  namespace DLS {
1092          if (pWavePoolTable) delete[] pWavePoolTable;          if (pWavePoolTable) delete[] pWavePoolTable;
1093          if (pWavePoolTableHi) delete[] pWavePoolTableHi;          if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1094          if (pVersion) delete pVersion;          if (pVersion) delete pVersion;
1095            for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)
1096                delete *i;
1097      }      }
1098    
1099      Sample* File::GetFirstSample() {      Sample* File::GetFirstSample() {
# Line 1090  namespace DLS { Line 1110  namespace DLS {
1110      }      }
1111    
1112      void File::LoadSamples() {      void File::LoadSamples() {
1113            if (!pSamples) pSamples = new SampleList;
1114          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1115          if (wvpl) {          if (wvpl) {
1116              unsigned long wvplFileOffset = wvpl->GetFilePos();              unsigned long wvplFileOffset = wvpl->GetFilePos();
1117              RIFF::List* wave = wvpl->GetFirstSubList();              RIFF::List* wave = wvpl->GetFirstSubList();
1118              while (wave) {              while (wave) {
1119                  if (wave->GetListType() == LIST_TYPE_WAVE) {                  if (wave->GetListType() == LIST_TYPE_WAVE) {
                     if (!pSamples) pSamples = new SampleList;  
1120                      unsigned long waveFileOffset = wave->GetFilePos();                      unsigned long waveFileOffset = wave->GetFilePos();
1121                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1122                  }                  }
# Line 1110  namespace DLS { Line 1130  namespace DLS {
1130                  RIFF::List* wave = dwpl->GetFirstSubList();                  RIFF::List* wave = dwpl->GetFirstSubList();
1131                  while (wave) {                  while (wave) {
1132                      if (wave->GetListType() == LIST_TYPE_WAVE) {                      if (wave->GetListType() == LIST_TYPE_WAVE) {
                         if (!pSamples) pSamples = new SampleList;  
1133                          unsigned long waveFileOffset = wave->GetFilePos();                          unsigned long waveFileOffset = wave->GetFilePos();
1134                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1135                      }                      }
# Line 1128  namespace DLS { Line 1147  namespace DLS {
1147       * @returns pointer to new Sample object       * @returns pointer to new Sample object
1148       */       */
1149      Sample* File::AddSample() {      Sample* File::AddSample() {
1150           if (!pSamples) LoadSamples();
1151         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
1152         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1153         // create new Sample object and its respective 'wave' list chunk         // create new Sample object and its respective 'wave' list chunk
        if (!pSamples) pSamples = new SampleList;  
1154         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
1155         Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);         Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
1156         pSamples->push_back(pSample);         pSamples->push_back(pSample);
# Line 1167  namespace DLS { Line 1186  namespace DLS {
1186      }      }
1187    
1188      void File::LoadInstruments() {      void File::LoadInstruments() {
1189            if (!pInstruments) pInstruments = new InstrumentList;
1190          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1191          if (lstInstruments) {          if (lstInstruments) {
1192              RIFF::List* lstInstr = lstInstruments->GetFirstSubList();              RIFF::List* lstInstr = lstInstruments->GetFirstSubList();
1193              while (lstInstr) {              while (lstInstr) {
1194                  if (lstInstr->GetListType() == LIST_TYPE_INS) {                  if (lstInstr->GetListType() == LIST_TYPE_INS) {
                     if (!pInstruments) pInstruments = new InstrumentList;  
1195                      pInstruments->push_back(new Instrument(this, lstInstr));                      pInstruments->push_back(new Instrument(this, lstInstr));
1196                  }                  }
1197                  lstInstr = lstInstruments->GetNextSubList();                  lstInstr = lstInstruments->GetNextSubList();
# Line 1188  namespace DLS { Line 1207  namespace DLS {
1207       * @returns pointer to new Instrument object       * @returns pointer to new Instrument object
1208       */       */
1209      Instrument* File::AddInstrument() {      Instrument* File::AddInstrument() {
1210           if (!pInstruments) LoadInstruments();
1211         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
        if (!pInstruments) pInstruments = new InstrumentList;  
1212         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1213         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
1214         Instrument* pInstrument = new Instrument(this, lstInstr);         Instrument* pInstrument = new Instrument(this, lstInstr);
# Line 1197  namespace DLS { Line 1216  namespace DLS {
1216         return pInstrument;         return pInstrument;
1217      }      }
1218    
1219      /** @brief Delete a instrument.      /** @brief Delete an instrument.
1220       *       *
1221       * This will delete the given Instrument object from the DLS file. You       * This will delete the given Instrument object from the DLS file. You
1222       * have to call Save() to make this persistent to the file.       * have to call Save() to make this persistent to the file.
# Line 1327  namespace DLS { Line 1346  namespace DLS {
1346      /**      /**
1347       * Updates (persistently) the wave pool table with offsets to all       * Updates (persistently) the wave pool table with offsets to all
1348       * currently available samples. <b>Caution:</b> this method assumes the       * currently available samples. <b>Caution:</b> this method assumes the
1349       * 'ptbl' chunk to be already of the correct size, so usually this       * 'ptbl' chunk to be already of the correct size and the file to be
1350       * method is only called after a Save() call.       * writable, so usually this method is only called after a Save() call.
1351       *       *
1352       * @throws Exception - if 'ptbl' chunk is too small (should only occur       * @throws Exception - if 'ptbl' chunk is too small (should only occur
1353       *                     if there's a bug)       *                     if there's a bug)
# Line 1341  namespace DLS { Line 1360  namespace DLS {
1360          WavePoolCount = (pSamples) ? pSamples->size() : 0;          WavePoolCount = (pSamples) ? pSamples->size() : 0;
1361          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
1362          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
1363          uint8_t* pData = (uint8_t*) ptbl->LoadChunkData();          // save the 'ptbl' chunk's current read/write position
1364            unsigned long ulOriginalPos = ptbl->GetPos();
1365          // update headers          // update headers
1366          memccpy(&pData[0], &WavePoolHeaderSize, 1, 4);          ptbl->SetPos(0);
1367          memccpy(&pData[4], &WavePoolCount, 1, 4);          ptbl->WriteUint32(&WavePoolHeaderSize);
1368            ptbl->WriteUint32(&WavePoolCount);
1369          // update offsets          // update offsets
1370            ptbl->SetPos(WavePoolHeaderSize);
1371          if (b64BitWavePoolOffsets) {          if (b64BitWavePoolOffsets) {
1372              for (int i = 0 ; i < WavePoolCount ; i++) {              for (int i = 0 ; i < WavePoolCount ; i++) {
1373                  memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTableHi[i], 1, 4);                  ptbl->WriteUint32(&pWavePoolTableHi[i]);
1374                  memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTable[i],   1, 4);                  ptbl->WriteUint32(&pWavePoolTable[i]);
1375              }              }
1376          } else { // conventional 32 bit offsets          } else { // conventional 32 bit offsets
1377              for (int i = 0 ; i < WavePoolCount ; i++)              for (int i = 0 ; i < WavePoolCount ; i++)
1378                  memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTable[i], 1, 4);                  ptbl->WriteUint32(&pWavePoolTable[i]);
1379          }          }
1380            // restore 'ptbl' chunk's original read/write position
1381            ptbl->SetPos(ulOriginalPos);
1382      }      }
1383    
1384      /**      /**
# Line 1377  namespace DLS { Line 1401  namespace DLS {
1401              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
1402              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
1403              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
1404                  uint64_t _64BitOffset = wvplFileOffset - (*iter)->pWaveList->GetFilePos() - LIST_HEADER_SIZE;                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1405                  (*iter)->ulWavePoolOffset = _64BitOffset;                  (*iter)->ulWavePoolOffset = _64BitOffset;
1406                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
1407                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;
# Line 1386  namespace DLS { Line 1410  namespace DLS {
1410              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
1411              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
1412              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
1413                  uint64_t _64BitOffset = wvplFileOffset - (*iter)->pWaveList->GetFilePos() - LIST_HEADER_SIZE;                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1414                  (*iter)->ulWavePoolOffset = _64BitOffset;                  (*iter)->ulWavePoolOffset = _64BitOffset;
1415                  pWavePoolTable[i] = (uint32_t) _64BitOffset;                  pWavePoolTable[i] = (uint32_t) _64BitOffset;
1416              }              }

Legend:
Removed from v.802  
changed lines
  Added in v.839

  ViewVC Help
Powered by ViewVC