/[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 1335 by schoenebeck, Sun Sep 9 21:22:58 2007 UTC revision 2543 by schoenebeck, Sat May 10 02:06:58 2014 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2007 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2014 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 25  Line 25 
25    
26  #include "helper.h"  #include "helper.h"
27    
28    #include <algorithm>
29  #include <math.h>  #include <math.h>
30  #include <iostream>  #include <iostream>
31    
# Line 255  namespace { Line 256  namespace {
256    
257    
258    
259    // *************** Internal CRC-32 (Cyclic Redundancy Check) functions  ***************
260    // *
261    
262        static uint32_t* __initCRCTable() {
263            static uint32_t res[256];
264    
265            for (int i = 0 ; i < 256 ; i++) {
266                uint32_t c = i;
267                for (int j = 0 ; j < 8 ; j++) {
268                    c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1;
269                }
270                res[i] = c;
271            }
272            return res;
273        }
274    
275        static const uint32_t* __CRCTable = __initCRCTable();
276    
277        /**
278         * Initialize a CRC variable.
279         *
280         * @param crc - variable to be initialized
281         */
282        inline static void __resetCRC(uint32_t& crc) {
283            crc = 0xffffffff;
284        }
285    
286        /**
287         * Used to calculate checksums of the sample data in a gig file. The
288         * checksums are stored in the 3crc chunk of the gig file and
289         * automatically updated when a sample is written with Sample::Write().
290         *
291         * One should call __resetCRC() to initialize the CRC variable to be
292         * used before calling this function the first time.
293         *
294         * After initializing the CRC variable one can call this function
295         * arbitrary times, i.e. to split the overall CRC calculation into
296         * steps.
297         *
298         * Once the whole data was processed by __calculateCRC(), one should
299         * call __encodeCRC() to get the final CRC result.
300         *
301         * @param buf     - pointer to data the CRC shall be calculated of
302         * @param bufSize - size of the data to be processed
303         * @param crc     - variable the CRC sum shall be stored to
304         */
305        static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) {
306            for (int i = 0 ; i < bufSize ; i++) {
307                crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);
308            }
309        }
310    
311        /**
312         * Returns the final CRC result.
313         *
314         * @param crc - variable previously passed to __calculateCRC()
315         */
316        inline static uint32_t __encodeCRC(const uint32_t& crc) {
317            return crc ^ 0xffffffff;
318        }
319    
320    
321    
322  // *************** Other Internal functions  ***************  // *************** Other Internal functions  ***************
323  // *  // *
324    
# Line 278  namespace { Line 342  namespace {
342    
343    
344    
 // *************** CRC ***************  
 // *  
   
     const uint32_t* CRC::table(initTable());  
   
     uint32_t* CRC::initTable() {  
         uint32_t* res = new uint32_t[256];  
   
         for (int i = 0 ; i < 256 ; i++) {  
             uint32_t c = i;  
             for (int j = 0 ; j < 8 ; j++) {  
                 c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1;  
             }  
             res[i] = c;  
         }  
         return res;  
     }  
   
   
   
345  // *************** Sample ***************  // *************** Sample ***************
346  // *  // *
347    
# Line 323  namespace { Line 367  namespace {
367       *                         is located, 0 otherwise       *                         is located, 0 otherwise
368       */       */
369      Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {      Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {
370          static const DLS::Info::FixedStringLength fixedStringLengths[] = {          static const DLS::Info::string_length_t fixedStringLengths[] = {
371              { CHUNK_ID_INAM, 64 },              { CHUNK_ID_INAM, 64 },
372              { 0, 0 }              { 0, 0 }
373          };          };
374          pInfo->FixedStringLengths = fixedStringLengths;          pInfo->SetFixedStringLengths(fixedStringLengths);
375          Instances++;          Instances++;
376          FileNo = fileNo;          FileNo = fileNo;
377    
378            __resetCRC(crc);
379    
380          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);
381          if (pCk3gix) {          if (pCk3gix) {
382              uint16_t iSampleGroup = pCk3gix->ReadInt16();              uint16_t iSampleGroup = pCk3gix->ReadInt16();
# Line 408  namespace { Line 454  namespace {
454      }      }
455    
456      /**      /**
457         * Make a (semi) deep copy of the Sample object given by @a orig (without
458         * the actual waveform data) and assign it to this object.
459         *
460         * Discussion: copying .gig samples is a bit tricky. It requires three
461         * steps:
462         * 1. Copy sample's meta informations (done by CopyAssignMeta()) including
463         *    its new sample waveform data size.
464         * 2. Saving the file (done by File::Save()) so that it gains correct size
465         *    and layout for writing the actual wave form data directly to disc
466         *    in next step.
467         * 3. Copy the waveform data with disk streaming (done by CopyAssignWave()).
468         *
469         * @param orig - original Sample object to be copied from
470         */
471        void Sample::CopyAssignMeta(const Sample* orig) {
472            // handle base classes
473            DLS::Sample::CopyAssignCore(orig);
474            
475            // handle actual own attributes of this class
476            Manufacturer = orig->Manufacturer;
477            Product = orig->Product;
478            SamplePeriod = orig->SamplePeriod;
479            MIDIUnityNote = orig->MIDIUnityNote;
480            FineTune = orig->FineTune;
481            SMPTEFormat = orig->SMPTEFormat;
482            SMPTEOffset = orig->SMPTEOffset;
483            Loops = orig->Loops;
484            LoopID = orig->LoopID;
485            LoopType = orig->LoopType;
486            LoopStart = orig->LoopStart;
487            LoopEnd = orig->LoopEnd;
488            LoopSize = orig->LoopSize;
489            LoopFraction = orig->LoopFraction;
490            LoopPlayCount = orig->LoopPlayCount;
491            
492            // schedule resizing this sample to the given sample's size
493            Resize(orig->GetSize());
494        }
495    
496        /**
497         * Should be called after CopyAssignMeta() and File::Save() sequence.
498         * Read more about it in the discussion of CopyAssignMeta(). This method
499         * copies the actual waveform data by disk streaming.
500         *
501         * @e CAUTION: this method is currently not thread safe! During this
502         * operation the sample must not be used for other purposes by other
503         * threads!
504         *
505         * @param orig - original Sample object to be copied from
506         */
507        void Sample::CopyAssignWave(const Sample* orig) {
508            const int iReadAtOnce = 32*1024;
509            char* buf = new char[iReadAtOnce * orig->FrameSize];
510            Sample* pOrig = (Sample*) orig; //HACK: remove constness for now
511            unsigned long restorePos = pOrig->GetPos();
512            pOrig->SetPos(0);
513            SetPos(0);
514            for (unsigned long n = pOrig->Read(buf, iReadAtOnce); n;
515                               n = pOrig->Read(buf, iReadAtOnce))
516            {
517                Write(buf, n);
518            }
519            pOrig->SetPos(restorePos);
520            delete [] buf;
521        }
522    
523        /**
524       * Apply sample and its settings to the respective RIFF chunks. You have       * Apply sample and its settings to the respective RIFF chunks. You have
525       * to call File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
526       *       *
# Line 468  namespace { Line 581  namespace {
581          // update '3gix' chunk          // update '3gix' chunk
582          pData = (uint8_t*) pCk3gix->LoadChunkData();          pData = (uint8_t*) pCk3gix->LoadChunkData();
583          store16(&pData[0], iSampleGroup);          store16(&pData[0], iSampleGroup);
584    
585            // if the library user toggled the "Compressed" attribute from true to
586            // false, then the EWAV chunk associated with compressed samples needs
587            // to be deleted
588            RIFF::Chunk* ewav = pWaveList->GetSubChunk(CHUNK_ID_EWAV);
589            if (ewav && !Compressed) {
590                pWaveList->DeleteSubChunk(ewav);
591            }
592      }      }
593    
594      /// Scans compressed samples for mandatory informations (e.g. actual number of total sample points).      /// Scans compressed samples for mandatory informations (e.g. actual number of total sample points).
# Line 631  namespace { Line 752  namespace {
752          if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal;          if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal;
753          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
754          unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize;          unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize;
755            SetPos(0); // reset read position to begin of sample
756          RAMCache.pStart            = new int8_t[allocationsize];          RAMCache.pStart            = new int8_t[allocationsize];
757          RAMCache.Size              = Read(RAMCache.pStart, SampleCount) * this->FrameSize;          RAMCache.Size              = Read(RAMCache.pStart, SampleCount) * this->FrameSize;
758          RAMCache.NullExtensionSize = allocationsize - RAMCache.Size;          RAMCache.NullExtensionSize = allocationsize - RAMCache.Size;
# Line 668  namespace { Line 790  namespace {
790          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
791          RAMCache.pStart = NULL;          RAMCache.pStart = NULL;
792          RAMCache.Size   = 0;          RAMCache.Size   = 0;
793            RAMCache.NullExtensionSize = 0;
794      }      }
795    
796      /** @brief Resize sample.      /** @brief Resize sample.
# Line 760  namespace { Line 883  namespace {
883      /**      /**
884       * Returns the current position in the sample (in sample points).       * Returns the current position in the sample (in sample points).
885       */       */
886      unsigned long Sample::GetPos() {      unsigned long Sample::GetPos() const {
887          if (Compressed) return SamplePos;          if (Compressed) return SamplePos;
888          else            return pCkData->GetPos() / FrameSize;          else            return pCkData->GetPos() / FrameSize;
889      }      }
# Line 862  namespace { Line 985  namespace {
985                                  }                                  }
986    
987                                  // reverse the sample frames for backward playback                                  // reverse the sample frames for backward playback
988                                  SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);                                  if (totalreadsamples > swapareastart) //FIXME: this if() is just a crash workaround for now (#102), but totalreadsamples <= swapareastart should never be the case, so there's probably still a bug above!
989                                        SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
990                              }                              }
991                          } while (samplestoread && readsamples);                          } while (samplestoread && readsamples);
992                          break;                          break;
# Line 1168  namespace { Line 1292  namespace {
1292          // if this is the first write in this sample, reset the          // if this is the first write in this sample, reset the
1293          // checksum calculator          // checksum calculator
1294          if (pCkData->GetPos() == 0) {          if (pCkData->GetPos() == 0) {
1295              crc.reset();              __resetCRC(crc);
1296          }          }
1297          if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");          if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");
1298          unsigned long res;          unsigned long res;
# Line 1178  namespace { Line 1302  namespace {
1302              res = Channels == 2 ? pCkData->Write(pBuffer, SampleCount << 1, 2) >> 1              res = Channels == 2 ? pCkData->Write(pBuffer, SampleCount << 1, 2) >> 1
1303                                  : pCkData->Write(pBuffer, SampleCount, 2);                                  : pCkData->Write(pBuffer, SampleCount, 2);
1304          }          }
1305          crc.update((unsigned char *)pBuffer, SampleCount * FrameSize);          __calculateCRC((unsigned char *)pBuffer, SampleCount * FrameSize, crc);
1306    
1307          // if this is the last write, update the checksum chunk in the          // if this is the last write, update the checksum chunk in the
1308          // file          // file
1309          if (pCkData->GetPos() == pCkData->GetSize()) {          if (pCkData->GetPos() == pCkData->GetSize()) {
1310              File* pFile = static_cast<File*>(GetParent());              File* pFile = static_cast<File*>(GetParent());
1311              pFile->SetSampleChecksum(this, crc.getValue());              pFile->SetSampleChecksum(this, __encodeCRC(crc));
1312          }          }
1313          return res;          return res;
1314      }      }
# Line 1384  namespace { Line 1508  namespace {
1508                                                          : vcf_res_ctrl_none;                                                          : vcf_res_ctrl_none;
1509              uint16_t eg3depth = _3ewa->ReadUint16();              uint16_t eg3depth = _3ewa->ReadUint16();
1510              EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */              EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */
1511                                          : (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */                                          : (-1) * (int16_t) ((eg3depth ^ 0xfff) + 1); /* binary complementary for negatives */
1512              _3ewa->ReadInt16(); // unknown              _3ewa->ReadInt16(); // unknown
1513              ChannelOffset = _3ewa->ReadUint8() / 4;              ChannelOffset = _3ewa->ReadUint8() / 4;
1514              uint8_t regoptions = _3ewa->ReadUint8();              uint8_t regoptions = _3ewa->ReadUint8();
# Line 1512  namespace { Line 1636  namespace {
1636                                                       VelocityResponseDepth,                                                       VelocityResponseDepth,
1637                                                       VelocityResponseCurveScaling);                                                       VelocityResponseCurveScaling);
1638    
1639          curve_type_t curveType = ReleaseVelocityResponseCurve;          pVelocityReleaseTable = GetReleaseVelocityTable(
1640          uint8_t depth = ReleaseVelocityResponseDepth;                                      ReleaseVelocityResponseCurve,
1641                                        ReleaseVelocityResponseDepth
1642          // this models a strange behaviour or bug in GSt: two of the                                  );
1643          // velocity response curves for release time are not used even  
1644          // if specified, instead another curve is chosen.          pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve,
1645          if ((curveType == curve_type_nonlinear && depth == 0) ||                                                        VCFVelocityDynamicRange,
1646              (curveType == curve_type_special   && depth == 4)) {                                                        VCFVelocityScale,
1647              curveType = curve_type_nonlinear;                                                        VCFCutoffController);
             depth = 3;  
         }  
         pVelocityReleaseTable = GetVelocityTable(curveType, depth, 0);  
   
         curveType = VCFVelocityCurve;  
         depth = VCFVelocityDynamicRange;  
   
         // even stranger GSt: two of the velocity response curves for  
         // filter cutoff are not used, instead another special curve  
         // is chosen. This curve is not used anywhere else.  
         if ((curveType == curve_type_nonlinear && depth == 0) ||  
             (curveType == curve_type_special   && depth == 4)) {  
             curveType = curve_type_special;  
             depth = 5;  
         }  
         pVelocityCutoffTable = GetVelocityTable(curveType, depth,  
                                                 VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0);  
