libgig 4.4.1
DLS.cpp
1/***************************************************************************
2 * *
3 * libgig - C++ cross-platform Gigasampler format file access library *
4 * *
5 * Copyright (C) 2003-2021 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
71namespace 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
181
182
183
184// *************** Articulator ***************
185// *
186
187 Articulator::Articulator(RIFF::List* ParentList) {
188 pParentList = ParentList;
189 pArticulations = NULL;
190 }
191
201 if (!pArticulations) LoadArticulations();
202 if (!pArticulations) return NULL;
203 if (pos >= pArticulations->size()) return NULL;
204 return (*pArticulations)[pos];
205 }
206
217 if (!pArticulations) LoadArticulations();
218 if (!pArticulations) return NULL;
219 ArticulationsIterator = pArticulations->begin();
220 return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
221 }
222
235 if (!pArticulations) return NULL;
236 ArticulationsIterator++;
237 return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
238 }
239
240 void Articulator::LoadArticulations() {
241 // prefer articulation level 2
242 RIFF::List* lart = pParentList->GetSubList(LIST_TYPE_LAR2);
243 if (!lart) lart = pParentList->GetSubList(LIST_TYPE_LART);
244 if (lart) {
245 uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2
246 : CHUNK_ID_ARTL;
247 size_t i = 0;
248 for (RIFF::Chunk* art = lart->GetSubChunkAt(i); art;
249 art = lart->GetSubChunkAt(++i))
250 {
251 if (art->GetChunkID() == artCkType) {
252 if (!pArticulations) pArticulations = new ArticulationList;
253 pArticulations->push_back(new Articulation(art));
254 }
255 }
256 }
257 }
258
259 Articulator::~Articulator() {
260 if (pArticulations) {
261 ArticulationList::iterator iter = pArticulations->begin();
262 ArticulationList::iterator end = pArticulations->end();
263 while (iter != end) {
264 delete *iter;
265 iter++;
266 }
267 delete pArticulations;
268 }
269 }
270
278 if (pArticulations) {
279 ArticulationList::iterator iter = pArticulations->begin();
280 ArticulationList::iterator end = pArticulations->end();
281 for (; iter != end; ++iter) {
282 (*iter)->UpdateChunks(pProgress);
283 }
284 }
285 }
286
292 if (pArticulations) {
293 ArticulationList::iterator iter = pArticulations->begin();
294 ArticulationList::iterator end = pArticulations->end();
295 for (; iter != end; ++iter) {
296 (*iter)->DeleteChunks();
297 }
298 }
299 }
300
307 //TODO: implement deep copy assignment for this class
308 }
309
310
311
312// *************** Info ***************
313// *
314
322 pFixedStringLengths = NULL;
323 pResourceListChunk = list;
324 if (list) {
325 RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
326 if (lstINFO) {
327 LoadString(CHUNK_ID_INAM, lstINFO, Name);
328 LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation);
329 LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate);
330 LoadString(CHUNK_ID_ICMT, lstINFO, Comments);
331 LoadString(CHUNK_ID_IPRD, lstINFO, Product);
332 LoadString(CHUNK_ID_ICOP, lstINFO, Copyright);
333 LoadString(CHUNK_ID_IART, lstINFO, Artists);
334 LoadString(CHUNK_ID_IGNR, lstINFO, Genre);
335 LoadString(CHUNK_ID_IKEY, lstINFO, Keywords);
336 LoadString(CHUNK_ID_IENG, lstINFO, Engineer);
337 LoadString(CHUNK_ID_ITCH, lstINFO, Technician);
338 LoadString(CHUNK_ID_ISFT, lstINFO, Software);
339 LoadString(CHUNK_ID_IMED, lstINFO, Medium);
340 LoadString(CHUNK_ID_ISRC, lstINFO, Source);
341 LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
342 LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
343 LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
344 }
345 }
346 }
347
348 Info::~Info() {
349 }
350
362 void Info::SetFixedStringLengths(const string_length_t* lengths) {
363 pFixedStringLengths = lengths;
364 }
365
371 void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
372 RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
373 ::LoadString(ck, s); // function from helper.h
374 }
375
391 void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) {
392 int size = 0;
393 if (pFixedStringLengths) {
394 for (int i = 0 ; pFixedStringLengths[i].length ; i++) {
395 if (pFixedStringLengths[i].chunkId == ChunkID) {
396 size = pFixedStringLengths[i].length;
397 break;
398 }
399 }
400 }
401 RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
402 ::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h
403 }
404
413 if (!pResourceListChunk) return;
414
415 // make sure INFO list chunk exists
416 RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
417
418 String defaultName = "";
419 String defaultCreationDate = "";
420 String defaultSoftware = "";
421 String defaultComments = "";
422
423 uint32_t resourceType = pResourceListChunk->GetListType();
424
425 if (!lstINFO) {
426 lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
427
428 // assemble default values
429 defaultName = "NONAME";
430
431 if (resourceType == RIFF_TYPE_DLS) {
432 // get current date
433 time_t now = time(NULL);
434 tm* pNowBroken = localtime(&now);
435 char buf[11];
436 strftime(buf, 11, "%F", pNowBroken);
437 defaultCreationDate = buf;
438
439 defaultComments = "Created with " + libraryName() + " " + libraryVersion();
440 }
441 if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS)
442 {
443 defaultSoftware = libraryName() + " " + libraryVersion();
444 }
445 }
446
447 // save values
448
449 SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));
450 SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));
451 SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));
452 SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);
453 SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));
454 SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);
455 SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));
456 SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));
457 SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));
458 SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""));
459 SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName);
460 SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""));
461 SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""));
462 SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware);
463 SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""));
464 SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));
465 SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));
466 }
467
478 }
479
486 void Info::CopyAssign(const Info* orig) {
487 Name = orig->Name;
490 Comments = orig->Comments;
491 Product = orig->Product;
492 Copyright = orig->Copyright;
493 Artists = orig->Artists;
494 Genre = orig->Genre;
495 Keywords = orig->Keywords;
496 Engineer = orig->Engineer;
497 Technician = orig->Technician;
498 Software = orig->Software;
499 Medium = orig->Medium;
500 Source = orig->Source;
501 SourceForm = orig->SourceForm;
503 Subject = orig->Subject;
504 //FIXME: hmm, is copying this pointer a good idea?
505 pFixedStringLengths = orig->pFixedStringLengths;
506 }
507
508
509
510// *************** Resource ***************
511// *
512
522 Resource::Resource(Resource* Parent, RIFF::List* lstResource) {
523 pParent = Parent;
524 pResourceList = lstResource;
525
526 pInfo = new Info(lstResource);
527
528 RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);
529 if (ckDLSID) {
530 ckDLSID->SetPos(0);
531
532 pDLSID = new dlsid_t;
533 ckDLSID->Read(&pDLSID->ulData1, 1, 4);
534 ckDLSID->Read(&pDLSID->usData2, 1, 2);
535 ckDLSID->Read(&pDLSID->usData3, 1, 2);
536 ckDLSID->Read(pDLSID->abData, 8, 1);
537 }
538 else pDLSID = NULL;
539 }
540
541 Resource::~Resource() {
542 if (pDLSID) delete pDLSID;
543 if (pInfo) delete pInfo;
544 }
545
557
569 pInfo->UpdateChunks(pProgress);
570
571 if (pDLSID) {
572 // make sure 'dlid' chunk exists
573 RIFF::Chunk* ckDLSID = pResourceList->GetSubChunk(CHUNK_ID_DLID);
574 if (!ckDLSID) ckDLSID = pResourceList->AddSubChunk(CHUNK_ID_DLID, 16);
575 uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
576 // update 'dlid' chunk
577 store32(&pData[0], pDLSID->ulData1);
578 store16(&pData[4], pDLSID->usData2);
579 store16(&pData[6], pDLSID->usData3);
580 memcpy(&pData[8], pDLSID->abData, 8);
581 }
582 }
583
588 #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
589 if (!pDLSID) pDLSID = new dlsid_t;
591 #endif
592 }
593
594 void Resource::GenerateDLSID(dlsid_t* pDLSID) {
595#ifdef WIN32
596 UUID uuid;
597 UuidCreate(&uuid);
598 pDLSID->ulData1 = uuid.Data1;
599 pDLSID->usData2 = uuid.Data2;
600 pDLSID->usData3 = uuid.Data3;
601 memcpy(pDLSID->abData, uuid.Data4, 8);
602
603#elif defined(__APPLE__)
604
605 CFUUIDRef uuidRef = CFUUIDCreate(NULL);
606 CFUUIDBytes uuid = CFUUIDGetUUIDBytes(uuidRef);
607 CFRelease(uuidRef);
608 pDLSID->ulData1 = uuid.byte0 | uuid.byte1 << 8 | uuid.byte2 << 16 | uuid.byte3 << 24;
609 pDLSID->usData2 = uuid.byte4 | uuid.byte5 << 8;
610 pDLSID->usData3 = uuid.byte6 | uuid.byte7 << 8;
611 pDLSID->abData[0] = uuid.byte8;
612 pDLSID->abData[1] = uuid.byte9;
613 pDLSID->abData[2] = uuid.byte10;
614 pDLSID->abData[3] = uuid.byte11;
615 pDLSID->abData[4] = uuid.byte12;
616 pDLSID->abData[5] = uuid.byte13;
617 pDLSID->abData[6] = uuid.byte14;
618 pDLSID->abData[7] = uuid.byte15;
619#elif defined(HAVE_UUID_GENERATE)
620 uuid_t uuid;
621 uuid_generate(uuid);
622 pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24;
623 pDLSID->usData2 = uuid[4] | uuid[5] << 8;
624 pDLSID->usData3 = uuid[6] | uuid[7] << 8;
625 memcpy(pDLSID->abData, &uuid[8], 8);
626#else
627# error "Missing support for uuid generation"
628#endif
629 }
630
637 void Resource::CopyAssign(const Resource* orig) {
638 pInfo->CopyAssign(orig->pInfo);
639 }
640
641
642// *************** Sampler ***************
643// *
644
645 Sampler::Sampler(RIFF::List* ParentList) {
646 pParentList = ParentList;
647 RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);
648 if (wsmp) {
649 wsmp->SetPos(0);
650
651 uiHeaderSize = wsmp->ReadUint32();
652 UnityNote = wsmp->ReadUint16();
653 FineTune = wsmp->ReadInt16();
654 Gain = wsmp->ReadInt32();
655 SamplerOptions = wsmp->ReadUint32();
656 SampleLoops = wsmp->ReadUint32();
657 } else { // 'wsmp' chunk missing
658 uiHeaderSize = 20;
659 UnityNote = 60;
660 FineTune = 0; // +- 0 cents
661 Gain = 0; // 0 dB
662 SamplerOptions = F_WSMP_NO_COMPRESSION;
663 SampleLoops = 0;
664 }
665 NoSampleDepthTruncation = SamplerOptions & F_WSMP_NO_TRUNCATION;
666 NoSampleCompression = SamplerOptions & F_WSMP_NO_COMPRESSION;
667 pSampleLoops = (SampleLoops) ? new sample_loop_t[SampleLoops] : NULL;
668 if (SampleLoops) {
669 wsmp->SetPos(uiHeaderSize);
670 for (uint32_t i = 0; i < SampleLoops; i++) {
671 wsmp->Read(pSampleLoops + i, 4, 4);
672 if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended
673 wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos);
674 }
675 }
676 }
677 }
678
679 Sampler::~Sampler() {
680 if (pSampleLoops) delete[] pSampleLoops;
681 }
682
683 void Sampler::SetGain(int32_t gain) {
684 Gain = gain;
685 }
686
694 // make sure 'wsmp' chunk exists
695 RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
696 int wsmpSize = uiHeaderSize + SampleLoops * 16;
697 if (!wsmp) {
698 wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize);
699 } else if (wsmp->GetSize() != wsmpSize) {
700 wsmp->Resize(wsmpSize);
701 }
702 uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
703 // update headers size
704 store32(&pData[0], uiHeaderSize);
705 // update respective sampler options bits
706 SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION
707 : SamplerOptions & (~F_WSMP_NO_TRUNCATION);
708 SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION
709 : SamplerOptions & (~F_WSMP_NO_COMPRESSION);
710 store16(&pData[4], UnityNote);
711 store16(&pData[6], FineTune);
712 store32(&pData[8], Gain);
713 store32(&pData[12], SamplerOptions);
714 store32(&pData[16], SampleLoops);
715 // update loop definitions
716 for (uint32_t i = 0; i < SampleLoops; i++) {
717 //FIXME: this does not handle extended loop structs correctly
718 store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size);
719 store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType);
720 store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart);
721 store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength);
722 }
723 }
724
735 }
736
743 sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1];
744 // copy old loops array
745 for (int i = 0; i < SampleLoops; i++) {
746 pNewLoops[i] = pSampleLoops[i];
747 }
748 // add the new loop
749 pNewLoops[SampleLoops] = *pLoopDef;
750 // auto correct size field
751 pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t);
752 // free the old array and update the member variables
753 if (SampleLoops) delete[] pSampleLoops;
754 pSampleLoops = pNewLoops;
755 SampleLoops++;
756 }
757
765 sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1];
766 // copy old loops array (skipping given loop)
767 for (int i = 0, o = 0; i < SampleLoops; i++) {
768 if (&pSampleLoops[i] == pLoopDef) continue;
769 if (o == SampleLoops - 1) {
770 delete[] pNewLoops;
771 throw Exception("Could not delete Sample Loop, because it does not exist");
772 }
773 pNewLoops[o] = pSampleLoops[i];
774 o++;
775 }
776 // free the old array and update the member variables
777 if (SampleLoops) delete[] pSampleLoops;
778 pSampleLoops = pNewLoops;
779 SampleLoops--;
780 }
781
788 void Sampler::CopyAssign(const Sampler* orig) {
789 // copy trivial scalars
790 UnityNote = orig->UnityNote;
791 FineTune = orig->FineTune;
792 Gain = orig->Gain;
793 NoSampleDepthTruncation = orig->NoSampleDepthTruncation;
794 NoSampleCompression = orig->NoSampleCompression;
795 SamplerOptions = orig->SamplerOptions;
796
797 // copy sample loops
798 if (SampleLoops) delete[] pSampleLoops;
800 memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t));
801 SampleLoops = orig->SampleLoops;
802 }
803
804
805// *************** Sample ***************
806// *
807
823 Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset) : Resource(pFile, waveList) {
824 pWaveList = waveList;
825 ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize());
826 pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
827 pCkData = waveList->GetSubChunk(CHUNK_ID_DATA);
828 if (pCkFormat) {
829 pCkFormat->SetPos(0);
830
831 // common fields
832 FormatTag = pCkFormat->ReadUint16();
833 Channels = pCkFormat->ReadUint16();
834 SamplesPerSecond = pCkFormat->ReadUint32();
835 AverageBytesPerSecond = pCkFormat->ReadUint32();
836 BlockAlign = pCkFormat->ReadUint16();
837 // PCM format specific
838 if (FormatTag == DLS_WAVE_FORMAT_PCM) {
839 BitDepth = pCkFormat->ReadUint16();
840 FrameSize = (BitDepth / 8) * Channels;
841 } else { // unsupported sample data format
842 BitDepth = 0;
843 FrameSize = 0;
844 }
845 } else { // 'fmt' chunk missing
846 FormatTag = DLS_WAVE_FORMAT_PCM;
847 BitDepth = 16;
848 Channels = 1;
849 SamplesPerSecond = 44100;
851 FrameSize = (BitDepth / 8) * Channels;
853 }
854 SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize
855 : 0
856 : 0;
857 }
858
864 if (pCkData)
865 pCkData->ReleaseChunkData();
866 if (pCkFormat)
867 pCkFormat->ReleaseChunkData();
868 }
869
875 // handle base class
877
878 // handle own RIFF chunks
879 if (pWaveList) {
880 RIFF::List* pParent = pWaveList->GetParent();
881 pParent->DeleteSubChunk(pWaveList);
882 pWaveList = NULL;
883 }
884 }
885
897 void Sample::CopyAssignCore(const Sample* orig) {
898 // handle base classes
900 // handle actual own attributes of this class
901 FormatTag = orig->FormatTag;
902 Channels = orig->Channels;
905 BlockAlign = orig->BlockAlign;
906 BitDepth = orig->BitDepth;
908 FrameSize = orig->FrameSize;
909 }
910
917 void Sample::CopyAssign(const Sample* orig) {
918 CopyAssignCore(orig);
919
920 // copy sample waveform data (reading directly from disc)
921 Resize(orig->GetSize());
922 char* buf = (char*) LoadSampleData();
923 Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now
924 const file_offset_t restorePos = pOrig->pCkData->GetPos();
925 pOrig->SetPos(0);
926 for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) {
927 const int iReadAtOnce = 64*1024;
928 file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo;
929 n = pOrig->Read(&buf[i], n);
930 if (!n) break;
931 todo -= n;
932 i += (n * pOrig->FrameSize);
933 }
934 pOrig->pCkData->SetPos(restorePos);
935 }
936
964 return (pCkData) ? pCkData->LoadChunkData() : NULL;
965 }
966
973 if (pCkData) pCkData->ReleaseChunkData();
974 }
975
986 file_offset_t Sample::GetSize() const {
987 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
988 return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
989 }
990
1019 void Sample::Resize(file_offset_t NewSize) {
1020 if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");
1021 if (NewSize < 1) throw Exception("Sample size must be at least one sample point");
1022 if ((NewSize >> 48) != 0)
1023 throw Exception("Unrealistic high DLS sample size detected");
1024 const file_offset_t sizeInBytes = NewSize * FrameSize;
1025 pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
1026 if (pCkData) pCkData->Resize(sizeInBytes);
1027 else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes);
1028 }
1029
1046 file_offset_t Sample::SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence) {
1047 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
1048 if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one");
1049 file_offset_t orderedBytes = SampleCount * FrameSize;
1050 file_offset_t result = pCkData->SetPos(orderedBytes, Whence);
1051 return (result == orderedBytes) ? SampleCount
1052 : result / FrameSize;
1053 }
1054
1064 file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) {
1065 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
1066 return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
1067 }
1068
1084 file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) {
1085 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
1086 if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");
1087 return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
1088 }
1089
1099 if (FormatTag != DLS_WAVE_FORMAT_PCM)
1100 throw Exception("Could not save sample, only PCM format is supported");
1101 // we refuse to do anything if not sample wave form was provided yet
1102 if (!pCkData)
1103 throw Exception("Could not save sample, there is no sample data to save");
1104 // update chunks of base class as well
1105 Resource::UpdateChunks(pProgress);
1106 // make sure 'fmt' chunk exists
1107 RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT);
1108 if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
1109 uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();
1110 // update 'fmt' chunk
1111 store16(&pData[0], FormatTag);
1112 store16(&pData[2], Channels);
1113 store32(&pData[4], SamplesPerSecond);
1114 store32(&pData[8], AverageBytesPerSecond);
1115 store16(&pData[12], BlockAlign);
1116 store16(&pData[14], BitDepth); // assuming PCM format
1117 }
1118
1119
1120
1121// *************** Region ***************
1122// *
1123
1124 Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) {
1125 pCkRegion = rgnList;
1126
1127 // articulation information
1128 RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);
1129 if (rgnh) {
1130 rgnh->SetPos(0);
1131
1132 rgnh->Read(&KeyRange, 2, 2);
1133 rgnh->Read(&VelocityRange, 2, 2);
1134 FormatOptionFlags = rgnh->ReadUint16();
1135 KeyGroup = rgnh->ReadUint16();
1136 // Layer is optional
1137 if (rgnh->RemainingBytes() >= sizeof(uint16_t)) {
1138 rgnh->Read(&Layer, 1, sizeof(uint16_t));
1139 } else Layer = 0;
1140 } else { // 'rgnh' chunk is missing
1141 KeyRange.low = 0;
1142 KeyRange.high = 127;
1143 VelocityRange.low = 0;
1144 VelocityRange.high = 127;
1145 FormatOptionFlags = F_RGN_OPTION_SELFNONEXCLUSIVE;
1146 KeyGroup = 0;
1147 Layer = 0;
1148 }
1149 SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;
1150
1151 // sample information
1152 RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);
1153 if (wlnk) {
1154 wlnk->SetPos(0);
1155
1156 WaveLinkOptionFlags = wlnk->ReadUint16();
1157 PhaseGroup = wlnk->ReadUint16();
1158 Channel = wlnk->ReadUint32();
1159 WavePoolTableIndex = wlnk->ReadUint32();
1160 } else { // 'wlnk' chunk is missing
1161 WaveLinkOptionFlags = 0;
1162 PhaseGroup = 0;
1163 Channel = 0; // mono
1164 WavePoolTableIndex = 0; // first entry in wave pool table
1165 }
1166 PhaseMaster = WaveLinkOptionFlags & F_WAVELINK_PHASE_MASTER;
1167 MultiChannel = WaveLinkOptionFlags & F_WAVELINK_MULTICHANNEL;
1168
1169 pSample = NULL;
1170 }
1171
1179
1185 // handle base classes
1189
1190 // handle own RIFF chunks
1191 if (pCkRegion) {
1192 RIFF::List* pParent = pCkRegion->GetParent();
1193 pParent->DeleteSubChunk(pCkRegion);
1194 pCkRegion = NULL;
1195 }
1196 }
1197
1198 Sample* Region::GetSample() {
1199 if (pSample) return pSample;
1200 File* file = (File*) GetParent()->GetParent();
1201 uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
1202 size_t i = 0;
1203 for (Sample* sample = file->GetSample(i); sample;
1204 sample = file->GetSample(++i))
1205 {
1206 if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample);
1207 }
1208 return NULL;
1209 }
1210
1216 void Region::SetSample(Sample* pSample) {
1217 this->pSample = pSample;
1218 WavePoolTableIndex = 0; // we update this offset when we Save()
1219 }
1220
1228 void Region::SetKeyRange(uint16_t Low, uint16_t High) {
1229 KeyRange.low = Low;
1230 KeyRange.high = High;
1231
1232 // make sure regions are already loaded
1233 Instrument* pInstrument = (Instrument*) GetParent();
1234 if (!pInstrument->pRegions) pInstrument->LoadRegions();
1235 if (!pInstrument->pRegions) return;
1236
1237 // find the r which is the first one to the right of this region
1238 // at its new position
1239 Region* r = NULL;
1240 Region* prev_region = NULL;
1241 for (
1242 Instrument::RegionList::iterator iter = pInstrument->pRegions->begin();
1243 iter != pInstrument->pRegions->end(); iter++
1244 ) {
1245 if ((*iter)->KeyRange.low > this->KeyRange.low) {
1246 r = *iter;
1247 break;
1248 }
1249 prev_region = *iter;
1250 }
1251
1252 // place this region before r if it's not already there
1253 if (prev_region != this) pInstrument->MoveRegion(this, r);
1254 }
1255
1264 // make sure 'rgnh' chunk exists
1265 RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);
1266 if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
1267 uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();
1268 FormatOptionFlags = (SelfNonExclusive)
1269 ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE
1270 : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);
1271 // update 'rgnh' chunk
1272 store16(&pData[0], KeyRange.low);
1273 store16(&pData[2], KeyRange.high);
1274 store16(&pData[4], VelocityRange.low);
1275 store16(&pData[6], VelocityRange.high);
1276 store16(&pData[8], FormatOptionFlags);
1277 store16(&pData[10], KeyGroup);
1278 if (rgnh->GetSize() >= 14) store16(&pData[12], Layer);
1279
1280 // update chunks of base classes as well (but skip Resource,
1281 // as a rgn doesn't seem to have dlid and INFO chunks)
1282 Articulator::UpdateChunks(pProgress);
1283 Sampler::UpdateChunks(pProgress);
1284
1285 // make sure 'wlnk' chunk exists
1286 RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK);
1287 if (!wlnk) wlnk = pCkRegion->AddSubChunk(CHUNK_ID_WLNK, 12);
1288 pData = (uint8_t*) wlnk->LoadChunkData();
1289 WaveLinkOptionFlags = (PhaseMaster)
1290 ? WaveLinkOptionFlags | F_WAVELINK_PHASE_MASTER
1291 : WaveLinkOptionFlags & (~F_WAVELINK_PHASE_MASTER);
1292 WaveLinkOptionFlags = (MultiChannel)
1293 ? WaveLinkOptionFlags | F_WAVELINK_MULTICHANNEL
1294 : WaveLinkOptionFlags & (~F_WAVELINK_MULTICHANNEL);
1295 // get sample's wave pool table index
1296 int index = -1;
1297 File* pFile = (File*) GetParent()->GetParent();
1298 if (pFile->pSamples) {
1299 File::SampleList::iterator iter = pFile->pSamples->begin();
1300 File::SampleList::iterator end = pFile->pSamples->end();
1301 for (int i = 0; iter != end; ++iter, i++) {
1302 if (*iter == pSample) {
1303 index = i;
1304 break;
1305 }
1306 }
1307 }
1308 WavePoolTableIndex = index;
1309 // update 'wlnk' chunk
1310 store16(&pData[0], WaveLinkOptionFlags);
1311 store16(&pData[2], PhaseGroup);
1312 store32(&pData[4], Channel);
1313 store32(&pData[8], WavePoolTableIndex);
1314 }
1315
1325 void Region::CopyAssign(const Region* orig) {
1326 // handle base classes
1329 Sampler::CopyAssign(orig);
1330 // handle actual own attributes of this class
1331 // (the trivial ones)
1332 VelocityRange = orig->VelocityRange;
1333 KeyGroup = orig->KeyGroup;
1334 Layer = orig->Layer;
1335 SelfNonExclusive = orig->SelfNonExclusive;
1336 PhaseMaster = orig->PhaseMaster;
1337 PhaseGroup = orig->PhaseGroup;
1338 MultiChannel = orig->MultiChannel;
1339 Channel = orig->Channel;
1340 // only take the raw sample reference if the two Region objects are
1341 // part of the same file
1342 if (GetParent()->GetParent() == orig->GetParent()->GetParent()) {
1343 WavePoolTableIndex = orig->WavePoolTableIndex;
1344 pSample = orig->pSample;
1345 } else {
1346 WavePoolTableIndex = -1;
1347 pSample = NULL;
1348 }
1349 FormatOptionFlags = orig->FormatOptionFlags;
1350 WaveLinkOptionFlags = orig->WaveLinkOptionFlags;
1351 // handle the last, a bit sensible attribute
1352 SetKeyRange(orig->KeyRange.low, orig->KeyRange.high);
1353 }
1354
1355
1356// *************** Instrument ***************
1357// *
1358
1372 Instrument::Instrument(File* pFile, RIFF::List* insList) : Resource(pFile, insList), Articulator(insList) {
1373 pCkInstrument = insList;
1374
1375 midi_locale_t locale;
1376 RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1377 if (insh) {
1378 insh->SetPos(0);
1379
1380 Regions = insh->ReadUint32();
1381 insh->Read(&locale, 2, 4);
1382 } else { // 'insh' chunk missing
1383 Regions = 0;
1384 locale.bank = 0;
1385 locale.instrument = 0;
1386 }
1387
1388 MIDIProgram = locale.instrument;
1389 IsDrum = locale.bank & DRUM_TYPE_MASK;
1390 MIDIBankCoarse = (uint8_t) MIDI_BANK_COARSE(locale.bank);
1391 MIDIBankFine = (uint8_t) MIDI_BANK_FINE(locale.bank);
1392 MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine);
1393
1394 pRegions = NULL;
1395 }
1396
1403 if (!pRegions) LoadRegions();
1404 if (!pRegions) return 0;
1405 return pRegions->size();
1406 }
1407
1419 if (!pRegions) LoadRegions();
1420 if (!pRegions) return NULL;
1421 if (pos >= pRegions->size()) return NULL;
1422 return (*pRegions)[pos];
1423 }
1424
1435 if (!pRegions) LoadRegions();
1436 if (!pRegions) return NULL;
1437 RegionsIterator = pRegions->begin();
1438 return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1439 }
1440
1452 if (!pRegions) return NULL;
1453 RegionsIterator++;
1454 return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
1455 }
1456
1457 void Instrument::LoadRegions() {
1458 if (!pRegions) pRegions = new RegionList;
1459 RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1460 if (lrgn) {
1461 uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2
1462 size_t i = 0;
1463 for (RIFF::List* rgn = lrgn->GetSubListAt(i); rgn;
1464 rgn = lrgn->GetSubListAt(++i))
1465 {
1466 if (rgn->GetListType() == regionCkType) {
1467 pRegions->push_back(new Region(this, rgn));
1468 }
1469 }
1470 }
1471 }
1472
1473 Region* Instrument::AddRegion() {
1474 if (!pRegions) LoadRegions();
1475 RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1476 if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
1477 RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
1478 Region* pNewRegion = new Region(this, rgn);
1479 const size_t idxIt = RegionsIterator - pRegions->begin();
1480 pRegions->push_back(pNewRegion);
1481 RegionsIterator = pRegions->begin() + std::min(idxIt, pRegions->size()); // avoid iterator invalidation
1482 Regions = (uint32_t) pRegions->size();
1483 return pNewRegion;
1484 }
1485
1486 void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
1487 RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
1488 lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0));
1489 for (size_t i = 0; i < pRegions->size(); ++i) {
1490 if ((*pRegions)[i] == pSrc) {
1491 const size_t idxIt = RegionsIterator - pRegions->begin();
1492 pRegions->erase(pRegions->begin() + i);
1493 RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
1494 pRegions->insert(iter, pSrc);
1495 RegionsIterator = pRegions->begin() + std::min(idxIt, pRegions->size()); // avoid iterator invalidation
1496 }
1497 }
1498 }
1499
1500 void Instrument::DeleteRegion(Region* pRegion) {
1501 if (!pRegions) return;
1502 RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
1503 if (iter == pRegions->end()) return;
1504 const size_t idxIt = RegionsIterator - pRegions->begin();
1505 pRegions->erase(iter);
1506 RegionsIterator = pRegions->begin() + std::min(idxIt, pRegions->size()); // avoid iterator invalidation
1507 Regions = (uint32_t) pRegions->size();
1508 pRegion->DeleteChunks();
1509 delete pRegion;
1510 }
1511
1520 // first update base classes' chunks
1521 Resource::UpdateChunks(pProgress);
1522 Articulator::UpdateChunks(pProgress);
1523 // make sure 'insh' chunk exists
1524 RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1525 if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
1526 uint8_t* pData = (uint8_t*) insh->LoadChunkData();
1527 // update 'insh' chunk
1528 Regions = (pRegions) ? uint32_t(pRegions->size()) : 0;
1529 midi_locale_t locale;
1530 locale.instrument = MIDIProgram;
1531 locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
1532 locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK);
1533 MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it
1534 store32(&pData[0], Regions);
1535 store32(&pData[4], locale.bank);
1536 store32(&pData[8], locale.instrument);
1537 // update Region's chunks
1538 if (!pRegions) return;
1539 RegionList::iterator iter = pRegions->begin();
1540 RegionList::iterator end = pRegions->end();
1541 for (int i = 0; iter != end; ++iter, ++i) {
1542 if (pProgress) {
1543 // divide local progress into subprogress
1544 progress_t subprogress;
1545 __divide_progress(pProgress, &subprogress, pRegions->size(), i);
1546 // do the actual work
1547 (*iter)->UpdateChunks(&subprogress);
1548 } else
1549 (*iter)->UpdateChunks(NULL);
1550 }
1551 if (pProgress)
1552 __notify_progress(pProgress, 1.0); // notify done
1553 }
1554
1560 if (pRegions) {
1561 RegionList::iterator iter = pRegions->begin();
1562 RegionList::iterator end = pRegions->end();
1563 while (iter != end) {
1564 delete *iter;
1565 iter++;
1566 }
1567 delete pRegions;
1568 }
1569 }
1570
1576 // handle base classes
1579
1580 // handle RIFF chunks of members
1581 if (pRegions) {
1582 RegionList::iterator it = pRegions->begin();
1583 RegionList::iterator end = pRegions->end();
1584 for (; it != end; ++it)
1585 (*it)->DeleteChunks();
1586 }
1587
1588 // handle own RIFF chunks
1589 if (pCkInstrument) {
1590 RIFF::List* pParent = pCkInstrument->GetParent();
1591 pParent->DeleteSubChunk(pCkInstrument);
1592 pCkInstrument = NULL;
1593 }
1594 }
1595
1596 void Instrument::CopyAssignCore(const Instrument* orig) {
1597 // handle base classes
1600 // handle actual own attributes of this class
1601 // (the trivial ones)
1602 IsDrum = orig->IsDrum;
1603 MIDIBank = orig->MIDIBank;
1605 MIDIBankFine = orig->MIDIBankFine;
1606 MIDIProgram = orig->MIDIProgram;
1607 }
1608
1619 CopyAssignCore(orig);
1620 // delete all regions first
1621 while (Regions) DeleteRegion(GetRegionAt(0));
1622 // now recreate and copy regions
1623 {
1624 RegionList::const_iterator it = orig->pRegions->begin();
1625 for (int i = 0; i < orig->Regions; ++i, ++it) {
1626 Region* dstRgn = AddRegion();
1627 //NOTE: Region does semi-deep copy !
1628 dstRgn->CopyAssign(*it);
1629 }
1630 }
1631 }
1632
1633
1634// *************** File ***************
1635// *
1636
1643 File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1644 pRIFF->SetByteOrder(RIFF::endian_little);
1645 bOwningRiff = true;
1646 pVersion = new version_t;
1647 pVersion->major = 0;
1648 pVersion->minor = 0;
1649 pVersion->release = 0;
1650 pVersion->build = 0;
1651
1652 Instruments = 0;
1653 WavePoolCount = 0;
1654 pWavePoolTable = NULL;
1655 pWavePoolTableHi = NULL;
1656 WavePoolHeaderSize = 8;
1657
1658 pSamples = NULL;
1659 pInstruments = NULL;
1660
1661 b64BitWavePoolOffsets = false;
1662 }
1663
1673 File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1674 if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1675 this->pRIFF = pRIFF;
1676 bOwningRiff = false;
1677 RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1678 if (ckVersion) {
1679 ckVersion->SetPos(0);
1680
1681 pVersion = new version_t;
1682 ckVersion->Read(pVersion, 4, 2);
1683 }
1684 else pVersion = NULL;
1685
1686 RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1687 if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found.");
1688 colh->SetPos(0);
1689 Instruments = colh->ReadUint32();
1690
1691 RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1692 if (!ptbl) { // pool table is missing - this is probably an ".art" file
1693 WavePoolCount = 0;
1694 pWavePoolTable = NULL;
1695 pWavePoolTableHi = NULL;
1696 WavePoolHeaderSize = 8;
1697 b64BitWavePoolOffsets = false;
1698 } else {
1699 ptbl->SetPos(0);
1700
1701 WavePoolHeaderSize = ptbl->ReadUint32();
1702 WavePoolCount = ptbl->ReadUint32();
1703 pWavePoolTable = new uint32_t[WavePoolCount];
1704 pWavePoolTableHi = new uint32_t[WavePoolCount];
1705 ptbl->SetPos(WavePoolHeaderSize);
1706
1707 // Check for 64 bit offsets (used in gig v3 files)
1708 b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8);
1709 if (b64BitWavePoolOffsets) {
1710 for (int i = 0 ; i < WavePoolCount ; i++) {
1711 pWavePoolTableHi[i] = ptbl->ReadUint32();
1712 pWavePoolTable[i] = ptbl->ReadUint32();
1713 //NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12)
1714 //if (pWavePoolTable[i] & 0x80000000)
1715 // throw DLS::Exception("Files larger than 2 GB not yet supported");
1716 }
1717 } else { // conventional 32 bit offsets
1718 ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
1719 for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0;
1720 }
1721 }
1722
1723 pSamples = NULL;
1724 pInstruments = NULL;
1725 }
1726
1727 File::~File() {
1728 if (pInstruments) {
1729 InstrumentList::iterator iter = pInstruments->begin();
1730 InstrumentList::iterator end = pInstruments->end();
1731 while (iter != end) {
1732 delete *iter;
1733 iter++;
1734 }
1735 delete pInstruments;
1736 }
1737
1738 if (pSamples) {
1739 SampleList::iterator iter = pSamples->begin();
1740 SampleList::iterator end = pSamples->end();
1741 while (iter != end) {
1742 delete *iter;
1743 iter++;
1744 }
1745 delete pSamples;
1746 }
1747
1748 if (pWavePoolTable) delete[] pWavePoolTable;
1749 if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1750 if (pVersion) delete pVersion;
1751 for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)
1752 delete *i;
1753 if (bOwningRiff)
1754 delete pRIFF;
1755 }
1756
1763 Sample* File::GetSample(size_t index) {
1764 if (!pSamples) LoadSamples();
1765 if (!pSamples) return NULL;
1766 if (index >= pSamples->size()) return NULL;
1767 return (*pSamples)[index];
1768 }
1769
1778 if (!pSamples) LoadSamples();
1779 if (!pSamples) return NULL;
1780 SamplesIterator = pSamples->begin();
1781 return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1782 }
1783
1792 if (!pSamples) return NULL;
1793 SamplesIterator++;
1794 return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1795 }
1796
1797 void File::LoadSamples() {
1798 if (!pSamples) pSamples = new SampleList;
1799 RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1800 if (wvpl) {
1801 file_offset_t wvplFileOffset = wvpl->GetFilePos() -
1802 wvpl->GetPos(); // should be zero, but just to be sure
1803 size_t i = 0;
1804 for (RIFF::List* wave = wvpl->GetSubListAt(i); wave;
1805 wave = wvpl->GetSubListAt(++i))
1806 {
1807 if (wave->GetListType() == LIST_TYPE_WAVE) {
1808 file_offset_t waveFileOffset = wave->GetFilePos() -
1809 wave->GetPos(); // should be zero, but just to be sure
1810 pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1811 }
1812 }
1813 }
1814 else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant)
1815 RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1816 if (dwpl) {
1817 file_offset_t dwplFileOffset = dwpl->GetFilePos() -
1818 dwpl->GetPos(); // should be zero, but just to be sure
1819 size_t i = 0;
1820 for (RIFF::List* wave = dwpl->GetSubListAt(i); wave;
1821 wave = dwpl->GetSubListAt(++i))
1822 {
1823 if (wave->GetListType() == LIST_TYPE_WAVE) {
1824 file_offset_t waveFileOffset = wave->GetFilePos() -
1825 wave->GetPos(); // should be zero, but just to be sure
1826 pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1827 }
1828 }
1829 }
1830 }
1831 }
1832
1841 if (!pSamples) LoadSamples();
1843 RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1844 // create new Sample object and its respective 'wave' list chunk
1845 RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
1846 Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
1847 const size_t idxIt = SamplesIterator - pSamples->begin();
1848 pSamples->push_back(pSample);
1849 SamplesIterator = pSamples->begin() + std::min(idxIt, pSamples->size()); // avoid iterator invalidation
1850 return pSample;
1851 }
1852
1861 if (!pSamples) return;
1862 SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1863 if (iter == pSamples->end()) return;
1864 const size_t idxIt = SamplesIterator - pSamples->begin();
1865 pSamples->erase(iter);
1866 SamplesIterator = pSamples->begin() + std::min(idxIt, pSamples->size()); // avoid iterator invalidation
1867 pSample->DeleteChunks();
1868 delete pSample;
1869 }
1870
1879 if (!pInstruments) LoadInstruments();
1880 if (!pInstruments) return NULL;
1881 if (index >= pInstruments->size()) return NULL;
1882 return (*pInstruments)[index];
1883 }
1884
1893 if (!pInstruments) LoadInstruments();
1894 if (!pInstruments) return NULL;
1895 InstrumentsIterator = pInstruments->begin();
1896 return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1897 }
1898
1907 if (!pInstruments) return NULL;
1908 InstrumentsIterator++;
1909 return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1910 }
1911
1912 void File::LoadInstruments() {
1913 if (!pInstruments) pInstruments = new InstrumentList;
1914 RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1915 if (lstInstruments) {
1916 size_t i = 0;
1917 for (RIFF::List* lstInstr = lstInstruments->GetSubListAt(i);
1918 lstInstr; lstInstr = lstInstruments->GetSubListAt(++i))
1919 {
1920 if (lstInstr->GetListType() == LIST_TYPE_INS) {
1921 pInstruments->push_back(new Instrument(this, lstInstr));
1922 }
1923 }
1924 }
1925 }
1926
1935 if (!pInstruments) LoadInstruments();
1937 RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1938 RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
1939 Instrument* pInstrument = new Instrument(this, lstInstr);
1940 const size_t idxIt = InstrumentsIterator - pInstruments->begin();
1941 pInstruments->push_back(pInstrument);
1942 InstrumentsIterator = pInstruments->begin() + std::min(idxIt, pInstruments->size()); // avoid iterator invalidation
1943 return pInstrument;
1944 }
1945
1954 if (!pInstruments) return;
1955 InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1956 if (iter == pInstruments->end()) return;
1957 const size_t idxIt = InstrumentsIterator - pInstruments->begin();
1958 pInstruments->erase(iter);
1959 InstrumentsIterator = pInstruments->begin() + std::min(idxIt, pInstruments->size()); // avoid iterator invalidation
1960 pInstrument->DeleteChunks();
1961 delete pInstrument;
1962 }
1963
1969 return pRIFF;
1970 }
1971
1985 if (index < 0 || index >= ExtensionFiles.size()) return NULL;
1986 std::list<RIFF::File*>::iterator iter = ExtensionFiles.begin();
1987 for (int i = 0; iter != ExtensionFiles.end(); ++iter, ++i)
1988 if (i == index) return *iter;
1989 return NULL;
1990 }
1991
2002 return pRIFF->GetFileName();
2003 }
2004
2009 void File::SetFileName(const String& name) {
2010 pRIFF->SetFileName(name);
2011 }
2012
2022 // first update base class's chunks
2023 Resource::UpdateChunks(pProgress);
2024
2025 // if version struct exists, update 'vers' chunk
2026 if (pVersion) {
2027 RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
2028 if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);
2029 uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();
2030 store16(&pData[0], pVersion->minor);
2031 store16(&pData[2], pVersion->major);
2032 store16(&pData[4], pVersion->build);
2033 store16(&pData[6], pVersion->release);
2034 }
2035
2036 // update 'colh' chunk
2037 Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0;
2038 RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
2039 if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
2040 uint8_t* pData = (uint8_t*) colh->LoadChunkData();
2041 store32(pData, Instruments);
2042
2043 // update instrument's chunks
2044 if (pInstruments) {
2045 if (pProgress) {
2046 // divide local progress into subprogress
2047 progress_t subprogress;
2048 __divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress
2049
2050 // do the actual work
2051 InstrumentList::iterator iter = pInstruments->begin();
2052 InstrumentList::iterator end = pInstruments->end();
2053 for (int i = 0; iter != end; ++iter, ++i) {
2054 // divide subprogress into sub-subprogress
2055 progress_t subsubprogress;
2056 __divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i);
2057 // do the actual work
2058 (*iter)->UpdateChunks(&subsubprogress);
2059 }
2060
2061 __notify_progress(&subprogress, 1.0); // notify subprogress done
2062 } else {
2063 InstrumentList::iterator iter = pInstruments->begin();
2064 InstrumentList::iterator end = pInstruments->end();
2065 for (int i = 0; iter != end; ++iter, ++i) {
2066 (*iter)->UpdateChunks(NULL);
2067 }
2068 }
2069 }
2070
2071 // update 'ptbl' chunk
2072 const int iSamples = (pSamples) ? int(pSamples->size()) : 0;
2073 int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2074 RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2075 if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/);
2076 int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
2077 ptbl->Resize(iPtblSize);
2078 pData = (uint8_t*) ptbl->LoadChunkData();
2079 WavePoolCount = iSamples;
2080 store32(&pData[4], WavePoolCount);
2081 // we actually update the sample offsets in the pool table when we Save()
2082 memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);
2083
2084 // update sample's chunks
2085 if (pSamples) {
2086 if (pProgress) {
2087 // divide local progress into subprogress
2088 progress_t subprogress;
2089 __divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress
2090
2091 // do the actual work
2092 SampleList::iterator iter = pSamples->begin();
2093 SampleList::iterator end = pSamples->end();
2094 for (int i = 0; iter != end; ++iter, ++i) {
2095 // divide subprogress into sub-subprogress
2096 progress_t subsubprogress;
2097 __divide_progress(&subprogress, &subsubprogress, pSamples->size(), i);
2098 // do the actual work
2099 (*iter)->UpdateChunks(&subsubprogress);
2100 }
2101
2102 __notify_progress(&subprogress, 1.0); // notify subprogress done
2103 } else {
2104 SampleList::iterator iter = pSamples->begin();
2105 SampleList::iterator end = pSamples->end();
2106 for (int i = 0; iter != end; ++iter, ++i) {
2107 (*iter)->UpdateChunks(NULL);
2108 }
2109 }
2110 }
2111
2112 // if there are any extension files, gather which ones are regular
2113 // extension files used as wave pool files (.gx00, .gx01, ... , .gx98)
2114 // and which one is probably a convolution (GigaPulse) file (always to
2115 // be saved as .gx99)
2116 std::list<RIFF::File*> poolFiles; // < for (.gx00, .gx01, ... , .gx98) files
2117 RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file
2118 if (!ExtensionFiles.empty()) {
2119 std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2120 for (; it != ExtensionFiles.end(); ++it) {
2121 //FIXME: the .gx99 file is always used by GSt for convolution
2122 // data (GigaPulse); so we should better detect by subchunk
2123 // whether the extension file is intended for convolution
2124 // instead of checkking for a file name, because the latter does
2125 // not work for saving new gigs created from scratch
2126 const std::string oldName = (*it)->GetFileName();
2127 const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2128 if (isGigaPulseFile)
2129 pGigaPulseFile = *it;
2130 else
2131 poolFiles.push_back(*it);
2132 }
2133 }
2134
2135 // update the 'xfil' chunk which describes all extension files (wave
2136 // pool files) except the .gx99 file
2137 if (!poolFiles.empty()) {
2138 const int n = poolFiles.size();
2139 const int iHeaderSize = 4;
2140 const int iEntrySize = 144;
2141
2142 // make sure chunk exists, and with correct size
2143 RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2144 if (ckXfil)
2145 ckXfil->Resize(iHeaderSize + n * iEntrySize);
2146 else
2147 ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize);
2148
2149 uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData();
2150
2151 // re-assemble the chunk's content
2152 store32(pData, n);
2153 std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin();
2154 for (int i = 0, iOffset = 4; i < n;
2155 ++itExtFile, ++i, iOffset += iEntrySize)
2156 {
2157 // update the filename string and 5 byte extension of each extension file
2158 std::string file = lastPathComponent(
2159 (*itExtFile)->GetFileName()
2160 );
2161 if (file.length() + 6 > 128)
2162 throw Exception("Fatal error, extension filename length exceeds 122 byte maximum");
2163 uint8_t* pStrings = &pData[iOffset];
2164 memset(pStrings, 0, 128);
2165 memcpy(pStrings, file.c_str(), file.length());
2166 pStrings += file.length() + 1;
2167 std::string ext = file.substr(file.length()-5);
2168 memcpy(pStrings, ext.c_str(), 5);
2169 // update the dlsid of the extension file
2170 uint8_t* pId = &pData[iOffset + 128];
2171 dlsid_t id;
2172 RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID);
2173 if (ckDLSID) {
2174 ckDLSID->Read(&id.ulData1, 1, 4);
2175 ckDLSID->Read(&id.usData2, 1, 2);
2176 ckDLSID->Read(&id.usData3, 1, 2);
2177 ckDLSID->Read(id.abData, 8, 1);
2178 } else {
2179 ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16);
2181 uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
2182 store32(&pData[0], id.ulData1);
2183 store16(&pData[4], id.usData2);
2184 store16(&pData[6], id.usData3);
2185 memcpy(&pData[8], id.abData, 8);
2186 }
2187 store32(&pId[0], id.ulData1);
2188 store16(&pId[4], id.usData2);
2189 store16(&pId[6], id.usData3);
2190 memcpy(&pId[8], id.abData, 8);
2191 }
2192 } else {
2193 // in case there was a 'xfil' chunk, remove it
2194 RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL);
2195 if (ckXfil) pRIFF->DeleteSubChunk(ckXfil);
2196 }
2197
2198 // update the 'doxf' chunk which describes a .gx99 extension file
2199 // which contains convolution data (GigaPulse)
2200 if (pGigaPulseFile) {
2201 RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2202 if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148);
2203
2204 uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData();
2205
2206 // update the dlsid from the extension file
2207 uint8_t* pId = &pData[132];
2208 RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID);
2209 if (!ckDLSID) { //TODO: auto generate DLS ID if missing
2210 throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk");
2211 } else {
2212 dlsid_t id;
2213 // read DLS ID from extension files's DLS ID chunk
2214 uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData();
2215 id.ulData1 = load32(&pData[0]);
2216 id.usData2 = load16(&pData[4]);
2217 id.usData3 = load16(&pData[6]);
2218 memcpy(id.abData, &pData[8], 8);
2219 // store DLS ID to 'doxf' chunk
2220 store32(&pId[0], id.ulData1);
2221 store16(&pId[4], id.usData2);
2222 store16(&pId[6], id.usData3);
2223 memcpy(&pId[8], id.abData, 8);
2224 }
2225 } else {
2226 // in case there was a 'doxf' chunk, remove it
2227 RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF);
2228 if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf);
2229 }
2230
2231 // the RIFF file to be written might now been grown >= 4GB or might
2232 // been shrunk < 4GB, so we might need to update the wave pool offset
2233 // size and thus accordingly we would need to resize the wave pool
2234 // chunk
2235 const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize();
2236 const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file
2237 poolFiles.size() > 0; // < 32 bit gig file where the hi 32 bits are used as extension file nr
2238 if (b64BitWavePoolOffsets != bRequires64Bit) {
2239 b64BitWavePoolOffsets = bRequires64Bit;
2240 iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2241 iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
2242 ptbl->Resize(iPtblSize);
2243 }
2244
2245 if (pProgress)
2246 __notify_progress(pProgress, 1.0); // notify done
2247 }
2248
2263 void File::Save(const String& Path, progress_t* pProgress) {
2264 // calculate number of tasks to notify progress appropriately
2265 const size_t nExtFiles = ExtensionFiles.size();
2266 const float tasks = 2.f + nExtFiles;
2267
2268 // save extension files (if required)
2269 if (!ExtensionFiles.empty()) {
2270 // for assembling path of extension files to be saved to
2271 const std::string baseName = pathWithoutExtension(Path);
2272 // save the individual extension files
2273 std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2274 for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2275 //FIXME: the .gx99 file is always used by GSt for convolution
2276 // data (GigaPulse); so we should better detect by subchunk
2277 // whether the extension file is intended for convolution
2278 // instead of checkking for a file name, because the latter does
2279 // not work for saving new gigs created from scratch
2280 const std::string oldName = (*it)->GetFileName();
2281 const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99");
2282 std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1);
2283 std::string newPath = baseName + ext;
2284 // save extension file to its new location
2285 if (pProgress) {
2286 // divide local progress into subprogress
2287 progress_t subprogress;
2288 __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2289 // do the actual work
2290 (*it)->Save(newPath, &subprogress);
2291 } else
2292 (*it)->Save(newPath);
2293 }
2294 }
2295
2296 if (pProgress) {
2297 // divide local progress into subprogress
2298 progress_t subprogress;
2299 __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2300 // do the actual work
2301 UpdateChunks(&subprogress);
2302 } else
2303 UpdateChunks(NULL);
2304
2305 if (pProgress) {
2306 // divide local progress into subprogress
2307 progress_t subprogress;
2308 __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2309 // do the actual work
2310 pRIFF->Save(Path, &subprogress);
2311 } else
2312 pRIFF->Save(Path);
2313
2315
2316 if (pProgress)
2317 __notify_progress(pProgress, 1.0); // notify done
2318 }
2319
2330 void File::Save(progress_t* pProgress) {
2331 // calculate number of tasks to notify progress appropriately
2332 const size_t nExtFiles = ExtensionFiles.size();
2333 const float tasks = 2.f + nExtFiles;
2334
2335 // save extension files (if required)
2336 if (!ExtensionFiles.empty()) {
2337 std::list<RIFF::File*>::iterator it = ExtensionFiles.begin();
2338 for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) {
2339 // save extension file
2340 if (pProgress) {
2341 // divide local progress into subprogress
2342 progress_t subprogress;
2343 __divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files
2344 // do the actual work
2345 (*it)->Save(&subprogress);
2346 } else
2347 (*it)->Save();
2348 }
2349 }
2350
2351 if (pProgress) {
2352 // divide local progress into subprogress
2353 progress_t subprogress;
2354 __divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2355 // do the actual work
2356 UpdateChunks(&subprogress);
2357 } else
2358 UpdateChunks(NULL);
2359
2360 if (pProgress) {
2361 // divide local progress into subprogress
2362 progress_t subprogress;
2363 __divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress)
2364 // do the actual work
2365 pRIFF->Save(&subprogress);
2366 } else
2367 pRIFF->Save();
2368
2370
2371 if (pProgress)
2372 __notify_progress(pProgress, 1.0); // notify done
2373 }
2374
2386 __UpdateWavePoolTableChunk();
2387 }
2388
2395 // enusre 'lins' list chunk exists (mandatory for instrument definitions)
2396 RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2397 if (!lstInstruments) pRIFF->AddSubList(LIST_TYPE_LINS);
2398 // ensure 'ptbl' chunk exists (mandatory for samples)
2399 RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2400 if (!ptbl) {
2401 const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2402 ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, WavePoolHeaderSize + iOffsetSize);
2403 }
2404 // enusre 'wvpl' list chunk exists (mandatory for samples)
2405 RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2406 if (!wvpl) pRIFF->AddSubList(LIST_TYPE_WVPL);
2407 }
2408
2418 void File::__UpdateWavePoolTableChunk() {
2419 __UpdateWavePoolTable();
2420 RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
2421 const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
2422 // check if 'ptbl' chunk is large enough
2423 WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2424 const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
2425 if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
2426 // save the 'ptbl' chunk's current read/write position
2427 file_offset_t ullOriginalPos = ptbl->GetPos();
2428 // update headers
2429 ptbl->SetPos(0);
2430 uint32_t tmp = WavePoolHeaderSize;
2431 ptbl->WriteUint32(&tmp);
2432 tmp = WavePoolCount;
2433 ptbl->WriteUint32(&tmp);
2434 // update offsets
2435 ptbl->SetPos(WavePoolHeaderSize);
2436 if (b64BitWavePoolOffsets) {
2437 for (int i = 0 ; i < WavePoolCount ; i++) {
2438 tmp = pWavePoolTableHi[i];
2439 ptbl->WriteUint32(&tmp);
2440 tmp = pWavePoolTable[i];
2441 ptbl->WriteUint32(&tmp);
2442 }
2443 } else { // conventional 32 bit offsets
2444 for (int i = 0 ; i < WavePoolCount ; i++) {
2445 tmp = pWavePoolTable[i];
2446 ptbl->WriteUint32(&tmp);
2447 }
2448 }
2449 // restore 'ptbl' chunk's original read/write position
2450 ptbl->SetPos(ullOriginalPos);
2451 }
2452
2458 void File::__UpdateWavePoolTable() {
2459 WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0;
2460 // resize wave pool table arrays
2461 if (pWavePoolTable) delete[] pWavePoolTable;
2462 if (pWavePoolTableHi) delete[] pWavePoolTableHi;
2463 pWavePoolTable = new uint32_t[WavePoolCount];
2464 pWavePoolTableHi = new uint32_t[WavePoolCount];
2465 if (!pSamples) return;
2466 // update offsets in wave pool table
2467 RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2468 uint64_t wvplFileOffset = wvpl->GetFilePos() -
2469 wvpl->GetPos(); // mandatory, since position might have changed
2470 if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ...
2471 SampleList::iterator iter = pSamples->begin();
2472 SampleList::iterator end = pSamples->end();
2473 for (int i = 0 ; iter != end ; ++iter, i++) {
2474 uint64_t _64BitOffset =
2475 (*iter)->pWaveList->GetFilePos() -
2476 (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2477 wvplFileOffset -
2478 LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2479 (*iter)->ullWavePoolOffset = _64BitOffset;
2480 pWavePoolTable[i] = (uint32_t) _64BitOffset;
2481 }
2482 } else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ...
2483 if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ...
2484 SampleList::iterator iter = pSamples->begin();
2485 SampleList::iterator end = pSamples->end();
2486 for (int i = 0 ; iter != end ; ++iter, i++) {
2487 uint64_t _64BitOffset =
2488 (*iter)->pWaveList->GetFilePos() -
2489 (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2490 wvplFileOffset -
2491 LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize());
2492 (*iter)->ullWavePoolOffset = _64BitOffset;
2493 pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
2494 pWavePoolTable[i] = (uint32_t) _64BitOffset;
2495 }
2496 } else { // 32 bit offsets with extension files (GigaStudio legacy support) ...
2497 // the main gig and the extension files may contain wave data
2498 std::vector<RIFF::File*> poolFiles;
2499 poolFiles.push_back(pRIFF);
2500 poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end());
2501
2502 RIFF::File* pCurPoolFile = NULL;
2503 int fileNo = 0;
2504 int waveOffset = 0;
2505 SampleList::iterator iter = pSamples->begin();
2506 SampleList::iterator end = pSamples->end();
2507 for (int i = 0 ; iter != end ; ++iter, i++) {
2508 RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile();
2509 // if this sample is located in the same pool file as the
2510 // last we reuse the previously computed fileNo and waveOffset
2511 if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ...
2512 pCurPoolFile = pPoolFile;
2513
2514 std::vector<RIFF::File*>::iterator sIter;
2515 sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile);
2516 if (sIter != poolFiles.end())
2517 fileNo = std::distance(poolFiles.begin(), sIter);
2518 else
2519 throw DLS::Exception("Fatal error, unknown pool file");
2520
2521 RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL);
2522 if (!extWvpl)
2523 throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk");
2524 waveOffset =
2525 extWvpl->GetFilePos() -
2526 extWvpl->GetPos() + // mandatory, since position might have changed
2527 LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize());
2528 }
2529 uint64_t _64BitOffset =
2530 (*iter)->pWaveList->GetFilePos() -
2531 (*iter)->pWaveList->GetPos() - // should be zero, but just to be sure
2532 waveOffset;
2533 // pWavePoolTableHi stores file number when extension files are in use
2534 pWavePoolTableHi[i] = (uint32_t) fileNo;
2535 pWavePoolTable[i] = (uint32_t) _64BitOffset;
2536 (*iter)->ullWavePoolOffset = _64BitOffset;
2537 }
2538 }
2539 }
2540 }
2541
2542
2543// *************** Exception ***************
2544// *
2545
2546 Exception::Exception() : RIFF::Exception() {
2547 }
2548
2549 Exception::Exception(String format, ...) : RIFF::Exception() {
2550 va_list arg;
2551 va_start(arg, format);
2552 Message = assemble(format, arg);
2553 va_end(arg);
2554 }
2555
2556 Exception::Exception(String format, va_list arg) : RIFF::Exception() {
2557 Message = assemble(format, arg);
2558 }
2559
2560 void Exception::PrintMessage() {
2561 std::cout << "DLS::Exception: " << Message << std::endl;
2562 }
2563
2564
2565// *************** functions ***************
2566// *
2567
2573 String libraryName() {
2574 return PACKAGE;
2575 }
2576
2582 return VERSION;
2583 }
2584
2585} // namespace DLS
Provides access to the defined connections used for the synthesis model.
Definition DLS.h:328
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:277
Articulation * GetNextArticulation()
Returns the next Articulation from the list of articulations.
Definition DLS.cpp:234
Articulation * GetFirstArticulation()
Returns the first Articulation in the list of articulations.
Definition DLS.cpp:216
Articulation * GetArticulation(size_t pos)
Returns Articulation at supplied pos position within the articulation list.
Definition DLS.cpp:200
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:306
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Articulator object.
Definition DLS.cpp:291
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:623
Parses DLS Level 1 and 2 compliant files and provides abstract access to the data.
Definition DLS.h:564
version_t * pVersion
Points to a version_t structure if the file provided a version number else is set to NULL.
Definition DLS.h:566
Instrument * AddInstrument()
Add a new instrument definition.
Definition DLS.cpp:1934
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:2021
void __ensureMandatoryChunksExist()
Checks if all (for DLS) mandatory chunks exist, if not they will be created.
Definition DLS.cpp:2394
File()
Constructor.
Definition DLS.cpp:1643
Instrument * GetNextInstrument()
Returns a pointer to the next Instrument object of the file, NULL otherwise.
Definition DLS.cpp:1906
Sample * GetSample(size_t index)
Returns Sample object of index.
Definition DLS.cpp:1763
Sample * GetNextSample()
Returns a pointer to the next Sample object of the file, NULL otherwise.
Definition DLS.cpp:1791
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:2009
virtual void UpdateFileOffsets()
Updates all file offsets stored all over the file.
Definition DLS.cpp:2385
void DeleteInstrument(Instrument *pInstrument)
Delete an instrument.
Definition DLS.cpp:1953
uint32_t Instruments
Reflects the number of available Instrument objects.
Definition DLS.h:567
virtual void Save(const String &Path, progress_t *pProgress=NULL)
Save changes to another file.
Definition DLS.cpp:2263
Instrument * GetInstrument(size_t index)
Returns the instrument with the given index from the list of instruments of this file.
Definition DLS.cpp:1878
bool bOwningRiff
If true then pRIFF was implicitly allocated by this class and hence pRIFF will automatically be freed...
Definition DLS.h:604
void DeleteSample(Sample *pSample)
Delete a sample.
Definition DLS.cpp:1860
RIFF::File * GetRiffFile()
Returns the underlying RIFF::File used for persistency of this DLS::File object.
Definition DLS.cpp:1968
Sample * GetFirstSample()
Returns a pointer to the first Sample object of the file, NULL otherwise.
Definition DLS.cpp:1777
Instrument * GetFirstInstrument()
Returns a pointer to the first Instrument object of the file, NULL otherwise.
Definition DLS.cpp:1892
Sample * AddSample()
Add a new sample.
Definition DLS.cpp:1840
RIFF::File * GetExtensionFile(int index)
Returns extension file of given index.
Definition DLS.cpp:1984
String GetFileName()
File name of this DLS file.
Definition DLS.cpp:2001
Optional information for DLS files, instruments, samples, etc.
Definition DLS.h:363
String Subject
<ISBJ-ck>. Describes the contents of the file.
Definition DLS.h:381
String Genre
<IGNR-ck>. Descirbes the original work, such as, Jazz, Classic, Rock, Techno, Rave,...
Definition DLS.h:372
String Keywords
<IKEY-ck>. Provides a list of keywords that refer to the file or subject of the file....
Definition DLS.h:373
String SourceForm
<ISRF-ck>. Identifies the original form of the material that was digitized, such as record,...
Definition DLS.h:379
String Comments
<ICMT-ck>. Provides general comments about the file or the subject of the file. Sentences might end w...
Definition DLS.h:368
String Medium
<IMED-ck>. Describes the original subject of the file, such as, record, CD, and so forth.
Definition DLS.h:377
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:362
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Info object.
Definition DLS.cpp:477
Info(RIFF::List *list)
Constructor.
Definition DLS.cpp:321
String Software
<ISFT-ck>. Identifies the name of the sofware package used to create the file.
Definition DLS.h:376
String Artists
<IART-ck>. Lists the artist of the original subject of the file.
Definition DLS.h:371
String Product
<IPRD-ck>. Specifies the name of the title the file was originally intended for, such as World Ruler ...
Definition DLS.h:369
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:486
String Name
<INAM-ck>. Stores the title of the subject of the file, such as, Seattle From Above.
Definition DLS.h:365
String CreationDate
<ICRD-ck>. Specifies the date the subject of the file was created. List dates in yyyy-mm-dd format.
Definition DLS.h:367
String ArchivalLocation
<IARL-ck>. Indicates where the subject of the file is stored.
Definition DLS.h:366
String Engineer
<IENG-ck>. Stores the name of the engineer who worked on the file. Multiple engineer names are separa...
Definition DLS.h:374
virtual void UpdateChunks(progress_t *pProgress)
Update chunks with current info values.
Definition DLS.cpp:412
String Commissioned
<ICMS-ck>. Lists the name of the person or organization that commissioned the subject of the file,...
Definition DLS.h:380
String Copyright
<ICOP-ck>. Records the copyright information for the file.
Definition DLS.h:370
String Source
<ISRC-ck>. Identifies the name of the person or organization who supplied the original subject of the...
Definition DLS.h:378
String Technician
<ITCH-ck>. Identifies the technician who sampled the subject file.
Definition DLS.h:375
Provides all neccessary information for the synthesis of a DLS Instrument.
Definition DLS.h:524
virtual ~Instrument()
Destructor.
Definition DLS.cpp:1559
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:1618
uint8_t MIDIBankFine
Reflects the MIDI Bank number for MIDI Control Change 32 (bank 1 - 128).
Definition DLS.h:529
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Instrument object.
Definition DLS.cpp:1575
Region * GetFirstRegion()
Returns the first Region of the instrument.
Definition DLS.cpp:1434
Region * GetNextRegion()
Returns the next Region of the instrument.
Definition DLS.cpp:1451
uint8_t MIDIBankCoarse
Reflects the MIDI Bank number for MIDI Control Change 0 (bank 1 - 128).
Definition DLS.h:528
bool IsDrum
Indicates if the Instrument is a drum type, as they differ in the synthesis model of DLS from melodic...
Definition DLS.h:526
uint32_t Regions
Reflects the number of Region defintions this Instrument has.
Definition DLS.h:531
uint16_t MIDIBank
Reflects combination of MIDIBankCoarse and MIDIBankFine (bank 1 - bank 16384). Do not change this val...
Definition DLS.h:527
Instrument(File *pFile, RIFF::List *insList)
Constructor.
Definition DLS.cpp:1372
uint32_t MIDIProgram
Specifies the MIDI Program Change Number this Instrument should be assigned to.
Definition DLS.h:530
Region * GetRegionAt(size_t pos)
Returns Region at supplied pos position within the region list of this instrument.
Definition DLS.cpp:1418
size_t CountRegions()
Returns the amount of regions of this instrument.
Definition DLS.cpp:1402
virtual void UpdateChunks(progress_t *pProgress)
Apply Instrument with all its Regions to the respective RIFF chunks.
Definition DLS.cpp:1519
Defines Region information of an Instrument.
Definition DLS.h:493
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:1325
virtual ~Region()
Destructor.
Definition DLS.cpp:1177
void SetSample(Sample *pSample)
Assign another sample to this Region.
Definition DLS.cpp:1216
virtual void UpdateChunks(progress_t *pProgress)
Apply Region settings to the respective RIFF chunks.
Definition DLS.cpp:1263
range_t KeyRange
Definition DLS.h:495
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:1228
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Region object.
Definition DLS.cpp:1184
Abstract base class which encapsulates data structures which all DLS resources are able to provide.
Definition DLS.h:404
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Resource object.
Definition DLS.cpp:555
Info * pInfo
Points (in any case) to an Info object, providing additional, optional infos and comments.
Definition DLS.h:406
dlsid_t * pDLSID
Points to a dlsid_t structure if the file provided a DLS ID else is NULL.
Definition DLS.h:407
virtual void UpdateChunks(progress_t *pProgress)
Update chunks with current Resource data.
Definition DLS.cpp:568
void GenerateDLSID()
Generates a new DLSID for the resource.
Definition DLS.cpp:587
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:637
Encapsulates sample waves used for playback.
Definition DLS.h:457
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:1064
void ReleaseSampleData()
Free sample data from RAM.
Definition DLS.cpp:972
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:897
uint16_t BlockAlign
The block alignment (in bytes) of the waveform data. Playback software needs to process a multiple of...
Definition DLS.h:463
virtual void UpdateChunks(progress_t *pProgress)
Apply sample and its settings to the respective RIFF chunks.
Definition DLS.cpp:1098
void Resize(file_offset_t NewSize)
Resize sample.
Definition DLS.cpp:1019
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:1046
uint16_t BitDepth
Size of each sample per channel (only if known sample data format is used, 0 otherwise).
Definition DLS.h:464
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:917
void * LoadSampleData()
Load sample data into RAM.
Definition DLS.cpp:963
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:460
file_offset_t GetSize() const
Returns sample size.
Definition DLS.cpp:986
uint32_t SamplesPerSecond
Sampling rate at which each channel should be played (defaults to 44100 if Sample was created with In...
Definition DLS.h:461
file_offset_t SamplesTotal
Reflects total number of sample points (only if known sample data format is used, 0 otherwise),...
Definition DLS.h:465
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Sample object.
Definition DLS.cpp:874
uint16_t FormatTag
Format ID of the waveform data (should be DLS_WAVE_FORMAT_PCM for DLS1 compliant files,...
Definition DLS.h:459
uint FrameSize
Reflects the size (in bytes) of one single sample point (only if known sample data format is used,...
Definition DLS.h:466
uint32_t AverageBytesPerSecond
The average number of bytes per second at which the waveform data should be transferred (Playback sof...
Definition DLS.h:462
virtual ~Sample()
Destructor.
Definition DLS.cpp:863
Sample(File *pFile, RIFF::List *waveList, file_offset_t WavePoolOffset)
Constructor.
Definition DLS.cpp:823
file_offset_t Write(void *pBuffer, file_offset_t SampleCount)
Write sample wave data.
Definition DLS.cpp:1084
Abstract base class which provides mandatory informations about sample players in general.
Definition DLS.h:425
virtual void DeleteChunks()
Remove all RIFF chunks associated with this Sampler object.
Definition DLS.cpp:734
virtual void UpdateChunks(progress_t *pProgress)
Apply all sample player options to the respective RIFF chunk.
Definition DLS.cpp:693
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:788
void AddSampleLoop(sample_loop_t *pLoopDef)
Adds a new sample loop with the provided loop definition.
Definition DLS.cpp:742
uint32_t SampleLoops
Reflects the number of sample loops.
Definition DLS.h:432
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:433
void DeleteSampleLoop(sample_loop_t *pLoopDef)
Deletes an existing sample loop.
Definition DLS.cpp:764
int32_t Gain
Definition DLS.h:429
Ordinary RIFF Chunk.
Definition RIFF.h:179
File * GetFile() const
Returns pointer to the chunk's File object.
Definition RIFF.h:184
List * GetParent() const
Returns pointer to the chunk's parent list chunk.
Definition RIFF.h:185
void Resize(file_offset_t NewSize)
Resize chunk.
Definition RIFF.cpp:1034
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:341
file_offset_t GetFilePos() const
Current, actual offset in file of current chunk data body read/write position.
Definition RIFF.cpp:324
void ReleaseChunkData()
Free loaded chunk body from RAM.
Definition RIFF.cpp:1009
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:517
file_offset_t RemainingBytes() const
Returns the number of bytes left to read in the chunk body.
Definition RIFF.cpp:376
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:788
file_offset_t GetPos() const
Current read/write position within the chunk data body (starting with 0).
Definition RIFF.cpp:311
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:710
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:441
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:671
uint32_t GetChunkID() const
Chunk ID in unsigned integer representation.
Definition RIFF.h:183
void * LoadChunkData()
Load chunk body into RAM.
Definition RIFF.cpp:960
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:749
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:828
file_offset_t GetSize() const
Chunk size in bytes (without header, thus the chunk data body)
Definition RIFF.h:186
RIFF File.
Definition RIFF.h:313
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:2555
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
Definition RIFF.cpp:2167
file_offset_t GetRequiredFileSize()
Returns the required size (in bytes) for this RIFF File to be saved to disk.
Definition RIFF.cpp:2491
virtual void Save(progress_t *pProgress=NULL)
Save changes to same file.
Definition RIFF.cpp:2185
RIFF List Chunk.
Definition RIFF.h:261
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
Definition RIFF.cpp:1246
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk witin this list.
Definition RIFF.cpp:1480
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
Definition RIFF.cpp:1537
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
Definition RIFF.cpp:1556
List * GetSubListAt(size_t pos)
Returns sublist chunk with list type ListType at supplied pos position among all subchunks of type Li...
Definition RIFF.cpp:1264
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
Definition RIFF.cpp:1287
uint32_t GetListType() const
Returns unsigned integer representation of the list's ID.
Definition RIFF.h:265
Chunk * AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize)
Creates a new sub chunk.
Definition RIFF.cpp:1458
Chunk * GetSubChunkAt(size_t pos)
Returns subchunk at supplied pos position within this chunk list.
Definition RIFF.cpp:1229
DLS specific classes and definitions.
Definition DLS.h:108
String libraryName()
Returns the name of this C++ library.
Definition DLS.cpp:2573
String libraryVersion()
Returns version of this C++ library.
Definition DLS.cpp:2581
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:97
stream_whence_t
File stream position dependent to these relations.
Definition RIFF.h:124
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
Definition RIFF.h:107
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:163