/[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 823 by schoenebeck, Fri Dec 23 01:38:50 2005 UTC revision 902 by persson, Sat Jul 22 14:22:01 2006 UTC
# Line 51  Line 51 
51    
52  namespace gig {  namespace gig {
53    
 // *************** dimension_def_t ***************  
 // *  
   
     dimension_def_t& dimension_def_t::operator=(const dimension_def_t& arg) {  
         dimension  = arg.dimension;  
         bits       = arg.bits;  
         zones      = arg.zones;  
         split_type = arg.split_type;  
         ranges     = arg.ranges;  
         zone_size  = arg.zone_size;  
         if (ranges) {  
             ranges = new range_t[zones];  
             for (int i = 0; i < zones; i++)  
                 ranges[i] = arg.ranges[i];  
         }  
         return *this;  
     }  
   
   
   
54  // *************** progress_t ***************  // *************** progress_t ***************
55  // *  // *
56    
# Line 131  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 170  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 193  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 368  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 742  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 904  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 914  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 964  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 1045  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 1449  namespace { Line 1425  namespace {
1425                                                  VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0);                                                  VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0);
1426    
1427          SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));          SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));
1428            VelocityTable = 0;
1429      }      }
1430    
1431      /**      /**
# Line 1968  namespace { Line 1945  namespace {
1945              delete pVelocityTables;              delete pVelocityTables;
1946              pVelocityTables = NULL;              pVelocityTables = NULL;
1947          }          }
1948            if (VelocityTable) delete[] VelocityTable;
1949      }      }
1950    
1951      /**      /**
# Line 2092  namespace { Line 2070  namespace {
2070                      pDimensionDefinitions[i].bits       = 0;                      pDimensionDefinitions[i].bits       = 0;
2071                      pDimensionDefinitions[i].zones      = 0;                      pDimensionDefinitions[i].zones      = 0;
2072                      pDimensionDefinitions[i].split_type = split_type_bit;                      pDimensionDefinitions[i].split_type = split_type_bit;
                     pDimensionDefinitions[i].ranges     = NULL;  
2073                      pDimensionDefinitions[i].zone_size  = 0;                      pDimensionDefinitions[i].zone_size  = 0;
2074                  }                  }
2075                  else { // active dimension                  else { // active dimension
# Line 2102  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;
                     pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point  
2086                      pDimensionDefinitions[i].zone_size  =                      pDimensionDefinitions[i].zone_size  =
2087                          (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones                          (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones
2088                                                                                     : 0;                                                                                     : 0;
# Line 2116  namespace { Line 2093  namespace {
2093                  }                  }
2094                  _3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition                  _3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition
2095              }              }
2096                for (int i = dimensionBits ; i < 8 ; i++) pDimensionDefinitions[i].bits = 0;
2097    
2098              // check velocity dimension (if there is one) for custom defined zone ranges              // if there's a velocity dimension and custom velocity zone splits are used,
2099              for (uint i = 0; i < Dimensions; i++) {              // update the VelocityTables in the dimension regions
2100                  dimension_def_t* pDimDef = pDimensionDefinitions + i;              UpdateVelocityTable();
                 if (pDimDef->dimension == dimension_velocity) {  
                     if (pDimensionRegions[0]->VelocityUpperLimit == 0) {  
                         // no custom defined ranges  
                         pDimDef->split_type = split_type_normal;  
                         pDimDef->ranges     = NULL;  
                     }  
                     else { // custom defined ranges  
                         pDimDef->split_type = split_type_customvelocity;  
                         pDimDef->ranges     = new range_t[pDimDef->zones];  
                         UpdateVelocityTable(pDimDef);  
                     }  
                 }  
             }  
2101    
2102              // jump to start of the wave pool indices (if not already there)              // jump to start of the wave pool indices (if not already there)
             File* file = (File*) GetParent()->GetParent();  
2103              if (file->pVersion && file->pVersion->major == 3)              if (file->pVersion && file->pVersion->major == 3)
2104                  _3lnk->SetPos(68); // version 3 has a different 3lnk structure                  _3lnk->SetPos(68); // version 3 has a different 3lnk structure
2105              else              else
# Line 2144  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 2233  namespace { Line 2197  namespace {
2197          }          }
2198      }      }
2199    
2200      void Region::UpdateVelocityTable(dimension_def_t* pDimDef) {      void Region::UpdateVelocityTable() {
2201          // get dimension's index          // get velocity dimension's index
2202          int iDimensionNr = -1;          int veldim = -1;
2203          for (int i = 0; i < Dimensions; i++) {          for (int i = 0 ; i < Dimensions ; i++) {
2204              if (&pDimensionDefinitions[i] == pDimDef) {              if (pDimensionDefinitions[i].dimension == gig::dimension_velocity) {
2205                  iDimensionNr = i;                  veldim = i;
2206                  break;                  break;
2207              }              }
2208          }          }
2209          if (iDimensionNr < 0) throw gig::Exception("Invalid dimension_def_t pointer");          if (veldim == -1) return;
2210    
2211            int step = 1;
2212            for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits;
2213            int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step;
2214            int end = step * pDimensionDefinitions[veldim].zones;
2215    
2216            // loop through all dimension regions for all dimensions except the velocity dimension
2217            int dim[8] = { 0 };
2218            for (int i = 0 ; i < DimensionRegions ; i++) {
2219    
2220                if (pDimensionRegions[i]->VelocityUpperLimit) {
2221                    // create the velocity table
2222                    uint8_t* table = pDimensionRegions[i]->VelocityTable;
2223                    if (!table) {
2224                        table = new uint8_t[128];
2225                        pDimensionRegions[i]->VelocityTable = table;
2226                    }
2227                    int tableidx = 0;
2228                    int velocityZone = 0;
2229                    for (int k = i ; k < end ; k += step) {
2230                        DimensionRegion *d = pDimensionRegions[k];
2231                        for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2232                        velocityZone++;
2233                    }
2234                } else {
2235                    if (pDimensionRegions[i]->VelocityTable) {
2236                        delete[] pDimensionRegions[i]->VelocityTable;
2237                        pDimensionRegions[i]->VelocityTable = 0;
2238                    }
2239                }
2240    
2241          uint8_t bits[8] = { 0 };              int j;
2242          int previousUpperLimit = -1;              int shift = 0;
2243          for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) {              for (j = 0 ; j < Dimensions ; j++) {
2244              bits[iDimensionNr] = velocityZone;                  if (j == veldim) i += skipveldim; // skip velocity dimension
2245              DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits);                  else {
2246                        dim[j]++;
2247              pDimDef->ranges[velocityZone].low  = previousUpperLimit + 1;                      if (dim[j] < pDimensionDefinitions[j].zones) break;
2248              pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit;                      else {
2249              previousUpperLimit = pDimDef->ranges[velocityZone].high;                          // skip unused dimension regions
2250              // fill velocity table                          dim[j] = 0;
2251              for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) {                          i += ((1 << pDimensionDefinitions[j].bits) -
2252                  VelocityTable[i] = velocityZone;                                pDimensionDefinitions[j].zones) << shift;
2253                        }
2254                    }
2255                    shift += pDimensionDefinitions[j].bits;
2256              }              }
2257                if (j == Dimensions) break;
2258          }          }
2259      }      }
2260    
# Line 2311  namespace { Line 2309  namespace {
2309          // if this is a layer dimension, update 'Layers' attribute          // if this is a layer dimension, update 'Layers' attribute
2310          if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones;          if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones;
2311    
2312          // if this is velocity dimension and got custom defined ranges, update velocity table          UpdateVelocityTable();
         if (pDimDef->dimension  == dimension_velocity &&  
             pDimDef->split_type == split_type_customvelocity) {  
             UpdateVelocityTable(pDimDef);  
         }  
2313      }      }
2314    
2315      /** @brief Delete an existing dimension.      /** @brief Delete an existing dimension.
# Line 2385  namespace { Line 2379  namespace {
2379          pDimensionDefinitions[Dimensions - 1].dimension = dimension_none;          pDimensionDefinitions[Dimensions - 1].dimension = dimension_none;
2380          pDimensionDefinitions[Dimensions - 1].bits      = 0;          pDimensionDefinitions[Dimensions - 1].bits      = 0;
2381          pDimensionDefinitions[Dimensions - 1].zones     = 0;          pDimensionDefinitions[Dimensions - 1].zones     = 0;
         if (pDimensionDefinitions[Dimensions - 1].ranges) {  
             delete[] pDimensionDefinitions[Dimensions - 1].ranges;  
             pDimensionDefinitions[Dimensions - 1].ranges = NULL;  
         }  
2382    
2383          Dimensions--;          Dimensions--;
2384    
# Line 2397  namespace { Line 2387  namespace {
2387      }      }
2388    
2389      Region::~Region() {      Region::~Region() {
         for (uint i = 0; i < Dimensions; i++) {  
             if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges;  
         }  
2390          for (int i = 0; i < 256; i++) {          for (int i = 0; i < 256; i++) {
2391              if (pDimensionRegions[i]) delete pDimensionRegions[i];              if (pDimensionRegions[i]) delete pDimensionRegions[i];
2392          }          }
# Line 2424  namespace { Line 2411  namespace {
2411       * @see             Dimensions       * @see             Dimensions
2412       */       */
2413      DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) {      DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) {
2414          uint8_t bits[8] = { 0 };          uint8_t bits;
2415            int veldim = -1;
2416            int velbitpos;
2417            int bitpos = 0;
2418            int dimregidx = 0;
2419          for (uint i = 0; i < Dimensions; i++) {          for (uint i = 0; i < Dimensions; i++) {
2420              bits[i] = DimValues[i];              if (pDimensionDefinitions[i].dimension == dimension_velocity) {
2421              switch (pDimensionDefinitions[i].split_type) {                  // the velocity dimension must be handled after the other dimensions
2422                  case split_type_normal:                  veldim = i;
2423                      bits[i] = uint8_t(bits[i] / pDimensionDefinitions[i].zone_size);                  velbitpos = bitpos;
2424                      break;              } else {
2425                  case split_type_customvelocity:                  switch (pDimensionDefinitions[i].split_type) {
2426                      bits[i] = VelocityTable[bits[i]];                      case split_type_normal:
2427                      break;                          bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
2428                  case split_type_bit: // the value is already the sought dimension bit number                          break;
2429                      const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;                      case split_type_bit: // the value is already the sought dimension bit number
2430                      bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
2431                      break;                          bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed
2432                            break;
2433                    }
2434                    dimregidx |= bits << bitpos;
2435              }              }
2436                bitpos += pDimensionDefinitions[i].bits;
2437          }          }
2438          return GetDimensionRegionByBit(bits);          DimensionRegion* dimreg = pDimensionRegions[dimregidx];
2439            if (veldim != -1) {
2440                // (dimreg is now the dimension region for the lowest velocity)
2441                if (dimreg->VelocityUpperLimit) // custom defined zone ranges
2442                    bits = dimreg->VelocityTable[DimValues[veldim]];
2443                else // normal split type
2444                    bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);
2445    
2446                dimregidx |= bits << velbitpos;
2447                dimreg = pDimensionRegions[dimregidx];
2448            }
2449            return dimreg;
2450      }      }
2451    
2452      /**      /**
# Line 2480  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);
# Line 2664  namespace { Line 2671  namespace {
2671      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
2672      }      }
2673    
     File::~File() {  
         // free extension files  
         for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)  
             delete *i;  
     }  
   
2674      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
2675          if (!pSamples) LoadSamples(pProgress);          if (!pSamples) LoadSamples(pProgress);
2676          if (!pSamples) return NULL;          if (!pSamples) return NULL;

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

  ViewVC Help
Powered by ViewVC