/[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 800 by schoenebeck, Wed Nov 9 20:04:11 2005 UTC revision 809 by schoenebeck, Tue Nov 22 11:26:55 2005 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 273  namespace DLS { Line 275  namespace DLS {
275       *       *
276       * 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
277       * subchunk of INFO list chunk \a lstINFO. If the given chunk already       * subchunk of INFO list chunk \a lstINFO. If the given chunk already
278       * 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
279       * 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
280       * and \a sDefault will be applied.       * will be created and either \a s or \a sDefault will be applied
281         * (depending on which one is not an empty string, if both are not an
282         * empty string \a s will be preferred).
283       *       *
284       * @param ChunkID  - 32 bit RIFF chunk ID of INFO subchunk       * @param ChunkID  - 32 bit RIFF chunk ID of INFO subchunk
285       * @param lstINFO  - parent (INFO) RIFF list chunk       * @param lstINFO  - parent (INFO) RIFF list chunk
# Line 288  namespace DLS { Line 292  namespace DLS {
292              ck->Resize(s.size() + 1);              ck->Resize(s.size() + 1);
293              char* pData = (char*) ck->LoadChunkData();              char* pData = (char*) ck->LoadChunkData();
294              memcpy(pData, s.c_str(), s.size() + 1);              memcpy(pData, s.c_str(), s.size() + 1);
295          } else if (sDefault != "") { // create chunk and use default value          } else if (s != "" || sDefault != "") { // create chunk
296              ck = lstINFO->AddSubChunk(ChunkID, sDefault.size() + 1);              const String& sToSave = (s != "") ? s : sDefault;
297                ck = lstINFO->AddSubChunk(ChunkID, sToSave.size() + 1);
298              char* pData = (char*) ck->LoadChunkData();              char* pData = (char*) ck->LoadChunkData();
299              memcpy(pData, sDefault.c_str(), sDefault.size() + 1);              memcpy(pData, sToSave.c_str(), sToSave.size() + 1);
300          }          }
301      }      }
302    
# Line 312  namespace DLS { Line 317  namespace DLS {
317          // get current date          // get current date
318          time_t now = time(NULL);          time_t now = time(NULL);
319          tm* pNowBroken = localtime(&now);          tm* pNowBroken = localtime(&now);
320          String defaultCreationDate = ToString(pNowBroken->tm_year) + "-" +          String defaultCreationDate = ToString(1900 + pNowBroken->tm_year) + "-" +
321                                       ToString(pNowBroken->tm_mon)  + "-" +                                       ToString(pNowBroken->tm_mon + 1)  + "-" +
322                                       ToString(pNowBroken->tm_mday);                                       ToString(pNowBroken->tm_mday);
323          String defaultSoftware = libraryName() + " " + libraryVersion();          String defaultSoftware = libraryName() + " " + libraryVersion();
324          String defaultComments = "Created with " + libraryName() + " " + libraryVersion();          String defaultComments = "Created with " + libraryName() + " " + libraryVersion();
# Line 816  namespace DLS { Line 821  namespace DLS {
821          // get sample's wave pool table index          // get sample's wave pool table index
822          int index = -1;          int index = -1;
823          File* pFile = (File*) GetParent()->GetParent();          File* pFile = (File*) GetParent()->GetParent();
824          File::SampleList::iterator iter = pFile->pSamples->begin();          if (pFile->pSamples) {
825          File::SampleList::iterator end  = pFile->pSamples->end();              File::SampleList::iterator iter = pFile->pSamples->begin();
826          for (int i = 0; iter != end; ++iter, i++) {              File::SampleList::iterator end  = pFile->pSamples->end();
827              if (*iter == pSample) {              for (int i = 0; iter != end; ++iter, i++) {
828                  index = i;                  if (*iter == pSample) {
829                  break;                      index = i;
830                        break;
831                    }
832              }              }
833          }          }
834          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 889  namespace DLS { Line 896  namespace DLS {
896    
897      void Instrument::LoadRegions() {      void Instrument::LoadRegions() {
898          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
899          if (!lrgn) throw DLS::Exception("Mandatory chunks in <ins > chunk not found.");          if (!lrgn) throw DLS::Exception("DLS::Instrument doesn't seem to have any Region (mandatory chunks in <ins > chunk not found)");
900          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
901          RIFF::List* rgn = lrgn->GetFirstSubList();          RIFF::List* rgn = lrgn->GetFirstSubList();
902          while (rgn) {          while (rgn) {
# Line 912  namespace DLS { Line 919  namespace DLS {
919      }      }
920    
921      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
922            if (!pRegions) return;
923          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
924          if (iter == pRegions->end()) return;          if (iter == pRegions->end()) return;
925          pRegions->erase(iter);          pRegions->erase(iter);
# Line 933  namespace DLS { Line 941  namespace DLS {
941          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);          if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
942          uint8_t* pData = (uint8_t*) insh->LoadChunkData();          uint8_t* pData = (uint8_t*) insh->LoadChunkData();
943          // update 'insh' chunk          // update 'insh' chunk
944          Regions = pRegions->size();          Regions = (pRegions) ? pRegions->size() : 0;
945          midi_locale_t locale;          midi_locale_t locale;
946          locale.instrument = MIDIProgram;          locale.instrument = MIDIProgram;
947          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
# Line 942  namespace DLS { Line 950  namespace DLS {
950          memccpy(&pData[0], &Regions, 1, 4);          memccpy(&pData[0], &Regions, 1, 4);
951          memccpy(&pData[4], &locale, 2, 4);          memccpy(&pData[4], &locale, 2, 4);
952          // update Region's chunks          // update Region's chunks
953            if (!pRegions) return;
954          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
955          RegionList::iterator end  = pRegions->end();          RegionList::iterator end  = pRegions->end();
956          for (; iter != end; ++iter) {          for (; iter != end; ++iter) {
# Line 980  namespace DLS { Line 989  namespace DLS {
989       * to add samples, instruments and finally call Save() to actually write       * to add samples, instruments and finally call Save() to actually write
990       * a DLS file.       * a DLS file.
991       */       */
992      File::File() : pRIFF(new RIFF::File(RIFF_TYPE_DLS)), Resource(NULL, pRIFF) {      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
993          pVersion = new version_t;          pVersion = new version_t;
994          pVersion->major   = 0;          pVersion->major   = 0;
995          pVersion->minor   = 0;          pVersion->minor   = 0;
# Line 1127  namespace DLS { Line 1136  namespace DLS {
1136       * @returns pointer to new Sample object       * @returns pointer to new Sample object
1137       */       */
1138      Sample* File::AddSample() {      Sample* File::AddSample() {
1139           if (!pSamples) LoadSamples();
1140         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
1141         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1142         // create new Sample object and its respective 'wave' list chunk         // create new Sample object and its respective 'wave' list chunk
# Line 1145  namespace DLS { Line 1155  namespace DLS {
1155       * @param pSample - sample to delete       * @param pSample - sample to delete
1156       */       */
1157      void File::DeleteSample(Sample* pSample) {      void File::DeleteSample(Sample* pSample) {
1158            if (!pSamples) return;
1159          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1160          if (iter == pSamples->end()) return;          if (iter == pSamples->end()) return;
1161          pSamples->erase(iter);          pSamples->erase(iter);
# Line 1186  namespace DLS { Line 1197  namespace DLS {
1197       * @returns pointer to new Instrument object       * @returns pointer to new Instrument object
1198       */       */
1199      Instrument* File::AddInstrument() {      Instrument* File::AddInstrument() {
1200           if (!pInstruments) LoadInstruments();
1201         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
1202         if (!pInstruments) pInstruments = new InstrumentList;         if (!pInstruments) pInstruments = new InstrumentList;
1203         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
# Line 1195  namespace DLS { Line 1207  namespace DLS {
1207         return pInstrument;         return pInstrument;
1208      }      }
1209    
1210      /** @brief Delete a instrument.      /** @brief Delete an instrument.
1211       *       *
1212       * This will delete the given Instrument object from the DLS file. You       * This will delete the given Instrument object from the DLS file. You
1213       * have to call Save() to make this persistent to the file.       * have to call Save() to make this persistent to the file.
# Line 1203  namespace DLS { Line 1215  namespace DLS {
1215       * @param pInstrument - instrument to delete       * @param pInstrument - instrument to delete
1216       */       */
1217      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
1218            if (!pInstruments) return;
1219          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1220          if (iter == pInstruments->end()) return;          if (iter == pInstruments->end()) return;
1221          pInstruments->erase(iter);          pInstruments->erase(iter);
# Line 1324  namespace DLS { Line 1337  namespace DLS {
1337      /**      /**
1338       * Updates (persistently) the wave pool table with offsets to all       * Updates (persistently) the wave pool table with offsets to all
1339       * currently available samples. <b>Caution:</b> this method assumes the       * currently available samples. <b>Caution:</b> this method assumes the
1340       * '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
1341       * method is only called after a Save() call.       * writable, so usually this method is only called after a Save() call.
1342       *       *
1343       * @throws Exception - if 'ptbl' chunk is too small (should only occur       * @throws Exception - if 'ptbl' chunk is too small (should only occur
1344       *                     if there's a bug)       *                     if there's a bug)
# Line 1335  namespace DLS { Line 1348  namespace DLS {
1348          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1349          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;          const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1350          // check if 'ptbl' chunk is large enough          // check if 'ptbl' chunk is large enough
1351          WavePoolCount = pSamples->size();          WavePoolCount = (pSamples) ? pSamples->size() : 0;
1352          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;          const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
1353          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");          if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
1354          uint8_t* pData = (uint8_t*) ptbl->LoadChunkData();          // save the 'ptbl' chunk's current read/write position
1355            unsigned long ulOriginalPos = ptbl->GetPos();
1356          // update headers          // update headers
1357          memccpy(&pData[0], &WavePoolHeaderSize, 1, 4);          ptbl->SetPos(0);
1358          memccpy(&pData[4], &WavePoolCount, 1, 4);          ptbl->WriteUint32(&WavePoolHeaderSize);
1359            ptbl->WriteUint32(&WavePoolCount);
1360          // update offsets          // update offsets
1361            ptbl->SetPos(WavePoolHeaderSize);
1362          if (b64BitWavePoolOffsets) {          if (b64BitWavePoolOffsets) {
1363              for (int i = 0 ; i < WavePoolCount ; i++) {              for (int i = 0 ; i < WavePoolCount ; i++) {
1364                  memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTableHi[i], 1, 4);                  ptbl->WriteUint32(&pWavePoolTableHi[i]);
1365                  memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTable[i],   1, 4);                  ptbl->WriteUint32(&pWavePoolTable[i]);
1366              }              }
1367          } else { // conventional 32 bit offsets          } else { // conventional 32 bit offsets
1368              for (int i = 0 ; i < WavePoolCount ; i++)              for (int i = 0 ; i < WavePoolCount ; i++)
1369                  memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTable[i], 1, 4);                  ptbl->WriteUint32(&pWavePoolTable[i]);
1370          }          }
1371            // restore 'ptbl' chunk's original read/write position
1372            ptbl->SetPos(ulOriginalPos);
1373      }      }
1374    
1375      /**      /**
# Line 1360  namespace DLS { Line 1378  namespace DLS {
1378       * exists already.       * exists already.
1379       */       */
1380      void File::__UpdateWavePoolTable() {      void File::__UpdateWavePoolTable() {
1381          WavePoolCount = pSamples->size();          WavePoolCount = (pSamples) ? pSamples->size() : 0;
1382          // resize wave pool table arrays          // resize wave pool table arrays
1383          if (pWavePoolTable)   delete[] pWavePoolTable;          if (pWavePoolTable)   delete[] pWavePoolTable;
1384          if (pWavePoolTableHi) delete[] pWavePoolTableHi;          if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1385          pWavePoolTable   = new uint32_t[WavePoolCount];          pWavePoolTable   = new uint32_t[WavePoolCount];
1386          pWavePoolTableHi = new uint32_t[WavePoolCount];          pWavePoolTableHi = new uint32_t[WavePoolCount];
1387            if (!pSamples) return;
1388          // update offsets int wave pool table          // update offsets int wave pool table
1389          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1390          uint64_t wvplFileOffset = wvpl->GetFilePos();          uint64_t wvplFileOffset = wvpl->GetFilePos();
# Line 1373  namespace DLS { Line 1392  namespace DLS {
1392              SampleList::iterator iter = pSamples->begin();              SampleList::iterator iter = pSamples->begin();
1393              SampleList::iterator end  = pSamples->end();              SampleList::iterator end  = pSamples->end();
1394              for (int i = 0 ; iter != end ; ++iter, i++) {              for (int i = 0 ; iter != end ; ++iter, i++) {
1395                  uint64_t _64BitOffset = wvplFileOffset - (*iter)->pWaveList->GetFilePos() - LIST_HEADER_SIZE;                  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1396                  (*iter)->ulWavePoolOffset = _64BitOffset;                  (*iter)->ulWavePoolOffset = _64BitOffset;
1397                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);                  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
1398                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;                  pWavePoolTable[i]   = (uint32_t) _64BitOffset;
# Line 1382  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                  pWavePoolTable[i] = (uint32_t) _64BitOffset;                  pWavePoolTable[i] = (uint32_t) _64BitOffset;
1407              }              }

Legend:
Removed from v.800  
changed lines
  Added in v.809

  ViewVC Help
Powered by ViewVC