/[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 809 by schoenebeck, Tue Nov 22 11:26:55 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 1183  namespace { Line 1159  namespace {
1159      DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {      DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {
1160          Instances++;          Instances++;
1161    
1162            pSample = NULL;
1163    
1164          memcpy(&Crossfade, &SamplerOptions, 4);          memcpy(&Crossfade, &SamplerOptions, 4);
1165          if (!pVelocityTables) pVelocityTables = new VelocityTableMap;          if (!pVelocityTables) pVelocityTables = new VelocityTableMap;
1166    
# Line 1447  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 1966  namespace { Line 1945  namespace {
1945              delete pVelocityTables;              delete pVelocityTables;
1946              pVelocityTables = NULL;              pVelocityTables = NULL;
1947          }          }
1948            if (VelocityTable) delete[] VelocityTable;
1949      }      }
1950    
1951      /**      /**
# Line 2090  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 2100  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 2114  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 2142  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    
2115            // make sure there is at least one dimension region
2116            if (!DimensionRegions) {
2117                RIFF::List* _3prg = rgnList->GetSubList(LIST_TYPE_3PRG);
2118                if (!_3prg) _3prg = rgnList->AddSubList(LIST_TYPE_3PRG);
2119                RIFF::List* _3ewl = _3prg->AddSubList(LIST_TYPE_3EWL);
2120                pDimensionRegions[0] = new DimensionRegion(_3ewl);
2121                DimensionRegions = 1;
2122            }
2123      }      }
2124    
2125      /**      /**
# Line 2161  namespace { Line 2136  namespace {
2136          DLS::Region::UpdateChunks();          DLS::Region::UpdateChunks();
2137    
2138          // update dimension region's chunks          // update dimension region's chunks
2139          for (int i = 0; i < Dimensions; i++)          for (int i = 0; i < DimensionRegions; i++) {
2140              pDimensionRegions[i]->UpdateChunks();              pDimensionRegions[i]->UpdateChunks();
2141            }
2142    
2143          File* pFile = (File*) GetParent()->GetParent();          File* pFile = (File*) GetParent()->GetParent();
2144          const int iMaxDimensions = (pFile->pVersion && pFile->pVersion->major == 3) ? 8 : 5;          const int iMaxDimensions = (pFile->pVersion && pFile->pVersion->major == 3) ? 8 : 5;
# Line 2190  namespace { Line 2166  namespace {
2166          for (uint i = 0; i < iMaxDimensionRegions; i++) {          for (uint i = 0; i < iMaxDimensionRegions; i++) {
2167              int iWaveIndex = -1;              int iWaveIndex = -1;
2168              if (i < DimensionRegions) {              if (i < DimensionRegions) {
2169                  if (!pFile->pSamples) throw gig::Exception("Could not update gig::Region, there are no samples");                  if (!pFile->pSamples || !pFile->pSamples->size()) throw gig::Exception("Could not update gig::Region, there are no samples");
2170                  std::list<Sample*>::iterator iter = pFile->pSamples->begin();                  File::SampleList::iterator iter = pFile->pSamples->begin();
2171                  std::list<Sample*>::iterator end  = pFile->pSamples->end();                  File::SampleList::iterator end  = pFile->pSamples->end();
2172                  for (int index = 0; iter != end; ++iter, ++index) {                  for (int index = 0; iter != end; ++iter, ++index) {
2173                      if (*iter == pDimensionRegions[i]->pSample) iWaveIndex = index;                      if (*iter == pDimensionRegions[i]->pSample) {
2174                      break;                          iWaveIndex = index;
2175                            break;
2176                        }
2177                  }                  }
2178                  if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample");                  if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample");
2179              }              }
# Line 2219  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 2297  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 2371  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 2383  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 2410  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 2466  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 2485  namespace { Line 2506  namespace {
2506      Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) {      Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) {
2507          // Initialization          // Initialization
2508          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;
         RegionIndex = -1;  
2509    
2510          // Loading          // Loading
2511          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 2503  namespace { Line 2523  namespace {
2523              }              }
2524          }          }
2525    
2526          pRegions = new Region*[Regions];          if (!pRegions) pRegions = new RegionList;
2527          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);
2528          if (lrgn) {          if (lrgn) {
             for (uint i = 0; i < Regions; i++) pRegions[i] = NULL;  
2529              RIFF::List* rgn = lrgn->GetFirstSubList();              RIFF::List* rgn = lrgn->GetFirstSubList();
             unsigned int iRegion = 0;  
2530              while (rgn) {              while (rgn) {
2531                  if (rgn->GetListType() == LIST_TYPE_RGN) {                  if (rgn->GetListType() == LIST_TYPE_RGN) {
2532                      __notify_progress(pProgress, (float) iRegion / (float) Regions);                      __notify_progress(pProgress, (float) pRegions->size() / (float) Regions);
2533                      pRegions[iRegion] = new Region(this, rgn);                      pRegions->push_back(new Region(this, rgn));
                     iRegion++;  
2534                  }                  }
2535                  rgn = lrgn->GetNextSubList();                  rgn = lrgn->GetNextSubList();
2536              }              }
# Line 2525  namespace { Line 2542  namespace {
2542      }      }
2543    
2544      void Instrument::UpdateRegionKeyTable() {      void Instrument::UpdateRegionKeyTable() {
2545          for (uint iReg = 0; iReg < Regions; iReg++) {          RegionList::iterator iter = pRegions->begin();
2546              for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) {          RegionList::iterator end  = pRegions->end();
2547                  RegionKeyTable[iKey] = pRegions[iReg];          for (; iter != end; ++iter) {
2548                gig::Region* pRegion = static_cast<gig::Region*>(*iter);
2549                for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) {
2550                    RegionKeyTable[iKey] = pRegion;
2551              }              }
2552          }          }
2553      }      }
2554    
2555      Instrument::~Instrument() {      Instrument::~Instrument() {
         for (uint i = 0; i < Regions; i++) {  
             if (pRegions) {  
                 if (pRegions[i]) delete (pRegions[i]);  
             }  
         }  
         if (pRegions) delete[] pRegions;  
2556      }      }
2557    
2558      /**      /**
# Line 2555  namespace { Line 2569  namespace {
2569          DLS::Instrument::UpdateChunks();          DLS::Instrument::UpdateChunks();
2570    
2571          // update Regions' chunks          // update Regions' chunks
2572          for (int i = 0; i < Regions; i++)          {
2573              pRegions[i]->UpdateChunks();              RegionList::iterator iter = pRegions->begin();
2574                RegionList::iterator end  = pRegions->end();
2575                for (; iter != end; ++iter)
2576                    (*iter)->UpdateChunks();
2577            }
2578    
2579          // make sure 'lart' RIFF list chunk exists          // make sure 'lart' RIFF list chunk exists
2580          RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART);
# Line 2584  namespace { Line 2602  namespace {
2602       *             there is no Region defined for the given \a Key       *             there is no Region defined for the given \a Key
2603       */       */
2604      Region* Instrument::GetRegion(unsigned int Key) {      Region* Instrument::GetRegion(unsigned int Key) {
2605          if (!pRegions || Key > 127) return NULL;          if (!pRegions || !pRegions->size() || Key > 127) return NULL;
2606          return RegionKeyTable[Key];          return RegionKeyTable[Key];
2607    
2608          /*for (int i = 0; i < Regions; i++) {          /*for (int i = 0; i < Regions; i++) {
2609              if (Key <= pRegions[i]->KeyRange.high &&              if (Key <= pRegions[i]->KeyRange.high &&
2610                  Key >= pRegions[i]->KeyRange.low) return pRegions[i];                  Key >= pRegions[i]->KeyRange.low) return pRegions[i];
# Line 2601  namespace { Line 2620  namespace {
2620       * @see      GetNextRegion()       * @see      GetNextRegion()
2621       */       */
2622      Region* Instrument::GetFirstRegion() {      Region* Instrument::GetFirstRegion() {
2623          if (!Regions) return NULL;          if (!pRegions) return NULL;
2624          RegionIndex = 1;          RegionsIterator = pRegions->begin();
2625          return pRegions[0];          return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL );
2626      }      }
2627    
2628      /**      /**
# Line 2615  namespace { Line 2634  namespace {
2634       * @see      GetFirstRegion()       * @see      GetFirstRegion()
2635       */       */
2636      Region* Instrument::GetNextRegion() {      Region* Instrument::GetNextRegion() {
2637          if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL;          if (!pRegions) return NULL;
2638          return pRegions[RegionIndex++];          RegionsIterator++;
2639            return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL );
2640      }      }
2641    
2642      Region* Instrument::AddRegion() {      Region* Instrument::AddRegion() {
# Line 2625  namespace { Line 2645  namespace {
2645          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
2646          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
2647          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
2648          // resize 'pRegions' array (increase by one)          pRegions->push_back(pNewRegion);
2649          Region** pNewRegions = new Region*[Regions + 1];          Regions = pRegions->size();
         memcpy(pNewRegions, pRegions, Regions * sizeof(Region*));  
         // add new Region object  
         pNewRegions[Regions] = pNewRegion;  
         // replace old 'pRegions' array by the new increased array  
         if (pRegions) delete[] pRegions;  
         pRegions = pNewRegions;  
         Regions++;  
2650          // update Region key table for fast lookup          // update Region key table for fast lookup
2651          UpdateRegionKeyTable();          UpdateRegionKeyTable();
2652          // done          // done
# Line 2642  namespace { Line 2655  namespace {
2655    
2656      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
2657          if (!pRegions) return;          if (!pRegions) return;
2658          int iOffset = 0;          DLS::Instrument::DeleteRegion((DLS::Region*) pRegion);
         // resize 'pRegions' array (decrease by one)  
         Region** pNewRegions = new Region*[Regions - 1];  
         for (int i = 0; i < Regions; i++) {  
             if (pRegions[i] == pRegion) { // found Region to delete  
                 iOffset = 1;  
                 delete pRegion;  
             }  
             if (i < Regions - 1) pNewRegions[i] = pRegions[i + iOffset];  
         }  
         if (!iOffset) throw gig::Exception("There is no such gig::Region to delete");  
         // replace old 'pRegions' array by the new decreased array  
         if (pRegions) delete[] pRegions;  
         pRegions = pNewRegions;  
         Regions--;  
2659          // update Region key table for fast lookup          // update Region key table for fast lookup
2660          UpdateRegionKeyTable();          UpdateRegionKeyTable();
2661      }      }
# Line 2667  namespace { Line 2666  namespace {
2666  // *  // *
2667    
2668      File::File() : DLS::File() {      File::File() : DLS::File() {
         pSamples     = NULL;  
         pInstruments = NULL;  
2669      }      }
2670    
2671      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
         pSamples     = NULL;  
         pInstruments = NULL;  
     }  
   
     File::~File() {  
         // free samples  
         if (pSamples) {  
             SamplesIterator = pSamples->begin();  
             while (SamplesIterator != pSamples->end() ) {  
                 delete (*SamplesIterator);  
                 SamplesIterator++;  
             }  
             pSamples->clear();  
             delete pSamples;  
   
         }  
         // free instruments  
         if (pInstruments) {  
             InstrumentsIterator = pInstruments->begin();  
             while (InstrumentsIterator != pInstruments->end() ) {  
                 delete (*InstrumentsIterator);  
                 InstrumentsIterator++;  
             }  
             pInstruments->clear();  
             delete pInstruments;  
         }  
         // free extension files  
         for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)  
             delete *i;  
