libgig  3.3.0.svn4
DLS.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * libgig - C++ cross-platform Gigasampler format file access library *
4  * *
5  * Copyright (C) 2003-2013 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 <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"
36 
37 // macros to decode connection transforms
38 #define CONN_TRANSFORM_SRC(x) ((x >> 10) & 0x000F)
39 #define CONN_TRANSFORM_CTL(x) ((x >> 4) & 0x000F)
40 #define CONN_TRANSFORM_DST(x) (x & 0x000F)
41 #define CONN_TRANSFORM_BIPOLAR_SRC(x) (x & 0x4000)
42 #define CONN_TRANSFORM_BIPOLAR_CTL(x) (x & 0x0100)
43 #define CONN_TRANSFORM_INVERT_SRC(x) (x & 0x8000)
44 #define CONN_TRANSFORM_INVERT_CTL(x) (x & 0x0200)
45 
46 // macros to encode connection transforms
47 #define CONN_TRANSFORM_SRC_ENCODE(x) ((x & 0x000F) << 10)
48 #define CONN_TRANSFORM_CTL_ENCODE(x) ((x & 0x000F) << 4)
49 #define CONN_TRANSFORM_DST_ENCODE(x) (x & 0x000F)
50 #define CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(x) ((x) ? 0x4000 : 0)
51 #define CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(x) ((x) ? 0x0100 : 0)
52 #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0)
53 #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0)
54 
55 #define DRUM_TYPE_MASK 0x80000000
56 
57 #define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001
58 
59 #define F_WAVELINK_PHASE_MASTER 0x0001
60 #define F_WAVELINK_MULTICHANNEL 0x0002
61 
62 #define F_WSMP_NO_TRUNCATION 0x0001
63 #define F_WSMP_NO_COMPRESSION 0x0002
64 
65 #define MIDI_BANK_COARSE(x) ((x & 0x00007F00) >> 8) // CC0
66 #define MIDI_BANK_FINE(x) (x & 0x0000007F) // CC32
67 #define MIDI_BANK_MERGE(coarse, fine) ((((uint16_t) coarse) << 7) | fine) // CC0 + CC32
68 #define MIDI_BANK_ENCODE(coarse, fine) (((coarse & 0x0000007F) << 8) | (fine & 0x0000007F))
69 
70 namespace DLS {
71 
72 // *************** Connection ***************
73 // *
74 
76  Source = (conn_src_t) Header->source;
77  Control = (conn_src_t) Header->control;
79  Scale = Header->scale;
87  }
88 
90  conn_block_t c;
91  c.source = Source;
92  c.control = Control;
94  c.scale = Scale;
102  return c;
103  }
104 
105 
106 
107 // *************** Articulation ***************
108 // *
109 
119  pArticulationCk = artl;
120  if (artl->GetChunkID() != CHUNK_ID_ART2 &&
121  artl->GetChunkID() != CHUNK_ID_ARTL) {
122  throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected");
123  }
124  HeaderSize = artl->ReadUint32();
125  Connections = artl->ReadUint32();
126  artl->SetPos(HeaderSize);
127 
129  Connection::conn_block_t connblock;
130  for (uint32_t i = 0; i < Connections; i++) {
131  artl->Read(&connblock.source, 1, 2);
132  artl->Read(&connblock.control, 1, 2);
133  artl->Read(&connblock.destination, 1, 2);
134  artl->Read(&connblock.transform, 1, 2);
135  artl->Read(&connblock.scale, 1, 4);
136  pConnections[i].Init(&connblock);
137  }
138  }
139 
141  if (pConnections) delete[] pConnections;
142  }
143 
149  const int iEntrySize = 12; // 12 bytes per connection block
150  pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
151  uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
152  store16(&pData[0], HeaderSize);
153  store16(&pData[2], Connections);
154  for (uint32_t i = 0; i < Connections; i++) {
156  store16(&pData[HeaderSize + i * iEntrySize], c.source);
157  store16(&pData[HeaderSize + i * iEntrySize + 2], c.control);
158  store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination);
159  store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform);
160  store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale);
161  }
162  }
163 
164 
165 
166 // *************** Articulator ***************
167 // *
168 
170  pParentList = ParentList;
171  pArticulations = NULL;
172  }
173 
176  if (!pArticulations) return NULL;
178  return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
179  }
180 
182  if (!pArticulations) return NULL;
184  return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
185  }
186 
188  // prefer articulation level 2
190  if (!lart) lart = pParentList->GetSubList(LIST_TYPE_LART);
191  if (lart) {
192  uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2
193  : CHUNK_ID_ARTL;
194  RIFF::Chunk* art = lart->GetFirstSubChunk();
195  while (art) {
196  if (art->GetChunkID() == artCkType) {
198  pArticulations->push_back(new Articulation(art));
199  }
200  art = lart->GetNextSubChunk();
201  }
202  }
203  }
204 
206  if (pArticulations) {
207  ArticulationList::iterator iter = pArticulations->begin();
208  ArticulationList::iterator end = pArticulations->end();
209  while (iter != end) {
210  delete *iter;
211  iter++;
212  }
213  delete pArticulations;
214  }
215  }
216 
222  if (pArticulations) {
223  ArticulationList::iterator iter = pArticulations->begin();
224  ArticulationList::iterator end = pArticulations->end();
225  for (; iter != end; ++iter) {
226  (*iter)->UpdateChunks();
227  }
228  }
229  }
230 
237  //TODO: implement deep copy assignment for this class
238  }
239 
240 
241 
242 // *************** Info ***************
243 // *
244 
252  pFixedStringLengths = NULL;
253  pResourceListChunk = list;
254  if (list) {
255  RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
256  if (lstINFO) {
257  LoadString(CHUNK_ID_INAM, lstINFO, Name);
258  LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation);
259  LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate);
260  LoadString(CHUNK_ID_ICMT, lstINFO, Comments);
261  LoadString(CHUNK_ID_IPRD, lstINFO, Product);
262  LoadString(CHUNK_ID_ICOP, lstINFO, Copyright);
263  LoadString(CHUNK_ID_IART, lstINFO, Artists);
264  LoadString(CHUNK_ID_IGNR, lstINFO, Genre);
265  LoadString(CHUNK_ID_IKEY, lstINFO, Keywords);
266  LoadString(CHUNK_ID_IENG, lstINFO, Engineer);
267  LoadString(CHUNK_ID_ITCH, lstINFO, Technician);
268  LoadString(CHUNK_ID_ISFT, lstINFO, Software);
269  LoadString(CHUNK_ID_IMED, lstINFO, Medium);
270  LoadString(CHUNK_ID_ISRC, lstINFO, Source);
271  LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
272  LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
273  LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
274  }
275  }
276  }
277 
279  }
280 
293  pFixedStringLengths = lengths;
294  }
295 
301  void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
302  RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
303  ::LoadString(ck, s); // function from helper.h
304  }
305 
321  void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) {
322  int size = 0;
323  if (pFixedStringLengths) {
324  for (int i = 0 ; pFixedStringLengths[i].length ; i++) {
325  if (pFixedStringLengths[i].chunkId == ChunkID) {
326  size = pFixedStringLengths[i].length;
327  break;
328  }
329  }
330  }
331  RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
332  ::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h
333  }
334 
341  if (!pResourceListChunk) return;
342 
343  // make sure INFO list chunk exists
344  RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
345 
346  String defaultName = "";
347  String defaultCreationDate = "";
348  String defaultSoftware = "";
349  String defaultComments = "";
350 
351  uint32_t resourceType = pResourceListChunk->GetListType();
352 
353  if (!lstINFO) {
354  lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
355 
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
376 
377  SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));
378  SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));
379  SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));
380  SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);
381  SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));
382  SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);
383  SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));
384  SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));
385  SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));
386  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(""));
392  SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));
393  SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));
394  }
395 
402  void Info::CopyAssign(const Info* orig) {
403  Name = orig->Name;
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 
425 
426 // *************** Resource ***************
427 // *
428 
438  Resource::Resource(Resource* Parent, RIFF::List* lstResource) {
439  pParent = Parent;
440  pResourceList = lstResource;
441 
442  pInfo = new Info(lstResource);
443 
444  RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);
445  if (ckDLSID) {
446  pDLSID = new dlsid_t;
447  ckDLSID->Read(&pDLSID->ulData1, 1, 4);
448  ckDLSID->Read(&pDLSID->usData2, 1, 2);
449  ckDLSID->Read(&pDLSID->usData3, 1, 2);
450  ckDLSID->Read(pDLSID->abData, 8, 1);
451  }
452  else pDLSID = NULL;
453  }
454 
456  if (pDLSID) delete pDLSID;
457  if (pInfo) delete pInfo;
458  }
459 
469  pInfo->UpdateChunks();
470 
471  if (pDLSID) {
472  // make sure 'dlid' chunk exists
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 
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 
534  void Resource::CopyAssign(const Resource* orig) {
535  pInfo->CopyAssign(orig->pInfo);
536  }
537 
538 
539 // *************** Sampler ***************
540 // *
541 
543  pParentList = ParentList;
544  RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);
545  if (wsmp) {
546  uiHeaderSize = wsmp->ReadUint32();
547  UnityNote = wsmp->ReadUint16();
548  FineTune = wsmp->ReadInt16();
549  Gain = wsmp->ReadInt32();
550  SamplerOptions = wsmp->ReadUint32();
551  SampleLoops = wsmp->ReadUint32();
552  } else { // 'wsmp' chunk missing
553  uiHeaderSize = 20;
554  UnityNote = 60;
555  FineTune = 0; // +- 0 cents
556  Gain = 0; // 0 dB
558  SampleLoops = 0;
559  }
563  if (SampleLoops) {
564  wsmp->SetPos(uiHeaderSize);
565  for (uint32_t i = 0; i < SampleLoops; i++) {
566  wsmp->Read(pSampleLoops + i, 4, 4);
567  if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended
568  wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos);
569  }
570  }
571  }
572  }
573 
575  if (pSampleLoops) delete[] pSampleLoops;
576  }
577 
578  void Sampler::SetGain(int32_t gain) {
579  Gain = gain;
580  }
581 
587  // make sure 'wsmp' chunk exists
589  int wsmpSize = uiHeaderSize + SampleLoops * 16;
590  if (!wsmp) {
591  wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize);
592  } else if (wsmp->GetSize() != wsmpSize) {
593  wsmp->Resize(wsmpSize);
594  }
595  uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
596  // update headers size
597  store32(&pData[0], uiHeaderSize);
598  // update respective sampler options bits
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
609  for (uint32_t i = 0; i < SampleLoops; i++) {
610  //FIXME: this does not handle extended loop structs correctly
611  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 
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 
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 
669  void Sampler::CopyAssign(const Sampler* orig) {
670  // copy trivial scalars
671  UnityNote = orig->UnityNote;
672  FineTune = orig->FineTune;
673  Gain = orig->Gain;
677 
678  // copy sample loops
679  if (SampleLoops) delete[] pSampleLoops;
681  memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t));
682  SampleLoops = orig->SampleLoops;
683  }
684 
685 
686 // *************** Sample ***************
687 // *
688 
704  Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : Resource(pFile, waveList) {
705  pWaveList = waveList;
706  ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE;
707  pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
708  pCkData = waveList->GetSubChunk(CHUNK_ID_DATA);
709  if (pCkFormat) {
710  // common fields
716  // PCM format specific
719  FrameSize = (BitDepth / 8) * Channels;
720  } else { // unsupported sample data format
721  BitDepth = 0;
722  FrameSize = 0;
723  }
724  } else { // 'fmt' chunk missing
726  BitDepth = 16;
727  Channels = 1;
728  SamplesPerSecond = 44100;
730  FrameSize = (BitDepth / 8) * Channels;
732  }
734  : 0
735  : 0;
736  }
737 
745  pParent->DeleteSubChunk(pWaveList);
746  }
747 
775  return (pCkData) ? pCkData->LoadChunkData() : NULL;
776  }
777 
785  }
786 
797  unsigned long Sample::GetSize() {
798  if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
799  return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
800  }
801 
830  void Sample::Resize(int iNewSize) {
831  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");
833  const int iSizeInBytes = iNewSize * FrameSize;
835  if (pCkData) pCkData->Resize(iSizeInBytes);
836  else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes);
837  }
838 
855  unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {
856  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");
858  unsigned long orderedBytes = SampleCount * FrameSize;
859  unsigned long result = pCkData->SetPos(orderedBytes, Whence);
860  return (result == orderedBytes) ? SampleCount
861  : result / FrameSize;
862  }
863 
873  unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {
874  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?
876  }
877 
893  unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {
894  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");
896  return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
897  }
898 
908  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
910  if (!pCkData)
911  throw Exception("Could not save sample, there is no sample data to save");
912  // update chunks of base class as well
914  // make sure 'fmt' chunk exists
916  if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
917  uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();
918  // update 'fmt' chunk
919  store16(&pData[0], FormatTag);
920  store16(&pData[2], Channels);
921  store32(&pData[4], SamplesPerSecond);
922  store32(&pData[8], AverageBytesPerSecond);
923  store16(&pData[12], BlockAlign);
924  store16(&pData[14], BitDepth); // assuming PCM format
925  }
926 
927 
928 
929 // *************** Region ***************
930 // *
931 
932  Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) {
933  pCkRegion = rgnList;
934 
935  // articulation informations
936  RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);
937  if (rgnh) {
938  rgnh->Read(&KeyRange, 2, 2);
939  rgnh->Read(&VelocityRange, 2, 2);
940  FormatOptionFlags = rgnh->ReadUint16();
941  KeyGroup = rgnh->ReadUint16();
942  // Layer is optional
943  if (rgnh->RemainingBytes() >= sizeof(uint16_t)) {
944  rgnh->Read(&Layer, 1, sizeof(uint16_t));
945  } else Layer = 0;
946  } else { // 'rgnh' chunk is missing
947  KeyRange.low = 0;
948  KeyRange.high = 127;
949  VelocityRange.low = 0;
950  VelocityRange.high = 127;
952  KeyGroup = 0;
953  Layer = 0;
954  }
956 
957  // sample informations
958  RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);
959  if (wlnk) {
961  PhaseGroup = wlnk->ReadUint16();
962  Channel = wlnk->ReadUint32();
963  WavePoolTableIndex = wlnk->ReadUint32();
964  } else { // 'wlnk' chunk is missing
966  PhaseGroup = 0;
967  Channel = 0; // mono
968  WavePoolTableIndex = 0; // first entry in wave pool table
969  }
972 
973  pSample = NULL;
974  }
975 
982  pParent->DeleteSubChunk(pCkRegion);
983  }
984 
986  if (pSample) return pSample;
987  File* file = (File*) GetParent()->GetParent();
988  unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
989  Sample* sample = file->GetFirstSample();
990  while (sample) {
991  if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample);
992  sample = file->GetNextSample();
993  }
994  return NULL;
995  }
996 
1002  void Region::SetSample(Sample* pSample) {
1003  this->pSample = pSample;
1004  WavePoolTableIndex = 0; // we update this offset when we Save()
1005  }
1006 
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 
1049  // make sure 'rgnh' chunk exists
1051  if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
1052  uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();
1056  // update 'rgnh' chunk
1057  store16(&pData[0], KeyRange.low);
1058  store16(&pData[2], KeyRange.high);
1059  store16(&pData[4], VelocityRange.low);
1060  store16(&pData[6], VelocityRange.high);
1061  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 (but skip Resource,
1066  // as a rgn doesn't seem to have dlid and INFO chunks)
1069 
1070  // make sure 'wlnk' chunk exists
1072  if (!wlnk) wlnk = pCkRegion->AddSubChunk(CHUNK_ID_WLNK, 12);
1073  pData = (uint8_t*) wlnk->LoadChunkData();
1080  // get sample's wave pool table index
1081  int index = -1;
1082  File* pFile = (File*) GetParent()->GetParent();
1083  if (pFile->pSamples) {
1084  File::SampleList::iterator iter = pFile->pSamples->begin();
1085  File::SampleList::iterator end = pFile->pSamples->end();
1086  for (int i = 0; iter != end; ++iter, i++) {
1087  if (*iter == pSample) {
1088  index = i;
1089  break;
1090  }
1091  }
1092  }
1093  WavePoolTableIndex = index;
1094  // update 'wlnk' chunk
1095  store16(&pData[0], WaveLinkOptionFlags);
1096  store16(&pData[2], PhaseGroup);
1097  store32(&pData[4], Channel);
1098  store32(&pData[8], WavePoolTableIndex);
1099  }
1100 
1110  void Region::CopyAssign(const Region* orig) {
1111  // handle base classes
1112  Resource::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;
1121  PhaseMaster = orig->PhaseMaster;
1122  PhaseGroup = orig->PhaseGroup;
1123  MultiChannel = orig->MultiChannel;
1124  Channel = orig->Channel;
1126  pSample = orig->pSample;
1129  // handle the last, a bit sensible attribute
1130  SetKeyRange(orig->KeyRange.low, orig->KeyRange.high);
1131  }
1132 
1133 
1134 // *************** Instrument ***************
1135 // *
1136 
1150  Instrument::Instrument(File* pFile, RIFF::List* insList) : Resource(pFile, insList), Articulator(insList) {
1151  pCkInstrument = insList;
1152 
1153  midi_locale_t locale;
1155  if (insh) {
1156  Regions = insh->ReadUint32();
1157  insh->Read(&locale, 2, 4);
1158  } else { // 'insh' chunk missing
1159  Regions = 0;
1160  locale.bank = 0;
1161  locale.instrument = 0;
1162  }
1163 
1164  MIDIProgram = locale.instrument;
1165  IsDrum = locale.bank & DRUM_TYPE_MASK;
1166  MIDIBankCoarse = (uint8_t) MIDI_BANK_COARSE(locale.bank);
1167  MIDIBankFine = (uint8_t) MIDI_BANK_FINE(locale.bank);
1169 
1170  pRegions = NULL;
1171  }
1172 
1174  if (!pRegions) LoadRegions();
1175  if (!pRegions) return NULL;
1176  RegionsIterator = pRegions->begin();
1177  return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1178  }
1179 
1181  if (!pRegions) return NULL;
1182  RegionsIterator++;
1183  return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1184  }
1185 
1187  if (!pRegions) pRegions = new RegionList;
1189  if (lrgn) {
1190  uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2
1191  RIFF::List* rgn = lrgn->GetFirstSubList();
1192  while (rgn) {
1193  if (rgn->GetListType() == regionCkType) {
1194  pRegions->push_back(new Region(this, rgn));
1195  }
1196  rgn = lrgn->GetNextSubList();
1197  }
1198  }
1199  }
1200 
1202  if (!pRegions) LoadRegions();
1204  if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
1205  RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
1206  Region* pNewRegion = new Region(this, rgn);
1207  pRegions->push_back(pNewRegion);
1208  Regions = pRegions->size();
1209  return pNewRegion;
1210  }
1211 
1212  void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
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 
1222  if (!pRegions) return;
1223  RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
1224  if (iter == pRegions->end()) return;
1225  pRegions->erase(iter);
1226  Regions = pRegions->size();
1227  delete pRegion;
1228  }
1229 
1237  // first update base classes' chunks
1240  // make sure 'insh' chunk exists
1242  if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
1243  uint8_t* pData = (uint8_t*) insh->LoadChunkData();
1244  // update 'insh' chunk
1245  Regions = (pRegions) ? pRegions->size() : 0;
1246  midi_locale_t locale;
1247  locale.instrument = MIDIProgram;
1249  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
1251  store32(&pData[0], Regions);
1252  store32(&pData[4], locale.bank);
1253  store32(&pData[8], locale.instrument);
1254  // update Region's chunks
1255  if (!pRegions) return;
1256  RegionList::iterator iter = pRegions->begin();
1257  RegionList::iterator end = pRegions->end();
1258  for (; iter != end; ++iter) {
1259  (*iter)->UpdateChunks();
1260  }
1261  }
1262 
1269  if (pRegions) {
1270  RegionList::iterator iter = pRegions->begin();
1271  RegionList::iterator end = pRegions->end();
1272  while (iter != end) {
1273  delete *iter;
1274  iter++;
1275  }
1276  delete pRegions;
1277  }
1278  // remove instrument's chunks
1280  pParent->DeleteSubChunk(pCkInstrument);
1281  }
1282 
1284  // handle base classes
1285  Resource::CopyAssign(orig);
1287  // handle actual own attributes of this class
1288  // (the trivial ones)
1289  IsDrum = orig->IsDrum;
1290  MIDIBank = orig->MIDIBank;
1292  MIDIBankFine = orig->MIDIBankFine;
1293  MIDIProgram = orig->MIDIProgram;
1294  }
1295 
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 ***************
1322 // *
1323 
1330  File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1332  pVersion = new version_t;
1333  pVersion->major = 0;
1334  pVersion->minor = 0;
1335  pVersion->release = 0;
1336  pVersion->build = 0;
1337 
1338  Instruments = 0;
1339  WavePoolCount = 0;
1340  pWavePoolTable = NULL;
1341  pWavePoolTableHi = NULL;
1342  WavePoolHeaderSize = 8;
1343 
1344  pSamples = NULL;
1345  pInstruments = NULL;
1346 
1347  b64BitWavePoolOffsets = false;
1348  }
1349 
1359  File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1360  if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1361  this->pRIFF = pRIFF;
1362 
1363  RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1364  if (ckVersion) {
1365  pVersion = new version_t;
1366  ckVersion->Read(pVersion, 4, 2);
1367  }
1368  else pVersion = NULL;
1369 
1370  RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1371  if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found.");
1372  Instruments = colh->ReadUint32();
1373 
1374  RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1375  if (!ptbl) { // pool table is missing - this is probably an ".art" file
1376  WavePoolCount = 0;
1377  pWavePoolTable = NULL;
1378  pWavePoolTableHi = NULL;
1379  WavePoolHeaderSize = 8;
1380  b64BitWavePoolOffsets = false;
1381  } else {
1382  WavePoolHeaderSize = ptbl->ReadUint32();
1383  WavePoolCount = ptbl->ReadUint32();
1384  pWavePoolTable = new uint32_t[WavePoolCount];
1385  pWavePoolTableHi = new uint32_t[WavePoolCount];
1386  ptbl->SetPos(WavePoolHeaderSize);
1387 
1388  // Check for 64 bit offsets (used in gig v3 files)
1390  if (b64BitWavePoolOffsets) {
1391  for (int i = 0 ; i < WavePoolCount ; i++) {
1392  pWavePoolTableHi[i] = ptbl->ReadUint32();
1393  pWavePoolTable[i] = ptbl->ReadUint32();
1394  if (pWavePoolTable[i] & 0x80000000)
1395  throw DLS::Exception("Files larger than 2 GB not yet supported");
1396  }
1397  } else { // conventional 32 bit offsets
1398  ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
1399  for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0;
1400  }
1401  }
1402 
1403  pSamples = NULL;
1404  pInstruments = NULL;
1405  }
1406 
1408  if (pInstruments) {
1409  InstrumentList::iterator iter = pInstruments->begin();
1410  InstrumentList::iterator end = pInstruments->end();
1411  while (iter != end) {
1412  delete *iter;
1413  iter++;
1414  }
1415  delete pInstruments;
1416  }
1417 
1418  if (pSamples) {
1419  SampleList::iterator iter = pSamples->begin();
1420  SampleList::iterator end = pSamples->end();
1421  while (iter != end) {
1422  delete *iter;
1423  iter++;
1424  }
1425  delete pSamples;
1426  }
1427 
1428  if (pWavePoolTable) delete[] pWavePoolTable;
1429  if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1430  if (pVersion) delete pVersion;
1431  for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)
1432  delete *i;
1433  }
1434 
1436  if (!pSamples) LoadSamples();
1437  if (!pSamples) return NULL;
1438  SamplesIterator = pSamples->begin();
1439  return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1440  }
1441 
1443  if (!pSamples) return NULL;
1444  SamplesIterator++;
1445  return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1446  }
1447 
1449  if (!pSamples) pSamples = new SampleList;
1451  if (wvpl) {
1452  unsigned long wvplFileOffset = wvpl->GetFilePos();
1453  RIFF::List* wave = wvpl->GetFirstSubList();
1454  while (wave) {
1455  if (wave->GetListType() == LIST_TYPE_WAVE) {
1456  unsigned long waveFileOffset = wave->GetFilePos();
1457  pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1458  }
1459  wave = wvpl->GetNextSubList();
1460  }
1461  }
1462  else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant)
1464  if (dwpl) {
1465  unsigned long dwplFileOffset = dwpl->GetFilePos();
1466  RIFF::List* wave = dwpl->GetFirstSubList();
1467  while (wave) {
1468  if (wave->GetListType() == LIST_TYPE_WAVE) {
1469  unsigned long waveFileOffset = wave->GetFilePos();
1470  pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1471  }
1472  wave = dwpl->GetNextSubList();
1473  }
1474  }
1475  }
1476  }
1477 
1486  if (!pSamples) LoadSamples();
1489  // create new Sample object and its respective 'wave' list chunk
1490  RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
1491  Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
1492  pSamples->push_back(pSample);
1493  return pSample;
1494  }
1495 
1503  void File::DeleteSample(Sample* pSample) {
1504  if (!pSamples) return;
1505  SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1506  if (iter == pSamples->end()) return;
1507  pSamples->erase(iter);
1508  delete pSample;
1509  }
1510 
1512  if (!pInstruments) LoadInstruments();
1513  if (!pInstruments) return NULL;
1514  InstrumentsIterator = pInstruments->begin();
1515  return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1516  }
1517 
1519  if (!pInstruments) return NULL;
1521  return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1522  }
1523 
1526  RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1527  if (lstInstruments) {
1528  RIFF::List* lstInstr = lstInstruments->GetFirstSubList();
1529  while (lstInstr) {
1530  if (lstInstr->GetListType() == LIST_TYPE_INS) {
1531  pInstruments->push_back(new Instrument(this, lstInstr));
1532  }
1533  lstInstr = lstInstruments->GetNextSubList();
1534  }
1535  }
1536  }
1537 
1546  if (!pInstruments) LoadInstruments();
1548  RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1549  RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
1550  Instrument* pInstrument = new Instrument(this, lstInstr);
1551  pInstruments->push_back(pInstrument);
1552  return pInstrument;
1553  }
1554 
1562  void File::DeleteInstrument(Instrument* pInstrument) {
1563  if (!pInstruments) return;
1564  InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1565  if (iter == pInstruments->end()) return;
1566  pInstruments->erase(iter);
1567  delete pInstrument;
1568  }
1569 
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 
1600  return pRIFF->GetFileName();
1601  }
1602 
1611  // first update base class's chunks
1613 
1614  // if version struct exists, update 'vers' chunk
1615  if (pVersion) {
1616  RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1617  if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);
1618  uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();
1619  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
1626  Instruments = (pInstruments) ? pInstruments->size() : 0;
1628  if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1629  uint8_t* pData = (uint8_t*) colh->LoadChunkData();
1630  store32(pData, Instruments);
1631 
1632  // update instrument's chunks
1633  if (pInstruments) {
1634  InstrumentList::iterator iter = pInstruments->begin();
1635  InstrumentList::iterator end = pInstruments->end();
1636  for (; iter != end; ++iter) {
1637  (*iter)->UpdateChunks();
1638  }
1639  }
1640 
1641  // update 'ptbl' chunk
1642  const int iSamples = (pSamples) ? pSamples->size() : 0;
1643  const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1645  if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/);
1646  const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1647  ptbl->Resize(iPtblSize);
1648  pData = (uint8_t*) ptbl->LoadChunkData();
1649  WavePoolCount = iSamples;
1650  store32(&pData[4], WavePoolCount);
1651  // we actually update the sample offsets in the pool table when we Save()
1652  memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);
1653 
1654  // update sample's chunks
1655  if (pSamples) {
1656  SampleList::iterator iter = pSamples->begin();
1657  SampleList::iterator end = pSamples->end();
1658  for (; iter != end; ++iter) {
1659  (*iter)->UpdateChunks();
1660  }
1661  }
1662  }
1663 
1677  void File::Save(const String& Path) {
1678  UpdateChunks();
1679  pRIFF->Save(Path);
1680  __UpdateWavePoolTableChunk();
1681  }
1682 
1692  void File::Save() {
1693  UpdateChunks();
1694  pRIFF->Save();
1695  __UpdateWavePoolTableChunk();
1696  }
1697 
1704  // enusre 'lins' list chunk exists (mandatory for instrument definitions)
1705  RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1706  if (!lstInstruments) pRIFF->AddSubList(LIST_TYPE_LINS);
1707  // ensure 'ptbl' chunk exists (mandatory for samples)
1709  if (!ptbl) {
1710  const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1711  ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, WavePoolHeaderSize + iOffsetSize);
1712  }
1713  // enusre 'wvpl' list chunk exists (mandatory for samples)
1715  if (!wvpl) pRIFF->AddSubList(LIST_TYPE_WVPL);
1716  }
1717 
1727  void File::__UpdateWavePoolTableChunk() {
1728  __UpdateWavePoolTable();
1730  const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1731  // check if 'ptbl' chunk is large enough
1732  WavePoolCount = (pSamples) ? pSamples->size() : 0;
1733  const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
1734  if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
1735  // save the 'ptbl' chunk's current read/write position
1736  unsigned long ulOriginalPos = ptbl->GetPos();
1737  // update headers
1738  ptbl->SetPos(0);
1739  uint32_t tmp = WavePoolHeaderSize;
1740  ptbl->WriteUint32(&tmp);
1741  tmp = WavePoolCount;
1742  ptbl->WriteUint32(&tmp);
1743  // update offsets
1744  ptbl->SetPos(WavePoolHeaderSize);
1745  if (b64BitWavePoolOffsets) {
1746  for (int i = 0 ; i < WavePoolCount ; i++) {
1747  tmp = pWavePoolTableHi[i];
1748  ptbl->WriteUint32(&tmp);
1749  tmp = pWavePoolTable[i];
1750  ptbl->WriteUint32(&tmp);
1751  }
1752  } else { // conventional 32 bit offsets
1753  for (int i = 0 ; i < WavePoolCount ; i++) {
1754  tmp = pWavePoolTable[i];
1755  ptbl->WriteUint32(&tmp);
1756  }
1757  }
1758  // restore 'ptbl' chunk's original read/write position
1759  ptbl->SetPos(ulOriginalPos);
1760  }
1761 
1767  void File::__UpdateWavePoolTable() {
1768  WavePoolCount = (pSamples) ? pSamples->size() : 0;
1769  // resize wave pool table arrays
1770  if (pWavePoolTable) delete[] pWavePoolTable;
1771  if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1772  pWavePoolTable = new uint32_t[WavePoolCount];
1773  pWavePoolTableHi = new uint32_t[WavePoolCount];
1774  if (!pSamples) return;
1775  // update offsets int wave pool table
1777  uint64_t wvplFileOffset = wvpl->GetFilePos();
1778  if (b64BitWavePoolOffsets) {
1779  SampleList::iterator iter = pSamples->begin();
1780  SampleList::iterator end = pSamples->end();
1781  for (int i = 0 ; iter != end ; ++iter, i++) {
1782  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1783  (*iter)->ulWavePoolOffset = _64BitOffset;
1784  pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
1785  pWavePoolTable[i] = (uint32_t) _64BitOffset;
1786  }
1787  } else { // conventional 32 bit offsets
1788  SampleList::iterator iter = pSamples->begin();
1789  SampleList::iterator end = pSamples->end();
1790  for (int i = 0 ; iter != end ; ++iter, i++) {
1791  uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1792  (*iter)->ulWavePoolOffset = _64BitOffset;
1793  pWavePoolTable[i] = (uint32_t) _64BitOffset;
1794  }
1795  }
1796  }
1797 
1798 
1799 
1800 // *************** Exception ***************
1801 // *
1802 
1803  Exception::Exception(String Message) : RIFF::Exception(Message) {
1804  }
1805 
1807  std::cout << "DLS::Exception: " << Message << std::endl;
1808  }
1809 
1810 
1811 // *************** functions ***************
1812 // *
1813 
1820  return PACKAGE;
1821  }
1822 
1828  return VERSION;
1829  }
1830 
1831 } // namespace DLS