/[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 352 by schoenebeck, Sat Jan 29 14:24:11 2005 UTC revision 372 by persson, Fri Feb 11 18:58:07 2005 UTC
# Line 23  Line 23 
23    
24  #include "gig.h"  #include "gig.h"
25    
26  namespace gig {  namespace gig { namespace {
27    
28    // *************** Internal functions for sample decopmression ***************
29    // *
30    
31        inline int get12lo(const unsigned char* pSrc)
32        {
33            const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8;
34            return x & 0x800 ? x - 0x1000 : x;
35        }
36    
37        inline int get12hi(const unsigned char* pSrc)
38        {
39            const int x = pSrc[1] >> 4 | pSrc[2] << 4;
40            return x & 0x800 ? x - 0x1000 : x;
41        }
42    
43        inline int16_t get16(const unsigned char* pSrc)
44        {
45            return int16_t(pSrc[0] | pSrc[1] << 8);
46        }
47    
48        inline int get24(const unsigned char* pSrc)
49        {
50            const int x = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16;
51            return x & 0x800000 ? x - 0x1000000 : x;
52        }
53    
54        void Decompress16(int compressionmode, const unsigned char* params,
55                          int srcStep, int dstStep,
56                          const unsigned char* pSrc, int16_t* pDst,
57                          unsigned long currentframeoffset,
58                          unsigned long copysamples)
59        {
60            switch (compressionmode) {
61                case 0: // 16 bit uncompressed
62                    pSrc += currentframeoffset * srcStep;
63                    while (copysamples) {
64                        *pDst = get16(pSrc);
65                        pDst += dstStep;
66                        pSrc += srcStep;
67                        copysamples--;
68                    }
69                    break;
70    
71                case 1: // 16 bit compressed to 8 bit
72                    int y  = get16(params);
73                    int dy = get16(params + 2);
74                    while (currentframeoffset) {
75                        dy -= int8_t(*pSrc);
76                        y  -= dy;
77                        pSrc += srcStep;
78                        currentframeoffset--;
79                    }
80                    while (copysamples) {
81                        dy -= int8_t(*pSrc);
82                        y  -= dy;
83                        *pDst = y;
84                        pDst += dstStep;
85                        pSrc += srcStep;
86                        copysamples--;
87                    }
88                    break;
89            }
90        }
91    
92        void Decompress24(int compressionmode, const unsigned char* params,
93                          int dstStep, const unsigned char* pSrc, int16_t* pDst,
94                          unsigned long currentframeoffset,
95                          unsigned long copysamples)
96        {
97            // Note: The 24 bits are truncated to 16 bits for now.
98    
99            // Note: The calculation of the initial value of y is strange
100            // and not 100% correct. What should the first two parameters
101            // really be used for? Why are they two? The correct value for
102            // y seems to lie somewhere between the values of the first
103            // two parameters.
104            //
105            // Strange thing #2: The formula in SKIP_ONE gives values for
106            // y that are twice as high as they should be. That's why
107            // COPY_ONE shifts 9 steps instead of 8, and also why y is
108            // initialized with a sum instead of a mean value.
109    
110            int y, dy, ddy;
111    
112    #define GET_PARAMS(params)                              \
113            y = (get24(params) + get24((params) + 3));      \
114            dy  = get24((params) + 6);                      \
115            ddy = get24((params) + 9)
116    
117    #define SKIP_ONE(x)                             \
118            ddy -= (x);                             \
119            dy -= ddy;                              \
120            y -= dy
121    
122    #define COPY_ONE(x)                             \
123            SKIP_ONE(x);                            \
124            *pDst = y >> 9;                         \
125            pDst += dstStep
126    
127            switch (compressionmode) {
128                case 2: // 24 bit uncompressed
129                    pSrc += currentframeoffset * 3;
130                    while (copysamples) {
131                        *pDst = get24(pSrc) >> 8;
132                        pDst += dstStep;
133                        pSrc += 3;
134                        copysamples--;
135                    }
136                    break;
137    
138                case 3: // 24 bit compressed to 16 bit
139                    GET_PARAMS(params);
140                    while (currentframeoffset) {
141                        SKIP_ONE(get16(pSrc));
142                        pSrc += 2;
143                        currentframeoffset--;
144                    }
145                    while (copysamples) {
146                        COPY_ONE(get16(pSrc));
147                        pSrc += 2;
148                        copysamples--;
149                    }
150                    break;
151    
152                case 4: // 24 bit compressed to 12 bit
153                    GET_PARAMS(params);
154                    while (currentframeoffset > 1) {
155                        SKIP_ONE(get12lo(pSrc));
156                        SKIP_ONE(get12hi(pSrc));
157                        pSrc += 3;
158                        currentframeoffset -= 2;
159                    }
160                    if (currentframeoffset) {
161                        SKIP_ONE(get12lo(pSrc));
162                        currentframeoffset--;
163                        if (copysamples) {
164                            COPY_ONE(get12hi(pSrc));
165                            pSrc += 3;
166                            copysamples--;
167                        }
168                    }
169                    while (copysamples > 1) {
170                        COPY_ONE(get12lo(pSrc));
171                        COPY_ONE(get12hi(pSrc));
172                        pSrc += 3;
173                        copysamples -= 2;
174                    }
175                    if (copysamples) {
176                        COPY_ONE(get12lo(pSrc));
177                    }
178                    break;
179    
180                case 5: // 24 bit compressed to 8 bit
181                    GET_PARAMS(params);
182                    while (currentframeoffset) {
183                        SKIP_ONE(int8_t(*pSrc++));
184                        currentframeoffset--;
185                    }
186                    while (copysamples) {
187                        COPY_ONE(int8_t(*pSrc++));
188                        copysamples--;
189                    }
190                    break;
191            }
192        }
193    
194        const int bytesPerFrame[] =      { 4096, 2052, 768, 524, 396, 268 };
195        const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 };
196        const int headerSize[] =         { 0, 4, 0, 12, 12, 12 };
197        const int bitsPerSample[] =      { 16, 8, 24, 16, 12, 8 };
198    }
199    
200    
201  // *************** Sample ***************  // *************** Sample ***************
202  // *  // *
203    
204      unsigned int  Sample::Instances               = 0;      unsigned int  Sample::Instances               = 0;
205      void*         Sample::pDecompressionBuffer    = NULL;      unsigned char* Sample::pDecompressionBuffer    = NULL;
206      unsigned long Sample::DecompressionBufferSize = 0;      unsigned long Sample::DecompressionBufferSize = 0;
207    
208      Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {      Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {
# Line 49  namespace gig { Line 222  namespace gig {
222          smpl->Read(&SMPTEFormat, 1, 4);          smpl->Read(&SMPTEFormat, 1, 4);
223          SMPTEOffset       = smpl->ReadInt32();          SMPTEOffset       = smpl->ReadInt32();
224          Loops             = smpl->ReadInt32();          Loops             = smpl->ReadInt32();
225          uint32_t manufByt = smpl->ReadInt32();          smpl->ReadInt32(); // manufByt
226          LoopID            = smpl->ReadInt32();          LoopID            = smpl->ReadInt32();
227          smpl->Read(&LoopType, 1, 4);          smpl->Read(&LoopType, 1, 4);
228          LoopStart         = smpl->ReadInt32();          LoopStart         = smpl->ReadInt32();
# Line 63  namespace gig { Line 236  namespace gig {
236          RAMCache.pStart            = NULL;          RAMCache.pStart            = NULL;
237          RAMCache.NullExtensionSize = 0;          RAMCache.NullExtensionSize = 0;
238    
239            if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported");
240    
241          Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV));          Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV));
242          if (Compressed) {          if (Compressed) {
243              ScanCompressedSample();              ScanCompressedSample();
244          }          }
245    
         if (BitDepth > 24)                throw gig::Exception("Only samples up to 24 bit supported");  
         if (Compressed && Channels == 1)  throw gig::Exception("Mono compressed samples not yet supported");  
         if (Compressed && BitDepth == 24) throw gig::Exception("24 bit compressed samples not yet supported");  
   
