/[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 902 by persson, Sat Jul 22 14:22:01 2006 UTC revision 2394 by schoenebeck, Mon Jan 7 23:23:58 2013 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-2013 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 23  Line 23 
23    
24  #include "DLS.h"  #include "DLS.h"
25    
26    #include <algorithm>
27  #include <time.h>  #include <time.h>
28    
29    #ifdef __APPLE__
30    #include <CoreFoundation/CFUUID.h>
31    #elif defined(HAVE_UUID_UUID_H)
32    #include <uuid/uuid.h>
33    #endif
34    
35  #include "helper.h"  #include "helper.h"
36    
37  // macros to decode connection transforms  // macros to decode connection transforms
# Line 45  Line 52 
52  #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x)             ((x) ? 0x8000 : 0)  #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x)             ((x) ? 0x8000 : 0)
53  #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x)             ((x) ? 0x0200 : 0)  #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x)             ((x) ? 0x0200 : 0)
54    
55  #define DRUM_TYPE_MASK                  0x00000001  #define DRUM_TYPE_MASK                  0x80000000
56    
57  #define F_RGN_OPTION_SELFNONEXCLUSIVE   0x0001  #define F_RGN_OPTION_SELFNONEXCLUSIVE   0x0001
58    
# Line 142  namespace DLS { Line 149  namespace DLS {
149          const int iEntrySize = 12; // 12 bytes per connection block          const int iEntrySize = 12; // 12 bytes per connection block
150          pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);          pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
151          uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();          uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
152          memccpy(&pData[0], &HeaderSize, 1, 2);          store16(&pData[0], HeaderSize);
153          memccpy(&pData[2], &Connections, 1, 2);          store16(&pData[2], Connections);
154          for (uint32_t i = 0; i < Connections; i++) {          for (uint32_t i = 0; i < Connections; i++) {
155              Connection::conn_block_t c = pConnections[i].ToConnBlock();              Connection::conn_block_t c = pConnections[i].ToConnBlock();
156              memccpy(&pData[HeaderSize + i * iEntrySize],     &c.source, 1, 2);              store16(&pData[HeaderSize + i * iEntrySize],     c.source);
157              memccpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 1, 2);              store16(&pData[HeaderSize + i * iEntrySize + 2], c.control);
158              memccpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 1, 2);              store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination);
159              memccpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 1, 2);              store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform);
160              memccpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 1, 4);              store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale);
161          }          }
162      }      }
163    
# Line 220  namespace DLS { Line 227  namespace DLS {
227              }              }
228          }          }
229      }      }
230        
231        /**
232         * Not yet implemented in this version, since the .gig format does
233         * not need to copy DLS articulators and so far nobody used pure
234         * DLS instrument AFAIK.
235         */
236        void Articulator::CopyAssign(const Articulator* orig) {
237            //TODO: implement deep copy assignment for this class
238        }
239    
240    
241    
# Line 228  namespace DLS { Line 244  namespace DLS {
244    
245      /** @brief Constructor.      /** @brief Constructor.
246       *       *
247       * Initializes the info strings with values provided by a INFO list chunk.       * Initializes the info strings with values provided by an INFO list chunk.
248       *       *
249       * @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
250       */       */
251      Info::Info(RIFF::List* list) {      Info::Info(RIFF::List* list) {
252            pFixedStringLengths = NULL;
253          pResourceListChunk = list;          pResourceListChunk = list;
254          if (list) {          if (list) {
255              RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);              RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
# Line 253  namespace DLS { Line 270  namespace DLS {
270                  LoadString(CHUNK_ID_ISRC, lstINFO, Source);                  LoadString(CHUNK_ID_ISRC, lstINFO, Source);
271                  LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);                  LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
272                  LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);                  LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
273                    LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
274              }              }
275          }          }
276      }      }
# Line 260  namespace DLS { Line 278  namespace DLS {
278      Info::~Info() {      Info::~Info() {
279      }      }
280    
281        /**
282         * Forces specific Info fields to be of a fixed length when being saved
283         * to a file. By default the respective RIFF chunk of an Info field
284         * will have a size analogue to its actual string length. With this
285         * method however this behavior can be overridden, allowing to force an
286         * arbitrary fixed size individually for each Info field.
287         *
288         * This method is used as a workaround for the gig format, not for DLS.
289         *
290         * @param lengths - NULL terminated array of string_length_t elements
291         */
292        void Info::SetFixedStringLengths(const string_length_t* lengths) {
293            pFixedStringLengths = lengths;
294        }
295    
296      /** @brief Load given INFO field.      /** @brief Load given INFO field.
297       *       *
298       * 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 267  namespace DLS { Line 300  namespace DLS {
300       */       */
301      void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {      void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
302          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
303          if (ck) {          ::LoadString(ck, s); // function from helper.h
             const char* str = (char*)ck->LoadChunkData();  
             int size = ck->GetSize();  
             int len;  
             for (len = 0 ; len < size ; len++)  
                 if (str[len] == '\0') break;  
             s.assign(str, len);  
             ck->ReleaseChunkData();  
         }  
304      }      }
305    
306      /** @brief Apply given INFO field to the respective chunk.      /** @brief Apply given INFO field to the respective chunk.
# Line 294  namespace DLS { Line 319  namespace DLS {
319       * @param sDefault - default value       * @param sDefault - default value
320       */       */
321      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) {
322          RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);          int size = 0;
323          if (ck) { // if chunk exists already, use 's' as value          if (pFixedStringLengths) {
324              ck->Resize(s.size() + 1);              for (int i = 0 ; pFixedStringLengths[i].length ; i++) {
325              char* pData = (char*) ck->LoadChunkData();                  if (pFixedStringLengths[i].chunkId == ChunkID) {
326              memcpy(pData, s.c_str(), s.size() + 1);                      size = pFixedStringLengths[i].length;
327          } else if (s != "" || sDefault != "") { // create chunk                      break;
328              const String& sToSave = (s != "") ? s : sDefault;                  }
329              ck = lstINFO->AddSubChunk(ChunkID, sToSave.size() + 1);              }
             char* pData = (char*) ck->LoadChunkData();  
             memcpy(pData, sToSave.c_str(), sToSave.size() + 1);  
330          }          }
331            RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
332            ::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h
333      }      }
334    
335      /** @brief Update chunks with current info values.      /** @brief Update chunks with current info values.
# Line 317  namespace DLS { Line 342  namespace DLS {
342    
343          // make sure INFO list chunk exists          // make sure INFO list chunk exists
344          RIFF::List* lstINFO   = pResourceListChunk->GetSubList(LIST_TYPE_INFO);          RIFF::List* lstINFO   = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
         if (!lstINFO) lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);  
345    
346          // assemble default values in case the respective chunk is missing yet          String defaultName = "";
347          String defaultName = "NONAME";          String defaultCreationDate = "";
348          // get current date          String defaultSoftware = "";
349          time_t now = time(NULL);          String defaultComments = "";
350          tm* pNowBroken = localtime(&now);  
351          String defaultCreationDate = ToString(1900 + pNowBroken->tm_year) + "-" +          uint32_t resourceType = pResourceListChunk->GetListType();
352                                       ToString(pNowBroken->tm_mon + 1)  + "-" +  
353                                       ToString(pNowBroken->tm_mday);          if (!lstINFO) {
354          String defaultSoftware = libraryName() + " " + libraryVersion();              lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
355          String defaultComments = "Created with " + libraryName() + " " + libraryVersion();  
356                // assemble default values
357                defaultName = "NONAME";
358    
359                if (resourceType == RIFF_TYPE_DLS) {
360                    // get current date
361                    time_t now = time(NULL);
362                    tm* pNowBroken = localtime(&now);
363                    char buf[11];
364                    strftime(buf, 11, "%F", pNowBroken);
365                    defaultCreationDate = buf;
366    
367                    defaultComments = "Created with " + libraryName() + " " + libraryVersion();
368                }
369                if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS)
370                {
371                    defaultSoftware = libraryName() + " " + libraryVersion();
372                }
373            }
374    
375          // save values          // save values
376          SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName);  
377          SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));          SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));
378          SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);          SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));
379            SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));
380          SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);          SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);
         SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""));  
381          SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));          SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));
382          SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));          SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);
383            SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));
384          SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));          SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));
385          SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));          SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));
         SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));  
         SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));  
         SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware);  
386          SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""));          SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""));
387            SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName);
388            SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""));
389            SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""));
390            SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware);
391          SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""));          SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""));
392          SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));          SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));
393          SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));          SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));
394        }
395        
396        /**
397         * Make a deep copy of the Info object given by @a orig and assign it to
398         * this object.
399         *
400         * @param orig - original Info object to be copied from
401         */
402        void Info::CopyAssign(const Info* orig) {
403            Name = orig->Name;
404            ArchivalLocation = orig->ArchivalLocation;
405            CreationDate = orig->CreationDate;
406            Comments = orig->Comments;
407            Product = orig->Product;
408            Copyright = orig->Copyright;
409            Artists = orig->Artists;
410            Genre = orig->Genre;
411            Keywords = orig->Keywords;
412            Engineer = orig->Engineer;
413            Technician = orig->Technician;
414            Software = orig->Software;
415            Medium = orig->Medium;
416            Source = orig->Source;
417            SourceForm = orig->SourceForm;
418            Commissioned = orig->Commissioned;
419            Subject = orig->Subject;
420            //FIXME: hmm, is copying this pointer a good idea?
421            pFixedStringLengths = orig->pFixedStringLengths;
422      }      }
423    
424    
# Line 395  namespace DLS { Line 467  namespace DLS {
467       */       */
468      void Resource::UpdateChunks() {      void Resource::UpdateChunks() {
469          pInfo->UpdateChunks();          pInfo->UpdateChunks();
470          //TODO: save DLSID  
471            if (pDLSID) {
472                // make sure 'dlid' chunk exists
473                RIFF::Chunk* ckDLSID = pResourceList->GetSubChunk(CHUNK_ID_DLID);
474                if (!ckDLSID) ckDLSID = pResourceList->AddSubChunk(CHUNK_ID_DLID, 16);
475                uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
476                // update 'dlid' chunk
477                store32(&pData[0], pDLSID->ulData1);
478                store16(&pData[4], pDLSID->usData2);
479                store16(&pData[6], pDLSID->usData3);
480                memcpy(&pData[8], pDLSID->abData, 8);
481            }
482      }      }
483    
484        /**
485         * Generates a new DLSID for the resource.
486         */
487        void Resource::GenerateDLSID() {
488    #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
489    
490            if (!pDLSID) pDLSID = new dlsid_t;
491    
492    #ifdef WIN32
493    
494            UUID uuid;
495            UuidCreate(&uuid);
496            pDLSID->ulData1 = uuid.Data1;
497            pDLSID->usData2 = uuid.Data2;
498            pDLSID->usData3 = uuid.Data3;
499            memcpy(pDLSID->abData, uuid.Data4, 8);
500    
501    #elif defined(__APPLE__)
502    
503            CFUUIDRef uuidRef = CFUUIDCreate(NULL);
504            CFUUIDBytes uuid = CFUUIDGetUUIDBytes(uuidRef);
505            CFRelease(uuidRef);
506            pDLSID->ulData1 = uuid.byte0 | uuid.byte1 << 8 | uuid.byte2 << 16 | uuid.byte3 << 24;
507            pDLSID->usData2 = uuid.byte4 | uuid.byte5 << 8;
508            pDLSID->usData3 = uuid.byte6 | uuid.byte7 << 8;
509            pDLSID->abData[0] = uuid.byte8;
510            pDLSID->abData[1] = uuid.byte9;
511            pDLSID->abData[2] = uuid.byte10;
512            pDLSID->abData[3] = uuid.byte11;
513            pDLSID->abData[4] = uuid.byte12;
514            pDLSID->abData[5] = uuid.byte13;
515            pDLSID->abData[6] = uuid.byte14;
516            pDLSID->abData[7] = uuid.byte15;
517    #else
518            uuid_t uuid;
519            uuid_generate(uuid);
520            pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24;
521            pDLSID->usData2 = uuid[4] | uuid[5] << 8;
522            pDLSID->usData3 = uuid[6] | uuid[7] << 8;
523            memcpy(pDLSID->abData, &uuid[8], 8);
524    #endif
525    #endif
526        }
527        
528        /**
529         * Make a deep copy of the Resource object given by @a orig and assign it
530         * to this object.
531         *
532         * @param orig - original Resource object to be copied from
533         */
534        void Resource::CopyAssign(const Resource* orig) {
535            pInfo->CopyAssign(orig->pInfo);
536        }
537    
538    
539  // *************** Sampler ***************  // *************** Sampler ***************
# Line 414  namespace DLS { Line 550  namespace DLS {
550              SamplerOptions = wsmp->ReadUint32();              SamplerOptions = wsmp->ReadUint32();
551              SampleLoops    = wsmp->ReadUint32();              SampleLoops    = wsmp->ReadUint32();
552          } else { // 'wsmp' chunk missing          } else { // 'wsmp' chunk missing
553              uiHeaderSize   = 0;              uiHeaderSize   = 20;
554              UnityNote      = 64;              UnityNote      = 60;
555              FineTune       = 0; // +- 0 cents              FineTune       = 0; // +- 0 cents
556              Gain           = 0; // 0 dB              Gain           = 0; // 0 dB
557              SamplerOptions = F_WSMP_NO_COMPRESSION;              SamplerOptions = F_WSMP_NO_COMPRESSION;
# Line 439  namespace DLS { Line 575  namespace DLS {
575          if (pSampleLoops) delete[] pSampleLoops;          if (pSampleLoops) delete[] pSampleLoops;
576      }      }
577    
578        void Sampler::SetGain(int32_t gain) {
579            Gain = gain;
580        }
581    
582      /**      /**
583       * Apply all sample player options to the respective RIFF chunk. You       * Apply all sample player options to the respective RIFF chunk. You
584       * have to call File::Save() to make changes persistent.       * have to call File::Save() to make changes persistent.
# Line 446  namespace DLS { Line 586  namespace DLS {
586      void Sampler::UpdateChunks() {      void Sampler::UpdateChunks() {
587          // make sure 'wsmp' chunk exists          // make sure 'wsmp' chunk exists
588          RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);          RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
589            int wsmpSize = uiHeaderSize + SampleLoops * 16;
590          if (!wsmp) {          if (!wsmp) {
591              uiHeaderSize = 20;              wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize);
592              wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, uiHeaderSize + SampleLoops * 16);          } else if (wsmp->GetSize() != wsmpSize) {
593                wsmp->Resize(wsmpSize);
594          }          }
595          uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();          uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
596          // update headers size          // update headers size
597          memccpy(&pData[0], &uiHeaderSize, 1, 4);          store32(&pData[0], uiHeaderSize);
598          // update respective sampler options bits          // update respective sampler options bits
599          SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION          SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION
600                                                     : SamplerOptions & (~F_WSMP_NO_TRUNCATION);                                                     : SamplerOptions & (~F_WSMP_NO_TRUNCATION);
601          SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION          SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION
602                                                 : SamplerOptions & (~F_WSMP_NO_COMPRESSION);                                                 : SamplerOptions & (~F_WSMP_NO_COMPRESSION);
603            store16(&pData[4], UnityNote);
604            store16(&pData[6], FineTune);
605            store32(&pData[8], Gain);
606            store32(&pData[12], SamplerOptions);
607            store32(&pData[16], SampleLoops);
608          // update loop definitions          // update loop definitions
609          for (uint32_t i = 0; i < SampleLoops; i++) {          for (uint32_t i = 0; i < SampleLoops; i++) {
610              //FIXME: this does not handle extended loop structs correctly              //FIXME: this does not handle extended loop structs correctly
611              memccpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4, 4);              store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size);
612                store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType);
613                store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart);
614                store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength);
615          }          }
616      }      }
617    
618        /**
619         * Adds a new sample loop with the provided loop definition.
620         *
621         * @param pLoopDef - points to a loop definition that is to be copied
622         */
623        void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) {
624            sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1];
625            // copy old loops array
626            for (int i = 0; i < SampleLoops; i++) {
627                pNewLoops[i] = pSampleLoops[i];
628            }
629            // add the new loop
630            pNewLoops[SampleLoops] = *pLoopDef;
631            // auto correct size field
632            pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t);
633            // free the old array and update the member variables
634            if (SampleLoops) delete[] pSampleLoops;
635            pSampleLoops = pNewLoops;
636            SampleLoops++;
637        }
638    
639        /**
640         * Deletes an existing sample loop.
641         *
642         * @param pLoopDef - pointer to existing loop definition
643         * @throws Exception - if given loop definition does not exist
644         */
645        void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) {
646            sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1];
647            // copy old loops array (skipping given loop)
648            for (int i = 0, o = 0; i < SampleLoops; i++) {
649                if (&pSampleLoops[i] == pLoopDef) continue;
650                if (o == SampleLoops - 1) {
651                    delete[] pNewLoops;
652                    throw Exception("Could not delete Sample Loop, because it does not exist");
653                }
654                pNewLoops[o] = pSampleLoops[i];
655                o++;
656            }
657            // free the old array and update the member variables
658            if (SampleLoops) delete[] pSampleLoops;
659            pSampleLoops = pNewLoops;
660            SampleLoops--;
661        }
662        
663        /**
664         * Make a deep copy of the Sampler object given by @a orig and assign it
665         * to this object.
666         *
667         * @param orig - original Sampler object to be copied from
668         */
669        void Sampler::CopyAssign(const Sampler* orig) {
670            // copy trivial scalars
671            UnityNote = orig->UnityNote;
672            FineTune = orig->FineTune;
673            Gain = orig->Gain;
674            NoSampleDepthTruncation = orig->NoSampleDepthTruncation;
675            NoSampleCompression = orig->NoSampleCompression;
676            SamplerOptions = orig->SamplerOptions;
677            
678            // copy sample loops
679            if (SampleLoops) delete[] pSampleLoops;
680            pSampleLoops = new sample_loop_t[orig->SampleLoops];
681            memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t));
682            SampleLoops = orig->SampleLoops;
683        }
684    
685    
686  // *************** Sample ***************  // *************** Sample ***************
# Line 498  namespace DLS { Line 714  namespace DLS {
714              AverageBytesPerSecond  = pCkFormat->ReadUint32();              AverageBytesPerSecond  = pCkFormat->ReadUint32();
715              BlockAlign             = pCkFormat->ReadUint16();              BlockAlign             = pCkFormat->ReadUint16();
716              // PCM format specific              // PCM format specific
717              if (FormatTag == WAVE_FORMAT_PCM) {              if (FormatTag == DLS_WAVE_FORMAT_PCM) {
718                  BitDepth     = pCkFormat->ReadUint16();                  BitDepth     = pCkFormat->ReadUint16();
719                  FrameSize    = (FormatTag == WAVE_FORMAT_PCM) ? (BitDepth / 8) * Channels                  FrameSize    = (BitDepth / 8) * Channels;
                                                             : 0;  
720              } else { // unsupported sample data format              } else { // unsupported sample data format
721                  BitDepth     = 0;                  BitDepth     = 0;
722                  FrameSize    = 0;                  FrameSize    = 0;
723              }              }
724          } else { // 'fmt' chunk missing          } else { // 'fmt' chunk missing
725              FormatTag              = WAVE_FORMAT_PCM;              FormatTag              = DLS_WAVE_FORMAT_PCM;
726              BitDepth               = 16;              BitDepth               = 16;
727              Channels               = 1;              Channels               = 1;
728              SamplesPerSecond       = 44100;              SamplesPerSecond       = 44100;
# Line 515  namespace DLS { Line 730  namespace DLS {
730              FrameSize              = (BitDepth / 8) * Channels;              FrameSize              = (BitDepth / 8) * Channels;
731              BlockAlign             = FrameSize;              BlockAlign             = FrameSize;
732          }          }
733          SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize          SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize
734                                                                    : 0                                                                        : 0
735                                   : 0;                                   : 0;
736      }      }
737    
# Line 576  namespace DLS { Line 791  namespace DLS {
791       * the RIFF chunk which encapsulates the sample's wave data. The       * the RIFF chunk which encapsulates the sample's wave data. The
792       * returned value is dependant to the current FrameSize value.       * returned value is dependant to the current FrameSize value.
793       *       *
794       * @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
795       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
796       */       */
797      unsigned long Sample::GetSize() {      unsigned long Sample::GetSize() {
798          if (FormatTag != WAVE_FORMAT_PCM) return 0;          if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
799          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;          return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
800      }      }
801    
# Line 602  namespace DLS { Line 817  namespace DLS {
817       * calling File::Save() as this might exceed the current sample's       * calling File::Save() as this might exceed the current sample's
818       * boundary!       * boundary!
819       *       *
820       * Also note: only WAVE_FORMAT_PCM is currently supported, that is       * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
821       * FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
822       * other formats will fail!       * other formats will fail!
823       *       *
824       * @param iNewSize - new sample wave data size in sample points (must be       * @param iNewSize - new sample wave data size in sample points (must be
825       *                   greater than zero)       *                   greater than zero)
826       * @throws Excecption if FormatTag != WAVE_FORMAT_PCM       * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM
827       * @throws Exception if \a iNewSize is less than 1       * @throws Exception if \a iNewSize is less than 1
828       * @see File::Save(), FrameSize, FormatTag       * @see File::Save(), FrameSize, FormatTag
829       */       */
830      void Sample::Resize(int iNewSize) {      void Sample::Resize(int iNewSize) {
831          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");
832          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");
833          const int iSizeInBytes = iNewSize * FrameSize;          const int iSizeInBytes = iNewSize * FrameSize;
834          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);          pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
# Line 626  namespace DLS { Line 841  namespace DLS {
841       * 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
842       * the sample into RAM, thus for disk streaming.       * the sample into RAM, thus for disk streaming.
843       *       *
844       * Also note: only WAVE_FORMAT_PCM is currently supported, that is       * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
845       * FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample
846       * with other formats will fail!       * with other formats will fail!
847       *       *
848       * @param SampleCount  number of sample points       * @param SampleCount  number of sample points
849       * @param Whence       to which relation \a SampleCount refers to       * @param Whence       to which relation \a SampleCount refers to
850       * @returns new position within the sample, 0 if       * @returns new position within the sample, 0 if
851       *          FormatTag != WAVE_FORMAT_PCM       *          FormatTag != DLS_WAVE_FORMAT_PCM
852       * @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
853       * @see FrameSize, FormatTag       * @see FrameSize, FormatTag
854       */       */
855      unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {      unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {
856          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
857          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");
858          unsigned long orderedBytes = SampleCount * FrameSize;          unsigned long orderedBytes = SampleCount * FrameSize;
859          unsigned long result = pCkData->SetPos(orderedBytes, Whence);          unsigned long result = pCkData->SetPos(orderedBytes, Whence);
# Line 656  namespace DLS { Line 871  namespace DLS {
871       * @param SampleCount  number of sample points to read       * @param SampleCount  number of sample points to read
872       */       */
873      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {      unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {
874          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
875          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?
876      }      }
877    
# Line 676  namespace DLS { Line 891  namespace DLS {
891       * @see LoadSampleData()       * @see LoadSampleData()
892       */       */
893      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {      unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {
894          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
895          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");
896          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?
897      }      }
# Line 685  namespace DLS { Line 900  namespace DLS {
900       * Apply sample and its settings to the respective RIFF chunks. You have       * Apply sample and its settings to the respective RIFF chunks. You have
901       * to call File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
902       *       *
903       * @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data       * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data
904       *                   was provided yet       *                   was provided yet
905       */       */
906      void Sample::UpdateChunks() {      void Sample::UpdateChunks() {
907          if (FormatTag != WAVE_FORMAT_PCM)          if (FormatTag != DLS_WAVE_FORMAT_PCM)
908              throw Exception("Could not save sample, only PCM format is supported");              throw Exception("Could not save sample, only PCM format is supported");
909          // 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
910          if (!pCkData)          if (!pCkData)
# Line 701  namespace DLS { Line 916  namespace DLS {
916          if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format          if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
917          uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();          uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();
918          // update 'fmt' chunk          // update 'fmt' chunk
919          memccpy(&pData[0], &FormatTag, 1, 2);          store16(&pData[0], FormatTag);
920          memccpy(&pData[2], &Channels,  1, 2);          store16(&pData[2], Channels);
921          memccpy(&pData[4], &SamplesPerSecond, 1, 4);          store32(&pData[4], SamplesPerSecond);
922          memccpy(&pData[8], &AverageBytesPerSecond, 1, 4);          store32(&pData[8], AverageBytesPerSecond);
923          memccpy(&pData[12], &BlockAlign, 1, 2);          store16(&pData[12], BlockAlign);
924          memccpy(&pData[14], &BitDepth, 1, 2); // assuming PCM format          store16(&pData[14], BitDepth); // assuming PCM format
925      }      }
926    
927    
# Line 790  namespace DLS { Line 1005  namespace DLS {
1005      }      }
1006    
1007      /**      /**
1008         * Modifies the key range of this Region and makes sure the respective
1009         * chunks are in correct order.
1010         *
1011         * @param Low  - lower end of key range
1012         * @param High - upper end of key range
1013         */
1014        void Region::SetKeyRange(uint16_t Low, uint16_t High) {
1015            KeyRange.low  = Low;
1016            KeyRange.high = High;
1017    
1018            // make sure regions are already loaded
1019            Instrument* pInstrument = (Instrument*) GetParent();
1020            if (!pInstrument->pRegions) pInstrument->LoadRegions();
1021            if (!pInstrument->pRegions) return;
1022    
1023            // find the r which is the first one to the right of this region
1024            // at its new position
1025            Region* r = NULL;
1026            Region* prev_region = NULL;
1027            for (
1028                Instrument::RegionList::iterator iter = pInstrument->pRegions->begin();
1029                iter != pInstrument->pRegions->end(); iter++
1030            ) {
1031                if ((*iter)->KeyRange.low > this->KeyRange.low) {
1032                    r = *iter;
1033                    break;
1034                }
1035                prev_region = *iter;
1036            }
1037    
1038            // place this region before r if it's not already there
1039            if (prev_region != this) pInstrument->MoveRegion(this, r);
1040        }
1041    
1042        /**
1043       * Apply Region settings to the respective RIFF chunks. You have to       * Apply Region settings to the respective RIFF chunks. You have to
1044       * call File::Save() to make changes persistent.       * call File::Save() to make changes persistent.
1045       *       *
# Line 798  namespace DLS { Line 1048  namespace DLS {
1048      void Region::UpdateChunks() {      void Region::UpdateChunks() {
1049          // make sure 'rgnh' chunk exists          // make sure 'rgnh' chunk exists
1050          RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);          RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);
1051          if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, 14);          if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
1052          uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();          uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();
1053          FormatOptionFlags = (SelfNonExclusive)          FormatOptionFlags = (SelfNonExclusive)
1054                                  ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE                                  ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE
1055                                  : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);                                  : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);
1056          // update 'rgnh' chunk          // update 'rgnh' chunk
1057          memccpy(&pData[0], &KeyRange, 2, 2);          store16(&pData[0], KeyRange.low);
1058          memccpy(&pData[4], &VelocityRange, 2, 2);          store16(&pData[2], KeyRange.high);
1059          memccpy(&pData[8], &FormatOptionFlags, 1, 2);          store16(&pData[4], VelocityRange.low);
1060          memccpy(&pData[10], &KeyGroup, 1, 2);          store16(&pData[6], VelocityRange.high);
1061          memccpy(&pData[12], &Layer, 1, 2);          store16(&pData[8], FormatOptionFlags);
1062            store16(&pData[10], KeyGroup);
1063            if (rgnh->GetSize() >= 14) store16(&pData[12], Layer);
1064    
1065          // update chunks of base classes as well          // update chunks of base classes as well (but skip Resource,
1066          Resource::UpdateChunks();          // as a rgn doesn't seem to have dlid and INFO chunks)
1067          Articulator::UpdateChunks();          Articulator::UpdateChunks();
1068          Sampler::UpdateChunks();          Sampler::UpdateChunks();
1069    
# Line 838  namespace DLS { Line 1090  namespace DLS {
1090                  }                  }
1091              }              }
1092          }          }
         if (index < 0) throw Exception("Could not save Region, could not find Region's sample");  
1093          WavePoolTableIndex = index;          WavePoolTableIndex = index;
1094          // update 'wlnk' chunk          // update 'wlnk' chunk
1095          memccpy(&pData[0], &WaveLinkOptionFlags, 1, 2);          store16(&pData[0], WaveLinkOptionFlags);
1096          memccpy(&pData[2], &PhaseGroup, 1, 2);          store16(&pData[2], PhaseGroup);
1097          memccpy(&pData[4], &Channel, 1, 4);          store32(&pData[4], Channel);
1098          memccpy(&pData[8], &WavePoolTableIndex, 1, 4);          store32(&pData[8], WavePoolTableIndex);
1099        }
1100        
1101        /**
1102         * Make a (semi) deep copy of the Region object given by @a orig and assign
1103         * it to this object.
1104         *
1105         * Note that the sample pointer referenced by @a orig is simply copied as
1106         * memory address. Thus the respective sample is shared, not duplicated!
1107         *
1108         * @param orig - original Region object to be copied from
1109         */
1110        void Region::CopyAssign(const Region* orig) {
1111            // handle base classes
1112            Resource::CopyAssign(orig);
1113            Articulator::CopyAssign(orig);
1114            Sampler::CopyAssign(orig);
1115            // handle actual own attributes of this class
1116            // (the trivial ones)
1117            VelocityRange = orig->VelocityRange;
1118            KeyGroup = orig->KeyGroup;
1119            Layer = orig->Layer;
1120            SelfNonExclusive = orig->SelfNonExclusive;
1121            PhaseMaster = orig->PhaseMaster;
1122            PhaseGroup = orig->PhaseGroup;
1123            MultiChannel = orig->MultiChannel;
1124            Channel = orig->Channel;
1125            WavePoolTableIndex = orig->WavePoolTableIndex;
1126            pSample = orig->pSample;
1127            FormatOptionFlags = orig->FormatOptionFlags;
1128            WaveLinkOptionFlags = orig->WaveLinkOptionFlags;
1129            // handle the last, a bit sensible attribute
1130            SetKeyRange(orig->KeyRange.low, orig->KeyRange.high);
1131      }      }
   
