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

Legend:
Removed from v.364  
changed lines
  Added in v.365

  ViewVC Help
Powered by ViewVC