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-2016 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 * |
30 |
#include <iostream> |
#include <iostream> |
31 |
#include <assert.h> |
#include <assert.h> |
32 |
|
|
33 |
|
/// libgig's current file format version (for extending the original Giga file |
34 |
|
/// format with libgig's own custom data / custom features). |
35 |
|
#define GIG_FILE_EXT_VERSION 2 |
36 |
|
|
37 |
/// Initial size of the sample buffer which is used for decompression of |
/// Initial size of the sample buffer which is used for decompression of |
38 |
/// compressed sample wave streams - this value should always be bigger than |
/// compressed sample wave streams - this value should always be bigger than |
39 |
/// the biggest sample piece expected to be read by the sampler engine, |
/// the biggest sample piece expected to be read by the sampler engine, |
57 |
|
|
58 |
namespace gig { |
namespace gig { |
59 |
|
|
|
// *************** progress_t *************** |
|
|
// * |
|
|
|
|
|
progress_t::progress_t() { |
|
|
callback = NULL; |
|
|
custom = NULL; |
|
|
__range_min = 0.0f; |
|
|
__range_max = 1.0f; |
|
|
} |
|
|
|
|
|
// private helper function to convert progress of a subprocess into the global progress |
|
|
static void __notify_progress(progress_t* pProgress, float subprogress) { |
|
|
if (pProgress && pProgress->callback) { |
|
|
const float totalrange = pProgress->__range_max - pProgress->__range_min; |
|
|
const float totalprogress = pProgress->__range_min + subprogress * totalrange; |
|
|
pProgress->factor = totalprogress; |
|
|
pProgress->callback(pProgress); // now actually notify about the progress |
|
|
} |
|
|
} |
|
|
|
|
|
// private helper function to divide a progress into subprogresses |
|
|
static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress, float totalTasks, float currentTask) { |
|
|
if (pParentProgress && pParentProgress->callback) { |
|
|
const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min; |
|
|
pSubProgress->callback = pParentProgress->callback; |
|
|
pSubProgress->custom = pParentProgress->custom; |
|
|
pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks; |
|
|
pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
60 |
// *************** Internal functions for sample decompression *************** |
// *************** Internal functions for sample decompression *************** |
61 |
// * |
// * |
62 |
|
|
95 |
void Decompress16(int compressionmode, const unsigned char* params, |
void Decompress16(int compressionmode, const unsigned char* params, |
96 |
int srcStep, int dstStep, |
int srcStep, int dstStep, |
97 |
const unsigned char* pSrc, int16_t* pDst, |
const unsigned char* pSrc, int16_t* pDst, |
98 |
unsigned long currentframeoffset, |
file_offset_t currentframeoffset, |
99 |
unsigned long copysamples) |
file_offset_t copysamples) |
100 |
{ |
{ |
101 |
switch (compressionmode) { |
switch (compressionmode) { |
102 |
case 0: // 16 bit uncompressed |
case 0: // 16 bit uncompressed |
132 |
|
|
133 |
void Decompress24(int compressionmode, const unsigned char* params, |
void Decompress24(int compressionmode, const unsigned char* params, |
134 |
int dstStep, const unsigned char* pSrc, uint8_t* pDst, |
int dstStep, const unsigned char* pSrc, uint8_t* pDst, |
135 |
unsigned long currentframeoffset, |
file_offset_t currentframeoffset, |
136 |
unsigned long copysamples, int truncatedBits) |
file_offset_t copysamples, int truncatedBits) |
137 |
{ |
{ |
138 |
int y, dy, ddy, dddy; |
int y, dy, ddy, dddy; |
139 |
|
|
269 |
* steps. |
* steps. |
270 |
* |
* |
271 |
* Once the whole data was processed by __calculateCRC(), one should |
* Once the whole data was processed by __calculateCRC(), one should |
272 |
* call __encodeCRC() to get the final CRC result. |
* call __finalizeCRC() to get the final CRC result. |
273 |
* |
* |
274 |
* @param buf - pointer to data the CRC shall be calculated of |
* @param buf - pointer to data the CRC shall be calculated of |
275 |
* @param bufSize - size of the data to be processed |
* @param bufSize - size of the data to be processed |
276 |
* @param crc - variable the CRC sum shall be stored to |
* @param crc - variable the CRC sum shall be stored to |
277 |
*/ |
*/ |
278 |
static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) { |
static void __calculateCRC(unsigned char* buf, size_t bufSize, uint32_t& crc) { |
279 |
for (int i = 0 ; i < bufSize ; i++) { |
for (size_t i = 0 ; i < bufSize ; i++) { |
280 |
crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); |
crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); |
281 |
} |
} |
282 |
} |
} |
286 |
* |
* |
287 |
* @param crc - variable previously passed to __calculateCRC() |
* @param crc - variable previously passed to __calculateCRC() |
288 |
*/ |
*/ |
289 |
inline static uint32_t __encodeCRC(const uint32_t& crc) { |
inline static void __finalizeCRC(uint32_t& crc) { |
290 |
return crc ^ 0xffffffff; |
crc ^= 0xffffffff; |
291 |
} |
} |
292 |
|
|
293 |
|
|
318 |
// *************** Sample *************** |
// *************** Sample *************** |
319 |
// * |
// * |
320 |
|
|
321 |
unsigned int Sample::Instances = 0; |
size_t Sample::Instances = 0; |
322 |
buffer_t Sample::InternalDecompressionBuffer; |
buffer_t Sample::InternalDecompressionBuffer; |
323 |
|
|
324 |
/** @brief Constructor. |
/** @brief Constructor. |
338 |
* ('wvpl') list chunk |
* ('wvpl') list chunk |
339 |
* @param fileNo - number of an extension file where this sample |
* @param fileNo - number of an extension file where this sample |
340 |
* is located, 0 otherwise |
* is located, 0 otherwise |
341 |
|
* @param index - wave pool index of sample (may be -1 on new sample) |
342 |
*/ |
*/ |
343 |
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, file_offset_t WavePoolOffset, unsigned long fileNo, int index) |
344 |
|
: DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) |
345 |
|
{ |
346 |
static const DLS::Info::string_length_t fixedStringLengths[] = { |
static const DLS::Info::string_length_t fixedStringLengths[] = { |
347 |
{ CHUNK_ID_INAM, 64 }, |
{ CHUNK_ID_INAM, 64 }, |
348 |
{ 0, 0 } |
{ 0, 0 } |
352 |
FileNo = fileNo; |
FileNo = fileNo; |
353 |
|
|
354 |
__resetCRC(crc); |
__resetCRC(crc); |
355 |
|
// if this is not a new sample, try to get the sample's already existing |
356 |
|
// CRC32 checksum from disk, this checksum will reflect the sample's CRC32 |
357 |
|
// checksum of the time when the sample was consciously modified by the |
358 |
|
// user for the last time (by calling Sample::Write() that is). |
359 |
|
if (index >= 0) { // not a new file ... |
360 |
|
try { |
361 |
|
uint32_t crc = pFile->GetSampleChecksumByIndex(index); |
362 |
|
this->crc = crc; |
363 |
|
} catch (...) {} |
364 |
|
} |
365 |
|
|
366 |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
367 |
if (pCk3gix) { |
if (pCk3gix) { |
494 |
const int iReadAtOnce = 32*1024; |
const int iReadAtOnce = 32*1024; |
495 |
char* buf = new char[iReadAtOnce * orig->FrameSize]; |
char* buf = new char[iReadAtOnce * orig->FrameSize]; |
496 |
Sample* pOrig = (Sample*) orig; //HACK: remove constness for now |
Sample* pOrig = (Sample*) orig; //HACK: remove constness for now |
497 |
unsigned long restorePos = pOrig->GetPos(); |
file_offset_t restorePos = pOrig->GetPos(); |
498 |
pOrig->SetPos(0); |
pOrig->SetPos(0); |
499 |
SetPos(0); |
SetPos(0); |
500 |
for (unsigned long n = pOrig->Read(buf, iReadAtOnce); n; |
for (file_offset_t n = pOrig->Read(buf, iReadAtOnce); n; |
501 |
n = pOrig->Read(buf, iReadAtOnce)) |
n = pOrig->Read(buf, iReadAtOnce)) |
502 |
{ |
{ |
503 |
Write(buf, n); |
Write(buf, n); |
513 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
514 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
515 |
* |
* |
516 |
|
* @param pProgress - callback function for progress notification |
517 |
* @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
* @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
518 |
* was provided yet |
* was provided yet |
519 |
* @throws gig::Exception if there is any invalid sample setting |
* @throws gig::Exception if there is any invalid sample setting |
520 |
*/ |
*/ |
521 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks(progress_t* pProgress) { |
522 |
// first update base class's chunks |
// first update base class's chunks |
523 |
DLS::Sample::UpdateChunks(); |
DLS::Sample::UpdateChunks(pProgress); |
524 |
|
|
525 |
// make sure 'smpl' chunk exists |
// make sure 'smpl' chunk exists |
526 |
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
582 |
void Sample::ScanCompressedSample() { |
void Sample::ScanCompressedSample() { |
583 |
//TODO: we have to add some more scans here (e.g. determine compression rate) |
//TODO: we have to add some more scans here (e.g. determine compression rate) |
584 |
this->SamplesTotal = 0; |
this->SamplesTotal = 0; |
585 |
std::list<unsigned long> frameOffsets; |
std::list<file_offset_t> frameOffsets; |
586 |
|
|
587 |
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
588 |
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
598 |
const int mode_l = pCkData->ReadUint8(); |
const int mode_l = pCkData->ReadUint8(); |
599 |
const int mode_r = pCkData->ReadUint8(); |
const int mode_r = pCkData->ReadUint8(); |
600 |
if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode"); |
if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode"); |
601 |
const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
const file_offset_t frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
602 |
|
|
603 |
if (pCkData->RemainingBytes() <= frameSize) { |
if (pCkData->RemainingBytes() <= frameSize) { |
604 |
SamplesInLastFrame = |
SamplesInLastFrame = |
617 |
|
|
618 |
const int mode = pCkData->ReadUint8(); |
const int mode = pCkData->ReadUint8(); |
619 |
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
620 |
const unsigned long frameSize = bytesPerFrame[mode]; |
const file_offset_t frameSize = bytesPerFrame[mode]; |
621 |
|
|
622 |
if (pCkData->RemainingBytes() <= frameSize) { |
if (pCkData->RemainingBytes() <= frameSize) { |
623 |
SamplesInLastFrame = |
SamplesInLastFrame = |
633 |
|
|
634 |
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
635 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
636 |
FrameTable = new unsigned long[frameOffsets.size()]; |
FrameTable = new file_offset_t[frameOffsets.size()]; |
637 |
std::list<unsigned long>::iterator end = frameOffsets.end(); |
std::list<file_offset_t>::iterator end = frameOffsets.end(); |
638 |
std::list<unsigned long>::iterator iter = frameOffsets.begin(); |
std::list<file_offset_t>::iterator iter = frameOffsets.begin(); |
639 |
for (int i = 0; iter != end; i++, iter++) { |
for (int i = 0; iter != end; i++, iter++) { |
640 |
FrameTable[i] = *iter; |
FrameTable[i] = *iter; |
641 |
} |
} |
676 |
* the cached sample data in bytes |
* the cached sample data in bytes |
677 |
* @see ReleaseSampleData(), Read(), SetPos() |
* @see ReleaseSampleData(), Read(), SetPos() |
678 |
*/ |
*/ |
679 |
buffer_t Sample::LoadSampleData(unsigned long SampleCount) { |
buffer_t Sample::LoadSampleData(file_offset_t SampleCount) { |
680 |
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
681 |
} |
} |
682 |
|
|
735 |
* size of the cached sample data in bytes |
* size of the cached sample data in bytes |
736 |
* @see ReleaseSampleData(), Read(), SetPos() |
* @see ReleaseSampleData(), Read(), SetPos() |
737 |
*/ |
*/ |
738 |
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) { |
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(file_offset_t SampleCount, uint NullSamplesCount) { |
739 |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
740 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
741 |
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
file_offset_t allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
742 |
SetPos(0); // reset read position to begin of sample |
SetPos(0); // reset read position to begin of sample |
743 |
RAMCache.pStart = new int8_t[allocationsize]; |
RAMCache.pStart = new int8_t[allocationsize]; |
744 |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
802 |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
803 |
* other formats will fail! |
* other formats will fail! |
804 |
* |
* |
805 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param NewSize - new sample wave data size in sample points (must be |
806 |
* greater than zero) |
* greater than zero) |
807 |
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
808 |
* or if \a iNewSize is less than 1 |
* @throws DLS::Exception if \a NewSize is less than 1 or unrealistic large |
809 |
* @throws gig::Exception if existing sample is compressed |
* @throws gig::Exception if existing sample is compressed |
810 |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
811 |
* DLS::Sample::FormatTag, File::Save() |
* DLS::Sample::FormatTag, File::Save() |
812 |
*/ |
*/ |
813 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(file_offset_t NewSize) { |
814 |
if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)"); |
if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)"); |
815 |
DLS::Sample::Resize(iNewSize); |
DLS::Sample::Resize(NewSize); |
816 |
} |
} |
817 |
|
|
818 |
/** |
/** |
836 |
* @returns the new sample position |
* @returns the new sample position |
837 |
* @see Read() |
* @see Read() |
838 |
*/ |
*/ |
839 |
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) { |
840 |
if (Compressed) { |
if (Compressed) { |
841 |
switch (Whence) { |
switch (Whence) { |
842 |
case RIFF::stream_curpos: |
case RIFF::stream_curpos: |
854 |
} |
} |
855 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
856 |
|
|
857 |
unsigned long frame = this->SamplePos / 2048; // to which frame to jump |
file_offset_t frame = this->SamplePos / 2048; // to which frame to jump |
858 |
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
859 |
pCkData->SetPos(FrameTable[frame]); // set chunk pointer to the start of sought frame |
pCkData->SetPos(FrameTable[frame]); // set chunk pointer to the start of sought frame |
860 |
return this->SamplePos; |
return this->SamplePos; |
861 |
} |
} |
862 |
else { // not compressed |
else { // not compressed |
863 |
unsigned long orderedBytes = SampleCount * this->FrameSize; |
file_offset_t orderedBytes = SampleCount * this->FrameSize; |
864 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
file_offset_t result = pCkData->SetPos(orderedBytes, Whence); |
865 |
return (result == orderedBytes) ? SampleCount |
return (result == orderedBytes) ? SampleCount |
866 |
: result / this->FrameSize; |
: result / this->FrameSize; |
867 |
} |
} |
870 |
/** |
/** |
871 |
* Returns the current position in the sample (in sample points). |
* Returns the current position in the sample (in sample points). |
872 |
*/ |
*/ |
873 |
unsigned long Sample::GetPos() const { |
file_offset_t Sample::GetPos() const { |
874 |
if (Compressed) return SamplePos; |
if (Compressed) return SamplePos; |
875 |
else return pCkData->GetPos() / FrameSize; |
else return pCkData->GetPos() / FrameSize; |
876 |
} |
} |
909 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
910 |
* @see CreateDecompressionBuffer() |
* @see CreateDecompressionBuffer() |
911 |
*/ |
*/ |
912 |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, |
file_offset_t Sample::ReadAndLoop(void* pBuffer, file_offset_t SampleCount, playback_state_t* pPlaybackState, |
913 |
DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { |
DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { |
914 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
file_offset_t samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
915 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
916 |
|
|
917 |
SetPos(pPlaybackState->position); // recover position from the last time |
SetPos(pPlaybackState->position); // recover position from the last time |
949 |
// reading, swap all sample frames so it reflects |
// reading, swap all sample frames so it reflects |
950 |
// backward playback |
// backward playback |
951 |
|
|
952 |
unsigned long swapareastart = totalreadsamples; |
file_offset_t swapareastart = totalreadsamples; |
953 |
unsigned long loopoffset = GetPos() - loop.LoopStart; |
file_offset_t loopoffset = GetPos() - loop.LoopStart; |
954 |
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
file_offset_t samplestoreadinloop = Min(samplestoread, loopoffset); |
955 |
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
file_offset_t reverseplaybackend = GetPos() - samplestoreadinloop; |
956 |
|
|
957 |
SetPos(reverseplaybackend); |
SetPos(reverseplaybackend); |
958 |
|
|
1000 |
// reading, swap all sample frames so it reflects |
// reading, swap all sample frames so it reflects |
1001 |
// backward playback |
// backward playback |
1002 |
|
|
1003 |
unsigned long swapareastart = totalreadsamples; |
file_offset_t swapareastart = totalreadsamples; |
1004 |
unsigned long loopoffset = GetPos() - loop.LoopStart; |
file_offset_t loopoffset = GetPos() - loop.LoopStart; |
1005 |
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset) |
file_offset_t samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset) |
1006 |
: samplestoread; |
: samplestoread; |
1007 |
unsigned long reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); |
file_offset_t reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); |
1008 |
|
|
1009 |
SetPos(reverseplaybackend); |
SetPos(reverseplaybackend); |
1010 |
|
|
1084 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
1085 |
* @see SetPos(), CreateDecompressionBuffer() |
* @see SetPos(), CreateDecompressionBuffer() |
1086 |
*/ |
*/ |
1087 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer) { |
file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount, buffer_t* pExternalDecompressionBuffer) { |
1088 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
1089 |
if (!Compressed) { |
if (!Compressed) { |
1090 |
if (BitDepth == 24) { |
if (BitDepth == 24) { |
1099 |
else { |
else { |
1100 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
if (this->SamplePos >= this->SamplesTotal) return 0; |
1101 |
//TODO: efficiency: maybe we should test for an average compression rate |
//TODO: efficiency: maybe we should test for an average compression rate |
1102 |
unsigned long assumedsize = GuessSize(SampleCount), |
file_offset_t assumedsize = GuessSize(SampleCount), |
1103 |
remainingbytes = 0, // remaining bytes in the local buffer |
remainingbytes = 0, // remaining bytes in the local buffer |
1104 |
remainingsamples = SampleCount, |
remainingsamples = SampleCount, |
1105 |
copysamples, skipsamples, |
copysamples, skipsamples, |
1122 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
1123 |
|
|
1124 |
while (remainingsamples && remainingbytes) { |
while (remainingsamples && remainingbytes) { |
1125 |
unsigned long framesamples = SamplesPerFrame; |
file_offset_t framesamples = SamplesPerFrame; |
1126 |
unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; |
file_offset_t framebytes, rightChannelOffset = 0, nextFrameOffset; |
1127 |
|
|
1128 |
int mode_l = *pSrc++, mode_r = 0; |
int mode_l = *pSrc++, mode_r = 0; |
1129 |
|
|
1273 |
* @throws gig::Exception if sample is compressed |
* @throws gig::Exception if sample is compressed |
1274 |
* @see DLS::LoadSampleData() |
* @see DLS::LoadSampleData() |
1275 |
*/ |
*/ |
1276 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) { |
1277 |
if (Compressed) throw gig::Exception("There is no support for writing compressed gig samples (yet)"); |
if (Compressed) throw gig::Exception("There is no support for writing compressed gig samples (yet)"); |
1278 |
|
|
1279 |
// if this is the first write in this sample, reset the |
// if this is the first write in this sample, reset the |
1282 |
__resetCRC(crc); |
__resetCRC(crc); |
1283 |
} |
} |
1284 |
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"); |
1285 |
unsigned long res; |
file_offset_t res; |
1286 |
if (BitDepth == 24) { |
if (BitDepth == 24) { |
1287 |
res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
1288 |
} else { // 16 bit |
} else { // 16 bit |
1294 |
// if this is the last write, update the checksum chunk in the |
// if this is the last write, update the checksum chunk in the |
1295 |
// file |
// file |
1296 |
if (pCkData->GetPos() == pCkData->GetSize()) { |
if (pCkData->GetPos() == pCkData->GetSize()) { |
1297 |
|
__finalizeCRC(crc); |
1298 |
File* pFile = static_cast<File*>(GetParent()); |
File* pFile = static_cast<File*>(GetParent()); |
1299 |
pFile->SetSampleChecksum(this, __encodeCRC(crc)); |
pFile->SetSampleChecksum(this, crc); |
1300 |
} |
} |
1301 |
return res; |
return res; |
1302 |
} |
} |
1317 |
* @returns allocated decompression buffer |
* @returns allocated decompression buffer |
1318 |
* @see DestroyDecompressionBuffer() |
* @see DestroyDecompressionBuffer() |
1319 |
*/ |
*/ |
1320 |
buffer_t Sample::CreateDecompressionBuffer(unsigned long MaxReadSize) { |
buffer_t Sample::CreateDecompressionBuffer(file_offset_t MaxReadSize) { |
1321 |
buffer_t result; |
buffer_t result; |
1322 |
const double worstCaseHeaderOverhead = |
const double worstCaseHeaderOverhead = |
1323 |
(256.0 /*frame size*/ + 12.0 /*header*/ + 2.0 /*compression type flag (stereo)*/) / 256.0; |
(256.0 /*frame size*/ + 12.0 /*header*/ + 2.0 /*compression type flag (stereo)*/) / 256.0; |
1324 |
result.Size = (unsigned long) (double(MaxReadSize) * 3.0 /*(24 Bit)*/ * 2.0 /*stereo*/ * worstCaseHeaderOverhead); |
result.Size = (file_offset_t) (double(MaxReadSize) * 3.0 /*(24 Bit)*/ * 2.0 /*stereo*/ * worstCaseHeaderOverhead); |
1325 |
result.pStart = new int8_t[result.Size]; |
result.pStart = new int8_t[result.Size]; |
1326 |
result.NullExtensionSize = 0; |
result.NullExtensionSize = 0; |
1327 |
return result; |
return result; |
1355 |
return pGroup; |
return pGroup; |
1356 |
} |
} |
1357 |
|
|
1358 |
|
/** |
1359 |
|
* Returns the CRC-32 checksum of the sample's raw wave form data at the |
1360 |
|
* time when this sample's wave form data was modified for the last time |
1361 |
|
* by calling Write(). This checksum only covers the raw wave form data, |
1362 |
|
* not any meta informations like i.e. bit depth or loop points. Since |
1363 |
|
* this method just returns the checksum stored for this sample i.e. when |
1364 |
|
* the gig file was loaded, this method returns immediately. So it does no |
1365 |
|
* recalcuation of the checksum with the currently available sample wave |
1366 |
|
* form data. |
1367 |
|
* |
1368 |
|
* @see VerifyWaveData() |
1369 |
|
*/ |
1370 |
|
uint32_t Sample::GetWaveDataCRC32Checksum() { |
1371 |
|
return crc; |
1372 |
|
} |
1373 |
|
|
1374 |
|
/** |
1375 |
|
* Checks the integrity of this sample's raw audio wave data. Whenever a |
1376 |
|
* Sample's raw wave data is intentionally modified (i.e. by calling |
1377 |
|
* Write() and supplying the new raw audio wave form data) a CRC32 checksum |
1378 |
|
* is calculated and stored/updated for this sample, along to the sample's |
1379 |
|
* meta informations. |
1380 |
|
* |
1381 |
|
* Now by calling this method the current raw audio wave data is checked |
1382 |
|
* against the already stored CRC32 check sum in order to check whether the |
1383 |
|
* sample data had been damaged unintentionally for some reason. Since by |
1384 |
|
* calling this method always the entire raw audio wave data has to be |
1385 |
|
* read, verifying all samples this way may take a long time accordingly. |
1386 |
|
* And that's also the reason why the sample integrity is not checked by |
1387 |
|
* default whenever a gig file is loaded. So this method must be called |
1388 |
|
* explicitly to fulfill this task. |
1389 |
|
* |
1390 |
|
* @param pActually - (optional) if provided, will be set to the actually |
1391 |
|
* calculated checksum of the current raw wave form data, |
1392 |
|
* you can get the expected checksum instead by calling |
1393 |
|
* GetWaveDataCRC32Checksum() |
1394 |
|
* @returns true if sample is OK or false if the sample is damaged |
1395 |
|
* @throws Exception if no checksum had been stored to disk for this |
1396 |
|
* sample yet, or on I/O issues |
1397 |
|
* @see GetWaveDataCRC32Checksum() |
1398 |
|
*/ |
1399 |
|
bool Sample::VerifyWaveData(uint32_t* pActually) { |
1400 |
|
//File* pFile = static_cast<File*>(GetParent()); |
1401 |
|
uint32_t crc = CalculateWaveDataChecksum(); |
1402 |
|
if (pActually) *pActually = crc; |
1403 |
|
return crc == this->crc; |
1404 |
|
} |
1405 |
|
|
1406 |
|
uint32_t Sample::CalculateWaveDataChecksum() { |
1407 |
|
const size_t sz = 20*1024; // 20kB buffer size |
1408 |
|
std::vector<uint8_t> buffer(sz); |
1409 |
|
buffer.resize(sz); |
1410 |
|
|
1411 |
|
const size_t n = sz / FrameSize; |
1412 |
|
SetPos(0); |
1413 |
|
uint32_t crc = 0; |
1414 |
|
__resetCRC(crc); |
1415 |
|
while (true) { |
1416 |
|
file_offset_t nRead = Read(&buffer[0], n); |
1417 |
|
if (nRead <= 0) break; |
1418 |
|
__calculateCRC(&buffer[0], nRead * FrameSize, crc); |
1419 |
|
} |
1420 |
|
__finalizeCRC(crc); |
1421 |
|
return crc; |
1422 |
|
} |
1423 |
|
|
1424 |
Sample::~Sample() { |
Sample::~Sample() { |
1425 |
Instances--; |
Instances--; |
1426 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
1437 |
// *************** DimensionRegion *************** |
// *************** DimensionRegion *************** |
1438 |
// * |
// * |
1439 |
|
|
1440 |
uint DimensionRegion::Instances = 0; |
size_t DimensionRegion::Instances = 0; |
1441 |
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
1442 |
|
|
1443 |
DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
1631 |
EG2Attack = 0.0; |
EG2Attack = 0.0; |
1632 |
EG2Decay1 = 0.005; |
EG2Decay1 = 0.005; |
1633 |
EG2Sustain = 1000; |
EG2Sustain = 1000; |
1634 |
EG2Release = 0.3; |
EG2Release = 60; |
1635 |
LFO2ControlDepth = 0; |
LFO2ControlDepth = 0; |
1636 |
LFO2Frequency = 1.0; |
LFO2Frequency = 1.0; |
1637 |
LFO2InternalDepth = 0; |
LFO2InternalDepth = 0; |
1805 |
* |
* |
1806 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
1807 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
1808 |
|
* |
1809 |
|
* @param pProgress - callback function for progress notification |
1810 |
*/ |
*/ |
1811 |
void DimensionRegion::UpdateChunks() { |
void DimensionRegion::UpdateChunks(progress_t* pProgress) { |
1812 |
// first update base class's chunk |
// first update base class's chunk |
1813 |
DLS::Sampler::UpdateChunks(); |
DLS::Sampler::UpdateChunks(pProgress); |
1814 |
|
|
1815 |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
1816 |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
1830 |
|
|
1831 |
// update '3ewa' chunk with DimensionRegion's current settings |
// update '3ewa' chunk with DimensionRegion's current settings |
1832 |
|
|
1833 |
const uint32_t chunksize = _3ewa->GetNewSize(); |
const uint32_t chunksize = (uint32_t) _3ewa->GetNewSize(); |
1834 |
store32(&pData[0], chunksize); // unknown, always chunk size? |
store32(&pData[0], chunksize); // unknown, always chunk size? |
1835 |
|
|
1836 |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
3081 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
3082 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
3083 |
* |
* |
3084 |
|
* @param pProgress - callback function for progress notification |
3085 |
* @throws gig::Exception if samples cannot be dereferenced |
* @throws gig::Exception if samples cannot be dereferenced |
3086 |
*/ |
*/ |
3087 |
void Region::UpdateChunks() { |
void Region::UpdateChunks(progress_t* pProgress) { |
3088 |
// in the gig format we don't care about the Region's sample reference |
// in the gig format we don't care about the Region's sample reference |
3089 |
// but we still have to provide some existing one to not corrupt the |
// but we still have to provide some existing one to not corrupt the |
3090 |
// file, so to avoid the latter we simply always assign the sample of |
// file, so to avoid the latter we simply always assign the sample of |
3092 |
pSample = pDimensionRegions[0]->pSample; |
pSample = pDimensionRegions[0]->pSample; |
3093 |
|
|
3094 |
// first update base class's chunks |
// first update base class's chunks |
3095 |
DLS::Region::UpdateChunks(); |
DLS::Region::UpdateChunks(pProgress); |
3096 |
|
|
3097 |
// update dimension region's chunks |
// update dimension region's chunks |
3098 |
for (int i = 0; i < DimensionRegions; i++) { |
for (int i = 0; i < DimensionRegions; i++) { |
3099 |
pDimensionRegions[i]->UpdateChunks(); |
pDimensionRegions[i]->UpdateChunks(pProgress); |
3100 |
} |
} |
3101 |
|
|
3102 |
File* pFile = (File*) GetParent()->GetParent(); |
File* pFile = (File*) GetParent()->GetParent(); |
3186 |
int step = 1; |
int step = 1; |
3187 |
for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; |
for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; |
3188 |
int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; |
int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; |
|
int end = step * pDimensionDefinitions[veldim].zones; |
|
3189 |
|
|
3190 |
// loop through all dimension regions for all dimensions except the velocity dimension |
// loop through all dimension regions for all dimensions except the velocity dimension |
3191 |
int dim[8] = { 0 }; |
int dim[8] = { 0 }; |
3192 |
for (int i = 0 ; i < DimensionRegions ; i++) { |
for (int i = 0 ; i < DimensionRegions ; i++) { |
3193 |
|
const int end = i + step * pDimensionDefinitions[veldim].zones; |
3194 |
|
|
3195 |
|
// create a velocity table for all cases where the velocity zone is zero |
3196 |
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
3197 |
pDimensionRegions[i]->VelocityUpperLimit) { |
pDimensionRegions[i]->VelocityUpperLimit) { |
3198 |
// create the velocity table |
// create the velocity table |
3223 |
} |
} |
3224 |
} |
} |
3225 |
|
|
3226 |
|
// jump to the next case where the velocity zone is zero |
3227 |
int j; |
int j; |
3228 |
int shift = 0; |
int shift = 0; |
3229 |
for (j = 0 ; j < Dimensions ; j++) { |
for (j = 0 ; j < Dimensions ; j++) { |
3710 |
UpdateVelocityTable(); |
UpdateVelocityTable(); |
3711 |
} |
} |
3712 |
|
|
3713 |
|
/** @brief Change type of an existing dimension. |
3714 |
|
* |
3715 |
|
* Alters the dimension type of a dimension already existing on this |
3716 |
|
* region. If there is currently no dimension on this Region with type |
3717 |
|
* @a oldType, then this call with throw an Exception. Likewise there are |
3718 |
|
* cases where the requested dimension type cannot be performed. For example |
3719 |
|
* if the new dimension type shall be gig::dimension_samplechannel, and the |
3720 |
|
* current dimension has more than 2 zones. In such cases an Exception is |
3721 |
|
* thrown as well. |
3722 |
|
* |
3723 |
|
* @param oldType - identifies the existing dimension to be changed |
3724 |
|
* @param newType - to which dimension type it should be changed to |
3725 |
|
* @throws gig::Exception if requested change cannot be performed |
3726 |
|
*/ |
3727 |
|
void Region::SetDimensionType(dimension_t oldType, dimension_t newType) { |
3728 |
|
if (oldType == newType) return; |
3729 |
|
dimension_def_t* def = GetDimensionDefinition(oldType); |
3730 |
|
if (!def) |
3731 |
|
throw gig::Exception("No dimension with provided old dimension type exists on this region"); |
3732 |
|
if (newType == dimension_samplechannel && def->zones != 2) |
3733 |
|
throw gig::Exception("Cannot change to dimension type 'sample channel', because existing dimension does not have 2 zones"); |
3734 |
|
if (GetDimensionDefinition(newType)) |
3735 |
|
throw gig::Exception("There is already a dimension with requested new dimension type on this region"); |
3736 |
|
def->dimension = newType; |
3737 |
|
def->split_type = __resolveSplitType(newType); |
3738 |
|
} |
3739 |
|
|
3740 |
DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) { |
DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) { |
3741 |
uint8_t bits[8] = {}; |
uint8_t bits[8] = {}; |
3742 |
for (std::map<dimension_t,int>::const_iterator it = DimCase.begin(); |
for (std::map<dimension_t,int>::const_iterator it = DimCase.begin(); |
3798 |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
3799 |
uint8_t bits; |
uint8_t bits; |
3800 |
int veldim = -1; |
int veldim = -1; |
3801 |
int velbitpos; |
int velbitpos = 0; |
3802 |
int bitpos = 0; |
int bitpos = 0; |
3803 |
int dimregidx = 0; |
int dimregidx = 0; |
3804 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
3847 |
int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) { |
int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) { |
3848 |
uint8_t bits; |
uint8_t bits; |
3849 |
int veldim = -1; |
int veldim = -1; |
3850 |
int velbitpos; |
int velbitpos = 0; |
3851 |
int bitpos = 0; |
int bitpos = 0; |
3852 |
int dimregidx = 0; |
int dimregidx = 0; |
3853 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
3932 |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
3933 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
3934 |
if (!file->pWavePoolTable) return NULL; |
if (!file->pWavePoolTable) return NULL; |
3935 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
// for new files or files >= 2 GB use 64 bit wave pool offsets |
3936 |
unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
if (file->pRIFF->IsNew() || (file->pRIFF->GetCurrentFileSize() >> 31)) { |
3937 |
Sample* sample = file->GetFirstSample(pProgress); |
// use 64 bit wave pool offsets (treating this as large file) |
3938 |
while (sample) { |
uint64_t soughtoffset = |
3939 |
if (sample->ulWavePoolOffset == soughtoffset && |
uint64_t(file->pWavePoolTable[WavePoolTableIndex]) | |
3940 |
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(sample); |
uint64_t(file->pWavePoolTableHi[WavePoolTableIndex]) << 32; |
3941 |
sample = file->GetNextSample(); |
Sample* sample = file->GetFirstSample(pProgress); |
3942 |
|
while (sample) { |
3943 |
|
if (sample->ullWavePoolOffset == soughtoffset) |
3944 |
|
return static_cast<gig::Sample*>(sample); |
3945 |
|
sample = file->GetNextSample(); |
3946 |
|
} |
3947 |
|
} else { |
3948 |
|
// use extension files and 32 bit wave pool offsets |
3949 |
|
file_offset_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
3950 |
|
file_offset_t soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
3951 |
|
Sample* sample = file->GetFirstSample(pProgress); |
3952 |
|
while (sample) { |
3953 |
|
if (sample->ullWavePoolOffset == soughtoffset && |
3954 |
|
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(sample); |
3955 |
|
sample = file->GetNextSample(); |
3956 |
|
} |
3957 |
} |
} |
3958 |
return NULL; |
return NULL; |
3959 |
} |
} |
4196 |
// to handle potential future extensions of the header |
// to handle potential future extensions of the header |
4197 |
ckScri->SetPos(sizeof(int32_t) + headerSize); |
ckScri->SetPos(sizeof(int32_t) + headerSize); |
4198 |
// read actual script data |
// read actual script data |
4199 |
uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos(); |
uint32_t scriptSize = uint32_t(ckScri->GetSize() - ckScri->GetPos()); |
4200 |
data.resize(scriptSize); |
data.resize(scriptSize); |
4201 |
for (int i = 0; i < scriptSize; ++i) |
for (int i = 0; i < scriptSize; ++i) |
4202 |
data[i] = ckScri->ReadUint8(); |
data[i] = ckScri->ReadUint8(); |
4234 |
memcpy(&data[0], &text[0], text.size()); |
memcpy(&data[0], &text[0], text.size()); |
4235 |
} |
} |
4236 |
|
|
4237 |
void Script::UpdateChunks() { |
/** |
4238 |
|
* Apply this script to the respective RIFF chunks. You have to call |
4239 |
|
* File::Save() to make changes persistent. |
4240 |
|
* |
4241 |
|
* Usually there is absolutely no need to call this method explicitly. |
4242 |
|
* It will be called automatically when File::Save() was called. |
4243 |
|
* |
4244 |
|
* @param pProgress - callback function for progress notification |
4245 |
|
*/ |
4246 |
|
void Script::UpdateChunks(progress_t* pProgress) { |
4247 |
// recalculate CRC32 check sum |
// recalculate CRC32 check sum |
4248 |
__resetCRC(crc); |
__resetCRC(crc); |
4249 |
__calculateCRC(&data[0], data.size(), crc); |
__calculateCRC(&data[0], data.size(), crc); |
4250 |
__encodeCRC(crc); |
__finalizeCRC(crc); |
4251 |
// make sure chunk exists and has the required size |
// make sure chunk exists and has the required size |
4252 |
const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size(); |
const file_offset_t chunkSize = (file_offset_t) 7*sizeof(int32_t) + Name.size() + data.size(); |
4253 |
if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); |
if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); |
4254 |
else pChunk->Resize(chunkSize); |
else pChunk->Resize(chunkSize); |
4255 |
// fill the chunk data to be written to disk |
// fill the chunk data to be written to disk |
4256 |
uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); |
4257 |
int pos = 0; |
int pos = 0; |
4258 |
store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size |
store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size())); // total header size |
4259 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4260 |
store32(&pData[pos], Compression); |
store32(&pData[pos], Compression); |
4261 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4267 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4268 |
store32(&pData[pos], crc); |
store32(&pData[pos], crc); |
4269 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4270 |
store32(&pData[pos], Name.size()); |
store32(&pData[pos], (uint32_t) Name.size()); |
4271 |
pos += sizeof(int32_t); |
pos += sizeof(int32_t); |
4272 |
for (int i = 0; i < Name.size(); ++i, ++pos) |
for (int i = 0; i < Name.size(); ++i, ++pos) |
4273 |
pData[pos] = Name[i]; |
pData[pos] = Name[i]; |
4282 |
* @param pGroup - script's new group |
* @param pGroup - script's new group |
4283 |
*/ |
*/ |
4284 |
void Script::SetGroup(ScriptGroup* pGroup) { |
void Script::SetGroup(ScriptGroup* pGroup) { |
4285 |
if (this->pGroup = pGroup) return; |
if (this->pGroup == pGroup) return; |
4286 |
if (pChunk) |
if (pChunk) |
4287 |
pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList); |
pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList); |
4288 |
this->pGroup = pGroup; |
this->pGroup = pGroup; |
4333 |
} |
} |
4334 |
} |
} |
4335 |
|
|
4336 |
void ScriptGroup::UpdateChunks() { |
/** |
4337 |
|
* Apply this script group to the respective RIFF chunks. You have to call |
4338 |
|
* File::Save() to make changes persistent. |
4339 |
|
* |
4340 |
|
* Usually there is absolutely no need to call this method explicitly. |
4341 |
|
* It will be called automatically when File::Save() was called. |
4342 |
|
* |
4343 |
|
* @param pProgress - callback function for progress notification |
4344 |
|
*/ |
4345 |
|
void ScriptGroup::UpdateChunks(progress_t* pProgress) { |
4346 |
if (pScripts) { |
if (pScripts) { |
4347 |
if (!pList) |
if (!pList) |
4348 |
pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS); |
pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS); |
4353 |
for (std::list<Script*>::iterator it = pScripts->begin(); |
for (std::list<Script*>::iterator it = pScripts->begin(); |
4354 |
it != pScripts->end(); ++it) |
it != pScripts->end(); ++it) |
4355 |
{ |
{ |
4356 |
(*it)->UpdateChunks(); |
(*it)->UpdateChunks(pProgress); |
4357 |
} |
} |
4358 |
} |
} |
4359 |
} |
} |
4444 |
EffectSend = 0; |
EffectSend = 0; |
4445 |
Attenuation = 0; |
Attenuation = 0; |
4446 |
FineTune = 0; |
FineTune = 0; |
4447 |
PitchbendRange = 0; |
PitchbendRange = 2; |
4448 |
PianoReleaseMode = false; |
PianoReleaseMode = false; |
4449 |
DimensionKeyRange.low = 0; |
DimensionKeyRange.low = 0; |
4450 |
DimensionKeyRange.high = 0; |
DimensionKeyRange.high = 0; |
4516 |
if (lst3LS) { |
if (lst3LS) { |
4517 |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
4518 |
if (ckSCSL) { |
if (ckSCSL) { |
4519 |
int slotCount = ckSCSL->ReadUint32(); |
int headerSize = ckSCSL->ReadUint32(); |
4520 |
int slotSize = ckSCSL->ReadUint32(); |
int slotCount = ckSCSL->ReadUint32(); |
4521 |
int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future extensions |
if (slotCount) { |
4522 |
for (int i = 0; i < slotCount; ++i) { |
int slotSize = ckSCSL->ReadUint32(); |
4523 |
_ScriptPooolEntry e; |
ckSCSL->SetPos(headerSize); // in case of future header extensions |
4524 |
e.fileOffset = ckSCSL->ReadUint32(); |
int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions |
4525 |
e.bypass = ckSCSL->ReadUint32() & 1; |
for (int i = 0; i < slotCount; ++i) { |
4526 |
if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions |
_ScriptPooolEntry e; |
4527 |
scriptPoolFileOffsets.push_back(e); |
e.fileOffset = ckSCSL->ReadUint32(); |
4528 |
|
e.bypass = ckSCSL->ReadUint32() & 1; |
4529 |
|
if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions |
4530 |
|
scriptPoolFileOffsets.push_back(e); |
4531 |
|
} |
4532 |
} |
} |
4533 |
} |
} |
4534 |
} |
} |
4563 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
4564 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
4565 |
* |
* |
4566 |
|
* @param pProgress - callback function for progress notification |
4567 |
* @throws gig::Exception if samples cannot be dereferenced |
* @throws gig::Exception if samples cannot be dereferenced |
4568 |
*/ |
*/ |
4569 |
void Instrument::UpdateChunks() { |
void Instrument::UpdateChunks(progress_t* pProgress) { |
4570 |
// first update base classes' chunks |
// first update base classes' chunks |
4571 |
DLS::Instrument::UpdateChunks(); |
DLS::Instrument::UpdateChunks(pProgress); |
4572 |
|
|
4573 |
// update Regions' chunks |
// update Regions' chunks |
4574 |
{ |
{ |
4575 |
RegionList::iterator iter = pRegions->begin(); |
RegionList::iterator iter = pRegions->begin(); |
4576 |
RegionList::iterator end = pRegions->end(); |
RegionList::iterator end = pRegions->end(); |
4577 |
for (; iter != end; ++iter) |
for (; iter != end; ++iter) |
4578 |
(*iter)->UpdateChunks(); |
(*iter)->UpdateChunks(pProgress); |
4579 |
} |
} |
4580 |
|
|
4581 |
// make sure 'lart' RIFF list chunk exists |
// make sure 'lart' RIFF list chunk exists |
4612 |
} |
} |
4613 |
|
|
4614 |
// own gig format extensions |
// own gig format extensions |
4615 |
if (pScriptRefs) { |
if (ScriptSlotCount()) { |
4616 |
|
// make sure we have converted the original loaded script file |
4617 |
|
// offsets into valid Script object pointers |
4618 |
|
LoadScripts(); |
4619 |
|
|
4620 |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4621 |
if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); |
if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); |
4622 |
const int totalSize = pScriptRefs->size() * 2*sizeof(uint32_t); |
const int slotCount = (int) pScriptRefs->size(); |
4623 |
|
const int headerSize = 3 * sizeof(uint32_t); |
4624 |
|
const int slotSize = 2 * sizeof(uint32_t); |
4625 |
|
const int totalChunkSize = headerSize + slotCount * slotSize; |
4626 |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
4627 |
if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalSize); |
if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize); |
4628 |
else ckSCSL->Resize(totalSize); |
else ckSCSL->Resize(totalChunkSize); |
4629 |
uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData(); |
uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData(); |
4630 |
for (int i = 0, pos = 0; i < pScriptRefs->size(); ++i) { |
int pos = 0; |
4631 |
int fileOffset = |
store32(&pData[pos], headerSize); |
4632 |
(*pScriptRefs)[i].script->pChunk->GetFilePos() - |
pos += sizeof(uint32_t); |
4633 |
(*pScriptRefs)[i].script->pChunk->GetPos() - |
store32(&pData[pos], slotCount); |
4634 |
CHUNK_HEADER_SIZE; |
pos += sizeof(uint32_t); |
4635 |
store32(&pData[pos], fileOffset); |
store32(&pData[pos], slotSize); |
4636 |
|
pos += sizeof(uint32_t); |
4637 |
|
for (int i = 0; i < slotCount; ++i) { |
4638 |
|
// arbitrary value, the actual file offset will be updated in |
4639 |
|
// UpdateScriptFileOffsets() after the file has been resized |
4640 |
|
int bogusFileOffset = 0; |
4641 |
|
store32(&pData[pos], bogusFileOffset); |
4642 |
pos += sizeof(uint32_t); |
pos += sizeof(uint32_t); |
4643 |
store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0); |
store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0); |
4644 |
pos += sizeof(uint32_t); |
pos += sizeof(uint32_t); |
4645 |
} |
} |
4646 |
|
} else { |
4647 |
|
// no script slots, so get rid of any LS custom RIFF chunks (if any) |
4648 |
|
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4649 |
|
if (lst3LS) pCkInstrument->DeleteSubChunk(lst3LS); |
4650 |
} |
} |
4651 |
} |
} |
4652 |
|
|
4653 |
|
void Instrument::UpdateScriptFileOffsets() { |
4654 |
|
// own gig format extensions |
4655 |
|
if (pScriptRefs && pScriptRefs->size() > 0) { |
4656 |
|
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4657 |
|
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
4658 |
|
const int slotCount = (int) pScriptRefs->size(); |
4659 |
|
const int headerSize = 3 * sizeof(uint32_t); |
4660 |
|
ckSCSL->SetPos(headerSize); |
4661 |
|
for (int i = 0; i < slotCount; ++i) { |
4662 |
|
uint32_t fileOffset = uint32_t( |
4663 |
|
(*pScriptRefs)[i].script->pChunk->GetFilePos() - |
4664 |
|
(*pScriptRefs)[i].script->pChunk->GetPos() - |
4665 |
|
CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()) |
4666 |
|
); |
4667 |
|
ckSCSL->WriteUint32(&fileOffset); |
4668 |
|
// jump over flags entry (containing the bypass flag) |
4669 |
|
ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); |
4670 |
|
} |
4671 |
|
} |
4672 |
|
} |
4673 |
|
|
4674 |
/** |
/** |
4675 |
* Returns the appropriate Region for a triggered note. |
* Returns the appropriate Region for a triggered note. |
4676 |
* |
* |
4723 |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
4724 |
Region* pNewRegion = new Region(this, rgn); |
Region* pNewRegion = new Region(this, rgn); |
4725 |
pRegions->push_back(pNewRegion); |
pRegions->push_back(pNewRegion); |
4726 |
Regions = pRegions->size(); |
Regions = (uint32_t) pRegions->size(); |
4727 |
// update Region key table for fast lookup |
// update Region key table for fast lookup |
4728 |
UpdateRegionKeyTable(); |
UpdateRegionKeyTable(); |
4729 |
// done |
// done |
4738 |
} |
} |
4739 |
|
|
4740 |
/** |
/** |
4741 |
|
* Move this instrument at the position before @arg dst. |
4742 |
|
* |
4743 |
|
* This method can be used to reorder the sequence of instruments in a |
4744 |
|
* .gig file. This might be helpful especially on large .gig files which |
4745 |
|
* contain a large number of instruments within the same .gig file. So |
4746 |
|
* grouping such instruments to similar ones, can help to keep track of them |
4747 |
|
* when working with such complex .gig files. |
4748 |
|
* |
4749 |
|
* When calling this method, this instrument will be removed from in its |
4750 |
|
* current position in the instruments list and moved to the requested |
4751 |
|
* target position provided by @param dst. You may also pass NULL as |
4752 |
|
* argument to this method, in that case this intrument will be moved to the |
4753 |
|
* very end of the .gig file's instrument list. |
4754 |
|
* |
4755 |
|
* You have to call Save() to make the order change persistent to the .gig |
4756 |
|
* file. |
4757 |
|
* |
4758 |
|
* Currently this method is limited to moving the instrument within the same |
4759 |
|
* .gig file. Trying to move it to another .gig file by calling this method |
4760 |
|
* will throw an exception. |
4761 |
|
* |
4762 |
|
* @param dst - destination instrument at which this instrument will be |
4763 |
|
* moved to, or pass NULL for moving to end of list |
4764 |
|
* @throw gig::Exception if this instrument and target instrument are not |
4765 |
|
* part of the same file |
4766 |
|
*/ |
4767 |
|
void Instrument::MoveTo(Instrument* dst) { |
4768 |
|
if (dst && GetParent() != dst->GetParent()) |
4769 |
|
throw Exception( |
4770 |
|
"gig::Instrument::MoveTo() can only be used for moving within " |
4771 |
|
"the same gig file." |
4772 |
|
); |
4773 |
|
|
4774 |
|
File* pFile = (File*) GetParent(); |
4775 |
|
|
4776 |
|
// move this instrument within the instrument list |
4777 |
|
{ |
4778 |
|
File::InstrumentList& list = *pFile->pInstruments; |
4779 |
|
|
4780 |
|
File::InstrumentList::iterator itFrom = |
4781 |
|
std::find(list.begin(), list.end(), static_cast<DLS::Instrument*>(this)); |
4782 |
|
|
4783 |
|
File::InstrumentList::iterator itTo = |
4784 |
|
std::find(list.begin(), list.end(), static_cast<DLS::Instrument*>(dst)); |
4785 |
|
|
4786 |
|
list.splice(itTo, list, itFrom); |
4787 |
|
} |
4788 |
|
|
4789 |
|
// move the instrument's actual list RIFF chunk appropriately |
4790 |
|
RIFF::List* lstCkInstruments = pFile->pRIFF->GetSubList(LIST_TYPE_LINS); |
4791 |
|
lstCkInstruments->MoveSubChunk( |
4792 |
|
this->pCkInstrument, |
4793 |
|
(RIFF::Chunk*) ((dst) ? dst->pCkInstrument : NULL) |
4794 |
|
); |
4795 |
|
} |
4796 |
|
|
4797 |
|
/** |
4798 |
* Returns a MIDI rule of the instrument. |
* Returns a MIDI rule of the instrument. |
4799 |
* |
* |
4800 |
* The list of MIDI rules, at least in gig v3, always contains at |
* The list of MIDI rules, at least in gig v3, always contains at |
4863 |
if (scriptPoolFileOffsets.empty()) return; |
if (scriptPoolFileOffsets.empty()) return; |
4864 |
File* pFile = (File*) GetParent(); |
File* pFile = (File*) GetParent(); |
4865 |
for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) { |
for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) { |
4866 |
uint32_t offset = scriptPoolFileOffsets[k].fileOffset; |
uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset; |
4867 |
for (uint i = 0; pFile->GetScriptGroup(i); ++i) { |
for (uint i = 0; pFile->GetScriptGroup(i); ++i) { |
4868 |
ScriptGroup* group = pFile->GetScriptGroup(i); |
ScriptGroup* group = pFile->GetScriptGroup(i); |
4869 |
for (uint s = 0; group->GetScript(s); ++s) { |
for (uint s = 0; group->GetScript(s); ++s) { |
4870 |
Script* script = group->GetScript(s); |
Script* script = group->GetScript(s); |
4871 |
if (script->pChunk) { |
if (script->pChunk) { |
4872 |
script->pChunk->SetPos(0); |
uint32_t offset = uint32_t( |
4873 |
if (script->pChunk->GetFilePos() - |
script->pChunk->GetFilePos() - |
4874 |
script->pChunk->GetPos() - |
script->pChunk->GetPos() - |
4875 |
CHUNK_HEADER_SIZE == offset) |
CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()) |
4876 |
|
); |
4877 |
|
if (offset == soughtOffset) |
4878 |
{ |
{ |
4879 |
_ScriptPooolRef ref; |
_ScriptPooolRef ref; |
4880 |
ref.script = script; |
ref.script = script; |
4998 |
*/ |
*/ |
4999 |
void Instrument::RemoveScript(Script* pScript) { |
void Instrument::RemoveScript(Script* pScript) { |
5000 |
LoadScripts(); |
LoadScripts(); |
5001 |
for (int i = pScriptRefs->size() - 1; i >= 0; --i) { |
for (ssize_t i = pScriptRefs->size() - 1; i >= 0; --i) { |
5002 |
if ((*pScriptRefs)[i].script == pScript) { |
if ((*pScriptRefs)[i].script == pScript) { |
5003 |
pScriptRefs->erase( pScriptRefs->begin() + i ); |
pScriptRefs->erase( pScriptRefs->begin() + i ); |
5004 |
} |
} |
5020 |
* gigedit. |
* gigedit. |
5021 |
*/ |
*/ |
5022 |
uint Instrument::ScriptSlotCount() const { |
uint Instrument::ScriptSlotCount() const { |
5023 |
return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size(); |
return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size()); |
5024 |
} |
} |
5025 |
|
|
5026 |
/** @brief Whether script execution shall be skipped. |
/** @brief Whether script execution shall be skipped. |
5156 |
* |
* |
5157 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
5158 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
5159 |
|
* |
5160 |
|
* @param pProgress - callback function for progress notification |
5161 |
*/ |
*/ |
5162 |
void Group::UpdateChunks() { |
void Group::UpdateChunks(progress_t* pProgress) { |
5163 |
// make sure <3gri> and <3gnl> list chunks exist |
// make sure <3gri> and <3gnl> list chunks exist |
5164 |
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
5165 |
if (!_3gri) { |
if (!_3gri) { |
5436 |
int iTotalSamples = WavePoolCount; |
int iTotalSamples = WavePoolCount; |
5437 |
|
|
5438 |
// check if samples should be loaded from extension files |
// check if samples should be loaded from extension files |
5439 |
|
// (only for old gig files < 2 GB) |
5440 |
int lastFileNo = 0; |
int lastFileNo = 0; |
5441 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
if (!file->IsNew() && !(file->GetCurrentFileSize() >> 31)) { |
5442 |
if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; |
for (int i = 0 ; i < WavePoolCount ; i++) { |
5443 |
|
if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; |
5444 |
|
} |
5445 |
} |
} |
5446 |
String name(pRIFF->GetFileName()); |
String name(pRIFF->GetFileName()); |
5447 |
int nameLen = name.length(); |
int nameLen = (int) name.length(); |
5448 |
char suffix[6]; |
char suffix[6]; |
5449 |
if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; |
if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; |
5450 |
|
|
5451 |
for (int fileNo = 0 ; ; ) { |
for (int fileNo = 0 ; ; ) { |
5452 |
RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); |
5453 |
if (wvpl) { |
if (wvpl) { |
5454 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
file_offset_t wvplFileOffset = wvpl->GetFilePos(); |
5455 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
RIFF::List* wave = wvpl->GetFirstSubList(); |
5456 |
while (wave) { |
while (wave) { |
5457 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
5459 |
const float subprogress = (float) iSampleIndex / (float) iTotalSamples; |
const float subprogress = (float) iSampleIndex / (float) iTotalSamples; |
5460 |
__notify_progress(pProgress, subprogress); |
__notify_progress(pProgress, subprogress); |
5461 |
|
|
5462 |
unsigned long waveFileOffset = wave->GetFilePos(); |
file_offset_t waveFileOffset = wave->GetFilePos(); |
5463 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo, iSampleIndex)); |
5464 |
|
|
5465 |
iSampleIndex++; |
iSampleIndex++; |
5466 |
} |
} |
5698 |
if (!_3crc) return; |
if (!_3crc) return; |
5699 |
|
|
5700 |
// get the index of the sample |
// get the index of the sample |
5701 |
int iWaveIndex = -1; |
int iWaveIndex = GetWaveTableIndexOf(pSample); |
|
File::SampleList::iterator iter = pSamples->begin(); |
|
|
File::SampleList::iterator end = pSamples->end(); |
|
|
for (int index = 0; iter != end; ++iter, ++index) { |
|
|
if (*iter == pSample) { |
|
|
iWaveIndex = index; |
|
|
break; |
|
|
} |
|
|
} |
|
5702 |
if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); |
if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); |
5703 |
|
|
5704 |
// write the CRC-32 checksum to disk |
// write the CRC-32 checksum to disk |
5705 |
_3crc->SetPos(iWaveIndex * 8); |
_3crc->SetPos(iWaveIndex * 8); |
5706 |
uint32_t tmp = 1; |
uint32_t one = 1; |
5707 |
_3crc->WriteUint32(&tmp); // unknown, always 1? |
_3crc->WriteUint32(&one); // always 1 |
5708 |
_3crc->WriteUint32(&crc); |
_3crc->WriteUint32(&crc); |
5709 |
} |
} |
5710 |
|
|
5711 |
|
uint32_t File::GetSampleChecksum(Sample* pSample) { |
5712 |
|
// get the index of the sample |
5713 |
|
int iWaveIndex = GetWaveTableIndexOf(pSample); |
5714 |
|
if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); |
5715 |
|
|
5716 |
|
return GetSampleChecksumByIndex(iWaveIndex); |
5717 |
|
} |
5718 |
|
|
5719 |
|
uint32_t File::GetSampleChecksumByIndex(int index) { |
5720 |
|
if (index < 0) throw gig::Exception("Could not retrieve reference crc of sample, invalid wave pool index of sample"); |
5721 |
|
|
5722 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5723 |
|
if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5724 |
|
uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); |
5725 |
|
if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5726 |
|
|
5727 |
|
// read the CRC-32 checksum directly from disk |
5728 |
|
size_t pos = index * 8; |
5729 |
|
if (pos + 8 > _3crc->GetNewSize()) |
5730 |
|
throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); |
5731 |
|
|
5732 |
|
uint32_t one = load32(&pData[pos]); // always 1 |
5733 |
|
if (one != 1) |
5734 |
|
throw gig::Exception("Could not retrieve reference crc of sample, because reference checksum table is damaged"); |
5735 |
|
|
5736 |
|
return load32(&pData[pos+4]); |
5737 |
|
} |
5738 |
|
|
5739 |
|
int File::GetWaveTableIndexOf(gig::Sample* pSample) { |
5740 |
|
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5741 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5742 |
|
File::SampleList::iterator end = pSamples->end(); |
5743 |
|
for (int index = 0; iter != end; ++iter, ++index) |
5744 |
|
if (*iter == pSample) |
5745 |
|
return index; |
5746 |
|
return -1; |
5747 |
|
} |
5748 |
|
|
5749 |
|
/** |
5750 |
|
* Checks whether the file's "3CRC" chunk was damaged. This chunk contains |
5751 |
|
* the CRC32 check sums of all samples' raw wave data. |
5752 |
|
* |
5753 |
|
* @return true if 3CRC chunk is OK, or false if 3CRC chunk is damaged |
5754 |
|
*/ |
5755 |
|
bool File::VerifySampleChecksumTable() { |
5756 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5757 |
|
if (!_3crc) return false; |
5758 |
|
if (_3crc->GetNewSize() <= 0) return false; |
5759 |
|
if (_3crc->GetNewSize() % 8) return false; |
5760 |
|
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5761 |
|
if (_3crc->GetNewSize() != pSamples->size() * 8) return false; |
5762 |
|
|
5763 |
|
const file_offset_t n = _3crc->GetNewSize() / 8; |
5764 |
|
|
5765 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5766 |
|
if (!pData) return false; |
5767 |
|
|
5768 |
|
for (file_offset_t i = 0; i < n; ++i) { |
5769 |
|
uint32_t one = pData[i*2]; |
5770 |
|
if (one != 1) return false; |
5771 |
|
} |
5772 |
|
|
5773 |
|
return true; |
5774 |
|
} |
5775 |
|
|
5776 |
|
/** |
5777 |
|
* Recalculates CRC32 checksums for all samples and rebuilds this gig |
5778 |
|
* file's checksum table with those new checksums. This might usually |
5779 |
|
* just be necessary if the checksum table was damaged. |
5780 |
|
* |
5781 |
|
* @e IMPORTANT: The current implementation of this method only works |
5782 |
|
* with files that have not been modified since it was loaded, because |
5783 |
|
* it expects that no externally caused file structure changes are |
5784 |
|
* required! |
5785 |
|
* |
5786 |
|
* Due to the expectation above, this method is currently protected |
5787 |
|
* and actually only used by the command line tool "gigdump" yet. |
5788 |
|
* |
5789 |
|
* @returns true if Save() is required to be called after this call, |
5790 |
|
* false if no further action is required |
5791 |
|
*/ |
5792 |
|
bool File::RebuildSampleChecksumTable() { |
5793 |
|
// make sure sample chunks were scanned |
5794 |
|
if (!pSamples) GetFirstSample(); |
5795 |
|
|
5796 |
|
bool bRequiresSave = false; |
5797 |
|
|
5798 |
|
// make sure "3CRC" chunk exists with required size |
5799 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5800 |
|
if (!_3crc) { |
5801 |
|
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
5802 |
|
// the order of einf and 3crc is not the same in v2 and v3 |
5803 |
|
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
5804 |
|
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
5805 |
|
bRequiresSave = true; |
5806 |
|
} else if (_3crc->GetNewSize() != pSamples->size() * 8) { |
5807 |
|
_3crc->Resize(pSamples->size() * 8); |
5808 |
|
bRequiresSave = true; |
5809 |
|
} |
5810 |
|
|
5811 |
|
if (bRequiresSave) { // refill CRC table for all samples in RAM ... |
5812 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5813 |
|
{ |
5814 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5815 |
|
File::SampleList::iterator end = pSamples->end(); |
5816 |
|
for (; iter != end; ++iter) { |
5817 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
5818 |
|
int index = GetWaveTableIndexOf(pSample); |
5819 |
|
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5820 |
|
pData[index*2] = 1; // always 1 |
5821 |
|
pData[index*2+1] = pSample->CalculateWaveDataChecksum(); |
5822 |
|
} |
5823 |
|
} |
5824 |
|
} else { // no file structure changes necessary, so directly write to disk and we are done ... |
5825 |
|
// make sure file is in write mode |
5826 |
|
pRIFF->SetMode(RIFF::stream_mode_read_write); |
5827 |
|
{ |
5828 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5829 |
|
File::SampleList::iterator end = pSamples->end(); |
5830 |
|
for (; iter != end; ++iter) { |
5831 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
5832 |
|
int index = GetWaveTableIndexOf(pSample); |
5833 |
|
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5834 |
|
pSample->crc = pSample->CalculateWaveDataChecksum(); |
5835 |
|
SetSampleChecksum(pSample, pSample->crc); |
5836 |
|
} |
5837 |
|
} |
5838 |
|
} |
5839 |
|
|
5840 |
|
return bRequiresSave; |
5841 |
|
} |
5842 |
|
|
5843 |
Group* File::GetFirstGroup() { |
Group* File::GetFirstGroup() { |
5844 |
if (!pGroups) LoadGroups(); |
if (!pGroups) LoadGroups(); |
5845 |
// there must always be at least one group |
// there must always be at least one group |
6062 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
6063 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
6064 |
* |
* |
6065 |
|
* @param pProgress - callback function for progress notification |
6066 |
* @throws Exception - on errors |
* @throws Exception - on errors |
6067 |
*/ |
*/ |
6068 |
void File::UpdateChunks() { |
void File::UpdateChunks(progress_t* pProgress) { |
6069 |
bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; |
bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; |
6070 |
|
|
|
b64BitWavePoolOffsets = pVersion && pVersion->major == 3; |
|
|
|
|
6071 |
// update own gig format extension chunks |
// update own gig format extension chunks |
6072 |
// (not part of the GigaStudio 4 format) |
// (not part of the GigaStudio 4 format) |
6073 |
// |
RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS); |
6074 |
|
if (!lst3LS) { |
6075 |
|
lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS); |
6076 |
|
} |
6077 |
|
// Make sure <3LS > chunk is placed before <ptbl> chunk. The precise |
6078 |
|
// location of <3LS > is irrelevant, however it should be located |
6079 |
|
// before the actual wave data |
6080 |
|
RIFF::Chunk* ckPTBL = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
6081 |
|
pRIFF->MoveSubChunk(lst3LS, ckPTBL); |
6082 |
|
|
6083 |
// This must be performed before writing the chunks for instruments, |
// This must be performed before writing the chunks for instruments, |
6084 |
// because the instruments' script slots will write the file offsets |
// because the instruments' script slots will write the file offsets |
6085 |
// of the respective instrument script chunk as reference. |
// of the respective instrument script chunk as reference. |
6086 |
if (pScriptGroups) { |
if (pScriptGroups) { |
6087 |
RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS); |
// Update instrument script (group) chunks. |
6088 |
if (pScriptGroups->empty()) { |
for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin(); |
6089 |
if (lst3LS) pRIFF->DeleteSubChunk(lst3LS); |
it != pScriptGroups->end(); ++it) |
6090 |
} else { |
{ |
6091 |
if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS); |
(*it)->UpdateChunks(pProgress); |
|
|
|
|
// Update instrument script (group) chunks. |
|
|
|
|
|
for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin(); |
|
|
it != pScriptGroups->end(); ++it) |
|
|
{ |
|
|
(*it)->UpdateChunks(); |
|
|
} |
|
6092 |
} |
} |
6093 |
} |
} |
6094 |
|
|
6095 |
|
// in case no libgig custom format data was added, then remove the |
6096 |
|
// custom "3LS " chunk again |
6097 |
|
if (!lst3LS->CountSubChunks()) { |
6098 |
|
pRIFF->DeleteSubChunk(lst3LS); |
6099 |
|
lst3LS = NULL; |
6100 |
|
} |
6101 |
|
|
6102 |
// first update base class's chunks |
// first update base class's chunks |
6103 |
DLS::File::UpdateChunks(); |
DLS::File::UpdateChunks(pProgress); |
6104 |
|
|
6105 |
if (newFile) { |
if (newFile) { |
6106 |
// INFO was added by Resource::UpdateChunks - make sure it |
// INFO was added by Resource::UpdateChunks - make sure it |
6137 |
std::list<Group*>::iterator iter = pGroups->begin(); |
std::list<Group*>::iterator iter = pGroups->begin(); |
6138 |
std::list<Group*>::iterator end = pGroups->end(); |
std::list<Group*>::iterator end = pGroups->end(); |
6139 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
6140 |
(*iter)->UpdateChunks(); |
(*iter)->UpdateChunks(pProgress); |
6141 |
} |
} |
6142 |
} |
} |
6143 |
|
|
6157 |
// Note that there are several fields with unknown use. These |
// Note that there are several fields with unknown use. These |
6158 |
// are set to zero. |
// are set to zero. |
6159 |
|
|
6160 |
int sublen = pSamples->size() / 8 + 49; |
int sublen = int(pSamples->size() / 8 + 49); |
6161 |
int einfSize = (Instruments + 1) * sublen; |
int einfSize = (Instruments + 1) * sublen; |
6162 |
|
|
6163 |
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
6230 |
store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); |
store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); |
6231 |
// next 8 bytes unknown |
// next 8 bytes unknown |
6232 |
store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); |
store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); |
6233 |
store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); |
store32(&pData[(instrumentIdx + 1) * sublen + 40], (uint32_t) pSamples->size()); |
6234 |
// next 4 bytes unknown |
// next 4 bytes unknown |
6235 |
|
|
6236 |
totnbregions += instrument->Regions; |
totnbregions += instrument->Regions; |
6248 |
store32(&pData[24], totnbloops); |
store32(&pData[24], totnbloops); |
6249 |
// next 8 bytes unknown |
// next 8 bytes unknown |
6250 |
// next 4 bytes unknown, not always 0 |
// next 4 bytes unknown, not always 0 |
6251 |
store32(&pData[40], pSamples->size()); |
store32(&pData[40], (uint32_t) pSamples->size()); |
6252 |
// next 4 bytes unknown |
// next 4 bytes unknown |
6253 |
} |
} |
6254 |
|
|
6255 |
// update 3crc chunk |
// update 3crc chunk |
6256 |
|
|
6257 |
// The 3crc chunk contains CRC-32 checksums for the |
// The 3crc chunk contains CRC-32 checksums for the |
6258 |
// samples. The actual checksum values will be filled in |
// samples. When saving a gig file to disk, we first update the 3CRC |
6259 |
// later, by Sample::Write. |
// chunk here (in RAM) with the old crc values which we read from the |
6260 |
|
// 3CRC chunk when we opened the file (available with gig::Sample::crc |
6261 |
|
// member variable). This step is required, because samples might have |
6262 |
|
// been deleted by the user since the file was opened, which in turn |
6263 |
|
// changes the order of the (i.e. old) checksums within the 3crc chunk. |
6264 |
|
// If a sample was conciously modified by the user (that is if |
6265 |
|
// Sample::Write() was called later on) then Sample::Write() will just |
6266 |
|
// update the respective individual checksum(s) directly on disk and |
6267 |
|
// leaves all other sample checksums untouched. |
6268 |
|
|
6269 |
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
6270 |
if (_3crc) { |
if (_3crc) { |
6271 |
_3crc->Resize(pSamples->size() * 8); |
_3crc->Resize(pSamples->size() * 8); |
6272 |
} else if (newFile) { |
} else /*if (newFile)*/ { |
6273 |
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
|
_3crc->LoadChunkData(); |
|
|
|
|
6274 |
// the order of einf and 3crc is not the same in v2 and v3 |
// the order of einf and 3crc is not the same in v2 and v3 |
6275 |
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
6276 |
} |
} |
6277 |
|
{ // must be performed in RAM here ... |
6278 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
6279 |
|
if (pData) { |
6280 |
|
File::SampleList::iterator iter = pSamples->begin(); |
6281 |
|
File::SampleList::iterator end = pSamples->end(); |
6282 |
|
for (int index = 0; iter != end; ++iter, ++index) { |
6283 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
6284 |
|
pData[index*2] = 1; // always 1 |
6285 |
|
pData[index*2+1] = pSample->crc; |
6286 |
|
} |
6287 |
|
} |
6288 |
|
} |
6289 |
|
} |
6290 |
|
|
6291 |
|
void File::UpdateFileOffsets() { |
6292 |
|
DLS::File::UpdateFileOffsets(); |
6293 |
|
|
6294 |
|
for (Instrument* instrument = GetFirstInstrument(); instrument; |
6295 |
|
instrument = GetNextInstrument()) |
6296 |
|
{ |
6297 |
|
instrument->UpdateScriptFileOffsets(); |
6298 |
|
} |
6299 |
} |
} |
6300 |
|
|
6301 |
/** |
/** |