246          // we use a buffer for decompression and for truncating 24 bit samples to 16 bit          // we use a buffer for decompression and for truncating 24 bit samples to 16 bit
247          if ((Compressed || BitDepth == 24) && !pDecompressionBuffer) {          if ((Compressed || BitDepth == 24) && !pDecompressionBuffer) {
248              pDecompressionBuffer    = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE];              pDecompressionBuffer    = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE];
249              DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE;              DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE;
250          }          }
251          FrameOffset = 0; // just for streaming compressed samples          FrameOffset = 0; // just for streaming compressed samples
# Line 88  namespace gig { Line 259  namespace gig {
259          this->SamplesTotal = 0;          this->SamplesTotal = 0;
260          std::list<unsigned long> frameOffsets;          std::list<unsigned long> frameOffsets;
261    
262            SamplesPerFrame = BitDepth == 24 ? 256 : 2048;
263            WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels;
264    
265          // Scanning          // Scanning
266          pCkData->SetPos(0);          pCkData->SetPos(0);
267          while (pCkData->GetState() == RIFF::stream_ready) {          if (Channels == 2) { // Stereo
268              frameOffsets.push_back(pCkData->GetPos());              for (int i = 0 ; ; i++) {
269              int16_t compressionmode = pCkData->ReadInt16();                  // for 24 bit samples every 8:th frame offset is
270              this->SamplesTotal += 2048;                  // stored, to save some memory
271              switch (compressionmode) {                  if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos());
272                  case 1:   // left channel compressed  
273                  case 256: // right channel compressed                  const int mode_l = pCkData->ReadUint8();
274                      pCkData->SetPos(6148, RIFF::stream_curpos);                  const int mode_r = pCkData->ReadUint8();
275                    if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode");
276                    const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r];
277    
278                    if (pCkData->RemainingBytes() <= frameSize) {
279                        SamplesInLastFrame =
280                            ((pCkData->RemainingBytes() - headerSize[mode_l] - headerSize[mode_r]) << 3) /
281                            (bitsPerSample[mode_l] + bitsPerSample[mode_r]);
282                        SamplesTotal += SamplesInLastFrame;
283                      break;                      break;
284                  case 257: // both channels compressed                  }
285                      pCkData->SetPos(4104, RIFF::stream_curpos);                  SamplesTotal += SamplesPerFrame;
286                    pCkData->SetPos(frameSize, RIFF::stream_curpos);
287                }
288            }
289            else { // Mono
290                for (int i = 0 ; ; i++) {
291                    if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos());
292    
293                    const int mode = pCkData->ReadUint8();
294                    if (mode > 5) throw gig::Exception("Unknown compression mode");
295                    const unsigned long frameSize = bytesPerFrame[mode];
296    
297                    if (pCkData->RemainingBytes() <= frameSize) {
298                        SamplesInLastFrame =
299                            ((pCkData->RemainingBytes() - headerSize[mode]) << 3) / bitsPerSample[mode];
300                        SamplesTotal += SamplesInLastFrame;
301                      break;                      break;
302                  default: // both channels uncompressed                  }
303                      pCkData->SetPos(8192, RIFF::stream_curpos);                  SamplesTotal += SamplesPerFrame;
304                    pCkData->SetPos(frameSize, RIFF::stream_curpos);
305              }              }
306          }          }
307          pCkData->SetPos(0);          pCkData->SetPos(0);
308    
         //FIXME: only seen compressed samples with 16 bit stereo so far  
         this->FrameSize = 4;  
         this->BitDepth  = 16;  
   
