1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2007 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 * |
51 |
|
|
52 |
namespace gig { |
namespace gig { |
53 |
|
|
|
// *************** dimension_def_t *************** |
|
|
// * |
|
|
|
|
|
dimension_def_t& dimension_def_t::operator=(const dimension_def_t& arg) { |
|
|
dimension = arg.dimension; |
|
|
bits = arg.bits; |
|
|
zones = arg.zones; |
|
|
split_type = arg.split_type; |
|
|
ranges = arg.ranges; |
|
|
zone_size = arg.zone_size; |
|
|
if (ranges) { |
|
|
ranges = new range_t[zones]; |
|
|
for (int i = 0; i < zones; i++) |
|
|
ranges[i] = arg.ranges[i]; |
|
|
} |
|
|
return *this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
54 |
// *************** progress_t *************** |
// *************** progress_t *************** |
55 |
// * |
// * |
56 |
|
|
111 |
return x & 0x800000 ? x - 0x1000000 : x; |
return x & 0x800000 ? x - 0x1000000 : x; |
112 |
} |
} |
113 |
|
|
114 |
|
inline void store24(unsigned char* pDst, int x) |
115 |
|
{ |
116 |
|
pDst[0] = x; |
117 |
|
pDst[1] = x >> 8; |
118 |
|
pDst[2] = x >> 16; |
119 |
|
} |
120 |
|
|
121 |
void Decompress16(int compressionmode, const unsigned char* params, |
void Decompress16(int compressionmode, const unsigned char* params, |
122 |
int srcStep, int dstStep, |
int srcStep, int dstStep, |
123 |
const unsigned char* pSrc, int16_t* pDst, |
const unsigned char* pSrc, int16_t* pDst, |
157 |
} |
} |
158 |
|
|
159 |
void Decompress24(int compressionmode, const unsigned char* params, |
void Decompress24(int compressionmode, const unsigned char* params, |
160 |
int dstStep, const unsigned char* pSrc, int16_t* pDst, |
int dstStep, const unsigned char* pSrc, uint8_t* pDst, |
161 |
unsigned long currentframeoffset, |
unsigned long currentframeoffset, |
162 |
unsigned long copysamples, int truncatedBits) |
unsigned long copysamples, int truncatedBits) |
163 |
{ |
{ |
|
// Note: The 24 bits are truncated to 16 bits for now. |
|
|
|
|
164 |
int y, dy, ddy, dddy; |
int y, dy, ddy, dddy; |
|
const int shift = 8 - truncatedBits; |
|
165 |
|
|
166 |
#define GET_PARAMS(params) \ |
#define GET_PARAMS(params) \ |
167 |
y = get24(params); \ |
y = get24(params); \ |
177 |
|
|
178 |
#define COPY_ONE(x) \ |
#define COPY_ONE(x) \ |
179 |
SKIP_ONE(x); \ |
SKIP_ONE(x); \ |
180 |
*pDst = y >> shift; \ |
store24(pDst, y << truncatedBits); \ |
181 |
pDst += dstStep |
pDst += dstStep |
182 |
|
|
183 |
switch (compressionmode) { |
switch (compressionmode) { |
184 |
case 2: // 24 bit uncompressed |
case 2: // 24 bit uncompressed |
185 |
pSrc += currentframeoffset * 3; |
pSrc += currentframeoffset * 3; |
186 |
while (copysamples) { |
while (copysamples) { |
187 |
*pDst = get24(pSrc) >> shift; |
store24(pDst, get24(pSrc) << truncatedBits); |
188 |
pDst += dstStep; |
pDst += dstStep; |
189 |
pSrc += 3; |
pSrc += 3; |
190 |
copysamples--; |
copysamples--; |
254 |
} |
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
// *************** Other Internal functions *************** |
259 |
|
// * |
260 |
|
|
261 |
|
static split_type_t __resolveSplitType(dimension_t dimension) { |
262 |
|
return ( |
263 |
|
dimension == dimension_layer || |
264 |
|
dimension == dimension_samplechannel || |
265 |
|
dimension == dimension_releasetrigger || |
266 |
|
dimension == dimension_keyboard || |
267 |
|
dimension == dimension_roundrobin || |
268 |
|
dimension == dimension_random || |
269 |
|
dimension == dimension_smartmidi || |
270 |
|
dimension == dimension_roundrobinkeyboard |
271 |
|
) ? split_type_bit : split_type_normal; |
272 |
|
} |
273 |
|
|
274 |
|
static int __resolveZoneSize(dimension_def_t& dimension_definition) { |
275 |
|
return (dimension_definition.split_type == split_type_normal) |
276 |
|
? int(128.0 / dimension_definition.zones) : 0; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
// *************** Sample *************** |
// *************** Sample *************** |
282 |
// * |
// * |
283 |
|
|
303 |
* is located, 0 otherwise |
* is located, 0 otherwise |
304 |
*/ |
*/ |
305 |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
306 |
|
static const DLS::Info::FixedStringLength fixedStringLengths[] = { |
307 |
|
{ CHUNK_ID_INAM, 64 }, |
308 |
|
{ 0, 0 } |
309 |
|
}; |
310 |
|
pInfo->FixedStringLengths = fixedStringLengths; |
311 |
Instances++; |
Instances++; |
312 |
FileNo = fileNo; |
FileNo = fileNo; |
313 |
|
|
314 |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
315 |
if (pCk3gix) { |
if (pCk3gix) { |
316 |
SampleGroup = pCk3gix->ReadInt16(); |
uint16_t iSampleGroup = pCk3gix->ReadInt16(); |
317 |
|
pGroup = pFile->GetGroup(iSampleGroup); |
318 |
} else { // '3gix' chunk missing |
} else { // '3gix' chunk missing |
319 |
// use default value(s) |
// by default assigned to that mandatory "Default Group" |
320 |
SampleGroup = 0; |
pGroup = pFile->GetGroup(0); |
321 |
} |
} |
322 |
|
|
323 |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
341 |
// use default values |
// use default values |
342 |
Manufacturer = 0; |
Manufacturer = 0; |
343 |
Product = 0; |
Product = 0; |
344 |
SamplePeriod = 1 / SamplesPerSecond; |
SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); |
345 |
MIDIUnityNote = 64; |
MIDIUnityNote = 64; |
346 |
FineTune = 0; |
FineTune = 0; |
347 |
|
SMPTEFormat = smpte_format_no_offset; |
348 |
SMPTEOffset = 0; |
SMPTEOffset = 0; |
349 |
Loops = 0; |
Loops = 0; |
350 |
LoopID = 0; |
LoopID = 0; |
351 |
|
LoopType = loop_type_normal; |
352 |
LoopStart = 0; |
LoopStart = 0; |
353 |
LoopEnd = 0; |
LoopEnd = 0; |
354 |
LoopFraction = 0; |
LoopFraction = 0; |
384 |
} |
} |
385 |
FrameOffset = 0; // just for streaming compressed samples |
FrameOffset = 0; // just for streaming compressed samples |
386 |
|
|
387 |
LoopSize = LoopEnd - LoopStart; |
LoopSize = LoopEnd - LoopStart + 1; |
388 |
} |
} |
389 |
|
|
390 |
/** |
/** |
394 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
395 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
396 |
* |
* |
397 |
* @throws DLS::Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
* @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
398 |
* was provided yet |
* was provided yet |
399 |
* @throws gig::Exception if there is any invalid sample setting |
* @throws gig::Exception if there is any invalid sample setting |
400 |
*/ |
*/ |
404 |
|
|
405 |
// make sure 'smpl' chunk exists |
// make sure 'smpl' chunk exists |
406 |
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
407 |
if (!pCkSmpl) pCkSmpl = pWaveList->AddSubChunk(CHUNK_ID_SMPL, 60); |
if (!pCkSmpl) { |
408 |
|
pCkSmpl = pWaveList->AddSubChunk(CHUNK_ID_SMPL, 60); |
409 |
|
memset(pCkSmpl->LoadChunkData(), 0, 60); |
410 |
|
} |
411 |
// update 'smpl' chunk |
// update 'smpl' chunk |
412 |
uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData(); |
413 |
SamplePeriod = 1 / SamplesPerSecond; |
SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); |
414 |
memcpy(&pData[0], &Manufacturer, 4); |
store32(&pData[0], Manufacturer); |
415 |
memcpy(&pData[4], &Product, 4); |
store32(&pData[4], Product); |
416 |
memcpy(&pData[8], &SamplePeriod, 4); |
store32(&pData[8], SamplePeriod); |
417 |
memcpy(&pData[12], &MIDIUnityNote, 4); |
store32(&pData[12], MIDIUnityNote); |
418 |
memcpy(&pData[16], &FineTune, 4); |
store32(&pData[16], FineTune); |
419 |
memcpy(&pData[20], &SMPTEFormat, 4); |
store32(&pData[20], SMPTEFormat); |
420 |
memcpy(&pData[24], &SMPTEOffset, 4); |
store32(&pData[24], SMPTEOffset); |
421 |
memcpy(&pData[28], &Loops, 4); |
store32(&pData[28], Loops); |
422 |
|
|
423 |
// we skip 'manufByt' for now (4 bytes) |
// we skip 'manufByt' for now (4 bytes) |
424 |
|
|
425 |
memcpy(&pData[36], &LoopID, 4); |
store32(&pData[36], LoopID); |
426 |
memcpy(&pData[40], &LoopType, 4); |
store32(&pData[40], LoopType); |
427 |
memcpy(&pData[44], &LoopStart, 4); |
store32(&pData[44], LoopStart); |
428 |
memcpy(&pData[48], &LoopEnd, 4); |
store32(&pData[48], LoopEnd); |
429 |
memcpy(&pData[52], &LoopFraction, 4); |
store32(&pData[52], LoopFraction); |
430 |
memcpy(&pData[56], &LoopPlayCount, 4); |
store32(&pData[56], LoopPlayCount); |
431 |
|
|
432 |
// make sure '3gix' chunk exists |
// make sure '3gix' chunk exists |
433 |
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
434 |
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
435 |
|
// determine appropriate sample group index (to be stored in chunk) |
436 |
|
uint16_t iSampleGroup = 0; // 0 refers to default sample group |
437 |
|
File* pFile = static_cast<File*>(pParent); |
438 |
|
if (pFile->pGroups) { |
439 |
|
std::list<Group*>::iterator iter = pFile->pGroups->begin(); |
440 |
|
std::list<Group*>::iterator end = pFile->pGroups->end(); |
441 |
|
for (int i = 0; iter != end; i++, iter++) { |
442 |
|
if (*iter == pGroup) { |
443 |
|
iSampleGroup = i; |
444 |
|
break; // found |
445 |
|
} |
446 |
|
} |
447 |
|
} |
448 |
// update '3gix' chunk |
// update '3gix' chunk |
449 |
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
450 |
memcpy(&pData[0], &SampleGroup, 2); |
store16(&pData[0], iSampleGroup); |
451 |
} |
} |
452 |
|
|
453 |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
668 |
* enlarged samples before calling File::Save() as this might exceed the |
* enlarged samples before calling File::Save() as this might exceed the |
669 |
* current sample's boundary! |
* current sample's boundary! |
670 |
* |
* |
671 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
672 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
673 |
* other formats will fail! |
* other formats will fail! |
674 |
* |
* |
675 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
676 |
* greater than zero) |
* greater than zero) |
677 |
* @throws DLS::Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
678 |
* or if \a iNewSize is less than 1 |
* or if \a iNewSize is less than 1 |
679 |
* @throws gig::Exception if existing sample is compressed |
* @throws gig::Exception if existing sample is compressed |
680 |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
774 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
775 |
* @param pPlaybackState will be used to store and reload the playback |
* @param pPlaybackState will be used to store and reload the playback |
776 |
* state for the next ReadAndLoop() call |
* state for the next ReadAndLoop() call |
777 |
|
* @param pDimRgn dimension region with looping information |
778 |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
779 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
780 |
* @see CreateDecompressionBuffer() |
* @see CreateDecompressionBuffer() |
781 |
*/ |
*/ |
782 |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer) { |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, |
783 |
|
DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { |
784 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
785 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
786 |
|
|
787 |
SetPos(pPlaybackState->position); // recover position from the last time |
SetPos(pPlaybackState->position); // recover position from the last time |
788 |
|
|
789 |
if (this->Loops && GetPos() <= this->LoopEnd) { // honor looping if there are loop points defined |
if (pDimRgn->SampleLoops) { // honor looping if there are loop points defined |
790 |
|
|
791 |
switch (this->LoopType) { |
const DLS::sample_loop_t& loop = pDimRgn->pSampleLoops[0]; |
792 |
|
const uint32_t loopEnd = loop.LoopStart + loop.LoopLength; |
793 |
|
|
794 |
case loop_type_bidirectional: { //TODO: not tested yet! |
if (GetPos() <= loopEnd) { |
795 |
do { |
switch (loop.LoopType) { |
|
// if not endless loop check if max. number of loop cycles have been passed |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
|
|
|
|
|
if (!pPlaybackState->reverse) { // forward playback |
|
|
do { |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
|
|
samplestoread -= readsamples; |
|
|
totalreadsamples += readsamples; |
|
|
if (readsamples == samplestoloopend) { |
|
|
pPlaybackState->reverse = true; |
|
|
break; |
|
|
} |
|
|
} while (samplestoread && readsamples); |
|
|
} |
|
|
else { // backward playback |
|
796 |
|
|
797 |
// as we can only read forward from disk, we have to |
case loop_type_bidirectional: { //TODO: not tested yet! |
798 |
// determine the end position within the loop first, |
do { |
799 |
// read forward from that 'end' and finally after |
// if not endless loop check if max. number of loop cycles have been passed |
800 |
// reading, swap all sample frames so it reflects |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
801 |
// backward playback |
|
802 |
|
if (!pPlaybackState->reverse) { // forward playback |
803 |
unsigned long swapareastart = totalreadsamples; |
do { |
804 |
unsigned long loopoffset = GetPos() - this->LoopStart; |
samplestoloopend = loopEnd - GetPos(); |
805 |
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
806 |
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
samplestoread -= readsamples; |
807 |
|
totalreadsamples += readsamples; |
808 |
SetPos(reverseplaybackend); |
if (readsamples == samplestoloopend) { |
809 |
|
pPlaybackState->reverse = true; |
810 |
// read samples for backward playback |
break; |
811 |
do { |
} |
812 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
} while (samplestoread && readsamples); |
813 |
samplestoreadinloop -= readsamples; |
} |
814 |
samplestoread -= readsamples; |
else { // backward playback |
|
totalreadsamples += readsamples; |
|
|
} while (samplestoreadinloop && readsamples); |
|
815 |
|
|
816 |
SetPos(reverseplaybackend); // pretend we really read backwards |
// as we can only read forward from disk, we have to |
817 |
|
// determine the end position within the loop first, |
818 |
|
// read forward from that 'end' and finally after |
819 |
|
// reading, swap all sample frames so it reflects |
820 |
|
// backward playback |
821 |
|
|
822 |
|
unsigned long swapareastart = totalreadsamples; |
823 |
|
unsigned long loopoffset = GetPos() - loop.LoopStart; |
824 |
|
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
825 |
|
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
826 |
|
|
827 |
|
SetPos(reverseplaybackend); |
828 |
|
|
829 |
|
// read samples for backward playback |
830 |
|
do { |
831 |
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
832 |
|
samplestoreadinloop -= readsamples; |
833 |
|
samplestoread -= readsamples; |
834 |
|
totalreadsamples += readsamples; |
835 |
|
} while (samplestoreadinloop && readsamples); |
836 |
|
|
837 |
|
SetPos(reverseplaybackend); // pretend we really read backwards |
838 |
|
|
839 |
|
if (reverseplaybackend == loop.LoopStart) { |
840 |
|
pPlaybackState->loop_cycles_left--; |
841 |
|
pPlaybackState->reverse = false; |
842 |
|
} |
843 |
|
|
844 |
if (reverseplaybackend == this->LoopStart) { |
// reverse the sample frames for backward playback |
845 |
pPlaybackState->loop_cycles_left--; |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
|
pPlaybackState->reverse = false; |
|
846 |
} |
} |
847 |
|
} while (samplestoread && readsamples); |
848 |
|
break; |
849 |
|
} |
850 |
|
|
851 |
// reverse the sample frames for backward playback |
case loop_type_backward: { // TODO: not tested yet! |
852 |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
// forward playback (not entered the loop yet) |
853 |
} |
if (!pPlaybackState->reverse) do { |
854 |
} while (samplestoread && readsamples); |
samplestoloopend = loopEnd - GetPos(); |
855 |
break; |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
856 |
} |
samplestoread -= readsamples; |
857 |
|
totalreadsamples += readsamples; |
858 |
case loop_type_backward: { // TODO: not tested yet! |
if (readsamples == samplestoloopend) { |
859 |
// forward playback (not entered the loop yet) |
pPlaybackState->reverse = true; |
860 |
if (!pPlaybackState->reverse) do { |
break; |
861 |
samplestoloopend = this->LoopEnd - GetPos(); |
} |
862 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
} while (samplestoread && readsamples); |
|
samplestoread -= readsamples; |
|
|
totalreadsamples += readsamples; |
|
|
if (readsamples == samplestoloopend) { |
|
|
pPlaybackState->reverse = true; |
|
|
break; |
|
|
} |
|
|
} while (samplestoread && readsamples); |
|
863 |
|
|
864 |
if (!samplestoread) break; |
if (!samplestoread) break; |
865 |
|
|
866 |
// as we can only read forward from disk, we have to |
// as we can only read forward from disk, we have to |
867 |
// determine the end position within the loop first, |
// determine the end position within the loop first, |
868 |
// read forward from that 'end' and finally after |
// read forward from that 'end' and finally after |
869 |
// reading, swap all sample frames so it reflects |
// reading, swap all sample frames so it reflects |
870 |
// backward playback |
// backward playback |
871 |
|
|
872 |
unsigned long swapareastart = totalreadsamples; |
unsigned long swapareastart = totalreadsamples; |
873 |
unsigned long loopoffset = GetPos() - this->LoopStart; |
unsigned long loopoffset = GetPos() - loop.LoopStart; |
874 |
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * LoopSize - loopoffset) |
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset) |
875 |
: samplestoread; |
: samplestoread; |
876 |
unsigned long reverseplaybackend = this->LoopStart + Abs((loopoffset - samplestoreadinloop) % this->LoopSize); |
unsigned long reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); |
877 |
|
|
878 |
SetPos(reverseplaybackend); |
SetPos(reverseplaybackend); |
879 |
|
|
880 |
// read samples for backward playback |
// read samples for backward playback |
881 |
do { |
do { |
882 |
// if not endless loop check if max. number of loop cycles have been passed |
// if not endless loop check if max. number of loop cycles have been passed |
883 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
884 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = loopEnd - GetPos(); |
885 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
886 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
887 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
888 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
889 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
890 |
pPlaybackState->loop_cycles_left--; |
pPlaybackState->loop_cycles_left--; |
891 |
SetPos(this->LoopStart); |
SetPos(loop.LoopStart); |
892 |
} |
} |
893 |
} while (samplestoreadinloop && readsamples); |
} while (samplestoreadinloop && readsamples); |
894 |
|
|
895 |
SetPos(reverseplaybackend); // pretend we really read backwards |
SetPos(reverseplaybackend); // pretend we really read backwards |
896 |
|
|
897 |
// reverse the sample frames for backward playback |
// reverse the sample frames for backward playback |
898 |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
899 |
break; |
break; |
900 |
} |
} |
901 |
|
|
902 |
default: case loop_type_normal: { |
default: case loop_type_normal: { |
903 |
do { |
do { |
904 |
// if not endless loop check if max. number of loop cycles have been passed |
// if not endless loop check if max. number of loop cycles have been passed |
905 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
906 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = loopEnd - GetPos(); |
907 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
908 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
909 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
910 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
911 |
pPlaybackState->loop_cycles_left--; |
pPlaybackState->loop_cycles_left--; |
912 |
SetPos(this->LoopStart); |
SetPos(loop.LoopStart); |
913 |
} |
} |
914 |
} while (samplestoread && readsamples); |
} while (samplestoread && readsamples); |
915 |
break; |
break; |
916 |
|
} |
917 |
} |
} |
918 |
} |
} |
919 |
} |
} |
943 |
* have to use an external decompression buffer for <b>EACH</b> |
* have to use an external decompression buffer for <b>EACH</b> |
944 |
* streaming thread to avoid race conditions and crashes! |
* streaming thread to avoid race conditions and crashes! |
945 |
* |
* |
946 |
|
* For 16 bit samples, the data in the buffer will be int16_t |
947 |
|
* (using native endianness). For 24 bit, the buffer will |
948 |
|
* contain three bytes per sample, little-endian. |
949 |
|
* |
950 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
951 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
952 |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
957 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
958 |
if (!Compressed) { |
if (!Compressed) { |
959 |
if (BitDepth == 24) { |
if (BitDepth == 24) { |
960 |
// 24 bit sample. For now just truncate to 16 bit. |
return pCkData->Read(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
|
unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); |
|
|
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
|
|
if (Channels == 2) { // Stereo |
|
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); |
|
|
pSrc++; |
|
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
|
|
*pDst++ = get16(pSrc); |
|
|
pSrc += 3; |
|
|
} |
|
|
return (pDst - static_cast<int16_t*>(pBuffer)) >> 1; |
|
|
} |
|
|
else { // Mono |
|
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1); |
|
|
pSrc++; |
|
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
|
|
*pDst++ = get16(pSrc); |
|
|
pSrc += 3; |
|
|
} |
|
|
return pDst - static_cast<int16_t*>(pBuffer); |
|
|
} |
|
961 |
} |
} |
962 |
else { // 16 bit |
else { // 16 bit |
963 |
// (pCkData->Read does endian correction) |
// (pCkData->Read does endian correction) |
987 |
|
|
988 |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
989 |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
990 |
|
uint8_t* pDst24 = static_cast<uint8_t*>(pBuffer); |
991 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
992 |
|
|
993 |
while (remainingsamples && remainingbytes) { |
while (remainingsamples && remainingbytes) { |
1069 |
const unsigned char* const param_r = pSrc; |
const unsigned char* const param_r = pSrc; |
1070 |
if (mode_r != 2) pSrc += 12; |
if (mode_r != 2) pSrc += 12; |
1071 |
|
|
1072 |
Decompress24(mode_l, param_l, 2, pSrc, pDst, |
Decompress24(mode_l, param_l, 6, pSrc, pDst24, |
1073 |
skipsamples, copysamples, TruncatedBits); |
skipsamples, copysamples, TruncatedBits); |
1074 |
Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, |
Decompress24(mode_r, param_r, 6, pSrc + rightChannelOffset, pDst24 + 3, |
1075 |
skipsamples, copysamples, TruncatedBits); |
skipsamples, copysamples, TruncatedBits); |
1076 |
pDst += copysamples << 1; |
pDst24 += copysamples * 6; |
1077 |
} |
} |
1078 |
else { // Mono |
else { // Mono |
1079 |
Decompress24(mode_l, param_l, 1, pSrc, pDst, |
Decompress24(mode_l, param_l, 3, pSrc, pDst24, |
1080 |
skipsamples, copysamples, TruncatedBits); |
skipsamples, copysamples, TruncatedBits); |
1081 |
pDst += copysamples; |
pDst24 += copysamples * 3; |
1082 |
} |
} |
1083 |
} |
} |
1084 |
else { // 16 bit |
else { // 16 bit |
1185 |
} |
} |
1186 |
} |
} |
1187 |
|
|
1188 |
|
/** |
1189 |
|
* Returns pointer to the Group this Sample belongs to. In the .gig |
1190 |
|
* format a sample always belongs to one group. If it wasn't explicitly |
1191 |
|
* assigned to a certain group, it will be automatically assigned to a |
1192 |
|
* default group. |
1193 |
|
* |
1194 |
|
* @returns Sample's Group (never NULL) |
1195 |
|
*/ |
1196 |
|
Group* Sample::GetGroup() const { |
1197 |
|
return pGroup; |
1198 |
|
} |
1199 |
|
|
1200 |
Sample::~Sample() { |
Sample::~Sample() { |
1201 |
Instances--; |
Instances--; |
1202 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
1226 |
|
|
1227 |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
1228 |
if (_3ewa) { // if '3ewa' chunk exists |
if (_3ewa) { // if '3ewa' chunk exists |
1229 |
_3ewa->ReadInt32(); // unknown, always 0x0000008C ? |
_3ewa->ReadInt32(); // unknown, always == chunk size ? |
1230 |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1231 |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1232 |
_3ewa->ReadInt16(); // unknown |
_3ewa->ReadInt16(); // unknown |
1371 |
if (lfo3ctrl & 0x40) // bit 6 |
if (lfo3ctrl & 0x40) // bit 6 |
1372 |
VCFType = vcf_type_lowpassturbo; |
VCFType = vcf_type_lowpassturbo; |
1373 |
} |
} |
1374 |
|
if (_3ewa->RemainingBytes() >= 8) { |
1375 |
|
_3ewa->Read(DimensionUpperLimits, 1, 8); |
1376 |
|
} else { |
1377 |
|
memset(DimensionUpperLimits, 0, 8); |
1378 |
|
} |
1379 |
} else { // '3ewa' chunk does not exist yet |
} else { // '3ewa' chunk does not exist yet |
1380 |
// use default values |
// use default values |
1381 |
LFO3Frequency = 1.0; |
LFO3Frequency = 1.0; |
1456 |
VCFVelocityDynamicRange = 0x04; |
VCFVelocityDynamicRange = 0x04; |
1457 |
VCFVelocityCurve = curve_type_linear; |
VCFVelocityCurve = curve_type_linear; |
1458 |
VCFType = vcf_type_lowpass; |
VCFType = vcf_type_lowpass; |
1459 |
|
memset(DimensionUpperLimits, 0, 8); |
1460 |
} |
} |
1461 |
|
|
1462 |
pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, |
pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, |
1491 |
VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0); |
VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0); |
1492 |
|
|
1493 |
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1494 |
|
VelocityTable = 0; |
1495 |
} |
} |
1496 |
|
|
1497 |
/** |
/** |
1512 |
|
|
1513 |
// update '3ewa' chunk with DimensionRegion's current settings |
// update '3ewa' chunk with DimensionRegion's current settings |
1514 |
|
|
1515 |
const uint32_t unknown = 0x0000008C; // unknown, always 0x0000008C ? |
const uint32_t chunksize = _3ewa->GetNewSize(); |
1516 |
memcpy(&pData[0], &unknown, 4); |
store32(&pData[0], chunksize); // unknown, always chunk size? |
1517 |
|
|
1518 |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
1519 |
memcpy(&pData[4], &lfo3freq, 4); |
store32(&pData[4], lfo3freq); |
1520 |
|
|
1521 |
const int32_t eg3attack = (int32_t) GIG_EXP_ENCODE(EG3Attack); |
const int32_t eg3attack = (int32_t) GIG_EXP_ENCODE(EG3Attack); |
1522 |
memcpy(&pData[4], &eg3attack, 4); |
store32(&pData[8], eg3attack); |
1523 |
|
|
1524 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1525 |
|
|
1526 |
memcpy(&pData[10], &LFO1InternalDepth, 2); |
store16(&pData[14], LFO1InternalDepth); |
1527 |
|
|
1528 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1529 |
|
|
1530 |
memcpy(&pData[14], &LFO3InternalDepth, 2); |
store16(&pData[18], LFO3InternalDepth); |
1531 |
|
|
1532 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1533 |
|
|
1534 |
memcpy(&pData[18], &LFO1ControlDepth, 2); |
store16(&pData[22], LFO1ControlDepth); |
1535 |
|
|
1536 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1537 |
|
|
1538 |
memcpy(&pData[22], &LFO3ControlDepth, 2); |
store16(&pData[26], LFO3ControlDepth); |
1539 |
|
|
1540 |
const int32_t eg1attack = (int32_t) GIG_EXP_ENCODE(EG1Attack); |
const int32_t eg1attack = (int32_t) GIG_EXP_ENCODE(EG1Attack); |
1541 |
memcpy(&pData[24], &eg1attack, 4); |
store32(&pData[28], eg1attack); |
1542 |
|
|
1543 |
const int32_t eg1decay1 = (int32_t) GIG_EXP_ENCODE(EG1Decay1); |
const int32_t eg1decay1 = (int32_t) GIG_EXP_ENCODE(EG1Decay1); |
1544 |
memcpy(&pData[28], &eg1decay1, 4); |
store32(&pData[32], eg1decay1); |
1545 |
|
|
1546 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1547 |
|
|
1548 |
memcpy(&pData[34], &EG1Sustain, 2); |
store16(&pData[38], EG1Sustain); |
1549 |
|
|
1550 |
const int32_t eg1release = (int32_t) GIG_EXP_ENCODE(EG1Release); |
const int32_t eg1release = (int32_t) GIG_EXP_ENCODE(EG1Release); |
1551 |
memcpy(&pData[36], &eg1release, 4); |
store32(&pData[40], eg1release); |
1552 |
|
|
1553 |
const uint8_t eg1ctl = (uint8_t) EncodeLeverageController(EG1Controller); |
const uint8_t eg1ctl = (uint8_t) EncodeLeverageController(EG1Controller); |
1554 |
memcpy(&pData[40], &eg1ctl, 1); |
pData[44] = eg1ctl; |
1555 |
|
|
1556 |
const uint8_t eg1ctrloptions = |
const uint8_t eg1ctrloptions = |
1557 |
(EG1ControllerInvert) ? 0x01 : 0x00 | |
(EG1ControllerInvert) ? 0x01 : 0x00 | |
1558 |
GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG1ControllerAttackInfluence) | |
GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG1ControllerAttackInfluence) | |
1559 |
GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG1ControllerDecayInfluence) | |
GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG1ControllerDecayInfluence) | |
1560 |
GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG1ControllerReleaseInfluence); |
GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG1ControllerReleaseInfluence); |
1561 |
memcpy(&pData[41], &eg1ctrloptions, 1); |
pData[45] = eg1ctrloptions; |
1562 |
|
|
1563 |
const uint8_t eg2ctl = (uint8_t) EncodeLeverageController(EG2Controller); |
const uint8_t eg2ctl = (uint8_t) EncodeLeverageController(EG2Controller); |
1564 |
memcpy(&pData[42], &eg2ctl, 1); |
pData[46] = eg2ctl; |
1565 |
|
|
1566 |
const uint8_t eg2ctrloptions = |
const uint8_t eg2ctrloptions = |
1567 |
(EG2ControllerInvert) ? 0x01 : 0x00 | |
(EG2ControllerInvert) ? 0x01 : 0x00 | |
1568 |
GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG2ControllerAttackInfluence) | |
GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG2ControllerAttackInfluence) | |
1569 |
GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG2ControllerDecayInfluence) | |
GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG2ControllerDecayInfluence) | |
1570 |
GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG2ControllerReleaseInfluence); |
GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG2ControllerReleaseInfluence); |
1571 |
memcpy(&pData[43], &eg2ctrloptions, 1); |
pData[47] = eg2ctrloptions; |
1572 |
|
|
1573 |
const int32_t lfo1freq = (int32_t) GIG_EXP_ENCODE(LFO1Frequency); |
const int32_t lfo1freq = (int32_t) GIG_EXP_ENCODE(LFO1Frequency); |
1574 |
memcpy(&pData[44], &lfo1freq, 4); |
store32(&pData[48], lfo1freq); |
1575 |
|
|
1576 |
const int32_t eg2attack = (int32_t) GIG_EXP_ENCODE(EG2Attack); |
const int32_t eg2attack = (int32_t) GIG_EXP_ENCODE(EG2Attack); |
1577 |
memcpy(&pData[48], &eg2attack, 4); |
store32(&pData[52], eg2attack); |
1578 |
|
|
1579 |
const int32_t eg2decay1 = (int32_t) GIG_EXP_ENCODE(EG2Decay1); |
const int32_t eg2decay1 = (int32_t) GIG_EXP_ENCODE(EG2Decay1); |
1580 |
memcpy(&pData[52], &eg2decay1, 4); |
store32(&pData[56], eg2decay1); |
1581 |
|
|
1582 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1583 |
|
|
1584 |
memcpy(&pData[58], &EG2Sustain, 2); |
store16(&pData[62], EG2Sustain); |
1585 |
|
|
1586 |
const int32_t eg2release = (int32_t) GIG_EXP_ENCODE(EG2Release); |
const int32_t eg2release = (int32_t) GIG_EXP_ENCODE(EG2Release); |
1587 |
memcpy(&pData[60], &eg2release, 4); |
store32(&pData[64], eg2release); |
1588 |
|
|
1589 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1590 |
|
|
1591 |
memcpy(&pData[66], &LFO2ControlDepth, 2); |
store16(&pData[70], LFO2ControlDepth); |
1592 |
|
|
1593 |
const int32_t lfo2freq = (int32_t) GIG_EXP_ENCODE(LFO2Frequency); |
const int32_t lfo2freq = (int32_t) GIG_EXP_ENCODE(LFO2Frequency); |
1594 |
memcpy(&pData[68], &lfo2freq, 4); |
store32(&pData[72], lfo2freq); |
1595 |
|
|
1596 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1597 |
|
|
1598 |
memcpy(&pData[72], &LFO2InternalDepth, 2); |
store16(&pData[78], LFO2InternalDepth); |
1599 |
|
|
1600 |
const int32_t eg1decay2 = (int32_t) (EG1InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG1Decay2); |
const int32_t eg1decay2 = (int32_t) (EG1InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG1Decay2); |
1601 |
memcpy(&pData[74], &eg1decay2, 4); |
store32(&pData[80], eg1decay2); |
1602 |
|
|
1603 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1604 |
|
|
1605 |
memcpy(&pData[80], &EG1PreAttack, 2); |
store16(&pData[86], EG1PreAttack); |
1606 |
|
|
1607 |
const int32_t eg2decay2 = (int32_t) (EG2InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG2Decay2); |
const int32_t eg2decay2 = (int32_t) (EG2InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG2Decay2); |
1608 |
memcpy(&pData[82], &eg2decay2, 4); |
store32(&pData[88], eg2decay2); |
1609 |
|
|
1610 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1611 |
|
|
1612 |
memcpy(&pData[88], &EG2PreAttack, 2); |
store16(&pData[94], EG2PreAttack); |
1613 |
|
|
1614 |
{ |
{ |
1615 |
if (VelocityResponseDepth > 4) throw Exception("VelocityResponseDepth must be between 0 and 4"); |
if (VelocityResponseDepth > 4) throw Exception("VelocityResponseDepth must be between 0 and 4"); |
1627 |
default: |
default: |
1628 |
throw Exception("Could not update DimensionRegion's chunk, unknown VelocityResponseCurve selected"); |
throw Exception("Could not update DimensionRegion's chunk, unknown VelocityResponseCurve selected"); |
1629 |
} |
} |
1630 |
memcpy(&pData[90], &velocityresponse, 1); |
pData[96] = velocityresponse; |
1631 |
} |
} |
1632 |
|
|
1633 |
{ |
{ |
1646 |
default: |
default: |
1647 |
throw Exception("Could not update DimensionRegion's chunk, unknown ReleaseVelocityResponseCurve selected"); |
throw Exception("Could not update DimensionRegion's chunk, unknown ReleaseVelocityResponseCurve selected"); |
1648 |
} |
} |
1649 |
memcpy(&pData[91], &releasevelocityresponse, 1); |
pData[97] = releasevelocityresponse; |
1650 |
} |
} |
1651 |
|
|
1652 |
memcpy(&pData[92], &VelocityResponseCurveScaling, 1); |
pData[98] = VelocityResponseCurveScaling; |
1653 |
|
|
1654 |
memcpy(&pData[93], &AttenuationControllerThreshold, 1); |
pData[99] = AttenuationControllerThreshold; |
1655 |
|
|
1656 |
// next 4 bytes unknown |
// next 4 bytes unknown |
1657 |
|
|
1658 |
memcpy(&pData[98], &SampleStartOffset, 2); |
store16(&pData[104], SampleStartOffset); |
1659 |
|
|
1660 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1661 |
|
|
1674 |
default: |
default: |
1675 |
throw Exception("Could not update DimensionRegion's chunk, unknown DimensionBypass selected"); |
throw Exception("Could not update DimensionRegion's chunk, unknown DimensionBypass selected"); |
1676 |
} |
} |
1677 |
memcpy(&pData[102], &pitchTrackDimensionBypass, 1); |
pData[108] = pitchTrackDimensionBypass; |
1678 |
} |
} |
1679 |
|
|
1680 |
const uint8_t pan = (Pan >= 0) ? Pan : ((-Pan) + 63); // signed 8 bit -> signed 7 bit |
const uint8_t pan = (Pan >= 0) ? Pan : ((-Pan) + 63); // signed 8 bit -> signed 7 bit |
1681 |
memcpy(&pData[103], &pan, 1); |
pData[109] = pan; |
1682 |
|
|
1683 |
const uint8_t selfmask = (SelfMask) ? 0x01 : 0x00; |
const uint8_t selfmask = (SelfMask) ? 0x01 : 0x00; |
1684 |
memcpy(&pData[104], &selfmask, 1); |
pData[110] = selfmask; |
1685 |
|
|
1686 |
// next byte unknown |
// next byte unknown |
1687 |
|
|
1690 |
if (LFO3Sync) lfo3ctrl |= 0x20; // bit 5 |
if (LFO3Sync) lfo3ctrl |= 0x20; // bit 5 |
1691 |
if (InvertAttenuationController) lfo3ctrl |= 0x80; // bit 7 |
if (InvertAttenuationController) lfo3ctrl |= 0x80; // bit 7 |
1692 |
if (VCFType == vcf_type_lowpassturbo) lfo3ctrl |= 0x40; // bit 6 |
if (VCFType == vcf_type_lowpassturbo) lfo3ctrl |= 0x40; // bit 6 |
1693 |
memcpy(&pData[106], &lfo3ctrl, 1); |
pData[112] = lfo3ctrl; |
1694 |
} |
} |
1695 |
|
|
1696 |
const uint8_t attenctl = EncodeLeverageController(AttenuationController); |
const uint8_t attenctl = EncodeLeverageController(AttenuationController); |
1697 |
memcpy(&pData[107], &attenctl, 1); |
pData[113] = attenctl; |
1698 |
|
|
1699 |
{ |
{ |
1700 |
uint8_t lfo2ctrl = LFO2Controller & 0x07; // lower 3 bits |
uint8_t lfo2ctrl = LFO2Controller & 0x07; // lower 3 bits |
1701 |
if (LFO2FlipPhase) lfo2ctrl |= 0x80; // bit 7 |
if (LFO2FlipPhase) lfo2ctrl |= 0x80; // bit 7 |
1702 |
if (LFO2Sync) lfo2ctrl |= 0x20; // bit 5 |
if (LFO2Sync) lfo2ctrl |= 0x20; // bit 5 |
1703 |
if (VCFResonanceController != vcf_res_ctrl_none) lfo2ctrl |= 0x40; // bit 6 |
if (VCFResonanceController != vcf_res_ctrl_none) lfo2ctrl |= 0x40; // bit 6 |
1704 |
memcpy(&pData[108], &lfo2ctrl, 1); |
pData[114] = lfo2ctrl; |
1705 |
} |
} |
1706 |
|
|
1707 |
{ |
{ |
1710 |
if (LFO1Sync) lfo1ctrl |= 0x40; // bit 6 |
if (LFO1Sync) lfo1ctrl |= 0x40; // bit 6 |
1711 |
if (VCFResonanceController != vcf_res_ctrl_none) |
if (VCFResonanceController != vcf_res_ctrl_none) |
1712 |
lfo1ctrl |= GIG_VCF_RESONANCE_CTRL_ENCODE(VCFResonanceController); |
lfo1ctrl |= GIG_VCF_RESONANCE_CTRL_ENCODE(VCFResonanceController); |
1713 |
memcpy(&pData[109], &lfo1ctrl, 1); |
pData[115] = lfo1ctrl; |
1714 |
} |
} |
1715 |
|
|
1716 |
const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth |
const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth |
1717 |
: uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */ |
: uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */ |
1718 |
memcpy(&pData[110], &eg3depth, 1); |
pData[116] = eg3depth; |
1719 |
|
|
1720 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1721 |
|
|
1722 |
const uint8_t channeloffset = ChannelOffset * 4; |
const uint8_t channeloffset = ChannelOffset * 4; |
1723 |
memcpy(&pData[113], &channeloffset, 1); |
pData[120] = channeloffset; |
1724 |
|
|
1725 |
{ |
{ |
1726 |
uint8_t regoptions = 0; |
uint8_t regoptions = 0; |
1727 |
if (MSDecode) regoptions |= 0x01; // bit 0 |
if (MSDecode) regoptions |= 0x01; // bit 0 |
1728 |
if (SustainDefeat) regoptions |= 0x02; // bit 1 |
if (SustainDefeat) regoptions |= 0x02; // bit 1 |
1729 |
memcpy(&pData[114], ®options, 1); |
pData[121] = regoptions; |
1730 |
} |
} |
1731 |
|
|
1732 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1733 |
|
|
1734 |
memcpy(&pData[117], &VelocityUpperLimit, 1); |
pData[124] = VelocityUpperLimit; |
1735 |
|
|
1736 |
// next 3 bytes unknown |
// next 3 bytes unknown |
1737 |
|
|
1738 |
memcpy(&pData[121], &ReleaseTriggerDecay, 1); |
pData[128] = ReleaseTriggerDecay; |
1739 |
|
|
1740 |
// next 2 bytes unknown |
// next 2 bytes unknown |
1741 |
|
|
1742 |
const uint8_t eg1hold = (EG1Hold) ? 0x80 : 0x00; // bit 7 |
const uint8_t eg1hold = (EG1Hold) ? 0x80 : 0x00; // bit 7 |
1743 |
memcpy(&pData[124], &eg1hold, 1); |
pData[131] = eg1hold; |
1744 |
|
|
1745 |
const uint8_t vcfcutoff = (VCFEnabled) ? 0x80 : 0x00 | /* bit 7 */ |
const uint8_t vcfcutoff = (VCFEnabled) ? 0x80 : 0x00 | /* bit 7 */ |
1746 |
(VCFCutoff) ? 0x7f : 0x00; /* lower 7 bits */ |
(VCFCutoff & 0x7f); /* lower 7 bits */ |
1747 |
memcpy(&pData[125], &vcfcutoff, 1); |
pData[132] = vcfcutoff; |
1748 |
|
|
1749 |
memcpy(&pData[126], &VCFCutoffController, 1); |
pData[133] = VCFCutoffController; |
1750 |
|
|
1751 |
const uint8_t vcfvelscale = (VCFCutoffControllerInvert) ? 0x80 : 0x00 | /* bit 7 */ |
const uint8_t vcfvelscale = (VCFCutoffControllerInvert) ? 0x80 : 0x00 | /* bit 7 */ |
1752 |
(VCFVelocityScale) ? 0x7f : 0x00; /* lower 7 bits */ |
(VCFVelocityScale & 0x7f); /* lower 7 bits */ |
1753 |
memcpy(&pData[127], &vcfvelscale, 1); |
pData[134] = vcfvelscale; |
1754 |
|
|
1755 |
// next byte unknown |
// next byte unknown |
1756 |
|
|
1757 |
const uint8_t vcfresonance = (VCFResonanceDynamic) ? 0x00 : 0x80 | /* bit 7 */ |
const uint8_t vcfresonance = (VCFResonanceDynamic) ? 0x00 : 0x80 | /* bit 7 */ |
1758 |
(VCFResonance) ? 0x7f : 0x00; /* lower 7 bits */ |
(VCFResonance & 0x7f); /* lower 7 bits */ |
1759 |
memcpy(&pData[129], &vcfresonance, 1); |
pData[136] = vcfresonance; |
1760 |
|
|
1761 |
const uint8_t vcfbreakpoint = (VCFKeyboardTracking) ? 0x80 : 0x00 | /* bit 7 */ |
const uint8_t vcfbreakpoint = (VCFKeyboardTracking) ? 0x80 : 0x00 | /* bit 7 */ |
1762 |
(VCFKeyboardTrackingBreakpoint) ? 0x7f : 0x00; /* lower 7 bits */ |
(VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */ |
1763 |
memcpy(&pData[130], &vcfbreakpoint, 1); |
pData[137] = vcfbreakpoint; |
1764 |
|
|
1765 |
const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 | |
const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 | |
1766 |
VCFVelocityCurve * 5; |
VCFVelocityCurve * 5; |
1767 |
memcpy(&pData[131], &vcfvelocity, 1); |
pData[138] = vcfvelocity; |
1768 |
|
|
1769 |
const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType; |
const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType; |
1770 |
memcpy(&pData[132], &vcftype, 1); |
pData[139] = vcftype; |
1771 |
|
|
1772 |
|
if (chunksize >= 148) { |
1773 |
|
memcpy(&pData[140], DimensionUpperLimits, 8); |
1774 |
|
} |
1775 |
} |
} |
1776 |
|
|
1777 |
// get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet |
// get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet |
1996 |
default: |
default: |
1997 |
throw gig::Exception("leverage controller number is not supported by the gig format"); |
throw gig::Exception("leverage controller number is not supported by the gig format"); |
1998 |
} |
} |
1999 |
|
break; |
2000 |
default: |
default: |
2001 |
throw gig::Exception("Unknown leverage controller type."); |
throw gig::Exception("Unknown leverage controller type."); |
2002 |
} |
} |
2016 |
delete pVelocityTables; |
delete pVelocityTables; |
2017 |
pVelocityTables = NULL; |
pVelocityTables = NULL; |
2018 |
} |
} |
2019 |
|
if (VelocityTable) delete[] VelocityTable; |
2020 |
} |
} |
2021 |
|
|
2022 |
/** |
/** |
2141 |
pDimensionDefinitions[i].bits = 0; |
pDimensionDefinitions[i].bits = 0; |
2142 |
pDimensionDefinitions[i].zones = 0; |
pDimensionDefinitions[i].zones = 0; |
2143 |
pDimensionDefinitions[i].split_type = split_type_bit; |
pDimensionDefinitions[i].split_type = split_type_bit; |
|
pDimensionDefinitions[i].ranges = NULL; |
|
2144 |
pDimensionDefinitions[i].zone_size = 0; |
pDimensionDefinitions[i].zone_size = 0; |
2145 |
} |
} |
2146 |
else { // active dimension |
else { // active dimension |
2147 |
pDimensionDefinitions[i].dimension = dimension; |
pDimensionDefinitions[i].dimension = dimension; |
2148 |
pDimensionDefinitions[i].bits = bits; |
pDimensionDefinitions[i].bits = bits; |
2149 |
pDimensionDefinitions[i].zones = zones ? zones : 0x01 << bits; // = pow(2,bits) |
pDimensionDefinitions[i].zones = zones ? zones : 0x01 << bits; // = pow(2,bits) |
2150 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
pDimensionDefinitions[i].split_type = __resolveSplitType(dimension); |
2151 |
dimension == dimension_samplechannel || |
pDimensionDefinitions[i].zone_size = __resolveZoneSize(pDimensionDefinitions[i]); |
|
dimension == dimension_releasetrigger || |
|
|
dimension == dimension_roundrobin || |
|
|
dimension == dimension_random) ? split_type_bit |
|
|
: split_type_normal; |
|
|
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
|
|
pDimensionDefinitions[i].zone_size = |
|
|
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones |
|
|
: 0; |
|
2152 |
Dimensions++; |
Dimensions++; |
2153 |
|
|
2154 |
// if this is a layer dimension, remember the amount of layers |
// if this is a layer dimension, remember the amount of layers |
2158 |
} |
} |
2159 |
for (int i = dimensionBits ; i < 8 ; i++) pDimensionDefinitions[i].bits = 0; |
for (int i = dimensionBits ; i < 8 ; i++) pDimensionDefinitions[i].bits = 0; |
2160 |
|
|
2161 |
// check velocity dimension (if there is one) for custom defined zone ranges |
// if there's a velocity dimension and custom velocity zone splits are used, |
2162 |
for (uint i = 0; i < Dimensions; i++) { |
// update the VelocityTables in the dimension regions |
2163 |
dimension_def_t* pDimDef = pDimensionDefinitions + i; |
UpdateVelocityTable(); |
|
if (pDimDef->dimension == dimension_velocity) { |
|
|
if (pDimensionRegions[0]->VelocityUpperLimit == 0) { |
|
|
// no custom defined ranges |
|
|
pDimDef->split_type = split_type_normal; |
|
|
pDimDef->ranges = NULL; |
|
|
} |
|
|
else { // custom defined ranges |
|
|
pDimDef->split_type = split_type_customvelocity; |
|
|
pDimDef->ranges = new range_t[pDimDef->zones]; |
|
|
UpdateVelocityTable(pDimDef); |
|
|
} |
|
|
} |
|
|
} |
|
2164 |
|
|
2165 |
// jump to start of the wave pool indices (if not already there) |
// jump to start of the wave pool indices (if not already there) |
|
File* file = (File*) GetParent()->GetParent(); |
|
2166 |
if (file->pVersion && file->pVersion->major == 3) |
if (file->pVersion && file->pVersion->major == 3) |
2167 |
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
2168 |
else |
else |
2171 |
// load sample references |
// load sample references |
2172 |
for (uint i = 0; i < DimensionRegions; i++) { |
for (uint i = 0; i < DimensionRegions; i++) { |
2173 |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
2174 |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
2175 |
|
} |
2176 |
|
GetSample(); // load global region sample reference |
2177 |
|
} else { |
2178 |
|
DimensionRegions = 0; |
2179 |
|
for (int i = 0 ; i < 8 ; i++) { |
2180 |
|
pDimensionDefinitions[i].dimension = dimension_none; |
2181 |
|
pDimensionDefinitions[i].bits = 0; |
2182 |
|
pDimensionDefinitions[i].zones = 0; |
2183 |
} |
} |
2184 |
} |
} |
2185 |
|
|
2203 |
* @throws gig::Exception if samples cannot be dereferenced |
* @throws gig::Exception if samples cannot be dereferenced |
2204 |
*/ |
*/ |
2205 |
void Region::UpdateChunks() { |
void Region::UpdateChunks() { |
2206 |
|
// in the gig format we don't care about the Region's sample reference |
2207 |
|
// but we still have to provide some existing one to not corrupt the |
2208 |
|
// file, so to avoid the latter we simply always assign the sample of |
2209 |
|
// the first dimension region of this region |
2210 |
|
pSample = pDimensionRegions[0]->pSample; |
2211 |
|
|
2212 |
// first update base class's chunks |
// first update base class's chunks |
2213 |
DLS::Region::UpdateChunks(); |
DLS::Region::UpdateChunks(); |
2214 |
|
|
2226 |
if (!_3lnk) { |
if (!_3lnk) { |
2227 |
const int _3lnkChunkSize = (pFile->pVersion && pFile->pVersion->major == 3) ? 1092 : 172; |
const int _3lnkChunkSize = (pFile->pVersion && pFile->pVersion->major == 3) ? 1092 : 172; |
2228 |
_3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); |
_3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); |
2229 |
|
memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); |
2230 |
|
|
2231 |
|
// move 3prg to last position |
2232 |
|
pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0); |
2233 |
} |
} |
2234 |
|
|
2235 |
// update dimension definitions in '3lnk' chunk |
// update dimension definitions in '3lnk' chunk |
2236 |
uint8_t* pData = (uint8_t*) _3lnk->LoadChunkData(); |
uint8_t* pData = (uint8_t*) _3lnk->LoadChunkData(); |
2237 |
|
store32(&pData[0], DimensionRegions); |
2238 |
for (int i = 0; i < iMaxDimensions; i++) { |
for (int i = 0; i < iMaxDimensions; i++) { |
2239 |
pData[i * 8] = (uint8_t) pDimensionDefinitions[i].dimension; |
pData[4 + i * 8] = (uint8_t) pDimensionDefinitions[i].dimension; |
2240 |
pData[i * 8 + 1] = pDimensionDefinitions[i].bits; |
pData[5 + i * 8] = pDimensionDefinitions[i].bits; |
2241 |
// next 2 bytes unknown |
// next 2 bytes unknown |
2242 |
pData[i * 8 + 4] = pDimensionDefinitions[i].zones; |
pData[8 + i * 8] = pDimensionDefinitions[i].zones; |
2243 |
// next 3 bytes unknown |
// next 3 bytes unknown |
2244 |
} |
} |
2245 |
|
|
2259 |
} |
} |
2260 |
if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample"); |
if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample"); |
2261 |
} |
} |
2262 |
memcpy(&pData[iWavePoolOffset + i * 4], &iWaveIndex, 4); |
store32(&pData[iWavePoolOffset + i * 4], iWaveIndex); |
2263 |
} |
} |
2264 |
} |
} |
2265 |
|
|
2279 |
} |
} |
2280 |
} |
} |
2281 |
|
|
2282 |
void Region::UpdateVelocityTable(dimension_def_t* pDimDef) { |
void Region::UpdateVelocityTable() { |
2283 |
// get dimension's index |
// get velocity dimension's index |
2284 |
int iDimensionNr = -1; |
int veldim = -1; |
2285 |
for (int i = 0; i < Dimensions; i++) { |
for (int i = 0 ; i < Dimensions ; i++) { |
2286 |
if (&pDimensionDefinitions[i] == pDimDef) { |
if (pDimensionDefinitions[i].dimension == gig::dimension_velocity) { |
2287 |
iDimensionNr = i; |
veldim = i; |
2288 |
break; |
break; |
2289 |
} |
} |
2290 |
} |
} |
2291 |
if (iDimensionNr < 0) throw gig::Exception("Invalid dimension_def_t pointer"); |
if (veldim == -1) return; |
2292 |
|
|
2293 |
|
int step = 1; |
2294 |
|
for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; |
2295 |
|
int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; |
2296 |
|
int end = step * pDimensionDefinitions[veldim].zones; |
2297 |
|
|
2298 |
|
// loop through all dimension regions for all dimensions except the velocity dimension |
2299 |
|
int dim[8] = { 0 }; |
2300 |
|
for (int i = 0 ; i < DimensionRegions ; i++) { |
2301 |
|
|
2302 |
|
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
2303 |
|
pDimensionRegions[i]->VelocityUpperLimit) { |
2304 |
|
// create the velocity table |
2305 |
|
uint8_t* table = pDimensionRegions[i]->VelocityTable; |
2306 |
|
if (!table) { |
2307 |
|
table = new uint8_t[128]; |
2308 |
|
pDimensionRegions[i]->VelocityTable = table; |
2309 |
|
} |
2310 |
|
int tableidx = 0; |
2311 |
|
int velocityZone = 0; |
2312 |
|
if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3 |
2313 |
|
for (int k = i ; k < end ; k += step) { |
2314 |
|
DimensionRegion *d = pDimensionRegions[k]; |
2315 |
|
for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone; |
2316 |
|
velocityZone++; |
2317 |
|
} |
2318 |
|
} else { // gig2 |
2319 |
|
for (int k = i ; k < end ; k += step) { |
2320 |
|
DimensionRegion *d = pDimensionRegions[k]; |
2321 |
|
for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone; |
2322 |
|
velocityZone++; |
2323 |
|
} |
2324 |
|
} |
2325 |
|
} else { |
2326 |
|
if (pDimensionRegions[i]->VelocityTable) { |
2327 |
|
delete[] pDimensionRegions[i]->VelocityTable; |
2328 |
|
pDimensionRegions[i]->VelocityTable = 0; |
2329 |
|
} |
2330 |
|
} |
2331 |
|
|
2332 |
uint8_t bits[8] = { 0 }; |
int j; |
2333 |
int previousUpperLimit = -1; |
int shift = 0; |
2334 |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
for (j = 0 ; j < Dimensions ; j++) { |
2335 |
bits[iDimensionNr] = velocityZone; |
if (j == veldim) i += skipveldim; // skip velocity dimension |
2336 |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); |
else { |
2337 |
|
dim[j]++; |
2338 |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
if (dim[j] < pDimensionDefinitions[j].zones) break; |
2339 |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
else { |
2340 |
previousUpperLimit = pDimDef->ranges[velocityZone].high; |
// skip unused dimension regions |
2341 |
// fill velocity table |
dim[j] = 0; |
2342 |
for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) { |
i += ((1 << pDimensionDefinitions[j].bits) - |
2343 |
VelocityTable[i] = velocityZone; |
pDimensionDefinitions[j].zones) << shift; |
2344 |
|
} |
2345 |
|
} |
2346 |
|
shift += pDimensionDefinitions[j].bits; |
2347 |
} |
} |
2348 |
|
if (j == Dimensions) break; |
2349 |
} |
} |
2350 |
} |
} |
2351 |
|
|
2387 |
// assign definition of new dimension |
// assign definition of new dimension |
2388 |
pDimensionDefinitions[Dimensions] = *pDimDef; |
pDimensionDefinitions[Dimensions] = *pDimDef; |
2389 |
|
|
2390 |
|
// auto correct certain dimension definition fields (where possible) |
2391 |
|
pDimensionDefinitions[Dimensions].split_type = |
2392 |
|
__resolveSplitType(pDimensionDefinitions[Dimensions].dimension); |
2393 |
|
pDimensionDefinitions[Dimensions].zone_size = |
2394 |
|
__resolveZoneSize(pDimensionDefinitions[Dimensions]); |
2395 |
|
|
2396 |
// create new dimension region(s) for this new dimension |
// create new dimension region(s) for this new dimension |
2397 |
for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) { |
for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) { |
2398 |
//TODO: maybe we should copy existing dimension regions if possible instead of simply creating new ones with default values |
//TODO: maybe we should copy existing dimension regions if possible instead of simply creating new ones with default values |
2399 |
RIFF::List* pNewDimRgnListChunk = pCkRegion->AddSubList(LIST_TYPE_3EWL); |
RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); |
2400 |
|
RIFF::List* pNewDimRgnListChunk = _3prg->AddSubList(LIST_TYPE_3EWL); |
2401 |
pDimensionRegions[i] = new DimensionRegion(pNewDimRgnListChunk); |
pDimensionRegions[i] = new DimensionRegion(pNewDimRgnListChunk); |
2402 |
DimensionRegions++; |
DimensionRegions++; |
2403 |
} |
} |
2407 |
// if this is a layer dimension, update 'Layers' attribute |
// if this is a layer dimension, update 'Layers' attribute |
2408 |
if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones; |
if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones; |
2409 |
|
|
2410 |
// if this is velocity dimension and got custom defined ranges, update velocity table |
UpdateVelocityTable(); |
|
if (pDimDef->dimension == dimension_velocity && |
|
|
pDimDef->split_type == split_type_customvelocity) { |
|
|
UpdateVelocityTable(pDimDef); |
|
|
} |
|
2411 |
} |
} |
2412 |
|
|
2413 |
/** @brief Delete an existing dimension. |
/** @brief Delete an existing dimension. |
2477 |
pDimensionDefinitions[Dimensions - 1].dimension = dimension_none; |
pDimensionDefinitions[Dimensions - 1].dimension = dimension_none; |
2478 |
pDimensionDefinitions[Dimensions - 1].bits = 0; |
pDimensionDefinitions[Dimensions - 1].bits = 0; |
2479 |
pDimensionDefinitions[Dimensions - 1].zones = 0; |
pDimensionDefinitions[Dimensions - 1].zones = 0; |
|
if (pDimensionDefinitions[Dimensions - 1].ranges) { |
|
|
delete[] pDimensionDefinitions[Dimensions - 1].ranges; |
|
|
pDimensionDefinitions[Dimensions - 1].ranges = NULL; |
|
|
} |
|
2480 |
|
|
2481 |
Dimensions--; |
Dimensions--; |
2482 |
|
|
2485 |
} |
} |
2486 |
|
|
2487 |
Region::~Region() { |
Region::~Region() { |
|
for (uint i = 0; i < Dimensions; i++) { |
|
|
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
|
|
} |
|
2488 |
for (int i = 0; i < 256; i++) { |
for (int i = 0; i < 256; i++) { |
2489 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
2490 |
} |
} |
2509 |
* @see Dimensions |
* @see Dimensions |
2510 |
*/ |
*/ |
2511 |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
2512 |
uint8_t bits[8] = { 0 }; |
uint8_t bits; |
2513 |
|
int veldim = -1; |
2514 |
|
int velbitpos; |
2515 |
|
int bitpos = 0; |
2516 |
|
int dimregidx = 0; |
2517 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
2518 |
bits[i] = DimValues[i]; |
if (pDimensionDefinitions[i].dimension == dimension_velocity) { |
2519 |
switch (pDimensionDefinitions[i].split_type) { |
// the velocity dimension must be handled after the other dimensions |
2520 |
case split_type_normal: |
veldim = i; |
2521 |
bits[i] = uint8_t(bits[i] / pDimensionDefinitions[i].zone_size); |
velbitpos = bitpos; |
2522 |
break; |
} else { |
2523 |
case split_type_customvelocity: |
switch (pDimensionDefinitions[i].split_type) { |
2524 |
bits[i] = VelocityTable[bits[i]]; |
case split_type_normal: |
2525 |
break; |
if (pDimensionRegions[0]->DimensionUpperLimits[i]) { |
2526 |
case split_type_bit: // the value is already the sought dimension bit number |
// gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges |
2527 |
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) { |
2528 |
bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed |
if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break; |
2529 |
break; |
} |
2530 |
|
} else { |
2531 |
|
// gig2: evenly sized zones |
2532 |
|
bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); |
2533 |
|
} |
2534 |
|
break; |
2535 |
|
case split_type_bit: // the value is already the sought dimension bit number |
2536 |
|
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
2537 |
|
bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed |
2538 |
|
break; |
2539 |
|
} |
2540 |
|
dimregidx |= bits << bitpos; |
2541 |
} |
} |
2542 |
|
bitpos += pDimensionDefinitions[i].bits; |
2543 |
|
} |
2544 |
|
DimensionRegion* dimreg = pDimensionRegions[dimregidx]; |
2545 |
|
if (veldim != -1) { |
2546 |
|
// (dimreg is now the dimension region for the lowest velocity) |
2547 |
|
if (dimreg->VelocityTable) // custom defined zone ranges |
2548 |
|
bits = dimreg->VelocityTable[DimValues[veldim]]; |
2549 |
|
else // normal split type |
2550 |
|
bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); |
2551 |
|
|
2552 |
|
dimregidx |= bits << velbitpos; |
2553 |
|
dimreg = pDimensionRegions[dimregidx]; |
2554 |
} |
} |
2555 |
return GetDimensionRegionByBit(bits); |
return dimreg; |
2556 |
} |
} |
2557 |
|
|
2558 |
/** |
/** |
2592 |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) { |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) { |
2593 |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
2594 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
2595 |
|
if (!file->pWavePoolTable) return NULL; |
2596 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
2597 |
unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
2598 |
Sample* sample = file->GetFirstSample(pProgress); |
Sample* sample = file->GetFirstSample(pProgress); |
2599 |
while (sample) { |
while (sample) { |
2600 |
if (sample->ulWavePoolOffset == soughtoffset && |
if (sample->ulWavePoolOffset == soughtoffset && |
2601 |
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(pSample = sample); |
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(sample); |
2602 |
sample = file->GetNextSample(); |
sample = file->GetNextSample(); |
2603 |
} |
} |
2604 |
return NULL; |
return NULL; |
2610 |
// * |
// * |
2611 |
|
|
2612 |
Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) { |
Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) { |
2613 |
|
static const DLS::Info::FixedStringLength fixedStringLengths[] = { |
2614 |
|
{ CHUNK_ID_INAM, 64 }, |
2615 |
|
{ CHUNK_ID_ISFT, 12 }, |
2616 |
|
{ 0, 0 } |
2617 |
|
}; |
2618 |
|
pInfo->FixedStringLengths = fixedStringLengths; |
2619 |
|
|
2620 |
// Initialization |
// Initialization |
2621 |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
2622 |
|
EffectSend = 0; |
2623 |
|
Attenuation = 0; |
2624 |
|
FineTune = 0; |
2625 |
|
PitchbendRange = 0; |
2626 |
|
PianoReleaseMode = false; |
2627 |
|
DimensionKeyRange.low = 0; |
2628 |
|
DimensionKeyRange.high = 0; |
2629 |
|
|
2630 |
// Loading |
// Loading |
2631 |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
2704 |
if (!_3ewg) _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, 12); |
if (!_3ewg) _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, 12); |
2705 |
// update '3ewg' RIFF chunk |
// update '3ewg' RIFF chunk |
2706 |
uint8_t* pData = (uint8_t*) _3ewg->LoadChunkData(); |
uint8_t* pData = (uint8_t*) _3ewg->LoadChunkData(); |
2707 |
memcpy(&pData[0], &EffectSend, 2); |
store16(&pData[0], EffectSend); |
2708 |
memcpy(&pData[2], &Attenuation, 4); |
store32(&pData[2], Attenuation); |
2709 |
memcpy(&pData[6], &FineTune, 2); |
store16(&pData[6], FineTune); |
2710 |
memcpy(&pData[8], &PitchbendRange, 2); |
store16(&pData[8], PitchbendRange); |
2711 |
const uint8_t dimkeystart = (PianoReleaseMode) ? 0x01 : 0x00 | |
const uint8_t dimkeystart = (PianoReleaseMode) ? 0x01 : 0x00 | |
2712 |
DimensionKeyRange.low << 1; |
DimensionKeyRange.low << 1; |
2713 |
memcpy(&pData[10], &dimkeystart, 1); |
pData[10] = dimkeystart; |
2714 |
memcpy(&pData[11], &DimensionKeyRange.high, 1); |
pData[11] = DimensionKeyRange.high; |
2715 |
} |
} |
2716 |
|
|
2717 |
/** |
/** |
2782 |
|
|
2783 |
|
|
2784 |
|
|
2785 |
|
// *************** Group *************** |
2786 |
|
// * |
2787 |
|
|
2788 |
|
/** @brief Constructor. |
2789 |
|
* |
2790 |
|
* @param file - pointer to the gig::File object |
2791 |
|
* @param ck3gnm - pointer to 3gnm chunk associated with this group or |
2792 |
|
* NULL if this is a new Group |
2793 |
|
*/ |
2794 |
|
Group::Group(File* file, RIFF::Chunk* ck3gnm) { |
2795 |
|
pFile = file; |
2796 |
|
pNameChunk = ck3gnm; |
2797 |
|
::LoadString(pNameChunk, Name); |
2798 |
|
} |
2799 |
|
|
2800 |
|
Group::~Group() { |
2801 |
|
// remove the chunk associated with this group (if any) |
2802 |
|
if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk); |
2803 |
|
} |
2804 |
|
|
2805 |
|
/** @brief Update chunks with current group settings. |
2806 |
|
* |
2807 |
|
* Apply current Group field values to the respective chunks. You have |
2808 |
|
* to call File::Save() to make changes persistent. |
2809 |
|
* |
2810 |
|
* Usually there is absolutely no need to call this method explicitly. |
2811 |
|
* It will be called automatically when File::Save() was called. |
2812 |
|
*/ |
2813 |
|
void Group::UpdateChunks() { |
2814 |
|
// make sure <3gri> and <3gnl> list chunks exist |
2815 |
|
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
2816 |
|
if (!_3gri) { |
2817 |
|
_3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); |
2818 |
|
pFile->pRIFF->MoveSubChunk(_3gri, pFile->pRIFF->GetSubChunk(CHUNK_ID_PTBL)); |
2819 |
|
} |
2820 |
|
RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); |
2821 |
|
if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); |
2822 |
|
// now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk |
2823 |
|
::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64); |
2824 |
|
} |
2825 |
|
|
2826 |
|
/** |
2827 |
|
* Returns the first Sample of this Group. You have to call this method |
2828 |
|
* once before you use GetNextSample(). |
2829 |
|
* |
2830 |
|
* <b>Notice:</b> this method might block for a long time, in case the |
2831 |
|
* samples of this .gig file were not scanned yet |
2832 |
|
* |
2833 |
|
* @returns pointer address to first Sample or NULL if there is none |
2834 |
|
* applied to this Group |
2835 |
|
* @see GetNextSample() |
2836 |
|
*/ |
2837 |
|
Sample* Group::GetFirstSample() { |
2838 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
2839 |
|
for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) { |
2840 |
|
if (pSample->GetGroup() == this) return pSample; |
2841 |
|
} |
2842 |
|
return NULL; |
2843 |
|
} |
2844 |
|
|
2845 |
|
/** |
2846 |
|
* Returns the next Sample of the Group. You have to call |
2847 |
|
* GetFirstSample() once before you can use this method. By calling this |
2848 |
|
* method multiple times it iterates through the Samples assigned to |
2849 |
|
* this Group. |
2850 |
|
* |
2851 |
|
* @returns pointer address to the next Sample of this Group or NULL if |
2852 |
|
* end reached |
2853 |
|
* @see GetFirstSample() |
2854 |
|
*/ |
2855 |
|
Sample* Group::GetNextSample() { |
2856 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
2857 |
|
for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) { |
2858 |
|
if (pSample->GetGroup() == this) return pSample; |
2859 |
|
} |
2860 |
|
return NULL; |
2861 |
|
} |
2862 |
|
|
2863 |
|
/** |
2864 |
|
* Move Sample given by \a pSample from another Group to this Group. |
2865 |
|
*/ |
2866 |
|
void Group::AddSample(Sample* pSample) { |
2867 |
|
pSample->pGroup = this; |
2868 |
|
} |
2869 |
|
|
2870 |
|
/** |
2871 |
|
* Move all members of this group to another group (preferably the 1st |
2872 |
|
* one except this). This method is called explicitly by |
2873 |
|
* File::DeleteGroup() thus when a Group was deleted. This code was |
2874 |
|
* intentionally not placed in the destructor! |
2875 |
|
*/ |
2876 |
|
void Group::MoveAll() { |
2877 |
|
// get "that" other group first |
2878 |
|
Group* pOtherGroup = NULL; |
2879 |
|
for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) { |
2880 |
|
if (pOtherGroup != this) break; |
2881 |
|
} |
2882 |
|
if (!pOtherGroup) throw Exception( |
2883 |
|
"Could not move samples to another group, since there is no " |
2884 |
|
"other Group. This is a bug, report it!" |
2885 |
|
); |
2886 |
|
// now move all samples of this group to the other group |
2887 |
|
for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { |
2888 |
|
pOtherGroup->AddSample(pSample); |
2889 |
|
} |
2890 |
|
} |
2891 |
|
|
2892 |
|
|
2893 |
|
|
2894 |
// *************** File *************** |
// *************** File *************** |
2895 |
// * |
// * |
2896 |
|
|
2897 |
|
const DLS::Info::FixedStringLength File::FixedStringLengths[] = { |
2898 |
|
{ CHUNK_ID_IARL, 256 }, |
2899 |
|
{ CHUNK_ID_IART, 128 }, |
2900 |
|
{ CHUNK_ID_ICMS, 128 }, |
2901 |
|
{ CHUNK_ID_ICMT, 1024 }, |
2902 |
|
{ CHUNK_ID_ICOP, 128 }, |
2903 |
|
{ CHUNK_ID_ICRD, 128 }, |
2904 |
|
{ CHUNK_ID_IENG, 128 }, |
2905 |
|
{ CHUNK_ID_IGNR, 128 }, |
2906 |
|
{ CHUNK_ID_IKEY, 128 }, |
2907 |
|
{ CHUNK_ID_IMED, 128 }, |
2908 |
|
{ CHUNK_ID_INAM, 128 }, |
2909 |
|
{ CHUNK_ID_IPRD, 128 }, |
2910 |
|
{ CHUNK_ID_ISBJ, 128 }, |
2911 |
|
{ CHUNK_ID_ISFT, 128 }, |
2912 |
|
{ CHUNK_ID_ISRC, 128 }, |
2913 |
|
{ CHUNK_ID_ISRF, 128 }, |
2914 |
|
{ CHUNK_ID_ITCH, 128 }, |
2915 |
|
{ 0, 0 } |
2916 |
|
}; |
2917 |
|
|
2918 |
File::File() : DLS::File() { |
File::File() : DLS::File() { |
2919 |
|
pGroups = NULL; |
2920 |
|
pInfo->FixedStringLengths = FixedStringLengths; |
2921 |
|
pInfo->ArchivalLocation = String(256, ' '); |
2922 |
|
|
2923 |
|
// add some mandatory chunks to get the file chunks in right |
2924 |
|
// order (INFO chunk will be moved to first position later) |
2925 |
|
pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
2926 |
|
pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
2927 |
} |
} |
2928 |
|
|
2929 |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
2930 |
|
pGroups = NULL; |
2931 |
|
pInfo->FixedStringLengths = FixedStringLengths; |
2932 |
|
} |
2933 |
|
|
2934 |
|
File::~File() { |
2935 |
|
if (pGroups) { |
2936 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
2937 |
|
std::list<Group*>::iterator end = pGroups->end(); |
2938 |
|
while (iter != end) { |
2939 |
|
delete *iter; |
2940 |
|
++iter; |
2941 |
|
} |
2942 |
|
delete pGroups; |
2943 |
|
} |
2944 |
} |
} |
2945 |
|
|
2946 |
Sample* File::GetFirstSample(progress_t* pProgress) { |
Sample* File::GetFirstSample(progress_t* pProgress) { |
2970 |
// create new Sample object and its respective 'wave' list chunk |
// create new Sample object and its respective 'wave' list chunk |
2971 |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
2972 |
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
2973 |
|
|
2974 |
|
// add mandatory chunks to get the chunks in right order |
2975 |
|
wave->AddSubChunk(CHUNK_ID_FMT, 16); |
2976 |
|
wave->AddSubList(LIST_TYPE_INFO); |
2977 |
|
|
2978 |
pSamples->push_back(pSample); |
pSamples->push_back(pSample); |
2979 |
return pSample; |
return pSample; |
2980 |
} |
} |
2991 |
if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); |
if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); |
2992 |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); |
2993 |
if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); |
if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); |
2994 |
|
if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation |
2995 |
pSamples->erase(iter); |
pSamples->erase(iter); |
2996 |
delete pSample; |
delete pSample; |
2997 |
} |
} |
3001 |
} |
} |
3002 |
|
|
3003 |
void File::LoadSamples(progress_t* pProgress) { |
void File::LoadSamples(progress_t* pProgress) { |
3004 |
|
// Groups must be loaded before samples, because samples will try |
3005 |
|
// to resolve the group they belong to |
3006 |
|
if (!pGroups) LoadGroups(); |
3007 |
|
|
3008 |
if (!pSamples) pSamples = new SampleList; |
if (!pSamples) pSamples = new SampleList; |
3009 |
|
|
3010 |
RIFF::File* file = pRIFF; |
RIFF::File* file = pRIFF; |
3117 |
__ensureMandatoryChunksExist(); |
__ensureMandatoryChunksExist(); |
3118 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
3119 |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
3120 |
|
|
3121 |
|
// add mandatory chunks to get the chunks in right order |
3122 |
|
lstInstr->AddSubList(LIST_TYPE_INFO); |
3123 |
|
|
3124 |
Instrument* pInstrument = new Instrument(this, lstInstr); |
Instrument* pInstrument = new Instrument(this, lstInstr); |
3125 |
|
|
3126 |
|
lstInstr->AddSubChunk(CHUNK_ID_INSH, 12); |
3127 |
|
|
3128 |
|
// this string is needed for the gig to be loadable in GSt: |
3129 |
|
pInstrument->pInfo->Software = "Endless Wave"; |
3130 |
|
|
3131 |
pInstruments->push_back(pInstrument); |
pInstruments->push_back(pInstrument); |
3132 |
return pInstrument; |
return pInstrument; |
3133 |
} |
} |
3138 |
* have to call Save() to make this persistent to the file. |
* have to call Save() to make this persistent to the file. |
3139 |
* |
* |
3140 |
* @param pInstrument - instrument to delete |
* @param pInstrument - instrument to delete |
3141 |
* @throws gig::Excption if given instrument could not be found |
* @throws gig::Exception if given instrument could not be found |
3142 |
*/ |
*/ |
3143 |
void File::DeleteInstrument(Instrument* pInstrument) { |
void File::DeleteInstrument(Instrument* pInstrument) { |
3144 |
if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments"); |
if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments"); |
3178 |
} |
} |
3179 |
} |
} |
3180 |
|
|
3181 |
|
Group* File::GetFirstGroup() { |
3182 |
|
if (!pGroups) LoadGroups(); |
3183 |
|
// there must always be at least one group |
3184 |
|
GroupsIterator = pGroups->begin(); |
3185 |
|
return *GroupsIterator; |
3186 |
|
} |
3187 |
|
|
3188 |
|
Group* File::GetNextGroup() { |
3189 |
|
if (!pGroups) return NULL; |
3190 |
|
++GroupsIterator; |
3191 |
|
return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator; |
3192 |
|
} |
3193 |
|
|
3194 |
|
/** |
3195 |
|
* Returns the group with the given index. |
3196 |
|
* |
3197 |
|
* @param index - number of the sought group (0..n) |
3198 |
|
* @returns sought group or NULL if there's no such group |
3199 |
|
*/ |
3200 |
|
Group* File::GetGroup(uint index) { |
3201 |
|
if (!pGroups) LoadGroups(); |
3202 |
|
GroupsIterator = pGroups->begin(); |
3203 |
|
for (uint i = 0; GroupsIterator != pGroups->end(); i++) { |
3204 |
|
if (i == index) return *GroupsIterator; |
3205 |
|
++GroupsIterator; |
3206 |
|
} |
3207 |
|
return NULL; |
3208 |
|
} |
3209 |
|
|
3210 |
|
Group* File::AddGroup() { |
3211 |
|
if (!pGroups) LoadGroups(); |
3212 |
|
// there must always be at least one group |
3213 |
|
__ensureMandatoryChunksExist(); |
3214 |
|
Group* pGroup = new Group(this, NULL); |
3215 |
|
pGroups->push_back(pGroup); |
3216 |
|
return pGroup; |
3217 |
|
} |
3218 |
|
|
3219 |
|
/** @brief Delete a group and its samples. |
3220 |
|
* |
3221 |
|
* This will delete the given Group object and all the samples that |
3222 |
|
* belong to this group from the gig file. You have to call Save() to |
3223 |
|
* make this persistent to the file. |
3224 |
|
* |
3225 |
|
* @param pGroup - group to delete |
3226 |
|
* @throws gig::Exception if given group could not be found |
3227 |
|
*/ |
3228 |
|
void File::DeleteGroup(Group* pGroup) { |
3229 |
|
if (!pGroups) LoadGroups(); |
3230 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
3231 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
3232 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
3233 |
|
// delete all members of this group |
3234 |
|
for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) { |
3235 |
|
DeleteSample(pSample); |
3236 |
|
} |
3237 |
|
// now delete this group object |
3238 |
|
pGroups->erase(iter); |
3239 |
|
delete pGroup; |
3240 |
|
} |
3241 |
|
|
3242 |
|
/** @brief Delete a group. |
3243 |
|
* |
3244 |
|
* This will delete the given Group object from the gig file. All the |
3245 |
|
* samples that belong to this group will not be deleted, but instead |
3246 |
|
* be moved to another group. You have to call Save() to make this |
3247 |
|
* persistent to the file. |
3248 |
|
* |
3249 |
|
* @param pGroup - group to delete |
3250 |
|
* @throws gig::Exception if given group could not be found |
3251 |
|
*/ |
3252 |
|
void File::DeleteGroupOnly(Group* pGroup) { |
3253 |
|
if (!pGroups) LoadGroups(); |
3254 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
3255 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
3256 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
3257 |
|
// move all members of this group to another group |
3258 |
|
pGroup->MoveAll(); |
3259 |
|
pGroups->erase(iter); |
3260 |
|
delete pGroup; |
3261 |
|
} |
3262 |
|
|
3263 |
|
void File::LoadGroups() { |
3264 |
|
if (!pGroups) pGroups = new std::list<Group*>; |
3265 |
|
// try to read defined groups from file |
3266 |
|
RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); |
3267 |
|
if (lst3gri) { |
3268 |
|
RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); |
3269 |
|
if (lst3gnl) { |
3270 |
|
RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); |
3271 |
|
while (ck) { |
3272 |
|
if (ck->GetChunkID() == CHUNK_ID_3GNM) { |
3273 |
|
pGroups->push_back(new Group(this, ck)); |
3274 |
|
} |
3275 |
|
ck = lst3gnl->GetNextSubChunk(); |
3276 |
|
} |
3277 |
|
} |
3278 |
|
} |
3279 |
|
// if there were no group(s), create at least the mandatory default group |
3280 |
|
if (!pGroups->size()) { |
3281 |
|
Group* pGroup = new Group(this, NULL); |
3282 |
|
pGroup->Name = "Default Group"; |
3283 |
|
pGroups->push_back(pGroup); |
3284 |
|
} |
3285 |
|
} |
3286 |
|
|
3287 |
|
/** |
3288 |
|
* Apply all the gig file's current instruments, samples, groups and settings |
3289 |
|
* to the respective RIFF chunks. You have to call Save() to make changes |
3290 |
|
* persistent. |
3291 |
|
* |
3292 |
|
* Usually there is absolutely no need to call this method explicitly. |
3293 |
|
* It will be called automatically when File::Save() was called. |
3294 |
|
* |
3295 |
|
* @throws Exception - on errors |
3296 |
|
*/ |
3297 |
|
void File::UpdateChunks() { |
3298 |
|
RIFF::Chunk* info = pRIFF->GetSubList(LIST_TYPE_INFO); |
3299 |
|
|
3300 |
|
// first update base class's chunks |
3301 |
|
DLS::File::UpdateChunks(); |
3302 |
|
|
3303 |
|
if (!info) { |
3304 |
|
// INFO was added by Resource::UpdateChunks - make sure it |
3305 |
|
// is placed first in file |
3306 |
|
info = pRIFF->GetSubList(LIST_TYPE_INFO); |
3307 |
|
RIFF::Chunk* first = pRIFF->GetFirstSubChunk(); |
3308 |
|
if (first != info) { |
3309 |
|
pRIFF->MoveSubChunk(info, first); |
3310 |
|
} |
3311 |
|
} |
3312 |
|
|
3313 |
|
// update group's chunks |
3314 |
|
if (pGroups) { |
3315 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
3316 |
|
std::list<Group*>::iterator end = pGroups->end(); |
3317 |
|
for (; iter != end; ++iter) { |
3318 |
|
(*iter)->UpdateChunks(); |
3319 |
|
} |
3320 |
|
} |
3321 |
|
} |
3322 |
|
|
3323 |
|
|
3324 |
|
|
3325 |
// *************** Exception *************** |
// *************** Exception *************** |