/[svn]/libgig/trunk/src/gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/gig.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 858 by persson, Sat May 6 11:29:29 2006 UTC revision 902 by persson, Sat Jul 22 14:22:01 2006 UTC
# Line 111  namespace { Line 111  namespace {
111          return x & 0x800000 ? x - 0x1000000 : x;          return x & 0x800000 ? x - 0x1000000 : x;
112      }      }
113    
114        inline void store24(unsigned char* pDst, int x)
115        {
116            pDst[0] = x;
117            pDst[1] = x >> 8;
118            pDst[2] = x >> 16;
119        }
120    
121      void Decompress16(int compressionmode, const unsigned char* params,      void Decompress16(int compressionmode, const unsigned char* params,
122                        int srcStep, int dstStep,                        int srcStep, int dstStep,
123                        const unsigned char* pSrc, int16_t* pDst,                        const unsigned char* pSrc, int16_t* pDst,
# Line 150  namespace { Line 157  namespace {
157      }      }
158    
159      void Decompress24(int compressionmode, const unsigned char* params,      void Decompress24(int compressionmode, const unsigned char* params,
160                        int dstStep, const unsigned char* pSrc, int16_t* pDst,                        int dstStep, const unsigned char* pSrc, uint8_t* pDst,
161                        unsigned long currentframeoffset,                        unsigned long currentframeoffset,
162                        unsigned long copysamples, int truncatedBits)                        unsigned long copysamples, int truncatedBits)
163      {      {
         // Note: The 24 bits are truncated to 16 bits for now.  
   
164          int y, dy, ddy, dddy;          int y, dy, ddy, dddy;
         const int shift = 8 - truncatedBits;  
165    
166  #define GET_PARAMS(params)                      \  #define GET_PARAMS(params)                      \
167          y    = get24(params);                   \          y    = get24(params);                   \
# Line 173  namespace { Line 177  namespace {
177    
178  #define COPY_ONE(x)                             \  #define COPY_ONE(x)                             \
179          SKIP_ONE(x);                            \          SKIP_ONE(x);                            \
180          *pDst = y >> shift;                     \          store24(pDst, y << truncatedBits);      \
181          pDst += dstStep          pDst += dstStep
182    
183          switch (compressionmode) {          switch (compressionmode) {
184              case 2: // 24 bit uncompressed              case 2: // 24 bit uncompressed
185                  pSrc += currentframeoffset * 3;                  pSrc += currentframeoffset * 3;
186                  while (copysamples) {                  while (copysamples) {
187                      *pDst = get24(pSrc) >> shift;                      store24(pDst, get24(pSrc) << truncatedBits);
188                      pDst += dstStep;                      pDst += dstStep;
189                      pSrc += 3;                      pSrc += 3;
190                      copysamples--;                      copysamples--;
# Line 348  namespace { Line 352  namespace {
352          }          }
353          FrameOffset = 0; // just for streaming compressed samples          FrameOffset = 0; // just for streaming compressed samples
354    
355          LoopSize = LoopEnd - LoopStart;          LoopSize = LoopEnd - LoopStart + 1;
356      }      }
357    
358      /**      /**
# Line 722  namespace { Line 726  namespace {
726       * @param SampleCount      number of sample points to read       * @param SampleCount      number of sample points to read
727       * @param pPlaybackState   will be used to store and reload the playback       * @param pPlaybackState   will be used to store and reload the playback
728       *                         state for the next ReadAndLoop() call       *                         state for the next ReadAndLoop() call
729         * @param pDimRgn          dimension region with looping information
730       * @param pExternalDecompressionBuffer  (optional) external buffer to use for decompression       * @param pExternalDecompressionBuffer  (optional) external buffer to use for decompression
731       * @returns                number of successfully read sample points       * @returns                number of successfully read sample points
732       * @see                    CreateDecompressionBuffer()       * @see                    CreateDecompressionBuffer()
733       */       */
734      unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer) {      unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState,
735                                          DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) {
736          unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend;          unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend;
737          uint8_t* pDst = (uint8_t*) pBuffer;          uint8_t* pDst = (uint8_t*) pBuffer;
738    
739          SetPos(pPlaybackState->position); // recover position from the last time          SetPos(pPlaybackState->position); // recover position from the last time
740    
741          if (this->Loops && GetPos() <= this->LoopEnd) { // honor looping if there are loop points defined          if (pDimRgn->SampleLoops) { // honor looping if there are loop points defined
742    
743              switch (this->LoopType) {              const DLS::sample_loop_t& loop = pDimRgn->pSampleLoops[0];
744                const uint32_t loopEnd = loop.LoopStart + loop.LoopLength;
745    
746                  case loop_type_bidirectional: { //TODO: not tested yet!              if (GetPos() <= loopEnd) {
747                      do {                  switch (loop.LoopType) {
                         // if not endless loop check if max. number of loop cycles have been passed  
                         if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;  
   
                         if (!pPlaybackState->reverse) { // forward playback  
                             do {  
                                 samplestoloopend  = this->LoopEnd - GetPos();  
                                 readsamples       = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);  
                                 samplestoread    -= readsamples;  
                                 totalreadsamples += readsamples;  
                                 if (readsamples == samplestoloopend) {  
                                     pPlaybackState->reverse = true;  
                                     break;  
                                 }  
                             } while (samplestoread && readsamples);  
                         }  
                         else { // backward playback  
748    
749                              // as we can only read forward from disk, we have to                      case loop_type_bidirectional: { //TODO: not tested yet!
750                              // determine the end position within the loop first,                          do {
751                              // read forward from that 'end' and finally after                              // if not endless loop check if max. number of loop cycles have been passed
752                              // reading, swap all sample frames so it reflects                              if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;
753                              // backward playback  
754                                if (!pPlaybackState->reverse) { // forward playback
755                              unsigned long swapareastart       = totalreadsamples;                                  do {
756                              unsigned long loopoffset          = GetPos() - this->LoopStart;                                      samplestoloopend  = loopEnd - GetPos();
757                              unsigned long samplestoreadinloop = Min(samplestoread, loopoffset);                                      readsamples       = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
758                              unsigned long reverseplaybackend  = GetPos() - samplestoreadinloop;                                      samplestoread    -= readsamples;
759                                        totalreadsamples += readsamples;
760                              SetPos(reverseplaybackend);                                      if (readsamples == samplestoloopend) {
761                                            pPlaybackState->reverse = true;
762                              // read samples for backward playback                                          break;
763                              do {                                      }
764                                  readsamples          = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer);                                  } while (samplestoread && readsamples);
765                                  samplestoreadinloop -= readsamples;                              }
766                                  samplestoread       -= readsamples;                              else { // backward playback
                                 totalreadsamples    += readsamples;  
                             } while (samplestoreadinloop && readsamples);  
767    
768                              SetPos(reverseplaybackend); // pretend we really read backwards                                  // as we can only read forward from disk, we have to
769                                    // determine the end position within the loop first,
770                                    // read forward from that 'end' and finally after
771                                    // reading, swap all sample frames so it reflects
772                                    // backward playback
773    
774                                    unsigned long swapareastart       = totalreadsamples;
775                                    unsigned long loopoffset          = GetPos() - loop.LoopStart;
776                                    unsigned long samplestoreadinloop = Min(samplestoread, loopoffset);
777                                    unsigned long reverseplaybackend  = GetPos() - samplestoreadinloop;
778    
779                                    SetPos(reverseplaybackend);
780    
781                                    // read samples for backward playback
782                                    do {
783                                        readsamples          = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer);
784                                        samplestoreadinloop -= readsamples;
785                                        samplestoread       -= readsamples;
786                                        totalreadsamples    += readsamples;
787                                    } while (samplestoreadinloop && readsamples);
788    
789                                    SetPos(reverseplaybackend); // pretend we really read backwards
790    
791                                    if (reverseplaybackend == loop.LoopStart) {
792                                        pPlaybackState->loop_cycles_left--;
793                                        pPlaybackState->reverse = false;
794                                    }
795    
796                              if (reverseplaybackend == this->LoopStart) {                                  // reverse the sample frames for backward playback
797                                  pPlaybackState->loop_cycles_left--;                                  SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
                                 pPlaybackState->reverse = false;  
798                              }                              }
799                            } while (samplestoread && readsamples);
800                            break;
801                        }
802    
803                              // reverse the sample frames for backward playback                      case loop_type_backward: { // TODO: not tested yet!
804                              SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);                          // forward playback (not entered the loop yet)
805                          }                          if (!pPlaybackState->reverse) do {
806                      } while (samplestoread && readsamples);                              samplestoloopend  = loopEnd - GetPos();
807                      break;                              readsamples       = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
808                  }                              samplestoread    -= readsamples;
809                                totalreadsamples += readsamples;
810                  case loop_type_backward: { // TODO: not tested yet!                              if (readsamples == samplestoloopend) {
811                      // forward playback (not entered the loop yet)                                  pPlaybackState->reverse = true;
812                      if (!pPlaybackState->reverse) do {                                  break;
813                          samplestoloopend  = this->LoopEnd - GetPos();                              }
814                          readsamples       = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);                          } while (samplestoread && readsamples);
                         samplestoread    -= readsamples;  
                         totalreadsamples += readsamples;  
                         if (readsamples == samplestoloopend) {  
                             pPlaybackState->reverse = true;  
                             break;  
                         }  
                     } while (samplestoread && readsamples);  