309          // Build the frames table (which is used for fast resolving of a frame's chunk offset)          // Build the frames table (which is used for fast resolving of a frame's chunk offset)
310          if (FrameTable) delete[] FrameTable;          if (FrameTable) delete[] FrameTable;
311          FrameTable = new unsigned long[frameOffsets.size()];          FrameTable = new unsigned long[frameOffsets.size()];
# Line 511  namespace gig { Line 705  namespace gig {
705          if (!Compressed) {          if (!Compressed) {
706              if (BitDepth == 24) {              if (BitDepth == 24) {
707                  // 24 bit sample. For now just truncate to 16 bit.                  // 24 bit sample. For now just truncate to 16 bit.
708                  int8_t* pSrc = (int8_t*)this->pDecompressionBuffer;                  unsigned char* pSrc = this->pDecompressionBuffer;
709                  int8_t* pDst = (int8_t*)pBuffer;                  int16_t* pDst = static_cast<int16_t*>(pBuffer);
710                  unsigned long n = pCkData->Read(pSrc, SampleCount, FrameSize);                  if (Channels == 2) { // Stereo
711                  for (int i = SampleCount * (FrameSize / 3) ; i > 0 ; i--) {                      unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1);
712                      pSrc++;                      pSrc++;
713                      *pDst++ = *pSrc++;                      for (unsigned long i = readBytes ; i > 0 ; i -= 3) {
714                      *pDst++ = *pSrc++;                          *pDst++ = get16(pSrc);
715                            pSrc += 3;
716                        }
717                        return (pDst - static_cast<int16_t*>(pBuffer)) >> 1;
718                    }
719                    else { // Mono
720                        unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1);
721                        pSrc++;
722                        for (unsigned long i = readBytes ; i > 0 ; i -= 3) {
723                            *pDst++ = get16(pSrc);
724                            pSrc += 3;
725                        }
726                        return pDst - static_cast<int16_t*>(pBuffer);
727                  }                  }
728                  return SampleCount;              }
729              } else {              else { // 16 bit
730                  return pCkData->Read(pBuffer, SampleCount, FrameSize); //FIXME: channel inversion due to endian correction?                  // (pCkData->Read does endian correction)
731                    return Channels == 2 ? pCkData->Read(pBuffer, SampleCount << 1, 2) >> 1
732                                         : pCkData->Read(pBuffer, SampleCount, 2);
733              }              }
734          }          }
735          else { //FIXME: no support for mono compressed samples yet, are there any?          else {
736              if (this->SamplePos >= this->SamplesTotal) return 0;              if (this->SamplePos >= this->SamplesTotal) return 0;
737              //TODO: efficiency: we simply assume here that all frames are compressed, maybe we should test for an average compression rate              //TODO: efficiency: maybe we should test for an average compression rate
738              // best case needed buffer size (all frames compressed)              unsigned long assumedsize      = GuessSize(SampleCount),
             unsigned long assumedsize      = (SampleCount << 1)  + // *2 (16 Bit, stereo, but assume all frames compressed)  
                                              (SampleCount >> 10) + // 10 bytes header per 2048 sample points  
                                              8194,                 // at least one worst case sample frame  
739                            remainingbytes   = 0,           // remaining bytes in the local buffer                            remainingbytes   = 0,           // remaining bytes in the local buffer
740                            remainingsamples = SampleCount,                            remainingsamples = SampleCount,
741                            copysamples;                            copysamples, skipsamples,
742              int currentframeoffset = this->FrameOffset;   // offset in current sample frame since last Read()                            currentframeoffset = this->FrameOffset;  // offset in current sample frame since last Read()
743              this->FrameOffset = 0;              this->FrameOffset = 0;
744    
745              if (assumedsize > this->DecompressionBufferSize) {              if (assumedsize > this->DecompressionBufferSize) {
746                  // local buffer reallocation - hope this won't happen                  // local buffer reallocation - hope this won't happen
747                  if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer;                  if (this->pDecompressionBuffer) delete[] this->pDecompressionBuffer;
748                  this->pDecompressionBuffer    = new int8_t[assumedsize << 1]; // double of current needed size                  this->pDecompressionBuffer    = new unsigned char[assumedsize << 1]; // double of current needed size
749                  this->DecompressionBufferSize = assumedsize;                  this->DecompressionBufferSize = assumedsize << 1;
750              }              }
751    
752              int16_t  compressionmode, left, dleft, right, dright;              unsigned char* pSrc = this->pDecompressionBuffer;
753              int8_t*  pSrc = (int8_t*)  this->pDecompressionBuffer;              int16_t* pDst = static_cast<int16_t*>(pBuffer);
             int16_t* pDst = (int16_t*) pBuffer;  
754              remainingbytes = pCkData->Read(pSrc, assumedsize, 1);              remainingbytes = pCkData->Read(pSrc, assumedsize, 1);
755    
756              while (remainingsamples) {              while (remainingsamples && remainingbytes) {
757                    unsigned long framesamples = SamplesPerFrame;
758                  // reload from disk to local buffer if needed                  unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset;
759                  if (remainingbytes < 8194) {  
760                      if (pCkData->GetState() != RIFF::stream_ready) {                  int mode_l = *pSrc++, mode_r = 0;
761                          this->SamplePos = this->SamplesTotal;  
762                          return (SampleCount - remainingsamples);                  if (Channels == 2) {
763                        mode_r = *pSrc++;
764                        framebytes = bytesPerFrame[mode_l] + bytesPerFrame[mode_r] + 2;
765                        rightChannelOffset = bytesPerFrameNoHdr[mode_l];
766                        nextFrameOffset = rightChannelOffset + bytesPerFrameNoHdr[mode_r];
767                        if (remainingbytes < framebytes) { // last frame in sample
768                            framesamples = SamplesInLastFrame;
769                            if (mode_l == 4 && (framesamples & 1)) {
770                                rightChannelOffset = ((framesamples + 1) * bitsPerSample[mode_l]) >> 3;
771                            }
772                            else {
773                                rightChannelOffset = (framesamples * bitsPerSample[mode_l]) >> 3;
774                            }
775                        }
776                    }
777                    else {
778                        framebytes = bytesPerFrame[mode_l] + 1;
779                        nextFrameOffset = bytesPerFrameNoHdr[mode_l];
780                        if (remainingbytes < framebytes) {
781                            framesamples = SamplesInLastFrame;
782                      }                      }
                     assumedsize    = remainingsamples;  
                     assumedsize    = (assumedsize << 1)  + // *2 (16 Bit, stereo, but assume all frames compressed)  
                                      (assumedsize >> 10) + // 10 bytes header per 2048 sample points  
                                      8194;                 // at least one worst case sample frame  
                     pCkData->SetPos(remainingbytes, RIFF::stream_backward);  
                     if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes();  
                     remainingbytes = pCkData->Read(this->pDecompressionBuffer, assumedsize, 1);  
                     pSrc = (int8_t*) this->pDecompressionBuffer;  
783                  }                  }
784    
785                  // determine how many samples in this frame to skip and read                  // determine how many samples in this frame to skip and read
786                  if (remainingsamples >= 2048) {                  if (currentframeoffset + remainingsamples >= framesamples) {
787                      copysamples       = 2048 - currentframeoffset;                      if (currentframeoffset <= framesamples) {
788                      remainingsamples -= copysamples;                          copysamples = framesamples - currentframeoffset;
789                            skipsamples = currentframeoffset;
790                        }
791                        else {
792                            copysamples = 0;
793                            skipsamples = framesamples;
794                        }
795                  }                  }
796                  else {                  else {
797                        // This frame has enough data for pBuffer, but not
798                        // all of the frame is needed. Set file position
799                        // to start of this frame for next call to Read.
800                      copysamples = remainingsamples;                      copysamples = remainingsamples;
801                      if (currentframeoffset + copysamples > 2048) {                      skipsamples = currentframeoffset;
802                          copysamples = 2048 - currentframeoffset;                      pCkData->SetPos(remainingbytes, RIFF::stream_backward);
803                          remainingsamples -= copysamples;                      this->FrameOffset = currentframeoffset + copysamples;
804                      }                  }
805                      else {                  remainingsamples -= copysamples;
806    
807                    if (remainingbytes > framebytes) {
808                        remainingbytes -= framebytes;
809                        if (remainingsamples == 0 &&
810                            currentframeoffset + copysamples == framesamples) {
811                            // This frame has enough data for pBuffer, and
812                            // all of the frame is needed. Set file
813                            // position to start of next frame for next
814                            // call to Read. FrameOffset is 0.
815                          pCkData->SetPos(remainingbytes, RIFF::stream_backward);                          pCkData->SetPos(remainingbytes, RIFF::stream_backward);
                         remainingsamples = 0;  
                         this->FrameOffset = currentframeoffset + copysamples;  
816                      }                      }
817                  }                  }
818                    else remainingbytes = 0;
819    
820                  // decompress and copy current frame from local buffer to destination buffer                  currentframeoffset -= skipsamples;
821                  compressionmode = *(int16_t*)pSrc; pSrc+=2;  
822                  switch (compressionmode) {                  if (copysamples == 0) {
823                      case 1: // left channel compressed                      // skip this frame
824                          remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header)                      pSrc += framebytes - Channels;
825                          if (!remainingsamples && copysamples == 2048)                  }
826                              pCkData->SetPos(remainingbytes, RIFF::stream_backward);                  else {
827                        const unsigned char* const param_l = pSrc;
828                          left  = *(int16_t*)pSrc; pSrc+=2;                      if (BitDepth == 24) {
829                          dleft = *(int16_t*)pSrc; pSrc+=2;                          if (mode_l != 2) pSrc += 12;
830                          while (currentframeoffset) {  
831                              dleft -= *pSrc;                          if (Channels == 2) { // Stereo
832                              left  -= dleft;                              const unsigned char* const param_r = pSrc;
833                              pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit)                              if (mode_r != 2) pSrc += 12;
834                              currentframeoffset--;  
835                          }                              Decompress24(mode_l, param_l, 2, pSrc, pDst, skipsamples, copysamples);
836                          while (copysamples) {                              Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1,
837                              dleft -= *pSrc; pSrc++;                                           skipsamples, copysamples);
838                              left  -= dleft;                              pDst += copysamples << 1;
                             *pDst = left; pDst++;  
                             *pDst = *(int16_t*)pSrc; pDst++; pSrc+=2;  
                             copysamples--;  
                         }  
                         break;  
                     case 256: // right channel compressed  
                         remainingbytes -= 6150; // (left 16 bit, right 8 bit, +6 byte header)  
                         if (!remainingsamples && copysamples == 2048)  
                             pCkData->SetPos(remainingbytes, RIFF::stream_backward);  
   
                         right  = *(int16_t*)pSrc; pSrc+=2;  
                         dright = *(int16_t*)pSrc; pSrc+=2;  
                         if (currentframeoffset) {  
                             pSrc+=2; // skip uncompressed left channel, now we can increment by 3  
                             while (currentframeoffset) {  
                                 dright -= *pSrc;  
                                 right  -= dright;  
                                 pSrc+=3; // 8 bit right channel, skip uncompressed left channel (16 bit)  
                                 currentframeoffset--;  
                             }  
                             pSrc-=2; // back aligned to left channel  
839                          }                          }
840                          while (copysamples) {                          else { // Mono
841                              *pDst = *(int16_t*)pSrc; pDst++; pSrc+=2;                              Decompress24(mode_l, param_l, 1, pSrc, pDst, skipsamples, copysamples);
842                              dright -= *pSrc; pSrc++;                              pDst += copysamples;
                             right  -= dright;  
                             *pDst = right; pDst++;  
                             copysamples--;  
843                          }                          }
844                          break;                      }
845                      case 257: // both channels compressed                      else { // 16 bit
846                          remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header)                          if (mode_l) pSrc += 4;
847                          if (!remainingsamples && copysamples == 2048)  
848                              pCkData->SetPos(remainingbytes, RIFF::stream_backward);                          int step;
849                            if (Channels == 2) { // Stereo
850                          left   = *(int16_t*)pSrc; pSrc+=2;                              const unsigned char* const param_r = pSrc;
851                          dleft  = *(int16_t*)pSrc; pSrc+=2;                              if (mode_r) pSrc += 4;
852                          right  = *(int16_t*)pSrc; pSrc+=2;  
853                          dright = *(int16_t*)pSrc; pSrc+=2;                              step = (2 - mode_l) + (2 - mode_r);
854                          while (currentframeoffset) {                              Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples);
855                              dleft  -= *pSrc; pSrc++;                              Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1,
856                              left   -= dleft;                                           skipsamples, copysamples);
857                              dright -= *pSrc; pSrc++;                              pDst += copysamples << 1;
                             right  -= dright;  
                             currentframeoffset--;  
858                          }                          }
859                          while (copysamples) {                          else { // Mono
860                              dleft  -= *pSrc; pSrc++;                              step = 2 - mode_l;
861                              left   -= dleft;                              Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples);
862                              dright -= *pSrc; pSrc++;                              pDst += copysamples;
                             right  -= dright;  
                             *pDst = left;  pDst++;  
                             *pDst = right; pDst++;  
                             copysamples--;  
863                          }                          }
864                          break;                      }
865                      default: // both channels uncompressed                      pSrc += nextFrameOffset;
                         remainingbytes -= 8194; // (left 16 bit, right 16 bit, +2 byte header)  
                         if (!remainingsamples && copysamples == 2048)  
                             pCkData->SetPos(remainingbytes, RIFF::stream_backward);  
   
                         pSrc += currentframeoffset << 2;  
                         currentframeoffset = 0;  
                         memcpy(pDst, pSrc, copysamples << 2);  
                         pDst += copysamples << 1;  
                         pSrc += copysamples << 2;  
                         break;  
