/[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 834 by persson, Mon Feb 6 17:58:21 2006 UTC revision 1154 by schoenebeck, Wed Apr 11 16:33:56 2007 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file loader library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2005 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2007 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 45  Line 45 
45  #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x)             ((x) ? 0x8000 : 0)  #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x)             ((x) ? 0x8000 : 0)
46  #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x)             ((x) ? 0x0200 : 0)  #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x)             ((x) ? 0x0200 : 0)
47    
48  #define DRUM_TYPE_MASK                  0x00000001  #define DRUM_TYPE_MASK                  0x80000000
49    
50  #define F_RGN_OPTION_SELFNONEXCLUSIVE   0x0001  #define F_RGN_OPTION_SELFNONEXCLUSIVE   0x0001
51    
# Line 142  namespace DLS { Line 142  namespace DLS {
142          const int iEntrySize = 12; // 12 bytes per connection block          const int iEntrySize = 12; // 12 bytes per connection block
143          pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);          pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
144          uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();          uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
145          memccpy(&pData[0], &HeaderSize, 1, 2);          memcpy(&pData[0], &HeaderSize, 2);
146          memccpy(&pData[2], &Connections, 1, 2);          memcpy(&pData[2], &Connections, 2);
147          for (uint32_t i = 0; i < Connections; i++) {          for (uint32_t i = 0; i < Connections; i++) {
148              Connection::conn_block_t c = pConnections[i].ToConnBlock();              Connection::conn_block_t c = pConnections[i].ToConnBlock();
149              memccpy(&pData[HeaderSize + i * iEntrySize],     &c.source, 1, 2);              memcpy(&pData[HeaderSize + i * iEntrySize],     &c.source, 2);
150              memccpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 1, 2);              memcpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 2);
151              memccpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 1, 2);              memcpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 2);
152              memccpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 1, 2);              memcpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 2);
153              memccpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 1, 4);              memcpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 4);
154          }          }
155      }      }
156    
# Line 228  namespace DLS { Line 228  namespace DLS {
228    
229      /** @brief Constructor.      /** @brief Constructor.
230       *       *
231       * Initializes the info strings with values provided by a INFO list chunk.       * Initializes the info strings with values provided by an INFO list chunk.
232       *       *
233       * @param list - pointer to a list chunk which contains a INFO list chunk       * @param list - pointer to a list chunk which contains an INFO list chunk
234       */       */
235      Info::Info(RIFF::List* list) {      Info::Info(RIFF::List* list) {
236            UseFixedLengthStrings = false;
237          pResourceListChunk = list;          pResourceListChunk = list;
238          if (list) {          if (list) {
239              RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);              RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
# Line 253  namespace DLS { Line 254  namespace DLS {
254                  LoadString(CHUNK_ID_ISRC, lstINFO, Source);                  LoadString(CHUNK_ID_ISRC, lstINFO, Source);
255                  LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);                  LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
256                  LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);                  LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
257                    LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
258              }              }
259          }          }
260      }      }
# Line 267  namespace DLS { Line 269  namespace DLS {
269       */       */
270      void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {      void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
271          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
272          if (ck) {          ::LoadString(ck, s); // function from helper.h
             // TODO: no check for ZSTR terminated strings yet  
             s.assign((char*) ck->LoadChunkData(), ck->GetSize());  
             ck->ReleaseChunkData();  
         }  
273      }      }
274    
275      /** @brief Apply given INFO field to the respective chunk.      /** @brief Apply given INFO field to the respective chunk.
# Line 288  namespace DLS { Line 286  namespace DLS {
286       * @param lstINFO  - parent (INFO) RIFF list chunk       * @param lstINFO  - parent (INFO) RIFF list chunk
287       * @param s        - current value of info field       * @param s        - current value of info field
288       * @param sDefault - default value       * @param sDefault - default value
289         * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
290         * @param size     - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
291       */       */
292      void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) {      void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault, bool bUseFixedLengthStrings, int size) {
293          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
294          if (ck) { // if chunk exists already, use 's' as value          ::SaveString(ChunkID, ck, lstINFO, s, sDefault, bUseFixedLengthStrings, size); // function from helper.h
             ck->Resize(s.size() + 1);  
             char* pData = (char*) ck->LoadChunkData();  
             memcpy(pData, s.c_str(), s.size() + 1);  
         } else if (s != "" || sDefault != "") { // create chunk  
             const String& sToSave = (s != "") ? s : sDefault;  
             ck = lstINFO->AddSubChunk(ChunkID, sToSave.size() + 1);  
             char* pData = (char*) ck->LoadChunkData();  
             memcpy(pData, sToSave.c_str(), sToSave.size() + 1);  
         }  
295      }      }
296    
297      /** @brief Update chunks with current info values.      /** @brief Update chunks with current info values.
# Line 313  namespace DLS { Line 304  namespace DLS {
304    
305          // make sure INFO list chunk exists          // make sure INFO list chunk exists
306          RIFF::List* lstINFO   = pResourceListChunk->GetSubList(LIST_TYPE_INFO);          RIFF::List* lstINFO   = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
         if (!lstINFO) lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);  
307    
308          // assemble default values in case the respective chunk is missing yet          String defaultName = "";
309          String defaultName = "NONAME";          String defaultCreationDate = "";
310          // get current date          String defaultSoftware = "";
311          time_t now = time(NULL);          String defaultComments = "";
312          tm* pNowBroken = localtime(&now);  
313          String defaultCreationDate = ToString(1900 + pNowBroken->tm_year) + "-" +          uint32_t resourceType = pResourceListChunk->GetListType();
314                                       ToString(pNowBroken->tm_mon + 1)  + "-" +  
315                                       ToString(pNowBroken->tm_mday);          if (!lstINFO) {
316          String defaultSoftware = libraryName() + " " + libraryVersion();              lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
317          String defaultComments = "Created with " + libraryName() + " " + libraryVersion();  
318                // assemble default values
319                defaultName = "NONAME";
320    
321                if (resourceType == RIFF_TYPE_DLS) {
322                    // get current date
323                    time_t now = time(NULL);
324                    tm* pNowBroken = localtime(&now);
325                    char buf[11];
326                    strftime(buf, 11, "%F", pNowBroken);
327                    defaultCreationDate = buf;
328    
329                    defaultComments = "Created with " + libraryName() + " " + libraryVersion();
330                }
331                if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS)
332                {
333                    defaultSoftware = libraryName() + " " + libraryVersion();
334                }
335            }
336    
337          // save values          // save values
338          SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName);  
339          SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));          // (the string size values are for gig files; they are only
340          SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);          // used if UseFixedLengthStrings is set to true)
341          SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);          SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName, UseFixedLengthStrings,
342          SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""));                     resourceType == RIFF_TYPE_DLS ? 128 : 64);
343          SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));          SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""), UseFixedLengthStrings, 256);
344          SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));          SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate, UseFixedLengthStrings, 128);
345          SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));          SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments, UseFixedLengthStrings, 1024);
346          SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));          SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""), UseFixedLengthStrings, 128);
347          SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));          SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""), UseFixedLengthStrings, 128);
348          SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));          SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""), UseFixedLengthStrings, 128);
349          SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware);          SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""), UseFixedLengthStrings, 128);
350          SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""));          SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""), UseFixedLengthStrings, 128);
351          SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""));          SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""), UseFixedLengthStrings, 128);
352          SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));          SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), UseFixedLengthStrings, 128);
353          SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));          SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, UseFixedLengthStrings,
354                       resourceType == LIST_TYPE_INS ?
355                       (Software == "" ? defaultSoftware.length()+1 : Software.length()+1) : 128);
356            SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), UseFixedLengthStrings, 128);
357            SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), UseFixedLengthStrings, 128);
358            SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), UseFixedLengthStrings, 128);
359            SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""), UseFixedLengthStrings, 128);
360            SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""), UseFixedLengthStrings, 128);
361      }      }
362    
363    
# Line 448  namespace DLS { Line 463  namespace DLS {
463          }          }
464          uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();          uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
465          // update headers size          // update headers size
466          memccpy(&pData[0], &uiHeaderSize, 1, 4);          memcpy(&pData[0], &uiHeaderSize, 4);
467          // update respective sampler options bits          // update respective sampler options bits
468          SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION          SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION
469                                                     : SamplerOptions & (~F_WSMP_NO_TRUNCATION);                                                     : SamplerOptions & (~F_WSMP_NO_TRUNCATION);
470          SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION          SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION
471                                                 : SamplerOptions & (~F_WSMP_NO_COMPRESSION);                                                 : SamplerOptions & (~F_WSMP_NO_COMPRESSION);
472            memcpy(&pData[4], &UnityNote, 2);
473            memcpy(&pData[6], &FineTune, 2);
474            memcpy(&pData[8], &Gain, 4);
475            memcpy(&pData[12], &SamplerOptions, 4);
476            memcpy(&pData[16], &SampleLoops, 4);
477          // update loop definitions          // update loop definitions
478          for (uint32_t i = 0; i < SampleLoops; i++) {          for (uint32_t i = 0; i < SampleLoops; i++) {
479              //FIXME: this does not handle extended loop structs correctly              //FIXME: this does not handle extended loop structs correctly
480              memccpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4, 4);              memcpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4 * 4);
481          }          }
482      }      }
483    
484        /**
485         * Adds a new sample loop with the provided loop definition.
486         *
487         * @param - points to a loop definition that is to be copied
488         */
489        void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) {
490            sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1];
491            // copy old loops array
492            for (int i = 0; i < SampleLoops; i++) {
493                pNewLoops[i] = pSampleLoops[i];
494            }
495            // add the new loop
496            pNewLoops[SampleLoops] = *pLoopDef;
497            // free the old array and update the member variables
498            if (SampleLoops) delete[] pSampleLoops;
499            pSampleLoops = pNewLoops;
500            SampleLoops++;
501        }
502    
503        /**
504         * Deletes an existing sample loop.
505         *
506         * @param pLoopDef - pointer to existing loop definition
507         * @throws Exception - if given loop definition does not exist
508         */
509        void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) {
510            sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1];
511            // copy old loops array (skipping given loop)
512            for (int i = 0, o = 0; i < SampleLoops; i++) {
513                if (&pSampleLoops[i] == pLoopDef) continue;
514                if (o == SampleLoops - 1)
515                    throw Exception("Could not delete Sample Loop, because it does not exist");
516                pNewLoops[o] = pSampleLoops[i];
517                o++;
518            }
519            // free the old array and update the member variables
520            if (SampleLoops) delete[] pSampleLoops;
521            pSampleLoops = pNewLoops;
522            SampleLoops--;
523        }
524    
525    
526    
527  // *************** Sample ***************  // *************** Sample ***************
# Line 494  namespace DLS { Line 555  namespace DLS {
555              AverageBytesPerSecond  = pCkFormat->ReadUint32();              AverageBytesPerSecond  = pCkFormat->ReadUint32();
556              BlockAlign             = pCkFormat->ReadUint16();              BlockAlign             = pCkFormat->ReadUint16();
557              // PCM format specific              // PCM format specific
558              if (FormatTag == WAVE_FORMAT_PCM) {              if (FormatTag == DLS_WAVE_FORMAT_PCM) {
559                  BitDepth     = pCkFormat->ReadUint16();                  BitDepth     = pCkFormat->ReadUint16();
560                  FrameSize    = (FormatTag == WAVE_FORMAT_PCM) ? (BitDepth / 8) * Channels                  FrameSize    = (BitDepth / 8) * Channels;
                                                             : 0;  
561              } else { // unsupported sample data format              } else { // unsupported sample data format
562                  BitDepth     = 0;                  BitDepth     = 0;
563                  FrameSize    = 0;                  FrameSize    = 0;
564              }              }
565          } else { // 'fmt' chunk missing          } else { // 'fmt' chunk missing
566              FormatTag              = WAVE_FORMAT_PCM;              FormatTag              = DLS_WAVE_FORMAT_PCM;
567              BitDepth               = 16;              BitDepth               = 16;
568              Channels               = 1;              Channels               = 1;
569              SamplesPerSecond       = 44100;              SamplesPerSecond       = 44100;
# Line 511  namespace DLS { Line 571  namespace DLS {
571              FrameSize              = (BitDepth / 8) * Channels;              FrameSize              = (BitDepth / 8) * Channels;
572              BlockAlign             = FrameSize;              BlockAlign             = FrameSize;
573          }          }
574          SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize          SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize
575                                                                    : 0                                                                        : 0
576                                   : 0;                                   : 0;
577      }      }
578    
# Line 572  namespace DLS { Line 632  namespace DLS {
632       * the RIFF chunk which encapsulates the sample's wave data. The       * the RIFF chunk which encapsulates the sample's wave data. The
633       * returned value is dependant to the current FrameSize value.       * returned value is dependant to the current FrameSize value.
634       *       *
635       * @returns number of sample points or 0 if FormatTag != WAVE_FORMAT_PCM       * @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM
636       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
637       */       */
638      unsigned long Sample::GetSize() {      unsigned long Sample::GetSize() {
639          if (FormatTag != WAVE_FORMAT_PCM) return 0;          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
640          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
641      }      }
642    
# Line 598  namespace DLS { Line 658  namespace DLS {
658       * calling File::Save() as this might exceed the current sample's       * calling File::Save() as this might exceed the current sample's
659       * boundary!       * boundary!
660       *       *
661       * Also note: only WAVE_FORMAT_PCM is currently supported, that is       * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
662       * FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
663       * other formats will fail!       * other formats will fail!
664       *       *
665       * @param iNewSize - new sample wave data size in sample points (must be       * @param iNewSize - new sample wave data size in sample points (must be
666       *                   greater than zero)       *                   greater than zero)
667       * @throws Excecption if FormatTag != WAVE_FORMAT_PCM       * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM
668       * @throws Exception if \a iNewSize is less than 1       * @throws Exception if \a iNewSize is less than 1
669       * @see File::Save(), FrameSize, FormatTag       * @see File::Save(), FrameSize, FormatTag
670       */       */
671      void Sample::Resize(int iNewSize) {      void Sample::Resize(int iNewSize) {
672          if (FormatTag != WAVE_FORMAT_PCM) throw Exception("Sample's format is not WAVE_FORMAT_PCM");          if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");
673          if (iNewSize < 1) throw Exception("Sample size must be at least one sample point");          if (iNewSize < 1) throw Exception("Sample size must be at least one sample point");
674          const int iSizeInBytes = iNewSize * FrameSize;          const int iSizeInBytes = iNewSize * FrameSize;
675          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
# Line 622  namespace DLS { Line 682  namespace DLS {
682       * bytes). Use this method and <i>Read()</i> if you don't want to load       * bytes). Use this method and <i>Read()</i> if you don't want to load
683       * the sample into RAM, thus for disk streaming.       * the sample into RAM, thus for disk streaming.
684       *       *
685       * Also note: only WAVE_FORMAT_PCM is currently supported, that is       * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
686       * FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample
687       * with other formats will fail!       * with other formats will fail!
688       *       *
689       * @param SampleCount  number of sample points       * @param SampleCount  number of sample points
690       * @param Whence       to which relation \a SampleCount refers to       * @param Whence       to which relation \a SampleCount refers to
691       * @returns new position within the sample, 0 if       * @returns new position within the sample, 0 if
692       *          FormatTag != WAVE_FORMAT_PCM       *          FormatTag != DLS_WAVE_FORMAT_PCM
693       * @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
694       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
695       */       */
696      unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {      unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {
697          if (FormatTag != 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
698          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");
699          unsigned long orderedBytes = SampleCount * FrameSize;          unsigned long orderedBytes = SampleCount * FrameSize;
700          unsigned long result = pCkData->SetPos(orderedBytes, Whence);          unsigned long result = pCkData->SetPos(orderedBytes, Whence);
# Line 652  namespace DLS { Line 712  namespace DLS {
712       * @param SampleCount  number of sample points to read       * @param SampleCount  number of sample points to read
713       */       */
714      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {
715          if (FormatTag != 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
716          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?
717      }      }
718    
# Line 672  namespace DLS { Line 732  namespace DLS {
732       * @see LoadSampleData()       * @see LoadSampleData()
733       */       */
734      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {
735          if (FormatTag != 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
736          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");
737          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?
738      }      }
# Line 681  namespace DLS { Line 741  namespace DLS {
741       * Apply sample and its settings to the respective RIFF chunks. You have       * Apply sample and its settings to the respective RIFF chunks. You have
742       * to call File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
743       *       *
744       * @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data       * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data
745       *                   was provided yet       *                   was provided yet
746       */       */
747      void Sample::UpdateChunks() {      void Sample::UpdateChunks() {
748          if (FormatTag != WAVE_FORMAT_PCM)          if (FormatTag != DLS_WAVE_FORMAT_PCM)
749              throw Exception("Could not save sample, only PCM format is supported");              throw Exception("Could not save sample, only PCM format is supported");
750          // we refuse to do anything if not sample wave form was provided yet          // we refuse to do anything if not sample wave form was provided yet
751          if (!pCkData)          if (!pCkData)
# Line 697  namespace DLS { Line 757  namespace DLS {
757          if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format          if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
758          uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();          uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();
759          // update 'fmt' chunk          // update 'fmt' chunk
760          memccpy(&pData[0], &FormatTag, 1, 2);          memcpy(&pData[0], &FormatTag, 2);
761          memccpy(&pData[2], &Channels,  1, 2);          memcpy(&pData[2], &Channels,  2);
762          memccpy(&pData[4], &SamplesPerSecond, 1, 4);          memcpy(&pData[4], &SamplesPerSecond, 4);
763          memccpy(&pData[8], &AverageBytesPerSecond, 1, 4);          memcpy(&pData[8], &AverageBytesPerSecond, 4);
764          memccpy(&pData[12], &BlockAlign, 1, 2);          memcpy(&pData[12], &BlockAlign, 2);
765          memccpy(&pData[14], &BitDepth, 1, 2); // assuming PCM format          memcpy(&pData[14], &BitDepth, 2); // assuming PCM format
766      }      }
767    
768    
# Line 794  namespace DLS { Line 854  namespace DLS {
854      void Region::UpdateChunks() {      void Region::UpdateChunks() {
855          // make sure 'rgnh' chunk exists          // make sure 'rgnh' chunk exists
856          RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);          RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);
857          if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, 14);          if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
858          uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();          uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();
859          FormatOptionFlags = (SelfNonExclusive)          FormatOptionFlags = (SelfNonExclusive)
860                                  ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE                                  ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE
861                                  : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);                                  : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);
862          // update 'rgnh' chunk          // update 'rgnh' chunk
863          memccpy(&pData[0], &KeyRange, 2, 2);          memcpy(&pData[0], &KeyRange, 2 * 2);
864          memccpy(&pData[4], &VelocityRange, 2, 2);          memcpy(&pData[4], &VelocityRange, 2 * 2);
865          memccpy(&pData[8], &FormatOptionFlags, 1, 2);          memcpy(&pData[8], &FormatOptionFlags, 2);
866          memccpy(&pData[10], &KeyGroup, 1, 2);          memcpy(&pData[10], &KeyGroup, 2);
867          memccpy(&pData[12], &Layer, 1, 2);          if (rgnh->GetSize() >= 14) memcpy(&pData[12], &Layer, 2);
868    
869          // update chunks of base classes as well          // update chunks of base classes as well (but skip Resource,
870          Resource::UpdateChunks();          // as a rgn doesn't seem to have dlid and INFO chunks)
871          Articulator::UpdateChunks();          Articulator::UpdateChunks();
872          Sampler::UpdateChunks();          Sampler::UpdateChunks();
873    
# Line 837  namespace DLS { Line 897  namespace DLS {
897          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");
898          WavePoolTableIndex = index;          WavePoolTableIndex = index;
899          // update 'wlnk' chunk          // update 'wlnk' chunk
900          memccpy(&pData[0], &WaveLinkOptionFlags, 1, 2);          memcpy(&pData[0], &WaveLinkOptionFlags, 2);
901          memccpy(&pData[2], &PhaseGroup, 1, 2);          memcpy(&pData[2], &PhaseGroup, 2);
902          memccpy(&pData[4], &Channel, 1, 4);          memcpy(&pData[4], &Channel, 4);
903          memccpy(&pData[8], &WavePoolTableIndex, 1, 4);          memcpy(&pData[8], &WavePoolTableIndex, 4);
904      }      }
905    
906    
# Line 923  namespace DLS { Line 983  namespace DLS {
983          return pNewRegion;          return pNewRegion;
984      }      }
985    
986        void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
987            RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
988            lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0);
989    
990            pRegions->remove(pSrc);
991            RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
992            pRegions->insert(iter, pSrc);
993        }
994    
995      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
996          if (!pRegions) return;          if (!pRegions) return;
997          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
# Line 953  namespace DLS { Line 1022  namespace DLS {
1022          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
1023          locale.bank       = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK);          locale.bank       = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK);
1024          MIDIBank          = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it          MIDIBank          = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it
1025          memccpy(&pData[0], &Regions, 1, 4);          memcpy(&pData[0], &Regions, 4);
1026          memccpy(&pData[4], &locale, 2, 4);          memcpy(&pData[4], &locale, 2 * 4);
1027          // update Region's chunks          // update Region's chunks
1028          if (!pRegions) return;          if (!pRegions) return;
1029          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
# Line 1039  namespace DLS { Line 1108  namespace DLS {
1108          Instruments = colh->ReadUint32();          Instruments = colh->ReadUint32();
1109    
1110          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1111          if (!ptbl) throw DLS::Exception("Mandatory <ptbl> chunk not found.");          if (!ptbl) { // pool table is missing - this is probably an ".art" file
1112          WavePoolHeaderSize = ptbl->ReadUint32();              WavePoolCount    = 0;
1113          WavePoolCount  = ptbl->ReadUint32();              pWavePoolTable   = NULL;
1114          pWavePoolTable = new uint32_t[WavePoolCount];              pWavePoolTableHi = NULL;
1115          pWavePoolTableHi = new uint32_t[WavePoolCount];              WavePoolHeaderSize = 8;
1116          ptbl->SetPos(WavePoolHeaderSize);              b64BitWavePoolOffsets = false;
1117            } else {
1118          // Check for 64 bit offsets (used in gig v3 files)              WavePoolHeaderSize = ptbl->ReadUint32();
1119          b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8);              WavePoolCount  = ptbl->ReadUint32();
1120          if (b64BitWavePoolOffsets) {              pWavePoolTable = new uint32_t[WavePoolCount];
1121              for (int i = 0 ; i < WavePoolCount ; i++) {              pWavePoolTableHi = new uint32_t[WavePoolCount];
1122                  pWavePoolTableHi[i] = ptbl->ReadUint32();              ptbl->SetPos(WavePoolHeaderSize);
1123                  pWavePoolTable[i] = ptbl->ReadUint32();  
1124                  if (pWavePoolTable[i] & 0x80000000)              // Check for 64 bit offsets (used in gig v3 files)
1125                      throw DLS::Exception("Files larger than 2 GB not yet supported");              b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8);
1126                if (b64BitWavePoolOffsets) {
1127                    for (int i = 0 ; i < WavePoolCount ; i++) {
1128                        pWavePoolTableHi[i] = ptbl->ReadUint32();
1129                        pWavePoolTable[i] = ptbl->ReadUint32();
1130                        if (pWavePoolTable[i] & 0x80000000)
1131                            throw DLS::Exception("Files larger than 2 GB not yet supported");
1132                    }
1133                } else { // conventional 32 bit offsets
1134                    ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
1135                    for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0;
1136              }              }
         } else { // conventional 32 bit offsets  
             ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));  
             for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0;  
