00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "gig.h"
00025
00026 #include "helper.h"
00027
00028 #include <algorithm>
00029 #include <math.h>
00030 #include <iostream>
00031
00037 #define INITIAL_SAMPLE_BUFFER_SIZE 512000 // 512 kB
00038
00040 #define GIG_EXP_DECODE(x) (pow(1.000000008813822, x))
00041 #define GIG_EXP_ENCODE(x) (log(x) / log(1.000000008813822))
00042 #define GIG_PITCH_TRACK_EXTRACT(x) (!(x & 0x01))
00043 #define GIG_PITCH_TRACK_ENCODE(x) ((x) ? 0x00 : 0x01)
00044 #define GIG_VCF_RESONANCE_CTRL_EXTRACT(x) ((x >> 4) & 0x03)
00045 #define GIG_VCF_RESONANCE_CTRL_ENCODE(x) ((x & 0x03) << 4)
00046 #define GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(x) ((x >> 1) & 0x03)
00047 #define GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(x) ((x >> 3) & 0x03)
00048 #define GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(x) ((x >> 5) & 0x03)
00049 #define GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(x) ((x & 0x03) << 1)
00050 #define GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(x) ((x & 0x03) << 3)
00051 #define GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(x) ((x & 0x03) << 5)
00052
00053 namespace gig {
00054
00055
00056
00057
00058 progress_t::progress_t() {
00059 callback = NULL;
00060 custom = NULL;
00061 __range_min = 0.0f;
00062 __range_max = 1.0f;
00063 }
00064
00065
00066 static void __notify_progress(progress_t* pProgress, float subprogress) {
00067 if (pProgress && pProgress->callback) {
00068 const float totalrange = pProgress->__range_max - pProgress->__range_min;
00069 const float totalprogress = pProgress->__range_min + subprogress * totalrange;
00070 pProgress->factor = totalprogress;
00071 pProgress->callback(pProgress);
00072 }
00073 }
00074
00075
00076 static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress, float totalTasks, float currentTask) {
00077 if (pParentProgress && pParentProgress->callback) {
00078 const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min;
00079 pSubProgress->callback = pParentProgress->callback;
00080 pSubProgress->custom = pParentProgress->custom;
00081 pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
00082 pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
00083 }
00084 }
00085
00086
00087
00088
00089
00090 namespace {
00091
00092 inline int get12lo(const unsigned char* pSrc)
00093 {
00094 const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8;
00095 return x & 0x800 ? x - 0x1000 : x;
00096 }
00097
00098 inline int get12hi(const unsigned char* pSrc)
00099 {
00100 const int x = pSrc[1] >> 4 | pSrc[2] << 4;
00101 return x & 0x800 ? x - 0x1000 : x;
00102 }
00103
00104 inline int16_t get16(const unsigned char* pSrc)
00105 {
00106 return int16_t(pSrc[0] | pSrc[1] << 8);
00107 }
00108
00109 inline int get24(const unsigned char* pSrc)
00110 {
00111 const int x = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16;
00112 return x & 0x800000 ? x - 0x1000000 : x;
00113 }
00114
00115 inline void store24(unsigned char* pDst, int x)
00116 {
00117 pDst[0] = x;
00118 pDst[1] = x >> 8;
00119 pDst[2] = x >> 16;
00120 }
00121
00122 void Decompress16(int compressionmode, const unsigned char* params,
00123 int srcStep, int dstStep,
00124 const unsigned char* pSrc, int16_t* pDst,
00125 unsigned long currentframeoffset,
00126 unsigned long copysamples)
00127 {
00128 switch (compressionmode) {
00129 case 0:
00130 pSrc += currentframeoffset * srcStep;
00131 while (copysamples) {
00132 *pDst = get16(pSrc);
00133 pDst += dstStep;
00134 pSrc += srcStep;
00135 copysamples--;
00136 }
00137 break;
00138
00139 case 1:
00140 int y = get16(params);
00141 int dy = get16(params + 2);
00142 while (currentframeoffset) {
00143 dy -= int8_t(*pSrc);
00144 y -= dy;
00145 pSrc += srcStep;
00146 currentframeoffset--;
00147 }
00148 while (copysamples) {
00149 dy -= int8_t(*pSrc);
00150 y -= dy;
00151 *pDst = y;
00152 pDst += dstStep;
00153 pSrc += srcStep;
00154 copysamples--;
00155 }
00156 break;
00157 }
00158 }
00159
00160 void Decompress24(int compressionmode, const unsigned char* params,
00161 int dstStep, const unsigned char* pSrc, uint8_t* pDst,
00162 unsigned long currentframeoffset,
00163 unsigned long copysamples, int truncatedBits)
00164 {
00165 int y, dy, ddy, dddy;
00166
00167 #define GET_PARAMS(params) \
00168 y = get24(params); \
00169 dy = y - get24((params) + 3); \
00170 ddy = get24((params) + 6); \
00171 dddy = get24((params) + 9)
00172
00173 #define SKIP_ONE(x) \
00174 dddy -= (x); \
00175 ddy -= dddy; \
00176 dy = -dy - ddy; \
00177 y += dy
00178
00179 #define COPY_ONE(x) \
00180 SKIP_ONE(x); \
00181 store24(pDst, y << truncatedBits); \
00182 pDst += dstStep
00183
00184 switch (compressionmode) {
00185 case 2:
00186 pSrc += currentframeoffset * 3;
00187 while (copysamples) {
00188 store24(pDst, get24(pSrc) << truncatedBits);
00189 pDst += dstStep;
00190 pSrc += 3;
00191 copysamples--;
00192 }
00193 break;
00194
00195 case 3:
00196 GET_PARAMS(params);
00197 while (currentframeoffset) {
00198 SKIP_ONE(get16(pSrc));
00199 pSrc += 2;
00200 currentframeoffset--;
00201 }
00202 while (copysamples) {
00203 COPY_ONE(get16(pSrc));
00204 pSrc += 2;
00205 copysamples--;
00206 }
00207 break;
00208
00209 case 4:
00210 GET_PARAMS(params);
00211 while (currentframeoffset > 1) {
00212 SKIP_ONE(get12lo(pSrc));
00213 SKIP_ONE(get12hi(pSrc));
00214 pSrc += 3;
00215 currentframeoffset -= 2;
00216 }
00217 if (currentframeoffset) {
00218 SKIP_ONE(get12lo(pSrc));
00219 currentframeoffset--;
00220 if (copysamples) {
00221 COPY_ONE(get12hi(pSrc));
00222 pSrc += 3;
00223 copysamples--;
00224 }
00225 }
00226 while (copysamples > 1) {
00227 COPY_ONE(get12lo(pSrc));
00228 COPY_ONE(get12hi(pSrc));
00229 pSrc += 3;
00230 copysamples -= 2;
00231 }
00232 if (copysamples) {
00233 COPY_ONE(get12lo(pSrc));
00234 }
00235 break;
00236
00237 case 5:
00238 GET_PARAMS(params);
00239 while (currentframeoffset) {
00240 SKIP_ONE(int8_t(*pSrc++));
00241 currentframeoffset--;
00242 }
00243 while (copysamples) {
00244 COPY_ONE(int8_t(*pSrc++));
00245 copysamples--;
00246 }
00247 break;
00248 }
00249 }
00250
00251 const int bytesPerFrame[] = { 4096, 2052, 768, 524, 396, 268 };
00252 const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 };
00253 const int headerSize[] = { 0, 4, 0, 12, 12, 12 };
00254 const int bitsPerSample[] = { 16, 8, 24, 16, 12, 8 };
00255 }
00256
00257
00258
00259
00260
00261
00262 static uint32_t* __initCRCTable() {
00263 static uint32_t res[256];
00264
00265 for (int i = 0 ; i < 256 ; i++) {
00266 uint32_t c = i;
00267 for (int j = 0 ; j < 8 ; j++) {
00268 c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1;
00269 }
00270 res[i] = c;
00271 }
00272 return res;
00273 }
00274
00275 static const uint32_t* __CRCTable = __initCRCTable();
00276
00282 inline static void __resetCRC(uint32_t& crc) {
00283 crc = 0xffffffff;
00284 }
00285
00305 static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) {
00306 for (int i = 0 ; i < bufSize ; i++) {
00307 crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);
00308 }
00309 }
00310
00316 inline static uint32_t __encodeCRC(const uint32_t& crc) {
00317 return crc ^ 0xffffffff;
00318 }
00319
00320
00321
00322
00323
00324
00325 static split_type_t __resolveSplitType(dimension_t dimension) {
00326 return (
00327 dimension == dimension_layer ||
00328 dimension == dimension_samplechannel ||
00329 dimension == dimension_releasetrigger ||
00330 dimension == dimension_keyboard ||
00331 dimension == dimension_roundrobin ||
00332 dimension == dimension_random ||
00333 dimension == dimension_smartmidi ||
00334 dimension == dimension_roundrobinkeyboard
00335 ) ? split_type_bit : split_type_normal;
00336 }
00337
00338 static int __resolveZoneSize(dimension_def_t& dimension_definition) {
00339 return (dimension_definition.split_type == split_type_normal)
00340 ? int(128.0 / dimension_definition.zones) : 0;
00341 }
00342
00343
00344
00345
00346
00347
00348 unsigned int Sample::Instances = 0;
00349 buffer_t Sample::InternalDecompressionBuffer;
00350
00369 Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {
00370 static const DLS::Info::string_length_t fixedStringLengths[] = {
00371 { CHUNK_ID_INAM, 64 },
00372 { 0, 0 }
00373 };
00374 pInfo->SetFixedStringLengths(fixedStringLengths);
00375 Instances++;
00376 FileNo = fileNo;
00377
00378 __resetCRC(crc);
00379
00380 pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);
00381 if (pCk3gix) {
00382 uint16_t iSampleGroup = pCk3gix->ReadInt16();
00383 pGroup = pFile->GetGroup(iSampleGroup);
00384 } else {
00385
00386 pGroup = pFile->GetGroup(0);
00387 }
00388
00389 pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);
00390 if (pCkSmpl) {
00391 Manufacturer = pCkSmpl->ReadInt32();
00392 Product = pCkSmpl->ReadInt32();
00393 SamplePeriod = pCkSmpl->ReadInt32();
00394 MIDIUnityNote = pCkSmpl->ReadInt32();
00395 FineTune = pCkSmpl->ReadInt32();
00396 pCkSmpl->Read(&SMPTEFormat, 1, 4);
00397 SMPTEOffset = pCkSmpl->ReadInt32();
00398 Loops = pCkSmpl->ReadInt32();
00399 pCkSmpl->ReadInt32();
00400 LoopID = pCkSmpl->ReadInt32();
00401 pCkSmpl->Read(&LoopType, 1, 4);
00402 LoopStart = pCkSmpl->ReadInt32();
00403 LoopEnd = pCkSmpl->ReadInt32();
00404 LoopFraction = pCkSmpl->ReadInt32();
00405 LoopPlayCount = pCkSmpl->ReadInt32();
00406 } else {
00407
00408 Manufacturer = 0;
00409 Product = 0;
00410 SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5);
00411 MIDIUnityNote = 60;
00412 FineTune = 0;
00413 SMPTEFormat = smpte_format_no_offset;
00414 SMPTEOffset = 0;
00415 Loops = 0;
00416 LoopID = 0;
00417 LoopType = loop_type_normal;
00418 LoopStart = 0;
00419 LoopEnd = 0;
00420 LoopFraction = 0;
00421 LoopPlayCount = 0;
00422 }
00423
00424 FrameTable = NULL;
00425 SamplePos = 0;
00426 RAMCache.Size = 0;
00427 RAMCache.pStart = NULL;
00428 RAMCache.NullExtensionSize = 0;
00429
00430 if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported");
00431
00432 RIFF::Chunk* ewav = waveList->GetSubChunk(CHUNK_ID_EWAV);
00433 Compressed = ewav;
00434 Dithered = false;
00435 TruncatedBits = 0;
00436 if (Compressed) {
00437 uint32_t version = ewav->ReadInt32();
00438 if (version == 3 && BitDepth == 24) {
00439 Dithered = ewav->ReadInt32();
00440 ewav->SetPos(Channels == 2 ? 84 : 64);
00441 TruncatedBits = ewav->ReadInt32();
00442 }
00443 ScanCompressedSample();
00444 }
00445
00446
00447 if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) {
00448 InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE];
00449 InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE;
00450 }
00451 FrameOffset = 0;
00452
00453 LoopSize = LoopEnd - LoopStart + 1;
00454 }
00455
00467 void Sample::UpdateChunks() {
00468
00469 DLS::Sample::UpdateChunks();
00470
00471
00472 pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL);
00473 if (!pCkSmpl) {
00474 pCkSmpl = pWaveList->AddSubChunk(CHUNK_ID_SMPL, 60);
00475 memset(pCkSmpl->LoadChunkData(), 0, 60);
00476 }
00477
00478 uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData();
00479 SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5);
00480 store32(&pData[0], Manufacturer);
00481 store32(&pData[4], Product);
00482 store32(&pData[8], SamplePeriod);
00483 store32(&pData[12], MIDIUnityNote);
00484 store32(&pData[16], FineTune);
00485 store32(&pData[20], SMPTEFormat);
00486 store32(&pData[24], SMPTEOffset);
00487 store32(&pData[28], Loops);
00488
00489
00490
00491 store32(&pData[36], LoopID);
00492 store32(&pData[40], LoopType);
00493 store32(&pData[44], LoopStart);
00494 store32(&pData[48], LoopEnd);
00495 store32(&pData[52], LoopFraction);
00496 store32(&pData[56], LoopPlayCount);
00497
00498
00499 pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX);
00500 if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4);
00501
00502 uint16_t iSampleGroup = 0;
00503 File* pFile = static_cast<File*>(pParent);
00504 if (pFile->pGroups) {
00505 std::list<Group*>::iterator iter = pFile->pGroups->begin();
00506 std::list<Group*>::iterator end = pFile->pGroups->end();
00507 for (int i = 0; iter != end; i++, iter++) {
00508 if (*iter == pGroup) {
00509 iSampleGroup = i;
00510 break;
00511 }
00512 }
00513 }
00514
00515 pData = (uint8_t*) pCk3gix->LoadChunkData();
00516 store16(&pData[0], iSampleGroup);
00517 }
00518
00520 void Sample::ScanCompressedSample() {
00521
00522 this->SamplesTotal = 0;
00523 std::list<unsigned long> frameOffsets;
00524
00525 SamplesPerFrame = BitDepth == 24 ? 256 : 2048;
00526 WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels;
00527
00528
00529 pCkData->SetPos(0);
00530 if (Channels == 2) {
00531 for (int i = 0 ; ; i++) {
00532
00533
00534 if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos());
00535
00536 const int mode_l = pCkData->ReadUint8();
00537 const int mode_r = pCkData->ReadUint8();
00538 if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode");
00539 const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r];
00540
00541 if (pCkData->RemainingBytes() <= frameSize) {
00542 SamplesInLastFrame =
00543 ((pCkData->RemainingBytes() - headerSize[mode_l] - headerSize[mode_r]) << 3) /
00544 (bitsPerSample[mode_l] + bitsPerSample[mode_r]);
00545 SamplesTotal += SamplesInLastFrame;
00546 break;
00547 }
00548 SamplesTotal += SamplesPerFrame;
00549 pCkData->SetPos(frameSize, RIFF::stream_curpos);
00550 }
00551 }
00552 else {
00553 for (int i = 0 ; ; i++) {
00554 if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos());
00555
00556 const int mode = pCkData->ReadUint8();
00557 if (mode > 5) throw gig::Exception("Unknown compression mode");
00558 const unsigned long frameSize = bytesPerFrame[mode];
00559
00560 if (pCkData->RemainingBytes() <= frameSize) {
00561 SamplesInLastFrame =
00562 ((pCkData->RemainingBytes() - headerSize[mode]) << 3) / bitsPerSample[mode];
00563 SamplesTotal += SamplesInLastFrame;
00564 break;
00565 }
00566 SamplesTotal += SamplesPerFrame;
00567 pCkData->SetPos(frameSize, RIFF::stream_curpos);
00568 }
00569 }
00570 pCkData->SetPos(0);
00571
00572
00573 if (FrameTable) delete[] FrameTable;
00574 FrameTable = new unsigned long[frameOffsets.size()];
00575 std::list<unsigned long>::iterator end = frameOffsets.end();
00576 std::list<unsigned long>::iterator iter = frameOffsets.begin();
00577 for (int i = 0; iter != end; i++, iter++) {
00578 FrameTable[i] = *iter;
00579 }
00580 }
00581
00591 buffer_t Sample::LoadSampleData() {
00592 return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, 0);
00593 }
00594
00617 buffer_t Sample::LoadSampleData(unsigned long SampleCount) {
00618 return LoadSampleDataWithNullSamplesExtension(SampleCount, 0);
00619 }
00620
00640 buffer_t Sample::LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount) {
00641 return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, NullSamplesCount);
00642 }
00643
00676 buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) {
00677 if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal;
00678 if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
00679 unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize;
00680 RAMCache.pStart = new int8_t[allocationsize];
00681 RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize;
00682 RAMCache.NullExtensionSize = allocationsize - RAMCache.Size;
00683
00684 memset((int8_t*)RAMCache.pStart + RAMCache.Size, 0, RAMCache.NullExtensionSize);
00685 return GetCache();
00686 }
00687
00698 buffer_t Sample::GetCache() {
00699
00700 buffer_t result;
00701 result.Size = this->RAMCache.Size;
00702 result.pStart = this->RAMCache.pStart;
00703 result.NullExtensionSize = this->RAMCache.NullExtensionSize;
00704 return result;
00705 }
00706
00713 void Sample::ReleaseSampleData() {
00714 if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
00715 RAMCache.pStart = NULL;
00716 RAMCache.Size = 0;
00717 }
00718
00749 void Sample::Resize(int iNewSize) {
00750 if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)");
00751 DLS::Sample::Resize(iNewSize);
00752 }
00753
00775 unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {
00776 if (Compressed) {
00777 switch (Whence) {
00778 case RIFF::stream_curpos:
00779 this->SamplePos += SampleCount;
00780 break;
00781 case RIFF::stream_end:
00782 this->SamplePos = this->SamplesTotal - 1 - SampleCount;
00783 break;
00784 case RIFF::stream_backward:
00785 this->SamplePos -= SampleCount;
00786 break;
00787 case RIFF::stream_start: default:
00788 this->SamplePos = SampleCount;
00789 break;
00790 }
00791 if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal;
00792
00793 unsigned long frame = this->SamplePos / 2048;
00794 this->FrameOffset = this->SamplePos % 2048;
00795 pCkData->SetPos(FrameTable[frame]);
00796 return this->SamplePos;
00797 }
00798 else {
00799 unsigned long orderedBytes = SampleCount * this->FrameSize;
00800 unsigned long result = pCkData->SetPos(orderedBytes, Whence);
00801 return (result == orderedBytes) ? SampleCount
00802 : result / this->FrameSize;
00803 }
00804 }
00805
00809 unsigned long Sample::GetPos() {
00810 if (Compressed) return SamplePos;
00811 else return pCkData->GetPos() / FrameSize;
00812 }
00813
00848 unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState,
00849 DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) {
00850 unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend;
00851 uint8_t* pDst = (uint8_t*) pBuffer;
00852
00853 SetPos(pPlaybackState->position);
00854
00855 if (pDimRgn->SampleLoops) {
00856
00857 const DLS::sample_loop_t& loop = pDimRgn->pSampleLoops[0];
00858 const uint32_t loopEnd = loop.LoopStart + loop.LoopLength;
00859
00860 if (GetPos() <= loopEnd) {
00861 switch (loop.LoopType) {
00862
00863 case loop_type_bidirectional: {
00864 do {
00865
00866 if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;
00867
00868 if (!pPlaybackState->reverse) {
00869 do {
00870 samplestoloopend = loopEnd - GetPos();
00871 readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
00872 samplestoread -= readsamples;
00873 totalreadsamples += readsamples;
00874 if (readsamples == samplestoloopend) {
00875 pPlaybackState->reverse = true;
00876 break;
00877 }
00878 } while (samplestoread && readsamples);
00879 }
00880 else {
00881
00882
00883
00884
00885
00886
00887
00888 unsigned long swapareastart = totalreadsamples;
00889 unsigned long loopoffset = GetPos() - loop.LoopStart;
00890 unsigned long samplestoreadinloop = Min(samplestoread, loopoffset);
00891 unsigned long reverseplaybackend = GetPos() - samplestoreadinloop;
00892
00893 SetPos(reverseplaybackend);
00894
00895
00896 do {
00897 readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer);
00898 samplestoreadinloop -= readsamples;
00899 samplestoread -= readsamples;
00900 totalreadsamples += readsamples;
00901 } while (samplestoreadinloop && readsamples);
00902
00903 SetPos(reverseplaybackend);
00904
00905 if (reverseplaybackend == loop.LoopStart) {
00906 pPlaybackState->loop_cycles_left--;
00907 pPlaybackState->reverse = false;
00908 }
00909
00910
00911 SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
00912 }
00913 } while (samplestoread && readsamples);
00914 break;
00915 }
00916
00917 case loop_type_backward: {
00918
00919 if (!pPlaybackState->reverse) do {
00920 samplestoloopend = loopEnd - GetPos();
00921 readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
00922 samplestoread -= readsamples;
00923 totalreadsamples += readsamples;
00924 if (readsamples == samplestoloopend) {
00925 pPlaybackState->reverse = true;
00926 break;
00927 }
00928 } while (samplestoread && readsamples);
00929
00930 if (!samplestoread) break;
00931
00932
00933
00934
00935
00936
00937
00938 unsigned long swapareastart = totalreadsamples;
00939 unsigned long loopoffset = GetPos() - loop.LoopStart;
00940 unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset)
00941 : samplestoread;
00942 unsigned long reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength);
00943
00944 SetPos(reverseplaybackend);
00945
00946
00947 do {
00948
00949 if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;
00950 samplestoloopend = loopEnd - GetPos();
00951 readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer);
00952 samplestoreadinloop -= readsamples;
00953 samplestoread -= readsamples;
00954 totalreadsamples += readsamples;
00955 if (readsamples == samplestoloopend) {
00956 pPlaybackState->loop_cycles_left--;
00957 SetPos(loop.LoopStart);
00958 }
00959 } while (samplestoreadinloop && readsamples);
00960
00961 SetPos(reverseplaybackend);
00962
00963
00964 SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
00965 break;
00966 }
00967
00968 default: case loop_type_normal: {
00969 do {
00970
00971 if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;
00972 samplestoloopend = loopEnd - GetPos();
00973 readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
00974 samplestoread -= readsamples;
00975 totalreadsamples += readsamples;
00976 if (readsamples == samplestoloopend) {
00977 pPlaybackState->loop_cycles_left--;
00978 SetPos(loop.LoopStart);
00979 }
00980 } while (samplestoread && readsamples);