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-2014 by Christian Schoenebeck * |
* Copyright (C) 2003-2019 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 * |
24 |
#include "DLS.h" |
#include "DLS.h" |
25 |
|
|
26 |
#include <algorithm> |
#include <algorithm> |
27 |
|
#include <vector> |
28 |
#include <time.h> |
#include <time.h> |
29 |
|
|
30 |
#ifdef __APPLE__ |
#ifdef __APPLE__ |
122 |
artl->GetChunkID() != CHUNK_ID_ARTL) { |
artl->GetChunkID() != CHUNK_ID_ARTL) { |
123 |
throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected"); |
throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected"); |
124 |
} |
} |
125 |
|
|
126 |
|
artl->SetPos(0); |
127 |
|
|
128 |
HeaderSize = artl->ReadUint32(); |
HeaderSize = artl->ReadUint32(); |
129 |
Connections = artl->ReadUint32(); |
Connections = artl->ReadUint32(); |
130 |
artl->SetPos(HeaderSize); |
artl->SetPos(HeaderSize); |
167 |
} |
} |
168 |
} |
} |
169 |
|
|
170 |
|
/** @brief Remove all RIFF chunks associated with this Articulation object. |
171 |
|
* |
172 |
|
* At the moment Articulation::DeleteChunks() does nothing. It is |
173 |
|
* recommended to call this method explicitly though from deriving classes's |
174 |
|
* own overridden implementation of this method to avoid potential future |
175 |
|
* compatiblity issues. |
176 |
|
* |
177 |
|
* See Storage::DeleteChunks() for details. |
178 |
|
*/ |
179 |
|
void Articulation::DeleteChunks() { |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
// *************** Articulator *************** |
// *************** Articulator *************** |
247 |
} |
} |
248 |
} |
} |
249 |
} |
} |
250 |
|
|
251 |
|
/** @brief Remove all RIFF chunks associated with this Articulator object. |
252 |
|
* |
253 |
|
* See Storage::DeleteChunks() for details. |
254 |
|
*/ |
255 |
|
void Articulator::DeleteChunks() { |
256 |
|
if (pArticulations) { |
257 |
|
ArticulationList::iterator iter = pArticulations->begin(); |
258 |
|
ArticulationList::iterator end = pArticulations->end(); |
259 |
|
for (; iter != end; ++iter) { |
260 |
|
(*iter)->DeleteChunks(); |
261 |
|
} |
262 |
|
} |
263 |
|
} |
264 |
|
|
265 |
/** |
/** |
266 |
* Not yet implemented in this version, since the .gig format does |
* Not yet implemented in this version, since the .gig format does |
267 |
* not need to copy DLS articulators and so far nobody used pure |
* not need to copy DLS articulators and so far nobody used pure |
428 |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
429 |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
430 |
} |
} |
431 |
|
|
432 |
|
/** @brief Remove all RIFF chunks associated with this Info object. |
433 |
|
* |
434 |
|
* At the moment Info::DeleteChunks() does nothing. It is |
435 |
|
* recommended to call this method explicitly though from deriving classes's |
436 |
|
* own overridden implementation of this method to avoid potential future |
437 |
|
* compatiblity issues. |
438 |
|
* |
439 |
|
* See Storage::DeleteChunks() for details. |
440 |
|
*/ |
441 |
|
void Info::DeleteChunks() { |
442 |
|
} |
443 |
|
|
444 |
/** |
/** |
445 |
* Make a deep copy of the Info object given by @a orig and assign it to |
* Make a deep copy of the Info object given by @a orig and assign it to |
446 |
* this object. |
* this object. |
491 |
|
|
492 |
RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID); |
RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID); |
493 |
if (ckDLSID) { |
if (ckDLSID) { |
494 |
|
ckDLSID->SetPos(0); |
495 |
|
|
496 |
pDLSID = new dlsid_t; |
pDLSID = new dlsid_t; |
497 |
ckDLSID->Read(&pDLSID->ulData1, 1, 4); |
ckDLSID->Read(&pDLSID->ulData1, 1, 4); |
498 |
ckDLSID->Read(&pDLSID->usData2, 1, 2); |
ckDLSID->Read(&pDLSID->usData2, 1, 2); |
507 |
if (pInfo) delete pInfo; |
if (pInfo) delete pInfo; |
508 |
} |
} |
509 |
|
|
510 |
|
/** @brief Remove all RIFF chunks associated with this Resource object. |
511 |
|
* |
512 |
|
* At the moment Resource::DeleteChunks() does nothing. It is recommended |
513 |
|
* to call this method explicitly though from deriving classes's own |
514 |
|
* overridden implementation of this method to avoid potential future |
515 |
|
* compatiblity issues. |
516 |
|
* |
517 |
|
* See Storage::DeleteChunks() for details. |
518 |
|
*/ |
519 |
|
void Resource::DeleteChunks() { |
520 |
|
} |
521 |
|
|
522 |
/** @brief Update chunks with current Resource data. |
/** @brief Update chunks with current Resource data. |
523 |
* |
* |
524 |
* Apply Resource data persistently below the previously given resource |
* Apply Resource data persistently below the previously given resource |
549 |
* Generates a new DLSID for the resource. |
* Generates a new DLSID for the resource. |
550 |
*/ |
*/ |
551 |
void Resource::GenerateDLSID() { |
void Resource::GenerateDLSID() { |
552 |
#if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE) |
#if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE) |
|
|
|
553 |
if (!pDLSID) pDLSID = new dlsid_t; |
if (!pDLSID) pDLSID = new dlsid_t; |
554 |
|
GenerateDLSID(pDLSID); |
555 |
|
#endif |
556 |
|
} |
557 |
|
|
558 |
|
void Resource::GenerateDLSID(dlsid_t* pDLSID) { |
559 |
|
#if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE) |
560 |
#ifdef WIN32 |
#ifdef WIN32 |
|
|
|
561 |
UUID uuid; |
UUID uuid; |
562 |
UuidCreate(&uuid); |
UuidCreate(&uuid); |
563 |
pDLSID->ulData1 = uuid.Data1; |
pDLSID->ulData1 = uuid.Data1; |
610 |
pParentList = ParentList; |
pParentList = ParentList; |
611 |
RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP); |
RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP); |
612 |
if (wsmp) { |
if (wsmp) { |
613 |
|
wsmp->SetPos(0); |
614 |
|
|
615 |
uiHeaderSize = wsmp->ReadUint32(); |
uiHeaderSize = wsmp->ReadUint32(); |
616 |
UnityNote = wsmp->ReadUint16(); |
UnityNote = wsmp->ReadUint16(); |
617 |
FineTune = wsmp->ReadInt16(); |
FineTune = wsmp->ReadInt16(); |
686 |
} |
} |
687 |
} |
} |
688 |
|
|
689 |
|
/** @brief Remove all RIFF chunks associated with this Sampler object. |
690 |
|
* |
691 |
|
* At the moment Sampler::DeleteChunks() does nothing. It is |
692 |
|
* recommended to call this method explicitly though from deriving classes's |
693 |
|
* own overridden implementation of this method to avoid potential future |
694 |
|
* compatiblity issues. |
695 |
|
* |
696 |
|
* See Storage::DeleteChunks() for details. |
697 |
|
*/ |
698 |
|
void Sampler::DeleteChunks() { |
699 |
|
} |
700 |
|
|
701 |
/** |
/** |
702 |
* Adds a new sample loop with the provided loop definition. |
* Adds a new sample loop with the provided loop definition. |
703 |
* |
* |
784 |
* @param WavePoolOffset - offset of this sample data from wave pool |
* @param WavePoolOffset - offset of this sample data from wave pool |
785 |
* ('wvpl') list chunk |
* ('wvpl') list chunk |
786 |
*/ |
*/ |
787 |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : Resource(pFile, waveList) { |
Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset) : Resource(pFile, waveList) { |
788 |
pWaveList = waveList; |
pWaveList = waveList; |
789 |
ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE; |
ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize()); |
790 |
pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT); |
pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT); |
791 |
pCkData = waveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = waveList->GetSubChunk(CHUNK_ID_DATA); |
792 |
if (pCkFormat) { |
if (pCkFormat) { |
793 |
|
pCkFormat->SetPos(0); |
794 |
|
|
795 |
// common fields |
// common fields |
796 |
FormatTag = pCkFormat->ReadUint16(); |
FormatTag = pCkFormat->ReadUint16(); |
797 |
Channels = pCkFormat->ReadUint16(); |
Channels = pCkFormat->ReadUint16(); |
822 |
|
|
823 |
/** @brief Destructor. |
/** @brief Destructor. |
824 |
* |
* |
825 |
* Removes RIFF chunks associated with this Sample and frees all |
* Frees all memory occupied by this sample. |
|
* memory occupied by this sample. |
|
826 |
*/ |
*/ |
827 |
Sample::~Sample() { |
Sample::~Sample() { |
828 |
RIFF::List* pParent = pWaveList->GetParent(); |
if (pCkData) |
829 |
pParent->DeleteSubChunk(pWaveList); |
pCkData->ReleaseChunkData(); |
830 |
|
if (pCkFormat) |
831 |
|
pCkFormat->ReleaseChunkData(); |
832 |
} |
} |
833 |
|
|
834 |
|
/** @brief Remove all RIFF chunks associated with this Sample object. |
835 |
|
* |
836 |
|
* See Storage::DeleteChunks() for details. |
837 |
|
*/ |
838 |
|
void Sample::DeleteChunks() { |
839 |
|
// handle base class |
840 |
|
Resource::DeleteChunks(); |
841 |
|
|
842 |
|
// handle own RIFF chunks |
843 |
|
if (pWaveList) { |
844 |
|
RIFF::List* pParent = pWaveList->GetParent(); |
845 |
|
pParent->DeleteSubChunk(pWaveList); |
846 |
|
pWaveList = NULL; |
847 |
|
} |
848 |
|
} |
849 |
|
|
850 |
/** |
/** |
851 |
* Make a deep copy of the Sample object given by @a orig (without the |
* Make a deep copy of the Sample object given by @a orig (without the |
852 |
* actual sample waveform data however) and assign it to this object. |
* actual sample waveform data however) and assign it to this object. |
885 |
Resize(orig->GetSize()); |
Resize(orig->GetSize()); |
886 |
char* buf = (char*) LoadSampleData(); |
char* buf = (char*) LoadSampleData(); |
887 |
Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now |
Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now |
888 |
const unsigned long restorePos = pOrig->pCkData->GetPos(); |
const file_offset_t restorePos = pOrig->pCkData->GetPos(); |
889 |
pOrig->SetPos(0); |
pOrig->SetPos(0); |
890 |
for (unsigned long todo = pOrig->GetSize(), i = 0; todo; ) { |
for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) { |
891 |
const int iReadAtOnce = 64*1024; |
const int iReadAtOnce = 64*1024; |
892 |
unsigned long n = (iReadAtOnce < todo) ? iReadAtOnce : todo; |
file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo; |
893 |
n = pOrig->Read(&buf[i], n); |
n = pOrig->Read(&buf[i], n); |
894 |
if (!n) break; |
if (!n) break; |
895 |
todo -= n; |
todo -= n; |
947 |
* @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 |
948 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
949 |
*/ |
*/ |
950 |
unsigned long Sample::GetSize() const { |
file_offset_t Sample::GetSize() const { |
951 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
952 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
953 |
} |
} |
974 |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
975 |
* other formats will fail! |
* other formats will fail! |
976 |
* |
* |
977 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param NewSize - new sample wave data size in sample points (must be |
978 |
* greater than zero) |
* greater than zero) |
979 |
* @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM |
980 |
* @throws Exception if \a iNewSize is less than 1 |
* @throws Exception if \a NewSize is less than 1 or unrealistic large |
981 |
* @see File::Save(), FrameSize, FormatTag |
* @see File::Save(), FrameSize, FormatTag |
982 |
*/ |
*/ |
983 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(file_offset_t NewSize) { |
984 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); |
if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); |
985 |
if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); |
if (NewSize < 1) throw Exception("Sample size must be at least one sample point"); |
986 |
const int iSizeInBytes = iNewSize * FrameSize; |
if ((NewSize >> 48) != 0) |
987 |
|
throw Exception("Unrealistic high DLS sample size detected"); |
988 |
|
const file_offset_t sizeInBytes = NewSize * FrameSize; |
989 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
990 |
if (pCkData) pCkData->Resize(iSizeInBytes); |
if (pCkData) pCkData->Resize(sizeInBytes); |
991 |
else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes); |
else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes); |
992 |
} |
} |
993 |
|
|
994 |
/** |
/** |
1007 |
* @throws Exception if no data RIFF chunk was created for the sample yet |
* @throws Exception if no data RIFF chunk was created for the sample yet |
1008 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
1009 |
*/ |
*/ |
1010 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
file_offset_t Sample::SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence) { |
1011 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
1012 |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
1013 |
unsigned long orderedBytes = SampleCount * FrameSize; |
file_offset_t orderedBytes = SampleCount * FrameSize; |
1014 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
file_offset_t result = pCkData->SetPos(orderedBytes, Whence); |
1015 |
return (result == orderedBytes) ? SampleCount |
return (result == orderedBytes) ? SampleCount |
1016 |
: result / FrameSize; |
: result / FrameSize; |
1017 |
} |
} |
1025 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
1026 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
1027 |
*/ |
*/ |
1028 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) { |
1029 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
1030 |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
1031 |
} |
} |
1045 |
* @throws Exception if current sample size is too small |
* @throws Exception if current sample size is too small |
1046 |
* @see LoadSampleData() |
* @see LoadSampleData() |
1047 |
*/ |
*/ |
1048 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) { |
1049 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
1050 |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
1051 |
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
1088 |
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) { |
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) { |
1089 |
pCkRegion = rgnList; |
pCkRegion = rgnList; |
1090 |
|
|
1091 |
// articulation informations |
// articulation information |
1092 |
RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH); |
RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH); |
1093 |
if (rgnh) { |
if (rgnh) { |
1094 |
|
rgnh->SetPos(0); |
1095 |
|
|
1096 |
rgnh->Read(&KeyRange, 2, 2); |
rgnh->Read(&KeyRange, 2, 2); |
1097 |
rgnh->Read(&VelocityRange, 2, 2); |
rgnh->Read(&VelocityRange, 2, 2); |
1098 |
FormatOptionFlags = rgnh->ReadUint16(); |
FormatOptionFlags = rgnh->ReadUint16(); |
1112 |
} |
} |
1113 |
SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE; |
SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE; |
1114 |
|
|
1115 |
// sample informations |
// sample information |
1116 |
RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK); |
RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK); |
1117 |
if (wlnk) { |
if (wlnk) { |
1118 |
|
wlnk->SetPos(0); |
1119 |
|
|
1120 |
WaveLinkOptionFlags = wlnk->ReadUint16(); |
WaveLinkOptionFlags = wlnk->ReadUint16(); |
1121 |
PhaseGroup = wlnk->ReadUint16(); |
PhaseGroup = wlnk->ReadUint16(); |
1122 |
Channel = wlnk->ReadUint32(); |
Channel = wlnk->ReadUint32(); |
1135 |
|
|
1136 |
/** @brief Destructor. |
/** @brief Destructor. |
1137 |
* |
* |
1138 |
* Removes RIFF chunks associated with this Region. |
* Intended to free up all memory occupied by this Region object. ATM this |
1139 |
|
* destructor implementation does nothing though. |
1140 |
*/ |
*/ |
1141 |
Region::~Region() { |
Region::~Region() { |
1142 |
RIFF::List* pParent = pCkRegion->GetParent(); |
} |
1143 |
pParent->DeleteSubChunk(pCkRegion); |
|
1144 |
|
/** @brief Remove all RIFF chunks associated with this Region object. |
1145 |
|
* |
1146 |
|
* See Storage::DeleteChunks() for details. |
1147 |
|
*/ |
1148 |
|
void Region::DeleteChunks() { |
1149 |
|
// handle base classes |
1150 |
|
Resource::DeleteChunks(); |
1151 |
|
Articulator::DeleteChunks(); |
1152 |
|
Sampler::DeleteChunks(); |
1153 |
|
|
1154 |
|
// handle own RIFF chunks |
1155 |
|
if (pCkRegion) { |
1156 |
|
RIFF::List* pParent = pCkRegion->GetParent(); |
1157 |
|
pParent->DeleteSubChunk(pCkRegion); |
1158 |
|
pCkRegion = NULL; |
1159 |
|
} |
1160 |
} |
} |
1161 |
|
|
1162 |
Sample* Region::GetSample() { |
Sample* Region::GetSample() { |
1163 |
if (pSample) return pSample; |
if (pSample) return pSample; |
1164 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
1165 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1166 |
Sample* sample = file->GetFirstSample(); |
Sample* sample = file->GetFirstSample(); |
1167 |
while (sample) { |
while (sample) { |
1168 |
if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample); |
if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample); |
1169 |
sample = file->GetNextSample(); |
sample = file->GetNextSample(); |
1170 |
} |
} |
1171 |
return NULL; |
return NULL; |
1338 |
midi_locale_t locale; |
midi_locale_t locale; |
1339 |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
1340 |
if (insh) { |
if (insh) { |
1341 |
|
insh->SetPos(0); |
1342 |
|
|
1343 |
Regions = insh->ReadUint32(); |
Regions = insh->ReadUint32(); |
1344 |
insh->Read(&locale, 2, 4); |
insh->Read(&locale, 2, 4); |
1345 |
} else { // 'insh' chunk missing |
} else { // 'insh' chunk missing |
1392 |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
1393 |
Region* pNewRegion = new Region(this, rgn); |
Region* pNewRegion = new Region(this, rgn); |
1394 |
pRegions->push_back(pNewRegion); |
pRegions->push_back(pNewRegion); |
1395 |
Regions = pRegions->size(); |
Regions = (uint32_t) pRegions->size(); |
1396 |
return pNewRegion; |
return pNewRegion; |
1397 |
} |
} |
1398 |
|
|
1410 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
1411 |
if (iter == pRegions->end()) return; |
if (iter == pRegions->end()) return; |
1412 |
pRegions->erase(iter); |
pRegions->erase(iter); |
1413 |
Regions = pRegions->size(); |
Regions = (uint32_t) pRegions->size(); |
1414 |
|
pRegion->DeleteChunks(); |
1415 |
delete pRegion; |
delete pRegion; |
1416 |
} |
} |
1417 |
|
|
1431 |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
1432 |
uint8_t* pData = (uint8_t*) insh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) insh->LoadChunkData(); |
1433 |
// update 'insh' chunk |
// update 'insh' chunk |
1434 |
Regions = (pRegions) ? pRegions->size() : 0; |
Regions = (pRegions) ? uint32_t(pRegions->size()) : 0; |
1435 |
midi_locale_t locale; |
midi_locale_t locale; |
1436 |
locale.instrument = MIDIProgram; |
locale.instrument = MIDIProgram; |
1437 |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
1456 |
|
|
1457 |
/** @brief Destructor. |
/** @brief Destructor. |
1458 |
* |
* |
1459 |
* Removes RIFF chunks associated with this Instrument and frees all |
* Frees all memory occupied by this instrument. |
|
* memory occupied by this instrument. |
|
1460 |
*/ |
*/ |
1461 |
Instrument::~Instrument() { |
Instrument::~Instrument() { |
1462 |
if (pRegions) { |
if (pRegions) { |
1468 |
} |
} |
1469 |
delete pRegions; |
delete pRegions; |
1470 |
} |
} |
|
// remove instrument's chunks |
|
|
RIFF::List* pParent = pCkInstrument->GetParent(); |
|
|
pParent->DeleteSubChunk(pCkInstrument); |
|
1471 |
} |
} |
1472 |
|
|
1473 |
|
/** @brief Remove all RIFF chunks associated with this Instrument object. |
1474 |
|
* |
1475 |
|
* See Storage::DeleteChunks() for details. |
1476 |
|
*/ |
1477 |
|
void Instrument::DeleteChunks() { |
1478 |
|
// handle base classes |
1479 |
|
Resource::DeleteChunks(); |
1480 |
|
Articulator::DeleteChunks(); |
1481 |
|
|
1482 |
|
// handle RIFF chunks of members |
1483 |
|
if (pRegions) { |
1484 |
|
RegionList::iterator it = pRegions->begin(); |
1485 |
|
RegionList::iterator end = pRegions->end(); |
1486 |
|
for (; it != end; ++it) |
1487 |
|
(*it)->DeleteChunks(); |
1488 |
|
} |
1489 |
|
|
1490 |
|
// handle own RIFF chunks |
1491 |
|
if (pCkInstrument) { |
1492 |
|
RIFF::List* pParent = pCkInstrument->GetParent(); |
1493 |
|
pParent->DeleteSubChunk(pCkInstrument); |
1494 |
|
pCkInstrument = NULL; |
1495 |
|
} |
1496 |
|
} |
1497 |
|
|
1498 |
void Instrument::CopyAssignCore(const Instrument* orig) { |
void Instrument::CopyAssignCore(const Instrument* orig) { |
1499 |
// handle base classes |
// handle base classes |
1500 |
Resource::CopyAssign(orig); |
Resource::CopyAssign(orig); |
1544 |
*/ |
*/ |
1545 |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
1546 |
pRIFF->SetByteOrder(RIFF::endian_little); |
pRIFF->SetByteOrder(RIFF::endian_little); |
1547 |
|
bOwningRiff = true; |
1548 |
pVersion = new version_t; |
pVersion = new version_t; |
1549 |
pVersion->major = 0; |
pVersion->major = 0; |
1550 |
pVersion->minor = 0; |
pVersion->minor = 0; |
1575 |
File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) { |
File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) { |
1576 |
if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object."); |
if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object."); |
1577 |
this->pRIFF = pRIFF; |
this->pRIFF = pRIFF; |
1578 |
|
bOwningRiff = false; |
1579 |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
1580 |
if (ckVersion) { |
if (ckVersion) { |
1581 |
|
ckVersion->SetPos(0); |
1582 |
|
|
1583 |
pVersion = new version_t; |
pVersion = new version_t; |
1584 |
ckVersion->Read(pVersion, 4, 2); |
ckVersion->Read(pVersion, 4, 2); |
1585 |
} |
} |
1587 |
|
|
1588 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1589 |
if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found."); |
if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found."); |
1590 |
|
colh->SetPos(0); |
1591 |
Instruments = colh->ReadUint32(); |
Instruments = colh->ReadUint32(); |
1592 |
|
|
1593 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1598 |
WavePoolHeaderSize = 8; |
WavePoolHeaderSize = 8; |
1599 |
b64BitWavePoolOffsets = false; |
b64BitWavePoolOffsets = false; |
1600 |
} else { |
} else { |
1601 |
|
ptbl->SetPos(0); |
1602 |
|
|
1603 |
WavePoolHeaderSize = ptbl->ReadUint32(); |
WavePoolHeaderSize = ptbl->ReadUint32(); |
1604 |
WavePoolCount = ptbl->ReadUint32(); |
WavePoolCount = ptbl->ReadUint32(); |
1605 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
pWavePoolTable = new uint32_t[WavePoolCount]; |
1612 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1613 |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
1614 |
pWavePoolTable[i] = ptbl->ReadUint32(); |
pWavePoolTable[i] = ptbl->ReadUint32(); |
1615 |
if (pWavePoolTable[i] & 0x80000000) |
//NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12) |
1616 |
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
//if (pWavePoolTable[i] & 0x80000000) |
1617 |
|
// throw DLS::Exception("Files larger than 2 GB not yet supported"); |
1618 |
} |
} |
1619 |
} else { // conventional 32 bit offsets |
} else { // conventional 32 bit offsets |
1620 |
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
1652 |
if (pVersion) delete pVersion; |
if (pVersion) delete pVersion; |
1653 |
for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) |
for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) |
1654 |
delete *i; |
delete *i; |
1655 |
|
if (bOwningRiff) |
1656 |
|
delete pRIFF; |
1657 |
} |
} |
1658 |
|
|
1659 |
Sample* File::GetFirstSample() { |
Sample* File::GetFirstSample() { |
1673 |
if (!pSamples) pSamples = new SampleList; |
if (!pSamples) pSamples = new SampleList; |
1674 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1675 |
if (wvpl) { |
if (wvpl) { |
1676 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
file_offset_t wvplFileOffset = wvpl->GetFilePos() - |
1677 |
|
wvpl->GetPos(); // should be zero, but just to be sure |
1678 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
RIFF::List* wave = wvpl->GetFirstSubList(); |
1679 |
while (wave) { |
while (wave) { |
1680 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1681 |
unsigned long waveFileOffset = wave->GetFilePos(); |
file_offset_t waveFileOffset = wave->GetFilePos() - |
1682 |
|
wave->GetPos(); // should be zero, but just to be sure |
1683 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1684 |
} |
} |
1685 |
wave = wvpl->GetNextSubList(); |
wave = wvpl->GetNextSubList(); |
1688 |
else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant) |
else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant) |
1689 |
RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL); |
RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL); |
1690 |
if (dwpl) { |
if (dwpl) { |
1691 |
unsigned long dwplFileOffset = dwpl->GetFilePos(); |
file_offset_t dwplFileOffset = dwpl->GetFilePos() - |
1692 |
|
dwpl->GetPos(); // should be zero, but just to be sure |
1693 |
RIFF::List* wave = dwpl->GetFirstSubList(); |
RIFF::List* wave = dwpl->GetFirstSubList(); |
1694 |
while (wave) { |
while (wave) { |
1695 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1696 |
unsigned long waveFileOffset = wave->GetFilePos(); |
file_offset_t waveFileOffset = wave->GetFilePos() - |
1697 |
|
wave->GetPos(); // should be zero, but just to be sure |
1698 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
1699 |
} |
} |
1700 |
wave = dwpl->GetNextSubList(); |
wave = dwpl->GetNextSubList(); |
1733 |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); |
1734 |
if (iter == pSamples->end()) return; |
if (iter == pSamples->end()) return; |
1735 |
pSamples->erase(iter); |
pSamples->erase(iter); |
1736 |
|
pSample->DeleteChunks(); |
1737 |
delete pSample; |
delete pSample; |
1738 |
} |
} |
1739 |
|
|
1793 |
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); |
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); |
1794 |
if (iter == pInstruments->end()) return; |
if (iter == pInstruments->end()) return; |
1795 |
pInstruments->erase(iter); |
pInstruments->erase(iter); |
1796 |
|
pInstrument->DeleteChunks(); |
1797 |
delete pInstrument; |
delete pInstrument; |
1798 |
} |
} |
1799 |
|
|
1800 |
/** |
/** |
1801 |
|
* Returns the underlying RIFF::File used for persistency of this DLS::File |
1802 |
|
* object. |
1803 |
|
*/ |
1804 |
|
RIFF::File* File::GetRiffFile() { |
1805 |
|
return pRIFF; |
1806 |
|
} |
1807 |
|
|
1808 |
|
/** |
1809 |
* Returns extension file of given index. Extension files are used |
* Returns extension file of given index. Extension files are used |
1810 |
* sometimes to circumvent the 2 GB file size limit of the RIFF format and |
* sometimes to circumvent the 2 GB file size limit of the RIFF format and |
1811 |
* of certain operating systems in general. In this case, instead of just |
* of certain operating systems in general. In this case, instead of just |
1870 |
} |
} |
1871 |
|
|
1872 |
// update 'colh' chunk |
// update 'colh' chunk |
1873 |
Instruments = (pInstruments) ? pInstruments->size() : 0; |
Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0; |
1874 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1875 |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
1876 |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
1897 |
} |
} |
1898 |
|
|
1899 |
// update 'ptbl' chunk |
// update 'ptbl' chunk |
1900 |
const int iSamples = (pSamples) ? pSamples->size() : 0; |
const int iSamples = (pSamples) ? int(pSamples->size()) : 0; |
1901 |
const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
1902 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1903 |
if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/); |
if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/); |
1904 |
const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; |
int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; |
1905 |
ptbl->Resize(iPtblSize); |
ptbl->Resize(iPtblSize); |
1906 |
pData = (uint8_t*) ptbl->LoadChunkData(); |
pData = (uint8_t*) ptbl->LoadChunkData(); |
1907 |
WavePoolCount = iSamples; |
WavePoolCount = iSamples; |
1929 |
__notify_progress(&subprogress, 1.0); // notify subprogress done |
__notify_progress(&subprogress, 1.0); // notify subprogress done |
1930 |
} |
} |
1931 |
|
|
1932 |
|
// if there are any extension files, gather which ones are regular |
1933 |
|
// extension files used as wave pool files (.gx00, .gx01, ... , .gx98) |
1934 |
|
// and which one is probably a convolution (GigaPulse) file (always to |
1935 |
|
// be saved as .gx99) |
1936 |
|
std::list<RIFF::File*> poolFiles; // < for (.gx00, .gx01, ... , .gx98) files |
1937 |
|
RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file |
1938 |
|
if (!ExtensionFiles.empty()) { |
1939 |
|
std::list<RIFF::File*>::iterator it = ExtensionFiles.begin(); |
1940 |
|
for (; it != ExtensionFiles.end(); ++it) { |
1941 |
|
//FIXME: the .gx99 file is always used by GSt for convolution |
1942 |
|
// data (GigaPulse); so we should better detect by subchunk |
1943 |
|
// whether the extension file is intended for convolution |
1944 |
|
// instead of checkking for a file name, because the latter does |
1945 |
|
// not work for saving new gigs created from scratch |
1946 |
|
const std::string oldName = (*it)->GetFileName(); |
1947 |
|
const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99"); |
1948 |
|
if (isGigaPulseFile) |
1949 |
|
pGigaPulseFile = *it; |
1950 |
|
else |
1951 |
|
poolFiles.push_back(*it); |
1952 |
|
} |
1953 |
|
} |
1954 |
|
|
1955 |
|
// update the 'xfil' chunk which describes all extension files (wave |
1956 |
|
// pool files) except the .gx99 file |
1957 |
|
if (!poolFiles.empty()) { |
1958 |
|
const int n = poolFiles.size(); |
1959 |
|
const int iHeaderSize = 4; |
1960 |
|
const int iEntrySize = 144; |
1961 |
|
|
1962 |
|
// make sure chunk exists, and with correct size |
1963 |
|
RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); |
1964 |
|
if (ckXfil) |
1965 |
|
ckXfil->Resize(iHeaderSize + n * iEntrySize); |
1966 |
|
else |
1967 |
|
ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize); |
1968 |
|
|
1969 |
|
uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData(); |
1970 |
|
|
1971 |
|
// re-assemble the chunk's content |
1972 |
|
store32(pData, n); |
1973 |
|
std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin(); |
1974 |
|
for (int i = 0, iOffset = 4; i < n; |
1975 |
|
++itExtFile, ++i, iOffset += iEntrySize) |
1976 |
|
{ |
1977 |
|
// update the filename string and 5 byte extension of each extension file |
1978 |
|
std::string file = lastPathComponent( |
1979 |
|
(*itExtFile)->GetFileName() |
1980 |
|
); |
1981 |
|
if (file.length() + 6 > 128) |
1982 |
|
throw Exception("Fatal error, extension filename length exceeds 122 byte maximum"); |
1983 |
|
uint8_t* pStrings = &pData[iOffset]; |
1984 |
|
memset(pStrings, 0, 128); |
1985 |
|
memcpy(pStrings, file.c_str(), file.length()); |
1986 |
|
pStrings += file.length() + 1; |
1987 |
|
std::string ext = file.substr(file.length()-5); |
1988 |
|
memcpy(pStrings, ext.c_str(), 5); |
1989 |
|
// update the dlsid of the extension file |
1990 |
|
uint8_t* pId = &pData[iOffset + 128]; |
1991 |
|
dlsid_t id; |
1992 |
|
RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID); |
1993 |
|
if (ckDLSID) { |
1994 |
|
ckDLSID->Read(&id.ulData1, 1, 4); |
1995 |
|
ckDLSID->Read(&id.usData2, 1, 2); |
1996 |
|
ckDLSID->Read(&id.usData3, 1, 2); |
1997 |
|
ckDLSID->Read(id.abData, 8, 1); |
1998 |
|
} else { |
1999 |
|
ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16); |
2000 |
|
Resource::GenerateDLSID(&id); |
2001 |
|
uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData(); |
2002 |
|
store32(&pData[0], id.ulData1); |
2003 |
|
store16(&pData[4], id.usData2); |
2004 |
|
store16(&pData[6], id.usData3); |
2005 |
|
memcpy(&pData[8], id.abData, 8); |
2006 |
|
} |
2007 |
|
store32(&pId[0], id.ulData1); |
2008 |
|
store16(&pId[4], id.usData2); |
2009 |
|
store16(&pId[6], id.usData3); |
2010 |
|
memcpy(&pId[8], id.abData, 8); |
2011 |
|
} |
2012 |
|
} else { |
2013 |
|
// in case there was a 'xfil' chunk, remove it |
2014 |
|
RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); |
2015 |
|
if (ckXfil) pRIFF->DeleteSubChunk(ckXfil); |
2016 |
|
} |
2017 |
|
|
2018 |
|
// update the 'doxf' chunk which describes a .gx99 extension file |
2019 |
|
// which contains convolution data (GigaPulse) |
2020 |
|
if (pGigaPulseFile) { |
2021 |
|
RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); |
2022 |
|
if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148); |
2023 |
|
|
2024 |
|
uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData(); |
2025 |
|
|
2026 |
|
// update the dlsid from the extension file |
2027 |
|
uint8_t* pId = &pData[132]; |
2028 |
|
RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID); |
2029 |
|
if (!ckDLSID) { //TODO: auto generate DLS ID if missing |
2030 |
|
throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk"); |
2031 |
|
} else { |
2032 |
|
dlsid_t id; |
2033 |
|
// read DLS ID from extension files's DLS ID chunk |
2034 |
|
uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData(); |
2035 |
|
id.ulData1 = load32(&pData[0]); |
2036 |
|
id.usData2 = load16(&pData[4]); |
2037 |
|
id.usData3 = load16(&pData[6]); |
2038 |
|
memcpy(id.abData, &pData[8], 8); |
2039 |
|
// store DLS ID to 'doxf' chunk |
2040 |
|
store32(&pId[0], id.ulData1); |
2041 |
|
store16(&pId[4], id.usData2); |
2042 |
|
store16(&pId[6], id.usData3); |
2043 |
|
memcpy(&pId[8], id.abData, 8); |
2044 |
|
} |
2045 |
|
} else { |
2046 |
|
// in case there was a 'doxf' chunk, remove it |
2047 |
|
RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); |
2048 |
|
if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf); |
2049 |
|
} |
2050 |
|
|
2051 |
|
// the RIFF file to be written might now been grown >= 4GB or might |
2052 |
|
// been shrunk < 4GB, so we might need to update the wave pool offset |
2053 |
|
// size and thus accordingly we would need to resize the wave pool |
2054 |
|
// chunk |
2055 |
|
const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize(); |
2056 |
|
const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file |
2057 |
|
poolFiles.size() > 0; // < 32 bit gig file where the hi 32 bits are used as extension file nr |
2058 |
|
if (b64BitWavePoolOffsets != bRequires64Bit) { |
2059 |
|
b64BitWavePoolOffsets = bRequires64Bit; |
2060 |
|
iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
2061 |
|
iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; |
2062 |
|
ptbl->Resize(iPtblSize); |
2063 |
|
} |
2064 |
|
|
2065 |
__notify_progress(pProgress, 1.0); // notify done |
__notify_progress(pProgress, 1.0); // notify done |
2066 |
} |
} |
2067 |
|
|
2080 |
* @param pProgress - optional: callback function for progress notification |
* @param pProgress - optional: callback function for progress notification |
2081 |
*/ |
*/ |
2082 |
void File::Save(const String& Path, progress_t* pProgress) { |
void File::Save(const String& Path, progress_t* pProgress) { |
2083 |
|
// calculate number of tasks to notify progress appropriately |
2084 |
|
const size_t nExtFiles = ExtensionFiles.size(); |
2085 |
|
const float tasks = 2.f + nExtFiles; |
2086 |
|
|
2087 |
|
// save extension files (if required) |
2088 |
|
if (!ExtensionFiles.empty()) { |
2089 |
|
// for assembling path of extension files to be saved to |
2090 |
|
const std::string baseName = pathWithoutExtension(Path); |
2091 |
|
// save the individual extension files |
2092 |
|
std::list<RIFF::File*>::iterator it = ExtensionFiles.begin(); |
2093 |
|
for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) { |
2094 |
|
// divide local progress into subprogress |
2095 |
|
progress_t subprogress; |
2096 |
|
__divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files |
2097 |
|
//FIXME: the .gx99 file is always used by GSt for convolution |
2098 |
|
// data (GigaPulse); so we should better detect by subchunk |
2099 |
|
// whether the extension file is intended for convolution |
2100 |
|
// instead of checkking for a file name, because the latter does |
2101 |
|
// not work for saving new gigs created from scratch |
2102 |
|
const std::string oldName = (*it)->GetFileName(); |
2103 |
|
const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99"); |
2104 |
|
std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1); |
2105 |
|
std::string newPath = baseName + ext; |
2106 |
|
// save extension file to its new location |
2107 |
|
(*it)->Save(newPath, &subprogress); |
2108 |
|
} |
2109 |
|
} |
2110 |
|
|
2111 |
{ |
{ |
2112 |
// divide local progress into subprogress |
// divide local progress into subprogress |
2113 |
progress_t subprogress; |
progress_t subprogress; |
2114 |
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress |
__divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2115 |
// do the actual work |
// do the actual work |
2116 |
UpdateChunks(&subprogress); |
UpdateChunks(&subprogress); |
|
|
|
2117 |
} |
} |
2118 |
{ |
{ |
2119 |
// divide local progress into subprogress |
// divide local progress into subprogress |
2120 |
progress_t subprogress; |
progress_t subprogress; |
2121 |
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress |
__divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2122 |
// do the actual work |
// do the actual work |
2123 |
pRIFF->Save(Path, &subprogress); |
pRIFF->Save(Path, &subprogress); |
2124 |
} |
} |
2133 |
* have at the end of the saving process. |
* have at the end of the saving process. |
2134 |
* |
* |
2135 |
* @param pProgress - optional: callback function for progress notification |
* @param pProgress - optional: callback function for progress notification |
2136 |
* @throws RIFF::Exception if any kind of IO error occured |
* @throws RIFF::Exception if any kind of IO error occurred |
2137 |
* @throws DLS::Exception if any kind of DLS specific error occured |
* @throws DLS::Exception if any kind of DLS specific error occurred |
2138 |
*/ |
*/ |
2139 |
void File::Save(progress_t* pProgress) { |
void File::Save(progress_t* pProgress) { |
2140 |
|
// calculate number of tasks to notify progress appropriately |
2141 |
|
const size_t nExtFiles = ExtensionFiles.size(); |
2142 |
|
const float tasks = 2.f + nExtFiles; |
2143 |
|
|
2144 |
|
// save extension files (if required) |
2145 |
|
if (!ExtensionFiles.empty()) { |
2146 |
|
std::list<RIFF::File*>::iterator it = ExtensionFiles.begin(); |
2147 |
|
for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) { |
2148 |
|
// divide local progress into subprogress |
2149 |
|
progress_t subprogress; |
2150 |
|
__divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files |
2151 |
|
// save extension file |
2152 |
|
(*it)->Save(&subprogress); |
2153 |
|
} |
2154 |
|
} |
2155 |
|
|
2156 |
{ |
{ |
2157 |
// divide local progress into subprogress |
// divide local progress into subprogress |
2158 |
progress_t subprogress; |
progress_t subprogress; |
2159 |
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 50% of total progress |
__divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2160 |
// do the actual work |
// do the actual work |
2161 |
UpdateChunks(&subprogress); |
UpdateChunks(&subprogress); |
2162 |
} |
} |
2163 |
{ |
{ |
2164 |
// divide local progress into subprogress |
// divide local progress into subprogress |
2165 |
progress_t subprogress; |
progress_t subprogress; |
2166 |
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 50% of total progress |
__divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2167 |
// do the actual work |
// do the actual work |
2168 |
pRIFF->Save(&subprogress); |
pRIFF->Save(&subprogress); |
2169 |
} |
} |
2219 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
2220 |
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
2221 |
// check if 'ptbl' chunk is large enough |
// check if 'ptbl' chunk is large enough |
2222 |
WavePoolCount = (pSamples) ? pSamples->size() : 0; |
WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0; |
2223 |
const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; |
const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; |
2224 |
if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); |
if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); |
2225 |
// save the 'ptbl' chunk's current read/write position |
// save the 'ptbl' chunk's current read/write position |
2226 |
unsigned long ulOriginalPos = ptbl->GetPos(); |
file_offset_t ullOriginalPos = ptbl->GetPos(); |
2227 |
// update headers |
// update headers |
2228 |
ptbl->SetPos(0); |
ptbl->SetPos(0); |
2229 |
uint32_t tmp = WavePoolHeaderSize; |
uint32_t tmp = WavePoolHeaderSize; |
2246 |
} |
} |
2247 |
} |
} |
2248 |
// restore 'ptbl' chunk's original read/write position |
// restore 'ptbl' chunk's original read/write position |
2249 |
ptbl->SetPos(ulOriginalPos); |
ptbl->SetPos(ullOriginalPos); |
2250 |
} |
} |
2251 |
|
|
2252 |
/** |
/** |
2255 |
* exists already. |
* exists already. |
2256 |
*/ |
*/ |
2257 |
void File::__UpdateWavePoolTable() { |
void File::__UpdateWavePoolTable() { |
2258 |
WavePoolCount = (pSamples) ? pSamples->size() : 0; |
WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0; |
2259 |
// resize wave pool table arrays |
// resize wave pool table arrays |
2260 |
if (pWavePoolTable) delete[] pWavePoolTable; |
if (pWavePoolTable) delete[] pWavePoolTable; |
2261 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
2262 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
pWavePoolTable = new uint32_t[WavePoolCount]; |
2263 |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
2264 |
if (!pSamples) return; |
if (!pSamples) return; |
2265 |
// update offsets int wave pool table |
// update offsets in wave pool table |
2266 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
2267 |
uint64_t wvplFileOffset = wvpl->GetFilePos(); |
uint64_t wvplFileOffset = wvpl->GetFilePos() - |
2268 |
if (b64BitWavePoolOffsets) { |
wvpl->GetPos(); // mandatory, since position might have changed |
2269 |
|
if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ... |
2270 |
SampleList::iterator iter = pSamples->begin(); |
SampleList::iterator iter = pSamples->begin(); |
2271 |
SampleList::iterator end = pSamples->end(); |
SampleList::iterator end = pSamples->end(); |
2272 |
for (int i = 0 ; iter != end ; ++iter, i++) { |
for (int i = 0 ; iter != end ; ++iter, i++) { |
2273 |
uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; |
uint64_t _64BitOffset = |
2274 |
(*iter)->ulWavePoolOffset = _64BitOffset; |
(*iter)->pWaveList->GetFilePos() - |
2275 |
pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); |
(*iter)->pWaveList->GetPos() - // should be zero, but just to be sure |
2276 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
wvplFileOffset - |
2277 |
} |
LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); |
2278 |
} else { // conventional 32 bit offsets |
(*iter)->ullWavePoolOffset = _64BitOffset; |
|
SampleList::iterator iter = pSamples->begin(); |
|
|
SampleList::iterator end = pSamples->end(); |
|
|
for (int i = 0 ; iter != end ; ++iter, i++) { |
|
|
uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; |
|
|
(*iter)->ulWavePoolOffset = _64BitOffset; |
|
2279 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
2280 |
} |
} |
2281 |
|
} else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ... |
2282 |
|
if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ... |
2283 |
|
SampleList::iterator iter = pSamples->begin(); |
2284 |
|
SampleList::iterator end = pSamples->end(); |
2285 |
|
for (int i = 0 ; iter != end ; ++iter, i++) { |
2286 |
|
uint64_t _64BitOffset = |
2287 |
|
(*iter)->pWaveList->GetFilePos() - |
2288 |
|
(*iter)->pWaveList->GetPos() - // should be zero, but just to be sure |
2289 |
|
wvplFileOffset - |
2290 |
|
LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); |
2291 |
|
(*iter)->ullWavePoolOffset = _64BitOffset; |
2292 |
|
pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); |
2293 |
|
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
2294 |
|
} |
2295 |
|
} else { // 32 bit offsets with extension files (GigaStudio legacy support) ... |
2296 |
|
// the main gig and the extension files may contain wave data |
2297 |
|
std::vector<RIFF::File*> poolFiles; |
2298 |
|
poolFiles.push_back(pRIFF); |
2299 |
|
poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end()); |
2300 |
|
|
2301 |
|
RIFF::File* pCurPoolFile = NULL; |
2302 |
|
int fileNo = 0; |
2303 |
|
int waveOffset = 0; |
2304 |
|
SampleList::iterator iter = pSamples->begin(); |
2305 |
|
SampleList::iterator end = pSamples->end(); |
2306 |
|
for (int i = 0 ; iter != end ; ++iter, i++) { |
2307 |
|
RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile(); |
2308 |
|
// if this sample is located in the same pool file as the |
2309 |
|
// last we reuse the previously computed fileNo and waveOffset |
2310 |
|
if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ... |
2311 |
|
pCurPoolFile = pPoolFile; |
2312 |
|
|
2313 |
|
std::vector<RIFF::File*>::iterator sIter; |
2314 |
|
sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile); |
2315 |
|
if (sIter != poolFiles.end()) |
2316 |
|
fileNo = std::distance(poolFiles.begin(), sIter); |
2317 |
|
else |
2318 |
|
throw DLS::Exception("Fatal error, unknown pool file"); |
2319 |
|
|
2320 |
|
RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL); |
2321 |
|
if (!extWvpl) |
2322 |
|
throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk"); |
2323 |
|
waveOffset = |
2324 |
|
extWvpl->GetFilePos() - |
2325 |
|
extWvpl->GetPos() + // mandatory, since position might have changed |
2326 |
|
LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize()); |
2327 |
|
} |
2328 |
|
uint64_t _64BitOffset = |
2329 |
|
(*iter)->pWaveList->GetFilePos() - |
2330 |
|
(*iter)->pWaveList->GetPos() - // should be zero, but just to be sure |
2331 |
|
waveOffset; |
2332 |
|
// pWavePoolTableHi stores file number when extension files are in use |
2333 |
|
pWavePoolTableHi[i] = (uint32_t) fileNo; |
2334 |
|
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
2335 |
|
(*iter)->ullWavePoolOffset = _64BitOffset; |
2336 |
|
} |
2337 |
|
} |
2338 |
} |
} |
2339 |
} |
} |
2340 |
|
|
2341 |
|
|
|
|
|
2342 |
// *************** Exception *************** |
// *************** Exception *************** |
2343 |
// * |
// * |
2344 |
|
|
2345 |
Exception::Exception(String Message) : RIFF::Exception(Message) { |
Exception::Exception() : RIFF::Exception() { |
2346 |
|
} |
2347 |
|
|
2348 |
|
Exception::Exception(String format, ...) : RIFF::Exception() { |
2349 |
|
va_list arg; |
2350 |
|
va_start(arg, format); |
2351 |
|
Message = assemble(format, arg); |
2352 |
|
va_end(arg); |
2353 |
|
} |
2354 |
|
|
2355 |
|
Exception::Exception(String format, va_list arg) : RIFF::Exception() { |
2356 |
|
Message = assemble(format, arg); |
2357 |
} |
} |
2358 |
|
|
2359 |
void Exception::PrintMessage() { |
void Exception::PrintMessage() { |