--- libgig/trunk/src/gig.cpp 2004/03/10 21:34:28 36 +++ libgig/trunk/src/gig.cpp 2005/09/17 14:24:45 773 @@ -2,8 +2,8 @@ * * * libgig - C++ cross-platform Gigasampler format file loader library * * * - * Copyright (C) 2003 by Christian Schoenebeck * - * * + * Copyright (C) 2003-2005 by Christian Schoenebeck * + * * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -23,17 +23,218 @@ #include "gig.h" +#include + 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; + } + } + + +// *************** Internal functions for sample decopmression *************** +// * + +namespace { + + inline int get12lo(const unsigned char* pSrc) + { + const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8; + return x & 0x800 ? x - 0x1000 : x; + } + + inline int get12hi(const unsigned char* pSrc) + { + const int x = pSrc[1] >> 4 | pSrc[2] << 4; + return x & 0x800 ? x - 0x1000 : x; + } + + inline int16_t get16(const unsigned char* pSrc) + { + return int16_t(pSrc[0] | pSrc[1] << 8); + } + + inline int get24(const unsigned char* pSrc) + { + const int x = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16; + return x & 0x800000 ? x - 0x1000000 : x; + } + + 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) + { + switch (compressionmode) { + case 0: // 16 bit uncompressed + pSrc += currentframeoffset * srcStep; + while (copysamples) { + *pDst = get16(pSrc); + pDst += dstStep; + pSrc += srcStep; + copysamples--; + } + break; + + case 1: // 16 bit compressed to 8 bit + int y = get16(params); + int dy = get16(params + 2); + while (currentframeoffset) { + dy -= int8_t(*pSrc); + y -= dy; + pSrc += srcStep; + currentframeoffset--; + } + while (copysamples) { + dy -= int8_t(*pSrc); + y -= dy; + *pDst = y; + pDst += dstStep; + pSrc += srcStep; + copysamples--; + } + break; + } + } + + void Decompress24(int compressionmode, const unsigned char* params, + int dstStep, const unsigned char* pSrc, int16_t* pDst, + unsigned long currentframeoffset, + unsigned long copysamples, int truncatedBits) + { + // Note: The 24 bits are truncated to 16 bits for now. + + int y, dy, ddy, dddy; + const int shift = 8 - truncatedBits; + +#define GET_PARAMS(params) \ + y = get24(params); \ + dy = y - get24((params) + 3); \ + ddy = get24((params) + 6); \ + dddy = get24((params) + 9) + +#define SKIP_ONE(x) \ + dddy -= (x); \ + ddy -= dddy; \ + dy = -dy - ddy; \ + y += dy + +#define COPY_ONE(x) \ + SKIP_ONE(x); \ + *pDst = y >> shift; \ + pDst += dstStep + + switch (compressionmode) { + case 2: // 24 bit uncompressed + pSrc += currentframeoffset * 3; + while (copysamples) { + *pDst = get24(pSrc) >> shift; + pDst += dstStep; + pSrc += 3; + copysamples--; + } + break; + + case 3: // 24 bit compressed to 16 bit + GET_PARAMS(params); + while (currentframeoffset) { + SKIP_ONE(get16(pSrc)); + pSrc += 2; + currentframeoffset--; + } + while (copysamples) { + COPY_ONE(get16(pSrc)); + pSrc += 2; + copysamples--; + } + break; + + case 4: // 24 bit compressed to 12 bit + GET_PARAMS(params); + while (currentframeoffset > 1) { + SKIP_ONE(get12lo(pSrc)); + SKIP_ONE(get12hi(pSrc)); + pSrc += 3; + currentframeoffset -= 2; + } + if (currentframeoffset) { + SKIP_ONE(get12lo(pSrc)); + currentframeoffset--; + if (copysamples) { + COPY_ONE(get12hi(pSrc)); + pSrc += 3; + copysamples--; + } + } + while (copysamples > 1) { + COPY_ONE(get12lo(pSrc)); + COPY_ONE(get12hi(pSrc)); + pSrc += 3; + copysamples -= 2; + } + if (copysamples) { + COPY_ONE(get12lo(pSrc)); + } + break; + + case 5: // 24 bit compressed to 8 bit + GET_PARAMS(params); + while (currentframeoffset) { + SKIP_ONE(int8_t(*pSrc++)); + currentframeoffset--; + } + while (copysamples) { + COPY_ONE(int8_t(*pSrc++)); + copysamples--; + } + break; + } + } + + const int bytesPerFrame[] = { 4096, 2052, 768, 524, 396, 268 }; + const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 }; + const int headerSize[] = { 0, 4, 0, 12, 12, 12 }; + const int bitsPerSample[] = { 16, 8, 24, 16, 12, 8 }; +} + + // *************** Sample *************** // * - unsigned int Sample::Instances = 0; - void* Sample::pDecompressionBuffer = NULL; - unsigned long Sample::DecompressionBufferSize = 0; + unsigned int Sample::Instances = 0; + buffer_t Sample::InternalDecompressionBuffer; - Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { + Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { Instances++; + FileNo = fileNo; RIFF::Chunk* _3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); if (!_3gix) throw gig::Exception("Mandatory chunks in list chunk not found."); @@ -49,7 +250,7 @@ smpl->Read(&SMPTEFormat, 1, 4); SMPTEOffset = smpl->ReadInt32(); Loops = smpl->ReadInt32(); - uint32_t manufByt = smpl->ReadInt32(); + smpl->ReadInt32(); // manufByt LoopID = smpl->ReadInt32(); smpl->Read(&LoopType, 1, 4); LoopStart = smpl->ReadInt32(); @@ -63,15 +264,28 @@ RAMCache.pStart = NULL; RAMCache.NullExtensionSize = 0; - Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); + if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported"); + + RIFF::Chunk* ewav = waveList->GetSubChunk(CHUNK_ID_EWAV); + Compressed = ewav; + Dithered = false; + TruncatedBits = 0; if (Compressed) { - ScanCompressedSample(); - if (!pDecompressionBuffer) { - pDecompressionBuffer = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE]; - DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE; + uint32_t version = ewav->ReadInt32(); + if (version == 3 && BitDepth == 24) { + Dithered = ewav->ReadInt32(); + ewav->SetPos(Channels == 2 ? 84 : 64); + TruncatedBits = ewav->ReadInt32(); } + ScanCompressedSample(); } - FrameOffset = 0; // just for streaming compressed samples + + // we use a buffer for decompression and for truncating 24 bit samples to 16 bit + if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) { + InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE]; + InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE; + } + FrameOffset = 0; // just for streaming compressed samples LoopSize = LoopEnd - LoopStart; } @@ -82,30 +296,53 @@ this->SamplesTotal = 0; std::list frameOffsets; + SamplesPerFrame = BitDepth == 24 ? 256 : 2048; + WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag + // Scanning pCkData->SetPos(0); - while (pCkData->GetState() == RIFF::stream_ready) { - frameOffsets.push_back(pCkData->GetPos()); - int16_t compressionmode = pCkData->ReadInt16(); - this->SamplesTotal += 2048; - switch (compressionmode) { - case 1: // left channel compressed - case 256: // right channel compressed - pCkData->SetPos(6148, RIFF::stream_curpos); + if (Channels == 2) { // Stereo + for (int i = 0 ; ; i++) { + // for 24 bit samples every 8:th frame offset is + // stored, to save some memory + if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); + + 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]; + + if (pCkData->RemainingBytes() <= frameSize) { + SamplesInLastFrame = + ((pCkData->RemainingBytes() - headerSize[mode_l] - headerSize[mode_r]) << 3) / + (bitsPerSample[mode_l] + bitsPerSample[mode_r]); + SamplesTotal += SamplesInLastFrame; break; - case 257: // both channels compressed - pCkData->SetPos(4104, RIFF::stream_curpos); + } + SamplesTotal += SamplesPerFrame; + pCkData->SetPos(frameSize, RIFF::stream_curpos); + } + } + else { // Mono + for (int i = 0 ; ; i++) { + if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); + + const int mode = pCkData->ReadUint8(); + if (mode > 5) throw gig::Exception("Unknown compression mode"); + const unsigned long frameSize = bytesPerFrame[mode]; + + if (pCkData->RemainingBytes() <= frameSize) { + SamplesInLastFrame = + ((pCkData->RemainingBytes() - headerSize[mode]) << 3) / bitsPerSample[mode]; + SamplesTotal += SamplesInLastFrame; break; - default: // both channels uncompressed - pCkData->SetPos(8192, RIFF::stream_curpos); + } + SamplesTotal += SamplesPerFrame; + pCkData->SetPos(frameSize, RIFF::stream_curpos); } } pCkData->SetPos(0); - //FIXME: only seen compressed samples with 16 bit stereo so far - this->FrameSize = 4; - this->BitDepth = 16; - // 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()]; @@ -141,9 +378,10 @@ * that will be returned to determine the actual cached samples, but note * that the size is given in bytes! You get the number of actually cached * samples by dividing it by the frame size of the sample: - * + * @code * buffer_t buf = pSample->LoadSampleData(acquired_samples); * long cachedsamples = buf.Size / pSample->FrameSize; + * @endcode * * @param SampleCount - number of sample points to load into RAM * @returns buffer_t structure with start address and size of @@ -189,10 +427,10 @@ * that will be returned to determine the actual cached samples, but note * that the size is given in bytes! You get the number of actually cached * samples by dividing it by the frame size of the sample: - * + * @code * buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); * long cachedsamples = buf.Size / pSample->FrameSize; - * + * @endcode * The method will add \a NullSamplesCount silence samples past the * official buffer end (this won't affect the 'Size' member of the * buffer_t structure, that means 'Size' always reflects the size of the @@ -323,25 +561,29 @@ * for the next time you call this method is stored in \a pPlaybackState. * You have to allocate and initialize the playback_state_t structure by * yourself before you use it to stream a sample: - * - * - * gig::playback_state_t playbackstate;
- * playbackstate.position = 0;
- * playbackstate.reverse = false;
- * playbackstate.loop_cycles_left = pSample->LoopPlayCount;
- *
- * + * @code + * gig::playback_state_t playbackstate; + * playbackstate.position = 0; + * playbackstate.reverse = false; + * playbackstate.loop_cycles_left = pSample->LoopPlayCount; + * @endcode * You don't have to take care of things like if there is actually a loop * defined or if the current read position is located within a loop area. * The method already handles such cases by itself. * + * Caution: If you are using more than one streaming thread, you + * have to use an external decompression buffer for EACH + * streaming thread to avoid race conditions and crashes! + * * @param pBuffer destination buffer * @param SampleCount number of sample points to read * @param pPlaybackState will be used to store and reload the playback * state for the next ReadAndLoop() call + * @param pExternalDecompressionBuffer (optional) external buffer to use for decompression * @returns number of successfully read sample points + * @see CreateDecompressionBuffer() */ - unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState) { + unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer) { unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; uint8_t* pDst = (uint8_t*) pBuffer; @@ -359,7 +601,7 @@ if (!pPlaybackState->reverse) { // forward playback do { samplestoloopend = this->LoopEnd - GetPos(); - readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); samplestoread -= readsamples; totalreadsamples += readsamples; if (readsamples == samplestoloopend) { @@ -385,7 +627,7 @@ // read samples for backward playback do { - readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); samplestoreadinloop -= readsamples; samplestoread -= readsamples; totalreadsamples += readsamples; @@ -409,7 +651,7 @@ // forward playback (not entered the loop yet) if (!pPlaybackState->reverse) do { samplestoloopend = this->LoopEnd - GetPos(); - readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); samplestoread -= readsamples; totalreadsamples += readsamples; if (readsamples == samplestoloopend) { @@ -439,7 +681,7 @@ // if not endless loop check if max. number of loop cycles have been passed if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; samplestoloopend = this->LoopEnd - GetPos(); - readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend)); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); samplestoreadinloop -= readsamples; samplestoread -= readsamples; totalreadsamples += readsamples; @@ -461,7 +703,7 @@ // if not endless loop check if max. number of loop cycles have been passed if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; samplestoloopend = this->LoopEnd - GetPos(); - readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); samplestoread -= readsamples; totalreadsamples += readsamples; if (readsamples == samplestoloopend) { @@ -476,7 +718,7 @@ // read on without looping if (samplestoread) do { - readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread, pExternalDecompressionBuffer); samplestoread -= readsamples; totalreadsamples += readsamples; } while (readsamples && samplestoread); @@ -495,172 +737,251 @@ * and SetPos() if you don't want to load the sample into RAM, * thus for disk streaming. * + * Caution: If you are using more than one streaming thread, you + * have to use an external decompression buffer for EACH + * streaming thread to avoid race conditions and crashes! + * * @param pBuffer destination buffer * @param SampleCount number of sample points to read + * @param pExternalDecompressionBuffer (optional) external buffer to use for decompression * @returns number of successfully read sample points - * @see SetPos() + * @see SetPos(), CreateDecompressionBuffer() */ - unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { + unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer) { if (SampleCount == 0) return 0; - if (!Compressed) return pCkData->Read(pBuffer, SampleCount, FrameSize); //FIXME: channel inversion due to endian correction? - else { //FIXME: no support for mono compressed samples yet, are there any? + if (!Compressed) { + if (BitDepth == 24) { + // 24 bit sample. For now just truncate to 16 bit. + unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); + int16_t* pDst = static_cast(pBuffer); + if (Channels == 2) { // Stereo + unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); + pSrc++; + for (unsigned long i = readBytes ; i > 0 ; i -= 3) { + *pDst++ = get16(pSrc); + pSrc += 3; + } + return (pDst - static_cast(pBuffer)) >> 1; + } + else { // Mono + unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1); + pSrc++; + for (unsigned long i = readBytes ; i > 0 ; i -= 3) { + *pDst++ = get16(pSrc); + pSrc += 3; + } + return pDst - static_cast(pBuffer); + } + } + else { // 16 bit + // (pCkData->Read does endian correction) + return Channels == 2 ? pCkData->Read(pBuffer, SampleCount << 1, 2) >> 1 + : pCkData->Read(pBuffer, SampleCount, 2); + } + } + else { if (this->SamplePos >= this->SamplesTotal) return 0; - //TODO: efficiency: we simply assume here that all frames are compressed, maybe we should test for an average compression rate - // best case needed buffer size (all frames compressed) - unsigned long assumedsize = (SampleCount << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) - (SampleCount >> 10) + // 10 bytes header per 2048 sample points - 8194, // at least one worst case sample frame + //TODO: efficiency: maybe we should test for an average compression rate + unsigned long assumedsize = GuessSize(SampleCount), remainingbytes = 0, // remaining bytes in the local buffer remainingsamples = SampleCount, - copysamples; - int currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() + copysamples, skipsamples, + currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() this->FrameOffset = 0; - if (assumedsize > this->DecompressionBufferSize) { - // local buffer reallocation - hope this won't happen - if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer; - this->pDecompressionBuffer = new int8_t[assumedsize << 1]; // double of current needed size - this->DecompressionBufferSize = assumedsize; + buffer_t* pDecompressionBuffer = (pExternalDecompressionBuffer) ? pExternalDecompressionBuffer : &InternalDecompressionBuffer; + + // if decompression buffer too small, then reduce amount of samples to read + if (pDecompressionBuffer->Size < assumedsize) { + std::cerr << "gig::Read(): WARNING - decompression buffer size too small!" << std::endl; + SampleCount = WorstCaseMaxSamples(pDecompressionBuffer); + remainingsamples = SampleCount; + assumedsize = GuessSize(SampleCount); } - int16_t compressionmode, left, dleft, right, dright; - int8_t* pSrc = (int8_t*) this->pDecompressionBuffer; - int16_t* pDst = (int16_t*) pBuffer; + unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; + int16_t* pDst = static_cast(pBuffer); remainingbytes = pCkData->Read(pSrc, assumedsize, 1); - while (remainingsamples) { - - // reload from disk to local buffer if needed - if (remainingbytes < 8194) { - if (pCkData->GetState() != RIFF::stream_ready) { - this->SamplePos = this->SamplesTotal; - return (SampleCount - remainingsamples); + while (remainingsamples && remainingbytes) { + unsigned long framesamples = SamplesPerFrame; + unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; + + int mode_l = *pSrc++, mode_r = 0; + + if (Channels == 2) { + mode_r = *pSrc++; + framebytes = bytesPerFrame[mode_l] + bytesPerFrame[mode_r] + 2; + rightChannelOffset = bytesPerFrameNoHdr[mode_l]; + nextFrameOffset = rightChannelOffset + bytesPerFrameNoHdr[mode_r]; + if (remainingbytes < framebytes) { // last frame in sample + framesamples = SamplesInLastFrame; + if (mode_l == 4 && (framesamples & 1)) { + rightChannelOffset = ((framesamples + 1) * bitsPerSample[mode_l]) >> 3; + } + else { + rightChannelOffset = (framesamples * bitsPerSample[mode_l]) >> 3; + } + } + } + else { + framebytes = bytesPerFrame[mode_l] + 1; + nextFrameOffset = bytesPerFrameNoHdr[mode_l]; + if (remainingbytes < framebytes) { + framesamples = SamplesInLastFrame; } - assumedsize = remainingsamples; - assumedsize = (assumedsize << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) - (assumedsize >> 10) + // 10 bytes header per 2048 sample points - 8194; // at least one worst case sample frame - pCkData->SetPos(remainingbytes, RIFF::stream_backward); - if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); - remainingbytes = pCkData->Read(this->pDecompressionBuffer, assumedsize, 1); - pSrc = (int8_t*) this->pDecompressionBuffer; } // determine how many samples in this frame to skip and read - if (remainingsamples >= 2048) { - copysamples = 2048 - currentframeoffset; - remainingsamples -= copysamples; + if (currentframeoffset + remainingsamples >= framesamples) { + if (currentframeoffset <= framesamples) { + copysamples = framesamples - currentframeoffset; + skipsamples = currentframeoffset; + } + else { + copysamples = 0; + skipsamples = framesamples; + } } else { + // This frame has enough data for pBuffer, but not + // all of the frame is needed. Set file position + // to start of this frame for next call to Read. copysamples = remainingsamples; - if (currentframeoffset + copysamples > 2048) { - copysamples = 2048 - currentframeoffset; - remainingsamples -= copysamples; - } - else { + skipsamples = currentframeoffset; + pCkData->SetPos(remainingbytes, RIFF::stream_backward); + this->FrameOffset = currentframeoffset + copysamples; + } + remainingsamples -= copysamples; + + if (remainingbytes > framebytes) { + remainingbytes -= framebytes; + if (remainingsamples == 0 && + currentframeoffset + copysamples == framesamples) { + // This frame has enough data for pBuffer, and + // all of the frame is needed. Set file + // position to start of next frame for next + // call to Read. FrameOffset is 0. pCkData->SetPos(remainingbytes, RIFF::stream_backward); - remainingsamples = 0; - this->FrameOffset = currentframeoffset + copysamples; } } + else remainingbytes = 0; - // decompress and copy current frame from local buffer to destination buffer - compressionmode = *(int16_t*)pSrc; pSrc+=2; - switch (compressionmode) { - case 1: // left channel compressed - remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header) - if (!remainingsamples && copysamples == 2048) - pCkData->SetPos(remainingbytes, RIFF::stream_backward); - - left = *(int16_t*)pSrc; pSrc+=2; - dleft = *(int16_t*)pSrc; pSrc+=2; - while (currentframeoffset) { - dleft -= *pSrc; - left -= dleft; - pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit) - currentframeoffset--; - } - while (copysamples) { - dleft -= *pSrc; pSrc++; - left -= dleft; - *pDst = left; pDst++; - *pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; - copysamples--; - } - break; - case 256: // right channel compressed - remainingbytes -= 6150; // (left 16 bit, right 8 bit, +6 byte header) - if (!remainingsamples && copysamples == 2048) - pCkData->SetPos(remainingbytes, RIFF::stream_backward); - - right = *(int16_t*)pSrc; pSrc+=2; - dright = *(int16_t*)pSrc; pSrc+=2; - if (currentframeoffset) { - pSrc+=2; // skip uncompressed left channel, now we can increment by 3 - while (currentframeoffset) { - dright -= *pSrc; - right -= dright; - pSrc+=3; // 8 bit right channel, skip uncompressed left channel (16 bit) - currentframeoffset--; - } - pSrc-=2; // back aligned to left channel + currentframeoffset -= skipsamples; + + if (copysamples == 0) { + // skip this frame + pSrc += framebytes - Channels; + } + else { + const unsigned char* const param_l = pSrc; + if (BitDepth == 24) { + if (mode_l != 2) pSrc += 12; + + if (Channels == 2) { // Stereo + const unsigned char* const param_r = pSrc; + if (mode_r != 2) pSrc += 12; + + Decompress24(mode_l, param_l, 2, pSrc, pDst, + skipsamples, copysamples, TruncatedBits); + Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, + skipsamples, copysamples, TruncatedBits); + pDst += copysamples << 1; } - while (copysamples) { - *pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; - dright -= *pSrc; pSrc++; - right -= dright; - *pDst = right; pDst++; - copysamples--; + else { // Mono + Decompress24(mode_l, param_l, 1, pSrc, pDst, + skipsamples, copysamples, TruncatedBits); + pDst += copysamples; } - break; - case 257: // both channels compressed - remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header) - if (!remainingsamples && copysamples == 2048) - pCkData->SetPos(remainingbytes, RIFF::stream_backward); - - left = *(int16_t*)pSrc; pSrc+=2; - dleft = *(int16_t*)pSrc; pSrc+=2; - right = *(int16_t*)pSrc; pSrc+=2; - dright = *(int16_t*)pSrc; pSrc+=2; - while (currentframeoffset) { - dleft -= *pSrc; pSrc++; - left -= dleft; - dright -= *pSrc; pSrc++; - right -= dright; - currentframeoffset--; + } + else { // 16 bit + if (mode_l) pSrc += 4; + + int step; + if (Channels == 2) { // Stereo + const unsigned char* const param_r = pSrc; + if (mode_r) pSrc += 4; + + step = (2 - mode_l) + (2 - mode_r); + Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples); + Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1, + skipsamples, copysamples); + pDst += copysamples << 1; } - while (copysamples) { - dleft -= *pSrc; pSrc++; - left -= dleft; - dright -= *pSrc; pSrc++; - right -= dright; - *pDst = left; pDst++; - *pDst = right; pDst++; - copysamples--; + else { // Mono + step = 2 - mode_l; + Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples); + pDst += copysamples; } - break; - default: // both channels uncompressed - remainingbytes -= 8194; // (left 16 bit, right 16 bit, +2 byte header) - if (!remainingsamples && copysamples == 2048) - pCkData->SetPos(remainingbytes, RIFF::stream_backward); - - pSrc += currentframeoffset << 2; - currentframeoffset = 0; - memcpy(pDst, pSrc, copysamples << 2); - pDst += copysamples << 1; - pSrc += copysamples << 2; - break; + } + pSrc += nextFrameOffset; } - } + + // reload from disk to local buffer if needed + if (remainingsamples && remainingbytes < WorstCaseFrameSize && pCkData->GetState() == RIFF::stream_ready) { + assumedsize = GuessSize(remainingsamples); + pCkData->SetPos(remainingbytes, RIFF::stream_backward); + if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); + remainingbytes = pCkData->Read(pDecompressionBuffer->pStart, assumedsize, 1); + pSrc = (unsigned char*) pDecompressionBuffer->pStart; + } + } // while + this->SamplePos += (SampleCount - remainingsamples); if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; return (SampleCount - remainingsamples); } } + /** + * Allocates a decompression buffer for streaming (compressed) samples + * with Sample::Read(). If you are using more than one streaming thread + * in your application you HAVE to create a decompression buffer + * for EACH of your streaming threads and provide it with the + * Sample::Read() call in order to avoid race conditions and crashes. + * + * You should free the memory occupied by the allocated buffer(s) once + * you don't need one of your streaming threads anymore by calling + * DestroyDecompressionBuffer(). + * + * @param MaxReadSize - the maximum size (in sample points) you ever + * expect to read with one Read() call + * @returns allocated decompression buffer + * @see DestroyDecompressionBuffer() + */ + buffer_t Sample::CreateDecompressionBuffer(unsigned long 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.pStart = new int8_t[result.Size]; + result.NullExtensionSize = 0; + return result; + } + + /** + * Free decompression buffer, previously created with + * CreateDecompressionBuffer(). + * + * @param DecompressionBuffer - previously allocated decompression + * buffer to free + */ + void Sample::DestroyDecompressionBuffer(buffer_t& DecompressionBuffer) { + if (DecompressionBuffer.Size && DecompressionBuffer.pStart) { + delete[] (int8_t*) DecompressionBuffer.pStart; + DecompressionBuffer.pStart = NULL; + DecompressionBuffer.Size = 0; + DecompressionBuffer.NullExtensionSize = 0; + } + } + Sample::~Sample() { Instances--; - if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer; + if (!Instances && InternalDecompressionBuffer.Size) { + delete[] (unsigned char*) InternalDecompressionBuffer.pStart; + InternalDecompressionBuffer.pStart = NULL; + InternalDecompressionBuffer.Size = 0; + } if (FrameTable) delete[] FrameTable; if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; } @@ -680,7 +1001,7 @@ if (!pVelocityTables) pVelocityTables = new VelocityTableMap; RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); - _3ewa->ReadInt32(); // unknown, allways 0x0000008C ? + _3ewa->ReadInt32(); // unknown, always 0x0000008C ? LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); _3ewa->ReadInt16(); // unknown @@ -774,17 +1095,13 @@ else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; else DimensionBypass = dim_bypass_ctrl_none; uint8_t pan = _3ewa->ReadUint8(); - Pan = (pan < 64) ? pan : (-1) * (int8_t)pan - 63; + Pan = (pan < 64) ? pan : -((int)pan - 63); // signed 7 bit -> signed 8 bit SelfMask = _3ewa->ReadInt8() & 0x01; _3ewa->ReadInt8(); // unknown uint8_t lfo3ctrl = _3ewa->ReadUint8(); LFO3Controller = static_cast(lfo3ctrl & 0x07); // lower 3 bits LFO3Sync = lfo3ctrl & 0x20; // bit 5 InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 - if (VCFType == vcf_type_lowpass) { - if (lfo3ctrl & 0x40) // bit 6 - VCFType = vcf_type_lowpassturbo; - } AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); uint8_t lfo2ctrl = _3ewa->ReadUint8(); LFO2Controller = static_cast(lfo2ctrl & 0x07); // lower 3 bits @@ -817,7 +1134,9 @@ VCFEnabled = vcfcutoff & 0x80; // bit 7 VCFCutoff = vcfcutoff & 0x7f; // lower 7 bits VCFCutoffController = static_cast(_3ewa->ReadUint8()); - VCFVelocityScale = _3ewa->ReadUint8(); + uint8_t vcfvelscale = _3ewa->ReadUint8(); + VCFCutoffControllerInvert = vcfvelscale & 0x80; // bit 7 + VCFVelocityScale = vcfvelscale & 0x7f; // lower 7 bits _3ewa->ReadInt8(); // unknown uint8_t vcfresonance = _3ewa->ReadUint8(); VCFResonance = vcfresonance & 0x7f; // lower 7 bits @@ -829,47 +1148,60 @@ VCFVelocityDynamicRange = vcfvelocity % 5; VCFVelocityCurve = static_cast(vcfvelocity / 5); VCFType = static_cast(_3ewa->ReadUint8()); + if (VCFType == vcf_type_lowpass) { + if (lfo3ctrl & 0x40) // bit 6 + VCFType = vcf_type_lowpassturbo; + } - // get the corresponding velocity->volume table from the table map or create & calculate that table if it doesn't exist yet - uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; + pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, + VelocityResponseDepth, + VelocityResponseCurveScaling); + + curve_type_t curveType = ReleaseVelocityResponseCurve; + uint8_t depth = ReleaseVelocityResponseDepth; + + // this models a strange behaviour or bug in GSt: two of the + // velocity response curves for release time are not used even + // if specified, instead another curve is chosen. + if ((curveType == curve_type_nonlinear && depth == 0) || + (curveType == curve_type_special && depth == 4)) { + curveType = curve_type_nonlinear; + depth = 3; + } + pVelocityReleaseTable = GetVelocityTable(curveType, depth, 0); + + curveType = VCFVelocityCurve; + depth = VCFVelocityDynamicRange; + + // even stranger GSt: two of the velocity response curves for + // filter cutoff are not used, instead another special curve + // is chosen. This curve is not used anywhere else. + if ((curveType == curve_type_nonlinear && depth == 0) || + (curveType == curve_type_special && depth == 4)) { + curveType = curve_type_special; + depth = 5; + } + pVelocityCutoffTable = GetVelocityTable(curveType, depth, + VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0); + + SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); + } + + // 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) + { + double* table; + uint32_t tableKey = (curveType<<16) | (depth<<8) | scaling; if (pVelocityTables->count(tableKey)) { // if key exists - pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; + table = (*pVelocityTables)[tableKey]; } else { - pVelocityAttenuationTable = new double[128]; - switch (VelocityResponseCurve) { // calculate the new table - case curve_type_nonlinear: - for (int velocity = 0; velocity < 128; velocity++) { - pVelocityAttenuationTable[velocity] = - GIG_VELOCITY_TRANSFORM_NONLINEAR((double)(velocity+1),(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); - if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; - else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; - } - break; - case curve_type_linear: - for (int velocity = 0; velocity < 128; velocity++) { - pVelocityAttenuationTable[velocity] = - GIG_VELOCITY_TRANSFORM_LINEAR((double)velocity,(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); - if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; - else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; - } - break; - case curve_type_special: - for (int velocity = 0; velocity < 128; velocity++) { - pVelocityAttenuationTable[velocity] = - GIG_VELOCITY_TRANSFORM_SPECIAL((double)(velocity+1),(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); - if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; - else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; - } - break; - case curve_type_unknown: - default: - throw gig::Exception("Unknown transform curve type."); - } - (*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map + table = CreateVelocityTable(curveType, depth, scaling); + (*pVelocityTables)[tableKey] = table; // put the new table into the tables map } + return table; } - + leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { leverage_ctrl_t decodedcontroller; switch (EncodedController) { @@ -886,7 +1218,7 @@ decodedcontroller.type = leverage_ctrl_t::type_channelaftertouch; decodedcontroller.controller_number = 0; break; - + // ordinary MIDI control change controller case _lev_ctrl_modwheel: decodedcontroller.type = leverage_ctrl_t::type_controlchange; @@ -980,7 +1312,7 @@ decodedcontroller.type = leverage_ctrl_t::type_controlchange; decodedcontroller.controller_number = 95; break; - + // unknown controller type default: throw gig::Exception("Unknown leverage controller type."); @@ -1018,6 +1350,80 @@ return pVelocityAttenuationTable[MIDIKeyVelocity]; } + double DimensionRegion::GetVelocityRelease(uint8_t MIDIKeyVelocity) { + return pVelocityReleaseTable[MIDIKeyVelocity]; + } + + double DimensionRegion::GetVelocityCutoff(uint8_t MIDIKeyVelocity) { + return pVelocityCutoffTable[MIDIKeyVelocity]; + } + + double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { + + // line-segment approximations of the 15 velocity curves + + // linear + const int lin0[] = { 1, 1, 127, 127 }; + const int lin1[] = { 1, 21, 127, 127 }; + const int lin2[] = { 1, 45, 127, 127 }; + const int lin3[] = { 1, 74, 127, 127 }; + const int lin4[] = { 1, 127, 127, 127 }; + + // non-linear + const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 }; + const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127, + 127, 127 }; + const int non2[] = { 1, 4, 46, 9, 57, 20, 102, 107, 107, 127, + 127, 127 }; + const int non3[] = { 1, 15, 10, 19, 67, 73, 80, 80, 90, 98, 98, 127, + 127, 127 }; + const int non4[] = { 1, 25, 33, 57, 82, 81, 92, 127, 127, 127 }; + + // special + const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44, + 113, 127, 127, 127 }; + const int spe1[] = { 1, 2, 27, 5, 67, 18, 89, 29, 95, 35, 107, 67, + 118, 127, 127, 127 }; + const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74, + 85, 90, 91, 127, 127, 127 }; + const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73, + 117, 127, 127, 127 }; + const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, + 127, 127 }; + + // this is only used by the VCF velocity curve + const int spe5[] = { 1, 2, 30, 5, 60, 19, 77, 70, 83, 85, 88, 106, + 91, 127, 127, 127 }; + + const int* const curves[] = { non0, non1, non2, non3, non4, + lin0, lin1, lin2, lin3, lin4, + spe0, spe1, spe2, spe3, spe4, spe5 }; + + double* const table = new double[128]; + + const int* curve = curves[curveType * 5 + depth]; + const int s = scaling == 0 ? 20 : scaling; // 0 or 20 means no scaling + + table[0] = 0; + for (int x = 1 ; x < 128 ; x++) { + + if (x > curve[2]) curve += 2; + double y = curve[1] + (x - curve[0]) * + (double(curve[3] - curve[1]) / (curve[2] - curve[0])); + y = y / 127; + + // Scale up for s > 20, down for s < 20. When + // down-scaling, the curve still ends at 1.0. + if (s < 20 && y >= 0.5) + y = y / ((2 - 40.0 / s) * y + 40.0 / s - 1); + else + y = y * (s / 20.0); + if (y > 1) y = 1; + + table[x] = y; + } + return table; + } // *************** Region *************** @@ -1026,9 +1432,12 @@ Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) { // Initialization Dimensions = 0; - for (int i = 0; i < 32; i++) { + for (int i = 0; i < 256; i++) { pDimensionRegions[i] = NULL; } + Layers = 1; + File* file = (File*) GetParent()->GetParent(); + int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; // Actual Loading @@ -1037,7 +1446,7 @@ RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); if (_3lnk) { DimensionRegions = _3lnk->ReadUint32(); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < dimensionBits; i++) { dimension_t dimension = static_cast(_3lnk->ReadUint8()); uint8_t bits = _3lnk->ReadUint8(); if (dimension == dimension_none) { // inactive dimension @@ -1053,13 +1462,19 @@ pDimensionDefinitions[i].bits = bits; pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) pDimensionDefinitions[i].split_type = (dimension == dimension_layer || - dimension == dimension_samplechannel) ? split_type_bit - : split_type_normal; + dimension == dimension_samplechannel || + dimension == dimension_releasetrigger || + dimension == dimension_roundrobin || + dimension == dimension_random) ? split_type_bit + : split_type_normal; pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point pDimensionDefinitions[i].zone_size = (pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones : 0; Dimensions++; + + // if this is a layer dimension, remember the amount of layers + if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; } _3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition } @@ -1076,11 +1491,11 @@ else { // custom defined ranges pDimDef->split_type = split_type_customvelocity; pDimDef->ranges = new range_t[pDimDef->zones]; - unsigned int bits[5] = {0,0,0,0,0}; + uint8_t bits[8] = { 0 }; int previousUpperLimit = -1; for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { bits[i] = velocityZone; - DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); + DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; @@ -1094,8 +1509,14 @@ } } + // jump to start of the wave pool indices (if not already there) + File* file = (File*) GetParent()->GetParent(); + if (file->pVersion && file->pVersion->major == 3) + _3lnk->SetPos(68); // version 3 has a different 3lnk structure + else + _3lnk->SetPos(44); + // load sample references - _3lnk->SetPos(44); // jump to start of the wave pool indices (if not already there) for (uint i = 0; i < DimensionRegions; i++) { uint32_t wavepoolindex = _3lnk->ReadUint32(); pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); @@ -1124,7 +1545,7 @@ for (uint i = 0; i < Dimensions; i++) { if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; } - for (int i = 0; i < 32; i++) { + for (int i = 0; i < 256; i++) { if (pDimensionRegions[i]) delete pDimensionRegions[i]; } } @@ -1142,18 +1563,15 @@ * left channel, 1 for right channel or 0 for layer 0, 1 for layer 1, * etc.). * - * @param Dim4Val MIDI controller value (0-127) for dimension 4 - * @param Dim3Val MIDI controller value (0-127) for dimension 3 - * @param Dim2Val MIDI controller value (0-127) for dimension 2 - * @param Dim1Val MIDI controller value (0-127) for dimension 1 - * @param Dim0Val MIDI controller value (0-127) for dimension 0 + * @param DimValues MIDI controller values (0-127) for dimension 0 to 7 * @returns adress to the DimensionRegion for the given situation * @see pDimensionDefinitions * @see Dimensions */ - DimensionRegion* Region::GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val) { - unsigned int bits[5] = {Dim0Val,Dim1Val,Dim2Val,Dim3Val,Dim4Val}; + DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { + uint8_t bits[8] = { 0 }; for (uint i = 0; i < Dimensions; i++) { + bits[i] = DimValues[i]; switch (pDimensionDefinitions[i].split_type) { case split_type_normal: bits[i] /= pDimensionDefinitions[i].zone_size; @@ -1161,10 +1579,13 @@ case split_type_customvelocity: bits[i] = VelocityTable[bits[i]]; break; - // else the value is already the sought dimension bit number + case split_type_bit: // the value is already the sought dimension bit number + const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; + bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed + break; } } - return GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); + return GetDimensionRegionByBit(bits); } /** @@ -1172,20 +1593,19 @@ * numbers (zone index). You usually use GetDimensionRegionByValue * instead of calling this method directly! * - * @param Dim4Bit Bit number for dimension 4 - * @param Dim3Bit Bit number for dimension 3 - * @param Dim2Bit Bit number for dimension 2 - * @param Dim1Bit Bit number for dimension 1 - * @param Dim0Bit Bit number for dimension 0 + * @param DimBits Bit numbers for dimension 0 to 7 * @returns adress to the DimensionRegion for the given dimension * bit numbers * @see GetDimensionRegionByValue() */ - DimensionRegion* Region::GetDimensionRegionByBit(uint8_t Dim4Bit, uint8_t Dim3Bit, uint8_t Dim2Bit, uint8_t Dim1Bit, uint8_t Dim0Bit) { - return *(pDimensionRegions + ((((((((Dim4Bit << pDimensionDefinitions[3].bits) | Dim3Bit) - << pDimensionDefinitions[2].bits) | Dim2Bit) - << pDimensionDefinitions[1].bits) | Dim1Bit) - << pDimensionDefinitions[0].bits) | Dim0Bit) ); + DimensionRegion* Region::GetDimensionRegionByBit(const uint8_t DimBits[8]) { + return pDimensionRegions[((((((DimBits[7] << pDimensionDefinitions[6].bits | DimBits[6]) + << pDimensionDefinitions[5].bits | DimBits[5]) + << pDimensionDefinitions[4].bits | DimBits[4]) + << pDimensionDefinitions[3].bits | DimBits[3]) + << pDimensionDefinitions[2].bits | DimBits[2]) + << pDimensionDefinitions[1].bits | DimBits[1]) + << pDimensionDefinitions[0].bits | DimBits[0]]; } /** @@ -1202,12 +1622,15 @@ else return static_cast(pSample = GetSampleFromWavePool(WavePoolTableIndex)); } - Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { + Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) { + if ((int32_t)WavePoolTableIndex == -1) return NULL; File* file = (File*) GetParent()->GetParent(); unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; - Sample* sample = file->GetFirstSample(); + unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; + Sample* sample = file->GetFirstSample(pProgress); while (sample) { - if (sample->ulWavePoolOffset == soughtoffset) return static_cast(pSample = sample); + if (sample->ulWavePoolOffset == soughtoffset && + sample->FileNo == soughtfileno) return static_cast(pSample = sample); sample = file->GetNextSample(); } return NULL; @@ -1218,7 +1641,7 @@ // *************** Instrument *************** // * - Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) { + Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) { // Initialization for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; RegionIndex = -1; @@ -1244,10 +1667,12 @@ RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); if (!lrgn) throw gig::Exception("Mandatory chunks in chunk not found."); pRegions = new Region*[Regions]; + for (uint i = 0; i < Regions; i++) pRegions[i] = NULL; RIFF::List* rgn = lrgn->GetFirstSubList(); unsigned int iRegion = 0; while (rgn) { if (rgn->GetListType() == LIST_TYPE_RGN) { + __notify_progress(pProgress, (float) iRegion / (float) Regions); pRegions[iRegion] = new Region(this, rgn); iRegion++; } @@ -1260,6 +1685,8 @@ RegionKeyTable[iKey] = pRegions[iReg]; } } + + __notify_progress(pProgress, 1.0f); // notify done } Instrument::~Instrument() { @@ -1267,8 +1694,8 @@ if (pRegions) { if (pRegions[i]) delete (pRegions[i]); } - delete[] pRegions; } + if (pRegions) delete[] pRegions; } /** @@ -1310,7 +1737,7 @@ * @see GetFirstRegion() */ Region* Instrument::GetNextRegion() { - if (RegionIndex < 0 || RegionIndex >= Regions) return NULL; + if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL; return pRegions[RegionIndex++]; } @@ -1324,8 +1751,35 @@ pInstruments = NULL; } - Sample* File::GetFirstSample() { - if (!pSamples) LoadSamples(); + File::~File() { + // free samples + if (pSamples) { + SamplesIterator = pSamples->begin(); + while (SamplesIterator != pSamples->end() ) { + delete (*SamplesIterator); + SamplesIterator++; + } + pSamples->clear(); + delete pSamples; + + } + // free instruments + if (pInstruments) { + InstrumentsIterator = pInstruments->begin(); + while (InstrumentsIterator != pInstruments->end() ) { + delete (*InstrumentsIterator); + InstrumentsIterator++; + } + pInstruments->clear(); + delete pInstruments; + } + // free extension files + for (std::list::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) + delete *i; + } + + Sample* File::GetFirstSample(progress_t* pProgress) { + if (!pSamples) LoadSamples(pProgress); if (!pSamples) return NULL; SamplesIterator = pSamples->begin(); return static_cast( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); @@ -1337,21 +1791,56 @@ return static_cast( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); } - void File::LoadSamples() { - RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); - if (wvpl) { - unsigned long wvplFileOffset = wvpl->GetFilePos(); - RIFF::List* wave = wvpl->GetFirstSubList(); - while (wave) { - if (wave->GetListType() == LIST_TYPE_WAVE) { - if (!pSamples) pSamples = new SampleList; - unsigned long waveFileOffset = wave->GetFilePos(); - pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); + void File::LoadSamples(progress_t* pProgress) { + RIFF::File* file = pRIFF; + + // just for progress calculation + int iSampleIndex = 0; + int iTotalSamples = WavePoolCount; + + // check if samples should be loaded from extension files + int lastFileNo = 0; + for (int i = 0 ; i < WavePoolCount ; i++) { + if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; + } + String name(pRIFF->Filename); + int nameLen = pRIFF->Filename.length(); + char suffix[6]; + if (nameLen > 4 && pRIFF->Filename.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(); + RIFF::List* wave = wvpl->GetFirstSubList(); + while (wave) { + if (wave->GetListType() == LIST_TYPE_WAVE) { + // notify current progress + const float subprogress = (float) iSampleIndex / (float) iTotalSamples; + __notify_progress(pProgress, subprogress); + + if (!pSamples) pSamples = new SampleList; + unsigned long waveFileOffset = wave->GetFilePos(); + pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); + + iSampleIndex++; + } + wave = wvpl->GetNextSubList(); } - wave = wvpl->GetNextSubList(); + + if (fileNo == lastFileNo) break; + + // open extension file (*.gx01, *.gx02, ...) + fileNo++; + sprintf(suffix, ".gx%02d", fileNo); + name.replace(nameLen, 5, suffix); + file = new RIFF::File(name); + ExtensionFiles.push_back(file); } + else throw gig::Exception("Mandatory chunk not found."); } - else throw gig::Exception("Mandatory chunk not found."); + + __notify_progress(pProgress, 1.0); // notify done } Instrument* File::GetFirstInstrument() { @@ -1370,10 +1859,30 @@ /** * Returns the instrument with the given index. * + * @param index - number of the sought instrument (0..n) + * @param pProgress - optional: callback function for progress notification * @returns sought instrument or NULL if there's no such instrument */ - Instrument* File::GetInstrument(uint index) { - if (!pInstruments) LoadInstruments(); + Instrument* File::GetInstrument(uint index, progress_t* pProgress) { + if (!pInstruments) { + // TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM) + + // sample loading subtask + progress_t subprogress; + __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask + __notify_progress(&subprogress, 0.0f); + GetFirstSample(&subprogress); // now force all samples to be loaded + __notify_progress(&subprogress, 1.0f); + + // instrument loading subtask + if (pProgress && pProgress->callback) { + subprogress.__range_min = subprogress.__range_max; + subprogress.__range_max = pProgress->__range_max; // schedule remaining percentage for this subtask + } + __notify_progress(&subprogress, 0.0f); + LoadInstruments(&subprogress); + __notify_progress(&subprogress, 1.0f); + } if (!pInstruments) return NULL; InstrumentsIterator = pInstruments->begin(); for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { @@ -1383,17 +1892,29 @@ return NULL; } - void File::LoadInstruments() { + void File::LoadInstruments(progress_t* pProgress) { RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); if (lstInstruments) { + int iInstrumentIndex = 0; RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); while (lstInstr) { if (lstInstr->GetListType() == LIST_TYPE_INS) { + // notify current progress + const float localProgress = (float) iInstrumentIndex / (float) Instruments; + __notify_progress(pProgress, localProgress); + + // divide local progress into subprogress for loading current Instrument + progress_t subprogress; + __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex); + if (!pInstruments) pInstruments = new InstrumentList; - pInstruments->push_back(new Instrument(this, lstInstr)); + pInstruments->push_back(new Instrument(this, lstInstr, &subprogress)); + + iInstrumentIndex++; } lstInstr = lstInstruments->GetNextSubList(); } + __notify_progress(pProgress, 1.0); // notify done } else throw gig::Exception("Mandatory list chunk not found."); } @@ -1410,4 +1931,25 @@ std::cout << "gig::Exception: " << Message << std::endl; } + +// *************** functions *************** +// * + + /** + * Returns the name of this C++ library. This is usually "libgig" of + * course. This call is equivalent to RIFF::libraryName() and + * DLS::libraryName(). + */ + String libraryName() { + return PACKAGE; + } + + /** + * Returns version of this C++ library. This call is equivalent to + * RIFF::libraryVersion() and DLS::libraryVersion(). + */ + String libraryVersion() { + return VERSION; + } + } // namespace gig