libgig  4.3.0
DLS.cpp
1 /***************************************************************************
2  * *
3  * libgig - C++ cross-platform Gigasampler format file access library *
4  * *
5  * Copyright (C) 2003-2020 by Christian Schoenebeck *
6  * <cuse@users.sourceforge.net> *
7  * *
8  * This library is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  ***************************************************************************/
23 
24 #include "DLS.h"
25 
26 #include <algorithm>
27 #include <vector>
28 #include <time.h>
29 
30 #ifdef __APPLE__
31 #include <CoreFoundation/CFUUID.h>
32 #elif defined(HAVE_UUID_UUID_H)
33 #include <uuid/uuid.h>
34 #endif
35 
36 #include "helper.h"
37 
38 // macros to decode connection transforms
39 #define CONN_TRANSFORM_SRC(x) ((x >> 10) & 0x000F)
40 #define CONN_TRANSFORM_CTL(x) ((x >> 4) & 0x000F)
41 #define CONN_TRANSFORM_DST(x) (x & 0x000F)
42 #define CONN_TRANSFORM_BIPOLAR_SRC(x) (x & 0x4000)
43 #define CONN_TRANSFORM_BIPOLAR_CTL(x) (x & 0x0100)
44 #define CONN_TRANSFORM_INVERT_SRC(x) (x & 0x8000)
45 #define CONN_TRANSFORM_INVERT_CTL(x) (x & 0x0200)
46 
47 // macros to encode connection transforms
48 #define CONN_TRANSFORM_SRC_ENCODE(x) ((x & 0x000F) << 10)
49 #define CONN_TRANSFORM_CTL_ENCODE(x) ((x & 0x000F) << 4)
50 #define CONN_TRANSFORM_DST_ENCODE(x) (x & 0x000F)
51 #define CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(x) ((x) ? 0x4000 : 0)
52 #define CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(x) ((x) ? 0x0100 : 0)
53 #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0)
54 #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0)
55 
56 #define DRUM_TYPE_MASK 0x80000000
57 
58 #define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001
59 
60 #define F_WAVELINK_PHASE_MASTER 0x0001
61 #define F_WAVELINK_MULTICHANNEL 0x0002
62 
63 #define F_WSMP_NO_TRUNCATION 0x0001
64 #define F_WSMP_NO_COMPRESSION 0x0002
65 
66 #define MIDI_BANK_COARSE(x) ((x & 0x00007F00) >> 8) // CC0
67 #define MIDI_BANK_FINE(x) (x & 0x0000007F) // CC32
68 #define MIDI_BANK_MERGE(coarse, fine) ((((uint16_t) coarse) << 7) | fine) // CC0 + CC32
69 #define MIDI_BANK_ENCODE(coarse, fine) (((coarse & 0x0000007F) << 8) | (fine & 0x0000007F))
70 
71 namespace DLS {
72 
73 // *************** Connection ***************
74 // *
75 
76  void Connection::Init(conn_block_t* Header) {
77  Source = (conn_src_t) Header->source;
78  Control = (conn_src_t) Header->control;
79  Destination = (conn_dst_t) Header->destination;
80  Scale = Header->scale;
81  SourceTransform = (conn_trn_t) CONN_TRANSFORM_SRC(Header->transform);
82  ControlTransform = (conn_trn_t) CONN_TRANSFORM_CTL(Header->transform);
83  DestinationTransform = (conn_trn_t) CONN_TRANSFORM_DST(Header->transform);
84  SourceInvert = CONN_TRANSFORM_INVERT_SRC(Header->transform);
85  SourceBipolar = CONN_TRANSFORM_BIPOLAR_SRC(Header->transform);
86  ControlInvert = CONN_TRANSFORM_INVERT_CTL(Header->transform);
87  ControlBipolar = CONN_TRANSFORM_BIPOLAR_CTL(Header->transform);
88  }
89 
90  Connection::conn_block_t Connection::ToConnBlock() {
91  conn_block_t c;
92  c.source = Source;
93  c.control = Control;
94  c.destination = Destination;
95  c.scale = Scale;
96  c.transform = CONN_TRANSFORM_SRC_ENCODE(SourceTransform) |
97  CONN_TRANSFORM_CTL_ENCODE(ControlTransform) |
98  CONN_TRANSFORM_DST_ENCODE(DestinationTransform) |
99  CONN_TRANSFORM_INVERT_SRC_ENCODE(SourceInvert) |
100  CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(SourceBipolar) |
101  CONN_TRANSFORM_INVERT_CTL_ENCODE(ControlInvert) |
102  CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(ControlBipolar);
103  return c;
104  }
105 
106 
107 
108 // *************** Articulation ***************
109 // *
110 
120  pArticulationCk = artl;
121  if (artl->GetChunkID() != CHUNK_ID_ART2 &&
122  artl->GetChunkID() != CHUNK_ID_ARTL) {
123  throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected");
124  }
125 
126  artl->SetPos(0);
127 
128  HeaderSize = artl->ReadUint32();
129  Connections = artl->ReadUint32();
130  artl->SetPos(HeaderSize);
131 
133  Connection::conn_block_t connblock;
134  for (uint32_t i = 0; i < Connections; i++) {
135  artl->Read(&connblock.source, 1, 2);
136  artl->Read(&connblock.control, 1, 2);
137  artl->Read(&connblock.destination, 1, 2);
138  artl->Read(&connblock.transform, 1, 2);
139  artl->Read(&connblock.scale, 1, 4);
140  pConnections[i].Init(&connblock);
141  }
142  }
143 
144  Articulation::~Articulation() {
145  if (pConnections) delete[] pConnections;
146  }
147 
155  const int iEntrySize = 12; // 12 bytes per connection block
156  pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
157  uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
158  store16(&pData[0], HeaderSize);
159  store16(&pData[2], Connections);
160  for (uint32_t i = 0; i < Connections; i++) {
161  Connection::conn_block_t c = pConnections[i].ToConnBlock();
162  store16(&pData[HeaderSize + i * iEntrySize], c.source);
163  store16(&pData[HeaderSize + i * iEntrySize + 2], c.control);
164  store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination);
165  store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform);
166  store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale);
167  }
168  }
169 
180  }
181 
182 
183 
184 // *************** Articulator ***************
185 // *
186 
187  Articulator::Articulator(RIFF::List* ParentList) {
188  pParentList = ParentList;
189  pArticulations = NULL;
190  }
191 
192  Articulation* Articulator::GetFirstArticulation() {
193  if (!pArticulations) LoadArticulations();
194  if (!pArticulations) return NULL;
195  ArticulationsIterator = pArticulations->begin();
196  return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
197  }
198 
199  Articulation* Articulator::GetNextArticulation() {
200  if (!pArticulations) return NULL;
201  ArticulationsIterator++;
202  return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
203  }
204 
205  void Articulator::LoadArticulations() {
206  // prefer articulation level 2
207  RIFF::List* lart = pParentList->GetSubList(LIST_TYPE_LAR2);
208  if (!lart) lart = pParentList->GetSubList(LIST_TYPE_LART);
209  if (lart) {
210  uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2
211  : CHUNK_ID_ARTL;
212  RIFF::Chunk* art = lart->GetFirstSubChunk();
213  while (art) {
214  if (art->GetChunkID() == artCkType) {
215  if (!pArticulations) pArticulations = new ArticulationList;
216  pArticulations->push_back(new Articulation(art));
217  }
218  art = lart->GetNextSubChunk();
219  }
220  }
221  }
222 
223  Articulator::~Articulator() {
224  if (pArticulations) {
225  ArticulationList::iterator iter = pArticulations->begin();
226  ArticulationList::iterator end = pArticulations->end();
227  while (iter != end) {
228  delete *iter;
229  iter++;
230  }
231  delete pArticulations;
232  }
233  }
234 
242  if (pArticulations) {
243  ArticulationList::iterator iter = pArticulations->begin();
244  ArticulationList::iterator end = pArticulations->end();
245  for (; iter != end; ++iter) {
246  (*iter)->UpdateChunks(pProgress);
247  }
248  }
249  }
250 
256  if (pArticulations) {
257  ArticulationList::iterator iter = pArticulations->begin();
258  ArticulationList::iterator end = pArticulations->end();
259  for (; iter != end; ++iter) {
260  (*iter)->DeleteChunks();
261  }
262  }
263  }
264 
271  //TODO: implement deep copy assignment for this class
272  }
273 
274 
275 
276 // *************** Info ***************
277 // *
278 
286  pFixedStringLengths = NULL;
287  pResourceListChunk = list;
288  if (list) {
289  RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
290  if (lstINFO) {
291  LoadString(CHUNK_ID_INAM, lstINFO, Name);
292  LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation);
293  LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate);
294  LoadString(CHUNK_ID_ICMT, lstINFO, Comments);
295  LoadString(CHUNK_ID_IPRD, lstINFO, Product);
296  LoadString(CHUNK_ID_ICOP, lstINFO, Copyright);
297  LoadString(CHUNK_ID_IART, lstINFO, Artists);
298  LoadString(CHUNK_ID_IGNR, lstINFO, Genre);
299  LoadString(CHUNK_ID_IKEY, lstINFO, Keywords);
300  LoadString(CHUNK_ID_IENG, lstINFO, Engineer);
301  LoadString(CHUNK_ID_ITCH, lstINFO, Technician);
302  LoadString(CHUNK_ID_ISFT, lstINFO, Software);
303  LoadString(CHUNK_ID_IMED, lstINFO, Medium);
304  LoadString(CHUNK_ID_ISRC, lstINFO, Source);
305  LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
306  LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
307  LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
308  }
309  }
310  }
311 
312  Info::~Info() {
313  }
314 
326  void Info::SetFixedStringLengths(const string_length_t* lengths) {
327  pFixedStringLengths = lengths;
328  }
329 
335  void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
336  RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
337  ::LoadString(ck, s); // function from helper.h
338  }
339 
355  void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) {
356  int size = 0;
357  if (pFixedStringLengths) {
358  for (int i = 0 ; pFixedStringLengths[i].length ; i++) {
359  if (pFixedStringLengths[i].chunkId == ChunkID) {
360  size = pFixedStringLengths[i].length;
361  break;
362  }
363  }
364  }
365  RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
366  ::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h
367  }
368 
376  void Info::UpdateChunks(progress_t* pProgress) {
377  if (!pResourceListChunk) return;
378 
379  // make sure INFO list chunk exists
380  RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
381 
382  String defaultName = "";
383  String defaultCreationDate = "";
384  String defaultSoftware = "";
385  String defaultComments = "";
386 
387  uint32_t resourceType = pResourceListChunk->GetListType();
388 
389  if (!lstINFO) {
390  lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
391 
392  // assemble default values
393  defaultName = "NONAME";
394 
395  if (resourceType == RIFF_TYPE_DLS) {
396  // get current date
397  time_t now = time(NULL);
398  tm* pNowBroken = localtime(&now);
399  char buf[11];
400  strftime(buf, 11, "%F", pNowBroken);
401  defaultCreationDate = buf;
402 
403  defaultComments = "Created with " + libraryName() + " " + libraryVersion();
404  }
405  if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS)
406  {
407  defaultSoftware = libraryName() + " " + libraryVersion();
408  }
409  }
410 
411  // save values
412 
413  SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));
414  SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));
415  SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));
416  SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);
417  SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));
418  SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);
419  SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));
420  SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));
421  SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));
422  SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""));
423  SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName);
424  SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""));
425  SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""));
426  SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware);
427  SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""));
428  SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));
429  SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));
430  }
431 
442  }
443 
450  void Info::CopyAssign(const Info* orig) {
451  Name = orig->Name;
453  CreationDate = orig->CreationDate;
454  Comments = orig->Comments;
455  Product = orig->Product;
456  Copyright = orig->Copyright;
457  Artists = orig->Artists;
458  Genre = orig->Genre;
459  Keywords = orig->Keywords;
460  Engineer = orig->Engineer;
461  Technician = orig->Technician;
462  Software = orig->Software;
463  Medium = orig->Medium;
464  Source = orig->Source;
465  SourceForm = orig->SourceForm;
466  Commissioned = orig->Commissioned;
467  Subject = orig->Subject;
468  //FIXME: hmm, is copying this pointer a good idea?
469  pFixedStringLengths = orig->pFixedStringLengths;
470  }
471 
472 
473 
474 // *************** Resource ***************
475 // *
476 
486  Resource::Resource(Resource* Parent, RIFF::List* lstResource) {
487  pParent = Parent;
488  pResourceList = lstResource;
489 
490  pInfo = new Info(lstResource);
491 
492  RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);
493  if (ckDLSID) {
494  ckDLSID->SetPos(0);
495 
496  pDLSID = new dlsid_t;
497  ckDLSID->Read(&pDLSID->ulData1, 1, 4);
498  ckDLSID->Read(&pDLSID->usData2, 1, 2);
499  ckDLSID->Read(&pDLSID->usData3, 1, 2);
500  ckDLSID->Read(pDLSID->abData, 8, 1);
501  }
502  else pDLSID = NULL;
503  }
504 
505  Resource::~Resource() {
506  if (pDLSID) delete pDLSID;
507  if (pInfo) delete pInfo;
508  }
509 
520  }
521 
533  pInfo->UpdateChunks(pProgress);
534 
535  if (pDLSID) {
536  // make sure 'dlid' chunk exists
537  RIFF::Chunk* ckDLSID = pResourceList->GetSubChunk(CHUNK_ID_DLID);
538  if (!ckDLSID) ckDLSID = pResourceList->AddSubChunk(CHUNK_ID_DLID, 16);
539  uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
540  // update 'dlid' chunk
541  store32(&pData[0], pDLSID->ulData1);
542  store16(&pData[4], pDLSID->usData2);
543  store16(&pData[6], pDLSID->usData3);
544  memcpy(&pData[8], pDLSID->abData, 8);
545  }
546  }
547 
552  #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
553  if (!pDLSID) pDLSID = new dlsid_t;
555  #endif
556  }
557 
558  void Resource::GenerateDLSID(dlsid_t* pDLSID) {
559 #ifdef WIN32
560  UUID uuid;
561  UuidCreate(&uuid);
562  pDLSID->ulData1 = uuid.Data1;
563  pDLSID->usData2 = uuid.Data2;
564  pDLSID->usData3 = uuid.Data3;
565  memcpy(pDLSID->abData, uuid.Data4, 8);
566 
567 #elif defined(__APPLE__)
568 
569  CFUUIDRef uuidRef = CFUUIDCreate(NULL);
570  CFUUIDBytes uuid = CFUUIDGetUUIDBytes(uuidRef);
571  CFRelease(uuidRef);
572  pDLSID->ulData1 = uuid.byte0 | uuid.byte1 << 8 | uuid.byte2 << 16 | uuid.byte3 << 24;
573  pDLSID->usData2 = uuid.byte4 | uuid.byte5 << 8;
574  pDLSID->usData3 = uuid.byte6 | uuid.byte7 << 8;
575  pDLSID->abData[0] = uuid.byte8;
576  pDLSID->abData[1] = uuid.byte9;
577  pDLSID->abData[2] = uuid.byte10;
578  pDLSID->abData[3] = uuid.byte11;
579  pDLSID->abData[4] = uuid.byte12;
580  pDLSID->abData[5] = uuid.byte13;
581  pDLSID->abData[6] = uuid.byte14;
582  pDLSID->abData[7] = uuid.byte15;
583 #elif defined(HAVE_UUID_GENERATE)
584  uuid_t uuid;
585  uuid_generate(uuid);
586  pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24;
587  pDLSID->usData2 = uuid[4] | uuid[5] << 8;
588  pDLSID->usData3 = uuid[6] | uuid[7] << 8;
589  memcpy(pDLSID->abData, &uuid[8], 8);
590 #else
591 # error "Missing support for uuid generation"
592 #endif
593  }
594 
601  void Resource::CopyAssign(const Resource* orig) {
602  pInfo->CopyAssign(orig->pInfo);
603  }
604 
605 
606 // *************** Sampler ***************
607 // *
608 
609  Sampler::Sampler(RIFF::List* ParentList) {
610  pParentList = ParentList;
611  RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);
612  if (wsmp) {
613  wsmp->SetPos(0);
614 
615  uiHeaderSize = wsmp->ReadUint32();
616  UnityNote = wsmp->ReadUint16();
617  FineTune = wsmp->ReadInt16();
618  Gain = wsmp->ReadInt32();
619  SamplerOptions = wsmp->ReadUint32();
620  SampleLoops = wsmp->ReadUint32();
621  } else { // 'wsmp' chunk missing
622  uiHeaderSize = 20;
623  UnityNote = 60;
624  FineTune = 0; // +- 0 cents
625  Gain = 0; // 0 dB
626  SamplerOptions = F_WSMP_NO_COMPRESSION;
627  SampleLoops = 0;
628  }
629  NoSampleDepthTruncation = SamplerOptions & F_WSMP_NO_TRUNCATION;
630  NoSampleCompression = SamplerOptions & F_WSMP_NO_COMPRESSION;
631  pSampleLoops = (SampleLoops) ? new sample_loop_t[SampleLoops] : NULL;
632  if (SampleLoops) {
633  wsmp->SetPos(uiHeaderSize);
634  for (uint32_t i = 0; i < SampleLoops; i++) {
635  wsmp->Read(pSampleLoops + i, 4, 4);
636  if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended
637  wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos);
638  }
639  }
640  }
641  }
642 
643  Sampler::~Sampler() {
644  if (pSampleLoops) delete[] pSampleLoops;
645  }
646 
647  void Sampler::SetGain(int32_t gain) {
648  Gain = gain;
649  }
650 
658  // make sure 'wsmp' chunk exists
659  RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
660  int wsmpSize = uiHeaderSize + SampleLoops * 16;
661  if (!wsmp) {
662  wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize);
663  } else if (wsmp->GetSize() != wsmpSize) {
664  wsmp->Resize(wsmpSize);
665  }
666  uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
667  // update headers size
668  store32(&pData[0], uiHeaderSize);
669  // update respective sampler options bits
670  SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION
671  : SamplerOptions & (~F_WSMP_NO_TRUNCATION);
672  SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION
673  : SamplerOptions & (~F_WSMP_NO_COMPRESSION);
674  store16(&pData[4], UnityNote);
675  store16(&pData[6], FineTune);
676  store32(&pData[8], Gain);
677  store32(&pData[12], SamplerOptions);
678  store32(&pData[16], SampleLoops);
679  // update loop definitions
680  for (uint32_t i = 0; i < SampleLoops; i++) {
681  //FIXME: this does not handle extended loop structs correctly
682  store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size);
683  store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType);
684  store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart);
685  store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength);
686  }
687  }
688 
699  }
700 
707  sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1];
708  // copy old loops array
709  for (int i = 0; i < SampleLoops; i++) {
710  pNewLoops[i] = pSampleLoops[i];
711  }
712  // add the new loop
713  pNewLoops[SampleLoops] = *pLoopDef;
714  // auto correct size field
715  pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t);
716  // free the old array and update the member variables
717  if (SampleLoops) delete[] pSampleLoops;
718  pSampleLoops = pNewLoops;
719  SampleLoops++;
720  }
721 
729  sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1];
730  // copy old loops array (skipping given loop)
731  for (int i = 0, o = 0; i < SampleLoops; i++) {
732  if (&pSampleLoops[i] == pLoopDef) continue;
733  if (o == SampleLoops - 1) {
734  delete[] pNewLoops;
735  throw Exception("Could not delete Sample Loop, because it does not exist");
736  }
737  pNewLoops[o] = pSampleLoops[i];
738  o++;
739  }
740  // free the old array and update the member variables
741  if (SampleLoops) delete[] pSampleLoops;
742  pSampleLoops = pNewLoops;
743  SampleLoops--;
744  }
745 
752  void Sampler::CopyAssign(const Sampler* orig) {
753  // copy trivial scalars
754  UnityNote = orig->UnityNote;
755  FineTune = orig->FineTune;
756  Gain = orig->Gain;
757  NoSampleDepthTruncation = orig->NoSampleDepthTruncation;
758  NoSampleCompression = orig->NoSampleCompression;
759  SamplerOptions = orig->SamplerOptions;
760 
761  // copy sample loops
762  if (SampleLoops) delete[] pSampleLoops;
764  memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t));
765  SampleLoops = orig->SampleLoops;
766  }
767 
768 
769 // *************** Sample ***************
770 // *
771 
787  Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset) : Resource(pFile, waveList) {
788  pWaveList = waveList;
789  ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize());
790  pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
791  pCkData = waveList->GetSubChunk(CHUNK_ID_DATA);
792  if (pCkFormat) {
793  pCkFormat->SetPos(0);
794 
795  // common fields
796  FormatTag = pCkFormat->ReadUint16();
797  Channels = pCkFormat->ReadUint16();
798  SamplesPerSecond = pCkFormat->ReadUint32();
799  AverageBytesPerSecond = pCkFormat->ReadUint32();
800  BlockAlign = pCkFormat->ReadUint16();
801  // PCM format specific
802  if (FormatTag == DLS_WAVE_FORMAT_PCM) {
803  BitDepth = pCkFormat->ReadUint16();
804  FrameSize = (BitDepth / 8) * Channels;
805  } else { // unsupported sample data format
806  BitDepth = 0;
807  FrameSize = 0;
808  }
809  } else { // 'fmt' chunk missing
810  FormatTag = DLS_WAVE_FORMAT_PCM;
811  BitDepth = 16;
812  Channels = 1;
813  SamplesPerSecond = 44100;
815  FrameSize = (BitDepth / 8) * Channels;
817  }
818  SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize
819  : 0
820  : 0;
821  }
822 
828  if (pCkData)
829  pCkData->ReleaseChunkData();
830  if (pCkFormat)
831  pCkFormat->ReleaseChunkData();
832  }
833 
839  // handle base class
841 
842  // handle own RIFF chunks
843  if (pWaveList) {
844  RIFF::List* pParent = pWaveList->GetParent();
845  pParent->DeleteSubChunk(pWaveList);
846  pWaveList = NULL;
847  }
848  }
849 
861  void Sample::CopyAssignCore(const Sample* orig) {
862  // handle base classes
863  Resource::CopyAssign(orig);
864  // handle actual own attributes of this class
865  FormatTag = orig->FormatTag;
866  Channels = orig->Channels;
869  BlockAlign = orig->BlockAlign;
870  BitDepth = orig->BitDepth;
871  SamplesTotal = orig->SamplesTotal;
872  FrameSize = orig->FrameSize;
873  }
874 
881  void Sample::CopyAssign(const Sample* orig) {
882  CopyAssignCore(orig);
883 
884  // copy sample waveform data (reading directly from disc)
885  Resize(orig->GetSize());
886  char* buf = (char*) LoadSampleData();
887  Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now
888  const file_offset_t restorePos = pOrig->pCkData->GetPos();
889  pOrig->SetPos(0);
890  for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) {
891  const int iReadAtOnce = 64*1024;
892  file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo;
893  n = pOrig->Read(&buf[i], n);
894  if (!n) break;
895  todo -= n;
896  i += (n * pOrig->FrameSize);
897  }
898  pOrig->pCkData->SetPos(restorePos);
899  }
900 
928  return (pCkData) ? pCkData->LoadChunkData() : NULL;
929  }
930 
937  if (pCkData) pCkData->ReleaseChunkData();
938  }
939 
950  file_offset_t Sample::GetSize() const {
951  if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
952  return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
953  }
954 
983  void Sample::Resize(file_offset_t NewSize) {
984  if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");
985  if (NewSize < 1) throw Exception("Sample size must be at least one sample point");
986  if ((NewSize >> 48) != 0)
987  throw Exception("Unrealistic high DLS sample size detected");
988  const file_offset_t sizeInBytes = NewSize * FrameSize;
989  pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
990  if (pCkData) pCkData->Resize(sizeInBytes);
991  else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes);
992  }
993 
1010  file_offset_t Sample::SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence) {
1011  if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
1012  if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one");
1013  file_offset_t orderedBytes = SampleCount * FrameSize;
1014  file_offset_t result = pCkData->SetPos(orderedBytes, Whence);
1015  return (result == orderedBytes) ? SampleCount
1016  : result / FrameSize;
1017  }
1018 
1028  file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) {
1029  if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
1030  return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
1031  }
1032 
1048  file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) {
1049  if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
1050  if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");
1051  return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
1052  }
1053 
1063  if (FormatTag != DLS_WAVE_FORMAT_PCM)
1064  throw Exception("Could not save sample, only PCM format is supported");
1065  // we refuse to do anything if not sample wave form was provided yet
1066  if (!pCkData)
1067  throw Exception("Could not save sample, there is no sample data to save");
1068  // update chunks of base class as well
1069  Resource::UpdateChunks(pProgress);
1070  // make sure 'fmt' chunk exists
1071  RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT);
1072  if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
1073  uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();
1074  // update 'fmt' chunk
1075  store16(&pData[0], FormatTag);
1076  store16(&pData[2], Channels);
1077  store32(&pData[4], SamplesPerSecond);
1078  store32(&pData[8], AverageBytesPerSecond);
1079  store16(&pData[12], BlockAlign);
1080  store16(&pData[14], BitDepth); // assuming PCM format
1081  }
1082 
1083 
1084 
1085 // *************** Region ***************
1086 // *
1087 
1088  Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) {
1089  pCkRegion = rgnList;
1090 
1091  // articulation information
1092  RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);
1093  if (rgnh) {
1094  rgnh->SetPos(0);
1095 
1096  rgnh->Read(&KeyRange, 2, 2);
1097  rgnh->Read(&VelocityRange, 2, 2);
1098  FormatOptionFlags = rgnh->ReadUint16();
1099  KeyGroup = rgnh->ReadUint16();
1100  // Layer is optional
1101  if (rgnh->RemainingBytes() >= sizeof(uint16_t)) {
1102  rgnh->Read(&Layer, 1, sizeof(uint16_t));
1103  } else Layer = 0;
1104  } else { // 'rgnh' chunk is missing
1105  KeyRange.low = 0;
1106  KeyRange.high = 127;
1107  VelocityRange.low = 0;
1108  VelocityRange.high = 127;
1109  FormatOptionFlags = F_RGN_OPTION_SELFNONEXCLUSIVE;
1110  KeyGroup = 0;
1111  Layer = 0;
1112  }
1113  SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;
1114 
1115  // sample information
1116  RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);
1117  if (wlnk) {
1118  wlnk->SetPos(0);
1119 
1120  WaveLinkOptionFlags = wlnk->ReadUint16();
1121  PhaseGroup = wlnk->ReadUint16();
1122  Channel = wlnk->ReadUint32();
1123  WavePoolTableIndex = wlnk->ReadUint32();
1124  } else { // 'wlnk' chunk is missing
1125  WaveLinkOptionFlags = 0;
1126  PhaseGroup = 0;
1127  Channel = 0; // mono
1128  WavePoolTableIndex = 0; // first entry in wave pool table
1129  }
1130  PhaseMaster = WaveLinkOptionFlags & F_WAVELINK_PHASE_MASTER;
1131  MultiChannel = WaveLinkOptionFlags & F_WAVELINK_MULTICHANNEL;
1132 
1133  pSample = NULL;
1134  }
1135 
1142  }
1143 
1149  // handle base classes
1153 
1154  // handle own RIFF chunks
1155  if (pCkRegion) {
1156  RIFF::List* pParent = pCkRegion->GetParent();
1157  pParent->DeleteSubChunk(pCkRegion);
1158  pCkRegion = NULL;
1159  }
1160  }
1161 
1162  Sample* Region::GetSample() {
1163  if (pSample) return pSample;
1164  File* file = (File*) GetParent()->GetParent();
1165  uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
1166  Sample* sample = file->GetFirstSample();
1167  while (sample) {
1168  if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample);
1169  sample = file->GetNextSample();
1170  }
1171  return NULL;
1172  }
1173 
1179  void Region::SetSample(Sample* pSample) {
1180  this->pSample = pSample;
1181  WavePoolTableIndex = 0; // we update this offset when we Save()
1182  }
1183 
1191  void Region::SetKeyRange(uint16_t Low, uint16_t High) {
1192  KeyRange.low = Low;
1193  KeyRange.high = High;
1194 
1195  // make sure regions are already loaded
1196  Instrument* pInstrument = (Instrument*) GetParent();
1197  if (!pInstrument->pRegions) pInstrument->LoadRegions();
1198  if (!pInstrument->pRegions) return;
1199 
1200  // find the r which is the first one to the right of this region
1201  // at its new position
1202  Region* r = NULL;
1203  Region* prev_region = NULL;
1204  for (
1205  Instrument::RegionList::iterator iter = pInstrument->pRegions->begin();
1206  iter != pInstrument->pRegions->end(); iter++
1207  ) {
1208  if ((*iter)->KeyRange.low > this->KeyRange.low) {
1209  r = *iter;
1210  break;
1211  }
1212  prev_region = *iter;
1213  }
1214 
1215  // place this region before r if it's not already there
1216  if (prev_region != this) pInstrument->MoveRegion(this, r);
1217  }
1218 
1227  // make sure 'rgnh' chunk exists
1228  RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);
1229  if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
1230  uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();
1231  FormatOptionFlags = (SelfNonExclusive)
1232  ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE
1233  : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);
1234  // update 'rgnh' chunk
1235  store16(&pData[0], KeyRange.low);
1236  store16(&pData[2], KeyRange.high);
1237  store16(&pData[4], VelocityRange.low);
1238  store16(&pData[6], VelocityRange.high);
1239  store16(&pData[8], FormatOptionFlags);
1240  store16(&pData[10], KeyGroup);
1241  if (rgnh->GetSize() >= 14) store16(&pData[12], Layer);
1242 
1243  // update chunks of base classes as well (but skip Resource,
1244  // as a rgn doesn't seem to have dlid and INFO chunks)
1245  Articulator::UpdateChunks(pProgress);
1246  Sampler::UpdateChunks(pProgress);
1247 
1248  // make sure 'wlnk' chunk exists
1249  RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK);
1250  if (!wlnk) wlnk = pCkRegion->AddSubChunk(CHUNK_ID_WLNK, 12);
1251  pData = (uint8_t*) wlnk->LoadChunkData();
1252  WaveLinkOptionFlags = (PhaseMaster)
1253  ? WaveLinkOptionFlags | F_WAVELINK_PHASE_MASTER
1254  : WaveLinkOptionFlags & (~F_WAVELINK_PHASE_MASTER);
1255  WaveLinkOptionFlags = (MultiChannel)
1256  ? WaveLinkOptionFlags | F_WAVELINK_MULTICHANNEL
1257  : WaveLinkOptionFlags & (~F_WAVELINK_MULTICHANNEL);
1258  // get sample's wave pool table index
1259  int index = -1;
1260  File* pFile = (File*) GetParent()->GetParent();
1261  if (pFile->pSamples) {
1262  File::SampleList::iterator iter = pFile->pSamples->begin();
1263  File::SampleList::iterator end = pFile->pSamples->end();
1264  for (int i = 0; iter != end; ++iter, i++) {
1265  if (*iter == pSample) {
1266  index = i;
1267  break;
1268  }
1269  }
1270  }
1271  WavePoolTableIndex = index;
1272  // update 'wlnk' chunk
1273  store16(&pData[0], WaveLinkOptionFlags);
1274  store16(&pData[2], PhaseGroup);
1275  store32(&pData[4], Channel);
1276  store32(&pData[8], WavePoolTableIndex);
1277  }
1278 
1288  void Region::CopyAssign(const Region* orig) {
1289  // handle base classes
1290  Resource::CopyAssign(orig);
1292  Sampler::CopyAssign(orig);
1293  // handle actual own attributes of this class
1294  // (the trivial ones)
1295  VelocityRange = orig->VelocityRange;
1296  KeyGroup = orig->KeyGroup;
1297  Layer = orig->Layer;
1298  SelfNonExclusive = orig->SelfNonExclusive;
1299  PhaseMaster = orig->PhaseMaster;
1300  PhaseGroup = orig->PhaseGroup;
1301  MultiChannel = orig->MultiChannel;
1302  Channel = orig->Channel;
1303  // only take the raw sample reference if the two Region objects are
1304  // part of the same file
1305  if (GetParent()->GetParent() == orig->GetParent()->GetParent()) {
1306  WavePoolTableIndex = orig->WavePoolTableIndex;
1307  pSample = orig->pSample;
1308  } else {
1309  WavePoolTableIndex = -1;
1310  pSample = NULL;
1311  }
1312  FormatOptionFlags = orig->FormatOptionFlags;
1313  WaveLinkOptionFlags = orig->WaveLinkOptionFlags;
1314  // handle the last, a bit sensible attribute
1315  SetKeyRange(orig->KeyRange.low, orig->KeyRange.high);
1316  }
1317 
1318 
1319 // *************** Instrument ***************
1320 // *
1321 
1335  Instrument::Instrument(File* pFile, RIFF::List* insList) : Resource(pFile, insList), Articulator(insList) {
1336  pCkInstrument = insList;
1337 
1338  midi_locale_t locale;
1339  RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1340  if (insh) {
1341  insh->SetPos(0);
1342 
1343  Regions = insh->ReadUint32();
1344  insh->Read(&locale, 2, 4);
1345  } else { // 'insh' chunk missing
1346  Regions = 0;
1347  locale.bank = 0;
1348  locale.instrument = 0;
1349  }
1350 
1351  MIDIProgram = locale.instrument;
1352  IsDrum = locale.bank & DRUM_TYPE_MASK;
1353  MIDIBankCoarse = (uint8_t) MIDI_BANK_COARSE(locale.bank);
1354  MIDIBankFine = (uint8_t) MIDI_BANK_FINE(locale.bank);
1355  MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine);
1356 
1357  pRegions = NULL;
1358  }
1359 
1360  Region* Instrument::GetFirstRegion() {
1361  if (!pRegions) LoadRegions();
1362  if (!pRegions) return NULL;
1363  RegionsIterator = pRegions->begin();
1364  return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1365  }
1366 
1367  Region* Instrument::GetNextRegion() {
1368  if (!pRegions) return NULL;
1369  RegionsIterator++;
1370  return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1371  }
1372 
1373  void Instrument::LoadRegions() {
1374  if (!pRegions) pRegions = new RegionList;
1375  RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1376  if (lrgn) {
1377  uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2
1378  RIFF::List* rgn = lrgn->GetFirstSubList();
1379  while (rgn) {
1380  if (rgn->GetListType() == regionCkType) {
1381  pRegions->push_back(new Region(this, rgn));
1382  }
1383  rgn = lrgn->GetNextSubList();
1384  }
1385  }
1386  }
1387 
1388  Region* Instrument::AddRegion() {
1389  if (!pRegions) LoadRegions();
1390  RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1391  if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
1392  RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
1393  Region* pNewRegion = new Region(this, rgn);
1394  pRegions->push_back(pNewRegion);
1395  Regions = (uint32_t) pRegions->size();
1396  return pNewRegion;
1397  }
1398 
1399  void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
1400  RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1401  lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));
1402 
1403  pRegions->remove(pSrc);
1404  RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
1405  pRegions->insert(iter, pSrc);
1406  }
1407 
1408  void Instrument::DeleteRegion(Region* pRegion) {
1409  if (!pRegions) return;
1410  RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
1411  if (iter == pRegions->end()) return;
1412  pRegions->erase(iter);
1413  Regions = (uint32_t) pRegions->size();
1414  pRegion->DeleteChunks();
1415  delete pRegion;
1416  }
1417 
1426  // first update base classes' chunks
1427  Resource::UpdateChunks(pProgress);
1428  Articulator::UpdateChunks(pProgress);
1429  // make sure 'insh' chunk exists
1430  RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1431  if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
1432  uint8_t* pData = (uint8_t*) insh->LoadChunkData();
1433  // update 'insh' chunk
1434  Regions = (pRegions) ? uint32_t(pRegions->size()) : 0;
1435  midi_locale_t locale;
1436  locale.instrument = MIDIProgram;
1437  locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
1438  locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK);
1439  MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it
1440  store32(&pData[0], Regions);
1441  store32(&pData[4], locale.bank);
1442  store32(&pData[8], locale.instrument);
1443  // update Region's chunks
1444  if (!pRegions) return;
1445  RegionList::iterator iter = pRegions->begin();
1446  RegionList::iterator end = pRegions->end();
1447  for (int i = 0; iter != end; ++iter, ++i) {
1448  if (pProgress) {
1449  // divide local progress into subprogress
1450  progress_t subprogress;
1451  __divide_progress(pProgress, &subprogress, pRegions->size(), i);
1452  // do the actual work
1453  (*iter)->UpdateChunks(&subprogress);
1454  } else
1455  (*iter)->UpdateChunks(NULL);
1456  }
1457  if (pProgress)
1458  __notify_progress(pProgress, 1.0); // notify done
1459  }
1460 
1466  if (pRegions) {
1467  RegionList::iterator iter = pRegions->begin();
1468  RegionList::iterator end = pRegions->end();
1469  while (iter != end) {
1470  delete *iter;
1471  iter++;
1472  }
1473  delete pRegions;
1474  }
1475  }
1476 
1482  // handle base classes
1485 
1486  // handle RIFF chunks of members
1487  if (pRegions) {
1488  RegionList::iterator it = pRegions->begin();
1489  RegionList::iterator end = pRegions->end();
1490  for (; it != end; ++it)
1491  (*it)->DeleteChunks();
1492  }
1493 
1494  // handle own RIFF chunks
1495  if (pCkInstrument) {
1496  RIFF::List* pParent = pCkInstrument->GetParent();
1497  pParent->DeleteSubChunk(pCkInstrument);
1498  pCkInstrument = NULL;
1499  }
1500  }
1501 
1502  void Instrument::CopyAssignCore(const Instrument* orig) {
1503  // handle base classes
1504  Resource::CopyAssign(orig);
1506  // handle actual own attributes of this class
1507  // (the trivial ones)
1508  IsDrum = orig->IsDrum;
1509  MIDIBank = orig->MIDIBank;
1511  MIDIBankFine = orig->MIDIBankFine;
1512  MIDIProgram = orig->MIDIProgram;
1513  }
1514 
1525  CopyAssignCore(orig);
1526  // delete all regions first
1527  while (Regions) DeleteRegion(GetFirstRegion());
1528  // now recreate and copy regions
1529  {
1530  RegionList::const_iterator it = orig->pRegions->begin();
1531  for (int i = 0; i < orig->Regions; ++i, ++it) {
1532  Region* dstRgn = AddRegion();
1533  //NOTE: Region does semi-deep copy !
1534  dstRgn->CopyAssign(*it);
1535  }
1536  }
1537  }
1538 
1539 
1540 // *************** File ***************
1541 // *
1542 
1549  File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1550  pRIFF->SetByteOrder(RIFF::endian_little);
1551  bOwningRiff = true;
1552  pVersion = new version_t;
1553  pVersion->major = 0;
1554  pVersion->minor = 0;
1555  pVersion->release = 0;
1556  pVersion->build = 0;
1557 
1558  Instruments = 0;
1559  WavePoolCount = 0;
1560  pWavePoolTable = NULL;
1561  pWavePoolTableHi = NULL;
1562  WavePoolHeaderSize = 8;
1563 
1564  pSamples = NULL;
1565  pInstruments = NULL;
1566 
1567  b64BitWavePoolOffsets = false;
1568  }
1569 
1579  File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1580  if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1581  this->pRIFF = pRIFF;
1582  bOwningRiff = false;
1583  RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1584  if (ckVersion) {
1585  ckVersion->SetPos(0);
1586 
1587  pVersion = new version_t;
1588  ckVersion->Read(pVersion, 4, 2);
1589  }
1590  else pVersion = NULL;
1591 
1592  RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1593  if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found.");
1594  colh->SetPos(0);
1595  Instruments = colh->ReadUint32();
1596 
1597  RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1598  if (!ptbl) { // pool table is missing - this is probably an ".art" file
1599  WavePoolCount = 0;
1600  pWavePoolTable = NULL;
1601  pWavePoolTableHi = NULL;
1602  WavePoolHeaderSize = 8;
1603  b64BitWavePoolOffsets = false;
1604  } else {
1605  ptbl->SetPos(0);
1606 
1607  WavePoolHeaderSize = ptbl->ReadUint32();
1608  WavePoolCount = ptbl->ReadUint32();
1609  pWavePoolTable = new uint32_t[WavePoolCount];
1610  pWavePoolTableHi = new uint32_t[WavePoolCount];
1611  ptbl->SetPos(WavePoolHeaderSize);
1612 
1613  // Check for 64 bit offsets (used in gig v3 files)
1614  b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8);
1615  if (b64BitWavePoolOffsets) {
1616  for (int i = 0 ; i < WavePoolCount ; i++) {
1617  pWavePoolTableHi[i] = ptbl->ReadUint32();
1618  pWavePoolTable[i] = ptbl->ReadUint32();
1619  //NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12)
1620  //if (pWavePoolTable[i] & 0x80000000)
1621  // throw DLS::Exception("Files larger than 2 GB not yet supported");
1622  }
1623  } else { // conventional 32 bit offsets
1624  ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
1625  for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0;
1626  }
1627  }
1628 
1629  pSamples = NULL;
1630  pInstruments = NULL;
1631  }
1632 
1633  File::~File() {
1634  if (pInstruments) {
1635  InstrumentList::iterator iter = pInstruments->begin();
1636  InstrumentList::iterator end = pInstruments->end();
1637  while (iter != end) {
1638  delete *iter;
1639  iter++;
1640  }
1641  delete pInstruments;
1642  }
1643 
1644  if (pSamples) {
1645  SampleList::iterator iter = pSamples->begin();
1646  SampleList::iterator end = pSamples->end();
1647  while (iter != end) {
1648  delete *iter;
1649  iter++;
1650  }
1651  delete pSamples;
1652  }
1653 
1654  if (pWavePoolTable) delete[] pWavePoolTable;
1655  if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1656  if (pVersion) delete pVersion;
1657  for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)
1658  delete *i;
1659  if (bOwningRiff)
1660  delete pRIFF;
1661  }
1662 
1664  if (!pSamples) LoadSamples();
1665  if (!pSamples) return NULL;
1666  SamplesIterator = pSamples->begin();
1667  return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1668  }
1669 
1671  if (!pSamples) return NULL;
1672  SamplesIterator++;
1673  return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1674  }
1675 
1676  void File::LoadSamples() {
1677  if (!pSamples) pSamples = new SampleList;
1678  RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1679  if (wvpl) {
1680  file_offset_t wvplFileOffset = wvpl->GetFilePos() -
1681  wvpl->GetPos(); // should be zero, but just to be sure
1682  RIFF::List* wave = wvpl->GetFirstSubList();
1683  while (wave) {
1684  if (wave->GetListType() == LIST_TYPE_WAVE) {
1685  file_offset_t waveFileOffset = wave->GetFilePos() -
1686  wave->GetPos(); // should be zero, but just to be sure
1687  pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1688  }
1689  wave = wvpl->GetNextSubList();
1690  }
1691  }
1692  else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant)
1693  RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1694  if (dwpl) {
1695  file_offset_t dwplFileOffset = dwpl->GetFilePos() -
1696  dwpl->GetPos(); // should be zero, but just to be sure
1697  RIFF::List* wave = dwpl->GetFirstSubList();
1698  while (wave) {
1699  if (wave->GetListType() == LIST_TYPE_WAVE) {
1700  file_offset_t waveFileOffset = wave->GetFilePos() -
1701  wave->GetPos(); // should be zero, but just to be sure
1702  pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1703  }
1704  wave = dwpl->GetNextSubList();
1705  }
1706  }
1707  }
1708  }
1709 
1718  if (!pSamples) LoadSamples();
1720  RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1721  // create new Sample object and its respective 'wave' list chunk
1722  RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
1723  Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
1724  pSamples->push_back(pSample);
1725  return pSample;
1726  }
1727 
1735  void File::DeleteSample(Sample* pSample) {
1736  if (!pSamples) return;
1737  SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1738  if (iter == pSamples->end()) return;
1739  pSamples->erase(iter);
1740  pSample->DeleteChunks();
1741  delete pSample;
1742  }
1743 
1745  if (!pInstruments) LoadInstruments();
1746  if (!pInstruments) return NULL;
1747  InstrumentsIterator = pInstruments->begin();
1748  return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1749  }
1750 
1752  if (!pInstruments) return NULL;
1753  InstrumentsIterator++;
1754  return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1755  }
1756 
1757  void File::LoadInstruments() {
1758  if (!pInstruments) pInstruments = new InstrumentList;
1759  RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1760  if (lstInstruments) {
1761  RIFF::List* lstInstr = lstInstruments->GetFirstSubList();
1762  while (lstInstr) {
1763  if (lstInstr->GetListType() == LIST_TYPE_INS) {
1764  pInstruments->push_back(new Instrument(this, lstInstr));
1765  }
1766  lstInstr = lstInstruments->GetNextSubList();
1767  }
1768  }
1769  }
1770 
1779  if (!pInstruments) LoadInstruments();
1781  RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1782  RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
1783  Instrument* pInstrument = new Instrument(this, lstInstr);
1784  pInstruments->push_back(pInstrument);
1785  return pInstrument;
1786  }
1787 
1795  void File::DeleteInstrument(Instrument* pInstrument) {
1796  if (!pInstruments) return;
1797  InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1798  if (iter == pInstruments->end()) return;
1799  pInstruments->erase(iter);
1800  pInstrument->DeleteChunks();
1801  delete pInstrument;
1802  }
1803 
1809  return pRIFF;
1810  }
1811 
1825  if (index < 0 || index >= ExtensionFiles.size()) return NULL;
1826  std::list<RIFF::File*>::iterator iter = ExtensionFiles.begin();
1827  for (int i = 0; iter != ExtensionFiles.end(); ++iter, ++i)
1828  if (i == index) return *iter;
1829  return NULL;
1830  }
1831 
1842  return pRIFF->GetFileName();
1843  }
1844 
1849  void File::SetFileName(const String& name) {
1850  pRIFF->SetFileName(name);
1851  }
1852 
1861  void File::UpdateChunks(progress_t* pProgress) {
1862  // first update base class's chunks
1863  Resource::UpdateChunks(pProgress);
1864 
1865  // if version struct exists, update 'vers' chunk
1866  if (pVersion) {
1867  RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1868  if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);
1869  uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();
1870  store16(&pData[0], pVersion->minor);
1871  store16(&pData[2], pVersion->major);
1872  store16(&pData[4], pVersion->build);
1873  store16(&pData[6], pVersion->release);
1874  }
1875 
1876  // update 'colh' chunk
1877  Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0;
1878  RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1879  if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1880  uint8_t* pData = (uint8_t*) colh->LoadChunkData();
1881  store32(pData, Instruments);
1882 
1883  // update instrument's chunks
1884  if (pInstruments) {
1885  if (pProgress) {
1886  // divide local progress into subprogress
1887  progress_t subprogress;
1888  __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress
1889 
1890  // do the actual work
1891  InstrumentList::iterator iter = pInstruments->begin();
1892  InstrumentList::iterator end = pInstruments->end();
1893  for (int i = 0; iter != end; ++iter, ++i) {
1894  // divide subprogress into sub-subprogress
1895  progress_t subsubprogress;
1896  __divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i);
1897  // do the actual work
1898  (*iter)->UpdateChunks(&subsubprogress);
1899  }
1900 
1901  __notify_progress(&subprogress, 1.0); // notify subprogress done
1902  } else {
1903  InstrumentList::iterator iter = pInstruments->begin();
1904  InstrumentList::iterator end = pInstruments->end();
1905  for (int i = 0; iter != end; ++iter, ++i) {
1906  (*iter)->UpdateChunks(NULL);
1907  }
1908  }
1909  }
1910 
1911  // update 'ptbl' chunk
1912  const int iSamples = (pSamples) ? int(pSamples->size()) : 0;
1913  int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1914  RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1915  if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/);
1916  int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1917  ptbl->Resize(iPtblSize);
1918  pData = (uint8_t*) ptbl->LoadChunkData();
1919  WavePoolCount = iSamples;
1920  store32(&pData[4], WavePoolCount);
1921  // we actually update the sample offsets in the pool table when we Save()
1922  memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);
1923 
1924  // update sample's chunks
1925  if (pSamples) {
1926  if (pProgress) {
1927  // divide local progress into subprogress
1928  progress_t subprogress;
1929  __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress
1930 
1931  // do the actual work
1932  SampleList::iterator iter = pSamples->begin();
1933  SampleList::iterator end = pSamples->end();
1934  for (int i = 0; iter != end; ++iter, ++i) {
1935  // divide subprogress into sub-subprogress
1936  progress_t subsubprogress;
1937  __divide_progress(&subprogress, &subsubprogress, pSamples->size(), i);
1938  // do the actual work
1939  (*iter)->UpdateChunks(&subsubprogress);
1940  }
1941 
1942  __notify_progress(&subprogress, 1.0); // notify subprogress done
1943  } else {
1944  SampleList::iterator iter = pSamples->begin();
1945  SampleList::iterator end = pSamples->end();
1946  for (int i = 0; iter != end; ++iter, ++i) {
1947  (*iter)->UpdateChunks(NULL);
1948  }
1949  }
1950  }
1951 
1952  // if there are any extension files, gather which ones are regular
1953  // extension files used as wave pool files (.gx00, .gx01, ... , .gx98)
1954  // and which one is probably a convolution (GigaPulse) file (always to
1955  // be saved as .gx99)
1956  std::list<RIFF::File*> poolFiles; // < for (.gx00, .gx01, ... , .gx98) files
1957  RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file
1958  if (!ExtensionFiles.empty()) {
1959  std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
1960  for (; it != ExtensionFiles.end(); ++it) {
1961  //FIXME: the .gx99 file is always used by GSt for convolution
1962  // data (GigaPulse); so we should better detect by subchunk
1963  // whether the extension file is intended for convolution
1964  // instead of checkking for a file name, because the latter does
1965  // not work for saving new gigs created from scratch
1966  const std::string oldName = (*it)->GetFileName();
1967  const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
1968  if (isGigaPulseFile)
1969  pGigaPulseFile = *it;
1970  else
1971  poolFiles.push_back(*it);
1972  }
1973  }
1974 
1975  // update the 'xfil' chunk which describes all extension files (wave
1976  // pool files) except the .gx99 file
1977  if (!poolFiles.empty()) {
1978  const int n = poolFiles.size();
1979  const int iHeaderSize = 4;
1980  const int iEntrySize = 144;
1981 
1982  // make sure chunk exists, and with correct size
1983  RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
1984  if (ckXfil)
1985  ckXfil->Resize(iHeaderSize + n * iEntrySize);
1986  else
1987  ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize);
1988 
1989  uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData();
1990 
1991  // re-assemble the chunk's content
1992  store32(pData, n);
1993  std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin();
1994  for (int i = 0, iOffset = 4; i < n;
1995  ++itExtFile, ++i, iOffset += iEntrySize)
1996  {
1997  // update the filename string and 5 byte extension of each extension file
1998  std::string file = lastPathComponent(
1999  (*itExtFile)->GetFileName()
2000  );
2001  if (file.length() + 6 > 128)
2002  throw Exception("Fatal error, extension filename length exceeds 122 byte maximum");
2003  uint8_t* pStrings = &pData[iOffset];
2004  memset(pStrings, 0, 128);
2005  memcpy(pStrings, file.c_str(), file.length());
2006  pStrings += file.length() + 1;
2007  std::string ext = file.substr(file.length()-5);
2008  memcpy(pStrings, ext.c_str(), 5);
2009  // update the dlsid of the extension file
2010  uint8_t* pId = &pData[iOffset + 128];
2011  dlsid_t id;
2012  RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID);
2013  if (ckDLSID) {
2014  ckDLSID->Read(&id.ulData1, 1, 4);
2015  ckDLSID->Read(&id.usData2, 1, 2);
2016  ckDLSID->Read(&id.usData3, 1, 2);
2017  ckDLSID->Read(id.abData, 8, 1);
2018  } else {
2019  ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16);
2021  uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
2022  store32(&pData[0], id.ulData1);
2023  store16(&pData[4], id.usData2);
2024  store16(&pData[6], id.usData3);
2025  memcpy(&pData[8], id.abData, 8);
2026  }
2027  store32(&pId[0], id.ulData1);
2028  store16(&pId[4], id.usData2);
2029  store16(&pId[6], id.usData3);
2030  memcpy(&pId[8], id.abData, 8);
2031  }
2032  } else {
2033  // in case there was a 'xfil' chunk, remove it
2034  RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2035  if (ckXfil) pRIFF->DeleteSubChunk(ckXfil);
2036  }
2037 
2038  // update the 'doxf' chunk which describes a .gx99 extension file
2039  // which contains convolution data (GigaPulse)
2040  if (pGigaPulseFile) {
2041  RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2042  if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148);
2043 
2044  uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData();
2045 
2046  // update the dlsid from the extension file
2047  uint8_t* pId = &pData[132];
2048  RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID);
2049  if (!ckDLSID) { //TODO: auto generate DLS ID if missing
2050  throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk");
2051  } else {
2052  dlsid_t id;
2053  // read DLS ID from extension files's DLS ID chunk
2054  uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData();
2055  id.ulData1 = load32(&pData[0]);
2056  id.usData2 = load16(&pData[4]);
2057  id.usData3 = load16(&pData[6]);
2058  memcpy(id.abData, &pData[8], 8);
2059  // store DLS ID to 'doxf' chunk
2060  store32(&pId[0], id.ulData1);
2061  store16(&pId[4], id.usData2);
2062  store16(&pId[6], id.usData3);
2063  memcpy(&pId[8], id.abData, 8);
2064  }
2065  } else {
2066  // in case there was a 'doxf' chunk, remove it
2067  RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2068  if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf);
2069  }
2070 
2071  // the RIFF file to be written might now been grown >= 4GB or might
2072  // been shrunk < 4GB, so we might need to update the wave pool offset
2073  // size and thus accordingly we would need to resize the wave pool
2074  // chunk
2075  const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();
2076  const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file
2077  poolFiles.size() > 0; // < 32 bit gig file where the hi 32 bits are used as extension file nr
2078  if (b64BitWavePoolOffsets != bRequires64Bit) {
2079  b64BitWavePoolOffsets = bRequires64Bit;
2080  iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2081  iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
2082  ptbl->Resize(iPtblSize);
2083  }
2084 
2085  if (pProgress)
2086  __notify_progress(pProgress, 1.0); // notify done
2087  }
2088 
2103  void File::Save(const String& Path, progress_t* pProgress) {
2104  // calculate number of tasks to notify progress appropriately
2105  const size_t nExtFiles = ExtensionFiles.size();
2106  const float tasks = 2.f + nExtFiles;
2107 
2108  // save extension files (if required)
2109  if (!ExtensionFiles.empty()) {
2110  // for assembling path of extension files to be saved to
2111  const std::string baseName = pathWithoutExtension(Path);
2112  // save the individual extension files
2113  std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2114  for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2115  //FIXME: the .gx99 file is always used by GSt for convolution
2116  // data (GigaPulse); so we should better detect by subchunk
2117  // whether the extension file is intended for convolution
2118  // instead of checkking for a file name, because the latter does
2119  // not work for saving new gigs created from scratch
2120  const std::string oldName = (*it)->GetFileName();
2121  const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2122  std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1);
2123  std::string newPath = baseName + ext;
2124  // save extension file to its new location
2125  if (pProgress) {
2126  // divide local progress into subprogress
2127  progress_t subprogress;
2128  __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2129  // do the actual work
2130  (*it)->Save(newPath, &subprogress);
2131  } else
2132  (*it)->Save(newPath);
2133  }
2134  }
2135 
2136  if (pProgress) {
2137  // divide local progress into subprogress
2138  progress_t subprogress;
2139  __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2140  // do the actual work
2141  UpdateChunks(&subprogress);
2142  } else
2143  UpdateChunks(NULL);
2144 
2145  if (pProgress) {
2146  // divide local progress into subprogress
2147  progress_t subprogress;
2148  __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2149  // do the actual work
2150  pRIFF->Save(Path, &subprogress);
2151  } else
2152  pRIFF->Save(Path);
2153 
2155 
2156  if (pProgress)
2157  __notify_progress(pProgress, 1.0); // notify done
2158  }
2159 
2170  void File::Save(progress_t* pProgress) {
2171  // calculate number of tasks to notify progress appropriately
2172  const size_t nExtFiles = ExtensionFiles.size();
2173  const float tasks = 2.f + nExtFiles;
2174 
2175  // save extension files (if required)
2176  if (!ExtensionFiles.empty()) {
2177  std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2178  for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2179  // save extension file
2180  if (pProgress) {
2181  // divide local progress into subprogress
2182  progress_t subprogress;
2183  __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2184  // do the actual work
2185  (*it)->Save(&subprogress);
2186  } else
2187  (*it)->Save();
2188  }
2189  }
2190 
2191  if (pProgress) {
2192  // divide local progress into subprogress
2193  progress_t subprogress;
2194  __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2195  // do the actual work
2196  UpdateChunks(&subprogress);
2197  } else
2198  UpdateChunks(NULL);
2199 
2200  if (pProgress) {
2201  // divide local progress into subprogress
2202  progress_t subprogress;
2203  __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2204  // do the actual work
2205  pRIFF->Save(&subprogress);
2206  } else
2207  pRIFF->Save();
2208 
2210 
2211  if (pProgress)
2212  __notify_progress(pProgress, 1.0); // notify done
2213  }
2214 
2226  __UpdateWavePoolTableChunk();
2227  }
2228 
2235  // enusre 'lins' list chunk exists (mandatory for instrument definitions)
2236  RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2237  if (!lstInstruments) pRIFF->AddSubList(LIST_TYPE_LINS);
2238  // ensure 'ptbl' chunk exists (mandatory for samples)
2239  RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2240  if (!ptbl) {
2241  const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2242  ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, WavePoolHeaderSize + iOffsetSize);
2243  }
2244  // enusre 'wvpl' list chunk exists (mandatory for samples)
2245  RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2246  if (!wvpl) pRIFF->AddSubList(LIST_TYPE_WVPL);
2247  }
2248 
2258  void File::__UpdateWavePoolTableChunk() {
2259  __UpdateWavePoolTable();
2260  RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2261  const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2262  // check if 'ptbl' chunk is large enough
2263  WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2264  const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
2265  if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
2266  // save the 'ptbl' chunk's current read/write position
2267  file_offset_t ullOriginalPos = ptbl->GetPos();
2268  // update headers
2269  ptbl->SetPos(0);
2270  uint32_t tmp = WavePoolHeaderSize;
2271  ptbl->WriteUint32(&tmp);
2272  tmp = WavePoolCount;
2273  ptbl->WriteUint32(&tmp);
2274  // update offsets
2275  ptbl->SetPos(WavePoolHeaderSize);
2276  if (b64BitWavePoolOffsets) {
2277  for (int i = 0 ; i < WavePoolCount ; i++) {
2278  tmp = pWavePoolTableHi[i];
2279  ptbl->WriteUint32(&tmp);
2280  tmp = pWavePoolTable[i];
2281  ptbl->WriteUint32(&tmp);
2282  }
2283  } else { // conventional 32 bit offsets
2284  for (int i = 0 ; i < WavePoolCount ; i++) {
2285  tmp = pWavePoolTable[i];
2286  ptbl->WriteUint32(&tmp);
2287  }
2288  }
2289  // restore 'ptbl' chunk's original read/write position
2290  ptbl->SetPos(ullOriginalPos);
2291  }
2292 
2298  void File::__UpdateWavePoolTable() {
2299  WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2300  // resize wave pool table arrays
2301  if (pWavePoolTable) delete[] pWavePoolTable;
2302  if (pWavePoolTableHi) delete[] pWavePoolTableHi;
2303  pWavePoolTable = new uint32_t[WavePoolCount];
2304  pWavePoolTableHi = new uint32_t[WavePoolCount];
2305  if (!pSamples) return;
2306  // update offsets in wave pool table
2307  RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2308  uint64_t wvplFileOffset = wvpl->GetFilePos() -
2309  wvpl->GetPos(); // mandatory, since position might have changed
2310  if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ...
2311  SampleList::iterator iter = pSamples->begin();
2312  SampleList::iterator end = pSamples->end();
2313  for (int i = 0 ; iter != end ; ++iter, i++) {
2314  uint64_t _64BitOffset =
2315  (*iter)->pWaveList->GetFilePos() -
2316  (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2317  wvplFileOffset -
2318  LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2319  (*iter)->ullWavePoolOffset = _64BitOffset;
2320  pWavePoolTable[i] = (uint32_t) _64BitOffset;
2321  }
2322  } else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ...
2323  if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ...
2324  SampleList::iterator iter = pSamples->begin();
2325  SampleList::iterator end = pSamples->end();
2326  for (int i = 0 ; iter != end ; ++iter, i++) {
2327  uint64_t _64BitOffset =
2328  (*iter)->pWaveList->GetFilePos() -
2329  (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2330  wvplFileOffset -
2331  LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2332  (*iter)->ullWavePoolOffset = _64BitOffset;
2333  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
2334  pWavePoolTable[i] = (uint32_t) _64BitOffset;
2335  }
2336  } else { // 32 bit offsets with extension files (GigaStudio legacy support) ...
2337  // the main gig and the extension files may contain wave data
2338  std::vector<RIFF::File*> poolFiles;
2339  poolFiles.push_back(pRIFF);
2340  poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end());
2341 
2342  RIFF::File* pCurPoolFile = NULL;
2343  int fileNo = 0;
2344  int waveOffset = 0;
2345  SampleList::iterator iter = pSamples->begin();
2346  SampleList::iterator end = pSamples->end();
2347  for (int i = 0 ; iter != end ; ++iter, i++) {
2348  RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile();
2349  // if this sample is located in the same pool file as the
2350  // last we reuse the previously computed fileNo and waveOffset
2351  if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ...
2352  pCurPoolFile = pPoolFile;
2353 
2354  std::vector<RIFF::File*>::iterator sIter;
2355  sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile);
2356  if (sIter != poolFiles.end())
2357  fileNo = std::distance(poolFiles.begin(), sIter);
2358  else
2359  throw DLS::Exception("Fatal error, unknown pool file");
2360 
2361  RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL);
2362  if (!extWvpl)
2363  throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk");
2364  waveOffset =
2365  extWvpl->GetFilePos() -
2366  extWvpl->GetPos() + // mandatory, since position might have changed
2367  LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize());
2368  }
2369  uint64_t _64BitOffset =
2370  (*iter)->pWaveList->GetFilePos() -
2371  (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2372  waveOffset;
2373  // pWavePoolTableHi stores file number when extension files are in use
2374  pWavePoolTableHi[i] = (uint32_t) fileNo;
2375  pWavePoolTable[i] = (uint32_t) _64BitOffset;
2376  (*iter)->ullWavePoolOffset = _64BitOffset;
2377  }
2378  }
2379  }
2380  }
2381 
2382 
2383 // *************** Exception ***************
2384 // *
2385 
2386  Exception::Exception() : RIFF::Exception() {
2387  }
2388 
2389  Exception::Exception(String format, ...) : RIFF::Exception() {
2390  va_list arg;
2391  va_start(arg, format);
2392  Message = assemble(format, arg);
2393  va_end(arg);
2394  }
2395 
2396  Exception::Exception(String format, va_list arg) : RIFF::Exception() {
2397  Message = assemble(format, arg);
2398  }
2399 
2400  void Exception::PrintMessage() {
2401  std::cout << "DLS::Exception: " << Message << std::endl;
2402  }
2403 
2404 
2405 // *************** functions ***************
2406 // *
2407 
2413  String libraryName() {
2414  return PACKAGE;
2415  }
2416 
2421  String libraryVersion() {
2422  return VERSION;
2423  }
2424 
2425 } // namespace DLS
Connection * pConnections
Points to the beginning of a Connection array.
Definition: DLS.h:330
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Articulation object.
Definition: DLS.cpp:179
virtual void UpdateChunks(progress_t *pProgress)
Apply articulation connections to the respective RIFF chunks.
Definition: DLS.cpp:154
uint32_t Connections
Reflects the number of Connections.
Definition: DLS.h:331
Articulation(RIFF::Chunk *artl)
Constructor.
Definition: DLS.cpp:119
Abstract base class for classes that provide articulation information (thus for Instrument and Region...
Definition: DLS.h:343
virtual void UpdateChunks(progress_t *pProgress)
Apply all articulations to the respective RIFF chunks.
Definition: DLS.cpp:241
virtual void CopyAssign(const Articulator *orig)
Not yet implemented in this version, since the .gig format does not need to copy DLS articulators and...
Definition: DLS.cpp:270
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Articulator object.
Definition: DLS.cpp:255
Defines a connection within the synthesis model.
Definition: DLS.h:249
Will be thrown whenever a DLS specific error occurs while trying to access a DLS File.
Definition: DLS.h:618
Parses DLS Level 1 and 2 compliant files and provides abstract access to the data.
Definition: DLS.h:561
version_t * pVersion
Points to a version_t structure if the file provided a version number else is set to NULL.
Definition: DLS.h:563
Instrument * AddInstrument()
Add a new instrument definition.
Definition: DLS.cpp:1778
virtual void UpdateChunks(progress_t *pProgress)
Apply all the DLS file's current instruments, samples and settings to the respective RIFF chunks.
Definition: DLS.cpp:1861
void __ensureMandatoryChunksExist()
Checks if all (for DLS) mandatory chunks exist, if not they will be created.
Definition: DLS.cpp:2234
File()
Constructor.
Definition: DLS.cpp:1549
Instrument * GetNextInstrument()
Returns a pointer to the next Instrument object of the file, NULL otherwise.
Definition: DLS.cpp:1751
Sample * GetNextSample()
Returns a pointer to the next Sample object of the file, NULL otherwise.
Definition: DLS.cpp:1670
void SetFileName(const String &name)
You may call this method store a future file name, so you don't have to to pass it to the Save() call...
Definition: DLS.cpp:1849
virtual void UpdateFileOffsets()
Updates all file offsets stored all over the file.
Definition: DLS.cpp:2225
void DeleteInstrument(Instrument *pInstrument)
Delete an instrument.
Definition: DLS.cpp:1795
uint32_t Instruments
Reflects the number of available Instrument objects.
Definition: DLS.h:564
virtual void Save(const String &Path, progress_t *pProgress=NULL)
Save changes to another file.
Definition: DLS.cpp:2103
bool bOwningRiff
If true then pRIFF was implicitly allocated by this class and hence pRIFF will automatically be freed...
Definition: DLS.h:599
void DeleteSample(Sample *pSample)
Delete a sample.
Definition: DLS.cpp:1735
RIFF::File * GetRiffFile()
Returns the underlying RIFF::File used for persistency of this DLS::File object.
Definition: DLS.cpp:1808
Sample * GetFirstSample()
Returns a pointer to the first Sample object of the file, NULL otherwise.
Definition: DLS.cpp:1663
Instrument * GetFirstInstrument()
Returns a pointer to the first Instrument object of the file, NULL otherwise.
Definition: DLS.cpp:1744
Sample * AddSample()
Add a new sample.
Definition: DLS.cpp:1717
RIFF::File * GetExtensionFile(int index)
Returns extension file of given index.
Definition: DLS.cpp:1824
String GetFileName()
File name of this DLS file.
Definition: DLS.cpp:1841
Optional information for DLS files, instruments, samples, etc.
Definition: DLS.h:362
String Subject
<ISBJ-ck>. Describes the contents of the file.
Definition: DLS.h:380
String Genre
<IGNR-ck>. Descirbes the original work, such as, Jazz, Classic, Rock, Techno, Rave,...
Definition: DLS.h:371
String Keywords
<IKEY-ck>. Provides a list of keywords that refer to the file or subject of the file....
Definition: DLS.h:372
String SourceForm
<ISRF-ck>. Identifies the original form of the material that was digitized, such as record,...
Definition: DLS.h:378
String Comments
<ICMT-ck>. Provides general comments about the file or the subject of the file. Sentences might end w...
Definition: DLS.h:367
String Medium
<IMED-ck>. Describes the original subject of the file, such as, record, CD, and so forth.
Definition: DLS.h:376
void SetFixedStringLengths(const string_length_t *lengths)
Forces specific Info fields to be of a fixed length when being saved to a file.
Definition: DLS.cpp:326
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Info object.
Definition: DLS.cpp:441
Info(RIFF::List *list)
Constructor.
Definition: DLS.cpp:285
String Software
<ISFT-ck>. Identifies the name of the sofware package used to create the file.
Definition: DLS.h:375
String Artists
<IART-ck>. Lists the artist of the original subject of the file.
Definition: DLS.h:370
String Product
<IPRD-ck>. Specifies the name of the title the file was originally intended for, such as World Ruler ...
Definition: DLS.h:368
virtual void CopyAssign(const Info *orig)
Make a deep copy of the Info object given by orig and assign it to this object.
Definition: DLS.cpp:450
String Name
<INAM-ck>. Stores the title of the subject of the file, such as, Seattle From Above.
Definition: DLS.h:364
String CreationDate
<ICRD-ck>. Specifies the date the subject of the file was created. List dates in yyyy-mm-dd format.
Definition: DLS.h:366
String ArchivalLocation
<IARL-ck>. Indicates where the subject of the file is stored.
Definition: DLS.h:365
String Engineer
<IENG-ck>. Stores the name of the engineer who worked on the file. Multiple engineer names are separa...
Definition: DLS.h:373
virtual void UpdateChunks(progress_t *pProgress)
Update chunks with current info values.
Definition: DLS.cpp:376
String Commissioned
<ICMS-ck>. Lists the name of the person or organization that commissioned the subject of the file,...
Definition: DLS.h:379
String Copyright
<ICOP-ck>. Records the copyright information for the file.
Definition: DLS.h:369
String Source
<ISRC-ck>. Identifies the name of the person or organization who supplied the original subject of the...
Definition: DLS.h:377
String Technician
<ITCH-ck>. Identifies the technician who sampled the subject file.
Definition: DLS.h:374
Provides all neccessary information for the synthesis of a DLS Instrument.
Definition: DLS.h:523
virtual ~Instrument()
Destructor.
Definition: DLS.cpp:1465
virtual void CopyAssign(const Instrument *orig)
Make a (semi) deep copy of the Instrument object given by orig and assign it to this object.
Definition: DLS.cpp:1524
uint8_t MIDIBankFine
Reflects the MIDI Bank number for MIDI Control Change 32 (bank 1 - 128).
Definition: DLS.h:528
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Instrument object.
Definition: DLS.cpp:1481
uint8_t MIDIBankCoarse
Reflects the MIDI Bank number for MIDI Control Change 0 (bank 1 - 128).
Definition: DLS.h:527
bool IsDrum
Indicates if the Instrument is a drum type, as they differ in the synthesis model of DLS from melodic...
Definition: DLS.h:525
uint32_t Regions
Reflects the number of Region defintions this Instrument has.
Definition: DLS.h:530
uint16_t MIDIBank
Reflects combination of MIDIBankCoarse and MIDIBankFine (bank 1 - bank 16384). Do not change this val...
Definition: DLS.h:526
Instrument(File *pFile, RIFF::List *insList)
Constructor.
Definition: DLS.cpp:1335
uint32_t MIDIProgram
Specifies the MIDI Program Change Number this Instrument should be assigned to.
Definition: DLS.h:529
virtual void UpdateChunks(progress_t *pProgress)
Apply Instrument with all its Regions to the respective RIFF chunks.
Definition: DLS.cpp:1425
Defines Region information of an Instrument.
Definition: DLS.h:492
virtual void CopyAssign(const Region *orig)
Make a (semi) deep copy of the Region object given by orig and assign it to this object.
Definition: DLS.cpp:1288
virtual ~Region()
Destructor.
Definition: DLS.cpp:1141
void SetSample(Sample *pSample)
Assign another sample to this Region.
Definition: DLS.cpp:1179
virtual void UpdateChunks(progress_t *pProgress)
Apply Region settings to the respective RIFF chunks.
Definition: DLS.cpp:1226
range_t KeyRange
Definition: DLS.h:494
virtual void SetKeyRange(uint16_t Low, uint16_t High)
Modifies the key range of this Region and makes sure the respective chunks are in correct order.
Definition: DLS.cpp:1191
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Region object.
Definition: DLS.cpp:1148
Abstract base class which encapsulates data structures which all DLS resources are able to provide.
Definition: DLS.h:403
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Resource object.
Definition: DLS.cpp:519
Resource(Resource *Parent, RIFF::List *lstResource)
Constructor.
Definition: DLS.cpp:486
Info * pInfo
Points (in any case) to an Info object, providing additional, optional infos and comments.
Definition: DLS.h:405
dlsid_t * pDLSID
Points to a dlsid_t structure if the file provided a DLS ID else is NULL.
Definition: DLS.h:406
virtual void UpdateChunks(progress_t *pProgress)
Update chunks with current Resource data.
Definition: DLS.cpp:532
void GenerateDLSID()
Generates a new DLSID for the resource.
Definition: DLS.cpp:551
virtual void CopyAssign(const Resource *orig)
Make a deep copy of the Resource object given by orig and assign it to this object.
Definition: DLS.cpp:601
Encapsulates sample waves used for playback.
Definition: DLS.h:456
file_offset_t Read(void *pBuffer, file_offset_t SampleCount)
Reads SampleCount number of sample points from the current position into the buffer pointed by pBuffe...
Definition: DLS.cpp:1028
void ReleaseSampleData()
Free sample data from RAM.
Definition: DLS.cpp:936
void CopyAssignCore(const Sample *orig)
Make a deep copy of the Sample object given by orig (without the actual sample waveform data however)...
Definition: DLS.cpp:861
uint16_t BlockAlign
The block alignment (in bytes) of the waveform data. Playback software needs to process a multiple of...
Definition: DLS.h:462
virtual void UpdateChunks(progress_t *pProgress)
Apply sample and its settings to the respective RIFF chunks.
Definition: DLS.cpp:1062
void Resize(file_offset_t NewSize)
Resize sample.
Definition: DLS.cpp:983
file_offset_t SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence=RIFF::stream_start)
Sets the position within the sample (in sample points, not in bytes).
Definition: DLS.cpp:1010
uint16_t BitDepth
Size of each sample per channel (only if known sample data format is used, 0 otherwise).
Definition: DLS.h:463
virtual void CopyAssign(const Sample *orig)
Make a deep copy of the Sample object given by orig and assign it to this object.
Definition: DLS.cpp:881
void * LoadSampleData()
Load sample data into RAM.
Definition: DLS.cpp:927
uint16_t Channels
Number of channels represented in the waveform data, e.g. 1 for mono, 2 for stereo (defaults to 1=mon...
Definition: DLS.h:459
file_offset_t GetSize() const
Returns sample size.
Definition: DLS.cpp:950
uint32_t SamplesPerSecond
Sampling rate at which each channel should be played (defaults to 44100 if Sample was created with In...
Definition: DLS.h:460
file_offset_t SamplesTotal
Reflects total number of sample points (only if known sample data format is used, 0 otherwise),...
Definition: DLS.h:464
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Sample object.
Definition: DLS.cpp:838
uint16_t FormatTag
Format ID of the waveform data (should be DLS_WAVE_FORMAT_PCM for DLS1 compliant files,...
Definition: DLS.h:458
uint FrameSize
Reflects the size (in bytes) of one single sample point (only if known sample data format is used,...
Definition: DLS.h:465
uint32_t AverageBytesPerSecond
The average number of bytes per second at which the waveform data should be transferred (Playback sof...
Definition: DLS.h:461
virtual ~Sample()
Destructor.
Definition: DLS.cpp:827
Sample(File *pFile, RIFF::List *waveList, file_offset_t WavePoolOffset)
Constructor.
Definition: DLS.cpp:787
file_offset_t Write(void *pBuffer, file_offset_t SampleCount)
Write sample wave data.
Definition: DLS.cpp:1048
Abstract base class which provides mandatory informations about sample players in general.
Definition: DLS.h:424
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Sampler object.
Definition: DLS.cpp:698
virtual void UpdateChunks(progress_t *pProgress)
Apply all sample player options to the respective RIFF chunk.
Definition: DLS.cpp:657
virtual void CopyAssign(const Sampler *orig)
Make a deep copy of the Sampler object given by orig and assign it to this object.
Definition: DLS.cpp:752
void AddSampleLoop(sample_loop_t *pLoopDef)
Adds a new sample loop with the provided loop definition.
Definition: DLS.cpp:706
uint32_t SampleLoops
Reflects the number of sample loops.
Definition: DLS.h:431
sample_loop_t * pSampleLoops
Points to the beginning of a sample loop array, or is NULL if there are no loops defined.
Definition: DLS.h:432
void DeleteSampleLoop(sample_loop_t *pLoopDef)
Deletes an existing sample loop.
Definition: DLS.cpp:728
int32_t Gain
Definition: DLS.h:428
Ordinary RIFF Chunk.
Definition: RIFF.h:232
void Resize(file_offset_t NewSize)
Resize chunk.
Definition: RIFF.cpp:936
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence=stream_start)
Sets the position within the chunk body, thus within the data portion of the chunk (in bytes).
Definition: RIFF.cpp:280
file_offset_t GetFilePos() const
Current, actual offset in file of current chunk data body read/write position.
Definition: RIFF.h:242
void ReleaseChunkData()
Free loaded chunk body from RAM.
Definition: RIFF.cpp:911
file_offset_t Write(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Writes WordCount number of data words with given WordSize from the buffer pointed by pData.
Definition: RIFF.cpp:444
file_offset_t RemainingBytes() const
Returns the number of bytes left to read in the chunk body.
Definition: RIFF.cpp:312
file_offset_t ReadUint32(uint32_t *pData, file_offset_t WordCount=1)
Reads WordCount number of 32 Bit unsigned integer words and copies it into the buffer pointed by pDat...
Definition: RIFF.cpp:702
file_offset_t GetPos() const
Position within the chunk data body (starting with 0).
Definition: RIFF.h:241
file_offset_t ReadUint16(uint16_t *pData, file_offset_t WordCount=1)
Reads WordCount number of 16 Bit unsigned integer words and copies it into the buffer pointed by pDat...
Definition: RIFF.cpp:628
file_offset_t Read(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Reads WordCount number of data words with given WordSize and copies it into a buffer pointed by pData...
Definition: RIFF.cpp:374
file_offset_t ReadInt16(int16_t *pData, file_offset_t WordCount=1)
Reads WordCount number of 16 Bit signed integer words and copies it into the buffer pointed by pData.
Definition: RIFF.cpp:591
uint32_t GetChunkID() const
Chunk ID in unsigned integer representation.
Definition: RIFF.h:236
void * LoadChunkData()
Load chunk body into RAM.
Definition: RIFF.cpp:865
file_offset_t ReadInt32(int32_t *pData, file_offset_t WordCount=1)
Reads WordCount number of 32 Bit signed integer words and copies it into the buffer pointed by pData.
Definition: RIFF.cpp:665
List * GetParent() const
Returns pointer to the chunk's parent list chunk.
Definition: RIFF.h:238
file_offset_t WriteUint32(uint32_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 32 Bit unsigned integer words from the buffer pointed by pData to the chun...
Definition: RIFF.cpp:740
file_offset_t GetSize() const
Chunk size in bytes (without header, thus the chunk data body)
Definition: RIFF.h:239
File * GetFile() const
Returns pointer to the chunk's File object.
Definition: RIFF.h:237
RIFF File.
Definition: RIFF.h:358
int GetFileOffsetSize() const
Returns the current size (in bytes) of file offsets stored in the headers of all chunks of this file.
Definition: RIFF.cpp:2285
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
Definition: RIFF.cpp:1910
file_offset_t GetRequiredFileSize()
Returns the required size (in bytes) for this RIFF File to be saved to disk.
Definition: RIFF.cpp:2221
virtual void Save(progress_t *pProgress=NULL)
Save changes to same file.
Definition: RIFF.cpp:1927
RIFF List Chunk.
Definition: RIFF.h:308
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
Definition: RIFF.cpp:1128
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk witin this list.
Definition: RIFF.cpp:1332
List * GetFirstSubList()
Returns the first sublist within the list (that is a subchunk with chunk ID "LIST").
Definition: RIFF.cpp:1208
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
Definition: RIFF.cpp:1380
Chunk * GetFirstSubChunk()
Returns the first subchunk within the list (which may be an ordinary chunk as well as a list chunk).
Definition: RIFF.cpp:1173
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
Definition: RIFF.cpp:1399
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
Definition: RIFF.cpp:1147
Chunk * GetNextSubChunk()
Returns the next subchunk within the list (which may be an ordinary chunk as well as a list chunk).
Definition: RIFF.cpp:1190
uint32_t GetListType() const
Returns unsigned integer representation of the list's ID.
Definition: RIFF.h:312
List * GetNextSubList()
Returns the next sublist (that is a subchunk with chunk ID "LIST") within the list.
Definition: RIFF.cpp:1230
Chunk * AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize)
Creates a new sub chunk.
Definition: RIFF.cpp:1310
DLS specific classes and definitions.
Definition: DLS.h:108
String libraryName()
Returns the name of this C++ library.
Definition: DLS.cpp:2413
String libraryVersion()
Returns version of this C++ library.
Definition: DLS.cpp:2421
conn_src_t
Connection Sources.
Definition: DLS.h:131
conn_dst_t
Connection Destinations.
Definition: DLS.h:157
conn_trn_t
Connection Transforms.
Definition: DLS.h:202
RIFF specific classes and definitions.
Definition: RIFF.h:150
stream_whence_t
File stream position dependent to these relations.
Definition: RIFF.h:177
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
Definition: RIFF.h:160
Every subject of an DLS file and the file itself can have an unique, computer generated ID.
Definition: DLS.h:123
uint16_t low
Low value of range.
Definition: DLS.h:211
uint16_t high
High value of range.
Definition: DLS.h:212
Defines Sample Loop Points.
Definition: DLS.h:235
uint32_t Size
For internal usage only: usually reflects exactly sizeof(sample_loop_t), otherwise if the value is la...
Definition: DLS.h:236
Quadtuple version number ("major.minor.release.build").
Definition: DLS.h:115
Used for indicating the progress of a certain task.
Definition: RIFF.h:216