815    
816                      if (!samplestoread) break;                          if (!samplestoread) break;
817    
818                      // as we can only read forward from disk, we have to                          // as we can only read forward from disk, we have to
819                      // determine the end position within the loop first,                          // determine the end position within the loop first,
820                      // read forward from that 'end' and finally after                          // read forward from that 'end' and finally after
821                      // reading, swap all sample frames so it reflects                          // reading, swap all sample frames so it reflects
822                      // backward playback                          // backward playback
823    
824                      unsigned long swapareastart       = totalreadsamples;                          unsigned long swapareastart       = totalreadsamples;
825                      unsigned long loopoffset          = GetPos() - this->LoopStart;                          unsigned long loopoffset          = GetPos() - loop.LoopStart;
826                      unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * LoopSize - loopoffset)                          unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset)
827                                                                                : samplestoread;                                                                                    : samplestoread;
828                      unsigned long reverseplaybackend  = this->LoopStart + Abs((loopoffset - samplestoreadinloop) % this->LoopSize);                          unsigned long reverseplaybackend  = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength);
829    
830                      SetPos(reverseplaybackend);                          SetPos(reverseplaybackend);
831    
832                      // read samples for backward playback                          // read samples for backward playback
833                      do {                          do {
834                          // if not endless loop check if max. number of loop cycles have been passed                              // if not endless loop check if max. number of loop cycles have been passed
835                          if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;                              if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;
836                          samplestoloopend     = this->LoopEnd - GetPos();                              samplestoloopend     = loopEnd - GetPos();
837                          readsamples          = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer);                              readsamples          = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer);
838                          samplestoreadinloop -= readsamples;                              samplestoreadinloop -= readsamples;
839                          samplestoread       -= readsamples;                              samplestoread       -= readsamples;
840                          totalreadsamples    += readsamples;                              totalreadsamples    += readsamples;
841                          if (readsamples == samplestoloopend) {                              if (readsamples == samplestoloopend) {
842                              pPlaybackState->loop_cycles_left--;                                  pPlaybackState->loop_cycles_left--;
843                              SetPos(this->LoopStart);                                  SetPos(loop.LoopStart);
844                          }                              }
845                      } while (samplestoreadinloop && readsamples);                          } while (samplestoreadinloop && readsamples);
846    
847                      SetPos(reverseplaybackend); // pretend we really read backwards                          SetPos(reverseplaybackend); // pretend we really read backwards
848    
849                      // reverse the sample frames for backward playback                          // reverse the sample frames for backward playback
850                      SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);                          SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
851                      break;                          break;
852                  }                      }
853    
854                  default: case loop_type_normal: {                      default: case loop_type_normal: {
855                      do {                          do {
856                          // if not endless loop check if max. number of loop cycles have been passed                              // if not endless loop check if max. number of loop cycles have been passed
857                          if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;                              if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break;
858                          samplestoloopend  = this->LoopEnd - GetPos();                              samplestoloopend  = loopEnd - GetPos();
859                          readsamples       = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);                              readsamples       = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer);
860                          samplestoread    -= readsamples;                              samplestoread    -= readsamples;
861                          totalreadsamples += readsamples;                              totalreadsamples += readsamples;
862                          if (readsamples == samplestoloopend) {                              if (readsamples == samplestoloopend) {
863                              pPlaybackState->loop_cycles_left--;                                  pPlaybackState->loop_cycles_left--;
864                              SetPos(this->LoopStart);                                  SetPos(loop.LoopStart);
865                          }                              }
866                      } while (samplestoread && readsamples);                          } while (samplestoread && readsamples);
867                      break;                          break;
868                        }
869                  }                  }
870              }              }
871          }          }
# Line 884  namespace { Line 895  namespace {
895       * have to use an external decompression buffer for <b>EACH</b>       * have to use an external decompression buffer for <b>EACH</b>
896       * streaming thread to avoid race conditions and crashes!       * streaming thread to avoid race conditions and crashes!
897       *       *
898         * For 16 bit samples, the data in the buffer will be int16_t
899         * (using native endianness). For 24 bit, the buffer will
900         * contain three bytes per sample, little-endian.
901         *
902       * @param pBuffer      destination buffer       * @param pBuffer      destination buffer
903       * @param SampleCount  number of sample points to read       * @param SampleCount  number of sample points to read
904       * @param pExternalDecompressionBuffer  (optional) external buffer to use for decompression       * @param pExternalDecompressionBuffer  (optional) external buffer to use for decompression
# Line 894  namespace { Line 909  namespace {
909          if (SampleCount == 0) return 0;          if (SampleCount == 0) return 0;
910          if (!Compressed) {          if (!Compressed) {
911              if (BitDepth == 24) {              if (BitDepth == 24) {
912                  // 24 bit sample. For now just truncate to 16 bit.                  return pCkData->Read(pBuffer, SampleCount * FrameSize, 1) / FrameSize;
                 unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart);  
                 int16_t* pDst = static_cast<int16_t*>(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<int16_t*>(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<int16_t*>(pBuffer);  
                 }  
913              }              }
914              else { // 16 bit              else { // 16 bit
915                  // (pCkData->Read does endian correction)                  // (pCkData->Read does endian correction)
# Line 944  namespace { Line 939  namespace {
939    
940              unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart;              unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart;
941              int16_t* pDst = static_cast<int16_t*>(pBuffer);              int16_t* pDst = static_cast<int16_t*>(pBuffer);
942                uint8_t* pDst24 = static_cast<uint8_t*>(pBuffer);
943              remainingbytes = pCkData->Read(pSrc, assumedsize, 1);              remainingbytes = pCkData->Read(pSrc, assumedsize, 1);
944    
945              while (remainingsamples && remainingbytes) {              while (remainingsamples && remainingbytes) {
# Line 1025  namespace { Line 1021  namespace {
1021                              const unsigned char* const param_r = pSrc;                              const unsigned char* const param_r = pSrc;
1022                              if (mode_r != 2) pSrc += 12;                              if (mode_r != 2) pSrc += 12;
1023    
1024                              Decompress24(mode_l, param_l, 2, pSrc, pDst,                              Decompress24(mode_l, param_l, 6, pSrc, pDst24,
1025                                           skipsamples, copysamples, TruncatedBits);                                           skipsamples, copysamples, TruncatedBits);
1026                              Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1,                              Decompress24(mode_r, param_r, 6, pSrc + rightChannelOffset, pDst24 + 3,
1027                                           skipsamples, copysamples, TruncatedBits);                                           skipsamples, copysamples, TruncatedBits);
1028                              pDst += copysamples << 1;                              pDst24 += copysamples * 6;
1029                          }                          }
1030                          else { // Mono                          else { // Mono
1031                              Decompress24(mode_l, param_l, 1, pSrc, pDst,                              Decompress24(mode_l, param_l, 3, pSrc, pDst24,
1032                                           skipsamples, copysamples, TruncatedBits);                                           skipsamples, copysamples, TruncatedBits);
1033                              pDst += copysamples;                              pDst24 += copysamples * 3;
1034                          }                          }
1035                      }                      }
1036                      else { // 16 bit                      else { // 16 bit
# Line 2083  namespace { Line 2079  namespace {
2079                      pDimensionDefinitions[i].split_type = (dimension == dimension_layer ||                      pDimensionDefinitions[i].split_type = (dimension == dimension_layer ||
2080                                                             dimension == dimension_samplechannel ||                                                             dimension == dimension_samplechannel ||
2081                                                             dimension == dimension_releasetrigger ||                                                             dimension == dimension_releasetrigger ||
2082                                                               dimension == dimension_keyboard ||
2083                                                             dimension == dimension_roundrobin ||                                                             dimension == dimension_roundrobin ||
2084                                                             dimension == dimension_random) ? split_type_bit                                                             dimension == dimension_random) ? split_type_bit
2085                                                                                            : split_type_normal;                                                                                            : split_type_normal;
# Line 2111  namespace { Line 2108  namespace {
2108              // load sample references              // load sample references
2109              for (uint i = 0; i < DimensionRegions; i++) {              for (uint i = 0; i < DimensionRegions; i++) {
2110                  uint32_t wavepoolindex = _3lnk->ReadUint32();                  uint32_t wavepoolindex = _3lnk->ReadUint32();
2111                  pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);                  if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);
2112              }              }
2113          }          }
2114    
# Line 2489  namespace { Line 2486  namespace {
2486      Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) {      Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) {
2487          if ((int32_t)WavePoolTableIndex == -1) return NULL;          if ((int32_t)WavePoolTableIndex == -1) return NULL;
2488          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
2489            if (!file->pWavePoolTable) return NULL;
2490          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
2491          unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex];          unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex];
2492          Sample* sample = file->GetFirstSample(pProgress);          Sample* sample = file->GetFirstSample(pProgress);

Legend:
Removed from v.858  
changed lines
  Added in v.902

  ViewVC Help
Powered by ViewVC