1648    
1649          SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));          SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));
1650          VelocityTable = 0;          VelocityTable = 0;
# Line 1549  namespace { Line 1656  namespace {
1656       */       */
1657      DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) {      DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) {
1658          Instances++;          Instances++;
1659            //NOTE: I think we cannot call CopyAssign() here (in a constructor) as long as its a virtual method
1660          *this = src; // default memberwise shallow copy of all parameters          *this = src; // default memberwise shallow copy of all parameters
1661          pParentList = _3ewl; // restore the chunk pointer          pParentList = _3ewl; // restore the chunk pointer
1662    
# Line 1564  namespace { Line 1672  namespace {
1672                  pSampleLoops[k] = src.pSampleLoops[k];                  pSampleLoops[k] = src.pSampleLoops[k];
1673          }          }
1674      }      }
1675        
1676        /**
1677         * Make a (semi) deep copy of the DimensionRegion object given by @a orig
1678         * and assign it to this object.
1679         *
1680         * Note that all sample pointers referenced by @a orig are simply copied as
1681         * memory address. Thus the respective samples are shared, not duplicated!
1682         *
1683         * @param orig - original DimensionRegion object to be copied from
1684         */
1685        void DimensionRegion::CopyAssign(const DimensionRegion* orig) {
1686            CopyAssign(orig, NULL);
1687        }
1688    
1689        /**
1690         * Make a (semi) deep copy of the DimensionRegion object given by @a orig
1691         * and assign it to this object.
1692         *
1693         * @param orig - original DimensionRegion object to be copied from
1694         * @param mSamples - crosslink map between the foreign file's samples and
1695         *                   this file's samples
1696         */
1697        void DimensionRegion::CopyAssign(const DimensionRegion* orig, const std::map<Sample*,Sample*>* mSamples) {
1698            // delete all allocated data first
1699            if (VelocityTable) delete [] VelocityTable;
1700            if (pSampleLoops) delete [] pSampleLoops;
1701            
1702            // backup parent list pointer
1703            RIFF::List* p = pParentList;
1704            
1705            gig::Sample* pOriginalSample = pSample;
1706            gig::Region* pOriginalRegion = pRegion;
1707            
1708            //NOTE: copy code copied from assignment constructor above, see comment there as well
1709            
1710            *this = *orig; // default memberwise shallow copy of all parameters
1711            pParentList = p; // restore the chunk pointer
1712            
1713            // only take the raw sample reference & parent region reference if the
1714            // two DimensionRegion objects are part of the same file
1715            if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) {
1716                pRegion = pOriginalRegion;
1717                pSample = pOriginalSample;
1718            }
1719            
1720            if (mSamples && mSamples->count(orig->pSample)) {
1721                pSample = mSamples->find(orig->pSample)->second;
1722            }
1723    
1724            // deep copy of owned structures
1725            if (orig->VelocityTable) {
1726                VelocityTable = new uint8_t[128];
1727                for (int k = 0 ; k < 128 ; k++)
1728                    VelocityTable[k] = orig->VelocityTable[k];
1729            }
1730            if (orig->pSampleLoops) {
1731                pSampleLoops = new DLS::sample_loop_t[orig->SampleLoops];
1732                for (int k = 0 ; k < orig->SampleLoops ; k++)
1733                    pSampleLoops[k] = orig->pSampleLoops[k];
1734            }
1735        }
1736    
1737        /**
1738         * Updates the respective member variable and updates @c SampleAttenuation
1739         * which depends on this value.
1740         */
1741        void DimensionRegion::SetGain(int32_t gain) {
1742            DLS::Sampler::SetGain(gain);
1743            SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));
1744        }
1745    
1746      /**      /**
1747       * Apply dimension region settings to the respective RIFF chunks. You       * Apply dimension region settings to the respective RIFF chunks. You
# Line 1796  namespace { Line 1974  namespace {
1974          }          }
1975    
1976          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth
1977                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xfff); /* binary complementary for negatives */
1978          pData[116] = eg3depth;          store16(&pData[116], eg3depth);
1979    
1980          // next 2 bytes unknown          // next 2 bytes unknown
1981    
# Line 1844  namespace { Line 2022  namespace {
2022                                        (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */                                        (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */
2023          pData[137] = vcfbreakpoint;          pData[137] = vcfbreakpoint;
2024    
2025          const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 |          const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 +
2026                                      VCFVelocityCurve * 5;                                      VCFVelocityCurve * 5;
2027          pData[138] = vcfvelocity;          pData[138] = vcfvelocity;
2028    
# Line 1856  namespace { Line 2034  namespace {
2034          }          }
2035      }      }
2036    
2037        double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) {
2038            curve_type_t curveType = releaseVelocityResponseCurve;
2039            uint8_t depth = releaseVelocityResponseDepth;
2040            // this models a strange behaviour or bug in GSt: two of the
2041            // velocity response curves for release time are not used even
2042            // if specified, instead another curve is chosen.
2043            if ((curveType == curve_type_nonlinear && depth == 0) ||
2044                (curveType == curve_type_special   && depth == 4)) {
2045                curveType = curve_type_nonlinear;
2046                depth = 3;
2047            }
2048            return GetVelocityTable(curveType, depth, 0);
2049        }
2050    
2051        double* DimensionRegion::GetCutoffVelocityTable(curve_type_t vcfVelocityCurve,
2052                                                        uint8_t vcfVelocityDynamicRange,
2053                                                        uint8_t vcfVelocityScale,
2054                                                        vcf_cutoff_ctrl_t vcfCutoffController)
2055        {
2056            curve_type_t curveType = vcfVelocityCurve;
2057            uint8_t depth = vcfVelocityDynamicRange;
2058            // even stranger GSt: two of the velocity response curves for
2059            // filter cutoff are not used, instead another special curve
2060            // is chosen. This curve is not used anywhere else.
2061            if ((curveType == curve_type_nonlinear && depth == 0) ||
2062                (curveType == curve_type_special   && depth == 4)) {
2063                curveType = curve_type_special;
2064                depth = 5;
2065            }
2066            return GetVelocityTable(curveType, depth,
2067                                    (vcfCutoffController <= vcf_cutoff_ctrl_none2)
2068                                        ? vcfVelocityScale : 0);
2069        }
2070    
2071      // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet      // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet
2072      double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling)      double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling)
2073      {      {
# Line 1875  namespace { Line 2087  namespace {
2087          return pRegion;          return pRegion;
2088      }      }
2089    
2090    // show error if some _lev_ctrl_* enum entry is not listed in the following function
2091    // (commented out for now, because "diagnostic push" not supported prior GCC 4.6)
2092    // TODO: uncomment and add a GCC version check (see also commented "#pragma GCC diagnostic pop" below)
2093    //#pragma GCC diagnostic push
2094    //#pragma GCC diagnostic error "-Wswitch"
2095    
2096      leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {      leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {
2097          leverage_ctrl_t decodedcontroller;          leverage_ctrl_t decodedcontroller;
2098          switch (EncodedController) {          switch (EncodedController) {
# Line 1986  namespace { Line 2204  namespace {
2204                  decodedcontroller.controller_number = 95;                  decodedcontroller.controller_number = 95;
2205                  break;                  break;
2206    
2207                // format extension (these controllers are so far only supported by
2208                // LinuxSampler & gigedit) they will *NOT* work with
2209                // Gigasampler/GigaStudio !
2210                case _lev_ctrl_CC3_EXT:
2211                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2212                    decodedcontroller.controller_number = 3;
2213                    break;
2214                case _lev_ctrl_CC6_EXT:
2215                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2216                    decodedcontroller.controller_number = 6;
2217                    break;
2218                case _lev_ctrl_CC7_EXT:
2219                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2220                    decodedcontroller.controller_number = 7;
2221                    break;
2222                case _lev_ctrl_CC8_EXT:
2223                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2224                    decodedcontroller.controller_number = 8;
2225                    break;
2226                case _lev_ctrl_CC9_EXT:
2227                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2228                    decodedcontroller.controller_number = 9;
2229                    break;
2230                case _lev_ctrl_CC10_EXT:
2231                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2232                    decodedcontroller.controller_number = 10;
2233                    break;
2234                case _lev_ctrl_CC11_EXT:
2235                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2236                    decodedcontroller.controller_number = 11;
2237                    break;
2238                case _lev_ctrl_CC14_EXT:
2239                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2240                    decodedcontroller.controller_number = 14;
2241                    break;
2242                case _lev_ctrl_CC15_EXT:
2243                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2244                    decodedcontroller.controller_number = 15;
2245                    break;
2246                case _lev_ctrl_CC20_EXT:
2247                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2248                    decodedcontroller.controller_number = 20;
2249                    break;
2250                case _lev_ctrl_CC21_EXT:
2251                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2252                    decodedcontroller.controller_number = 21;
2253                    break;
2254                case _lev_ctrl_CC22_EXT:
2255                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2256                    decodedcontroller.controller_number = 22;
2257                    break;
2258                case _lev_ctrl_CC23_EXT:
2259                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2260                    decodedcontroller.controller_number = 23;
2261                    break;
2262                case _lev_ctrl_CC24_EXT:
2263                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2264                    decodedcontroller.controller_number = 24;
2265                    break;
2266                case _lev_ctrl_CC25_EXT:
2267                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2268                    decodedcontroller.controller_number = 25;
2269                    break;
2270                case _lev_ctrl_CC26_EXT:
2271                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2272                    decodedcontroller.controller_number = 26;
2273                    break;
2274                case _lev_ctrl_CC27_EXT:
2275                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2276                    decodedcontroller.controller_number = 27;
2277                    break;
2278                case _lev_ctrl_CC28_EXT:
2279                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2280                    decodedcontroller.controller_number = 28;
2281                    break;
2282                case _lev_ctrl_CC29_EXT:
2283                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2284                    decodedcontroller.controller_number = 29;
2285                    break;
2286                case _lev_ctrl_CC30_EXT:
2287                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2288                    decodedcontroller.controller_number = 30;
2289                    break;
2290                case _lev_ctrl_CC31_EXT:
2291                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2292                    decodedcontroller.controller_number = 31;
2293                    break;
2294                case _lev_ctrl_CC68_EXT:
2295                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2296                    decodedcontroller.controller_number = 68;
2297                    break;
2298                case _lev_ctrl_CC69_EXT:
2299                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2300                    decodedcontroller.controller_number = 69;
2301                    break;
2302                case _lev_ctrl_CC70_EXT:
2303                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2304                    decodedcontroller.controller_number = 70;
2305                    break;
2306                case _lev_ctrl_CC71_EXT:
2307                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2308                    decodedcontroller.controller_number = 71;
2309                    break;
2310                case _lev_ctrl_CC72_EXT:
2311                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2312                    decodedcontroller.controller_number = 72;
2313                    break;
2314                case _lev_ctrl_CC73_EXT:
2315                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2316                    decodedcontroller.controller_number = 73;
2317                    break;
2318                case _lev_ctrl_CC74_EXT:
2319                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2320                    decodedcontroller.controller_number = 74;
2321                    break;
2322                case _lev_ctrl_CC75_EXT:
2323                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2324                    decodedcontroller.controller_number = 75;
2325                    break;
2326                case _lev_ctrl_CC76_EXT:
2327                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2328                    decodedcontroller.controller_number = 76;
2329                    break;
2330                case _lev_ctrl_CC77_EXT:
2331                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2332                    decodedcontroller.controller_number = 77;
2333                    break;
2334                case _lev_ctrl_CC78_EXT:
2335                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2336                    decodedcontroller.controller_number = 78;
2337                    break;
2338                case _lev_ctrl_CC79_EXT:
2339                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2340                    decodedcontroller.controller_number = 79;
2341                    break;
2342                case _lev_ctrl_CC84_EXT:
2343                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2344                    decodedcontroller.controller_number = 84;
2345                    break;
2346                case _lev_ctrl_CC85_EXT:
2347                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2348                    decodedcontroller.controller_number = 85;
2349                    break;
2350                case _lev_ctrl_CC86_EXT:
2351                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2352                    decodedcontroller.controller_number = 86;
2353                    break;
2354                case _lev_ctrl_CC87_EXT:
2355                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2356                    decodedcontroller.controller_number = 87;
2357                    break;
2358                case _lev_ctrl_CC89_EXT:
2359                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2360                    decodedcontroller.controller_number = 89;
2361                    break;
2362                case _lev_ctrl_CC90_EXT:
2363                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2364                    decodedcontroller.controller_number = 90;
2365                    break;
2366                case _lev_ctrl_CC96_EXT:
2367                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2368                    decodedcontroller.controller_number = 96;
2369                    break;
2370                case _lev_ctrl_CC97_EXT:
2371                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2372                    decodedcontroller.controller_number = 97;
2373                    break;
2374                case _lev_ctrl_CC102_EXT:
2375                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2376                    decodedcontroller.controller_number = 102;
2377                    break;
2378                case _lev_ctrl_CC103_EXT:
2379                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2380                    decodedcontroller.controller_number = 103;
2381                    break;
2382                case _lev_ctrl_CC104_EXT:
2383                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2384                    decodedcontroller.controller_number = 104;
2385                    break;
2386                case _lev_ctrl_CC105_EXT:
2387                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2388                    decodedcontroller.controller_number = 105;
2389                    break;
2390                case _lev_ctrl_CC106_EXT:
2391                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2392                    decodedcontroller.controller_number = 106;
2393                    break;
2394                case _lev_ctrl_CC107_EXT:
2395                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2396                    decodedcontroller.controller_number = 107;
2397                    break;
2398                case _lev_ctrl_CC108_EXT:
2399                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2400                    decodedcontroller.controller_number = 108;
2401                    break;
2402                case _lev_ctrl_CC109_EXT:
2403                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2404                    decodedcontroller.controller_number = 109;
2405                    break;
2406                case _lev_ctrl_CC110_EXT:
2407                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2408                    decodedcontroller.controller_number = 110;
2409                    break;
2410                case _lev_ctrl_CC111_EXT:
2411                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2412                    decodedcontroller.controller_number = 111;
2413                    break;
2414                case _lev_ctrl_CC112_EXT:
2415                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2416                    decodedcontroller.controller_number = 112;
2417                    break;
2418                case _lev_ctrl_CC113_EXT:
2419                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2420                    decodedcontroller.controller_number = 113;
2421                    break;
2422                case _lev_ctrl_CC114_EXT:
2423                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2424                    decodedcontroller.controller_number = 114;
2425                    break;
2426                case _lev_ctrl_CC115_EXT:
2427                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2428                    decodedcontroller.controller_number = 115;
2429                    break;
2430                case _lev_ctrl_CC116_EXT:
2431                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2432                    decodedcontroller.controller_number = 116;
2433                    break;
2434                case _lev_ctrl_CC117_EXT:
2435                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2436                    decodedcontroller.controller_number = 117;
2437                    break;
2438                case _lev_ctrl_CC118_EXT:
2439                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2440                    decodedcontroller.controller_number = 118;
2441                    break;
2442                case _lev_ctrl_CC119_EXT:
2443                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2444                    decodedcontroller.controller_number = 119;
2445                    break;
2446    
2447              // unknown controller type              // unknown controller type
2448              default:              default:
2449                  throw gig::Exception("Unknown leverage controller type.");                  throw gig::Exception("Unknown leverage controller type.");
2450          }          }
2451          return decodedcontroller;          return decodedcontroller;
2452      }      }
2453        
2454    // see above (diagnostic push not supported prior GCC 4.6)
2455    //#pragma GCC diagnostic pop
2456    
2457      DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {      DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {
2458          _lev_ctrl_t encodedcontroller;          _lev_ctrl_t encodedcontroller;
# Line 2079  namespace { Line 2540  namespace {
2540                      case 95:                      case 95:
2541                          encodedcontroller = _lev_ctrl_effect5depth;                          encodedcontroller = _lev_ctrl_effect5depth;
2542                          break;                          break;
2543    
2544                        // format extension (these controllers are so far only
2545                        // supported by LinuxSampler & gigedit) they will *NOT*
2546                        // work with Gigasampler/GigaStudio !
2547                        case 3:
2548                            encodedcontroller = _lev_ctrl_CC3_EXT;
2549                            break;
2550                        case 6:
2551                            encodedcontroller = _lev_ctrl_CC6_EXT;
2552                            break;
2553                        case 7:
2554                            encodedcontroller = _lev_ctrl_CC7_EXT;
2555                            break;
2556                        case 8:
2557                            encodedcontroller = _lev_ctrl_CC8_EXT;
2558                            break;
2559                        case 9:
2560                            encodedcontroller = _lev_ctrl_CC9_EXT;
2561                            break;
2562                        case 10:
2563                            encodedcontroller = _lev_ctrl_CC10_EXT;
2564                            break;
2565                        case 11:
2566                            encodedcontroller = _lev_ctrl_CC11_EXT;
2567                            break;
2568                        case 14:
2569                            encodedcontroller = _lev_ctrl_CC14_EXT;
2570                            break;
2571                        case 15:
2572                            encodedcontroller = _lev_ctrl_CC15_EXT;
2573                            break;
2574                        case 20:
2575                            encodedcontroller = _lev_ctrl_CC20_EXT;
2576                            break;
2577                        case 21:
2578                            encodedcontroller = _lev_ctrl_CC21_EXT;
2579                            break;
2580                        case 22:
2581                            encodedcontroller = _lev_ctrl_CC22_EXT;
2582                            break;
2583                        case 23:
2584                            encodedcontroller = _lev_ctrl_CC23_EXT;
2585                            break;
2586                        case 24:
2587                            encodedcontroller = _lev_ctrl_CC24_EXT;
2588                            break;
2589                        case 25:
2590                            encodedcontroller = _lev_ctrl_CC25_EXT;
2591                            break;
2592                        case 26:
2593                            encodedcontroller = _lev_ctrl_CC26_EXT;
2594                            break;
2595                        case 27:
2596                            encodedcontroller = _lev_ctrl_CC27_EXT;
2597                            break;
2598                        case 28:
2599                            encodedcontroller = _lev_ctrl_CC28_EXT;
2600                            break;
2601                        case 29:
2602                            encodedcontroller = _lev_ctrl_CC29_EXT;
2603                            break;
2604                        case 30:
2605                            encodedcontroller = _lev_ctrl_CC30_EXT;
2606                            break;
2607                        case 31:
2608                            encodedcontroller = _lev_ctrl_CC31_EXT;
2609                            break;
2610                        case 68:
2611                            encodedcontroller = _lev_ctrl_CC68_EXT;
2612                            break;
2613                        case 69:
2614                            encodedcontroller = _lev_ctrl_CC69_EXT;
2615                            break;
2616                        case 70:
2617                            encodedcontroller = _lev_ctrl_CC70_EXT;
2618                            break;
2619                        case 71:
2620                            encodedcontroller = _lev_ctrl_CC71_EXT;
2621                            break;
2622                        case 72:
2623                            encodedcontroller = _lev_ctrl_CC72_EXT;
2624                            break;
2625                        case 73:
2626                            encodedcontroller = _lev_ctrl_CC73_EXT;
2627                            break;
2628                        case 74:
2629                            encodedcontroller = _lev_ctrl_CC74_EXT;
2630                            break;
2631                        case 75:
2632                            encodedcontroller = _lev_ctrl_CC75_EXT;
2633                            break;
2634                        case 76:
2635                            encodedcontroller = _lev_ctrl_CC76_EXT;
2636                            break;
2637                        case 77:
2638                            encodedcontroller = _lev_ctrl_CC77_EXT;
2639                            break;
2640                        case 78:
2641                            encodedcontroller = _lev_ctrl_CC78_EXT;
2642                            break;
2643                        case 79:
2644                            encodedcontroller = _lev_ctrl_CC79_EXT;
2645                            break;
2646                        case 84:
2647                            encodedcontroller = _lev_ctrl_CC84_EXT;
2648                            break;
2649                        case 85:
2650                            encodedcontroller = _lev_ctrl_CC85_EXT;
2651                            break;
2652                        case 86:
2653                            encodedcontroller = _lev_ctrl_CC86_EXT;
2654                            break;
2655                        case 87:
2656                            encodedcontroller = _lev_ctrl_CC87_EXT;
2657                            break;
2658                        case 89:
2659                            encodedcontroller = _lev_ctrl_CC89_EXT;
2660                            break;
2661                        case 90:
2662                            encodedcontroller = _lev_ctrl_CC90_EXT;
2663                            break;
2664                        case 96:
2665                            encodedcontroller = _lev_ctrl_CC96_EXT;
2666                            break;
2667                        case 97:
2668                            encodedcontroller = _lev_ctrl_CC97_EXT;
2669                            break;
2670                        case 102:
2671                            encodedcontroller = _lev_ctrl_CC102_EXT;
2672                            break;
2673                        case 103:
2674                            encodedcontroller = _lev_ctrl_CC103_EXT;
2675                            break;
2676                        case 104:
2677                            encodedcontroller = _lev_ctrl_CC104_EXT;
2678                            break;
2679                        case 105:
2680                            encodedcontroller = _lev_ctrl_CC105_EXT;
2681                            break;
2682                        case 106:
2683                            encodedcontroller = _lev_ctrl_CC106_EXT;
2684                            break;
2685                        case 107:
2686                            encodedcontroller = _lev_ctrl_CC107_EXT;
2687                            break;
2688                        case 108:
2689                            encodedcontroller = _lev_ctrl_CC108_EXT;
2690                            break;
2691                        case 109:
2692                            encodedcontroller = _lev_ctrl_CC109_EXT;
2693                            break;
2694                        case 110:
2695                            encodedcontroller = _lev_ctrl_CC110_EXT;
2696                            break;
2697                        case 111:
2698                            encodedcontroller = _lev_ctrl_CC111_EXT;
2699                            break;
2700                        case 112:
2701                            encodedcontroller = _lev_ctrl_CC112_EXT;
2702                            break;
2703                        case 113:
2704                            encodedcontroller = _lev_ctrl_CC113_EXT;
2705                            break;
2706                        case 114:
2707                            encodedcontroller = _lev_ctrl_CC114_EXT;
2708                            break;
2709                        case 115:
2710                            encodedcontroller = _lev_ctrl_CC115_EXT;
2711                            break;
2712                        case 116:
2713                            encodedcontroller = _lev_ctrl_CC116_EXT;
2714                            break;
2715                        case 117:
2716                            encodedcontroller = _lev_ctrl_CC117_EXT;
2717                            break;
2718                        case 118:
2719                            encodedcontroller = _lev_ctrl_CC118_EXT;
2720                            break;
2721                        case 119:
2722                            encodedcontroller = _lev_ctrl_CC119_EXT;
2723                            break;
2724    
2725                      default:                      default:
2726                          throw gig::Exception("leverage controller number is not supported by the gig format");                          throw gig::Exception("leverage controller number is not supported by the gig format");
2727                  }                  }
# Line 2128  namespace { Line 2771  namespace {
2771          return pVelocityCutoffTable[MIDIKeyVelocity];          return pVelocityCutoffTable[MIDIKeyVelocity];
2772      }      }
2773    
2774        /**
2775         * Updates the respective member variable and the lookup table / cache
2776         * that depends on this value.
2777         */
2778        void DimensionRegion::SetVelocityResponseCurve(curve_type_t curve) {
2779            pVelocityAttenuationTable =
2780                GetVelocityTable(
2781                    curve, VelocityResponseDepth, VelocityResponseCurveScaling
2782                );
2783            VelocityResponseCurve = curve;
2784        }
2785    
2786        /**
2787         * Updates the respective member variable and the lookup table / cache
2788         * that depends on this value.
2789         */
2790        void DimensionRegion::SetVelocityResponseDepth(uint8_t depth) {
2791            pVelocityAttenuationTable =
2792                GetVelocityTable(
2793                    VelocityResponseCurve, depth, VelocityResponseCurveScaling
2794                );
2795            VelocityResponseDepth = depth;
2796        }
2797    
2798        /**
2799         * Updates the respective member variable and the lookup table / cache
2800         * that depends on this value.
2801         */
2802        void DimensionRegion::SetVelocityResponseCurveScaling(uint8_t scaling) {
2803            pVelocityAttenuationTable =
2804                GetVelocityTable(
2805                    VelocityResponseCurve, VelocityResponseDepth, scaling
2806                );
2807            VelocityResponseCurveScaling = scaling;
2808        }
2809    
2810        /**
2811         * Updates the respective member variable and the lookup table / cache
2812         * that depends on this value.
2813         */
2814        void DimensionRegion::SetReleaseVelocityResponseCurve(curve_type_t curve) {
2815            pVelocityReleaseTable = GetReleaseVelocityTable(curve, ReleaseVelocityResponseDepth);
2816            ReleaseVelocityResponseCurve = curve;
2817        }
2818    
2819        /**
2820         * Updates the respective member variable and the lookup table / cache
2821         * that depends on this value.
2822         */
2823        void DimensionRegion::SetReleaseVelocityResponseDepth(uint8_t depth) {
2824            pVelocityReleaseTable = GetReleaseVelocityTable(ReleaseVelocityResponseCurve, depth);
2825            ReleaseVelocityResponseDepth = depth;
2826        }
2827    
2828        /**
2829         * Updates the respective member variable and the lookup table / cache
2830         * that depends on this value.
2831         */
2832        void DimensionRegion::SetVCFCutoffController(vcf_cutoff_ctrl_t controller) {
2833            pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, VCFVelocityDynamicRange, VCFVelocityScale, controller);
2834            VCFCutoffController = controller;
2835        }
2836    
2837        /**
2838         * Updates the respective member variable and the lookup table / cache
2839         * that depends on this value.
2840         */
2841        void DimensionRegion::SetVCFVelocityCurve(curve_type_t curve) {
2842            pVelocityCutoffTable = GetCutoffVelocityTable(curve, VCFVelocityDynamicRange, VCFVelocityScale, VCFCutoffController);
2843            VCFVelocityCurve = curve;
2844        }
2845    
2846        /**
2847         * Updates the respective member variable and the lookup table / cache
2848         * that depends on this value.
2849         */
2850        void DimensionRegion::SetVCFVelocityDynamicRange(uint8_t range) {
2851            pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, range, VCFVelocityScale, VCFCutoffController);
2852            VCFVelocityDynamicRange = range;
2853        }
2854    
2855        /**
2856         * Updates the respective member variable and the lookup table / cache
2857         * that depends on this value.
2858         */
2859        void DimensionRegion::SetVCFVelocityScale(uint8_t scaling) {
2860            pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, VCFVelocityDynamicRange, scaling, VCFCutoffController);
2861            VCFVelocityScale = scaling;
2862        }
2863    
2864      double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) {      double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) {
2865    
2866          // line-segment approximations of the 15 velocity curves          // line-segment approximations of the 15 velocity curves
# Line 2211  namespace { Line 2944  namespace {
2944    
2945          // Actual Loading          // Actual Loading
2946    
2947            if (!file->GetAutoLoad()) return;
2948    
2949          LoadDimensionRegions(rgnList);          LoadDimensionRegions(rgnList);
2950    
2951          RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK);          RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK);
# Line 2254  namespace { Line 2989  namespace {
2989              else              else
2990                  _3lnk->SetPos(44);                  _3lnk->SetPos(44);
2991    
2992              // load sample references              // load sample references (if auto loading is enabled)
2993              for (uint i = 0; i < DimensionRegions; i++) {              if (file->GetAutoLoad()) {
2994                  uint32_t wavepoolindex = _3lnk->ReadUint32();                  for (uint i = 0; i < DimensionRegions; i++) {
2995                  if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);                      uint32_t wavepoolindex = _3lnk->ReadUint32();
2996                        if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);
2997                    }
2998                    GetSample(); // load global region sample reference
2999              }              }
             GetSample(); // load global region sample reference  
