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 |
|
|
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. |
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 |
*/ |
*/ |
342 |
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) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
343 |
static const DLS::Info::string_length_t fixedStringLengths[] = { |
static const DLS::Info::string_length_t fixedStringLengths[] = { |
344 |
{ CHUNK_ID_INAM, 64 }, |
{ CHUNK_ID_INAM, 64 }, |
345 |
{ 0, 0 } |
{ 0, 0 } |
481 |
const int iReadAtOnce = 32*1024; |
const int iReadAtOnce = 32*1024; |
482 |
char* buf = new char[iReadAtOnce * orig->FrameSize]; |
char* buf = new char[iReadAtOnce * orig->FrameSize]; |
483 |
Sample* pOrig = (Sample*) orig; //HACK: remove constness for now |
Sample* pOrig = (Sample*) orig; //HACK: remove constness for now |
484 |
unsigned long restorePos = pOrig->GetPos(); |
file_offset_t restorePos = pOrig->GetPos(); |
485 |
pOrig->SetPos(0); |
pOrig->SetPos(0); |
486 |
SetPos(0); |
SetPos(0); |
487 |
for (unsigned long n = pOrig->Read(buf, iReadAtOnce); n; |
for (file_offset_t n = pOrig->Read(buf, iReadAtOnce); n; |
488 |
n = pOrig->Read(buf, iReadAtOnce)) |
n = pOrig->Read(buf, iReadAtOnce)) |
489 |
{ |
{ |
490 |
Write(buf, n); |
Write(buf, n); |
500 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
501 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
502 |
* |
* |
503 |
|
* @param pProgress - callback function for progress notification |
504 |
* @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 |
505 |
* was provided yet |
* was provided yet |
506 |
* @throws gig::Exception if there is any invalid sample setting |
* @throws gig::Exception if there is any invalid sample setting |
507 |
*/ |
*/ |
508 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks(progress_t* pProgress) { |
509 |
// first update base class's chunks |
// first update base class's chunks |
510 |
DLS::Sample::UpdateChunks(); |
DLS::Sample::UpdateChunks(pProgress); |
511 |
|
|
512 |
// make sure 'smpl' chunk exists |
// make sure 'smpl' chunk exists |
513 |
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
569 |
void Sample::ScanCompressedSample() { |
void Sample::ScanCompressedSample() { |
570 |
//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) |
571 |
this->SamplesTotal = 0; |
this->SamplesTotal = 0; |
572 |
std::list<unsigned long> frameOffsets; |
std::list<file_offset_t> frameOffsets; |
573 |
|
|
574 |
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
575 |
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
585 |
const int mode_l = pCkData->ReadUint8(); |
const int mode_l = pCkData->ReadUint8(); |
586 |
const int mode_r = pCkData->ReadUint8(); |
const int mode_r = pCkData->ReadUint8(); |
587 |
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"); |
588 |
const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
const file_offset_t frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
589 |
|
|
590 |
if (pCkData->RemainingBytes() <= frameSize) { |
if (pCkData->RemainingBytes() <= frameSize) { |
591 |
SamplesInLastFrame = |
SamplesInLastFrame = |
604 |
|
|
605 |
const int mode = pCkData->ReadUint8(); |
const int mode = pCkData->ReadUint8(); |
606 |
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
607 |
const unsigned long frameSize = bytesPerFrame[mode]; |
const file_offset_t frameSize = bytesPerFrame[mode]; |
608 |
|
|
609 |
if (pCkData->RemainingBytes() <= frameSize) { |
if (pCkData->RemainingBytes() <= frameSize) { |
610 |
SamplesInLastFrame = |
SamplesInLastFrame = |
620 |
|
|
621 |
// 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) |
622 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
623 |
FrameTable = new unsigned long[frameOffsets.size()]; |
FrameTable = new file_offset_t[frameOffsets.size()]; |
624 |
std::list<unsigned long>::iterator end = frameOffsets.end(); |
std::list<file_offset_t>::iterator end = frameOffsets.end(); |
625 |
std::list<unsigned long>::iterator iter = frameOffsets.begin(); |
std::list<file_offset_t>::iterator iter = frameOffsets.begin(); |
626 |
for (int i = 0; iter != end; i++, iter++) { |
for (int i = 0; iter != end; i++, iter++) { |
627 |
FrameTable[i] = *iter; |
FrameTable[i] = *iter; |
628 |
} |
} |
663 |
* the cached sample data in bytes |
* the cached sample data in bytes |
664 |
* @see ReleaseSampleData(), Read(), SetPos() |
* @see ReleaseSampleData(), Read(), SetPos() |
665 |
*/ |
*/ |
666 |
buffer_t Sample::LoadSampleData(unsigned long SampleCount) { |
buffer_t Sample::LoadSampleData(file_offset_t SampleCount) { |
667 |
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
668 |
} |
} |
669 |
|
|
722 |
* size of the cached sample data in bytes |
* size of the cached sample data in bytes |
723 |
* @see ReleaseSampleData(), Read(), SetPos() |
* @see ReleaseSampleData(), Read(), SetPos() |
724 |
*/ |
*/ |
725 |
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) { |
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(file_offset_t SampleCount, uint NullSamplesCount) { |
726 |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
727 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
728 |
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
file_offset_t allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
729 |
SetPos(0); // reset read position to begin of sample |
SetPos(0); // reset read position to begin of sample |
730 |
RAMCache.pStart = new int8_t[allocationsize]; |
RAMCache.pStart = new int8_t[allocationsize]; |
731 |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
789 |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
790 |
* other formats will fail! |
* other formats will fail! |
791 |
* |
* |
792 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param NewSize - new sample wave data size in sample points (must be |
793 |
* greater than zero) |
* greater than zero) |
794 |
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
795 |
* or if \a iNewSize is less than 1 |
* @throws DLS::Exception if \a NewSize is less than 1 or unrealistic large |
796 |
* @throws gig::Exception if existing sample is compressed |
* @throws gig::Exception if existing sample is compressed |
797 |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
798 |
* DLS::Sample::FormatTag, File::Save() |
* DLS::Sample::FormatTag, File::Save() |
799 |
*/ |
*/ |
800 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(file_offset_t NewSize) { |
801 |
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)"); |
802 |
DLS::Sample::Resize(iNewSize); |
DLS::Sample::Resize(NewSize); |
803 |
} |
} |
804 |
|
|
805 |
/** |
/** |
823 |
* @returns the new sample position |
* @returns the new sample position |
824 |
* @see Read() |
* @see Read() |
825 |
*/ |
*/ |
826 |
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) { |
827 |
if (Compressed) { |
if (Compressed) { |
828 |
switch (Whence) { |
switch (Whence) { |
829 |
case RIFF::stream_curpos: |
case RIFF::stream_curpos: |
841 |
} |
} |
842 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
843 |
|
|
844 |
unsigned long frame = this->SamplePos / 2048; // to which frame to jump |
file_offset_t frame = this->SamplePos / 2048; // to which frame to jump |
845 |
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
846 |
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 |
847 |
return this->SamplePos; |
return this->SamplePos; |
848 |
} |
} |
849 |
else { // not compressed |
else { // not compressed |
850 |
unsigned long orderedBytes = SampleCount * this->FrameSize; |
file_offset_t orderedBytes = SampleCount * this->FrameSize; |
851 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
file_offset_t result = pCkData->SetPos(orderedBytes, Whence); |
852 |
return (result == orderedBytes) ? SampleCount |
return (result == orderedBytes) ? SampleCount |
853 |
: result / this->FrameSize; |
: result / this->FrameSize; |
854 |
} |
} |
857 |
/** |
/** |
858 |
* Returns the current position in the sample (in sample points). |
* Returns the current position in the sample (in sample points). |
859 |
*/ |
*/ |
860 |
unsigned long Sample::GetPos() const { |
file_offset_t Sample::GetPos() const { |
861 |
if (Compressed) return SamplePos; |
if (Compressed) return SamplePos; |
862 |
else return pCkData->GetPos() / FrameSize; |
else return pCkData->GetPos() / FrameSize; |
863 |
} |
} |
896 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
897 |
* @see CreateDecompressionBuffer() |
* @see CreateDecompressionBuffer() |
898 |
*/ |
*/ |
899 |
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, |
900 |
DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { |
DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { |
901 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
file_offset_t samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
902 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
903 |
|
|
904 |
SetPos(pPlaybackState->position); // recover position from the last time |
SetPos(pPlaybackState->position); // recover position from the last time |
936 |
// reading, swap all sample frames so it reflects |
// reading, swap all sample frames so it reflects |
937 |
// backward playback |
// backward playback |
938 |
|
|
939 |
unsigned long swapareastart = totalreadsamples; |
file_offset_t swapareastart = totalreadsamples; |
940 |
unsigned long loopoffset = GetPos() - loop.LoopStart; |
file_offset_t loopoffset = GetPos() - loop.LoopStart; |
941 |
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
file_offset_t samplestoreadinloop = Min(samplestoread, loopoffset); |
942 |
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
file_offset_t reverseplaybackend = GetPos() - samplestoreadinloop; |
943 |
|
|
944 |
SetPos(reverseplaybackend); |
SetPos(reverseplaybackend); |
945 |
|
|
987 |
// reading, swap all sample frames so it reflects |
// reading, swap all sample frames so it reflects |
988 |
// backward playback |
// backward playback |
989 |
|
|
990 |
unsigned long swapareastart = totalreadsamples; |
file_offset_t swapareastart = totalreadsamples; |
991 |
unsigned long loopoffset = GetPos() - loop.LoopStart; |
file_offset_t loopoffset = GetPos() - loop.LoopStart; |
992 |
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) |
993 |
: samplestoread; |
: samplestoread; |
994 |
unsigned long reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); |
file_offset_t reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); |
995 |
|
|
996 |
SetPos(reverseplaybackend); |
SetPos(reverseplaybackend); |
997 |
|
|
1071 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
1072 |
* @see SetPos(), CreateDecompressionBuffer() |
* @see SetPos(), CreateDecompressionBuffer() |
1073 |
*/ |
*/ |
1074 |
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) { |
1075 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
1076 |
if (!Compressed) { |
if (!Compressed) { |
1077 |
if (BitDepth == 24) { |
if (BitDepth == 24) { |
1086 |
else { |
else { |
1087 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
if (this->SamplePos >= this->SamplesTotal) return 0; |
1088 |
//TODO: efficiency: maybe we should test for an average compression rate |
//TODO: efficiency: maybe we should test for an average compression rate |
1089 |
unsigned long assumedsize = GuessSize(SampleCount), |
file_offset_t assumedsize = GuessSize(SampleCount), |
1090 |
remainingbytes = 0, // remaining bytes in the local buffer |
remainingbytes = 0, // remaining bytes in the local buffer |
1091 |
remainingsamples = SampleCount, |
remainingsamples = SampleCount, |
1092 |
copysamples, skipsamples, |
copysamples, skipsamples, |
1109 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
1110 |
|
|
1111 |
while (remainingsamples && remainingbytes) { |
while (remainingsamples && remainingbytes) { |
1112 |
unsigned long framesamples = SamplesPerFrame; |
file_offset_t framesamples = SamplesPerFrame; |
1113 |
unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; |
file_offset_t framebytes, rightChannelOffset = 0, nextFrameOffset; |
1114 |
|
|
1115 |
int mode_l = *pSrc++, mode_r = 0; |
int mode_l = *pSrc++, mode_r = 0; |
1116 |
|
|
1260 |
* @throws gig::Exception if sample is compressed |
* @throws gig::Exception if sample is compressed |
1261 |
* @see DLS::LoadSampleData() |
* @see DLS::LoadSampleData() |
1262 |
*/ |
*/ |
1263 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) { |
1264 |
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)"); |
1265 |
|
|
1266 |
// if this is the first write in this sample, reset the |
// if this is the first write in this sample, reset the |
1269 |
__resetCRC(crc); |
__resetCRC(crc); |
1270 |
} |
} |
1271 |
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"); |
1272 |
unsigned long res; |
file_offset_t res; |
1273 |
if (BitDepth == 24) { |
if (BitDepth == 24) { |
1274 |
res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
1275 |
} else { // 16 bit |
} else { // 16 bit |
1303 |
* @returns allocated decompression buffer |
* @returns allocated decompression buffer |
1304 |
* @see DestroyDecompressionBuffer() |
* @see DestroyDecompressionBuffer() |
1305 |
*/ |
*/ |
1306 |
buffer_t Sample::CreateDecompressionBuffer(unsigned long MaxReadSize) { |
buffer_t Sample::CreateDecompressionBuffer(file_offset_t MaxReadSize) { |
1307 |
buffer_t result; |
buffer_t result; |
1308 |
const double worstCaseHeaderOverhead = |
const double worstCaseHeaderOverhead = |
1309 |
(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; |
1310 |
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); |
1311 |
result.pStart = new int8_t[result.Size]; |
result.pStart = new int8_t[result.Size]; |
1312 |
result.NullExtensionSize = 0; |
result.NullExtensionSize = 0; |
1313 |
return result; |
return result; |
1341 |
return pGroup; |
return pGroup; |
1342 |
} |
} |
1343 |
|
|
1344 |
|
/** |
1345 |
|
* Checks the integrity of this sample's raw audio wave data. Whenever a |
1346 |
|
* Sample's raw wave data is intentionally modified (i.e. by calling |
1347 |
|
* Write() and supplying the new raw audio wave form data) a CRC32 checksum |
1348 |
|
* is calculated and stored/updated for this sample, along to the sample's |
1349 |
|
* meta informations. |
1350 |
|
* |
1351 |
|
* Now by calling this method the current raw audio wave data is checked |
1352 |
|
* against the already stored CRC32 check sum in order to check whether the |
1353 |
|
* sample data had been damaged unintentionally for some reason. Since by |
1354 |
|
* calling this method always the entire raw audio wave data has to be |
1355 |
|
* read, verifying all samples this way may take a long time accordingly. |
1356 |
|
* And that's also the reason why the sample integrity is not checked by |
1357 |
|
* default whenever a gig file is loaded. So this method must be called |
1358 |
|
* explicitly to fulfill this task. |
1359 |
|
* |
1360 |
|
* @returns true if sample is OK or false if the sample is damaged |
1361 |
|
* @throws Exception if no checksum had been stored to disk for this |
1362 |
|
* sample yet, or on I/O issues |
1363 |
|
*/ |
1364 |
|
bool Sample::VerifyWaveData() { |
1365 |
|
File* pFile = static_cast<File*>(GetParent()); |
1366 |
|
const uint32_t referenceCRC = pFile->GetSampleChecksum(this); |
1367 |
|
uint32_t crc = CalculateWaveDataChecksum(); |
1368 |
|
return crc == referenceCRC; |
1369 |
|
} |
1370 |
|
|
1371 |
|
uint32_t Sample::CalculateWaveDataChecksum() { |
1372 |
|
const size_t sz = 20*1024; // 20kB buffer size |
1373 |
|
std::vector<uint8_t> buffer(sz); |
1374 |
|
buffer.resize(sz); |
1375 |
|
|
1376 |
|
const size_t n = sz / FrameSize; |
1377 |
|
SetPos(0); |
1378 |
|
uint32_t crc = 0; |
1379 |
|
__resetCRC(crc); |
1380 |
|
while (true) { |
1381 |
|
file_offset_t nRead = Read(&buffer[0], n); |
1382 |
|
if (nRead <= 0) break; |
1383 |
|
__calculateCRC(&buffer[0], nRead * FrameSize, crc); |
1384 |
|
} |
1385 |
|
__encodeCRC(crc); |
1386 |
|
return crc; |
1387 |
|
} |
1388 |
|
|
1389 |
Sample::~Sample() { |
Sample::~Sample() { |
1390 |
Instances--; |
Instances--; |
1391 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
1402 |
// *************** DimensionRegion *************** |
// *************** DimensionRegion *************** |
1403 |
// * |
// * |
1404 |
|
|
1405 |
uint DimensionRegion::Instances = 0; |
size_t DimensionRegion::Instances = 0; |
1406 |
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
1407 |
|
|
1408 |
DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
1770 |
* |
* |
1771 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
1772 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
1773 |
|
* |
1774 |
|
* @param pProgress - callback function for progress notification |
1775 |
*/ |
*/ |
1776 |
void DimensionRegion::UpdateChunks() { |
void DimensionRegion::UpdateChunks(progress_t* pProgress) { |
1777 |
// first update base class's chunk |
// first update base class's chunk |
1778 |
DLS::Sampler::UpdateChunks(); |
DLS::Sampler::UpdateChunks(pProgress); |
1779 |
|
|
1780 |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
1781 |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
3046 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
3047 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
3048 |
* |
* |
3049 |
|
* @param pProgress - callback function for progress notification |
3050 |
* @throws gig::Exception if samples cannot be dereferenced |
* @throws gig::Exception if samples cannot be dereferenced |
3051 |
*/ |
*/ |
3052 |
void Region::UpdateChunks() { |
void Region::UpdateChunks(progress_t* pProgress) { |
3053 |
// 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 |
3054 |
// 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 |
3055 |
// 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 |
3057 |
pSample = pDimensionRegions[0]->pSample; |
pSample = pDimensionRegions[0]->pSample; |
3058 |
|
|
3059 |
// first update base class's chunks |
// first update base class's chunks |
3060 |
DLS::Region::UpdateChunks(); |
DLS::Region::UpdateChunks(pProgress); |
3061 |
|
|
3062 |
// update dimension region's chunks |
// update dimension region's chunks |
3063 |
for (int i = 0; i < DimensionRegions; i++) { |
for (int i = 0; i < DimensionRegions; i++) { |
3064 |
pDimensionRegions[i]->UpdateChunks(); |
pDimensionRegions[i]->UpdateChunks(pProgress); |
3065 |
} |
} |
3066 |
|
|
3067 |
File* pFile = (File*) GetParent()->GetParent(); |
File* pFile = (File*) GetParent()->GetParent(); |
3151 |
int step = 1; |
int step = 1; |
3152 |
for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; |
for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; |
3153 |
int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; |
int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; |
|
int end = step * pDimensionDefinitions[veldim].zones; |
|
3154 |
|
|
3155 |
// loop through all dimension regions for all dimensions except the velocity dimension |
// loop through all dimension regions for all dimensions except the velocity dimension |
3156 |
int dim[8] = { 0 }; |
int dim[8] = { 0 }; |
3157 |
for (int i = 0 ; i < DimensionRegions ; i++) { |
for (int i = 0 ; i < DimensionRegions ; i++) { |
3158 |
|
const int end = i + step * pDimensionDefinitions[veldim].zones; |
3159 |
|
|
3160 |
|
// create a velocity table for all cases where the velocity zone is zero |
3161 |
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
3162 |
pDimensionRegions[i]->VelocityUpperLimit) { |
pDimensionRegions[i]->VelocityUpperLimit) { |
3163 |
// create the velocity table |
// create the velocity table |
3188 |
} |
} |
3189 |
} |
} |
3190 |
|
|
3191 |
|
// jump to the next case where the velocity zone is zero |
3192 |
int j; |
int j; |
3193 |
int shift = 0; |
int shift = 0; |
3194 |
for (j = 0 ; j < Dimensions ; j++) { |
for (j = 0 ; j < Dimensions ; j++) { |
3897 |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
3898 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
3899 |
if (!file->pWavePoolTable) return NULL; |
if (!file->pWavePoolTable) return NULL; |
3900 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
// for new files or files >= 2 GB use 64 bit wave pool offsets |
3901 |
unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
if (file->pRIFF->IsNew() || (file->pRIFF->GetCurrentFileSize() >> 31)) { |
3902 |
Sample* sample = file->GetFirstSample(pProgress); |
// use 64 bit wave pool offsets (treating this as large file) |
3903 |
while (sample) { |
uint64_t soughtoffset = |
3904 |
if (sample->ulWavePoolOffset == soughtoffset && |
uint64_t(file->pWavePoolTable[WavePoolTableIndex]) | |
3905 |
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(sample); |
uint64_t(file->pWavePoolTableHi[WavePoolTableIndex]) << 32; |
3906 |
sample = file->GetNextSample(); |
Sample* sample = file->GetFirstSample(pProgress); |
3907 |
|
while (sample) { |
3908 |
|
if (sample->ullWavePoolOffset == soughtoffset) |
3909 |
|
return static_cast<gig::Sample*>(sample); |
3910 |
|
sample = file->GetNextSample(); |
3911 |
|
} |
3912 |
|
} else { |
3913 |
|
// use extension files and 32 bit wave pool offsets |
3914 |
|
file_offset_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
3915 |
|
file_offset_t soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
3916 |
|
Sample* sample = file->GetFirstSample(pProgress); |
3917 |
|
while (sample) { |
3918 |
|
if (sample->ullWavePoolOffset == soughtoffset && |
3919 |
|
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(sample); |
3920 |
|
sample = file->GetNextSample(); |
3921 |
|
} |
3922 |
} |
} |
3923 |
return NULL; |
return NULL; |
3924 |
} |
} |
4199 |
memcpy(&data[0], &text[0], text.size()); |
memcpy(&data[0], &text[0], text.size()); |
4200 |
} |
} |
4201 |
|
|
4202 |
void Script::UpdateChunks() { |
/** |
4203 |
|
* Apply this script to the respective RIFF chunks. You have to call |
4204 |
|
* File::Save() to make changes persistent. |
4205 |
|
* |
4206 |
|
* Usually there is absolutely no need to call this method explicitly. |
4207 |
|
* It will be called automatically when File::Save() was called. |
4208 |
|
* |
4209 |
|
* @param pProgress - callback function for progress notification |
4210 |
|
*/ |
4211 |
|
void Script::UpdateChunks(progress_t* pProgress) { |
4212 |
// recalculate CRC32 check sum |
// recalculate CRC32 check sum |
4213 |
__resetCRC(crc); |
__resetCRC(crc); |
4214 |
__calculateCRC(&data[0], data.size(), crc); |
__calculateCRC(&data[0], data.size(), crc); |
4247 |
* @param pGroup - script's new group |
* @param pGroup - script's new group |
4248 |
*/ |
*/ |
4249 |
void Script::SetGroup(ScriptGroup* pGroup) { |
void Script::SetGroup(ScriptGroup* pGroup) { |
4250 |
if (this->pGroup = pGroup) return; |
if (this->pGroup == pGroup) return; |
4251 |
if (pChunk) |
if (pChunk) |
4252 |
pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList); |
pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList); |
4253 |
this->pGroup = pGroup; |
this->pGroup = pGroup; |
4298 |
} |
} |
4299 |
} |
} |
4300 |
|
|
4301 |
void ScriptGroup::UpdateChunks() { |
/** |
4302 |
|
* Apply this script group to the respective RIFF chunks. You have to call |
4303 |
|
* File::Save() to make changes persistent. |
4304 |
|
* |
4305 |
|
* Usually there is absolutely no need to call this method explicitly. |
4306 |
|
* It will be called automatically when File::Save() was called. |
4307 |
|
* |
4308 |
|
* @param pProgress - callback function for progress notification |
4309 |
|
*/ |
4310 |
|
void ScriptGroup::UpdateChunks(progress_t* pProgress) { |
4311 |
if (pScripts) { |
if (pScripts) { |
4312 |
if (!pList) |
if (!pList) |
4313 |
pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS); |
pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS); |
4318 |
for (std::list<Script*>::iterator it = pScripts->begin(); |
for (std::list<Script*>::iterator it = pScripts->begin(); |
4319 |
it != pScripts->end(); ++it) |
it != pScripts->end(); ++it) |
4320 |
{ |
{ |
4321 |
(*it)->UpdateChunks(); |
(*it)->UpdateChunks(pProgress); |
4322 |
} |
} |
4323 |
} |
} |
4324 |
} |
} |
4528 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
4529 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
4530 |
* |
* |
4531 |
|
* @param pProgress - callback function for progress notification |
4532 |
* @throws gig::Exception if samples cannot be dereferenced |
* @throws gig::Exception if samples cannot be dereferenced |
4533 |
*/ |
*/ |
4534 |
void Instrument::UpdateChunks() { |
void Instrument::UpdateChunks(progress_t* pProgress) { |
4535 |
// first update base classes' chunks |
// first update base classes' chunks |
4536 |
DLS::Instrument::UpdateChunks(); |
DLS::Instrument::UpdateChunks(pProgress); |
4537 |
|
|
4538 |
// update Regions' chunks |
// update Regions' chunks |
4539 |
{ |
{ |
4540 |
RegionList::iterator iter = pRegions->begin(); |
RegionList::iterator iter = pRegions->begin(); |
4541 |
RegionList::iterator end = pRegions->end(); |
RegionList::iterator end = pRegions->end(); |
4542 |
for (; iter != end; ++iter) |
for (; iter != end; ++iter) |
4543 |
(*iter)->UpdateChunks(); |
(*iter)->UpdateChunks(pProgress); |
4544 |
} |
} |
4545 |
|
|
4546 |
// make sure 'lart' RIFF list chunk exists |
// make sure 'lart' RIFF list chunk exists |
4617 |
|
|
4618 |
void Instrument::UpdateScriptFileOffsets() { |
void Instrument::UpdateScriptFileOffsets() { |
4619 |
// own gig format extensions |
// own gig format extensions |
4620 |
if (pScriptRefs) { |
if (pScriptRefs && pScriptRefs->size() > 0) { |
4621 |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4622 |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
4623 |
const int slotCount = pScriptRefs->size(); |
const int slotCount = pScriptRefs->size(); |
4627 |
uint32_t fileOffset = |
uint32_t fileOffset = |
4628 |
(*pScriptRefs)[i].script->pChunk->GetFilePos() - |
(*pScriptRefs)[i].script->pChunk->GetFilePos() - |
4629 |
(*pScriptRefs)[i].script->pChunk->GetPos() - |
(*pScriptRefs)[i].script->pChunk->GetPos() - |
4630 |
CHUNK_HEADER_SIZE; |
CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()); |
4631 |
ckSCSL->WriteUint32(&fileOffset); |
ckSCSL->WriteUint32(&fileOffset); |
4632 |
// jump over flags entry (containing the bypass flag) |
// jump over flags entry (containing the bypass flag) |
4633 |
ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); |
ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); |
4702 |
} |
} |
4703 |
|
|
4704 |
/** |
/** |
4705 |
|
* Move this instrument at the position before @arg dst. |
4706 |
|
* |
4707 |
|
* This method can be used to reorder the sequence of instruments in a |
4708 |
|
* .gig file. This might be helpful especially on large .gig files which |
4709 |
|
* contain a large number of instruments within the same .gig file. So |
4710 |
|
* grouping such instruments to similar ones, can help to keep track of them |
4711 |
|
* when working with such complex .gig files. |
4712 |
|
* |
4713 |
|
* When calling this method, this instrument will be removed from in its |
4714 |
|
* current position in the instruments list and moved to the requested |
4715 |
|
* target position provided by @param dst. You may also pass NULL as |
4716 |
|
* argument to this method, in that case this intrument will be moved to the |
4717 |
|
* very end of the .gig file's instrument list. |
4718 |
|
* |
4719 |
|
* You have to call Save() to make the order change persistent to the .gig |
4720 |
|
* file. |
4721 |
|
* |
4722 |
|
* Currently this method is limited to moving the instrument within the same |
4723 |
|
* .gig file. Trying to move it to another .gig file by calling this method |
4724 |
|
* will throw an exception. |
4725 |
|
* |
4726 |
|
* @param dst - destination instrument at which this instrument will be |
4727 |
|
* moved to, or pass NULL for moving to end of list |
4728 |
|
* @throw gig::Exception if this instrument and target instrument are not |
4729 |
|
* part of the same file |
4730 |
|
*/ |
4731 |
|
void Instrument::MoveTo(Instrument* dst) { |
4732 |
|
if (dst && GetParent() != dst->GetParent()) |
4733 |
|
throw Exception( |
4734 |
|
"gig::Instrument::MoveTo() can only be used for moving within " |
4735 |
|
"the same gig file." |
4736 |
|
); |
4737 |
|
|
4738 |
|
File* pFile = (File*) GetParent(); |
4739 |
|
|
4740 |
|
// move this instrument within the instrument list |
4741 |
|
{ |
4742 |
|
File::InstrumentList& list = *pFile->pInstruments; |
4743 |
|
|
4744 |
|
File::InstrumentList::iterator itFrom = |
4745 |
|
std::find(list.begin(), list.end(), static_cast<DLS::Instrument*>(this)); |
4746 |
|
|
4747 |
|
File::InstrumentList::iterator itTo = |
4748 |
|
std::find(list.begin(), list.end(), static_cast<DLS::Instrument*>(dst)); |
4749 |
|
|
4750 |
|
list.splice(itTo, list, itFrom); |
4751 |
|
} |
4752 |
|
|
4753 |
|
// move the instrument's actual list RIFF chunk appropriately |
4754 |
|
RIFF::List* lstCkInstruments = pFile->pRIFF->GetSubList(LIST_TYPE_LINS); |
4755 |
|
lstCkInstruments->MoveSubChunk( |
4756 |
|
this->pCkInstrument, |
4757 |
|
(RIFF::Chunk*) ((dst) ? dst->pCkInstrument : NULL) |
4758 |
|
); |
4759 |
|
} |
4760 |
|
|
4761 |
|
/** |
4762 |
* Returns a MIDI rule of the instrument. |
* Returns a MIDI rule of the instrument. |
4763 |
* |
* |
4764 |
* 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 |
4835 |
if (script->pChunk) { |
if (script->pChunk) { |
4836 |
uint32_t offset = script->pChunk->GetFilePos() - |
uint32_t offset = script->pChunk->GetFilePos() - |
4837 |
script->pChunk->GetPos() - |
script->pChunk->GetPos() - |
4838 |
CHUNK_HEADER_SIZE; |
CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()); |
4839 |
if (offset == soughtOffset) |
if (offset == soughtOffset) |
4840 |
{ |
{ |
4841 |
_ScriptPooolRef ref; |
_ScriptPooolRef ref; |
5118 |
* |
* |
5119 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
5120 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
5121 |
|
* |
5122 |
|
* @param pProgress - callback function for progress notification |
5123 |
*/ |
*/ |
5124 |
void Group::UpdateChunks() { |
void Group::UpdateChunks(progress_t* pProgress) { |
5125 |
// make sure <3gri> and <3gnl> list chunks exist |
// make sure <3gri> and <3gnl> list chunks exist |
5126 |
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
5127 |
if (!_3gri) { |
if (!_3gri) { |
5398 |
int iTotalSamples = WavePoolCount; |
int iTotalSamples = WavePoolCount; |
5399 |
|
|
5400 |
// check if samples should be loaded from extension files |
// check if samples should be loaded from extension files |
5401 |
|
// (only for old gig files < 2 GB) |
5402 |
int lastFileNo = 0; |
int lastFileNo = 0; |
5403 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
if (!file->IsNew() && !(file->GetCurrentFileSize() >> 31)) { |
5404 |
if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; |
for (int i = 0 ; i < WavePoolCount ; i++) { |
5405 |
|
if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; |
5406 |
|
} |
5407 |
} |
} |
5408 |
String name(pRIFF->GetFileName()); |
String name(pRIFF->GetFileName()); |
5409 |
int nameLen = name.length(); |
int nameLen = name.length(); |
5413 |
for (int fileNo = 0 ; ; ) { |
for (int fileNo = 0 ; ; ) { |
5414 |
RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); |
5415 |
if (wvpl) { |
if (wvpl) { |
5416 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
file_offset_t wvplFileOffset = wvpl->GetFilePos(); |
5417 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
RIFF::List* wave = wvpl->GetFirstSubList(); |
5418 |
while (wave) { |
while (wave) { |
5419 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
5421 |
const float subprogress = (float) iSampleIndex / (float) iTotalSamples; |
const float subprogress = (float) iSampleIndex / (float) iTotalSamples; |
5422 |
__notify_progress(pProgress, subprogress); |
__notify_progress(pProgress, subprogress); |
5423 |
|
|
5424 |
unsigned long waveFileOffset = wave->GetFilePos(); |
file_offset_t waveFileOffset = wave->GetFilePos(); |
5425 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); |
5426 |
|
|
5427 |
iSampleIndex++; |
iSampleIndex++; |
5660 |
if (!_3crc) return; |
if (!_3crc) return; |
5661 |
|
|
5662 |
// get the index of the sample |
// get the index of the sample |
5663 |
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; |
|
|
} |
|
|
} |
|
5664 |
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"); |
5665 |
|
|
5666 |
// write the CRC-32 checksum to disk |
// write the CRC-32 checksum to disk |
5667 |
_3crc->SetPos(iWaveIndex * 8); |
_3crc->SetPos(iWaveIndex * 8); |
5668 |
uint32_t tmp = 1; |
uint32_t one = 1; |
5669 |
_3crc->WriteUint32(&tmp); // unknown, always 1? |
_3crc->WriteUint32(&one); // always 1 |
5670 |
_3crc->WriteUint32(&crc); |
_3crc->WriteUint32(&crc); |
5671 |
|
|
5672 |
|
// reload CRC table to RAM to keep it persistent over several subsequent save operations |
5673 |
|
_3crc->ReleaseChunkData(); |
5674 |
|
_3crc->LoadChunkData(); |
5675 |
|
} |
5676 |
|
|
5677 |
|
uint32_t File::GetSampleChecksum(Sample* pSample) { |
5678 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5679 |
|
if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5680 |
|
uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); |
5681 |
|
if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5682 |
|
|
5683 |
|
// get the index of the sample |
5684 |
|
int iWaveIndex = GetWaveTableIndexOf(pSample); |
5685 |
|
if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); |
5686 |
|
|
5687 |
|
// read the CRC-32 checksum directly from disk |
5688 |
|
size_t pos = iWaveIndex * 8; |
5689 |
|
if (pos + 8 > _3crc->GetNewSize()) |
5690 |
|
throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); |
5691 |
|
|
5692 |
|
uint32_t one = load32(&pData[pos]); // always 1 |
5693 |
|
if (one != 1) |
5694 |
|
throw gig::Exception("Could not verify sample, because reference checksum table is damaged"); |
5695 |
|
|
5696 |
|
return load32(&pData[pos+4]); |
5697 |
|
} |
5698 |
|
|
5699 |
|
int File::GetWaveTableIndexOf(gig::Sample* pSample) { |
5700 |
|
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5701 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5702 |
|
File::SampleList::iterator end = pSamples->end(); |
5703 |
|
for (int index = 0; iter != end; ++iter, ++index) |
5704 |
|
if (*iter == pSample) |
5705 |
|
return index; |
5706 |
|
return -1; |
5707 |
|
} |
5708 |
|
|
5709 |
|
/** |
5710 |
|
* Checks whether the file's "3CRC" chunk was damaged. This chunk contains |
5711 |
|
* the CRC32 check sums of all samples' raw wave data. |
5712 |
|
* |
5713 |
|
* @return true if 3CRC chunk is OK, or false if 3CRC chunk is damaged |
5714 |
|
*/ |
5715 |
|
bool File::VerifySampleChecksumTable() { |
5716 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5717 |
|
if (!_3crc) return false; |
5718 |
|
if (_3crc->GetNewSize() <= 0) return false; |
5719 |
|
if (_3crc->GetNewSize() % 8) return false; |
5720 |
|
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5721 |
|
if (_3crc->GetNewSize() != pSamples->size() * 8) return false; |
5722 |
|
|
5723 |
|
const int n = _3crc->GetNewSize() / 8; |
5724 |
|
|
5725 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5726 |
|
if (!pData) return false; |
5727 |
|
|
5728 |
|
for (int i = 0; i < n; ++i) { |
5729 |
|
uint32_t one = pData[i*2]; |
5730 |
|
if (one != 1) return false; |
5731 |
|
} |
5732 |
|
|
5733 |
|
return true; |
5734 |
|
} |
5735 |
|
|
5736 |
|
/** |
5737 |
|
* Recalculates CRC32 checksums for all samples and rebuilds this gig |
5738 |
|
* file's checksum table with those new checksums. This might usually |
5739 |
|
* just be necessary if the checksum table was damaged. |
5740 |
|
* |
5741 |
|
* @e IMPORTANT: The current implementation of this method only works |
5742 |
|
* with files that have not been modified since it was loaded, because |
5743 |
|
* it expects that no externally caused file structure changes are |
5744 |
|
* required! |
5745 |
|
* |
5746 |
|
* Due to the expectation above, this method is currently protected |
5747 |
|
* and actually only used by the command line tool "gigdump" yet. |
5748 |
|
* |
5749 |
|
* @returns true if Save() is required to be called after this call, |
5750 |
|
* false if no further action is required |
5751 |
|
*/ |
5752 |
|
bool File::RebuildSampleChecksumTable() { |
5753 |
|
// make sure sample chunks were scanned |
5754 |
|
if (!pSamples) GetFirstSample(); |
5755 |
|
|
5756 |
|
bool bRequiresSave = false; |
5757 |
|
|
5758 |
|
// make sure "3CRC" chunk exists with required size |
5759 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5760 |
|
if (!_3crc) { |
5761 |
|
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
5762 |
|
bRequiresSave = true; |
5763 |
|
} else if (_3crc->GetNewSize() != pSamples->size() * 8) { |
5764 |
|
_3crc->Resize(pSamples->size() * 8); |
5765 |
|
bRequiresSave = true; |
5766 |
|
} |
5767 |
|
|
5768 |
|
if (bRequiresSave) { // refill CRC table for all samples in RAM ... |
5769 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5770 |
|
{ |
5771 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5772 |
|
File::SampleList::iterator end = pSamples->end(); |
5773 |
|
for (; iter != end; ++iter) { |
5774 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
5775 |
|
int index = GetWaveTableIndexOf(pSample); |
5776 |
|
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5777 |
|
pData[index*2] = 1; // always 1 |
5778 |
|
pData[index*2+1] = pSample->CalculateWaveDataChecksum(); |
5779 |
|
} |
5780 |
|
} |
5781 |
|
} else { // no file structure changes necessary, so directly write to disk and we are done ... |
5782 |
|
// make sure file is in write mode |
5783 |
|
pRIFF->SetMode(RIFF::stream_mode_read_write); |
5784 |
|
{ |
5785 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5786 |
|
File::SampleList::iterator end = pSamples->end(); |
5787 |
|
for (; iter != end; ++iter) { |
5788 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
5789 |
|
int index = GetWaveTableIndexOf(pSample); |
5790 |
|
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5791 |
|
uint32_t crc = pSample->CalculateWaveDataChecksum(); |
5792 |
|
SetSampleChecksum(pSample, crc); |
5793 |
|
} |
5794 |
|
} |
5795 |
|
} |
5796 |
|
|
5797 |
|
return bRequiresSave; |
5798 |
} |
} |
5799 |
|
|
5800 |
Group* File::GetFirstGroup() { |
Group* File::GetFirstGroup() { |
6019 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
6020 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
6021 |
* |
* |
6022 |
|
* @param pProgress - callback function for progress notification |
6023 |
* @throws Exception - on errors |
* @throws Exception - on errors |
6024 |
*/ |
*/ |
6025 |
void File::UpdateChunks() { |
void File::UpdateChunks(progress_t* pProgress) { |
6026 |
bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; |
bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; |
6027 |
|
|
|
b64BitWavePoolOffsets = pVersion && pVersion->major == 3; |
|
|
|
|
6028 |
// update own gig format extension chunks |
// update own gig format extension chunks |
6029 |
// (not part of the GigaStudio 4 format) |
// (not part of the GigaStudio 4 format) |
6030 |
// |
RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS); |
6031 |
|
if (!lst3LS) { |
6032 |
|
lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS); |
6033 |
|
} |
6034 |
|
// Make sure <3LS > chunk is placed before <ptbl> chunk. The precise |
6035 |
|
// location of <3LS > is irrelevant, however it should be located |
6036 |
|
// before the actual wave data |
6037 |
|
RIFF::Chunk* ckPTBL = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
6038 |
|
pRIFF->MoveSubChunk(lst3LS, ckPTBL); |
6039 |
|
|
6040 |
// This must be performed before writing the chunks for instruments, |
// This must be performed before writing the chunks for instruments, |
6041 |
// because the instruments' script slots will write the file offsets |
// because the instruments' script slots will write the file offsets |
6042 |
// of the respective instrument script chunk as reference. |
// of the respective instrument script chunk as reference. |
6043 |
if (pScriptGroups) { |
if (pScriptGroups) { |
6044 |
RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS); |
// Update instrument script (group) chunks. |
6045 |
if (pScriptGroups->empty()) { |
for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin(); |
6046 |
if (lst3LS) pRIFF->DeleteSubChunk(lst3LS); |
it != pScriptGroups->end(); ++it) |
6047 |
} else { |
{ |
6048 |
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(); |
|
|
} |
|
6049 |
} |
} |
6050 |
} |
} |
6051 |
|
|
6052 |
|
// in case no libgig custom format data was added, then remove the |
6053 |
|
// custom "3LS " chunk again |
6054 |
|
if (!lst3LS->CountSubChunks()) { |
6055 |
|
pRIFF->DeleteSubChunk(lst3LS); |
6056 |
|
lst3LS = NULL; |
6057 |
|
} |
6058 |
|
|
6059 |
|
// if there is a 3CRC chunk, make sure it is loaded into RAM, otherwise |
6060 |
|
// its data might get lost or damaged on file structure changes |
6061 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
6062 |
|
if (_3crc) _3crc->LoadChunkData(); |
6063 |
|
|
6064 |
// first update base class's chunks |
// first update base class's chunks |
6065 |
DLS::File::UpdateChunks(); |
DLS::File::UpdateChunks(pProgress); |
6066 |
|
|
6067 |
if (newFile) { |
if (newFile) { |
6068 |
// INFO was added by Resource::UpdateChunks - make sure it |
// INFO was added by Resource::UpdateChunks - make sure it |
6099 |
std::list<Group*>::iterator iter = pGroups->begin(); |
std::list<Group*>::iterator iter = pGroups->begin(); |
6100 |
std::list<Group*>::iterator end = pGroups->end(); |
std::list<Group*>::iterator end = pGroups->end(); |
6101 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
6102 |
(*iter)->UpdateChunks(); |
(*iter)->UpdateChunks(pProgress); |
6103 |
} |
} |
6104 |
} |
} |
6105 |
|
|
6220 |
// samples. The actual checksum values will be filled in |
// samples. The actual checksum values will be filled in |
6221 |
// later, by Sample::Write. |
// later, by Sample::Write. |
6222 |
|
|
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
|
6223 |
if (_3crc) { |
if (_3crc) { |
6224 |
_3crc->Resize(pSamples->size() * 8); |
_3crc->Resize(pSamples->size() * 8); |
6225 |
} else if (newFile) { |
} else if (newFile) { |