2672      }      }
2673    
2674      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 2728  namespace { Line 2696  namespace {
2696         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
2697         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2698         // create new Sample object and its respective 'wave' list chunk         // create new Sample object and its respective 'wave' list chunk
        if (!pSamples) pSamples = new SampleList;  
2699         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
2700         Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);         Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
2701         pSamples->push_back(pSample);         pSamples->push_back(pSample);
# Line 2744  namespace { Line 2711  namespace {
2711       * @throws gig::Exception if given sample could not be found       * @throws gig::Exception if given sample could not be found
2712       */       */
2713      void File::DeleteSample(Sample* pSample) {      void File::DeleteSample(Sample* pSample) {
2714          if (!pSamples) throw gig::Exception("Could not delete sample as there are no samples");          if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples");
2715          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);
2716          if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample");          if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample");
2717          pSamples->erase(iter);          pSamples->erase(iter);
2718          delete pSample;          delete pSample;
2719      }      }
2720    
2721        void File::LoadSamples() {
2722            LoadSamples(NULL);
2723        }
2724    
2725      void File::LoadSamples(progress_t* pProgress) {      void File::LoadSamples(progress_t* pProgress) {
2726            if (!pSamples) pSamples = new SampleList;
2727    
2728          RIFF::File* file = pRIFF;          RIFF::File* file = pRIFF;
2729    
2730          // just for progress calculation          // just for progress calculation
# Line 2779  namespace { Line 2752  namespace {
2752                          const float subprogress = (float) iSampleIndex / (float) iTotalSamples;                          const float subprogress = (float) iSampleIndex / (float) iTotalSamples;
2753                          __notify_progress(pProgress, subprogress);                          __notify_progress(pProgress, subprogress);
2754    
                         if (!pSamples) pSamples = new SampleList;  
2755                          unsigned long waveFileOffset = wave->GetFilePos();                          unsigned long waveFileOffset = wave->GetFilePos();
2756                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));
2757    
# Line 2796  namespace { Line 2768  namespace {
2768                  name.replace(nameLen, 5, suffix);                  name.replace(nameLen, 5, suffix);
2769                  file = new RIFF::File(name);                  file = new RIFF::File(name);
2770                  ExtensionFiles.push_back(file);                  ExtensionFiles.push_back(file);
2771              }              } else break;
             else throw gig::Exception("Mandatory <wvpl> chunk not found.");  