3000          } else {          } else {
3001              DimensionRegions = 0;              DimensionRegions = 0;
3002              for (int i = 0 ; i < 8 ; i++) {              for (int i = 0 ; i < 8 ; i++) {
# Line 2756  namespace { Line 3493  namespace {
3493          }          }
3494          return NULL;          return NULL;
3495      }      }
3496        
3497        /**
3498         * Make a (semi) deep copy of the Region object given by @a orig
3499         * and assign it to this object.
3500         *
3501         * Note that all sample pointers referenced by @a orig are simply copied as
3502         * memory address. Thus the respective samples are shared, not duplicated!
3503         *
3504         * @param orig - original Region object to be copied from
3505         */
3506        void Region::CopyAssign(const Region* orig) {
3507            CopyAssign(orig, NULL);
3508        }
3509        
3510        /**
3511         * Make a (semi) deep copy of the Region object given by @a orig and
3512         * assign it to this object
3513         *
3514         * @param mSamples - crosslink map between the foreign file's samples and
3515         *                   this file's samples
3516         */
3517        void Region::CopyAssign(const Region* orig, const std::map<Sample*,Sample*>* mSamples) {
3518            // handle base classes
3519            DLS::Region::CopyAssign(orig);
3520            
3521            if (mSamples && mSamples->count((gig::Sample*)orig->pSample)) {
3522                pSample = mSamples->find((gig::Sample*)orig->pSample)->second;
3523            }
3524            
3525            // handle own member variables
3526            for (int i = Dimensions - 1; i >= 0; --i) {
3527                DeleteDimension(&pDimensionDefinitions[i]);
3528            }
3529            Layers = 0; // just to be sure
3530            for (int i = 0; i < orig->Dimensions; i++) {
3531                // we need to copy the dim definition here, to avoid the compiler
3532                // complaining about const-ness issue
3533                dimension_def_t def = orig->pDimensionDefinitions[i];
3534                AddDimension(&def);
3535            }
3536            for (int i = 0; i < 256; i++) {
3537                if (pDimensionRegions[i] && orig->pDimensionRegions[i]) {
3538                    pDimensionRegions[i]->CopyAssign(
3539                        orig->pDimensionRegions[i],
3540                        mSamples
3541                    );
3542                }
3543            }
3544            Layers = orig->Layers;
3545        }
3546    
3547    
3548    // *************** MidiRule ***************
3549    // *
3550    
3551        MidiRuleCtrlTrigger::MidiRuleCtrlTrigger(RIFF::Chunk* _3ewg) {
3552            _3ewg->SetPos(36);
3553            Triggers = _3ewg->ReadUint8();
3554            _3ewg->SetPos(40);
3555            ControllerNumber = _3ewg->ReadUint8();
3556            _3ewg->SetPos(46);
3557            for (int i = 0 ; i < Triggers ; i++) {
3558                pTriggers[i].TriggerPoint = _3ewg->ReadUint8();
3559                pTriggers[i].Descending = _3ewg->ReadUint8();
3560                pTriggers[i].VelSensitivity = _3ewg->ReadUint8();
3561                pTriggers[i].Key = _3ewg->ReadUint8();
3562                pTriggers[i].NoteOff = _3ewg->ReadUint8();
3563                pTriggers[i].Velocity = _3ewg->ReadUint8();
3564                pTriggers[i].OverridePedal = _3ewg->ReadUint8();
3565                _3ewg->ReadUint8();
3566            }
3567        }
3568    
3569        MidiRuleCtrlTrigger::MidiRuleCtrlTrigger() :
3570            ControllerNumber(0),
3571            Triggers(0) {
3572        }
3573    
3574        void MidiRuleCtrlTrigger::UpdateChunks(uint8_t* pData) const {
3575            pData[32] = 4;
3576            pData[33] = 16;
3577            pData[36] = Triggers;
3578            pData[40] = ControllerNumber;
3579            for (int i = 0 ; i < Triggers ; i++) {
3580                pData[46 + i * 8] = pTriggers[i].TriggerPoint;
3581                pData[47 + i * 8] = pTriggers[i].Descending;
3582                pData[48 + i * 8] = pTriggers[i].VelSensitivity;
3583                pData[49 + i * 8] = pTriggers[i].Key;
3584                pData[50 + i * 8] = pTriggers[i].NoteOff;
3585                pData[51 + i * 8] = pTriggers[i].Velocity;
3586                pData[52 + i * 8] = pTriggers[i].OverridePedal;
3587            }
3588        }
3589    
3590        MidiRuleLegato::MidiRuleLegato(RIFF::Chunk* _3ewg) {
3591            _3ewg->SetPos(36);
3592            LegatoSamples = _3ewg->ReadUint8(); // always 12
3593            _3ewg->SetPos(40);
3594            BypassUseController = _3ewg->ReadUint8();
3595            BypassKey = _3ewg->ReadUint8();
3596            BypassController = _3ewg->ReadUint8();
3597            ThresholdTime = _3ewg->ReadUint16();
3598            _3ewg->ReadInt16();
3599            ReleaseTime = _3ewg->ReadUint16();
3600            _3ewg->ReadInt16();
3601            KeyRange.low = _3ewg->ReadUint8();
3602            KeyRange.high = _3ewg->ReadUint8();
3603            _3ewg->SetPos(64);
3604            ReleaseTriggerKey = _3ewg->ReadUint8();
3605            AltSustain1Key = _3ewg->ReadUint8();
3606            AltSustain2Key = _3ewg->ReadUint8();
3607        }
3608    
3609        MidiRuleLegato::MidiRuleLegato() :
3610            LegatoSamples(12),
3611            BypassUseController(false),
3612            BypassKey(0),
3613            BypassController(1),
3614            ThresholdTime(20),
3615            ReleaseTime(20),
3616            ReleaseTriggerKey(0),
3617            AltSustain1Key(0),
3618            AltSustain2Key(0)
3619        {
3620            KeyRange.low = KeyRange.high = 0;
3621        }
3622    
3623        void MidiRuleLegato::UpdateChunks(uint8_t* pData) const {
3624            pData[32] = 0;
3625            pData[33] = 16;
3626            pData[36] = LegatoSamples;
3627            pData[40] = BypassUseController;
3628            pData[41] = BypassKey;
3629            pData[42] = BypassController;
3630            store16(&pData[43], ThresholdTime);
3631            store16(&pData[47], ReleaseTime);
3632            pData[51] = KeyRange.low;
3633            pData[52] = KeyRange.high;
3634            pData[64] = ReleaseTriggerKey;
3635            pData[65] = AltSustain1Key;
3636            pData[66] = AltSustain2Key;
3637        }
3638    
3639        MidiRuleAlternator::MidiRuleAlternator(RIFF::Chunk* _3ewg) {
3640            _3ewg->SetPos(36);
3641            Articulations = _3ewg->ReadUint8();
3642            int flags = _3ewg->ReadUint8();
3643            Polyphonic = flags & 8;
3644            Chained = flags & 4;
3645            Selector = (flags & 2) ? selector_controller :
3646                (flags & 1) ? selector_key_switch : selector_none;
3647            Patterns = _3ewg->ReadUint8();
3648            _3ewg->ReadUint8(); // chosen row
3649            _3ewg->ReadUint8(); // unknown
3650            _3ewg->ReadUint8(); // unknown
3651            _3ewg->ReadUint8(); // unknown
3652            KeySwitchRange.low = _3ewg->ReadUint8();
3653            KeySwitchRange.high = _3ewg->ReadUint8();
3654            Controller = _3ewg->ReadUint8();
3655            PlayRange.low = _3ewg->ReadUint8();
3656            PlayRange.high = _3ewg->ReadUint8();
3657    
3658            int n = std::min(int(Articulations), 32);
3659            for (int i = 0 ; i < n ; i++) {
3660                _3ewg->ReadString(pArticulations[i], 32);
3661            }
3662            _3ewg->SetPos(1072);
3663            n = std::min(int(Patterns), 32);
3664            for (int i = 0 ; i < n ; i++) {
3665                _3ewg->ReadString(pPatterns[i].Name, 16);
3666                pPatterns[i].Size = _3ewg->ReadUint8();
3667                _3ewg->Read(&pPatterns[i][0], 1, 32);
3668            }
3669        }
3670    
3671        MidiRuleAlternator::MidiRuleAlternator() :
3672            Articulations(0),
3673            Patterns(0),
3674            Selector(selector_none),
3675            Controller(0),
3676            Polyphonic(false),
3677            Chained(false)
3678        {
3679            PlayRange.low = PlayRange.high = 0;
3680            KeySwitchRange.low = KeySwitchRange.high = 0;
3681        }
3682    
3683        void MidiRuleAlternator::UpdateChunks(uint8_t* pData) const {
3684            pData[32] = 3;
3685            pData[33] = 16;
3686            pData[36] = Articulations;
3687            pData[37] = (Polyphonic ? 8 : 0) | (Chained ? 4 : 0) |
3688                (Selector == selector_controller ? 2 :
3689                 (Selector == selector_key_switch ? 1 : 0));
3690            pData[38] = Patterns;
3691    
3692            pData[43] = KeySwitchRange.low;
3693            pData[44] = KeySwitchRange.high;
3694            pData[45] = Controller;
3695            pData[46] = PlayRange.low;
3696            pData[47] = PlayRange.high;
3697    
3698            char* str = reinterpret_cast<char*>(pData);
3699            int pos = 48;
3700            int n = std::min(int(Articulations), 32);
3701            for (int i = 0 ; i < n ; i++, pos += 32) {
3702                strncpy(&str[pos], pArticulations[i].c_str(), 32);
3703            }
3704    
3705            pos = 1072;
3706            n = std::min(int(Patterns), 32);
3707            for (int i = 0 ; i < n ; i++, pos += 49) {
3708                strncpy(&str[pos], pPatterns[i].Name.c_str(), 16);
3709                pData[pos + 16] = pPatterns[i].Size;
3710                memcpy(&pData[pos + 16], &(pPatterns[i][0]), 32);
3711            }
3712        }
3713    
3714  // *************** Instrument ***************  // *************** Instrument ***************
3715  // *  // *
3716    
3717      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) {
3718          static const DLS::Info::FixedStringLength fixedStringLengths[] = {          static const DLS::Info::string_length_t fixedStringLengths[] = {
3719              { CHUNK_ID_INAM, 64 },              { CHUNK_ID_INAM, 64 },
3720              { CHUNK_ID_ISFT, 12 },              { CHUNK_ID_ISFT, 12 },
3721              { 0, 0 }              { 0, 0 }
3722          };          };
3723          pInfo->FixedStringLengths = fixedStringLengths;          pInfo->SetFixedStringLengths(fixedStringLengths);
3724    
3725          // Initialization          // Initialization
3726          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;
# Line 2779  namespace { Line 3731  namespace {
3731          PianoReleaseMode = false;          PianoReleaseMode = false;
3732          DimensionKeyRange.low = 0;          DimensionKeyRange.low = 0;
3733          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
3734            pMidiRules = new MidiRule*[3];
3735            pMidiRules[0] = NULL;
3736    
3737          // Loading          // Loading
3738          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 2793  namespace { Line 3747  namespace {
3747                  PianoReleaseMode       = dimkeystart & 0x01;                  PianoReleaseMode       = dimkeystart & 0x01;
3748                  DimensionKeyRange.low  = dimkeystart >> 1;                  DimensionKeyRange.low  = dimkeystart >> 1;
3749                  DimensionKeyRange.high = _3ewg->ReadUint8();                  DimensionKeyRange.high = _3ewg->ReadUint8();
3750    
3751                    if (_3ewg->GetSize() > 32) {
3752                        // read MIDI rules
3753                        int i = 0;
3754                        _3ewg->SetPos(32);
3755                        uint8_t id1 = _3ewg->ReadUint8();
3756                        uint8_t id2 = _3ewg->ReadUint8();
3757    
3758                        if (id2 == 16) {
3759                            if (id1 == 4) {
3760                                pMidiRules[i++] = new MidiRuleCtrlTrigger(_3ewg);
3761                            } else if (id1 == 0) {
3762                                pMidiRules[i++] = new MidiRuleLegato(_3ewg);
3763                            } else if (id1 == 3) {
3764                                pMidiRules[i++] = new MidiRuleAlternator(_3ewg);
3765                            } else {
3766                                pMidiRules[i++] = new MidiRuleUnknown;
3767                            }
3768                        }
3769                        else if (id1 != 0 || id2 != 0) {
3770                            pMidiRules[i++] = new MidiRuleUnknown;
3771                        }
3772                        //TODO: all the other types of rules
3773    
3774                        pMidiRules[i] = NULL;
3775                    }
3776              }              }
3777          }          }
3778    
3779          if (!pRegions) pRegions = new RegionList;          if (pFile->GetAutoLoad()) {
3780          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);              if (!pRegions) pRegions = new RegionList;
3781          if (lrgn) {              RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);
3782              RIFF::List* rgn = lrgn->GetFirstSubList();              if (lrgn) {
3783              while (rgn) {                  RIFF::List* rgn = lrgn->GetFirstSubList();
3784                  if (rgn->GetListType() == LIST_TYPE_RGN) {                  while (rgn) {
3785                      __notify_progress(pProgress, (float) pRegions->size() / (float) Regions);                      if (rgn->GetListType() == LIST_TYPE_RGN) {
3786                      pRegions->push_back(new Region(this, rgn));                          __notify_progress(pProgress, (float) pRegions->size() / (float) Regions);
3787                            pRegions->push_back(new Region(this, rgn));
3788                        }
3789                        rgn = lrgn->GetNextSubList();
3790                  }                  }
3791                  rgn = lrgn->GetNextSubList();                  // Creating Region Key Table for fast lookup
3792                    UpdateRegionKeyTable();
3793              }              }
             // Creating Region Key Table for fast lookup  
             UpdateRegionKeyTable();  
