2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
* Copyright (C) 2003-2014 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
23 |
|
|
24 |
#include "DLS.h" |
#include "DLS.h" |
25 |
|
|
26 |
|
#include <algorithm> |
27 |
#include <time.h> |
#include <time.h> |
28 |
|
|
29 |
#ifdef __APPLE__ |
#ifdef __APPLE__ |
144 |
/** |
/** |
145 |
* Apply articulation connections to the respective RIFF chunks. You |
* Apply articulation connections to the respective RIFF chunks. You |
146 |
* have to call File::Save() to make changes persistent. |
* have to call File::Save() to make changes persistent. |
147 |
|
* |
148 |
|
* @param pProgress - callback function for progress notification |
149 |
*/ |
*/ |
150 |
void Articulation::UpdateChunks() { |
void Articulation::UpdateChunks(progress_t* pProgress) { |
151 |
const int iEntrySize = 12; // 12 bytes per connection block |
const int iEntrySize = 12; // 12 bytes per connection block |
152 |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
153 |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
219 |
/** |
/** |
220 |
* Apply all articulations to the respective RIFF chunks. You have to |
* Apply all articulations to the respective RIFF chunks. You have to |
221 |
* call File::Save() to make changes persistent. |
* call File::Save() to make changes persistent. |
222 |
|
* |
223 |
|
* @param pProgress - callback function for progress notification |
224 |
*/ |
*/ |
225 |
void Articulator::UpdateChunks() { |
void Articulator::UpdateChunks(progress_t* pProgress) { |
226 |
if (pArticulations) { |
if (pArticulations) { |
227 |
ArticulationList::iterator iter = pArticulations->begin(); |
ArticulationList::iterator iter = pArticulations->begin(); |
228 |
ArticulationList::iterator end = pArticulations->end(); |
ArticulationList::iterator end = pArticulations->end(); |
229 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
230 |
(*iter)->UpdateChunks(); |
(*iter)->UpdateChunks(pProgress); |
231 |
} |
} |
232 |
} |
} |
233 |
} |
} |
234 |
|
|
235 |
|
/** |
236 |
|
* Not yet implemented in this version, since the .gig format does |
237 |
|
* not need to copy DLS articulators and so far nobody used pure |
238 |
|
* DLS instrument AFAIK. |
239 |
|
*/ |
240 |
|
void Articulator::CopyAssign(const Articulator* orig) { |
241 |
|
//TODO: implement deep copy assignment for this class |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
253 |
* @param list - pointer to a list chunk which contains an INFO list chunk |
* @param list - pointer to a list chunk which contains an INFO list chunk |
254 |
*/ |
*/ |
255 |
Info::Info(RIFF::List* list) { |
Info::Info(RIFF::List* list) { |
256 |
FixedStringLengths = NULL; |
pFixedStringLengths = NULL; |
257 |
pResourceListChunk = list; |
pResourceListChunk = list; |
258 |
if (list) { |
if (list) { |
259 |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
282 |
Info::~Info() { |
Info::~Info() { |
283 |
} |
} |
284 |
|
|
285 |
|
/** |
286 |
|
* Forces specific Info fields to be of a fixed length when being saved |
287 |
|
* to a file. By default the respective RIFF chunk of an Info field |
288 |
|
* will have a size analogue to its actual string length. With this |
289 |
|
* method however this behavior can be overridden, allowing to force an |
290 |
|
* arbitrary fixed size individually for each Info field. |
291 |
|
* |
292 |
|
* This method is used as a workaround for the gig format, not for DLS. |
293 |
|
* |
294 |
|
* @param lengths - NULL terminated array of string_length_t elements |
295 |
|
*/ |
296 |
|
void Info::SetFixedStringLengths(const string_length_t* lengths) { |
297 |
|
pFixedStringLengths = lengths; |
298 |
|
} |
299 |
|
|
300 |
/** @brief Load given INFO field. |
/** @brief Load given INFO field. |
301 |
* |
* |
302 |
* Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO |
* Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO |
324 |
*/ |
*/ |
325 |
void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) { |
void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) { |
326 |
int size = 0; |
int size = 0; |
327 |
if (FixedStringLengths) { |
if (pFixedStringLengths) { |
328 |
for (int i = 0 ; FixedStringLengths[i].length ; i++) { |
for (int i = 0 ; pFixedStringLengths[i].length ; i++) { |
329 |
if (FixedStringLengths[i].chunkId == ChunkID) { |
if (pFixedStringLengths[i].chunkId == ChunkID) { |
330 |
size = FixedStringLengths[i].length; |
size = pFixedStringLengths[i].length; |
331 |
break; |
break; |
332 |
} |
} |
333 |
} |
} |
340 |
* |
* |
341 |
* Apply current INFO field values to the respective INFO chunks. You |
* Apply current INFO field values to the respective INFO chunks. You |
342 |
* have to call File::Save() to make changes persistent. |
* have to call File::Save() to make changes persistent. |
343 |
|
* |
344 |
|
* @param pProgress - callback function for progress notification |
345 |
*/ |
*/ |
346 |
void Info::UpdateChunks() { |
void Info::UpdateChunks(progress_t* pProgress) { |
347 |
if (!pResourceListChunk) return; |
if (!pResourceListChunk) return; |
348 |
|
|
349 |
// make sure INFO list chunk exists |
// make sure INFO list chunk exists |
398 |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
399 |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
400 |
} |
} |
401 |
|
|
402 |
|
/** |
403 |
|
* Make a deep copy of the Info object given by @a orig and assign it to |
404 |
|
* this object. |
405 |
|
* |
406 |
|
* @param orig - original Info object to be copied from |
407 |
|
*/ |
408 |
|
void Info::CopyAssign(const Info* orig) { |
409 |
|
Name = orig->Name; |
410 |
|
ArchivalLocation = orig->ArchivalLocation; |
411 |
|
CreationDate = orig->CreationDate; |
412 |
|
Comments = orig->Comments; |
413 |
|
Product = orig->Product; |
414 |
|
Copyright = orig->Copyright; |
415 |
|
Artists = orig->Artists; |
416 |
|
Genre = orig->Genre; |
417 |
|
Keywords = orig->Keywords; |
418 |
|
Engineer = orig->Engineer; |
419 |
|
Technician = orig->Technician; |
420 |
|
Software = orig->Software; |
421 |
|
Medium = orig->Medium; |
422 |
|
Source = orig->Source; |
423 |
|
SourceForm = orig->SourceForm; |
424 |
|
Commissioned = orig->Commissioned; |
425 |
|
Subject = orig->Subject; |
426 |
|
//FIXME: hmm, is copying this pointer a good idea? |
427 |
|
pFixedStringLengths = orig->pFixedStringLengths; |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
|
470 |
* will not be applied at the moment (yet). |
* will not be applied at the moment (yet). |
471 |
* |
* |
472 |
* You have to call File::Save() to make changes persistent. |
* You have to call File::Save() to make changes persistent. |
473 |
|
* |
474 |
|
* @param pProgress - callback function for progress notification |
475 |
*/ |
*/ |
476 |
void Resource::UpdateChunks() { |
void Resource::UpdateChunks(progress_t* pProgress) { |
477 |
pInfo->UpdateChunks(); |
pInfo->UpdateChunks(pProgress); |
478 |
|
|
479 |
if (pDLSID) { |
if (pDLSID) { |
480 |
// make sure 'dlid' chunk exists |
// make sure 'dlid' chunk exists |
502 |
UUID uuid; |
UUID uuid; |
503 |
UuidCreate(&uuid); |
UuidCreate(&uuid); |
504 |
pDLSID->ulData1 = uuid.Data1; |
pDLSID->ulData1 = uuid.Data1; |
505 |
pDLSID->usData1 = uuid.Data2; |
pDLSID->usData2 = uuid.Data2; |
506 |
pDLSID->usData2 = uuid.Data3; |
pDLSID->usData3 = uuid.Data3; |
507 |
memcpy(pDLSID->abData, uuid.Data4, 8); |
memcpy(pDLSID->abData, uuid.Data4, 8); |
508 |
|
|
509 |
#elif defined(__APPLE__) |
#elif defined(__APPLE__) |
532 |
#endif |
#endif |
533 |
#endif |
#endif |
534 |
} |
} |
535 |
|
|
536 |
|
/** |
537 |
|
* Make a deep copy of the Resource object given by @a orig and assign it |
538 |
|
* to this object. |
539 |
|
* |
540 |
|
* @param orig - original Resource object to be copied from |
541 |
|
*/ |
542 |
|
void Resource::CopyAssign(const Resource* orig) { |
543 |
|
pInfo->CopyAssign(orig->pInfo); |
544 |
|
} |
545 |
|
|
546 |
|
|
547 |
// *************** Sampler *************** |
// *************** Sampler *************** |
558 |
SamplerOptions = wsmp->ReadUint32(); |
SamplerOptions = wsmp->ReadUint32(); |
559 |
SampleLoops = wsmp->ReadUint32(); |
SampleLoops = wsmp->ReadUint32(); |
560 |
} else { // 'wsmp' chunk missing |
} else { // 'wsmp' chunk missing |
561 |
uiHeaderSize = 0; |
uiHeaderSize = 20; |
562 |
UnityNote = 60; |
UnityNote = 60; |
563 |
FineTune = 0; // +- 0 cents |
FineTune = 0; // +- 0 cents |
564 |
Gain = 0; // 0 dB |
Gain = 0; // 0 dB |
583 |
if (pSampleLoops) delete[] pSampleLoops; |
if (pSampleLoops) delete[] pSampleLoops; |
584 |
} |
} |
585 |
|
|
586 |
|
void Sampler::SetGain(int32_t gain) { |
587 |
|
Gain = gain; |
588 |
|
} |
589 |
|
|
590 |
/** |
/** |
591 |
* Apply all sample player options to the respective RIFF chunk. You |
* Apply all sample player options to the respective RIFF chunk. You |
592 |
* have to call File::Save() to make changes persistent. |
* have to call File::Save() to make changes persistent. |
593 |
|
* |
594 |
|
* @param pProgress - callback function for progress notification |
595 |
*/ |
*/ |
596 |
void Sampler::UpdateChunks() { |
void Sampler::UpdateChunks(progress_t* pProgress) { |
597 |
// make sure 'wsmp' chunk exists |
// make sure 'wsmp' chunk exists |
598 |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
599 |
|
int wsmpSize = uiHeaderSize + SampleLoops * 16; |
600 |
if (!wsmp) { |
if (!wsmp) { |
601 |
uiHeaderSize = 20; |
wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize); |
602 |
wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, uiHeaderSize + SampleLoops * 16); |
} else if (wsmp->GetSize() != wsmpSize) { |
603 |
|
wsmp->Resize(wsmpSize); |
604 |
} |
} |
605 |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
606 |
// update headers size |
// update headers size |
657 |
// copy old loops array (skipping given loop) |
// copy old loops array (skipping given loop) |
658 |
for (int i = 0, o = 0; i < SampleLoops; i++) { |
for (int i = 0, o = 0; i < SampleLoops; i++) { |
659 |
if (&pSampleLoops[i] == pLoopDef) continue; |
if (&pSampleLoops[i] == pLoopDef) continue; |
660 |
if (o == SampleLoops - 1) |
if (o == SampleLoops - 1) { |
661 |
|
delete[] pNewLoops; |
662 |
throw Exception("Could not delete Sample Loop, because it does not exist"); |
throw Exception("Could not delete Sample Loop, because it does not exist"); |
663 |
|
} |
664 |
pNewLoops[o] = pSampleLoops[i]; |
pNewLoops[o] = pSampleLoops[i]; |
665 |
o++; |
o++; |
666 |
} |
} |
669 |
pSampleLoops = pNewLoops; |
pSampleLoops = pNewLoops; |
670 |
SampleLoops--; |
SampleLoops--; |
671 |
} |
} |
672 |
|
|
673 |
|
/** |
674 |
|
* Make a deep copy of the Sampler object given by @a orig and assign it |
675 |
|
* to this object. |
676 |
|
* |
677 |
|
* @param orig - original Sampler object to be copied from |
678 |
|
*/ |
679 |
|
void Sampler::CopyAssign(const Sampler* orig) { |
680 |
|
// copy trivial scalars |
681 |
|
UnityNote = orig->UnityNote; |
682 |
|
FineTune = orig->FineTune; |
683 |
|
Gain = orig->Gain; |
684 |
|
NoSampleDepthTruncation = orig->NoSampleDepthTruncation; |
685 |
|
NoSampleCompression = orig->NoSampleCompression; |
686 |
|
SamplerOptions = orig->SamplerOptions; |
687 |
|
|
688 |
|
// copy sample loops |
689 |
|
if (SampleLoops) delete[] pSampleLoops; |
690 |
|
pSampleLoops = new sample_loop_t[orig->SampleLoops]; |
691 |
|
memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t)); |
692 |
|
SampleLoops = orig->SampleLoops; |
693 |
|
} |
694 |
|
|
695 |
|
|
696 |
// *************** Sample *************** |
// *************** Sample *************** |
754 |
RIFF::List* pParent = pWaveList->GetParent(); |
RIFF::List* pParent = pWaveList->GetParent(); |
755 |
pParent->DeleteSubChunk(pWaveList); |
pParent->DeleteSubChunk(pWaveList); |
756 |
} |
} |
757 |
|
|
758 |
|
/** |
759 |
|
* Make a deep copy of the Sample object given by @a orig (without the |
760 |
|
* actual sample waveform data however) and assign it to this object. |
761 |
|
* |
762 |
|
* This is a special internal variant of CopyAssign() which only copies the |
763 |
|
* most mandatory member variables. It will be called by gig::Sample |
764 |
|
* descendent instead of CopyAssign() since gig::Sample has its own |
765 |
|
* implementation to access and copy the actual sample waveform data. |
766 |
|
* |
767 |
|
* @param orig - original Sample object to be copied from |
768 |
|
*/ |
769 |
|
void Sample::CopyAssignCore(const Sample* orig) { |
770 |
|
// handle base classes |
771 |
|
Resource::CopyAssign(orig); |
772 |
|
// handle actual own attributes of this class |
773 |
|
FormatTag = orig->FormatTag; |
774 |
|
Channels = orig->Channels; |
775 |
|
SamplesPerSecond = orig->SamplesPerSecond; |
776 |
|
AverageBytesPerSecond = orig->AverageBytesPerSecond; |
777 |
|
BlockAlign = orig->BlockAlign; |
778 |
|
BitDepth = orig->BitDepth; |
779 |
|
SamplesTotal = orig->SamplesTotal; |
780 |
|
FrameSize = orig->FrameSize; |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
|
* Make a deep copy of the Sample object given by @a orig and assign it to |
785 |
|
* this object. |
786 |
|
* |
787 |
|
* @param orig - original Sample object to be copied from |
788 |
|
*/ |
789 |
|
void Sample::CopyAssign(const Sample* orig) { |
790 |
|
CopyAssignCore(orig); |
791 |
|
|
792 |
|
// copy sample waveform data (reading directly from disc) |
793 |
|
Resize(orig->GetSize()); |
794 |
|
char* buf = (char*) LoadSampleData(); |
795 |
|
Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now |
796 |
|
const unsigned long restorePos = pOrig->pCkData->GetPos(); |
797 |
|
pOrig->SetPos(0); |
798 |
|
for (unsigned long todo = pOrig->GetSize(), i = 0; todo; ) { |
799 |
|
const int iReadAtOnce = 64*1024; |
800 |
|
unsigned long n = (iReadAtOnce < todo) ? iReadAtOnce : todo; |
801 |
|
n = pOrig->Read(&buf[i], n); |
802 |
|
if (!n) break; |
803 |
|
todo -= n; |
804 |
|
i += (n * pOrig->FrameSize); |
805 |
|
} |
806 |
|
pOrig->pCkData->SetPos(restorePos); |
807 |
|
} |
808 |
|
|
809 |
/** @brief Load sample data into RAM. |
/** @brief Load sample data into RAM. |
810 |
* |
* |
855 |
* @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM |
* @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM |
856 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
857 |
*/ |
*/ |
858 |
unsigned long Sample::GetSize() { |
unsigned long Sample::GetSize() const { |
859 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
860 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
861 |
} |
} |
961 |
* Apply sample and its settings to the respective RIFF chunks. You have |
* Apply sample and its settings to the respective RIFF chunks. You have |
962 |
* to call File::Save() to make changes persistent. |
* to call File::Save() to make changes persistent. |
963 |
* |
* |
964 |
|
* @param pProgress - callback function for progress notification |
965 |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
966 |
* was provided yet |
* was provided yet |
967 |
*/ |
*/ |
968 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks(progress_t* pProgress) { |
969 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
970 |
throw Exception("Could not save sample, only PCM format is supported"); |
throw Exception("Could not save sample, only PCM format is supported"); |
971 |
// we refuse to do anything if not sample wave form was provided yet |
// we refuse to do anything if not sample wave form was provided yet |
972 |
if (!pCkData) |
if (!pCkData) |
973 |
throw Exception("Could not save sample, there is no sample data to save"); |
throw Exception("Could not save sample, there is no sample data to save"); |
974 |
// update chunks of base class as well |
// update chunks of base class as well |
975 |
Resource::UpdateChunks(); |
Resource::UpdateChunks(pProgress); |
976 |
// make sure 'fmt' chunk exists |
// make sure 'fmt' chunk exists |
977 |
RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT); |
RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT); |
978 |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
1067 |
} |
} |
1068 |
|
|
1069 |
/** |
/** |
1070 |
|
* Modifies the key range of this Region and makes sure the respective |
1071 |
|
* chunks are in correct order. |
1072 |
|
* |
1073 |
|
* @param Low - lower end of key range |
1074 |
|
* @param High - upper end of key range |
1075 |
|
*/ |
1076 |
|
void Region::SetKeyRange(uint16_t Low, uint16_t High) { |
1077 |
|
KeyRange.low = Low; |
1078 |
|
KeyRange.high = High; |
1079 |
|
|
1080 |
|
// make sure regions are already loaded |
1081 |
|
Instrument* pInstrument = (Instrument*) GetParent(); |
1082 |
|
if (!pInstrument->pRegions) pInstrument->LoadRegions(); |
1083 |
|
if (!pInstrument->pRegions) return; |
1084 |
|
|
1085 |
|
// find the r which is the first one to the right of this region |
1086 |
|
// at its new position |
1087 |
|
Region* r = NULL; |
1088 |
|
Region* prev_region = NULL; |
1089 |
|
for ( |
1090 |
|
Instrument::RegionList::iterator iter = pInstrument->pRegions->begin(); |
1091 |
|
iter != pInstrument->pRegions->end(); iter++ |
1092 |
|
) { |
1093 |
|
if ((*iter)->KeyRange.low > this->KeyRange.low) { |
1094 |
|
r = *iter; |
1095 |
|
break; |
1096 |
|
} |
1097 |
|
prev_region = *iter; |
1098 |
|
} |
1099 |
|
|
1100 |
|
// place this region before r if it's not already there |
1101 |
|
if (prev_region != this) pInstrument->MoveRegion(this, r); |
1102 |
|
} |
1103 |
|
|
1104 |
|
/** |
1105 |
* Apply Region settings to the respective RIFF chunks. You have to |
* Apply Region settings to the respective RIFF chunks. You have to |
1106 |
* call File::Save() to make changes persistent. |
* call File::Save() to make changes persistent. |
1107 |
* |
* |
1108 |
|
* @param pProgress - callback function for progress notification |
1109 |
* @throws Exception - if the Region's sample could not be found |
* @throws Exception - if the Region's sample could not be found |
1110 |
*/ |
*/ |
1111 |
void Region::UpdateChunks() { |
void Region::UpdateChunks(progress_t* pProgress) { |
1112 |
// make sure 'rgnh' chunk exists |
// make sure 'rgnh' chunk exists |
1113 |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
1114 |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); |
1127 |
|
|
1128 |
// update chunks of base classes as well (but skip Resource, |
// update chunks of base classes as well (but skip Resource, |
1129 |
// as a rgn doesn't seem to have dlid and INFO chunks) |
// as a rgn doesn't seem to have dlid and INFO chunks) |
1130 |
Articulator::UpdateChunks(); |
Articulator::UpdateChunks(pProgress); |
1131 |
Sampler::UpdateChunks(); |
Sampler::UpdateChunks(pProgress); |
1132 |
|
|
1133 |
// make sure 'wlnk' chunk exists |
// make sure 'wlnk' chunk exists |
1134 |
RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK); |
RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK); |
1153 |
} |
} |
1154 |
} |
} |
1155 |
} |
} |
|
if (index < 0) throw Exception("Could not save Region, could not find Region's sample"); |
|
1156 |
WavePoolTableIndex = index; |
WavePoolTableIndex = index; |
1157 |
// update 'wlnk' chunk |
// update 'wlnk' chunk |
1158 |
store16(&pData[0], WaveLinkOptionFlags); |
store16(&pData[0], WaveLinkOptionFlags); |
1160 |
store32(&pData[4], Channel); |
store32(&pData[4], Channel); |
1161 |
store32(&pData[8], WavePoolTableIndex); |
store32(&pData[8], WavePoolTableIndex); |
1162 |
} |
} |
1163 |
|
|
1164 |
|
/** |
1165 |
|
* Make a (semi) deep copy of the Region object given by @a orig and assign |
1166 |
|
* it to this object. |
1167 |
|
* |
1168 |
|
* Note that the sample pointer referenced by @a orig is simply copied as |
1169 |
|
* memory address. Thus the respective sample is shared, not duplicated! |
1170 |
|
* |
1171 |
|
* @param orig - original Region object to be copied from |
1172 |
|
*/ |
1173 |
|
void Region::CopyAssign(const Region* orig) { |
1174 |
|
// handle base classes |
1175 |
|
Resource::CopyAssign(orig); |
1176 |
|
Articulator::CopyAssign(orig); |
1177 |
|
Sampler::CopyAssign(orig); |
1178 |
|
// handle actual own attributes of this class |
1179 |
|
// (the trivial ones) |
1180 |
|
VelocityRange = orig->VelocityRange; |
1181 |
|
KeyGroup = orig->KeyGroup; |
1182 |
|
Layer = orig->Layer; |
1183 |
|
SelfNonExclusive = orig->SelfNonExclusive; |
1184 |
|
PhaseMaster = orig->PhaseMaster; |
1185 |
|
PhaseGroup = orig->PhaseGroup; |
1186 |
|
MultiChannel = orig->MultiChannel; |
1187 |
|
Channel = orig->Channel; |
1188 |
|
// only take the raw sample reference if the two Region objects are |
1189 |
|
// part of the same file |
1190 |
|
if (GetParent()->GetParent() == orig->GetParent()->GetParent()) { |
1191 |
|
WavePoolTableIndex = orig->WavePoolTableIndex; |
1192 |
|
pSample = orig->pSample; |
1193 |
|
} else { |
1194 |
|
WavePoolTableIndex = -1; |
1195 |
|
pSample = NULL; |
1196 |
|
} |
1197 |
|
FormatOptionFlags = orig->FormatOptionFlags; |
1198 |
|
WaveLinkOptionFlags = orig->WaveLinkOptionFlags; |
1199 |
|
// handle the last, a bit sensible attribute |
1200 |
|
SetKeyRange(orig->KeyRange.low, orig->KeyRange.high); |
1201 |
|
} |
1202 |
|
|
1203 |
|
|
1204 |
// *************** Instrument *************** |
// *************** Instrument *************** |
1281 |
|
|
1282 |
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
1283 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1284 |
lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); |
lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0)); |
1285 |
|
|
1286 |
pRegions->remove(pSrc); |
pRegions->remove(pSrc); |
1287 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
1301 |
* Apply Instrument with all its Regions to the respective RIFF chunks. |
* Apply Instrument with all its Regions to the respective RIFF chunks. |
1302 |
* You have to call File::Save() to make changes persistent. |
* You have to call File::Save() to make changes persistent. |
1303 |
* |
* |
1304 |
|
* @param pProgress - callback function for progress notification |
1305 |
* @throws Exception - on errors |
* @throws Exception - on errors |
1306 |
*/ |
*/ |
1307 |
void Instrument::UpdateChunks() { |
void Instrument::UpdateChunks(progress_t* pProgress) { |
1308 |
// first update base classes' chunks |
// first update base classes' chunks |
1309 |
Resource::UpdateChunks(); |
Resource::UpdateChunks(pProgress); |
1310 |
Articulator::UpdateChunks(); |
Articulator::UpdateChunks(pProgress); |
1311 |
// make sure 'insh' chunk exists |
// make sure 'insh' chunk exists |
1312 |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
1313 |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
1326 |
if (!pRegions) return; |
if (!pRegions) return; |
1327 |
RegionList::iterator iter = pRegions->begin(); |
RegionList::iterator iter = pRegions->begin(); |
1328 |
RegionList::iterator end = pRegions->end(); |
RegionList::iterator end = pRegions->end(); |
1329 |
for (; iter != end; ++iter) { |
for (int i = 0; iter != end; ++iter, ++i) { |
1330 |
(*iter)->UpdateChunks(); |
// divide local progress into subprogress |
1331 |
|
progress_t subprogress; |
1332 |
|
__divide_progress(pProgress, &subprogress, pRegions->size(), i); |
1333 |
|
// do the actual work |
1334 |
|
(*iter)->UpdateChunks(&subprogress); |
1335 |
} |
} |
1336 |
|
__notify_progress(pProgress, 1.0); // notify done |
1337 |
} |
} |
1338 |
|
|
1339 |
/** @brief Destructor. |
/** @brief Destructor. |
1355 |
RIFF::List* pParent = pCkInstrument->GetParent(); |
RIFF::List* pParent = pCkInstrument->GetParent(); |
1356 |
pParent->DeleteSubChunk(pCkInstrument); |
pParent->DeleteSubChunk(pCkInstrument); |
1357 |
} |
} |
1358 |
|
|
1359 |
|
void Instrument::CopyAssignCore(const Instrument* orig) { |
1360 |
|
// handle base classes |
1361 |
|
Resource::CopyAssign(orig); |
1362 |
|
Articulator::CopyAssign(orig); |
1363 |
|
// handle actual own attributes of this class |
1364 |
|
// (the trivial ones) |
1365 |
|
IsDrum = orig->IsDrum; |
1366 |
|
MIDIBank = orig->MIDIBank; |
1367 |
|
MIDIBankCoarse = orig->MIDIBankCoarse; |
1368 |
|
MIDIBankFine = orig->MIDIBankFine; |
1369 |
|
MIDIProgram = orig->MIDIProgram; |
1370 |
|
} |
1371 |
|
|
1372 |
|
/** |
1373 |
|
* Make a (semi) deep copy of the Instrument object given by @a orig and assign |
1374 |
|
* it to this object. |
1375 |
|
* |
1376 |
|
* Note that all sample pointers referenced by @a orig are simply copied as |
1377 |
|
* memory address. Thus the respective samples are shared, not duplicated! |
1378 |
|
* |
1379 |
|
* @param orig - original Instrument object to be copied from |
1380 |
|
*/ |
1381 |
|
void Instrument::CopyAssign(const Instrument* orig) { |
1382 |
|
CopyAssignCore(orig); |
1383 |
|
// delete all regions first |
1384 |
|
while (Regions) DeleteRegion(GetFirstRegion()); |
1385 |
|
// now recreate and copy regions |
1386 |
|
{ |
1387 |
|
RegionList::const_iterator it = orig->pRegions->begin(); |
1388 |
|
for (int i = 0; i < orig->Regions; ++i, ++it) { |
1389 |
|
Region* dstRgn = AddRegion(); |
1390 |
|
//NOTE: Region does semi-deep copy ! |
1391 |
|
dstRgn->CopyAssign(*it); |
1392 |
|
} |
1393 |
|
} |
1394 |
|
} |
1395 |
|
|
1396 |
|
|
1397 |
// *************** File *************** |
// *************** File *************** |
1467 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1468 |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
1469 |
pWavePoolTable[i] = ptbl->ReadUint32(); |
pWavePoolTable[i] = ptbl->ReadUint32(); |
1470 |
if (pWavePoolTable[i] & 0x80000000) |
//NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12) |
1471 |
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
//if (pWavePoolTable[i] & 0x80000000) |
1472 |
|
// throw DLS::Exception("Files larger than 2 GB not yet supported"); |
1473 |
} |
} |
1474 |
} else { // conventional 32 bit offsets |
} else { // conventional 32 bit offsets |
1475 |
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
1645 |
} |
} |
1646 |
|
|
1647 |
/** |
/** |
1648 |
|
* Returns extension file of given index. Extension files are used |
1649 |
|
* sometimes to circumvent the 2 GB file size limit of the RIFF format and |
1650 |
|
* of certain operating systems in general. In this case, instead of just |
1651 |
|
* using one file, the content is spread among several files with similar |
1652 |
|
* file name scheme. This is especially used by some GigaStudio sound |
1653 |
|
* libraries. |
1654 |
|
* |
1655 |
|
* @param index - index of extension file |
1656 |
|
* @returns sought extension file, NULL if index out of bounds |
1657 |
|
* @see GetFileName() |
1658 |
|
*/ |
1659 |
|
RIFF::File* File::GetExtensionFile(int index) { |
1660 |
|
if (index < 0 || index >= ExtensionFiles.size()) return NULL; |
1661 |
|
std::list<RIFF::File*>::iterator iter = ExtensionFiles.begin(); |
1662 |
|
for (int i = 0; iter != ExtensionFiles.end(); ++iter, ++i) |
1663 |
|
if (i == index) return *iter; |
1664 |
|
return NULL; |
1665 |
|
} |
1666 |
|
|
1667 |
|
/** @brief File name of this DLS file. |
1668 |
|
* |
1669 |
|
* This method returns the file name as it was provided when loading |
1670 |
|
* the respective DLS file. However in case the File object associates |
1671 |
|
* an empty, that is new DLS file, which was not yet saved to disk, |
1672 |
|
* this method will return an empty string. |
1673 |
|
* |
1674 |
|
* @see GetExtensionFile() |
1675 |
|
*/ |
1676 |
|
String File::GetFileName() { |
1677 |
|
return pRIFF->GetFileName(); |
1678 |
|
} |
1679 |
|
|
1680 |
|
/** |
1681 |
|
* You may call this method store a future file name, so you don't have to |
1682 |
|
* to pass it to the Save() call later on. |
1683 |
|
*/ |
1684 |
|
void File::SetFileName(const String& name) { |
1685 |
|
pRIFF->SetFileName(name); |
1686 |
|
} |
1687 |
|
|
1688 |
|
/** |
1689 |
* Apply all the DLS file's current instruments, samples and settings to |
* Apply all the DLS file's current instruments, samples and settings to |
1690 |
* the respective RIFF chunks. You have to call Save() to make changes |
* the respective RIFF chunks. You have to call Save() to make changes |
1691 |
* persistent. |
* persistent. |
1692 |
* |
* |
1693 |
|
* @param pProgress - callback function for progress notification |
1694 |
* @throws Exception - on errors |
* @throws Exception - on errors |
1695 |
*/ |
*/ |
1696 |
void File::UpdateChunks() { |
void File::UpdateChunks(progress_t* pProgress) { |
1697 |
// first update base class's chunks |
// first update base class's chunks |
1698 |
Resource::UpdateChunks(); |
Resource::UpdateChunks(pProgress); |
1699 |
|
|
1700 |
// if version struct exists, update 'vers' chunk |
// if version struct exists, update 'vers' chunk |
1701 |
if (pVersion) { |
if (pVersion) { |
1717 |
|
|
1718 |
// update instrument's chunks |
// update instrument's chunks |
1719 |
if (pInstruments) { |
if (pInstruments) { |
1720 |
|
// divide local progress into subprogress |
1721 |
|
progress_t subprogress; |
1722 |
|
__divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress |
1723 |
|
|
1724 |
|
// do the actual work |
1725 |
InstrumentList::iterator iter = pInstruments->begin(); |
InstrumentList::iterator iter = pInstruments->begin(); |
1726 |
InstrumentList::iterator end = pInstruments->end(); |
InstrumentList::iterator end = pInstruments->end(); |
1727 |
for (; iter != end; ++iter) { |
for (int i = 0; iter != end; ++iter, ++i) { |
1728 |
(*iter)->UpdateChunks(); |
// divide subprogress into sub-subprogress |
1729 |
|
progress_t subsubprogress; |
1730 |
|
__divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i); |
1731 |
|
// do the actual work |
1732 |
|
(*iter)->UpdateChunks(&subsubprogress); |
1733 |
} |
} |
1734 |
|
|
1735 |
|
__notify_progress(&subprogress, 1.0); // notify subprogress done |
1736 |
} |
} |
1737 |
|
|
1738 |
// update 'ptbl' chunk |
// update 'ptbl' chunk |
1750 |
|
|
1751 |
// update sample's chunks |
// update sample's chunks |
1752 |
if (pSamples) { |
if (pSamples) { |
1753 |
|
// divide local progress into subprogress |
1754 |
|
progress_t subprogress; |
1755 |
|
__divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress |
1756 |
|
|
1757 |
|
// do the actual work |
1758 |
SampleList::iterator iter = pSamples->begin(); |
SampleList::iterator iter = pSamples->begin(); |
1759 |
SampleList::iterator end = pSamples->end(); |
SampleList::iterator end = pSamples->end(); |
1760 |
for (; iter != end; ++iter) { |
for (int i = 0; iter != end; ++iter, ++i) { |
1761 |
(*iter)->UpdateChunks(); |
// divide subprogress into sub-subprogress |
1762 |
|
progress_t subsubprogress; |
1763 |
|
__divide_progress(&subprogress, &subsubprogress, pSamples->size(), i); |
1764 |
|
// do the actual work |
1765 |
|
(*iter)->UpdateChunks(&subsubprogress); |
1766 |
} |
} |
1767 |
|
|
1768 |
|
__notify_progress(&subprogress, 1.0); // notify subprogress done |
1769 |
} |
} |
1770 |
|
|
1771 |
|
__notify_progress(pProgress, 1.0); // notify done |
1772 |
} |
} |
1773 |
|
|
1774 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1783 |
* the new file (given by \a Path) afterwards. |
* the new file (given by \a Path) afterwards. |
1784 |
* |
* |
1785 |
* @param Path - path and file name where everything should be written to |
* @param Path - path and file name where everything should be written to |
1786 |
|
* @param pProgress - optional: callback function for progress notification |
1787 |
*/ |
*/ |
1788 |
void File::Save(const String& Path) { |
void File::Save(const String& Path, progress_t* pProgress) { |
1789 |
UpdateChunks(); |
{ |
1790 |
pRIFF->Save(Path); |
// divide local progress into subprogress |
1791 |
__UpdateWavePoolTableChunk(); |
progress_t subprogress; |
1792 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress |
1793 |
|
// do the actual work |
1794 |
|
UpdateChunks(&subprogress); |
1795 |
|
|
1796 |
|
} |
1797 |
|
{ |
1798 |
|
// divide local progress into subprogress |
1799 |
|
progress_t subprogress; |
1800 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress |
1801 |
|
// do the actual work |
1802 |
|
pRIFF->Save(Path, &subprogress); |
1803 |
|
} |
1804 |
|
UpdateFileOffsets(); |
1805 |
|
__notify_progress(pProgress, 1.0); // notify done |
1806 |
} |
} |
1807 |
|
|
1808 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1811 |
* file. The file might temporarily grow to a higher size than it will |
* file. The file might temporarily grow to a higher size than it will |
1812 |
* have at the end of the saving process. |
* have at the end of the saving process. |
1813 |
* |
* |
1814 |
|
* @param pProgress - optional: callback function for progress notification |
1815 |
* @throws RIFF::Exception if any kind of IO error occured |
* @throws RIFF::Exception if any kind of IO error occured |
1816 |
* @throws DLS::Exception if any kind of DLS specific error occured |
* @throws DLS::Exception if any kind of DLS specific error occured |
1817 |
*/ |
*/ |
1818 |
void File::Save() { |
void File::Save(progress_t* pProgress) { |
1819 |
UpdateChunks(); |
{ |
1820 |
pRIFF->Save(); |
// divide local progress into subprogress |
1821 |
|
progress_t subprogress; |
1822 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress |
1823 |
|
// do the actual work |
1824 |
|
UpdateChunks(&subprogress); |
1825 |
|
} |
1826 |
|
{ |
1827 |
|
// divide local progress into subprogress |
1828 |
|
progress_t subprogress; |
1829 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress |
1830 |
|
// do the actual work |
1831 |
|
pRIFF->Save(&subprogress); |
1832 |
|
} |
1833 |
|
UpdateFileOffsets(); |
1834 |
|
__notify_progress(pProgress, 1.0); // notify done |
1835 |
|
} |
1836 |
|
|
1837 |
|
/** @brief Updates all file offsets stored all over the file. |
1838 |
|
* |
1839 |
|
* This virtual method is called whenever the overall file layout has been |
1840 |
|
* changed (i.e. file or individual RIFF chunks have been resized). It is |
1841 |
|
* then the responsibility of this method to update all file offsets stored |
1842 |
|
* in the file format. For example samples are referenced by instruments by |
1843 |
|
* file offsets. The gig format also stores references to instrument |
1844 |
|
* scripts as file offsets, and thus it overrides this method to update |
1845 |
|
* those file offsets as well. |
1846 |
|
*/ |
1847 |
|
void File::UpdateFileOffsets() { |
1848 |
__UpdateWavePoolTableChunk(); |
__UpdateWavePoolTableChunk(); |
1849 |
} |
} |
1850 |
|
|