2772          }          }
2773    
2774          __notify_progress(pProgress, 1.0); // notify done          __notify_progress(pProgress, 1.0); // notify done
# Line 2807  namespace { Line 2778  namespace {
2778          if (!pInstruments) LoadInstruments();          if (!pInstruments) LoadInstruments();
2779          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2780          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
2781          return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;          return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL );
2782      }      }
2783    
2784      Instrument* File::GetNextInstrument() {      Instrument* File::GetNextInstrument() {
2785          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2786          InstrumentsIterator++;          InstrumentsIterator++;
2787          return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;          return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL );
2788      }      }
2789    
2790      /**      /**
# Line 2846  namespace { Line 2817  namespace {
2817          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2818          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
2819          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {
2820              if (i == index) return *InstrumentsIterator;              if (i == index) return static_cast<gig::Instrument*>( *InstrumentsIterator );
2821              InstrumentsIterator++;              InstrumentsIterator++;
2822          }          }
2823          return NULL;          return NULL;
# Line 2862  namespace { Line 2833  namespace {
2833      Instrument* File::AddInstrument() {      Instrument* File::AddInstrument() {
2834         if (!pInstruments) LoadInstruments();         if (!pInstruments) LoadInstruments();
2835         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
        if (!pInstruments) pInstruments = new InstrumentList;  
2836         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2837         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
2838         Instrument* pInstrument = new Instrument(this, lstInstr);         Instrument* pInstrument = new Instrument(this, lstInstr);
# Line 2880  namespace { Line 2850  namespace {
2850       */       */
2851      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
2852          if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments");          if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments");
2853          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), (DLS::Instrument*) pInstrument);
2854          if (iter == pInstruments->end()) throw gig::Exception("Could not delete instrument, could not find given instrument");          if (iter == pInstruments->end()) throw gig::Exception("Could not delete instrument, could not find given instrument");
2855          pInstruments->erase(iter);          pInstruments->erase(iter);
2856          delete pInstrument;          delete pInstrument;
2857      }      }
2858    
2859        void File::LoadInstruments() {
2860            LoadInstruments(NULL);
2861        }
2862    
2863      void File::LoadInstruments(progress_t* pProgress) {      void File::LoadInstruments(progress_t* pProgress) {
2864            if (!pInstruments) pInstruments = new InstrumentList;
2865          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2866          if (lstInstruments) {          if (lstInstruments) {
2867              int iInstrumentIndex = 0;              int iInstrumentIndex = 0;
# Line 2901  namespace { Line 2876  namespace {
2876                      progress_t subprogress;                      progress_t subprogress;
2877                      __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);                      __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);
2878    
                     if (!pInstruments) pInstruments = new InstrumentList;  
2879                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));
2880    
2881                      iInstrumentIndex++;                      iInstrumentIndex++;
# Line 2910  namespace { Line 2884  namespace {
2884              }              }
2885              __notify_progress(pProgress, 1.0); // notify done              __notify_progress(pProgress, 1.0); // notify done
2886          }          }
         else throw gig::Exception("Mandatory <lins> list chunk not found.");  
2887      }      }
2888    
2889    

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

  ViewVC Help
Powered by ViewVC