3794          }          }
3795    
3796          __notify_progress(pProgress, 1.0f); // notify done          __notify_progress(pProgress, 1.0f); // notify done
# Line 2827  namespace { Line 3809  namespace {
3809      }      }
3810    
3811      Instrument::~Instrument() {      Instrument::~Instrument() {
3812            for (int i = 0 ; pMidiRules[i] ; i++) {
3813                delete pMidiRules[i];
3814            }
3815            delete[] pMidiRules;
3816      }      }
3817    
3818      /**      /**
# Line 2873  namespace { Line 3859  namespace {
3859                                      DimensionKeyRange.low << 1;                                      DimensionKeyRange.low << 1;
3860          pData[10] = dimkeystart;          pData[10] = dimkeystart;
3861          pData[11] = DimensionKeyRange.high;          pData[11] = DimensionKeyRange.high;
3862    
3863            if (pMidiRules[0] == 0 && _3ewg->GetSize() >= 34) {
3864                pData[32] = 0;
3865                pData[33] = 0;
3866            } else {
3867                for (int i = 0 ; pMidiRules[i] ; i++) {
3868                    pMidiRules[i]->UpdateChunks(pData);
3869                }
3870            }
3871      }      }
3872    
3873      /**      /**
# Line 2941  namespace { Line 3936  namespace {
3936          UpdateRegionKeyTable();          UpdateRegionKeyTable();
3937      }      }
3938    
3939        /**
3940         * Returns a MIDI rule of the instrument.
3941         *
3942         * The list of MIDI rules, at least in gig v3, always contains at
3943         * most two rules. The second rule can only be the DEF filter
3944         * (which currently isn't supported by libgig).
3945         *
3946         * @param i - MIDI rule number
3947         * @returns   pointer address to MIDI rule number i or NULL if there is none
3948         */
3949        MidiRule* Instrument::GetMidiRule(int i) {
3950            return pMidiRules[i];
3951        }
3952    
3953        /**
3954         * Adds the "controller trigger" MIDI rule to the instrument.
3955         *
3956         * @returns the new MIDI rule
3957         */
3958        MidiRuleCtrlTrigger* Instrument::AddMidiRuleCtrlTrigger() {
3959            delete pMidiRules[0];
3960            MidiRuleCtrlTrigger* r = new MidiRuleCtrlTrigger;
3961            pMidiRules[0] = r;
3962            pMidiRules[1] = 0;
3963            return r;
3964        }
3965    
3966        /**
3967         * Adds the legato MIDI rule to the instrument.
3968         *
3969         * @returns the new MIDI rule
3970         */
3971        MidiRuleLegato* Instrument::AddMidiRuleLegato() {
3972            delete pMidiRules[0];
3973            MidiRuleLegato* r = new MidiRuleLegato;
3974            pMidiRules[0] = r;
3975            pMidiRules[1] = 0;
3976            return r;
3977        }
3978    
3979        /**
3980         * Adds the alternator MIDI rule to the instrument.
3981         *
3982         * @returns the new MIDI rule
3983         */
3984        MidiRuleAlternator* Instrument::AddMidiRuleAlternator() {
3985            delete pMidiRules[0];
3986            MidiRuleAlternator* r = new MidiRuleAlternator;
3987            pMidiRules[0] = r;
3988            pMidiRules[1] = 0;
3989            return r;
3990        }
3991    
3992        /**
3993         * Deletes a MIDI rule from the instrument.
3994         *
3995         * @param i - MIDI rule number
3996         */
3997        void Instrument::DeleteMidiRule(int i) {
3998            delete pMidiRules[i];
3999            pMidiRules[i] = 0;
4000        }
4001    
4002        /**
4003         * Make a (semi) deep copy of the Instrument object given by @a orig
4004         * and assign it to this object.
4005         *
4006         * Note that all sample pointers referenced by @a orig are simply copied as
4007         * memory address. Thus the respective samples are shared, not duplicated!
4008         *
4009         * @param orig - original Instrument object to be copied from
4010         */
4011        void Instrument::CopyAssign(const Instrument* orig) {
4012            CopyAssign(orig, NULL);
4013        }
4014            
4015        /**
4016         * Make a (semi) deep copy of the Instrument object given by @a orig
4017         * and assign it to this object.
4018         *
4019         * @param orig - original Instrument object to be copied from
4020         * @param mSamples - crosslink map between the foreign file's samples and
4021         *                   this file's samples
4022         */
4023        void Instrument::CopyAssign(const Instrument* orig, const std::map<Sample*,Sample*>* mSamples) {
4024            // handle base class
4025            // (without copying DLS region stuff)
4026            DLS::Instrument::CopyAssignCore(orig);
4027            
4028            // handle own member variables
4029            Attenuation = orig->Attenuation;
4030            EffectSend = orig->EffectSend;
4031            FineTune = orig->FineTune;
4032            PitchbendRange = orig->PitchbendRange;
4033            PianoReleaseMode = orig->PianoReleaseMode;
4034            DimensionKeyRange = orig->DimensionKeyRange;
4035            
4036            // free old midi rules
4037            for (int i = 0 ; pMidiRules[i] ; i++) {
4038                delete pMidiRules[i];
4039            }
4040            //TODO: MIDI rule copying
4041            pMidiRules[0] = NULL;
4042            
4043            // delete all old regions
4044            while (Regions) DeleteRegion(GetFirstRegion());
4045            // create new regions and copy them from original
4046            {
4047                RegionList::const_iterator it = orig->pRegions->begin();
4048                for (int i = 0; i < orig->Regions; ++i, ++it) {
4049                    Region* dstRgn = AddRegion();
4050                    //NOTE: Region does semi-deep copy !
4051                    dstRgn->CopyAssign(
4052                        static_cast<gig::Region*>(*it),
4053                        mSamples
4054                    );
4055                }
4056            }
4057    
4058            UpdateRegionKeyTable();
4059        }
4060    
4061    
4062  // *************** Group ***************  // *************** Group ***************
# Line 3066  namespace { Line 4182  namespace {
4182  // *************** File ***************  // *************** File ***************
4183  // *  // *
4184    
4185      // File version 2.0, 1998-06-28      /// Reflects Gigasampler file format version 2.0 (1998-06-28).
4186      const DLS::version_t File::VERSION_2 = {      const DLS::version_t File::VERSION_2 = {
4187          0, 2, 19980628 & 0xffff, 19980628 >> 16          0, 2, 19980628 & 0xffff, 19980628 >> 16
4188      };      };
4189    
4190      // File version 3.0, 2003-03-31      /// Reflects Gigasampler file format version 3.0 (2003-03-31).
4191      const DLS::version_t File::VERSION_3 = {      const DLS::version_t File::VERSION_3 = {
4192          0, 3, 20030331 & 0xffff, 20030331 >> 16          0, 3, 20030331 & 0xffff, 20030331 >> 16
4193      };      };
4194    
4195      const DLS::Info::FixedStringLength File::FixedStringLengths[] = {      static const DLS::Info::string_length_t _FileFixedStringLengths[] = {
4196          { CHUNK_ID_IARL, 256 },          { CHUNK_ID_IARL, 256 },
4197          { CHUNK_ID_IART, 128 },          { CHUNK_ID_IART, 128 },
4198          { CHUNK_ID_ICMS, 128 },          { CHUNK_ID_ICMS, 128 },
# Line 3098  namespace { Line 4214  namespace {
4214      };      };
4215    
4216      File::File() : DLS::File() {      File::File() : DLS::File() {
4217            bAutoLoad = true;
4218          *pVersion = VERSION_3;          *pVersion = VERSION_3;
4219          pGroups = NULL;          pGroups = NULL;
4220          pInfo->FixedStringLengths = FixedStringLengths;          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
4221          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
4222    
4223          // add some mandatory chunks to get the file chunks in right          // add some mandatory chunks to get the file chunks in right
# Line 3113  namespace { Line 4230  namespace {
4230      }      }
4231    
4232      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
4233            bAutoLoad = true;
4234          pGroups = NULL;          pGroups = NULL;
4235          pInfo->FixedStringLengths = FixedStringLengths;          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
4236      }      }
4237    
4238      File::~File() {      File::~File() {
# Line 3141  namespace { Line 4259  namespace {
4259          SamplesIterator++;          SamplesIterator++;
4260          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );
4261      }      }
4262        
4263        /**
4264         * Returns Sample object of @a index.
4265         *
4266         * @returns sample object or NULL if index is out of bounds
4267         */
4268        Sample* File::GetSample(uint index) {
4269            if (!pSamples) LoadSamples();
4270            if (!pSamples) return NULL;
4271            DLS::File::SampleList::iterator it = pSamples->begin();
4272            for (int i = 0; i < index; ++i) {
4273                ++it;
4274                if (it == pSamples->end()) return NULL;
4275            }
4276            if (it == pSamples->end()) return NULL;
4277            return static_cast<gig::Sample*>( *it );
4278        }
4279    
4280      /** @brief Add a new sample.      /** @brief Add a new sample.
4281       *       *
# Line 3182  namespace { Line 4317  namespace {
4317          pSamples->erase(iter);          pSamples->erase(iter);
4318          delete pSample;          delete pSample;
4319    
4320            SampleList::iterator tmp = SamplesIterator;
4321          // remove all references to the sample          // remove all references to the sample
4322          for (Instrument* instrument = GetFirstInstrument() ; instrument ;          for (Instrument* instrument = GetFirstInstrument() ; instrument ;
4323               instrument = GetNextInstrument()) {               instrument = GetNextInstrument()) {
# Line 3196  namespace { Line 4332  namespace {
4332                  }                  }
4333              }              }
4334          }          }
4335            SamplesIterator = tmp; // restore iterator
4336      }      }
4337    
4338      void File::LoadSamples() {      void File::LoadSamples() {
# Line 3286  namespace { Line 4423  namespace {
4423              progress_t subprogress;              progress_t subprogress;
4424              __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask              __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask
4425              __notify_progress(&subprogress, 0.0f);              __notify_progress(&subprogress, 0.0f);
4426              GetFirstSample(&subprogress); // now force all samples to be loaded              if (GetAutoLoad())
4427                    GetFirstSample(&subprogress); // now force all samples to be loaded
4428              __notify_progress(&subprogress, 1.0f);              __notify_progress(&subprogress, 1.0f);
4429    
4430              // instrument loading subtask              // instrument loading subtask
# Line 3335  namespace { Line 4473  namespace {
4473         pInstruments->push_back(pInstrument);         pInstruments->push_back(pInstrument);
4474         return pInstrument;         return pInstrument;
4475      }      }
4476        
4477        /** @brief Add a duplicate of an existing instrument.
4478         *
4479         * Duplicates the instrument definition given by @a orig and adds it
4480         * to this file. This allows in an instrument editor application to
4481         * easily create variations of an instrument, which will be stored in
4482         * the same .gig file, sharing i.e. the same samples.
4483         *
4484         * Note that all sample pointers referenced by @a orig are simply copied as
4485         * memory address. Thus the respective samples are shared, not duplicated!
4486         *
4487         * You have to call Save() to make this persistent to the file.
4488         *
4489         * @param orig - original instrument to be copied
4490         * @returns duplicated copy of the given instrument
4491         */
4492        Instrument* File::AddDuplicateInstrument(const Instrument* orig) {
4493            Instrument* instr = AddInstrument();
4494            instr->CopyAssign(orig);
4495            return instr;
4496        }
4497        
4498        /** @brief Add content of another existing file.
4499         *
4500         * Duplicates the samples, groups and instruments of the original file
4501         * given by @a pFile and adds them to @c this File. In case @c this File is
4502         * a new one that you haven't saved before, then you have to call
4503         * SetFileName() before calling AddContentOf(), because this method will
4504         * automatically save this file during operation, which is required for
4505         * writing the sample waveform data by disk streaming.
4506         *
4507         * @param pFile - original file whose's content shall be copied from
4508         */
4509        void File::AddContentOf(File* pFile) {
4510            static int iCallCount = -1;
4511            iCallCount++;
4512            std::map<Group*,Group*> mGroups;
4513            std::map<Sample*,Sample*> mSamples;
4514            
4515            // clone sample groups
4516            for (int i = 0; pFile->GetGroup(i); ++i) {
4517                Group* g = AddGroup();
4518                g->Name =
4519                    "COPY" + ToString(iCallCount) + "_" + pFile->GetGroup(i)->Name;
4520                mGroups[pFile->GetGroup(i)] = g;
4521            }
4522            
4523            // clone samples (not waveform data here yet)
4524            for (int i = 0; pFile->GetSample(i); ++i) {
4525                Sample* s = AddSample();
4526                s->CopyAssignMeta(pFile->GetSample(i));
4527                mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s);
4528                mSamples[pFile->GetSample(i)] = s;
4529            }
4530            
4531            //BUG: For some reason this method only works with this additional
4532            //     Save() call in between here.
4533            //
4534            // Important: The correct one of the 2 Save() methods has to be called
4535            // here, depending on whether the file is completely new or has been
4536            // saved to disk already, otherwise it will result in data corruption.
4537            if (pRIFF->IsNew())
4538                Save(GetFileName());
4539            else
4540                Save();
4541            
4542            // clone instruments
4543            // (passing the crosslink table here for the cloned samples)
4544            for (int i = 0; pFile->GetInstrument(i); ++i) {
4545                Instrument* instr = AddInstrument();
4546                instr->CopyAssign(pFile->GetInstrument(i), &mSamples);
4547            }
4548            
4549            // Mandatory: file needs to be saved to disk at this point, so this
4550            // file has the correct size and data layout for writing the samples'
4551            // waveform data to disk.
4552            Save();
4553            
4554            // clone samples' waveform data
4555            // (using direct read & write disk streaming)
4556            for (int i = 0; pFile->GetSample(i); ++i) {
4557                mSamples[pFile->GetSample(i)]->CopyAssignWave(pFile->GetSample(i));
4558            }
4559        }
4560    
4561      /** @brief Delete an instrument.      /** @brief Delete an instrument.
4562       *       *
# Line 3437  namespace { Line 4659  namespace {
4659          return NULL;          return NULL;
4660      }      }
4661    
4662        /**
4663         * Returns the group with the given group name.
4664         *
4665         * Note: group names don't have to be unique in the gig format! So there
4666         * can be multiple groups with the same name. This method will simply
4667         * return the first group found with the given name.
4668         *
4669         * @param name - name of the sought group
4670         * @returns sought group or NULL if there's no group with that name
4671         */
4672        Group* File::GetGroup(String name) {
4673            if (!pGroups) LoadGroups();
4674            GroupsIterator = pGroups->begin();
4675            for (uint i = 0; GroupsIterator != pGroups->end(); ++GroupsIterator, ++i)
4676                if ((*GroupsIterator)->Name == name) return *GroupsIterator;
4677            return NULL;
4678        }
4679    
4680      Group* File::AddGroup() {      Group* File::AddGroup() {
4681          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
4682          // there must always be at least one group          // there must always be at least one group
# Line 3547  namespace { Line 4787  namespace {
4787    
4788          // update group's chunks          // update group's chunks
4789          if (pGroups) {          if (pGroups) {
4790              std::list<Group*>::iterator iter = pGroups->begin();              // make sure '3gri' and '3gnl' list chunks exist
4791              std::list<Group*>::iterator end  = pGroups->end();              // (before updating the Group chunks)
4792              for (; iter != end; ++iter) {              RIFF::List* _3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);
4793                  (*iter)->UpdateChunks();              if (!_3gri) {
4794                    _3gri = pRIFF->AddSubList(LIST_TYPE_3GRI);
4795                    pRIFF->MoveSubChunk(_3gri, pRIFF->GetSubChunk(CHUNK_ID_PTBL));
4796              }              }
4797                RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);
4798                if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL);
4799    
4800              // v3: make sure the file has 128 3gnm chunks              // v3: make sure the file has 128 3gnm chunks
4801                // (before updating the Group chunks)
4802              if (pVersion && pVersion->major == 3) {              if (pVersion && pVersion->major == 3) {
                 RIFF::List* _3gnl = pRIFF->GetSubList(LIST_TYPE_3GRI)->GetSubList(LIST_TYPE_3GNL);  
4803                  RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk();                  RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk();
4804                  for (int i = 0 ; i < 128 ; i++) {                  for (int i = 0 ; i < 128 ; i++) {
4805                      if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64);                      if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64);
4806                      if (_3gnm) _3gnm = _3gnl->GetNextSubChunk();                      if (_3gnm) _3gnm = _3gnl->GetNextSubChunk();
4807                  }                  }
4808              }              }
4809    
4810                std::list<Group*>::iterator iter = pGroups->begin();
4811                std::list<Group*>::iterator end  = pGroups->end();
4812                for (; iter != end; ++iter) {
4813                    (*iter)->UpdateChunks();
4814                }
4815          }          }
4816    
4817          // update einf chunk          // update einf chunk
# Line 3693  namespace { Line 4943  namespace {
4943          }          }
4944      }      }
4945    
4946        /**
4947         * Enable / disable automatic loading. By default this properyt is
4948         * enabled and all informations are loaded automatically. However
4949         * loading all Regions, DimensionRegions and especially samples might
4950         * take a long time for large .gig files, and sometimes one might only
4951         * be interested in retrieving very superficial informations like the
4952         * amount of instruments and their names. In this case one might disable
4953         * automatic loading to avoid very slow response times.
4954         *
4955         * @e CAUTION: by disabling this property many pointers (i.e. sample
4956         * references) and informations will have invalid or even undefined
4957         * data! This feature is currently only intended for retrieving very
4958         * superficial informations in a very fast way. Don't use it to retrieve
4959         * details like synthesis informations or even to modify .gig files!
4960         */
4961        void File::SetAutoLoad(bool b) {
4962            bAutoLoad = b;
4963        }
4964    
4965        /**
4966         * Returns whether automatic loading is enabled.
4967         * @see SetAutoLoad()
4968         */
4969        bool File::GetAutoLoad() {
4970            return bAutoLoad;
4971        }
4972    
4973    
4974    
4975  // *************** Exception ***************  // *************** Exception ***************

Legend:
Removed from v.1335  
changed lines
  Added in v.2543

  ViewVC Help
Powered by ViewVC