--- libgig/trunk/src/gig.cpp 2014/05/10 02:06:58 2543 +++ libgig/trunk/src/gig.cpp 2017/10/03 17:35:02 3350 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2014 by Christian Schoenebeck * + * Copyright (C) 2003-2017 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -24,10 +24,16 @@ #include "gig.h" #include "helper.h" +#include "Serialization.h" #include #include #include +#include + +/// libgig's current file format version (for extending the original Giga file +/// format with libgig's own custom data / custom features). +#define GIG_FILE_EXT_VERSION 2 /// Initial size of the sample buffer which is used for decompression of /// compressed sample wave streams - this value should always be bigger than @@ -50,39 +56,10 @@ #define GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(x) ((x & 0x03) << 3) #define GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(x) ((x & 0x03) << 5) -namespace gig { - -// *************** 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; - } - } +#define SRLZ(member) \ + archive->serializeMember(*this, member, #member); +namespace gig { // *************** Internal functions for sample decompression *************** // * @@ -122,8 +99,8 @@ void Decompress16(int compressionmode, const unsigned char* params, int srcStep, int dstStep, const unsigned char* pSrc, int16_t* pDst, - unsigned long currentframeoffset, - unsigned long copysamples) + file_offset_t currentframeoffset, + file_offset_t copysamples) { switch (compressionmode) { case 0: // 16 bit uncompressed @@ -159,8 +136,8 @@ void Decompress24(int compressionmode, const unsigned char* params, int dstStep, const unsigned char* pSrc, uint8_t* pDst, - unsigned long currentframeoffset, - unsigned long copysamples, int truncatedBits) + file_offset_t currentframeoffset, + file_offset_t copysamples, int truncatedBits) { int y, dy, ddy, dddy; @@ -296,14 +273,14 @@ * steps. * * Once the whole data was processed by __calculateCRC(), one should - * call __encodeCRC() to get the final CRC result. + * call __finalizeCRC() to get the final CRC result. * * @param buf - pointer to data the CRC shall be calculated of * @param bufSize - size of the data to be processed * @param crc - variable the CRC sum shall be stored to */ - static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) { - for (int i = 0 ; i < bufSize ; i++) { + static void __calculateCRC(unsigned char* buf, size_t bufSize, uint32_t& crc) { + for (size_t i = 0 ; i < bufSize ; i++) { crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); } } @@ -313,8 +290,8 @@ * * @param crc - variable previously passed to __calculateCRC() */ - inline static uint32_t __encodeCRC(const uint32_t& crc) { - return crc ^ 0xffffffff; + inline static void __finalizeCRC(uint32_t& crc) { + crc ^= 0xffffffff; } @@ -342,10 +319,53 @@ +// *************** leverage_ctrl_t *************** +// * + + void leverage_ctrl_t::serialize(Serialization::Archive* archive) { + SRLZ(type); + SRLZ(controller_number); + } + + + +// *************** crossfade_t *************** +// * + + void crossfade_t::serialize(Serialization::Archive* archive) { + SRLZ(in_start); + SRLZ(in_end); + SRLZ(out_start); + SRLZ(out_end); + } + + + +// *************** eg_opt_t *************** +// * + + eg_opt_t::eg_opt_t() { + AttackCancel = true; + AttackHoldCancel = true; + Decay1Cancel = true; + Decay2Cancel = true; + ReleaseCancel = true; + } + + void eg_opt_t::serialize(Serialization::Archive* archive) { + SRLZ(AttackCancel); + SRLZ(AttackHoldCancel); + SRLZ(Decay1Cancel); + SRLZ(Decay2Cancel); + SRLZ(ReleaseCancel); + } + + + // *************** Sample *************** // * - unsigned int Sample::Instances = 0; + size_t Sample::Instances = 0; buffer_t Sample::InternalDecompressionBuffer; /** @brief Constructor. @@ -365,8 +385,11 @@ * ('wvpl') list chunk * @param fileNo - number of an extension file where this sample * is located, 0 otherwise + * @param index - wave pool index of sample (may be -1 on new sample) */ - 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) + : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) + { static const DLS::Info::string_length_t fixedStringLengths[] = { { CHUNK_ID_INAM, 64 }, { 0, 0 } @@ -376,6 +399,16 @@ FileNo = fileNo; __resetCRC(crc); + // if this is not a new sample, try to get the sample's already existing + // CRC32 checksum from disk, this checksum will reflect the sample's CRC32 + // checksum of the time when the sample was consciously modified by the + // user for the last time (by calling Sample::Write() that is). + if (index >= 0) { // not a new file ... + try { + uint32_t crc = pFile->GetSampleChecksumByIndex(index); + this->crc = crc; + } catch (...) {} + } pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); if (pCk3gix) { @@ -508,10 +541,10 @@ const int iReadAtOnce = 32*1024; char* buf = new char[iReadAtOnce * orig->FrameSize]; Sample* pOrig = (Sample*) orig; //HACK: remove constness for now - unsigned long restorePos = pOrig->GetPos(); + file_offset_t restorePos = pOrig->GetPos(); pOrig->SetPos(0); SetPos(0); - for (unsigned long n = pOrig->Read(buf, iReadAtOnce); n; + for (file_offset_t n = pOrig->Read(buf, iReadAtOnce); n; n = pOrig->Read(buf, iReadAtOnce)) { Write(buf, n); @@ -527,13 +560,14 @@ * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. * + * @param pProgress - callback function for progress notification * @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data * was provided yet * @throws gig::Exception if there is any invalid sample setting */ - void Sample::UpdateChunks() { + void Sample::UpdateChunks(progress_t* pProgress) { // first update base class's chunks - DLS::Sample::UpdateChunks(); + DLS::Sample::UpdateChunks(pProgress); // make sure 'smpl' chunk exists pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); @@ -595,7 +629,7 @@ void Sample::ScanCompressedSample() { //TODO: we have to add some more scans here (e.g. determine compression rate) this->SamplesTotal = 0; - std::list frameOffsets; + std::list frameOffsets; SamplesPerFrame = BitDepth == 24 ? 256 : 2048; WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag @@ -611,7 +645,7 @@ const int mode_l = pCkData->ReadUint8(); const int mode_r = pCkData->ReadUint8(); if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode"); - const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; + const file_offset_t frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; if (pCkData->RemainingBytes() <= frameSize) { SamplesInLastFrame = @@ -630,7 +664,7 @@ const int mode = pCkData->ReadUint8(); if (mode > 5) throw gig::Exception("Unknown compression mode"); - const unsigned long frameSize = bytesPerFrame[mode]; + const file_offset_t frameSize = bytesPerFrame[mode]; if (pCkData->RemainingBytes() <= frameSize) { SamplesInLastFrame = @@ -646,9 +680,9 @@ // Build the frames table (which is used for fast resolving of a frame's chunk offset) if (FrameTable) delete[] FrameTable; - FrameTable = new unsigned long[frameOffsets.size()]; - std::list::iterator end = frameOffsets.end(); - std::list::iterator iter = frameOffsets.begin(); + FrameTable = new file_offset_t[frameOffsets.size()]; + std::list::iterator end = frameOffsets.end(); + std::list::iterator iter = frameOffsets.begin(); for (int i = 0; iter != end; i++, iter++) { FrameTable[i] = *iter; } @@ -689,7 +723,7 @@ * the cached sample data in bytes * @see ReleaseSampleData(), Read(), SetPos() */ - buffer_t Sample::LoadSampleData(unsigned long SampleCount) { + buffer_t Sample::LoadSampleData(file_offset_t SampleCount) { return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples } @@ -748,10 +782,10 @@ * size of the cached sample data in bytes * @see ReleaseSampleData(), Read(), SetPos() */ - buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) { + buffer_t Sample::LoadSampleDataWithNullSamplesExtension(file_offset_t SampleCount, uint NullSamplesCount) { if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; - unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; + file_offset_t allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; SetPos(0); // reset read position to begin of sample RAMCache.pStart = new int8_t[allocationsize]; RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; @@ -815,17 +849,17 @@ * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with * other formats will fail! * - * @param iNewSize - new sample wave data size in sample points (must be - * greater than zero) + * @param NewSize - new sample wave data size in sample points (must be + * greater than zero) * @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM - * or if \a iNewSize is less than 1 + * @throws DLS::Exception if \a NewSize is less than 1 or unrealistic large * @throws gig::Exception if existing sample is compressed * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, * DLS::Sample::FormatTag, File::Save() */ - void Sample::Resize(int iNewSize) { + void Sample::Resize(file_offset_t NewSize) { if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)"); - DLS::Sample::Resize(iNewSize); + DLS::Sample::Resize(NewSize); } /** @@ -849,7 +883,7 @@ * @returns the new sample position * @see Read() */ - 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) { if (Compressed) { switch (Whence) { case RIFF::stream_curpos: @@ -867,14 +901,14 @@ } if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; - unsigned long frame = this->SamplePos / 2048; // to which frame to jump + file_offset_t frame = this->SamplePos / 2048; // to which frame to jump this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame pCkData->SetPos(FrameTable[frame]); // set chunk pointer to the start of sought frame return this->SamplePos; } else { // not compressed - unsigned long orderedBytes = SampleCount * this->FrameSize; - unsigned long result = pCkData->SetPos(orderedBytes, Whence); + file_offset_t orderedBytes = SampleCount * this->FrameSize; + file_offset_t result = pCkData->SetPos(orderedBytes, Whence); return (result == orderedBytes) ? SampleCount : result / this->FrameSize; } @@ -883,7 +917,7 @@ /** * Returns the current position in the sample (in sample points). */ - unsigned long Sample::GetPos() const { + file_offset_t Sample::GetPos() const { if (Compressed) return SamplePos; else return pCkData->GetPos() / FrameSize; } @@ -922,9 +956,9 @@ * @returns number of successfully read sample points * @see CreateDecompressionBuffer() */ - 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, DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { - unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; + file_offset_t samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; uint8_t* pDst = (uint8_t*) pBuffer; SetPos(pPlaybackState->position); // recover position from the last time @@ -962,10 +996,10 @@ // reading, swap all sample frames so it reflects // backward playback - unsigned long swapareastart = totalreadsamples; - unsigned long loopoffset = GetPos() - loop.LoopStart; - unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); - unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; + file_offset_t swapareastart = totalreadsamples; + file_offset_t loopoffset = GetPos() - loop.LoopStart; + file_offset_t samplestoreadinloop = Min(samplestoread, loopoffset); + file_offset_t reverseplaybackend = GetPos() - samplestoreadinloop; SetPos(reverseplaybackend); @@ -1013,11 +1047,11 @@ // reading, swap all sample frames so it reflects // backward playback - unsigned long swapareastart = totalreadsamples; - unsigned long loopoffset = GetPos() - loop.LoopStart; - unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset) + file_offset_t swapareastart = totalreadsamples; + file_offset_t loopoffset = GetPos() - loop.LoopStart; + file_offset_t samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset) : samplestoread; - unsigned long reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); + file_offset_t reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); SetPos(reverseplaybackend); @@ -1097,7 +1131,7 @@ * @returns number of successfully read sample points * @see SetPos(), CreateDecompressionBuffer() */ - 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) { if (SampleCount == 0) return 0; if (!Compressed) { if (BitDepth == 24) { @@ -1112,7 +1146,7 @@ else { if (this->SamplePos >= this->SamplesTotal) return 0; //TODO: efficiency: maybe we should test for an average compression rate - unsigned long assumedsize = GuessSize(SampleCount), + file_offset_t assumedsize = GuessSize(SampleCount), remainingbytes = 0, // remaining bytes in the local buffer remainingsamples = SampleCount, copysamples, skipsamples, @@ -1135,8 +1169,8 @@ remainingbytes = pCkData->Read(pSrc, assumedsize, 1); while (remainingsamples && remainingbytes) { - unsigned long framesamples = SamplesPerFrame; - unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; + file_offset_t framesamples = SamplesPerFrame; + file_offset_t framebytes, rightChannelOffset = 0, nextFrameOffset; int mode_l = *pSrc++, mode_r = 0; @@ -1286,7 +1320,7 @@ * @throws gig::Exception if sample is compressed * @see DLS::LoadSampleData() */ - unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { + file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) { if (Compressed) throw gig::Exception("There is no support for writing compressed gig samples (yet)"); // if this is the first write in this sample, reset the @@ -1295,7 +1329,7 @@ __resetCRC(crc); } if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); - unsigned long res; + file_offset_t res; if (BitDepth == 24) { res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; } else { // 16 bit @@ -1307,8 +1341,9 @@ // if this is the last write, update the checksum chunk in the // file if (pCkData->GetPos() == pCkData->GetSize()) { + __finalizeCRC(crc); File* pFile = static_cast(GetParent()); - pFile->SetSampleChecksum(this, __encodeCRC(crc)); + pFile->SetSampleChecksum(this, crc); } return res; } @@ -1329,11 +1364,11 @@ * @returns allocated decompression buffer * @see DestroyDecompressionBuffer() */ - buffer_t Sample::CreateDecompressionBuffer(unsigned long MaxReadSize) { + buffer_t Sample::CreateDecompressionBuffer(file_offset_t MaxReadSize) { buffer_t result; const double worstCaseHeaderOverhead = (256.0 /*frame size*/ + 12.0 /*header*/ + 2.0 /*compression type flag (stereo)*/) / 256.0; - 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); result.pStart = new int8_t[result.Size]; result.NullExtensionSize = 0; return result; @@ -1367,6 +1402,72 @@ return pGroup; } + /** + * Returns the CRC-32 checksum of the sample's raw wave form data at the + * time when this sample's wave form data was modified for the last time + * by calling Write(). This checksum only covers the raw wave form data, + * not any meta informations like i.e. bit depth or loop points. Since + * this method just returns the checksum stored for this sample i.e. when + * the gig file was loaded, this method returns immediately. So it does no + * recalcuation of the checksum with the currently available sample wave + * form data. + * + * @see VerifyWaveData() + */ + uint32_t Sample::GetWaveDataCRC32Checksum() { + return crc; + } + + /** + * Checks the integrity of this sample's raw audio wave data. Whenever a + * Sample's raw wave data is intentionally modified (i.e. by calling + * Write() and supplying the new raw audio wave form data) a CRC32 checksum + * is calculated and stored/updated for this sample, along to the sample's + * meta informations. + * + * Now by calling this method the current raw audio wave data is checked + * against the already stored CRC32 check sum in order to check whether the + * sample data had been damaged unintentionally for some reason. Since by + * calling this method always the entire raw audio wave data has to be + * read, verifying all samples this way may take a long time accordingly. + * And that's also the reason why the sample integrity is not checked by + * default whenever a gig file is loaded. So this method must be called + * explicitly to fulfill this task. + * + * @param pActually - (optional) if provided, will be set to the actually + * calculated checksum of the current raw wave form data, + * you can get the expected checksum instead by calling + * GetWaveDataCRC32Checksum() + * @returns true if sample is OK or false if the sample is damaged + * @throws Exception if no checksum had been stored to disk for this + * sample yet, or on I/O issues + * @see GetWaveDataCRC32Checksum() + */ + bool Sample::VerifyWaveData(uint32_t* pActually) { + //File* pFile = static_cast(GetParent()); + uint32_t crc = CalculateWaveDataChecksum(); + if (pActually) *pActually = crc; + return crc == this->crc; + } + + uint32_t Sample::CalculateWaveDataChecksum() { + const size_t sz = 20*1024; // 20kB buffer size + std::vector buffer(sz); + buffer.resize(sz); + + const size_t n = sz / FrameSize; + SetPos(0); + uint32_t crc = 0; + __resetCRC(crc); + while (true) { + file_offset_t nRead = Read(&buffer[0], n); + if (nRead <= 0) break; + __calculateCRC(&buffer[0], nRead * FrameSize, crc); + } + __finalizeCRC(crc); + return crc; + } + Sample::~Sample() { Instances--; if (!Instances && InternalDecompressionBuffer.Size) { @@ -1383,7 +1484,7 @@ // *************** DimensionRegion *************** // * - uint DimensionRegion::Instances = 0; + size_t DimensionRegion::Instances = 0; DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { @@ -1577,7 +1678,7 @@ EG2Attack = 0.0; EG2Decay1 = 0.005; EG2Sustain = 1000; - EG2Release = 0.3; + EG2Release = 60; LFO2ControlDepth = 0; LFO2Frequency = 1.0; LFO2InternalDepth = 0; @@ -1631,6 +1732,20 @@ VCFType = vcf_type_lowpass; memset(DimensionUpperLimits, 127, 8); } + // format extension for EG behavior options, these will *NOT* work with + // Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = _3ewl->GetSubChunk(CHUNK_ID_LSDE); + if (lsde) { + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + for (int i = 0; i < 2; ++i) { + unsigned char byte = lsde->ReadUint8(); + pEGOpts[i]->AttackCancel = byte & 1; + pEGOpts[i]->AttackHoldCancel = byte & (1 << 1); + pEGOpts[i]->Decay1Cancel = byte & (1 << 2); + pEGOpts[i]->Decay2Cancel = byte & (1 << 3); + pEGOpts[i]->ReleaseCancel = byte & (1 << 4); + } + } pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, VelocityResponseDepth, @@ -1708,12 +1823,14 @@ //NOTE: copy code copied from assignment constructor above, see comment there as well *this = *orig; // default memberwise shallow copy of all parameters + + // restore members that shall not be altered pParentList = p; // restore the chunk pointer + pRegion = pOriginalRegion; - // only take the raw sample reference & parent region reference if the + // only take the raw sample reference reference if the // two DimensionRegion objects are part of the same file if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) { - pRegion = pOriginalRegion; pSample = pOriginalSample; } @@ -1734,6 +1851,97 @@ } } + void DimensionRegion::serialize(Serialization::Archive* archive) { + // in case this class will become backward incompatible one day, + // then set a version and minimum version for this class like: + //archive->setVersion(*this, 2); + //archive->setMinVersion(*this, 1); + + SRLZ(VelocityUpperLimit); + SRLZ(EG1PreAttack); + SRLZ(EG1Attack); + SRLZ(EG1Decay1); + SRLZ(EG1Decay2); + SRLZ(EG1InfiniteSustain); + SRLZ(EG1Sustain); + SRLZ(EG1Release); + SRLZ(EG1Hold); + SRLZ(EG1Controller); + SRLZ(EG1ControllerInvert); + SRLZ(EG1ControllerAttackInfluence); + SRLZ(EG1ControllerDecayInfluence); + SRLZ(EG1ControllerReleaseInfluence); + SRLZ(LFO1Frequency); + SRLZ(LFO1InternalDepth); + SRLZ(LFO1ControlDepth); + SRLZ(LFO1Controller); + SRLZ(LFO1FlipPhase); + SRLZ(LFO1Sync); + SRLZ(EG2PreAttack); + SRLZ(EG2Attack); + SRLZ(EG2Decay1); + SRLZ(EG2Decay2); + SRLZ(EG2InfiniteSustain); + SRLZ(EG2Sustain); + SRLZ(EG2Release); + SRLZ(EG2Controller); + SRLZ(EG2ControllerInvert); + SRLZ(EG2ControllerAttackInfluence); + SRLZ(EG2ControllerDecayInfluence); + SRLZ(EG2ControllerReleaseInfluence); + SRLZ(LFO2Frequency); + SRLZ(LFO2InternalDepth); + SRLZ(LFO2ControlDepth); + SRLZ(LFO2Controller); + SRLZ(LFO2FlipPhase); + SRLZ(LFO2Sync); + SRLZ(EG3Attack); + SRLZ(EG3Depth); + SRLZ(LFO3Frequency); + SRLZ(LFO3InternalDepth); + SRLZ(LFO3ControlDepth); + SRLZ(LFO3Controller); + SRLZ(LFO3Sync); + SRLZ(VCFEnabled); + SRLZ(VCFType); + SRLZ(VCFCutoffController); + SRLZ(VCFCutoffControllerInvert); + SRLZ(VCFCutoff); + SRLZ(VCFVelocityCurve); + SRLZ(VCFVelocityScale); + SRLZ(VCFVelocityDynamicRange); + SRLZ(VCFResonance); + SRLZ(VCFResonanceDynamic); + SRLZ(VCFResonanceController); + SRLZ(VCFKeyboardTracking); + SRLZ(VCFKeyboardTrackingBreakpoint); + SRLZ(VelocityResponseCurve); + SRLZ(VelocityResponseDepth); + SRLZ(VelocityResponseCurveScaling); + SRLZ(ReleaseVelocityResponseCurve); + SRLZ(ReleaseVelocityResponseDepth); + SRLZ(ReleaseTriggerDecay); + SRLZ(Crossfade); + SRLZ(PitchTrack); + SRLZ(DimensionBypass); + SRLZ(Pan); + SRLZ(SelfMask); + SRLZ(AttenuationController); + SRLZ(InvertAttenuationController); + SRLZ(AttenuationControllerThreshold); + SRLZ(ChannelOffset); + SRLZ(SustainDefeat); + SRLZ(MSDecode); + //SRLZ(SampleStartOffset); + SRLZ(SampleAttenuation); + SRLZ(EG1Options); + SRLZ(EG2Options); + + // derived attributes from DLS::Sampler + SRLZ(FineTune); + SRLZ(Gain); + } + /** * Updates the respective member variable and updates @c SampleAttenuation * which depends on this value. @@ -1749,10 +1957,12 @@ * * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. + * + * @param pProgress - callback function for progress notification */ - void DimensionRegion::UpdateChunks() { + void DimensionRegion::UpdateChunks(progress_t* pProgress) { // first update base class's chunk - DLS::Sampler::UpdateChunks(); + DLS::Sampler::UpdateChunks(pProgress); RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); @@ -1772,7 +1982,7 @@ // update '3ewa' chunk with DimensionRegion's current settings - const uint32_t chunksize = _3ewa->GetNewSize(); + const uint32_t chunksize = (uint32_t) _3ewa->GetNewSize(); store32(&pData[0], chunksize); // unknown, always chunk size? const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); @@ -2032,6 +2242,34 @@ if (chunksize >= 148) { memcpy(&pData[140], DimensionUpperLimits, 8); } + + // format extension for EG behavior options, these will *NOT* work with + // Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = pParentList->GetSubChunk(CHUNK_ID_LSDE); + if (!lsde) { + // only add this "LSDE" chunk if the EG options do not match the + // default EG behavior + eg_opt_t defaultOpt; + if (memcmp(&EG1Options, &defaultOpt, sizeof(eg_opt_t)) || + memcmp(&EG2Options, &defaultOpt, sizeof(eg_opt_t))) + { + lsde = pParentList->AddSubChunk(CHUNK_ID_LSDE, 2); + // move LSDE chunk to the end of parent list + pParentList->MoveSubChunk(lsde, (RIFF::Chunk*)NULL); + } + } + if (lsde) { + unsigned char* pData = (unsigned char*) lsde->LoadChunkData(); + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + for (int i = 0; i < 2; ++i) { + pData[i] = + (pEGOpts[i]->AttackCancel ? 1 : 0) | + (pEGOpts[i]->AttackHoldCancel ? (1<<1) : 0) | + (pEGOpts[i]->Decay1Cancel ? (1<<2) : 0) | + (pEGOpts[i]->Decay2Cancel ? (1<<3) : 0) | + (pEGOpts[i]->ReleaseCancel ? (1<<4) : 0); + } + } } double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) { @@ -2071,6 +2309,33 @@ // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { + // sanity check input parameters + // (fallback to some default parameters on ill input) + switch (curveType) { + case curve_type_nonlinear: + case curve_type_linear: + if (depth > 4) { + printf("Warning: Invalid depth (0x%x) for velocity curve type (0x%x).\n", depth, curveType); + depth = 0; + scaling = 0; + } + break; + case curve_type_special: + if (depth > 5) { + printf("Warning: Invalid depth (0x%x) for velocity curve type 'special'.\n", depth); + depth = 0; + scaling = 0; + } + break; + case curve_type_unknown: + default: + printf("Warning: Unknown velocity curve type (0x%x).\n", curveType); + curveType = curve_type_linear; + depth = 0; + scaling = 0; + break; + } + double* table; uint32_t tableKey = (curveType<<16) | (depth<<8) | scaling; if (pVelocityTables->count(tableKey)) { // if key exists @@ -2446,7 +2711,10 @@ // unknown controller type default: - throw gig::Exception("Unknown leverage controller type."); + decodedcontroller.type = leverage_ctrl_t::type_none; + decodedcontroller.controller_number = 0; + printf("Warning: Unknown leverage controller type (0x%x).\n", EncodedController); + break; } return decodedcontroller; } @@ -2993,7 +3261,8 @@ if (file->GetAutoLoad()) { for (uint i = 0; i < DimensionRegions; i++) { uint32_t wavepoolindex = _3lnk->ReadUint32(); - if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); + if (file->pWavePoolTable && pDimensionRegions[i]) + pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); } GetSample(); // load global region sample reference } @@ -3023,9 +3292,10 @@ * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. * + * @param pProgress - callback function for progress notification * @throws gig::Exception if samples cannot be dereferenced */ - void Region::UpdateChunks() { + void Region::UpdateChunks(progress_t* pProgress) { // in the gig format we don't care about the Region's sample reference // but we still have to provide some existing one to not corrupt the // file, so to avoid the latter we simply always assign the sample of @@ -3033,11 +3303,11 @@ pSample = pDimensionRegions[0]->pSample; // first update base class's chunks - DLS::Region::UpdateChunks(); + DLS::Region::UpdateChunks(pProgress); // update dimension region's chunks for (int i = 0; i < DimensionRegions; i++) { - pDimensionRegions[i]->UpdateChunks(); + pDimensionRegions[i]->UpdateChunks(pProgress); } File* pFile = (File*) GetParent()->GetParent(); @@ -3053,7 +3323,7 @@ memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); // move 3prg to last position - pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0); + pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), (RIFF::Chunk*)NULL); } // update dimension definitions in '3lnk' chunk @@ -3127,12 +3397,13 @@ int step = 1; for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; - int end = step * pDimensionDefinitions[veldim].zones; // loop through all dimension regions for all dimensions except the velocity dimension int dim[8] = { 0 }; for (int i = 0 ; i < DimensionRegions ; i++) { + const int end = i + step * pDimensionDefinitions[veldim].zones; + // create a velocity table for all cases where the velocity zone is zero if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || pDimensionRegions[i]->VelocityUpperLimit) { // create the velocity table @@ -3163,6 +3434,7 @@ } } + // jump to the next case where the velocity zone is zero int j; int shift = 0; for (j = 0 ; j < Dimensions ; j++) { @@ -3199,6 +3471,18 @@ * dimension bits limit is violated */ void Region::AddDimension(dimension_def_t* pDimDef) { + // some initial sanity checks of the given dimension definition + if (pDimDef->zones < 2) + throw gig::Exception("Could not add new dimension, amount of requested zones must always be at least two"); + if (pDimDef->bits < 1) + throw gig::Exception("Could not add new dimension, amount of requested requested zone bits must always be at least one"); + if (pDimDef->dimension == dimension_samplechannel) { + if (pDimDef->zones != 2) + throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zones must always be 2 for this dimension type"); + if (pDimDef->bits != 1) + throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zone bits must always be 1 for this dimension type"); + } + // check if max. amount of dimensions reached File* file = (File*) GetParent()->GetParent(); const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; @@ -3374,6 +3658,330 @@ if (pDimDef->dimension == dimension_layer) Layers = 1; } + /** @brief Delete one split zone of a dimension (decrement zone amount). + * + * Instead of deleting an entire dimensions, this method will only delete + * one particular split zone given by @a zone of the Region's dimension + * given by @a type. So this method will simply decrement the amount of + * zones by one of the dimension in question. To be able to do that, the + * respective dimension must exist on this Region and it must have at least + * 3 zones. All DimensionRegion objects associated with the zone will be + * deleted. + * + * @param type - identifies the dimension where a zone shall be deleted + * @param zone - index of the dimension split zone that shall be deleted + * @throws gig::Exception if requested zone could not be deleted + */ + void Region::DeleteDimensionZone(dimension_t type, int zone) { + dimension_def_t* oldDef = GetDimensionDefinition(type); + if (!oldDef) + throw gig::Exception("Could not delete dimension zone, no such dimension of given type"); + if (oldDef->zones <= 2) + throw gig::Exception("Could not delete dimension zone, because it would end up with only one zone."); + if (zone < 0 || zone >= oldDef->zones) + throw gig::Exception("Could not delete dimension zone, requested zone index out of bounds."); + + const int newZoneSize = oldDef->zones - 1; + + // create a temporary Region which just acts as a temporary copy + // container and will be deleted at the end of this function and will + // also not be visible through the API during this process + gig::Region* tempRgn = NULL; + { + // adding these temporary chunks is probably not even necessary + Instrument* instr = static_cast(GetParent()); + RIFF::List* pCkInstrument = instr->pCkInstrument; + RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); + if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); + RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); + tempRgn = new Region(instr, rgn); + } + + // copy this region's dimensions (with already the dimension split size + // requested by the arguments of this method call) to the temporary + // region, and don't use Region::CopyAssign() here for this task, since + // it would also alter fast lookup helper variables here and there + dimension_def_t newDef; + for (int i = 0; i < Dimensions; ++i) { + dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference + // is this the dimension requested by the method arguments? ... + if (def.dimension == type) { // ... if yes, decrement zone amount by one + def.zones = newZoneSize; + if ((1 << (def.bits - 1)) == def.zones) def.bits--; + newDef = def; + } + tempRgn->AddDimension(&def); + } + + // find the dimension index in the tempRegion which is the dimension + // type passed to this method (paranoidly expecting different order) + int tempReducedDimensionIndex = -1; + for (int d = 0; d < tempRgn->Dimensions; ++d) { + if (tempRgn->pDimensionDefinitions[d].dimension == type) { + tempReducedDimensionIndex = d; + break; + } + } + + // copy dimension regions from this region to the temporary region + for (int iDst = 0; iDst < 256; ++iDst) { + DimensionRegion* dstDimRgn = tempRgn->pDimensionRegions[iDst]; + if (!dstDimRgn) continue; + std::map dimCase; + bool isValidZone = true; + for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) { + const int dstBits = tempRgn->pDimensionDefinitions[d].bits; + dimCase[tempRgn->pDimensionDefinitions[d].dimension] = + (iDst >> baseBits) & ((1 << dstBits) - 1); + baseBits += dstBits; + // there are also DimensionRegion objects of unused zones, skip them + if (dimCase[tempRgn->pDimensionDefinitions[d].dimension] >= tempRgn->pDimensionDefinitions[d].zones) { + isValidZone = false; + break; + } + } + if (!isValidZone) continue; + // a bit paranoid: cope with the chance that the dimensions would + // have different order in source and destination regions + const bool isLastZone = (dimCase[type] == newZoneSize - 1); + if (dimCase[type] >= zone) dimCase[type]++; + DimensionRegion* srcDimRgn = GetDimensionRegionByBit(dimCase); + dstDimRgn->CopyAssign(srcDimRgn); + // if this is the upper most zone of the dimension passed to this + // method, then correct (raise) its upper limit to 127 + if (newDef.split_type == split_type_normal && isLastZone) + dstDimRgn->DimensionUpperLimits[tempReducedDimensionIndex] = 127; + } + + // now tempRegion's dimensions and DimensionRegions basically reflect + // what we wanted to get for this actual Region here, so we now just + // delete and recreate the dimension in question with the new amount + // zones and then copy back from tempRegion + DeleteDimension(oldDef); + AddDimension(&newDef); + for (int iSrc = 0; iSrc < 256; ++iSrc) { + DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc]; + if (!srcDimRgn) continue; + std::map dimCase; + for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) { + const int srcBits = tempRgn->pDimensionDefinitions[d].bits; + dimCase[tempRgn->pDimensionDefinitions[d].dimension] = + (iSrc >> baseBits) & ((1 << srcBits) - 1); + baseBits += srcBits; + } + // a bit paranoid: cope with the chance that the dimensions would + // have different order in source and destination regions + DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase); + if (!dstDimRgn) continue; + dstDimRgn->CopyAssign(srcDimRgn); + } + + // delete temporary region + delete tempRgn; + + UpdateVelocityTable(); + } + + /** @brief Divide split zone of a dimension in two (increment zone amount). + * + * This will increment the amount of zones for the dimension (given by + * @a type) by one. It will do so by dividing the zone (given by @a zone) + * in the middle of its zone range in two. So the two zones resulting from + * the zone being splitted, will be an equivalent copy regarding all their + * articulation informations and sample reference. The two zones will only + * differ in their zone's upper limit + * (DimensionRegion::DimensionUpperLimits). + * + * @param type - identifies the dimension where a zone shall be splitted + * @param zone - index of the dimension split zone that shall be splitted + * @throws gig::Exception if requested zone could not be splitted + */ + void Region::SplitDimensionZone(dimension_t type, int zone) { + dimension_def_t* oldDef = GetDimensionDefinition(type); + if (!oldDef) + throw gig::Exception("Could not split dimension zone, no such dimension of given type"); + if (zone < 0 || zone >= oldDef->zones) + throw gig::Exception("Could not split dimension zone, requested zone index out of bounds."); + + const int newZoneSize = oldDef->zones + 1; + + // create a temporary Region which just acts as a temporary copy + // container and will be deleted at the end of this function and will + // also not be visible through the API during this process + gig::Region* tempRgn = NULL; + { + // adding these temporary chunks is probably not even necessary + Instrument* instr = static_cast(GetParent()); + RIFF::List* pCkInstrument = instr->pCkInstrument; + RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); + if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); + RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); + tempRgn = new Region(instr, rgn); + } + + // copy this region's dimensions (with already the dimension split size + // requested by the arguments of this method call) to the temporary + // region, and don't use Region::CopyAssign() here for this task, since + // it would also alter fast lookup helper variables here and there + dimension_def_t newDef; + for (int i = 0; i < Dimensions; ++i) { + dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference + // is this the dimension requested by the method arguments? ... + if (def.dimension == type) { // ... if yes, increment zone amount by one + def.zones = newZoneSize; + if ((1 << oldDef->bits) < newZoneSize) def.bits++; + newDef = def; + } + tempRgn->AddDimension(&def); + } + + // find the dimension index in the tempRegion which is the dimension + // type passed to this method (paranoidly expecting different order) + int tempIncreasedDimensionIndex = -1; + for (int d = 0; d < tempRgn->Dimensions; ++d) { + if (tempRgn->pDimensionDefinitions[d].dimension == type) { + tempIncreasedDimensionIndex = d; + break; + } + } + + // copy dimension regions from this region to the temporary region + for (int iSrc = 0; iSrc < 256; ++iSrc) { + DimensionRegion* srcDimRgn = pDimensionRegions[iSrc]; + if (!srcDimRgn) continue; + std::map dimCase; + bool isValidZone = true; + for (int d = 0, baseBits = 0; d < Dimensions; ++d) { + const int srcBits = pDimensionDefinitions[d].bits; + dimCase[pDimensionDefinitions[d].dimension] = + (iSrc >> baseBits) & ((1 << srcBits) - 1); + // there are also DimensionRegion objects for unused zones, skip them + if (dimCase[pDimensionDefinitions[d].dimension] >= pDimensionDefinitions[d].zones) { + isValidZone = false; + break; + } + baseBits += srcBits; + } + if (!isValidZone) continue; + // a bit paranoid: cope with the chance that the dimensions would + // have different order in source and destination regions + if (dimCase[type] > zone) dimCase[type]++; + DimensionRegion* dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase); + dstDimRgn->CopyAssign(srcDimRgn); + // if this is the requested zone to be splitted, then also copy + // the source DimensionRegion to the newly created target zone + // and set the old zones upper limit lower + if (dimCase[type] == zone) { + // lower old zones upper limit + if (newDef.split_type == split_type_normal) { + const int high = + dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex]; + int low = 0; + if (zone > 0) { + std::map lowerCase = dimCase; + lowerCase[type]--; + DimensionRegion* dstDimRgnLow = tempRgn->GetDimensionRegionByBit(lowerCase); + low = dstDimRgnLow->DimensionUpperLimits[tempIncreasedDimensionIndex]; + } + dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex] = low + (high - low) / 2; + } + // fill the newly created zone of the divided zone as well + dimCase[type]++; + dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase); + dstDimRgn->CopyAssign(srcDimRgn); + } + } + + // now tempRegion's dimensions and DimensionRegions basically reflect + // what we wanted to get for this actual Region here, so we now just + // delete and recreate the dimension in question with the new amount + // zones and then copy back from tempRegion + DeleteDimension(oldDef); + AddDimension(&newDef); + for (int iSrc = 0; iSrc < 256; ++iSrc) { + DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc]; + if (!srcDimRgn) continue; + std::map dimCase; + for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) { + const int srcBits = tempRgn->pDimensionDefinitions[d].bits; + dimCase[tempRgn->pDimensionDefinitions[d].dimension] = + (iSrc >> baseBits) & ((1 << srcBits) - 1); + baseBits += srcBits; + } + // a bit paranoid: cope with the chance that the dimensions would + // have different order in source and destination regions + DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase); + if (!dstDimRgn) continue; + dstDimRgn->CopyAssign(srcDimRgn); + } + + // delete temporary region + delete tempRgn; + + UpdateVelocityTable(); + } + + /** @brief Change type of an existing dimension. + * + * Alters the dimension type of a dimension already existing on this + * region. If there is currently no dimension on this Region with type + * @a oldType, then this call with throw an Exception. Likewise there are + * cases where the requested dimension type cannot be performed. For example + * if the new dimension type shall be gig::dimension_samplechannel, and the + * current dimension has more than 2 zones. In such cases an Exception is + * thrown as well. + * + * @param oldType - identifies the existing dimension to be changed + * @param newType - to which dimension type it should be changed to + * @throws gig::Exception if requested change cannot be performed + */ + void Region::SetDimensionType(dimension_t oldType, dimension_t newType) { + if (oldType == newType) return; + dimension_def_t* def = GetDimensionDefinition(oldType); + if (!def) + throw gig::Exception("No dimension with provided old dimension type exists on this region"); + if (newType == dimension_samplechannel && def->zones != 2) + throw gig::Exception("Cannot change to dimension type 'sample channel', because existing dimension does not have 2 zones"); + if (GetDimensionDefinition(newType)) + throw gig::Exception("There is already a dimension with requested new dimension type on this region"); + def->dimension = newType; + def->split_type = __resolveSplitType(newType); + } + + DimensionRegion* Region::GetDimensionRegionByBit(const std::map& DimCase) { + uint8_t bits[8] = {}; + for (std::map::const_iterator it = DimCase.begin(); + it != DimCase.end(); ++it) + { + for (int d = 0; d < Dimensions; ++d) { + if (pDimensionDefinitions[d].dimension == it->first) { + bits[d] = it->second; + goto nextDimCaseSlice; + } + } + assert(false); // do crash ... too harsh maybe ? ignore it instead ? + nextDimCaseSlice: + ; // noop + } + return GetDimensionRegionByBit(bits); + } + + /** + * Searches in the current Region for a dimension of the given dimension + * type and returns the precise configuration of that dimension in this + * Region. + * + * @param type - dimension type of the sought dimension + * @returns dimension definition or NULL if there is no dimension with + * sought type in this Region. + */ + dimension_def_t* Region::GetDimensionDefinition(dimension_t type) { + for (int i = 0; i < Dimensions; ++i) + if (pDimensionDefinitions[i].dimension == type) + return &pDimensionDefinitions[i]; + return NULL; + } + Region::~Region() { for (int i = 0; i < 256; i++) { if (pDimensionRegions[i]) delete pDimensionRegions[i]; @@ -3401,7 +4009,7 @@ DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { uint8_t bits; int veldim = -1; - int velbitpos; + int velbitpos = 0; int bitpos = 0; int dimregidx = 0; for (uint i = 0; i < Dimensions; i++) { @@ -3431,20 +4039,72 @@ } bitpos += pDimensionDefinitions[i].bits; } - DimensionRegion* dimreg = pDimensionRegions[dimregidx]; + DimensionRegion* dimreg = pDimensionRegions[dimregidx & 255]; + if (!dimreg) return NULL; if (veldim != -1) { // (dimreg is now the dimension region for the lowest velocity) if (dimreg->VelocityTable) // custom defined zone ranges - bits = dimreg->VelocityTable[DimValues[veldim]]; + bits = dimreg->VelocityTable[DimValues[veldim] & 127]; else // normal split type - bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); + bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size); - dimregidx |= bits << velbitpos; - dimreg = pDimensionRegions[dimregidx]; + const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1; + dimregidx |= (bits & limiter_mask) << velbitpos; + dimreg = pDimensionRegions[dimregidx & 255]; } return dimreg; } + int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) { + uint8_t bits; + int veldim = -1; + int velbitpos = 0; + int bitpos = 0; + int dimregidx = 0; + for (uint i = 0; i < Dimensions; i++) { + if (pDimensionDefinitions[i].dimension == dimension_velocity) { + // the velocity dimension must be handled after the other dimensions + veldim = i; + velbitpos = bitpos; + } else { + switch (pDimensionDefinitions[i].split_type) { + case split_type_normal: + if (pDimensionRegions[0]->DimensionUpperLimits[i]) { + // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges + for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) { + if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break; + } + } else { + // gig2: evenly sized zones + bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); + } + break; + case split_type_bit: // the value is already the sought dimension bit number + const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; + bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed + break; + } + dimregidx |= bits << bitpos; + } + bitpos += pDimensionDefinitions[i].bits; + } + dimregidx &= 255; + DimensionRegion* dimreg = pDimensionRegions[dimregidx]; + if (!dimreg) return -1; + if (veldim != -1) { + // (dimreg is now the dimension region for the lowest velocity) + if (dimreg->VelocityTable) // custom defined zone ranges + bits = dimreg->VelocityTable[DimValues[veldim] & 127]; + else // normal split type + bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size); + + const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1; + dimregidx |= (bits & limiter_mask) << velbitpos; + dimregidx &= 255; + } + return dimregidx; + } + /** * Returns the appropriate DimensionRegion for the given dimension bit * numbers (zone index). You usually use GetDimensionRegionByValue @@ -3483,13 +4143,29 @@ if ((int32_t)WavePoolTableIndex == -1) return NULL; File* file = (File*) GetParent()->GetParent(); if (!file->pWavePoolTable) return NULL; - unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; - unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; - Sample* sample = file->GetFirstSample(pProgress); - while (sample) { - if (sample->ulWavePoolOffset == soughtoffset && - sample->FileNo == soughtfileno) return static_cast(sample); - sample = file->GetNextSample(); + if (WavePoolTableIndex + 1 > file->WavePoolCount) return NULL; + // for new files or files >= 2 GB use 64 bit wave pool offsets + if (file->pRIFF->IsNew() || (file->pRIFF->GetCurrentFileSize() >> 31)) { + // use 64 bit wave pool offsets (treating this as large file) + uint64_t soughtoffset = + uint64_t(file->pWavePoolTable[WavePoolTableIndex]) | + uint64_t(file->pWavePoolTableHi[WavePoolTableIndex]) << 32; + Sample* sample = file->GetFirstSample(pProgress); + while (sample) { + if (sample->ullWavePoolOffset == soughtoffset) + return static_cast(sample); + sample = file->GetNextSample(); + } + } else { + // use extension files and 32 bit wave pool offsets + file_offset_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; + file_offset_t soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; + Sample* sample = file->GetFirstSample(pProgress); + while (sample) { + if (sample->ullWavePoolOffset == soughtoffset && + sample->FileNo == soughtfileno) return static_cast(sample); + sample = file->GetNextSample(); + } } return NULL; } @@ -3711,6 +4387,275 @@ } } +// *************** Script *************** +// * + + Script::Script(ScriptGroup* group, RIFF::Chunk* ckScri) { + pGroup = group; + pChunk = ckScri; + if (ckScri) { // object is loaded from file ... + // read header + uint32_t headerSize = ckScri->ReadUint32(); + Compression = (Compression_t) ckScri->ReadUint32(); + Encoding = (Encoding_t) ckScri->ReadUint32(); + Language = (Language_t) ckScri->ReadUint32(); + Bypass = (Language_t) ckScri->ReadUint32() & 1; + crc = ckScri->ReadUint32(); + uint32_t nameSize = ckScri->ReadUint32(); + Name.resize(nameSize, ' '); + for (int i = 0; i < nameSize; ++i) + Name[i] = ckScri->ReadUint8(); + // to handle potential future extensions of the header + ckScri->SetPos(sizeof(int32_t) + headerSize); + // read actual script data + uint32_t scriptSize = uint32_t(ckScri->GetSize() - ckScri->GetPos()); + data.resize(scriptSize); + for (int i = 0; i < scriptSize; ++i) + data[i] = ckScri->ReadUint8(); + } else { // this is a new script object, so just initialize it as such ... + Compression = COMPRESSION_NONE; + Encoding = ENCODING_ASCII; + Language = LANGUAGE_NKSP; + Bypass = false; + crc = 0; + Name = "Unnamed Script"; + } + } + + Script::~Script() { + } + + /** + * Returns the current script (i.e. as source code) in text format. + */ + String Script::GetScriptAsText() { + String s; + s.resize(data.size(), ' '); + memcpy(&s[0], &data[0], data.size()); + return s; + } + + /** + * Replaces the current script with the new script source code text given + * by @a text. + * + * @param text - new script source code + */ + void Script::SetScriptAsText(const String& text) { + data.resize(text.size()); + memcpy(&data[0], &text[0], text.size()); + } + + /** + * Apply this script to the respective RIFF chunks. You have to call + * File::Save() to make changes persistent. + * + * Usually there is absolutely no need to call this method explicitly. + * It will be called automatically when File::Save() was called. + * + * @param pProgress - callback function for progress notification + */ + void Script::UpdateChunks(progress_t* pProgress) { + // recalculate CRC32 check sum + __resetCRC(crc); + __calculateCRC(&data[0], data.size(), crc); + __finalizeCRC(crc); + // make sure chunk exists and has the required size + const file_offset_t chunkSize = (file_offset_t) 7*sizeof(int32_t) + Name.size() + data.size(); + if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); + else pChunk->Resize(chunkSize); + // fill the chunk data to be written to disk + uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); + int pos = 0; + store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size())); // total header size + pos += sizeof(int32_t); + store32(&pData[pos], Compression); + pos += sizeof(int32_t); + store32(&pData[pos], Encoding); + pos += sizeof(int32_t); + store32(&pData[pos], Language); + pos += sizeof(int32_t); + store32(&pData[pos], Bypass ? 1 : 0); + pos += sizeof(int32_t); + store32(&pData[pos], crc); + pos += sizeof(int32_t); + store32(&pData[pos], (uint32_t) Name.size()); + pos += sizeof(int32_t); + for (int i = 0; i < Name.size(); ++i, ++pos) + pData[pos] = Name[i]; + for (int i = 0; i < data.size(); ++i, ++pos) + pData[pos] = data[i]; + } + + /** + * Move this script from its current ScriptGroup to another ScriptGroup + * given by @a pGroup. + * + * @param pGroup - script's new group + */ + void Script::SetGroup(ScriptGroup* pGroup) { + if (this->pGroup == pGroup) return; + if (pChunk) + pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList); + this->pGroup = pGroup; + } + + /** + * Returns the script group this script currently belongs to. Each script + * is a member of exactly one ScriptGroup. + * + * @returns current script group + */ + ScriptGroup* Script::GetGroup() const { + return pGroup; + } + + /** + * Make a (semi) deep copy of the Script object given by @a orig + * and assign it to this object. Note: the ScriptGroup this Script + * object belongs to remains untouched by this call. + * + * @param orig - original Script object to be copied from + */ + void Script::CopyAssign(const Script* orig) { + Name = orig->Name; + Compression = orig->Compression; + Encoding = orig->Encoding; + Language = orig->Language; + Bypass = orig->Bypass; + data = orig->data; + } + + void Script::RemoveAllScriptReferences() { + File* pFile = pGroup->pFile; + for (int i = 0; pFile->GetInstrument(i); ++i) { + Instrument* instr = pFile->GetInstrument(i); + instr->RemoveScript(this); + } + } + +// *************** ScriptGroup *************** +// * + + ScriptGroup::ScriptGroup(File* file, RIFF::List* lstRTIS) { + pFile = file; + pList = lstRTIS; + pScripts = NULL; + if (lstRTIS) { + RIFF::Chunk* ckName = lstRTIS->GetSubChunk(CHUNK_ID_LSNM); + ::LoadString(ckName, Name); + } else { + Name = "Default Group"; + } + } + + ScriptGroup::~ScriptGroup() { + if (pScripts) { + std::list::iterator iter = pScripts->begin(); + std::list::iterator end = pScripts->end(); + while (iter != end) { + delete *iter; + ++iter; + } + delete pScripts; + } + } + + /** + * Apply this script group to the respective RIFF chunks. You have to call + * File::Save() to make changes persistent. + * + * Usually there is absolutely no need to call this method explicitly. + * It will be called automatically when File::Save() was called. + * + * @param pProgress - callback function for progress notification + */ + void ScriptGroup::UpdateChunks(progress_t* pProgress) { + if (pScripts) { + if (!pList) + pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS); + + // now store the name of this group as chunk as subchunk of the list chunk + ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64); + + for (std::list::iterator it = pScripts->begin(); + it != pScripts->end(); ++it) + { + (*it)->UpdateChunks(pProgress); + } + } + } + + /** @brief Get instrument script. + * + * Returns the real-time instrument script with the given index. + * + * @param index - number of the sought script (0..n) + * @returns sought script or NULL if there's no such script + */ + Script* ScriptGroup::GetScript(uint index) { + if (!pScripts) LoadScripts(); + std::list::iterator it = pScripts->begin(); + for (uint i = 0; it != pScripts->end(); ++i, ++it) + if (i == index) return *it; + return NULL; + } + + /** @brief Add new instrument script. + * + * Adds a new real-time instrument script to the file. The script is not + * actually used / executed unless it is referenced by an instrument to be + * used. This is similar to samples, which you can add to a file, without + * an instrument necessarily actually using it. + * + * You have to call Save() to make this persistent to the file. + * + * @return new empty script object + */ + Script* ScriptGroup::AddScript() { + if (!pScripts) LoadScripts(); + Script* pScript = new Script(this, NULL); + pScripts->push_back(pScript); + return pScript; + } + + /** @brief Delete an instrument script. + * + * This will delete the given real-time instrument script. References of + * instruments that are using that script will be removed accordingly. + * + * You have to call Save() to make this persistent to the file. + * + * @param pScript - script to delete + * @throws gig::Exception if given script could not be found + */ + void ScriptGroup::DeleteScript(Script* pScript) { + if (!pScripts) LoadScripts(); + std::list::iterator iter = + find(pScripts->begin(), pScripts->end(), pScript); + if (iter == pScripts->end()) + throw gig::Exception("Could not delete script, could not find given script"); + pScripts->erase(iter); + pScript->RemoveAllScriptReferences(); + if (pScript->pChunk) + pScript->pChunk->GetParent()->DeleteSubChunk(pScript->pChunk); + delete pScript; + } + + void ScriptGroup::LoadScripts() { + if (pScripts) return; + pScripts = new std::list; + if (!pList) return; + + for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck; + ck = pList->GetNextSubChunk()) + { + if (ck->GetChunkID() == CHUNK_ID_SCRI) { + pScripts->push_back(new Script(this, ck)); + } + } + } + // *************** Instrument *************** // * @@ -3727,12 +4672,13 @@ EffectSend = 0; Attenuation = 0; FineTune = 0; - PitchbendRange = 0; + PitchbendRange = 2; PianoReleaseMode = false; DimensionKeyRange.low = 0; DimensionKeyRange.high = 0; pMidiRules = new MidiRule*[3]; pMidiRules[0] = NULL; + pScriptRefs = NULL; // Loading RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); @@ -3793,6 +4739,28 @@ } } + // own gig format extensions + RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS); + if (lst3LS) { + RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); + if (ckSCSL) { + int headerSize = ckSCSL->ReadUint32(); + int slotCount = ckSCSL->ReadUint32(); + if (slotCount) { + int slotSize = ckSCSL->ReadUint32(); + ckSCSL->SetPos(headerSize); // in case of future header extensions + int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions + for (int i = 0; i < slotCount; ++i) { + _ScriptPooolEntry e; + e.fileOffset = ckSCSL->ReadUint32(); + e.bypass = ckSCSL->ReadUint32() & 1; + if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions + scriptPoolFileOffsets.push_back(e); + } + } + } + } + __notify_progress(pProgress, 1.0f); // notify done } @@ -3802,7 +4770,9 @@ RegionList::iterator end = pRegions->end(); for (; iter != end; ++iter) { gig::Region* pRegion = static_cast(*iter); - for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) { + const int low = std::max(int(pRegion->KeyRange.low), 0); + const int high = std::min(int(pRegion->KeyRange.high), 127); + for (int iKey = low; iKey <= high; iKey++) { RegionKeyTable[iKey] = pRegion; } } @@ -3813,6 +4783,7 @@ delete pMidiRules[i]; } delete[] pMidiRules; + if (pScriptRefs) delete pScriptRefs; } /** @@ -3822,18 +4793,19 @@ * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. * + * @param pProgress - callback function for progress notification * @throws gig::Exception if samples cannot be dereferenced */ - void Instrument::UpdateChunks() { + void Instrument::UpdateChunks(progress_t* pProgress) { // first update base classes' chunks - DLS::Instrument::UpdateChunks(); + DLS::Instrument::UpdateChunks(pProgress); // update Regions' chunks { RegionList::iterator iter = pRegions->begin(); RegionList::iterator end = pRegions->end(); for (; iter != end; ++iter) - (*iter)->UpdateChunks(); + (*iter)->UpdateChunks(pProgress); } // make sure 'lart' RIFF list chunk exists @@ -3868,6 +4840,65 @@ pMidiRules[i]->UpdateChunks(pData); } } + + // own gig format extensions + if (ScriptSlotCount()) { + // make sure we have converted the original loaded script file + // offsets into valid Script object pointers + LoadScripts(); + + RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); + if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); + const int slotCount = (int) pScriptRefs->size(); + const int headerSize = 3 * sizeof(uint32_t); + const int slotSize = 2 * sizeof(uint32_t); + const int totalChunkSize = headerSize + slotCount * slotSize; + RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); + if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize); + else ckSCSL->Resize(totalChunkSize); + uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData(); + int pos = 0; + store32(&pData[pos], headerSize); + pos += sizeof(uint32_t); + store32(&pData[pos], slotCount); + pos += sizeof(uint32_t); + store32(&pData[pos], slotSize); + pos += sizeof(uint32_t); + for (int i = 0; i < slotCount; ++i) { + // arbitrary value, the actual file offset will be updated in + // UpdateScriptFileOffsets() after the file has been resized + int bogusFileOffset = 0; + store32(&pData[pos], bogusFileOffset); + pos += sizeof(uint32_t); + store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0); + pos += sizeof(uint32_t); + } + } else { + // no script slots, so get rid of any LS custom RIFF chunks (if any) + RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); + if (lst3LS) pCkInstrument->DeleteSubChunk(lst3LS); + } + } + + void Instrument::UpdateScriptFileOffsets() { + // own gig format extensions + if (pScriptRefs && pScriptRefs->size() > 0) { + RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); + RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); + const int slotCount = (int) pScriptRefs->size(); + const int headerSize = 3 * sizeof(uint32_t); + ckSCSL->SetPos(headerSize); + for (int i = 0; i < slotCount; ++i) { + uint32_t fileOffset = uint32_t( + (*pScriptRefs)[i].script->pChunk->GetFilePos() - + (*pScriptRefs)[i].script->pChunk->GetPos() - + CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize()) + ); + ckSCSL->WriteUint32(&fileOffset); + // jump over flags entry (containing the bypass flag) + ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos); + } + } } /** @@ -3922,7 +4953,7 @@ RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); Region* pNewRegion = new Region(this, rgn); pRegions->push_back(pNewRegion); - Regions = pRegions->size(); + Regions = (uint32_t) pRegions->size(); // update Region key table for fast lookup UpdateRegionKeyTable(); // done @@ -3937,6 +4968,63 @@ } /** + * Move this instrument at the position before @arg dst. + * + * This method can be used to reorder the sequence of instruments in a + * .gig file. This might be helpful especially on large .gig files which + * contain a large number of instruments within the same .gig file. So + * grouping such instruments to similar ones, can help to keep track of them + * when working with such complex .gig files. + * + * When calling this method, this instrument will be removed from in its + * current position in the instruments list and moved to the requested + * target position provided by @param dst. You may also pass NULL as + * argument to this method, in that case this intrument will be moved to the + * very end of the .gig file's instrument list. + * + * You have to call Save() to make the order change persistent to the .gig + * file. + * + * Currently this method is limited to moving the instrument within the same + * .gig file. Trying to move it to another .gig file by calling this method + * will throw an exception. + * + * @param dst - destination instrument at which this instrument will be + * moved to, or pass NULL for moving to end of list + * @throw gig::Exception if this instrument and target instrument are not + * part of the same file + */ + void Instrument::MoveTo(Instrument* dst) { + if (dst && GetParent() != dst->GetParent()) + throw Exception( + "gig::Instrument::MoveTo() can only be used for moving within " + "the same gig file." + ); + + File* pFile = (File*) GetParent(); + + // move this instrument within the instrument list + { + File::InstrumentList& list = *pFile->pInstruments; + + File::InstrumentList::iterator itFrom = + std::find(list.begin(), list.end(), static_cast(this)); + + File::InstrumentList::iterator itTo = + std::find(list.begin(), list.end(), static_cast(dst)); + + list.splice(itTo, list, itFrom); + } + + // move the instrument's actual list RIFF chunk appropriately + RIFF::List* lstCkInstruments = pFile->pRIFF->GetSubList(LIST_TYPE_LINS); + lstCkInstruments->MoveSubChunk( + this->pCkInstrument, + (RIFF::Chunk*) ((dst) ? dst->pCkInstrument : NULL) + ); + } + + /** * Returns a MIDI rule of the instrument. * * The list of MIDI rules, at least in gig v3, always contains at @@ -3999,6 +5087,216 @@ pMidiRules[i] = 0; } + void Instrument::LoadScripts() { + if (pScriptRefs) return; + pScriptRefs = new std::vector<_ScriptPooolRef>; + if (scriptPoolFileOffsets.empty()) return; + File* pFile = (File*) GetParent(); + for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) { + uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset; + for (uint i = 0; pFile->GetScriptGroup(i); ++i) { + ScriptGroup* group = pFile->GetScriptGroup(i); + for (uint s = 0; group->GetScript(s); ++s) { + Script* script = group->GetScript(s); + if (script->pChunk) { + uint32_t offset = uint32_t( + script->pChunk->GetFilePos() - + script->pChunk->GetPos() - + CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize()) + ); + if (offset == soughtOffset) + { + _ScriptPooolRef ref; + ref.script = script; + ref.bypass = scriptPoolFileOffsets[k].bypass; + pScriptRefs->push_back(ref); + break; + } + } + } + } + } + // we don't need that anymore + scriptPoolFileOffsets.clear(); + } + + /** @brief Get instrument script (gig format extension). + * + * Returns the real-time instrument script of instrument script slot + * @a index. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + * + * @param index - instrument script slot index + * @returns script or NULL if index is out of bounds + */ + Script* Instrument::GetScriptOfSlot(uint index) { + LoadScripts(); + if (index >= pScriptRefs->size()) return NULL; + return pScriptRefs->at(index).script; + } + + /** @brief Add new instrument script slot (gig format extension). + * + * Add the given real-time instrument script reference to this instrument, + * which shall be executed by the sampler for for this instrument. The + * script will be added to the end of the script list of this instrument. + * The positions of the scripts in the Instrument's Script list are + * relevant, because they define in which order they shall be executed by + * the sampler. For this reason it is also legal to add the same script + * twice to an instrument, for example you might have a script called + * "MyFilter" which performs an event filter task, and you might have + * another script called "MyNoteTrigger" which triggers new notes, then you + * might for example have the following list of scripts on the instrument: + * + * 1. Script "MyFilter" + * 2. Script "MyNoteTrigger" + * 3. Script "MyFilter" + * + * Which would make sense, because the 2nd script launched new events, which + * you might need to filter as well. + * + * There are two ways to disable / "bypass" scripts. You can either disable + * a script locally for the respective script slot on an instrument (i.e. by + * passing @c false to the 2nd argument of this method, or by calling + * SetScriptBypassed()). Or you can disable a script globally for all slots + * and all instruments by setting Script::Bypass. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + * + * @param pScript - script that shall be executed for this instrument + * @param bypass - if enabled, the sampler shall skip executing this + * script (in the respective list position) + * @see SetScriptBypassed() + */ + void Instrument::AddScriptSlot(Script* pScript, bool bypass) { + LoadScripts(); + _ScriptPooolRef ref = { pScript, bypass }; + pScriptRefs->push_back(ref); + } + + /** @brief Flip two script slots with each other (gig format extension). + * + * Swaps the position of the two given scripts in the Instrument's Script + * list. The positions of the scripts in the Instrument's Script list are + * relevant, because they define in which order they shall be executed by + * the sampler. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + * + * @param index1 - index of the first script slot to swap + * @param index2 - index of the second script slot to swap + */ + void Instrument::SwapScriptSlots(uint index1, uint index2) { + LoadScripts(); + if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size()) + return; + _ScriptPooolRef tmp = (*pScriptRefs)[index1]; + (*pScriptRefs)[index1] = (*pScriptRefs)[index2]; + (*pScriptRefs)[index2] = tmp; + } + + /** @brief Remove script slot. + * + * Removes the script slot with the given slot index. + * + * @param index - index of script slot to remove + */ + void Instrument::RemoveScriptSlot(uint index) { + LoadScripts(); + if (index >= pScriptRefs->size()) return; + pScriptRefs->erase( pScriptRefs->begin() + index ); + } + + /** @brief Remove reference to given Script (gig format extension). + * + * This will remove all script slots on the instrument which are referencing + * the given script. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + * + * @param pScript - script reference to remove from this instrument + * @see RemoveScriptSlot() + */ + void Instrument::RemoveScript(Script* pScript) { + LoadScripts(); + for (ssize_t i = pScriptRefs->size() - 1; i >= 0; --i) { + if ((*pScriptRefs)[i].script == pScript) { + pScriptRefs->erase( pScriptRefs->begin() + i ); + } + } + } + + /** @brief Instrument's amount of script slots. + * + * This method returns the amount of script slots this instrument currently + * uses. + * + * A script slot is a reference of a real-time instrument script to be + * executed by the sampler. The scripts will be executed by the sampler in + * sequence of the slots. One (same) script may be referenced multiple + * times in different slots. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + */ + uint Instrument::ScriptSlotCount() const { + return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size()); + } + + /** @brief Whether script execution shall be skipped. + * + * Defines locally for the Script reference slot in the Instrument's Script + * list, whether the script shall be skipped by the sampler regarding + * execution. + * + * It is also possible to ignore exeuction of the script globally, for all + * slots and for all instruments by setting Script::Bypass. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + * + * @param index - index of the script slot on this instrument + * @see Script::Bypass + */ + bool Instrument::IsScriptSlotBypassed(uint index) { + if (index >= ScriptSlotCount()) return false; + return pScriptRefs ? pScriptRefs->at(index).bypass + : scriptPoolFileOffsets.at(index).bypass; + + } + + /** @brief Defines whether execution shall be skipped. + * + * You can call this method to define locally whether or whether not the + * given script slot shall be executed by the sampler. + * + * @note This is an own format extension which did not exist i.e. in the + * GigaStudio 4 software. It will currently only work with LinuxSampler and + * gigedit. + * + * @param index - script slot index on this instrument + * @param bBypass - if true, the script slot will be skipped by the sampler + * @see Script::Bypass + */ + void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) { + if (index >= ScriptSlotCount()) return; + if (pScriptRefs) + pScriptRefs->at(index).bypass = bBypass; + else + scriptPoolFileOffsets.at(index).bypass = bBypass; + } + /** * Make a (semi) deep copy of the Instrument object given by @a orig * and assign it to this object. @@ -4032,6 +5330,8 @@ PitchbendRange = orig->PitchbendRange; PianoReleaseMode = orig->PianoReleaseMode; DimensionKeyRange = orig->DimensionKeyRange; + scriptPoolFileOffsets = orig->scriptPoolFileOffsets; + pScriptRefs = orig->pScriptRefs; // free old midi rules for (int i = 0 ; pMidiRules[i] ; i++) { @@ -4086,8 +5386,10 @@ * * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. + * + * @param pProgress - callback function for progress notification */ - void Group::UpdateChunks() { + void Group::UpdateChunks(progress_t* pProgress) { // make sure <3gri> and <3gnl> list chunks exist RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); if (!_3gri) { @@ -4217,6 +5519,7 @@ bAutoLoad = true; *pVersion = VERSION_3; pGroups = NULL; + pScriptGroups = NULL; pInfo->SetFixedStringLengths(_FileFixedStringLengths); pInfo->ArchivalLocation = String(256, ' '); @@ -4232,6 +5535,7 @@ File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { bAutoLoad = true; pGroups = NULL; + pScriptGroups = NULL; pInfo->SetFixedStringLengths(_FileFixedStringLengths); } @@ -4245,6 +5549,15 @@ } delete pGroups; } + if (pScriptGroups) { + std::list::iterator iter = pScriptGroups->begin(); + std::list::iterator end = pScriptGroups->end(); + while (iter != end) { + delete *iter; + ++iter; + } + delete pScriptGroups; + } } Sample* File::GetFirstSample(progress_t* pProgress) { @@ -4353,19 +5666,22 @@ int iTotalSamples = WavePoolCount; // check if samples should be loaded from extension files + // (only for old gig files < 2 GB) int lastFileNo = 0; - for (int i = 0 ; i < WavePoolCount ; i++) { - if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; + if (!file->IsNew() && !(file->GetCurrentFileSize() >> 31)) { + for (int i = 0 ; i < WavePoolCount ; i++) { + if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; + } } String name(pRIFF->GetFileName()); - int nameLen = name.length(); + int nameLen = (int) name.length(); char suffix[6]; if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; for (int fileNo = 0 ; ; ) { RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); if (wvpl) { - unsigned long wvplFileOffset = wvpl->GetFilePos(); + file_offset_t wvplFileOffset = wvpl->GetFilePos(); RIFF::List* wave = wvpl->GetFirstSubList(); while (wave) { if (wave->GetListType() == LIST_TYPE_WAVE) { @@ -4373,8 +5689,8 @@ const float subprogress = (float) iSampleIndex / (float) iTotalSamples; __notify_progress(pProgress, subprogress); - unsigned long waveFileOffset = wave->GetFilePos(); - pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); + file_offset_t waveFileOffset = wave->GetFilePos(); + pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo, iSampleIndex)); iSampleIndex++; } @@ -4527,7 +5843,19 @@ mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s); mSamples[pFile->GetSample(i)] = s; } - + + // clone script groups and their scripts + for (int iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { + ScriptGroup* sg = pFile->GetScriptGroup(iGroup); + ScriptGroup* dg = AddScriptGroup(); + dg->Name = "COPY" + ToString(iCallCount) + "_" + sg->Name; + for (int iScript = 0; sg->GetScript(iScript); ++iScript) { + Script* ss = sg->GetScript(iScript); + Script* ds = dg->AddScript(); + ds->CopyAssign(ss); + } + } + //BUG: For some reason this method only works with this additional // Save() call in between here. // @@ -4612,24 +5940,148 @@ if (!_3crc) return; // get the index of the sample - int iWaveIndex = -1; - 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; - } - } + int iWaveIndex = GetWaveTableIndexOf(pSample); if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); // write the CRC-32 checksum to disk _3crc->SetPos(iWaveIndex * 8); - uint32_t tmp = 1; - _3crc->WriteUint32(&tmp); // unknown, always 1? + uint32_t one = 1; + _3crc->WriteUint32(&one); // always 1 _3crc->WriteUint32(&crc); } + uint32_t File::GetSampleChecksum(Sample* pSample) { + // get the index of the sample + int iWaveIndex = GetWaveTableIndexOf(pSample); + if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); + + return GetSampleChecksumByIndex(iWaveIndex); + } + + uint32_t File::GetSampleChecksumByIndex(int index) { + if (index < 0) throw gig::Exception("Could not retrieve reference crc of sample, invalid wave pool index of sample"); + + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); + uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); + if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); + + // read the CRC-32 checksum directly from disk + size_t pos = index * 8; + if (pos + 8 > _3crc->GetNewSize()) + throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); + + uint32_t one = load32(&pData[pos]); // always 1 + if (one != 1) + throw gig::Exception("Could not retrieve reference crc of sample, because reference checksum table is damaged"); + + return load32(&pData[pos+4]); + } + + int File::GetWaveTableIndexOf(gig::Sample* pSample) { + if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (int index = 0; iter != end; ++iter, ++index) + if (*iter == pSample) + return index; + return -1; + } + + /** + * Checks whether the file's "3CRC" chunk was damaged. This chunk contains + * the CRC32 check sums of all samples' raw wave data. + * + * @return true if 3CRC chunk is OK, or false if 3CRC chunk is damaged + */ + bool File::VerifySampleChecksumTable() { + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (!_3crc) return false; + if (_3crc->GetNewSize() <= 0) return false; + if (_3crc->GetNewSize() % 8) return false; + if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned + if (_3crc->GetNewSize() != pSamples->size() * 8) return false; + + const file_offset_t n = _3crc->GetNewSize() / 8; + + uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); + if (!pData) return false; + + for (file_offset_t i = 0; i < n; ++i) { + uint32_t one = pData[i*2]; + if (one != 1) return false; + } + + return true; + } + + /** + * Recalculates CRC32 checksums for all samples and rebuilds this gig + * file's checksum table with those new checksums. This might usually + * just be necessary if the checksum table was damaged. + * + * @e IMPORTANT: The current implementation of this method only works + * with files that have not been modified since it was loaded, because + * it expects that no externally caused file structure changes are + * required! + * + * Due to the expectation above, this method is currently protected + * and actually only used by the command line tool "gigdump" yet. + * + * @returns true if Save() is required to be called after this call, + * false if no further action is required + */ + bool File::RebuildSampleChecksumTable() { + // make sure sample chunks were scanned + if (!pSamples) GetFirstSample(); + + bool bRequiresSave = false; + + // make sure "3CRC" chunk exists with required size + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (!_3crc) { + _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); + // the order of einf and 3crc is not the same in v2 and v3 + RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); + if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); + bRequiresSave = true; + } else if (_3crc->GetNewSize() != pSamples->size() * 8) { + _3crc->Resize(pSamples->size() * 8); + bRequiresSave = true; + } + + if (bRequiresSave) { // refill CRC table for all samples in RAM ... + uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); + { + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (; iter != end; ++iter) { + gig::Sample* pSample = (gig::Sample*) *iter; + int index = GetWaveTableIndexOf(pSample); + if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); + pData[index*2] = 1; // always 1 + pData[index*2+1] = pSample->CalculateWaveDataChecksum(); + } + } + } else { // no file structure changes necessary, so directly write to disk and we are done ... + // make sure file is in write mode + pRIFF->SetMode(RIFF::stream_mode_read_write); + { + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (; iter != end; ++iter) { + gig::Sample* pSample = (gig::Sample*) *iter; + int index = GetWaveTableIndexOf(pSample); + if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); + pSample->crc = pSample->CalculateWaveDataChecksum(); + SetSampleChecksum(pSample, pSample->crc); + } + } + } + + return bRequiresSave; + } + Group* File::GetFirstGroup() { if (!pGroups) LoadGroups(); // there must always be at least one group @@ -4757,6 +6209,93 @@ } } + /** @brief Get instrument script group (by index). + * + * Returns the real-time instrument script group with the given index. + * + * @param index - number of the sought group (0..n) + * @returns sought script group or NULL if there's no such group + */ + ScriptGroup* File::GetScriptGroup(uint index) { + if (!pScriptGroups) LoadScriptGroups(); + std::list::iterator it = pScriptGroups->begin(); + for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) + if (i == index) return *it; + return NULL; + } + + /** @brief Get instrument script group (by name). + * + * Returns the first real-time instrument script group found with the given + * group name. Note that group names may not necessarily be unique. + * + * @param name - name of the sought script group + * @returns sought script group or NULL if there's no such group + */ + ScriptGroup* File::GetScriptGroup(const String& name) { + if (!pScriptGroups) LoadScriptGroups(); + std::list::iterator it = pScriptGroups->begin(); + for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) + if ((*it)->Name == name) return *it; + return NULL; + } + + /** @brief Add new instrument script group. + * + * Adds a new, empty real-time instrument script group to the file. + * + * You have to call Save() to make this persistent to the file. + * + * @return new empty script group + */ + ScriptGroup* File::AddScriptGroup() { + if (!pScriptGroups) LoadScriptGroups(); + ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL); + pScriptGroups->push_back(pScriptGroup); + return pScriptGroup; + } + + /** @brief Delete an instrument script group. + * + * This will delete the given real-time instrument script group and all its + * instrument scripts it contains. References inside instruments that are + * using the deleted scripts will be removed from the respective instruments + * accordingly. + * + * You have to call Save() to make this persistent to the file. + * + * @param pScriptGroup - script group to delete + * @throws gig::Exception if given script group could not be found + */ + void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) { + if (!pScriptGroups) LoadScriptGroups(); + std::list::iterator iter = + find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup); + if (iter == pScriptGroups->end()) + throw gig::Exception("Could not delete script group, could not find given script group"); + pScriptGroups->erase(iter); + for (int i = 0; pScriptGroup->GetScript(i); ++i) + pScriptGroup->DeleteScript(pScriptGroup->GetScript(i)); + if (pScriptGroup->pList) + pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList); + delete pScriptGroup; + } + + void File::LoadScriptGroups() { + if (pScriptGroups) return; + pScriptGroups = new std::list; + RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS); + if (lstLS) { + for (RIFF::List* lst = lstLS->GetFirstSubList(); lst; + lst = lstLS->GetNextSubList()) + { + if (lst->GetListType() == LIST_TYPE_RTIS) { + pScriptGroups->push_back(new ScriptGroup(this, lst)); + } + } + } + } + /** * Apply all the gig file's current instruments, samples, groups and settings * to the respective RIFF chunks. You have to call Save() to make changes @@ -4765,15 +6304,45 @@ * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. * + * @param pProgress - callback function for progress notification * @throws Exception - on errors */ - void File::UpdateChunks() { + void File::UpdateChunks(progress_t* pProgress) { bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; - b64BitWavePoolOffsets = pVersion && pVersion->major == 3; + // update own gig format extension chunks + // (not part of the GigaStudio 4 format) + RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS); + if (!lst3LS) { + lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS); + } + // Make sure <3LS > chunk is placed before chunk. The precise + // location of <3LS > is irrelevant, however it should be located + // before the actual wave data + RIFF::Chunk* ckPTBL = pRIFF->GetSubChunk(CHUNK_ID_PTBL); + pRIFF->MoveSubChunk(lst3LS, ckPTBL); + + // This must be performed before writing the chunks for instruments, + // because the instruments' script slots will write the file offsets + // of the respective instrument script chunk as reference. + if (pScriptGroups) { + // Update instrument script (group) chunks. + for (std::list::iterator it = pScriptGroups->begin(); + it != pScriptGroups->end(); ++it) + { + (*it)->UpdateChunks(pProgress); + } + } + + // in case no libgig custom format data was added, then remove the + // custom "3LS " chunk again + if (!lst3LS->CountSubChunks()) { + pRIFF->DeleteSubChunk(lst3LS); + lst3LS = NULL; + } // first update base class's chunks - DLS::File::UpdateChunks(); + DLS::File::UpdateChunks(pProgress); if (newFile) { // INFO was added by Resource::UpdateChunks - make sure it @@ -4810,7 +6379,7 @@ std::list::iterator iter = pGroups->begin(); std::list::iterator end = pGroups->end(); for (; iter != end; ++iter) { - (*iter)->UpdateChunks(); + (*iter)->UpdateChunks(pProgress); } } @@ -4830,7 +6399,7 @@ // Note that there are several fields with unknown use. These // are set to zero. - int sublen = pSamples->size() / 8 + 49; + int sublen = int(pSamples->size() / 8 + 49); int einfSize = (Instruments + 1) * sublen; RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); @@ -4903,7 +6472,7 @@ store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); // next 8 bytes unknown store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); - store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); + store32(&pData[(instrumentIdx + 1) * sublen + 40], (uint32_t) pSamples->size()); // next 4 bytes unknown totnbregions += instrument->Regions; @@ -4921,26 +6490,54 @@ store32(&pData[24], totnbloops); // next 8 bytes unknown // next 4 bytes unknown, not always 0 - store32(&pData[40], pSamples->size()); + store32(&pData[40], (uint32_t) pSamples->size()); // next 4 bytes unknown } // update 3crc chunk // The 3crc chunk contains CRC-32 checksums for the - // samples. The actual checksum values will be filled in - // later, by Sample::Write. + // samples. When saving a gig file to disk, we first update the 3CRC + // chunk here (in RAM) with the old crc values which we read from the + // 3CRC chunk when we opened the file (available with gig::Sample::crc + // member variable). This step is required, because samples might have + // been deleted by the user since the file was opened, which in turn + // changes the order of the (i.e. old) checksums within the 3crc chunk. + // If a sample was conciously modified by the user (that is if + // Sample::Write() was called later on) then Sample::Write() will just + // update the respective individual checksum(s) directly on disk and + // leaves all other sample checksums untouched. RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); if (_3crc) { _3crc->Resize(pSamples->size() * 8); - } else if (newFile) { + } else /*if (newFile)*/ { _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); - _3crc->LoadChunkData(); - // the order of einf and 3crc is not the same in v2 and v3 if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); } + { // must be performed in RAM here ... + uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); + if (pData) { + File::SampleList::iterator iter = pSamples->begin(); + File::SampleList::iterator end = pSamples->end(); + for (int index = 0; iter != end; ++iter, ++index) { + gig::Sample* pSample = (gig::Sample*) *iter; + pData[index*2] = 1; // always 1 + pData[index*2+1] = pSample->crc; + } + } + } + } + + void File::UpdateFileOffsets() { + DLS::File::UpdateFileOffsets(); + + for (Instrument* instrument = GetFirstInstrument(); instrument; + instrument = GetNextInstrument()) + { + instrument->UpdateScriptFileOffsets(); + } } /** @@ -4975,7 +6572,18 @@ // *************** Exception *************** // * - Exception::Exception(String Message) : DLS::Exception(Message) { + Exception::Exception() : DLS::Exception() { + } + + Exception::Exception(String format, ...) : DLS::Exception() { + va_list arg; + va_start(arg, format); + Message = assemble(format, arg); + va_end(arg); + } + + Exception::Exception(String format, va_list arg) : DLS::Exception() { + Message = assemble(format, arg); } void Exception::PrintMessage() {