1137          }          }
1138    
1139          pSamples     = NULL;          pSamples     = NULL;
# Line 1243  namespace DLS { Line 1319  namespace DLS {
1319              RIFF::Chunk* ckVersion    = pRIFF->GetSubChunk(CHUNK_ID_VERS);              RIFF::Chunk* ckVersion    = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1320              if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);              if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);
1321              uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();              uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();
1322              memccpy(pData, pVersion, 2, 4);              memcpy(pData, pVersion, 2 * 4);
1323          }          }
1324    
1325          // update 'colh' chunk          // update 'colh' chunk
# Line 1251  namespace DLS { Line 1327  namespace DLS {
1327          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1328          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1329          uint8_t* pData = (uint8_t*) colh->LoadChunkData();          uint8_t* pData = (uint8_t*) colh->LoadChunkData();
1330          memccpy(pData, &Instruments, 1, 4);          memcpy(pData, &Instruments, 4);
1331    
1332          // update instrument's chunks          // update instrument's chunks
1333          if (pInstruments) {          if (pInstruments) {
# Line 1271  namespace DLS { Line 1347  namespace DLS {
1347          ptbl->Resize(iPtblSize);          ptbl->Resize(iPtblSize);
1348          pData = (uint8_t*) ptbl->LoadChunkData();          pData = (uint8_t*) ptbl->LoadChunkData();
1349          WavePoolCount = iSamples;          WavePoolCount = iSamples;
1350          memccpy(&pData[4], &WavePoolCount, 1, 4);          memcpy(&pData[4], &WavePoolCount, 4);
1351          // we actually update the sample offsets in the pool table when we Save()          // we actually update the sample offsets in the pool table when we Save()
1352          memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);          memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);
1353    

Legend:
Removed from v.834  
changed lines
  Added in v.1154

  ViewVC Help
Powered by ViewVC