866                  }                  }
867              }  
868                    // reload from disk to local buffer if needed
869                    if (remainingsamples && remainingbytes < WorstCaseFrameSize && pCkData->GetState() == RIFF::stream_ready) {
870                        assumedsize    = GuessSize(remainingsamples);
871                        pCkData->SetPos(remainingbytes, RIFF::stream_backward);
872                        if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes();
873                        remainingbytes = pCkData->Read(this->pDecompressionBuffer, assumedsize, 1);
874                        pSrc = this->pDecompressionBuffer;
875                    }
876                } // while
877    
878              this->SamplePos += (SampleCount - remainingsamples);              this->SamplePos += (SampleCount - remainingsamples);
879              if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal;              if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal;
880              return (SampleCount - remainingsamples);              return (SampleCount - remainingsamples);
# Line 681  namespace gig { Line 883  namespace gig {
883    
884      Sample::~Sample() {      Sample::~Sample() {
885          Instances--;          Instances--;
886          if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer;          if (!Instances && pDecompressionBuffer) {
887                delete[] pDecompressionBuffer;
888                pDecompressionBuffer = NULL;
889            }
890          if (FrameTable) delete[] FrameTable;          if (FrameTable) delete[] FrameTable;
891          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
892      }      }
# Line 1381  namespace gig { Line 1586  namespace gig {
1586       * @see      GetFirstRegion()       * @see      GetFirstRegion()
1587       */       */
1588      Region* Instrument::GetNextRegion() {      Region* Instrument::GetNextRegion() {
1589          if (RegionIndex < 0 || RegionIndex >= Regions) return NULL;          if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL;
1590          return pRegions[RegionIndex++];          return pRegions[RegionIndex++];
1591      }      }
1592    
# Line 1404  namespace gig { Line 1609  namespace gig {
1609                  SamplesIterator++;                  SamplesIterator++;
1610              }              }
1611              pSamples->clear();              pSamples->clear();
1612                delete pSamples;
1613    
1614          }          }
1615          // free instruments          // free instruments
# Line 1414  namespace gig { Line 1620  namespace gig {
1620                  InstrumentsIterator++;                  InstrumentsIterator++;
1621              }              }
1622              pInstruments->clear();              pInstruments->clear();
1623                delete pInstruments;
1624          }          }
1625      }      }
1626    

Legend:
Removed from v.352  
changed lines
  Added in v.372

  ViewVC Help
Powered by ViewVC