1132    
1133    
1134  // *************** Instrument ***************  // *************** Instrument ***************
# Line 927  namespace DLS { Line 1209  namespace DLS {
1209          return pNewRegion;          return pNewRegion;
1210      }      }
1211    
1212        void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
1213            RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1214            lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0);
1215    
1216            pRegions->remove(pSrc);
1217            RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
1218            pRegions->insert(iter, pSrc);
1219        }
1220    
1221      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
1222          if (!pRegions) return;          if (!pRegions) return;
1223          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);          RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
# Line 957  namespace DLS { Line 1248  namespace DLS {
1248          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);          locale.bank       = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
1249          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);
1250          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
1251          memccpy(&pData[0], &Regions, 1, 4);          store32(&pData[0], Regions);
1252          memccpy(&pData[4], &locale, 2, 4);          store32(&pData[4], locale.bank);
1253            store32(&pData[8], locale.instrument);
1254          // update Region's chunks          // update Region's chunks
1255          if (!pRegions) return;          if (!pRegions) return;
1256          RegionList::iterator iter = pRegions->begin();          RegionList::iterator iter = pRegions->begin();
# Line 987  namespace DLS { Line 1279  namespace DLS {
1279          RIFF::List* pParent = pCkInstrument->GetParent();          RIFF::List* pParent = pCkInstrument->GetParent();
1280          pParent->DeleteSubChunk(pCkInstrument);          pParent->DeleteSubChunk(pCkInstrument);
1281      }      }
1282        
1283        void Instrument::CopyAssignCore(const Instrument* orig) {
1284            // handle base classes
1285            Resource::CopyAssign(orig);
1286            Articulator::CopyAssign(orig);
1287            // handle actual own attributes of this class
1288            // (the trivial ones)
1289            IsDrum = orig->IsDrum;
1290            MIDIBank = orig->MIDIBank;
1291            MIDIBankCoarse = orig->MIDIBankCoarse;
1292            MIDIBankFine = orig->MIDIBankFine;
1293            MIDIProgram = orig->MIDIProgram;
1294        }
1295        
1296        /**
1297         * Make a (semi) deep copy of the Instrument object given by @a orig and assign
1298         * it to this object.
1299         *
1300         * Note that all sample pointers referenced by @a orig are simply copied as
1301         * memory address. Thus the respective samples are shared, not duplicated!
1302         *
1303         * @param orig - original Instrument object to be copied from
1304         */
1305        void Instrument::CopyAssign(const Instrument* orig) {
1306            CopyAssignCore(orig);
1307            // delete all regions first
1308            while (Regions) DeleteRegion(GetFirstRegion());
1309            // now recreate and copy regions
1310            {
1311                RegionList::const_iterator it = orig->pRegions->begin();
1312                for (int i = 0; i < orig->Regions; ++i, ++it) {
1313                    Region* dstRgn = AddRegion();
1314                    //NOTE: Region does semi-deep copy !
1315                    dstRgn->CopyAssign(*it);
1316                }
1317            }
1318        }
1319    
1320    
1321  // *************** File ***************  // *************** File ***************
# Line 1000  namespace DLS { Line 1328  namespace DLS {
1328       * a DLS file.       * a DLS file.
1329       */       */
1330      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {      File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1331            pRIFF->SetByteOrder(RIFF::endian_little);
1332          pVersion = new version_t;          pVersion = new version_t;
1333          pVersion->major   = 0;          pVersion->major   = 0;
1334          pVersion->minor   = 0;          pVersion->minor   = 0;
# Line 1239  namespace DLS { Line 1568  namespace DLS {
1568      }      }
1569    
1570      /**      /**
1571         * Returns extension file of given index. Extension files are used
1572         * sometimes to circumvent the 2 GB file size limit of the RIFF format and
1573         * of certain operating systems in general. In this case, instead of just
1574         * using one file, the content is spread among several files with similar
1575         * file name scheme. This is especially used by some GigaStudio sound
1576         * libraries.
1577         *
1578         * @param index - index of extension file
1579         * @returns sought extension file, NULL if index out of bounds
1580         * @see GetFileName()
1581         */
1582        RIFF::File* File::GetExtensionFile(int index) {
1583            if (index < 0 || index >= ExtensionFiles.size()) return NULL;
1584            std::list<RIFF::File*>::iterator iter = ExtensionFiles.begin();
1585            for (int i = 0; iter != ExtensionFiles.end(); ++iter, ++i)
1586                if (i == index) return *iter;
1587            return NULL;
1588        }
1589    
1590        /** @brief File name of this DLS file.
1591         *
1592         * This method returns the file name as it was provided when loading
1593         * the respective DLS file. However in case the File object associates
1594         * an empty, that is new DLS file, which was not yet saved to disk,
1595         * this method will return an empty string.
1596         *
1597         * @see GetExtensionFile()
1598         */
1599        String File::GetFileName() {
1600            return pRIFF->GetFileName();
1601        }
1602    
1603        /**
1604       * Apply all the DLS file's current instruments, samples and settings to       * Apply all the DLS file's current instruments, samples and settings to
1605       * the respective RIFF chunks. You have to call Save() to make changes       * the respective RIFF chunks. You have to call Save() to make changes
1606       * persistent.       * persistent.
# Line 1254  namespace DLS { Line 1616  namespace DLS {
1616              RIFF::Chunk* ckVersion    = pRIFF->GetSubChunk(CHUNK_ID_VERS);              RIFF::Chunk* ckVersion    = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1617              if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);              if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);
1618              uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();              uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();
1619              memccpy(pData, pVersion, 2, 4);              store16(&pData[0], pVersion->minor);
1620                store16(&pData[2], pVersion->major);
1621                store16(&pData[4], pVersion->build);
1622                store16(&pData[6], pVersion->release);
1623          }          }
1624    
1625          // update 'colh' chunk          // update 'colh' chunk
# Line 1262  namespace DLS { Line 1627  namespace DLS {
1627          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);          RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1628          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);          if (!colh)   colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1629          uint8_t* pData = (uint8_t*) colh->LoadChunkData();          uint8_t* pData = (uint8_t*) colh->LoadChunkData();
1630          memccpy(pData, &Instruments, 1, 4);          store32(pData, Instruments);
1631    
1632          // update instrument's chunks          // update instrument's chunks
1633          if (pInstruments) {          if (pInstruments) {
# Line 1282  namespace DLS { Line 1647  namespace DLS {
1647          ptbl->Resize(iPtblSize);          ptbl->Resize(iPtblSize);
1648          pData = (uint8_t*) ptbl->LoadChunkData();          pData = (uint8_t*) ptbl->LoadChunkData();
1649          WavePoolCount = iSamples;          WavePoolCount = iSamples;
1650          memccpy(&pData[4], &WavePoolCount, 1, 4);          store32(&pData[4], WavePoolCount);
1651          // 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()
1652          memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);          memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);
1653    
# Line 1371  namespace DLS { Line 1736  namespace DLS {
1736          unsigned long ulOriginalPos = ptbl->GetPos();          unsigned long ulOriginalPos = ptbl->GetPos();
1737          // update headers          // update headers
1738          ptbl->SetPos(0);          ptbl->SetPos(0);
1739          ptbl->WriteUint32(&WavePoolHeaderSize);          uint32_t tmp = WavePoolHeaderSize;
1740          ptbl->WriteUint32(&WavePoolCount);          ptbl->WriteUint32(&tmp);
1741            tmp = WavePoolCount;
1742            ptbl->WriteUint32(&tmp);
1743          // update offsets          // update offsets
1744          ptbl->SetPos(WavePoolHeaderSize);          ptbl->SetPos(WavePoolHeaderSize);
1745          if (b64BitWavePoolOffsets) {          if (b64BitWavePoolOffsets) {
1746              for (int i = 0 ; i < WavePoolCount ; i++) {              for (int i = 0 ; i < WavePoolCount ; i++) {
1747                  ptbl->WriteUint32(&pWavePoolTableHi[i]);                  tmp = pWavePoolTableHi[i];
1748                  ptbl->WriteUint32(&pWavePoolTable[i]);                  ptbl->WriteUint32(&tmp);
1749                    tmp = pWavePoolTable[i];
1750                    ptbl->WriteUint32(&tmp);
1751              }              }
1752          } else { // conventional 32 bit offsets          } else { // conventional 32 bit offsets
1753              for (int i = 0 ; i < WavePoolCount ; i++)              for (int i = 0 ; i < WavePoolCount ; i++) {
1754                  ptbl->WriteUint32(&pWavePoolTable[i]);                  tmp = pWavePoolTable[i];
1755                    ptbl->WriteUint32(&tmp);
1756                }
1757          }          }
1758          // restore 'ptbl' chunk's original read/write position          // restore 'ptbl' chunk's original read/write position
1759          ptbl->SetPos(ulOriginalPos);          ptbl->SetPos(ulOriginalPos);

Legend:
Removed from v.902  
changed lines
  Added in v.2394

  ViewVC Help
Powered by ViewVC