/[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 1524 by schoenebeck, Sun Nov 25 17:29:37 2007 UTC revision 2682 by schoenebeck, Mon Dec 29 16:25:51 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    #include <assert.h>
32    
33  /// Initial size of the sample buffer which is used for decompression of  /// Initial size of the sample buffer which is used for decompression of
34  /// compressed sample wave streams - this value should always be bigger than  /// compressed sample wave streams - this value should always be bigger than
# Line 51  Line 53 
53    
54  namespace gig {  namespace gig {
55    
 // *************** progress_t ***************  
 // *  
   
     progress_t::progress_t() {  
         callback    = NULL;  
         custom      = NULL;  
         __range_min = 0.0f;  
         __range_max = 1.0f;  
     }  
   
     // private helper function to convert progress of a subprocess into the global progress  
     static void __notify_progress(progress_t* pProgress, float subprogress) {  
         if (pProgress && pProgress->callback) {  
             const float totalrange    = pProgress->__range_max - pProgress->__range_min;  
             const float totalprogress = pProgress->__range_min + subprogress * totalrange;  
             pProgress->factor         = totalprogress;  
             pProgress->callback(pProgress); // now actually notify about the progress  
         }  
     }  
   
     // private helper function to divide a progress into subprogresses  
     static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress, float totalTasks, float currentTask) {  
         if (pParentProgress && pParentProgress->callback) {  
             const float totalrange    = pParentProgress->__range_max - pParentProgress->__range_min;  
             pSubProgress->callback    = pParentProgress->callback;  
             pSubProgress->custom      = pParentProgress->custom;  
             pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;  
             pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;  
         }  
     }  
   
   
56  // *************** Internal functions for sample decompression ***************  // *************** Internal functions for sample decompression ***************
57  // *  // *
58    
# Line 453  namespace { Line 423  namespace {
423      }      }
424    
425      /**      /**
426         * Make a (semi) deep copy of the Sample object given by @a orig (without
427         * the actual waveform data) and assign it to this object.
428         *
429         * Discussion: copying .gig samples is a bit tricky. It requires three
430         * steps:
431         * 1. Copy sample's meta informations (done by CopyAssignMeta()) including
432         *    its new sample waveform data size.
433         * 2. Saving the file (done by File::Save()) so that it gains correct size
434         *    and layout for writing the actual wave form data directly to disc
435         *    in next step.
436         * 3. Copy the waveform data with disk streaming (done by CopyAssignWave()).
437         *
438         * @param orig - original Sample object to be copied from
439         */
440        void Sample::CopyAssignMeta(const Sample* orig) {
441            // handle base classes
442            DLS::Sample::CopyAssignCore(orig);
443            
444            // handle actual own attributes of this class
445            Manufacturer = orig->Manufacturer;
446            Product = orig->Product;
447            SamplePeriod = orig->SamplePeriod;
448            MIDIUnityNote = orig->MIDIUnityNote;
449            FineTune = orig->FineTune;
450            SMPTEFormat = orig->SMPTEFormat;
451            SMPTEOffset = orig->SMPTEOffset;
452            Loops = orig->Loops;
453            LoopID = orig->LoopID;
454            LoopType = orig->LoopType;
455            LoopStart = orig->LoopStart;
456            LoopEnd = orig->LoopEnd;
457            LoopSize = orig->LoopSize;
458            LoopFraction = orig->LoopFraction;
459            LoopPlayCount = orig->LoopPlayCount;
460            
461            // schedule resizing this sample to the given sample's size
462            Resize(orig->GetSize());
463        }
464    
465        /**
466         * Should be called after CopyAssignMeta() and File::Save() sequence.
467         * Read more about it in the discussion of CopyAssignMeta(). This method
468         * copies the actual waveform data by disk streaming.
469         *
470         * @e CAUTION: this method is currently not thread safe! During this
471         * operation the sample must not be used for other purposes by other
472         * threads!
473         *
474         * @param orig - original Sample object to be copied from
475         */
476        void Sample::CopyAssignWave(const Sample* orig) {
477            const int iReadAtOnce = 32*1024;
478            char* buf = new char[iReadAtOnce * orig->FrameSize];
479            Sample* pOrig = (Sample*) orig; //HACK: remove constness for now
480            unsigned long restorePos = pOrig->GetPos();
481            pOrig->SetPos(0);
482            SetPos(0);
483            for (unsigned long n = pOrig->Read(buf, iReadAtOnce); n;
484                               n = pOrig->Read(buf, iReadAtOnce))
485            {
486                Write(buf, n);
487            }
488            pOrig->SetPos(restorePos);
489            delete [] buf;
490        }
491    
492        /**
493       * Apply sample and its settings to the respective RIFF chunks. You have       * Apply sample and its settings to the respective RIFF chunks. You have
494       * to call File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
495       *       *
496       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
497       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
498       *       *
499         * @param pProgress - callback function for progress notification
500       * @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data       * @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data
501       *                        was provided yet       *                        was provided yet
502       * @throws gig::Exception if there is any invalid sample setting       * @throws gig::Exception if there is any invalid sample setting
503       */       */
504      void Sample::UpdateChunks() {      void Sample::UpdateChunks(progress_t* pProgress) {
505          // first update base class's chunks          // first update base class's chunks
506          DLS::Sample::UpdateChunks();          DLS::Sample::UpdateChunks(pProgress);
507    
508          // make sure 'smpl' chunk exists          // make sure 'smpl' chunk exists
509          pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL);          pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL);
# Line 513  namespace { Line 551  namespace {
551          // update '3gix' chunk          // update '3gix' chunk
552          pData = (uint8_t*) pCk3gix->LoadChunkData();          pData = (uint8_t*) pCk3gix->LoadChunkData();
553          store16(&pData[0], iSampleGroup);          store16(&pData[0], iSampleGroup);
554    
555            // if the library user toggled the "Compressed" attribute from true to
556            // false, then the EWAV chunk associated with compressed samples needs
557            // to be deleted
558            RIFF::Chunk* ewav = pWaveList->GetSubChunk(CHUNK_ID_EWAV);
559            if (ewav && !Compressed) {
560                pWaveList->DeleteSubChunk(ewav);
561            }
562      }      }
563    
564      /// 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 676  namespace { Line 722  namespace {
722          if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal;          if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal;
723          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
724          unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize;          unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize;
725            SetPos(0); // reset read position to begin of sample
726          RAMCache.pStart            = new int8_t[allocationsize];          RAMCache.pStart            = new int8_t[allocationsize];
727          RAMCache.Size              = Read(RAMCache.pStart, SampleCount) * this->FrameSize;          RAMCache.Size              = Read(RAMCache.pStart, SampleCount) * this->FrameSize;
728          RAMCache.NullExtensionSize = allocationsize - RAMCache.Size;          RAMCache.NullExtensionSize = allocationsize - RAMCache.Size;
# Line 713  namespace { Line 760  namespace {
760          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;          if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
761          RAMCache.pStart = NULL;          RAMCache.pStart = NULL;
762          RAMCache.Size   = 0;          RAMCache.Size   = 0;
763            RAMCache.NullExtensionSize = 0;
764      }      }
765    
766      /** @brief Resize sample.      /** @brief Resize sample.
# Line 805  namespace { Line 853  namespace {
853      /**      /**
854       * Returns the current position in the sample (in sample points).       * Returns the current position in the sample (in sample points).
855       */       */
856      unsigned long Sample::GetPos() {      unsigned long Sample::GetPos() const {
857          if (Compressed) return SamplePos;          if (Compressed) return SamplePos;
858          else            return pCkData->GetPos() / FrameSize;          else            return pCkData->GetPos() / FrameSize;
859      }      }
# Line 907  namespace { Line 955  namespace {
955                                  }                                  }
956    
957                                  // reverse the sample frames for backward playback                                  // reverse the sample frames for backward playback
958                                  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!
959                                        SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize);
960                              }                              }
961                          } while (samplestoread && readsamples);                          } while (samplestoread && readsamples);
962                          break;                          break;
# Line 1429  namespace { Line 1478  namespace {
1478                                                          : vcf_res_ctrl_none;                                                          : vcf_res_ctrl_none;
1479              uint16_t eg3depth = _3ewa->ReadUint16();              uint16_t eg3depth = _3ewa->ReadUint16();
1480              EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */              EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */
1481                                          : (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */                                          : (-1) * (int16_t) ((eg3depth ^ 0xfff) + 1); /* binary complementary for negatives */
1482              _3ewa->ReadInt16(); // unknown              _3ewa->ReadInt16(); // unknown
1483              ChannelOffset = _3ewa->ReadUint8() / 4;              ChannelOffset = _3ewa->ReadUint8() / 4;
1484              uint8_t regoptions = _3ewa->ReadUint8();              uint8_t regoptions = _3ewa->ReadUint8();
# Line 1577  namespace { Line 1626  namespace {
1626       */       */
1627      DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) {      DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) {
1628          Instances++;          Instances++;
1629            //NOTE: I think we cannot call CopyAssign() here (in a constructor) as long as its a virtual method
1630          *this = src; // default memberwise shallow copy of all parameters          *this = src; // default memberwise shallow copy of all parameters
1631          pParentList = _3ewl; // restore the chunk pointer          pParentList = _3ewl; // restore the chunk pointer
1632    
# Line 1592  namespace { Line 1642  namespace {
1642                  pSampleLoops[k] = src.pSampleLoops[k];                  pSampleLoops[k] = src.pSampleLoops[k];
1643          }          }
1644      }      }
1645        
1646        /**
1647         * Make a (semi) deep copy of the DimensionRegion object given by @a orig
1648         * and assign it to this object.
1649         *
1650         * Note that all sample pointers referenced by @a orig are simply copied as
1651         * memory address. Thus the respective samples are shared, not duplicated!
1652         *
1653         * @param orig - original DimensionRegion object to be copied from
1654         */
1655        void DimensionRegion::CopyAssign(const DimensionRegion* orig) {
1656            CopyAssign(orig, NULL);
1657        }
1658    
1659        /**
1660         * Make a (semi) deep copy of the DimensionRegion object given by @a orig
1661         * and assign it to this object.
1662         *
1663         * @param orig - original DimensionRegion object to be copied from
1664         * @param mSamples - crosslink map between the foreign file's samples and
1665         *                   this file's samples
1666         */
1667        void DimensionRegion::CopyAssign(const DimensionRegion* orig, const std::map<Sample*,Sample*>* mSamples) {
1668            // delete all allocated data first
1669            if (VelocityTable) delete [] VelocityTable;
1670            if (pSampleLoops) delete [] pSampleLoops;
1671            
1672            // backup parent list pointer
1673            RIFF::List* p = pParentList;
1674            
1675            gig::Sample* pOriginalSample = pSample;
1676            gig::Region* pOriginalRegion = pRegion;
1677            
1678            //NOTE: copy code copied from assignment constructor above, see comment there as well
1679            
1680            *this = *orig; // default memberwise shallow copy of all parameters
1681            
1682            // restore members that shall not be altered
1683            pParentList = p; // restore the chunk pointer
1684            pRegion = pOriginalRegion;
1685            
1686            // only take the raw sample reference reference if the
1687            // two DimensionRegion objects are part of the same file
1688            if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) {
1689                pSample = pOriginalSample;
1690            }
1691            
1692            if (mSamples && mSamples->count(orig->pSample)) {
1693                pSample = mSamples->find(orig->pSample)->second;
1694            }
1695    
1696            // deep copy of owned structures
1697            if (orig->VelocityTable) {
1698                VelocityTable = new uint8_t[128];
1699                for (int k = 0 ; k < 128 ; k++)
1700                    VelocityTable[k] = orig->VelocityTable[k];
1701            }
1702            if (orig->pSampleLoops) {
1703                pSampleLoops = new DLS::sample_loop_t[orig->SampleLoops];
1704                for (int k = 0 ; k < orig->SampleLoops ; k++)
1705                    pSampleLoops[k] = orig->pSampleLoops[k];
1706            }
1707        }
1708    
1709      /**      /**
1710       * Updates the respective member variable and updates @c SampleAttenuation       * Updates the respective member variable and updates @c SampleAttenuation
# Line 1608  namespace { Line 1721  namespace {
1721       *       *
1722       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
1723       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
1724         *
1725         * @param pProgress - callback function for progress notification
1726       */       */
1727      void DimensionRegion::UpdateChunks() {      void DimensionRegion::UpdateChunks(progress_t* pProgress) {
1728          // first update base class's chunk          // first update base class's chunk
1729          DLS::Sampler::UpdateChunks();          DLS::Sampler::UpdateChunks(pProgress);
1730    
1731          RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);          RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
1732          uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();          uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
# Line 1833  namespace { Line 1948  namespace {
1948          }          }
1949    
1950          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth
1951                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xfff); /* binary complementary for negatives */
1952          pData[116] = eg3depth;          store16(&pData[116], eg3depth);
1953    
1954          // next 2 bytes unknown          // next 2 bytes unknown
1955    
# Line 1881  namespace { Line 1996  namespace {
1996                                        (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */                                        (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */
1997          pData[137] = vcfbreakpoint;          pData[137] = vcfbreakpoint;
1998    
1999          const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 |          const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 +
2000                                      VCFVelocityCurve * 5;                                      VCFVelocityCurve * 5;
2001          pData[138] = vcfvelocity;          pData[138] = vcfvelocity;
2002    
# Line 1946  namespace { Line 2061  namespace {
2061          return pRegion;          return pRegion;
2062      }      }
2063    
2064    // show error if some _lev_ctrl_* enum entry is not listed in the following function
2065    // (commented out for now, because "diagnostic push" not supported prior GCC 4.6)
2066    // TODO: uncomment and add a GCC version check (see also commented "#pragma GCC diagnostic pop" below)
2067    //#pragma GCC diagnostic push
2068    //#pragma GCC diagnostic error "-Wswitch"
2069    
2070      leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {      leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) {
2071          leverage_ctrl_t decodedcontroller;          leverage_ctrl_t decodedcontroller;
2072          switch (EncodedController) {          switch (EncodedController) {
# Line 2057  namespace { Line 2178  namespace {
2178                  decodedcontroller.controller_number = 95;                  decodedcontroller.controller_number = 95;
2179                  break;                  break;
2180    
2181                // format extension (these controllers are so far only supported by
2182                // LinuxSampler & gigedit) they will *NOT* work with
2183                // Gigasampler/GigaStudio !
2184                case _lev_ctrl_CC3_EXT:
2185                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2186                    decodedcontroller.controller_number = 3;
2187                    break;
2188                case _lev_ctrl_CC6_EXT:
2189                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2190                    decodedcontroller.controller_number = 6;
2191                    break;
2192                case _lev_ctrl_CC7_EXT:
2193                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2194                    decodedcontroller.controller_number = 7;
2195                    break;
2196                case _lev_ctrl_CC8_EXT:
2197                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2198                    decodedcontroller.controller_number = 8;
2199                    break;
2200                case _lev_ctrl_CC9_EXT:
2201                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2202                    decodedcontroller.controller_number = 9;
2203                    break;
2204                case _lev_ctrl_CC10_EXT:
2205                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2206                    decodedcontroller.controller_number = 10;
2207                    break;
2208                case _lev_ctrl_CC11_EXT:
2209                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2210                    decodedcontroller.controller_number = 11;
2211                    break;
2212                case _lev_ctrl_CC14_EXT:
2213                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2214                    decodedcontroller.controller_number = 14;
2215                    break;
2216                case _lev_ctrl_CC15_EXT:
2217                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2218                    decodedcontroller.controller_number = 15;
2219                    break;
2220                case _lev_ctrl_CC20_EXT:
2221                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2222                    decodedcontroller.controller_number = 20;
2223                    break;
2224                case _lev_ctrl_CC21_EXT:
2225                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2226                    decodedcontroller.controller_number = 21;
2227                    break;
2228                case _lev_ctrl_CC22_EXT:
2229                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2230                    decodedcontroller.controller_number = 22;
2231                    break;
2232                case _lev_ctrl_CC23_EXT:
2233                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2234                    decodedcontroller.controller_number = 23;
2235                    break;
2236                case _lev_ctrl_CC24_EXT:
2237                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2238                    decodedcontroller.controller_number = 24;
2239                    break;
2240                case _lev_ctrl_CC25_EXT:
2241                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2242                    decodedcontroller.controller_number = 25;
2243                    break;
2244                case _lev_ctrl_CC26_EXT:
2245                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2246                    decodedcontroller.controller_number = 26;
2247                    break;
2248                case _lev_ctrl_CC27_EXT:
2249                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2250                    decodedcontroller.controller_number = 27;
2251                    break;
2252                case _lev_ctrl_CC28_EXT:
2253                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2254                    decodedcontroller.controller_number = 28;
2255                    break;
2256                case _lev_ctrl_CC29_EXT:
2257                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2258                    decodedcontroller.controller_number = 29;
2259                    break;
2260                case _lev_ctrl_CC30_EXT:
2261                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2262                    decodedcontroller.controller_number = 30;
2263                    break;
2264                case _lev_ctrl_CC31_EXT:
2265                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2266                    decodedcontroller.controller_number = 31;
2267                    break;
2268                case _lev_ctrl_CC68_EXT:
2269                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2270                    decodedcontroller.controller_number = 68;
2271                    break;
2272                case _lev_ctrl_CC69_EXT:
2273                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2274                    decodedcontroller.controller_number = 69;
2275                    break;
2276                case _lev_ctrl_CC70_EXT:
2277                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2278                    decodedcontroller.controller_number = 70;
2279                    break;
2280                case _lev_ctrl_CC71_EXT:
2281                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2282                    decodedcontroller.controller_number = 71;
2283                    break;
2284                case _lev_ctrl_CC72_EXT:
2285                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2286                    decodedcontroller.controller_number = 72;
2287                    break;
2288                case _lev_ctrl_CC73_EXT:
2289                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2290                    decodedcontroller.controller_number = 73;
2291                    break;
2292                case _lev_ctrl_CC74_EXT:
2293                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2294                    decodedcontroller.controller_number = 74;
2295                    break;
2296                case _lev_ctrl_CC75_EXT:
2297                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2298                    decodedcontroller.controller_number = 75;
2299                    break;
2300                case _lev_ctrl_CC76_EXT:
2301                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2302                    decodedcontroller.controller_number = 76;
2303                    break;
2304                case _lev_ctrl_CC77_EXT:
2305                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2306                    decodedcontroller.controller_number = 77;
2307                    break;
2308                case _lev_ctrl_CC78_EXT:
2309                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2310                    decodedcontroller.controller_number = 78;
2311                    break;
2312                case _lev_ctrl_CC79_EXT:
2313                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2314                    decodedcontroller.controller_number = 79;
2315                    break;
2316                case _lev_ctrl_CC84_EXT:
2317                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2318                    decodedcontroller.controller_number = 84;
2319                    break;
2320                case _lev_ctrl_CC85_EXT:
2321                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2322                    decodedcontroller.controller_number = 85;
2323                    break;
2324                case _lev_ctrl_CC86_EXT:
2325                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2326                    decodedcontroller.controller_number = 86;
2327                    break;
2328                case _lev_ctrl_CC87_EXT:
2329                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2330                    decodedcontroller.controller_number = 87;
2331                    break;
2332                case _lev_ctrl_CC89_EXT:
2333                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2334                    decodedcontroller.controller_number = 89;
2335                    break;
2336                case _lev_ctrl_CC90_EXT:
2337                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2338                    decodedcontroller.controller_number = 90;
2339                    break;
2340                case _lev_ctrl_CC96_EXT:
2341                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2342                    decodedcontroller.controller_number = 96;
2343                    break;
2344                case _lev_ctrl_CC97_EXT:
2345                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2346                    decodedcontroller.controller_number = 97;
2347                    break;
2348                case _lev_ctrl_CC102_EXT:
2349                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2350                    decodedcontroller.controller_number = 102;
2351                    break;
2352                case _lev_ctrl_CC103_EXT:
2353                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2354                    decodedcontroller.controller_number = 103;
2355                    break;
2356                case _lev_ctrl_CC104_EXT:
2357                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2358                    decodedcontroller.controller_number = 104;
2359                    break;
2360                case _lev_ctrl_CC105_EXT:
2361                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2362                    decodedcontroller.controller_number = 105;
2363                    break;
2364                case _lev_ctrl_CC106_EXT:
2365                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2366                    decodedcontroller.controller_number = 106;
2367                    break;
2368                case _lev_ctrl_CC107_EXT:
2369                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2370                    decodedcontroller.controller_number = 107;
2371                    break;
2372                case _lev_ctrl_CC108_EXT:
2373                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2374                    decodedcontroller.controller_number = 108;
2375                    break;
2376                case _lev_ctrl_CC109_EXT:
2377                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2378                    decodedcontroller.controller_number = 109;
2379                    break;
2380                case _lev_ctrl_CC110_EXT:
2381                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2382                    decodedcontroller.controller_number = 110;
2383                    break;
2384                case _lev_ctrl_CC111_EXT:
2385                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2386                    decodedcontroller.controller_number = 111;
2387                    break;
2388                case _lev_ctrl_CC112_EXT:
2389                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2390                    decodedcontroller.controller_number = 112;
2391                    break;
2392                case _lev_ctrl_CC113_EXT:
2393                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2394                    decodedcontroller.controller_number = 113;
2395                    break;
2396                case _lev_ctrl_CC114_EXT:
2397                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2398                    decodedcontroller.controller_number = 114;
2399                    break;
2400                case _lev_ctrl_CC115_EXT:
2401                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2402                    decodedcontroller.controller_number = 115;
2403                    break;
2404                case _lev_ctrl_CC116_EXT:
2405                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2406                    decodedcontroller.controller_number = 116;
2407                    break;
2408                case _lev_ctrl_CC117_EXT:
2409                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2410                    decodedcontroller.controller_number = 117;
2411                    break;
2412                case _lev_ctrl_CC118_EXT:
2413                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2414                    decodedcontroller.controller_number = 118;
2415                    break;
2416                case _lev_ctrl_CC119_EXT:
2417                    decodedcontroller.type = leverage_ctrl_t::type_controlchange;
2418                    decodedcontroller.controller_number = 119;
2419                    break;
2420    
2421              // unknown controller type              // unknown controller type
2422              default:              default:
2423                  throw gig::Exception("Unknown leverage controller type.");                  throw gig::Exception("Unknown leverage controller type.");
2424          }          }
2425          return decodedcontroller;          return decodedcontroller;
2426      }      }
2427        
2428    // see above (diagnostic push not supported prior GCC 4.6)
2429    //#pragma GCC diagnostic pop
2430    
2431      DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {      DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) {
2432          _lev_ctrl_t encodedcontroller;          _lev_ctrl_t encodedcontroller;
# Line 2150  namespace { Line 2514  namespace {
2514                      case 95:                      case 95:
2515                          encodedcontroller = _lev_ctrl_effect5depth;                          encodedcontroller = _lev_ctrl_effect5depth;
2516                          break;                          break;
2517    
2518                        // format extension (these controllers are so far only
2519                        // supported by LinuxSampler & gigedit) they will *NOT*
2520                        // work with Gigasampler/GigaStudio !
2521                        case 3:
2522                            encodedcontroller = _lev_ctrl_CC3_EXT;
2523                            break;
2524                        case 6:
2525                            encodedcontroller = _lev_ctrl_CC6_EXT;
2526                            break;
2527                        case 7:
2528                            encodedcontroller = _lev_ctrl_CC7_EXT;
2529                            break;
2530                        case 8:
2531                            encodedcontroller = _lev_ctrl_CC8_EXT;
2532                            break;
2533                        case 9:
2534                            encodedcontroller = _lev_ctrl_CC9_EXT;
2535                            break;
2536                        case 10:
2537                            encodedcontroller = _lev_ctrl_CC10_EXT;
2538                            break;
2539                        case 11:
2540                            encodedcontroller = _lev_ctrl_CC11_EXT;
2541                            break;
2542                        case 14:
2543                            encodedcontroller = _lev_ctrl_CC14_EXT;
2544                            break;
2545                        case 15:
2546                            encodedcontroller = _lev_ctrl_CC15_EXT;
2547                            break;
2548                        case 20:
2549                            encodedcontroller = _lev_ctrl_CC20_EXT;
2550                            break;
2551                        case 21:
2552                            encodedcontroller = _lev_ctrl_CC21_EXT;
2553                            break;
2554                        case 22:
2555                            encodedcontroller = _lev_ctrl_CC22_EXT;
2556                            break;
2557                        case 23:
2558                            encodedcontroller = _lev_ctrl_CC23_EXT;
2559                            break;
2560                        case 24:
2561                            encodedcontroller = _lev_ctrl_CC24_EXT;
2562                            break;
2563                        case 25:
2564                            encodedcontroller = _lev_ctrl_CC25_EXT;
2565                            break;
2566                        case 26:
2567                            encodedcontroller = _lev_ctrl_CC26_EXT;
2568                            break;
2569                        case 27:
2570                            encodedcontroller = _lev_ctrl_CC27_EXT;
2571                            break;
2572                        case 28:
2573                            encodedcontroller = _lev_ctrl_CC28_EXT;
2574                            break;
2575                        case 29:
2576                            encodedcontroller = _lev_ctrl_CC29_EXT;
2577                            break;
2578                        case 30:
2579                            encodedcontroller = _lev_ctrl_CC30_EXT;
2580                            break;
2581                        case 31:
2582                            encodedcontroller = _lev_ctrl_CC31_EXT;
2583                            break;
2584                        case 68:
2585                            encodedcontroller = _lev_ctrl_CC68_EXT;
2586                            break;
2587                        case 69:
2588                            encodedcontroller = _lev_ctrl_CC69_EXT;
2589                            break;
2590                        case 70:
2591                            encodedcontroller = _lev_ctrl_CC70_EXT;
2592                            break;
2593                        case 71:
2594                            encodedcontroller = _lev_ctrl_CC71_EXT;
2595                            break;
2596                        case 72:
2597                            encodedcontroller = _lev_ctrl_CC72_EXT;
2598                            break;
2599                        case 73:
2600                            encodedcontroller = _lev_ctrl_CC73_EXT;
2601                            break;
2602                        case 74:
2603                            encodedcontroller = _lev_ctrl_CC74_EXT;
2604                            break;
2605                        case 75:
2606                            encodedcontroller = _lev_ctrl_CC75_EXT;
2607                            break;
2608                        case 76:
2609                            encodedcontroller = _lev_ctrl_CC76_EXT;
2610                            break;
2611                        case 77:
2612                            encodedcontroller = _lev_ctrl_CC77_EXT;
2613                            break;
2614                        case 78:
2615                            encodedcontroller = _lev_ctrl_CC78_EXT;
2616                            break;
2617                        case 79:
2618                            encodedcontroller = _lev_ctrl_CC79_EXT;
2619                            break;
2620                        case 84:
2621                            encodedcontroller = _lev_ctrl_CC84_EXT;
2622                            break;
2623                        case 85:
2624                            encodedcontroller = _lev_ctrl_CC85_EXT;
2625                            break;
2626                        case 86:
2627                            encodedcontroller = _lev_ctrl_CC86_EXT;
2628                            break;
2629                        case 87:
2630                            encodedcontroller = _lev_ctrl_CC87_EXT;
2631                            break;
2632                        case 89:
2633                            encodedcontroller = _lev_ctrl_CC89_EXT;
2634                            break;
2635                        case 90:
2636                            encodedcontroller = _lev_ctrl_CC90_EXT;
2637                            break;
2638                        case 96:
2639                            encodedcontroller = _lev_ctrl_CC96_EXT;
2640                            break;
2641                        case 97:
2642                            encodedcontroller = _lev_ctrl_CC97_EXT;
2643                            break;
2644                        case 102:
2645                            encodedcontroller = _lev_ctrl_CC102_EXT;
2646                            break;
2647                        case 103:
2648                            encodedcontroller = _lev_ctrl_CC103_EXT;
2649                            break;
2650                        case 104:
2651                            encodedcontroller = _lev_ctrl_CC104_EXT;
2652                            break;
2653                        case 105:
2654                            encodedcontroller = _lev_ctrl_CC105_EXT;
2655                            break;
2656                        case 106:
2657                            encodedcontroller = _lev_ctrl_CC106_EXT;
2658                            break;
2659                        case 107:
2660                            encodedcontroller = _lev_ctrl_CC107_EXT;
2661                            break;
2662                        case 108:
2663                            encodedcontroller = _lev_ctrl_CC108_EXT;
2664                            break;
2665                        case 109:
2666                            encodedcontroller = _lev_ctrl_CC109_EXT;
2667                            break;
2668                        case 110:
2669                            encodedcontroller = _lev_ctrl_CC110_EXT;
2670                            break;
2671                        case 111:
2672                            encodedcontroller = _lev_ctrl_CC111_EXT;
2673                            break;
2674                        case 112:
2675                            encodedcontroller = _lev_ctrl_CC112_EXT;
2676                            break;
2677                        case 113:
2678                            encodedcontroller = _lev_ctrl_CC113_EXT;
2679                            break;
2680                        case 114:
2681                            encodedcontroller = _lev_ctrl_CC114_EXT;
2682                            break;
2683                        case 115:
2684                            encodedcontroller = _lev_ctrl_CC115_EXT;
2685                            break;
2686                        case 116:
2687                            encodedcontroller = _lev_ctrl_CC116_EXT;
2688                            break;
2689                        case 117:
2690                            encodedcontroller = _lev_ctrl_CC117_EXT;
2691                            break;
2692                        case 118:
2693                            encodedcontroller = _lev_ctrl_CC118_EXT;
2694                            break;
2695                        case 119:
2696                            encodedcontroller = _lev_ctrl_CC119_EXT;
2697                            break;
2698    
2699                      default:                      default:
2700                          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");
2701                  }                  }
# Line 2451  namespace { Line 2997  namespace {
2997       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
2998       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
2999       *       *
3000         * @param pProgress - callback function for progress notification
3001       * @throws gig::Exception if samples cannot be dereferenced       * @throws gig::Exception if samples cannot be dereferenced
3002       */       */
3003      void Region::UpdateChunks() {      void Region::UpdateChunks(progress_t* pProgress) {
3004          // in the gig format we don't care about the Region's sample reference          // in the gig format we don't care about the Region's sample reference
3005          // but we still have to provide some existing one to not corrupt the          // but we still have to provide some existing one to not corrupt the
3006          // file, so to avoid the latter we simply always assign the sample of          // file, so to avoid the latter we simply always assign the sample of
# Line 2461  namespace { Line 3008  namespace {
3008          pSample = pDimensionRegions[0]->pSample;          pSample = pDimensionRegions[0]->pSample;
3009    
3010          // first update base class's chunks          // first update base class's chunks
3011          DLS::Region::UpdateChunks();          DLS::Region::UpdateChunks(pProgress);
3012    
3013          // update dimension region's chunks          // update dimension region's chunks
3014          for (int i = 0; i < DimensionRegions; i++) {          for (int i = 0; i < DimensionRegions; i++) {
3015              pDimensionRegions[i]->UpdateChunks();              pDimensionRegions[i]->UpdateChunks(pProgress);
3016          }          }
3017    
3018          File* pFile = (File*) GetParent()->GetParent();          File* pFile = (File*) GetParent()->GetParent();
# Line 2481  namespace { Line 3028  namespace {
3028              memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize);              memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize);
3029    
3030              // move 3prg to last position              // move 3prg to last position
3031              pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0);              pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), (RIFF::Chunk*)NULL);
3032          }          }
3033    
3034          // update dimension definitions in '3lnk' chunk          // update dimension definitions in '3lnk' chunk
# Line 2627  namespace { Line 3174  namespace {
3174       *                        dimension bits limit is violated       *                        dimension bits limit is violated
3175       */       */
3176      void Region::AddDimension(dimension_def_t* pDimDef) {      void Region::AddDimension(dimension_def_t* pDimDef) {
3177            // some initial sanity checks of the given dimension definition
3178            if (pDimDef->zones < 2)
3179                throw gig::Exception("Could not add new dimension, amount of requested zones must always be at least two");
3180            if (pDimDef->bits < 1)
3181                throw gig::Exception("Could not add new dimension, amount of requested requested zone bits must always be at least one");
3182            if (pDimDef->dimension == dimension_samplechannel) {
3183                if (pDimDef->zones != 2)
3184                    throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zones must always be 2 for this dimension type");
3185                if (pDimDef->bits != 1)
3186                    throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zone bits must always be 1 for this dimension type");
3187            }
3188    
3189          // check if max. amount of dimensions reached          // check if max. amount of dimensions reached
3190          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
3191          const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5;          const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5;
# Line 2802  namespace { Line 3361  namespace {
3361          if (pDimDef->dimension == dimension_layer) Layers = 1;          if (pDimDef->dimension == dimension_layer) Layers = 1;
3362      }      }
3363    
3364        /** @brief Delete one split zone of a dimension (decrement zone amount).
3365         *
3366         * Instead of deleting an entire dimensions, this method will only delete
3367         * one particular split zone given by @a zone of the Region's dimension
3368         * given by @a type. So this method will simply decrement the amount of
3369         * zones by one of the dimension in question. To be able to do that, the
3370         * respective dimension must exist on this Region and it must have at least
3371         * 3 zones. All DimensionRegion objects associated with the zone will be
3372         * deleted.
3373         *
3374         * @param type - identifies the dimension where a zone shall be deleted
3375         * @param zone - index of the dimension split zone that shall be deleted
3376         * @throws gig::Exception if requested zone could not be deleted
3377         */
3378        void Region::DeleteDimensionZone(dimension_t type, int zone) {
3379            dimension_def_t* oldDef = GetDimensionDefinition(type);
3380            if (!oldDef)
3381                throw gig::Exception("Could not delete dimension zone, no such dimension of given type");
3382            if (oldDef->zones <= 2)
3383                throw gig::Exception("Could not delete dimension zone, because it would end up with only one zone.");
3384            if (zone < 0 || zone >= oldDef->zones)
3385                throw gig::Exception("Could not delete dimension zone, requested zone index out of bounds.");
3386    
3387            const int newZoneSize = oldDef->zones - 1;
3388    
3389            // create a temporary Region which just acts as a temporary copy
3390            // container and will be deleted at the end of this function and will
3391            // also not be visible through the API during this process
3392            gig::Region* tempRgn = NULL;
3393            {
3394                // adding these temporary chunks is probably not even necessary
3395                Instrument* instr = static_cast<Instrument*>(GetParent());
3396                RIFF::List* pCkInstrument = instr->pCkInstrument;
3397                RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
3398                if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
3399                RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
3400                tempRgn = new Region(instr, rgn);
3401            }
3402    
3403            // copy this region's dimensions (with already the dimension split size
3404            // requested by the arguments of this method call) to the temporary
3405            // region, and don't use Region::CopyAssign() here for this task, since
3406            // it would also alter fast lookup helper variables here and there
3407            dimension_def_t newDef;
3408            for (int i = 0; i < Dimensions; ++i) {
3409                dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3410                // is this the dimension requested by the method arguments? ...
3411                if (def.dimension == type) { // ... if yes, decrement zone amount by one
3412                    def.zones = newZoneSize;
3413                    if ((1 << (def.bits - 1)) == def.zones) def.bits--;
3414                    newDef = def;
3415                }
3416                tempRgn->AddDimension(&def);
3417            }
3418    
3419            // find the dimension index in the tempRegion which is the dimension
3420            // type passed to this method (paranoidly expecting different order)
3421            int tempReducedDimensionIndex = -1;
3422            for (int d = 0; d < tempRgn->Dimensions; ++d) {
3423                if (tempRgn->pDimensionDefinitions[d].dimension == type) {
3424                    tempReducedDimensionIndex = d;
3425                    break;
3426                }
3427            }
3428    
3429            // copy dimension regions from this region to the temporary region
3430            for (int iDst = 0; iDst < 256; ++iDst) {
3431                DimensionRegion* dstDimRgn = tempRgn->pDimensionRegions[iDst];
3432                if (!dstDimRgn) continue;
3433                std::map<dimension_t,int> dimCase;
3434                bool isValidZone = true;
3435                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3436                    const int dstBits = tempRgn->pDimensionDefinitions[d].bits;
3437                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3438                        (iDst >> baseBits) & ((1 << dstBits) - 1);
3439                    baseBits += dstBits;
3440                    // there are also DimensionRegion objects of unused zones, skip them
3441                    if (dimCase[tempRgn->pDimensionDefinitions[d].dimension] >= tempRgn->pDimensionDefinitions[d].zones) {
3442                        isValidZone = false;
3443                        break;
3444                    }
3445                }
3446                if (!isValidZone) continue;
3447                // a bit paranoid: cope with the chance that the dimensions would
3448                // have different order in source and destination regions
3449                const bool isLastZone = (dimCase[type] == newZoneSize - 1);
3450                if (dimCase[type] >= zone) dimCase[type]++;
3451                DimensionRegion* srcDimRgn = GetDimensionRegionByBit(dimCase);
3452                dstDimRgn->CopyAssign(srcDimRgn);
3453                // if this is the upper most zone of the dimension passed to this
3454                // method, then correct (raise) its upper limit to 127
3455                if (newDef.split_type == split_type_normal && isLastZone)
3456                    dstDimRgn->DimensionUpperLimits[tempReducedDimensionIndex] = 127;
3457            }
3458    
3459            // now tempRegion's dimensions and DimensionRegions basically reflect
3460            // what we wanted to get for this actual Region here, so we now just
3461            // delete and recreate the dimension in question with the new amount
3462            // zones and then copy back from tempRegion      
3463            DeleteDimension(oldDef);
3464            AddDimension(&newDef);
3465            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3466                DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc];
3467                if (!srcDimRgn) continue;
3468                std::map<dimension_t,int> dimCase;
3469                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3470                    const int srcBits = tempRgn->pDimensionDefinitions[d].bits;
3471                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3472                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3473                    baseBits += srcBits;
3474                }
3475                // a bit paranoid: cope with the chance that the dimensions would
3476                // have different order in source and destination regions
3477                DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase);
3478                if (!dstDimRgn) continue;
3479                dstDimRgn->CopyAssign(srcDimRgn);
3480            }
3481    
3482            // delete temporary region
3483            delete tempRgn;
3484    
3485            UpdateVelocityTable();
3486        }
3487    
3488        /** @brief Divide split zone of a dimension in two (increment zone amount).
3489         *
3490         * This will increment the amount of zones for the dimension (given by
3491         * @a type) by one. It will do so by dividing the zone (given by @a zone)
3492         * in the middle of its zone range in two. So the two zones resulting from
3493         * the zone being splitted, will be an equivalent copy regarding all their
3494         * articulation informations and sample reference. The two zones will only
3495         * differ in their zone's upper limit
3496         * (DimensionRegion::DimensionUpperLimits).
3497         *
3498         * @param type - identifies the dimension where a zone shall be splitted
3499         * @param zone - index of the dimension split zone that shall be splitted
3500         * @throws gig::Exception if requested zone could not be splitted
3501         */
3502        void Region::SplitDimensionZone(dimension_t type, int zone) {
3503            dimension_def_t* oldDef = GetDimensionDefinition(type);
3504            if (!oldDef)
3505                throw gig::Exception("Could not split dimension zone, no such dimension of given type");
3506            if (zone < 0 || zone >= oldDef->zones)
3507                throw gig::Exception("Could not split dimension zone, requested zone index out of bounds.");
3508    
3509            const int newZoneSize = oldDef->zones + 1;
3510    
3511            // create a temporary Region which just acts as a temporary copy
3512            // container and will be deleted at the end of this function and will
3513            // also not be visible through the API during this process
3514            gig::Region* tempRgn = NULL;
3515            {
3516                // adding these temporary chunks is probably not even necessary
3517                Instrument* instr = static_cast<Instrument*>(GetParent());
3518                RIFF::List* pCkInstrument = instr->pCkInstrument;
3519                RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
3520                if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
3521                RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
3522                tempRgn = new Region(instr, rgn);
3523            }
3524    
3525            // copy this region's dimensions (with already the dimension split size
3526            // requested by the arguments of this method call) to the temporary
3527            // region, and don't use Region::CopyAssign() here for this task, since
3528            // it would also alter fast lookup helper variables here and there
3529            dimension_def_t newDef;
3530            for (int i = 0; i < Dimensions; ++i) {
3531                dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3532                // is this the dimension requested by the method arguments? ...
3533                if (def.dimension == type) { // ... if yes, increment zone amount by one
3534                    def.zones = newZoneSize;
3535                    if ((1 << oldDef->bits) < newZoneSize) def.bits++;
3536                    newDef = def;
3537                }
3538                tempRgn->AddDimension(&def);
3539            }
3540    
3541            // find the dimension index in the tempRegion which is the dimension
3542            // type passed to this method (paranoidly expecting different order)
3543            int tempIncreasedDimensionIndex = -1;
3544            for (int d = 0; d < tempRgn->Dimensions; ++d) {
3545                if (tempRgn->pDimensionDefinitions[d].dimension == type) {
3546                    tempIncreasedDimensionIndex = d;
3547                    break;
3548                }
3549            }
3550    
3551            // copy dimension regions from this region to the temporary region
3552            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3553                DimensionRegion* srcDimRgn = pDimensionRegions[iSrc];
3554                if (!srcDimRgn) continue;
3555                std::map<dimension_t,int> dimCase;
3556                bool isValidZone = true;
3557                for (int d = 0, baseBits = 0; d < Dimensions; ++d) {
3558                    const int srcBits = pDimensionDefinitions[d].bits;
3559                    dimCase[pDimensionDefinitions[d].dimension] =
3560                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3561                    // there are also DimensionRegion objects for unused zones, skip them
3562                    if (dimCase[pDimensionDefinitions[d].dimension] >= pDimensionDefinitions[d].zones) {
3563                        isValidZone = false;
3564                        break;
3565                    }
3566                    baseBits += srcBits;
3567                }
3568                if (!isValidZone) continue;
3569                // a bit paranoid: cope with the chance that the dimensions would
3570                // have different order in source and destination regions            
3571                if (dimCase[type] > zone) dimCase[type]++;
3572                DimensionRegion* dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase);
3573                dstDimRgn->CopyAssign(srcDimRgn);
3574                // if this is the requested zone to be splitted, then also copy
3575                // the source DimensionRegion to the newly created target zone
3576                // and set the old zones upper limit lower
3577                if (dimCase[type] == zone) {
3578                    // lower old zones upper limit
3579                    if (newDef.split_type == split_type_normal) {
3580                        const int high =
3581                            dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex];
3582                        int low = 0;
3583                        if (zone > 0) {
3584                            std::map<dimension_t,int> lowerCase = dimCase;
3585                            lowerCase[type]--;
3586                            DimensionRegion* dstDimRgnLow = tempRgn->GetDimensionRegionByBit(lowerCase);
3587                            low = dstDimRgnLow->DimensionUpperLimits[tempIncreasedDimensionIndex];
3588                        }
3589                        dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex] = low + (high - low) / 2;
3590                    }
3591                    // fill the newly created zone of the divided zone as well
3592                    dimCase[type]++;
3593                    dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase);
3594                    dstDimRgn->CopyAssign(srcDimRgn);
3595                }
3596            }
3597    
3598            // now tempRegion's dimensions and DimensionRegions basically reflect
3599            // what we wanted to get for this actual Region here, so we now just
3600            // delete and recreate the dimension in question with the new amount
3601            // zones and then copy back from tempRegion      
3602            DeleteDimension(oldDef);
3603            AddDimension(&newDef);
3604            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3605                DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc];
3606                if (!srcDimRgn) continue;
3607                std::map<dimension_t,int> dimCase;
3608                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3609                    const int srcBits = tempRgn->pDimensionDefinitions[d].bits;
3610                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3611                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3612                    baseBits += srcBits;
3613                }
3614                // a bit paranoid: cope with the chance that the dimensions would
3615                // have different order in source and destination regions
3616                DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase);
3617                if (!dstDimRgn) continue;
3618                dstDimRgn->CopyAssign(srcDimRgn);
3619            }
3620    
3621            // delete temporary region
3622            delete tempRgn;
3623    
3624            UpdateVelocityTable();
3625        }
3626    
3627        /** @brief Change type of an existing dimension.
3628         *
3629         * Alters the dimension type of a dimension already existing on this
3630         * region. If there is currently no dimension on this Region with type
3631         * @a oldType, then this call with throw an Exception. Likewise there are
3632         * cases where the requested dimension type cannot be performed. For example
3633         * if the new dimension type shall be gig::dimension_samplechannel, and the
3634         * current dimension has more than 2 zones. In such cases an Exception is
3635         * thrown as well.
3636         *
3637         * @param oldType - identifies the existing dimension to be changed
3638         * @param newType - to which dimension type it should be changed to
3639         * @throws gig::Exception if requested change cannot be performed
3640         */
3641        void Region::SetDimensionType(dimension_t oldType, dimension_t newType) {
3642            if (oldType == newType) return;
3643            dimension_def_t* def = GetDimensionDefinition(oldType);
3644            if (!def)
3645                throw gig::Exception("No dimension with provided old dimension type exists on this region");
3646            if (newType == dimension_samplechannel && def->zones != 2)
3647                throw gig::Exception("Cannot change to dimension type 'sample channel', because existing dimension does not have 2 zones");
3648            if (GetDimensionDefinition(newType))
3649                throw gig::Exception("There is already a dimension with requested new dimension type on this region");
3650            def->dimension  = newType;
3651            def->split_type = __resolveSplitType(newType);
3652        }
3653    
3654        DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {
3655            uint8_t bits[8] = {};
3656            for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();
3657                 it != DimCase.end(); ++it)
3658            {
3659                for (int d = 0; d < Dimensions; ++d) {
3660                    if (pDimensionDefinitions[d].dimension == it->first) {
3661                        bits[d] = it->second;
3662                        goto nextDimCaseSlice;
3663                    }
3664                }
3665                assert(false); // do crash ... too harsh maybe ? ignore it instead ?
3666                nextDimCaseSlice:
3667                ; // noop
3668            }
3669            return GetDimensionRegionByBit(bits);
3670        }
3671    
3672        /**
3673         * Searches in the current Region for a dimension of the given dimension
3674         * type and returns the precise configuration of that dimension in this
3675         * Region.
3676         *
3677         * @param type - dimension type of the sought dimension
3678         * @returns dimension definition or NULL if there is no dimension with
3679         *          sought type in this Region.
3680         */
3681        dimension_def_t* Region::GetDimensionDefinition(dimension_t type) {
3682            for (int i = 0; i < Dimensions; ++i)
3683                if (pDimensionDefinitions[i].dimension == type)
3684                    return &pDimensionDefinitions[i];
3685            return NULL;
3686        }
3687    
3688      Region::~Region() {      Region::~Region() {
3689          for (int i = 0; i < 256; i++) {          for (int i = 0; i < 256; i++) {
3690              if (pDimensionRegions[i]) delete pDimensionRegions[i];              if (pDimensionRegions[i]) delete pDimensionRegions[i];
# Line 2859  namespace { Line 3742  namespace {
3742              }              }
3743              bitpos += pDimensionDefinitions[i].bits;              bitpos += pDimensionDefinitions[i].bits;
3744          }          }
3745          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx & 255];
3746            if (!dimreg) return NULL;
3747          if (veldim != -1) {          if (veldim != -1) {
3748              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
3749              if (dimreg->VelocityTable) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
3750                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3751              else // normal split type              else // normal split type
3752                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3753    
3754              dimregidx |= bits << velbitpos;              const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3755              dimreg = pDimensionRegions[dimregidx];              dimregidx |= (bits & limiter_mask) << velbitpos;
3756                dimreg = pDimensionRegions[dimregidx & 255];
3757          }          }
3758          return dimreg;          return dimreg;
3759      }      }
3760    
3761        int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) {
3762            uint8_t bits;
3763            int veldim = -1;
3764            int velbitpos;
3765            int bitpos = 0;
3766            int dimregidx = 0;
3767            for (uint i = 0; i < Dimensions; i++) {
3768                if (pDimensionDefinitions[i].dimension == dimension_velocity) {
3769                    // the velocity dimension must be handled after the other dimensions
3770                    veldim = i;
3771                    velbitpos = bitpos;
3772                } else {
3773                    switch (pDimensionDefinitions[i].split_type) {
3774                        case split_type_normal:
3775                            if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
3776                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
3777                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
3778                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
3779                                }
3780                            } else {
3781                                // gig2: evenly sized zones
3782                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
3783                            }
3784                            break;
3785                        case split_type_bit: // the value is already the sought dimension bit number
3786                            const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
3787                            bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed
3788                            break;
3789                    }
3790                    dimregidx |= bits << bitpos;
3791                }
3792                bitpos += pDimensionDefinitions[i].bits;
3793            }
3794            dimregidx &= 255;
3795            DimensionRegion* dimreg = pDimensionRegions[dimregidx];
3796            if (!dimreg) return -1;
3797            if (veldim != -1) {
3798                // (dimreg is now the dimension region for the lowest velocity)
3799                if (dimreg->VelocityTable) // custom defined zone ranges
3800                    bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3801                else // normal split type
3802                    bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3803    
3804                const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3805                dimregidx |= (bits & limiter_mask) << velbitpos;
3806                dimregidx &= 255;
3807            }
3808            return dimregidx;
3809        }
3810    
3811      /**      /**
3812       * Returns the appropriate DimensionRegion for the given dimension bit       * Returns the appropriate DimensionRegion for the given dimension bit
3813       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>
# Line 2921  namespace { Line 3856  namespace {
3856          }          }
3857          return NULL;          return NULL;
3858      }      }
3859        
3860        /**
3861         * Make a (semi) deep copy of the Region object given by @a orig
3862         * and assign it to this object.
3863         *
3864         * Note that all sample pointers referenced by @a orig are simply copied as
3865         * memory address. Thus the respective samples are shared, not duplicated!
3866         *
3867         * @param orig - original Region object to be copied from
3868         */
3869        void Region::CopyAssign(const Region* orig) {
3870            CopyAssign(orig, NULL);
3871        }
3872        
3873        /**
3874         * Make a (semi) deep copy of the Region object given by @a orig and
3875         * assign it to this object
3876         *
3877         * @param mSamples - crosslink map between the foreign file's samples and
3878         *                   this file's samples
3879         */
3880        void Region::CopyAssign(const Region* orig, const std::map<Sample*,Sample*>* mSamples) {
3881            // handle base classes
3882            DLS::Region::CopyAssign(orig);
3883            
3884            if (mSamples && mSamples->count((gig::Sample*)orig->pSample)) {
3885                pSample = mSamples->find((gig::Sample*)orig->pSample)->second;
3886            }
3887            
3888            // handle own member variables
3889            for (int i = Dimensions - 1; i >= 0; --i) {
3890                DeleteDimension(&pDimensionDefinitions[i]);
3891            }
3892            Layers = 0; // just to be sure
3893            for (int i = 0; i < orig->Dimensions; i++) {
3894                // we need to copy the dim definition here, to avoid the compiler
3895                // complaining about const-ness issue
3896                dimension_def_t def = orig->pDimensionDefinitions[i];
3897                AddDimension(&def);
3898            }
3899            for (int i = 0; i < 256; i++) {
3900                if (pDimensionRegions[i] && orig->pDimensionRegions[i]) {
3901                    pDimensionRegions[i]->CopyAssign(
3902                        orig->pDimensionRegions[i],
3903                        mSamples
3904                    );
3905                }
3906            }
3907            Layers = orig->Layers;
3908        }
3909    
3910    
3911    // *************** MidiRule ***************
3912    // *
3913    
3914        MidiRuleCtrlTrigger::MidiRuleCtrlTrigger(RIFF::Chunk* _3ewg) {
3915            _3ewg->SetPos(36);
3916            Triggers = _3ewg->ReadUint8();
3917            _3ewg->SetPos(40);
3918            ControllerNumber = _3ewg->ReadUint8();
3919            _3ewg->SetPos(46);
3920            for (int i = 0 ; i < Triggers ; i++) {
3921                pTriggers[i].TriggerPoint = _3ewg->ReadUint8();
3922                pTriggers[i].Descending = _3ewg->ReadUint8();
3923                pTriggers[i].VelSensitivity = _3ewg->ReadUint8();
3924                pTriggers[i].Key = _3ewg->ReadUint8();
3925                pTriggers[i].NoteOff = _3ewg->ReadUint8();
3926                pTriggers[i].Velocity = _3ewg->ReadUint8();
3927                pTriggers[i].OverridePedal = _3ewg->ReadUint8();
3928                _3ewg->ReadUint8();
3929            }
3930        }
3931    
3932        MidiRuleCtrlTrigger::MidiRuleCtrlTrigger() :
3933            ControllerNumber(0),
3934            Triggers(0) {
3935        }
3936    
3937        void MidiRuleCtrlTrigger::UpdateChunks(uint8_t* pData) const {
3938            pData[32] = 4;
3939            pData[33] = 16;
3940            pData[36] = Triggers;
3941            pData[40] = ControllerNumber;
3942            for (int i = 0 ; i < Triggers ; i++) {
3943                pData[46 + i * 8] = pTriggers[i].TriggerPoint;
3944                pData[47 + i * 8] = pTriggers[i].Descending;
3945                pData[48 + i * 8] = pTriggers[i].VelSensitivity;
3946                pData[49 + i * 8] = pTriggers[i].Key;
3947                pData[50 + i * 8] = pTriggers[i].NoteOff;
3948                pData[51 + i * 8] = pTriggers[i].Velocity;
3949                pData[52 + i * 8] = pTriggers[i].OverridePedal;
3950            }
3951        }
3952    
3953        MidiRuleLegato::MidiRuleLegato(RIFF::Chunk* _3ewg) {
3954            _3ewg->SetPos(36);
3955            LegatoSamples = _3ewg->ReadUint8(); // always 12
3956            _3ewg->SetPos(40);
3957            BypassUseController = _3ewg->ReadUint8();
3958            BypassKey = _3ewg->ReadUint8();
3959            BypassController = _3ewg->ReadUint8();
3960            ThresholdTime = _3ewg->ReadUint16();
3961            _3ewg->ReadInt16();
3962            ReleaseTime = _3ewg->ReadUint16();
3963            _3ewg->ReadInt16();
3964            KeyRange.low = _3ewg->ReadUint8();
3965            KeyRange.high = _3ewg->ReadUint8();
3966            _3ewg->SetPos(64);
3967            ReleaseTriggerKey = _3ewg->ReadUint8();
3968            AltSustain1Key = _3ewg->ReadUint8();
3969            AltSustain2Key = _3ewg->ReadUint8();
3970        }
3971    
3972        MidiRuleLegato::MidiRuleLegato() :
3973            LegatoSamples(12),
3974            BypassUseController(false),
3975            BypassKey(0),
3976            BypassController(1),
3977            ThresholdTime(20),
3978            ReleaseTime(20),
3979            ReleaseTriggerKey(0),
3980            AltSustain1Key(0),
3981            AltSustain2Key(0)
3982        {
3983            KeyRange.low = KeyRange.high = 0;
3984        }
3985    
3986        void MidiRuleLegato::UpdateChunks(uint8_t* pData) const {
3987            pData[32] = 0;
3988            pData[33] = 16;
3989            pData[36] = LegatoSamples;
3990            pData[40] = BypassUseController;
3991            pData[41] = BypassKey;
3992            pData[42] = BypassController;
3993            store16(&pData[43], ThresholdTime);
3994            store16(&pData[47], ReleaseTime);
3995            pData[51] = KeyRange.low;
3996            pData[52] = KeyRange.high;
3997            pData[64] = ReleaseTriggerKey;
3998            pData[65] = AltSustain1Key;
3999            pData[66] = AltSustain2Key;
4000        }
4001    
4002        MidiRuleAlternator::MidiRuleAlternator(RIFF::Chunk* _3ewg) {
4003            _3ewg->SetPos(36);
4004            Articulations = _3ewg->ReadUint8();
4005            int flags = _3ewg->ReadUint8();
4006            Polyphonic = flags & 8;
4007            Chained = flags & 4;
4008            Selector = (flags & 2) ? selector_controller :
4009                (flags & 1) ? selector_key_switch : selector_none;
4010            Patterns = _3ewg->ReadUint8();
4011            _3ewg->ReadUint8(); // chosen row
4012            _3ewg->ReadUint8(); // unknown
4013            _3ewg->ReadUint8(); // unknown
4014            _3ewg->ReadUint8(); // unknown
4015            KeySwitchRange.low = _3ewg->ReadUint8();
4016            KeySwitchRange.high = _3ewg->ReadUint8();
4017            Controller = _3ewg->ReadUint8();
4018            PlayRange.low = _3ewg->ReadUint8();
4019            PlayRange.high = _3ewg->ReadUint8();
4020    
4021            int n = std::min(int(Articulations), 32);
4022            for (int i = 0 ; i < n ; i++) {
4023                _3ewg->ReadString(pArticulations[i], 32);
4024            }
4025            _3ewg->SetPos(1072);
4026            n = std::min(int(Patterns), 32);
4027            for (int i = 0 ; i < n ; i++) {
4028                _3ewg->ReadString(pPatterns[i].Name, 16);
4029                pPatterns[i].Size = _3ewg->ReadUint8();
4030                _3ewg->Read(&pPatterns[i][0], 1, 32);
4031            }
4032        }
4033    
4034        MidiRuleAlternator::MidiRuleAlternator() :
4035            Articulations(0),
4036            Patterns(0),
4037            Selector(selector_none),
4038            Controller(0),
4039            Polyphonic(false),
4040            Chained(false)
4041        {
4042            PlayRange.low = PlayRange.high = 0;
4043            KeySwitchRange.low = KeySwitchRange.high = 0;
4044        }
4045    
4046        void MidiRuleAlternator::UpdateChunks(uint8_t* pData) const {
4047            pData[32] = 3;
4048            pData[33] = 16;
4049            pData[36] = Articulations;
4050            pData[37] = (Polyphonic ? 8 : 0) | (Chained ? 4 : 0) |
4051                (Selector == selector_controller ? 2 :
4052                 (Selector == selector_key_switch ? 1 : 0));
4053            pData[38] = Patterns;
4054    
4055            pData[43] = KeySwitchRange.low;
4056            pData[44] = KeySwitchRange.high;
4057            pData[45] = Controller;
4058            pData[46] = PlayRange.low;
4059            pData[47] = PlayRange.high;
4060    
4061            char* str = reinterpret_cast<char*>(pData);
4062            int pos = 48;
4063            int n = std::min(int(Articulations), 32);
4064            for (int i = 0 ; i < n ; i++, pos += 32) {
4065                strncpy(&str[pos], pArticulations[i].c_str(), 32);
4066            }
4067    
4068            pos = 1072;
4069            n = std::min(int(Patterns), 32);
4070            for (int i = 0 ; i < n ; i++, pos += 49) {
4071                strncpy(&str[pos], pPatterns[i].Name.c_str(), 16);
4072                pData[pos + 16] = pPatterns[i].Size;
4073                memcpy(&pData[pos + 16], &(pPatterns[i][0]), 32);
4074            }
4075        }
4076    
4077    // *************** Script ***************
4078    // *
4079    
4080        Script::Script(ScriptGroup* group, RIFF::Chunk* ckScri) {
4081            pGroup = group;
4082            pChunk = ckScri;
4083            if (ckScri) { // object is loaded from file ...
4084                // read header
4085                uint32_t headerSize = ckScri->ReadUint32();
4086                Compression = (Compression_t) ckScri->ReadUint32();
4087                Encoding    = (Encoding_t) ckScri->ReadUint32();
4088                Language    = (Language_t) ckScri->ReadUint32();
4089                Bypass      = (Language_t) ckScri->ReadUint32() & 1;
4090                crc         = ckScri->ReadUint32();
4091                uint32_t nameSize = ckScri->ReadUint32();
4092                Name.resize(nameSize, ' ');
4093                for (int i = 0; i < nameSize; ++i)
4094                    Name[i] = ckScri->ReadUint8();
4095                // to handle potential future extensions of the header
4096                ckScri->SetPos(sizeof(int32_t) + headerSize);
4097                // read actual script data
4098                uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();
4099                data.resize(scriptSize);
4100                for (int i = 0; i < scriptSize; ++i)
4101                    data[i] = ckScri->ReadUint8();
4102            } else { // this is a new script object, so just initialize it as such ...
4103                Compression = COMPRESSION_NONE;
4104                Encoding = ENCODING_ASCII;
4105                Language = LANGUAGE_NKSP;
4106                Bypass   = false;
4107                crc      = 0;
4108                Name     = "Unnamed Script";
4109            }
4110        }
4111    
4112        Script::~Script() {
4113        }
4114    
4115        /**
4116         * Returns the current script (i.e. as source code) in text format.
4117         */
4118        String Script::GetScriptAsText() {
4119            String s;
4120            s.resize(data.size(), ' ');
4121            memcpy(&s[0], &data[0], data.size());
4122            return s;
4123        }
4124    
4125        /**
4126         * Replaces the current script with the new script source code text given
4127         * by @a text.
4128         *
4129         * @param text - new script source code
4130         */
4131        void Script::SetScriptAsText(const String& text) {
4132            data.resize(text.size());
4133            memcpy(&data[0], &text[0], text.size());
4134        }
4135    
4136        /**
4137         * Apply this script to the respective RIFF chunks. You have to call
4138         * File::Save() to make changes persistent.
4139         *
4140         * Usually there is absolutely no need to call this method explicitly.
4141         * It will be called automatically when File::Save() was called.
4142         *
4143         * @param pProgress - callback function for progress notification
4144         */
4145        void Script::UpdateChunks(progress_t* pProgress) {
4146            // recalculate CRC32 check sum
4147            __resetCRC(crc);
4148            __calculateCRC(&data[0], data.size(), crc);
4149            __encodeCRC(crc);
4150            // make sure chunk exists and has the required size
4151            const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size();
4152            if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);
4153            else pChunk->Resize(chunkSize);
4154            // fill the chunk data to be written to disk
4155            uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();
4156            int pos = 0;
4157            store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size
4158            pos += sizeof(int32_t);
4159            store32(&pData[pos], Compression);
4160            pos += sizeof(int32_t);
4161            store32(&pData[pos], Encoding);
4162            pos += sizeof(int32_t);
4163            store32(&pData[pos], Language);
4164            pos += sizeof(int32_t);
4165            store32(&pData[pos], Bypass ? 1 : 0);
4166            pos += sizeof(int32_t);
4167            store32(&pData[pos], crc);
4168            pos += sizeof(int32_t);
4169            store32(&pData[pos], Name.size());
4170            pos += sizeof(int32_t);
4171            for (int i = 0; i < Name.size(); ++i, ++pos)
4172                pData[pos] = Name[i];
4173            for (int i = 0; i < data.size(); ++i, ++pos)
4174                pData[pos] = data[i];
4175        }
4176    
4177        /**
4178         * Move this script from its current ScriptGroup to another ScriptGroup
4179         * given by @a pGroup.
4180         *
4181         * @param pGroup - script's new group
4182         */
4183        void Script::SetGroup(ScriptGroup* pGroup) {
4184            if (this->pGroup = pGroup) return;
4185            if (pChunk)
4186                pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList);
4187            this->pGroup = pGroup;
4188        }
4189    
4190        /**
4191         * Returns the script group this script currently belongs to. Each script
4192         * is a member of exactly one ScriptGroup.
4193         *
4194         * @returns current script group
4195         */
4196        ScriptGroup* Script::GetGroup() const {
4197            return pGroup;
4198        }
4199    
4200        void Script::RemoveAllScriptReferences() {
4201            File* pFile = pGroup->pFile;
4202            for (int i = 0; pFile->GetInstrument(i); ++i) {
4203                Instrument* instr = pFile->GetInstrument(i);
4204                instr->RemoveScript(this);
4205            }
4206        }
4207    
4208    // *************** ScriptGroup ***************
4209    // *
4210    
4211        ScriptGroup::ScriptGroup(File* file, RIFF::List* lstRTIS) {
4212            pFile = file;
4213            pList = lstRTIS;
4214            pScripts = NULL;
4215            if (lstRTIS) {
4216                RIFF::Chunk* ckName = lstRTIS->GetSubChunk(CHUNK_ID_LSNM);
4217                ::LoadString(ckName, Name);
4218            } else {
4219                Name = "Default Group";
4220            }
4221        }
4222    
4223        ScriptGroup::~ScriptGroup() {
4224            if (pScripts) {
4225                std::list<Script*>::iterator iter = pScripts->begin();
4226                std::list<Script*>::iterator end  = pScripts->end();
4227                while (iter != end) {
4228                    delete *iter;
4229                    ++iter;
4230                }
4231                delete pScripts;
4232            }
4233        }
4234    
4235        /**
4236         * Apply this script group to the respective RIFF chunks. You have to call
4237         * File::Save() to make changes persistent.
4238         *
4239         * Usually there is absolutely no need to call this method explicitly.
4240         * It will be called automatically when File::Save() was called.
4241         *
4242         * @param pProgress - callback function for progress notification
4243         */
4244        void ScriptGroup::UpdateChunks(progress_t* pProgress) {
4245            if (pScripts) {
4246                if (!pList)
4247                    pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS);
4248    
4249                // now store the name of this group as <LSNM> chunk as subchunk of the <RTIS> list chunk
4250                ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64);
4251    
4252                for (std::list<Script*>::iterator it = pScripts->begin();
4253                     it != pScripts->end(); ++it)
4254                {
4255                    (*it)->UpdateChunks(pProgress);
4256                }
4257            }
4258        }
4259    
4260        /** @brief Get instrument script.
4261         *
4262         * Returns the real-time instrument script with the given index.
4263         *
4264         * @param index - number of the sought script (0..n)
4265         * @returns sought script or NULL if there's no such script
4266         */
4267        Script* ScriptGroup::GetScript(uint index) {
4268            if (!pScripts) LoadScripts();
4269            std::list<Script*>::iterator it = pScripts->begin();
4270            for (uint i = 0; it != pScripts->end(); ++i, ++it)
4271                if (i == index) return *it;
4272            return NULL;
4273        }
4274    
4275        /** @brief Add new instrument script.
4276         *
4277         * Adds a new real-time instrument script to the file. The script is not
4278         * actually used / executed unless it is referenced by an instrument to be
4279         * used. This is similar to samples, which you can add to a file, without
4280         * an instrument necessarily actually using it.
4281         *
4282         * You have to call Save() to make this persistent to the file.
4283         *
4284         * @return new empty script object
4285         */
4286        Script* ScriptGroup::AddScript() {
4287            if (!pScripts) LoadScripts();
4288            Script* pScript = new Script(this, NULL);
4289            pScripts->push_back(pScript);
4290            return pScript;
4291        }
4292    
4293        /** @brief Delete an instrument script.
4294         *
4295         * This will delete the given real-time instrument script. References of
4296         * instruments that are using that script will be removed accordingly.
4297         *
4298         * You have to call Save() to make this persistent to the file.
4299         *
4300         * @param pScript - script to delete
4301         * @throws gig::Exception if given script could not be found
4302         */
4303        void ScriptGroup::DeleteScript(Script* pScript) {
4304            if (!pScripts) LoadScripts();
4305            std::list<Script*>::iterator iter =
4306                find(pScripts->begin(), pScripts->end(), pScript);
4307            if (iter == pScripts->end())
4308                throw gig::Exception("Could not delete script, could not find given script");
4309            pScripts->erase(iter);
4310            pScript->RemoveAllScriptReferences();
4311            if (pScript->pChunk)
4312                pScript->pChunk->GetParent()->DeleteSubChunk(pScript->pChunk);
4313            delete pScript;
4314        }
4315    
4316        void ScriptGroup::LoadScripts() {
4317            if (pScripts) return;
4318            pScripts = new std::list<Script*>;
4319            if (!pList) return;
4320    
4321            for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck;
4322                 ck = pList->GetNextSubChunk())
4323            {
4324                if (ck->GetChunkID() == CHUNK_ID_SCRI) {
4325                    pScripts->push_back(new Script(this, ck));
4326                }
4327            }
4328        }
4329    
4330  // *************** Instrument ***************  // *************** Instrument ***************
4331  // *  // *
# Line 2944  namespace { Line 4347  namespace {
4347          PianoReleaseMode = false;          PianoReleaseMode = false;
4348          DimensionKeyRange.low = 0;          DimensionKeyRange.low = 0;
4349          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
4350            pMidiRules = new MidiRule*[3];
4351            pMidiRules[0] = NULL;
4352            pScriptRefs = NULL;
4353    
4354          // Loading          // Loading
4355          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 2958  namespace { Line 4364  namespace {
4364                  PianoReleaseMode       = dimkeystart & 0x01;                  PianoReleaseMode       = dimkeystart & 0x01;
4365                  DimensionKeyRange.low  = dimkeystart >> 1;                  DimensionKeyRange.low  = dimkeystart >> 1;
4366                  DimensionKeyRange.high = _3ewg->ReadUint8();                  DimensionKeyRange.high = _3ewg->ReadUint8();
4367    
4368                    if (_3ewg->GetSize() > 32) {
4369                        // read MIDI rules
4370                        int i = 0;
4371                        _3ewg->SetPos(32);
4372                        uint8_t id1 = _3ewg->ReadUint8();
4373                        uint8_t id2 = _3ewg->ReadUint8();
4374    
4375                        if (id2 == 16) {
4376                            if (id1 == 4) {
4377                                pMidiRules[i++] = new MidiRuleCtrlTrigger(_3ewg);
4378                            } else if (id1 == 0) {
4379                                pMidiRules[i++] = new MidiRuleLegato(_3ewg);
4380                            } else if (id1 == 3) {
4381                                pMidiRules[i++] = new MidiRuleAlternator(_3ewg);
4382                            } else {
4383                                pMidiRules[i++] = new MidiRuleUnknown;
4384                            }
4385                        }
4386                        else if (id1 != 0 || id2 != 0) {
4387                            pMidiRules[i++] = new MidiRuleUnknown;
4388                        }
4389                        //TODO: all the other types of rules
4390    
4391                        pMidiRules[i] = NULL;
4392                    }
4393              }              }
4394          }          }
4395    
# Line 2978  namespace { Line 4410  namespace {
4410              }              }
4411          }          }
4412    
4413            // own gig format extensions
4414            RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS);
4415            if (lst3LS) {
4416                RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4417                if (ckSCSL) {
4418                    int headerSize = ckSCSL->ReadUint32();
4419                    int slotCount  = ckSCSL->ReadUint32();
4420                    if (slotCount) {
4421                        int slotSize  = ckSCSL->ReadUint32();
4422                        ckSCSL->SetPos(headerSize); // in case of future header extensions
4423                        int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions
4424                        for (int i = 0; i < slotCount; ++i) {
4425                            _ScriptPooolEntry e;
4426                            e.fileOffset = ckSCSL->ReadUint32();
4427                            e.bypass     = ckSCSL->ReadUint32() & 1;
4428                            if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions
4429                            scriptPoolFileOffsets.push_back(e);
4430                        }
4431                    }
4432                }
4433            }
4434    
4435          __notify_progress(pProgress, 1.0f); // notify done          __notify_progress(pProgress, 1.0f); // notify done
4436      }      }
4437    
# Line 2994  namespace { Line 4448  namespace {
4448      }      }
4449    
4450      Instrument::~Instrument() {      Instrument::~Instrument() {
4451            for (int i = 0 ; pMidiRules[i] ; i++) {
4452                delete pMidiRules[i];
4453            }
4454            delete[] pMidiRules;
4455            if (pScriptRefs) delete pScriptRefs;
4456      }      }
4457    
4458      /**      /**
# Line 3003  namespace { Line 4462  namespace {
4462       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
4463       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
4464       *       *
4465         * @param pProgress - callback function for progress notification
4466       * @throws gig::Exception if samples cannot be dereferenced       * @throws gig::Exception if samples cannot be dereferenced
4467       */       */
4468      void Instrument::UpdateChunks() {      void Instrument::UpdateChunks(progress_t* pProgress) {
4469          // first update base classes' chunks          // first update base classes' chunks
4470          DLS::Instrument::UpdateChunks();          DLS::Instrument::UpdateChunks(pProgress);
4471    
4472          // update Regions' chunks          // update Regions' chunks
4473          {          {
4474              RegionList::iterator iter = pRegions->begin();              RegionList::iterator iter = pRegions->begin();
4475              RegionList::iterator end  = pRegions->end();              RegionList::iterator end  = pRegions->end();
4476              for (; iter != end; ++iter)              for (; iter != end; ++iter)
4477                  (*iter)->UpdateChunks();                  (*iter)->UpdateChunks(pProgress);
4478          }          }
4479    
4480          // make sure 'lart' RIFF list chunk exists          // make sure 'lart' RIFF list chunk exists
# Line 3040  namespace { Line 4500  namespace {
4500                                      DimensionKeyRange.low << 1;                                      DimensionKeyRange.low << 1;
4501          pData[10] = dimkeystart;          pData[10] = dimkeystart;
4502          pData[11] = DimensionKeyRange.high;          pData[11] = DimensionKeyRange.high;
4503    
4504            if (pMidiRules[0] == 0 && _3ewg->GetSize() >= 34) {
4505                pData[32] = 0;
4506                pData[33] = 0;
4507            } else {
4508                for (int i = 0 ; pMidiRules[i] ; i++) {
4509                    pMidiRules[i]->UpdateChunks(pData);
4510                }
4511            }
4512    
4513            // own gig format extensions
4514           if (ScriptSlotCount()) {
4515               // make sure we have converted the original loaded script file
4516               // offsets into valid Script object pointers
4517               LoadScripts();
4518    
4519               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4520               if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4521               const int slotCount = pScriptRefs->size();
4522               const int headerSize = 3 * sizeof(uint32_t);
4523               const int slotSize  = 2 * sizeof(uint32_t);
4524               const int totalChunkSize = headerSize + slotCount * slotSize;
4525               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4526               if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize);
4527               else ckSCSL->Resize(totalChunkSize);
4528               uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();
4529               int pos = 0;
4530               store32(&pData[pos], headerSize);
4531               pos += sizeof(uint32_t);
4532               store32(&pData[pos], slotCount);
4533               pos += sizeof(uint32_t);
4534               store32(&pData[pos], slotSize);
4535               pos += sizeof(uint32_t);
4536               for (int i = 0; i < slotCount; ++i) {
4537                   // arbitrary value, the actual file offset will be updated in
4538                   // UpdateScriptFileOffsets() after the file has been resized
4539                   int bogusFileOffset = 0;
4540                   store32(&pData[pos], bogusFileOffset);
4541                   pos += sizeof(uint32_t);
4542                   store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
4543                   pos += sizeof(uint32_t);
4544               }
4545           } else {
4546               // no script slots, so get rid of any LS custom RIFF chunks (if any)
4547               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4548               if (lst3LS) pCkInstrument->DeleteSubChunk(lst3LS);
4549           }
4550        }
4551    
4552        void Instrument::UpdateScriptFileOffsets() {
4553           // own gig format extensions
4554           if (pScriptRefs && pScriptRefs->size() > 0) {
4555               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4556               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4557               const int slotCount = pScriptRefs->size();
4558               const int headerSize = 3 * sizeof(uint32_t);
4559               ckSCSL->SetPos(headerSize);
4560               for (int i = 0; i < slotCount; ++i) {
4561                   uint32_t fileOffset =
4562                        (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4563                        (*pScriptRefs)[i].script->pChunk->GetPos() -
4564                        CHUNK_HEADER_SIZE;
4565                   ckSCSL->WriteUint32(&fileOffset);
4566                   // jump over flags entry (containing the bypass flag)
4567                   ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos);
4568               }
4569           }        
4570      }      }
4571    
4572      /**      /**
# Line 3108  namespace { Line 4635  namespace {
4635          UpdateRegionKeyTable();          UpdateRegionKeyTable();
4636      }      }
4637    
4638        /**
4639         * Returns a MIDI rule of the instrument.
4640         *
4641         * The list of MIDI rules, at least in gig v3, always contains at
4642         * most two rules. The second rule can only be the DEF filter
4643         * (which currently isn't supported by libgig).
4644         *
4645         * @param i - MIDI rule number
4646         * @returns   pointer address to MIDI rule number i or NULL if there is none
4647         */
4648        MidiRule* Instrument::GetMidiRule(int i) {
4649            return pMidiRules[i];
4650        }
4651    
4652        /**
4653         * Adds the "controller trigger" MIDI rule to the instrument.
4654         *
4655         * @returns the new MIDI rule
4656         */
4657        MidiRuleCtrlTrigger* Instrument::AddMidiRuleCtrlTrigger() {
4658            delete pMidiRules[0];
4659            MidiRuleCtrlTrigger* r = new MidiRuleCtrlTrigger;
4660            pMidiRules[0] = r;
4661            pMidiRules[1] = 0;
4662            return r;
4663        }
4664    
4665        /**
4666         * Adds the legato MIDI rule to the instrument.
4667         *
4668         * @returns the new MIDI rule
4669         */
4670        MidiRuleLegato* Instrument::AddMidiRuleLegato() {
4671            delete pMidiRules[0];
4672            MidiRuleLegato* r = new MidiRuleLegato;
4673            pMidiRules[0] = r;
4674            pMidiRules[1] = 0;
4675            return r;
4676        }
4677    
4678        /**
4679         * Adds the alternator MIDI rule to the instrument.
4680         *
4681         * @returns the new MIDI rule
4682         */
4683        MidiRuleAlternator* Instrument::AddMidiRuleAlternator() {
4684            delete pMidiRules[0];
4685            MidiRuleAlternator* r = new MidiRuleAlternator;
4686            pMidiRules[0] = r;
4687            pMidiRules[1] = 0;
4688            return r;
4689        }
4690    
4691        /**
4692         * Deletes a MIDI rule from the instrument.
4693         *
4694         * @param i - MIDI rule number
4695         */
4696        void Instrument::DeleteMidiRule(int i) {
4697            delete pMidiRules[i];
4698            pMidiRules[i] = 0;
4699        }
4700    
4701        void Instrument::LoadScripts() {
4702            if (pScriptRefs) return;
4703            pScriptRefs = new std::vector<_ScriptPooolRef>;
4704            if (scriptPoolFileOffsets.empty()) return;
4705            File* pFile = (File*) GetParent();
4706            for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4707                uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset;
4708                for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4709                    ScriptGroup* group = pFile->GetScriptGroup(i);
4710                    for (uint s = 0; group->GetScript(s); ++s) {
4711                        Script* script = group->GetScript(s);
4712                        if (script->pChunk) {
4713                            uint32_t offset = script->pChunk->GetFilePos() -
4714                                              script->pChunk->GetPos() -
4715                                              CHUNK_HEADER_SIZE;
4716                            if (offset == soughtOffset)
4717                            {
4718                                _ScriptPooolRef ref;
4719                                ref.script = script;
4720                                ref.bypass = scriptPoolFileOffsets[k].bypass;
4721                                pScriptRefs->push_back(ref);
4722                                break;
4723                            }
4724                        }
4725                    }
4726                }
4727            }
4728            // we don't need that anymore
4729            scriptPoolFileOffsets.clear();
4730        }
4731    
4732        /** @brief Get instrument script (gig format extension).
4733         *
4734         * Returns the real-time instrument script of instrument script slot
4735         * @a index.
4736         *
4737         * @note This is an own format extension which did not exist i.e. in the
4738         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4739         * gigedit.
4740         *
4741         * @param index - instrument script slot index
4742         * @returns script or NULL if index is out of bounds
4743         */
4744        Script* Instrument::GetScriptOfSlot(uint index) {
4745            LoadScripts();
4746            if (index >= pScriptRefs->size()) return NULL;
4747            return pScriptRefs->at(index).script;
4748        }
4749    
4750        /** @brief Add new instrument script slot (gig format extension).
4751         *
4752         * Add the given real-time instrument script reference to this instrument,
4753         * which shall be executed by the sampler for for this instrument. The
4754         * script will be added to the end of the script list of this instrument.
4755         * The positions of the scripts in the Instrument's Script list are
4756         * relevant, because they define in which order they shall be executed by
4757         * the sampler. For this reason it is also legal to add the same script
4758         * twice to an instrument, for example you might have a script called
4759         * "MyFilter" which performs an event filter task, and you might have
4760         * another script called "MyNoteTrigger" which triggers new notes, then you
4761         * might for example have the following list of scripts on the instrument:
4762         *
4763         * 1. Script "MyFilter"
4764         * 2. Script "MyNoteTrigger"
4765         * 3. Script "MyFilter"
4766         *
4767         * Which would make sense, because the 2nd script launched new events, which
4768         * you might need to filter as well.
4769         *
4770         * There are two ways to disable / "bypass" scripts. You can either disable
4771         * a script locally for the respective script slot on an instrument (i.e. by
4772         * passing @c false to the 2nd argument of this method, or by calling
4773         * SetScriptBypassed()). Or you can disable a script globally for all slots
4774         * and all instruments by setting Script::Bypass.
4775         *
4776         * @note This is an own format extension which did not exist i.e. in the
4777         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4778         * gigedit.
4779         *
4780         * @param pScript - script that shall be executed for this instrument
4781         * @param bypass  - if enabled, the sampler shall skip executing this
4782         *                  script (in the respective list position)
4783         * @see SetScriptBypassed()
4784         */
4785        void Instrument::AddScriptSlot(Script* pScript, bool bypass) {
4786            LoadScripts();
4787            _ScriptPooolRef ref = { pScript, bypass };
4788            pScriptRefs->push_back(ref);
4789        }
4790    
4791        /** @brief Flip two script slots with each other (gig format extension).
4792         *
4793         * Swaps the position of the two given scripts in the Instrument's Script
4794         * list. The positions of the scripts in the Instrument's Script list are
4795         * relevant, because they define in which order they shall be executed by
4796         * the sampler.
4797         *
4798         * @note This is an own format extension which did not exist i.e. in the
4799         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4800         * gigedit.
4801         *
4802         * @param index1 - index of the first script slot to swap
4803         * @param index2 - index of the second script slot to swap
4804         */
4805        void Instrument::SwapScriptSlots(uint index1, uint index2) {
4806            LoadScripts();
4807            if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size())
4808                return;
4809            _ScriptPooolRef tmp = (*pScriptRefs)[index1];
4810            (*pScriptRefs)[index1] = (*pScriptRefs)[index2];
4811            (*pScriptRefs)[index2] = tmp;
4812        }
4813    
4814        /** @brief Remove script slot.
4815         *
4816         * Removes the script slot with the given slot index.
4817         *
4818         * @param index - index of script slot to remove
4819         */
4820        void Instrument::RemoveScriptSlot(uint index) {
4821            LoadScripts();
4822            if (index >= pScriptRefs->size()) return;
4823            pScriptRefs->erase( pScriptRefs->begin() + index );
4824        }
4825    
4826        /** @brief Remove reference to given Script (gig format extension).
4827         *
4828         * This will remove all script slots on the instrument which are referencing
4829         * the given script.
4830         *
4831         * @note This is an own format extension which did not exist i.e. in the
4832         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4833         * gigedit.
4834         *
4835         * @param pScript - script reference to remove from this instrument
4836         * @see RemoveScriptSlot()
4837         */
4838        void Instrument::RemoveScript(Script* pScript) {
4839            LoadScripts();
4840            for (int i = pScriptRefs->size() - 1; i >= 0; --i) {
4841                if ((*pScriptRefs)[i].script == pScript) {
4842                    pScriptRefs->erase( pScriptRefs->begin() + i );
4843                }
4844            }
4845        }
4846    
4847        /** @brief Instrument's amount of script slots.
4848         *
4849         * This method returns the amount of script slots this instrument currently
4850         * uses.
4851         *
4852         * A script slot is a reference of a real-time instrument script to be
4853         * executed by the sampler. The scripts will be executed by the sampler in
4854         * sequence of the slots. One (same) script may be referenced multiple
4855         * times in different slots.
4856         *
4857         * @note This is an own format extension which did not exist i.e. in the
4858         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4859         * gigedit.
4860         */
4861        uint Instrument::ScriptSlotCount() const {
4862            return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();
4863        }
4864    
4865        /** @brief Whether script execution shall be skipped.
4866         *
4867         * Defines locally for the Script reference slot in the Instrument's Script
4868         * list, whether the script shall be skipped by the sampler regarding
4869         * execution.
4870         *
4871         * It is also possible to ignore exeuction of the script globally, for all
4872         * slots and for all instruments by setting Script::Bypass.
4873         *
4874         * @note This is an own format extension which did not exist i.e. in the
4875         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4876         * gigedit.
4877         *
4878         * @param index - index of the script slot on this instrument
4879         * @see Script::Bypass
4880         */
4881        bool Instrument::IsScriptSlotBypassed(uint index) {
4882            if (index >= ScriptSlotCount()) return false;
4883            return pScriptRefs ? pScriptRefs->at(index).bypass
4884                               : scriptPoolFileOffsets.at(index).bypass;
4885            
4886        }
4887    
4888        /** @brief Defines whether execution shall be skipped.
4889         *
4890         * You can call this method to define locally whether or whether not the
4891         * given script slot shall be executed by the sampler.
4892         *
4893         * @note This is an own format extension which did not exist i.e. in the
4894         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4895         * gigedit.
4896         *
4897         * @param index - script slot index on this instrument
4898         * @param bBypass - if true, the script slot will be skipped by the sampler
4899         * @see Script::Bypass
4900         */
4901        void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) {
4902            if (index >= ScriptSlotCount()) return;
4903            if (pScriptRefs)
4904                pScriptRefs->at(index).bypass = bBypass;
4905            else
4906                scriptPoolFileOffsets.at(index).bypass = bBypass;
4907        }
4908    
4909        /**
4910         * Make a (semi) deep copy of the Instrument object given by @a orig
4911         * and assign it to this object.
4912         *
4913         * Note that all sample pointers referenced by @a orig are simply copied as
4914         * memory address. Thus the respective samples are shared, not duplicated!
4915         *
4916         * @param orig - original Instrument object to be copied from
4917         */
4918        void Instrument::CopyAssign(const Instrument* orig) {
4919            CopyAssign(orig, NULL);
4920        }
4921            
4922        /**
4923         * Make a (semi) deep copy of the Instrument object given by @a orig
4924         * and assign it to this object.
4925         *
4926         * @param orig - original Instrument object to be copied from
4927         * @param mSamples - crosslink map between the foreign file's samples and
4928         *                   this file's samples
4929         */
4930        void Instrument::CopyAssign(const Instrument* orig, const std::map<Sample*,Sample*>* mSamples) {
4931            // handle base class
4932            // (without copying DLS region stuff)
4933            DLS::Instrument::CopyAssignCore(orig);
4934            
4935            // handle own member variables
4936            Attenuation = orig->Attenuation;
4937            EffectSend = orig->EffectSend;
4938            FineTune = orig->FineTune;
4939            PitchbendRange = orig->PitchbendRange;
4940            PianoReleaseMode = orig->PianoReleaseMode;
4941            DimensionKeyRange = orig->DimensionKeyRange;
4942            scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
4943            pScriptRefs = orig->pScriptRefs;
4944            
4945            // free old midi rules
4946            for (int i = 0 ; pMidiRules[i] ; i++) {
4947                delete pMidiRules[i];
4948            }
4949            //TODO: MIDI rule copying
4950            pMidiRules[0] = NULL;
4951            
4952            // delete all old regions
4953            while (Regions) DeleteRegion(GetFirstRegion());
4954            // create new regions and copy them from original
4955            {
4956                RegionList::const_iterator it = orig->pRegions->begin();
4957                for (int i = 0; i < orig->Regions; ++i, ++it) {
4958                    Region* dstRgn = AddRegion();
4959                    //NOTE: Region does semi-deep copy !
4960                    dstRgn->CopyAssign(
4961                        static_cast<gig::Region*>(*it),
4962                        mSamples
4963                    );
4964                }
4965            }
4966    
4967            UpdateRegionKeyTable();
4968        }
4969    
4970    
4971  // *************** Group ***************  // *************** Group ***************
# Line 3137  namespace { Line 4995  namespace {
4995       *       *
4996       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
4997       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
4998         *
4999         * @param pProgress - callback function for progress notification
5000       */       */
5001      void Group::UpdateChunks() {      void Group::UpdateChunks(progress_t* pProgress) {
5002          // make sure <3gri> and <3gnl> list chunks exist          // make sure <3gri> and <3gnl> list chunks exist
5003          RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);          RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);
5004          if (!_3gri) {          if (!_3gri) {
# Line 3268  namespace { Line 5128  namespace {
5128          bAutoLoad = true;          bAutoLoad = true;
5129          *pVersion = VERSION_3;          *pVersion = VERSION_3;
5130          pGroups = NULL;          pGroups = NULL;
5131            pScriptGroups = NULL;
5132          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5133          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
5134    
# Line 3283  namespace { Line 5144  namespace {
5144      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
5145          bAutoLoad = true;          bAutoLoad = true;
5146          pGroups = NULL;          pGroups = NULL;
5147            pScriptGroups = NULL;
5148          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5149      }      }
5150    
# Line 3296  namespace { Line 5158  namespace {
5158              }              }
5159              delete pGroups;              delete pGroups;
5160          }          }
5161            if (pScriptGroups) {
5162                std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin();
5163                std::list<ScriptGroup*>::iterator end  = pScriptGroups->end();
5164                while (iter != end) {
5165                    delete *iter;
5166                    ++iter;
5167                }
5168                delete pScriptGroups;
5169            }
5170      }      }
5171    
5172      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 3310  namespace { Line 5181  namespace {
5181          SamplesIterator++;          SamplesIterator++;
5182          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );
5183      }      }
5184        
5185        /**
5186         * Returns Sample object of @a index.
5187         *
5188         * @returns sample object or NULL if index is out of bounds
5189         */
5190        Sample* File::GetSample(uint index) {
5191            if (!pSamples) LoadSamples();
5192            if (!pSamples) return NULL;
5193            DLS::File::SampleList::iterator it = pSamples->begin();
5194            for (int i = 0; i < index; ++i) {
5195                ++it;
5196                if (it == pSamples->end()) return NULL;
5197            }
5198            if (it == pSamples->end()) return NULL;
5199            return static_cast<gig::Sample*>( *it );
5200        }
5201    
5202      /** @brief Add a new sample.      /** @brief Add a new sample.
5203       *       *
# Line 3351  namespace { Line 5239  namespace {
5239          pSamples->erase(iter);          pSamples->erase(iter);
5240          delete pSample;          delete pSample;
5241    
5242            SampleList::iterator tmp = SamplesIterator;
5243          // remove all references to the sample          // remove all references to the sample
5244          for (Instrument* instrument = GetFirstInstrument() ; instrument ;          for (Instrument* instrument = GetFirstInstrument() ; instrument ;
5245               instrument = GetNextInstrument()) {               instrument = GetNextInstrument()) {
# Line 3365  namespace { Line 5254  namespace {
5254                  }                  }
5255              }              }
5256          }          }
5257            SamplesIterator = tmp; // restore iterator
5258      }      }
5259    
5260      void File::LoadSamples() {      void File::LoadSamples() {
# Line 3505  namespace { Line 5395  namespace {
5395         pInstruments->push_back(pInstrument);         pInstruments->push_back(pInstrument);
5396         return pInstrument;         return pInstrument;
5397      }      }
5398        
5399        /** @brief Add a duplicate of an existing instrument.
5400         *
5401         * Duplicates the instrument definition given by @a orig and adds it
5402         * to this file. This allows in an instrument editor application to
5403         * easily create variations of an instrument, which will be stored in
5404         * the same .gig file, sharing i.e. the same samples.
5405         *
5406         * Note that all sample pointers referenced by @a orig are simply copied as
5407         * memory address. Thus the respective samples are shared, not duplicated!
5408         *
5409         * You have to call Save() to make this persistent to the file.
5410         *
5411         * @param orig - original instrument to be copied
5412         * @returns duplicated copy of the given instrument
5413         */
5414        Instrument* File::AddDuplicateInstrument(const Instrument* orig) {
5415            Instrument* instr = AddInstrument();
5416            instr->CopyAssign(orig);
5417            return instr;
5418        }
5419        
5420        /** @brief Add content of another existing file.
5421         *
5422         * Duplicates the samples, groups and instruments of the original file
5423         * given by @a pFile and adds them to @c this File. In case @c this File is
5424         * a new one that you haven't saved before, then you have to call
5425         * SetFileName() before calling AddContentOf(), because this method will
5426         * automatically save this file during operation, which is required for
5427         * writing the sample waveform data by disk streaming.
5428         *
5429         * @param pFile - original file whose's content shall be copied from
5430         */
5431        void File::AddContentOf(File* pFile) {
5432            static int iCallCount = -1;
5433            iCallCount++;
5434            std::map<Group*,Group*> mGroups;
5435            std::map<Sample*,Sample*> mSamples;
5436            
5437            // clone sample groups
5438            for (int i = 0; pFile->GetGroup(i); ++i) {
5439                Group* g = AddGroup();
5440                g->Name =
5441                    "COPY" + ToString(iCallCount) + "_" + pFile->GetGroup(i)->Name;
5442                mGroups[pFile->GetGroup(i)] = g;
5443            }
5444            
5445            // clone samples (not waveform data here yet)
5446            for (int i = 0; pFile->GetSample(i); ++i) {
5447                Sample* s = AddSample();
5448                s->CopyAssignMeta(pFile->GetSample(i));
5449                mGroups[pFile->GetSample(i)->GetGroup()]->AddSample(s);
5450                mSamples[pFile->GetSample(i)] = s;
5451            }
5452            
5453            //BUG: For some reason this method only works with this additional
5454            //     Save() call in between here.
5455            //
5456            // Important: The correct one of the 2 Save() methods has to be called
5457            // here, depending on whether the file is completely new or has been
5458            // saved to disk already, otherwise it will result in data corruption.
5459            if (pRIFF->IsNew())
5460                Save(GetFileName());
5461            else
5462                Save();
5463            
5464            // clone instruments
5465            // (passing the crosslink table here for the cloned samples)
5466            for (int i = 0; pFile->GetInstrument(i); ++i) {
5467                Instrument* instr = AddInstrument();
5468                instr->CopyAssign(pFile->GetInstrument(i), &mSamples);
5469            }
5470            
5471            // Mandatory: file needs to be saved to disk at this point, so this
5472            // file has the correct size and data layout for writing the samples'
5473            // waveform data to disk.
5474            Save();
5475            
5476            // clone samples' waveform data
5477            // (using direct read & write disk streaming)
5478            for (int i = 0; pFile->GetSample(i); ++i) {
5479                mSamples[pFile->GetSample(i)]->CopyAssignWave(pFile->GetSample(i));
5480            }
5481        }
5482    
5483      /** @brief Delete an instrument.      /** @brief Delete an instrument.
5484       *       *
# Line 3607  namespace { Line 5581  namespace {
5581          return NULL;          return NULL;
5582      }      }
5583    
5584        /**
5585         * Returns the group with the given group name.
5586         *
5587         * Note: group names don't have to be unique in the gig format! So there
5588         * can be multiple groups with the same name. This method will simply
5589         * return the first group found with the given name.
5590         *
5591         * @param name - name of the sought group
5592         * @returns sought group or NULL if there's no group with that name
5593         */
5594        Group* File::GetGroup(String name) {
5595            if (!pGroups) LoadGroups();
5596            GroupsIterator = pGroups->begin();
5597            for (uint i = 0; GroupsIterator != pGroups->end(); ++GroupsIterator, ++i)
5598                if ((*GroupsIterator)->Name == name) return *GroupsIterator;
5599            return NULL;
5600        }
5601    
5602      Group* File::AddGroup() {      Group* File::AddGroup() {
5603          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
5604          // there must always be at least one group          // there must always be at least one group
# Line 3687  namespace { Line 5679  namespace {
5679          }          }
5680      }      }
5681    
5682        /** @brief Get instrument script group (by index).
5683         *
5684         * Returns the real-time instrument script group with the given index.
5685         *
5686         * @param index - number of the sought group (0..n)
5687         * @returns sought script group or NULL if there's no such group
5688         */
5689        ScriptGroup* File::GetScriptGroup(uint index) {
5690            if (!pScriptGroups) LoadScriptGroups();
5691            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5692            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5693                if (i == index) return *it;
5694            return NULL;
5695        }
5696    
5697        /** @brief Get instrument script group (by name).
5698         *
5699         * Returns the first real-time instrument script group found with the given
5700         * group name. Note that group names may not necessarily be unique.
5701         *
5702         * @param name - name of the sought script group
5703         * @returns sought script group or NULL if there's no such group
5704         */
5705        ScriptGroup* File::GetScriptGroup(const String& name) {
5706            if (!pScriptGroups) LoadScriptGroups();
5707            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5708            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5709                if ((*it)->Name == name) return *it;
5710            return NULL;
5711        }
5712    
5713        /** @brief Add new instrument script group.
5714         *
5715         * Adds a new, empty real-time instrument script group to the file.
5716         *
5717         * You have to call Save() to make this persistent to the file.
5718         *
5719         * @return new empty script group
5720         */
5721        ScriptGroup* File::AddScriptGroup() {
5722            if (!pScriptGroups) LoadScriptGroups();
5723            ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL);
5724            pScriptGroups->push_back(pScriptGroup);
5725            return pScriptGroup;
5726        }
5727    
5728        /** @brief Delete an instrument script group.
5729         *
5730         * This will delete the given real-time instrument script group and all its
5731         * instrument scripts it contains. References inside instruments that are
5732         * using the deleted scripts will be removed from the respective instruments
5733         * accordingly.
5734         *
5735         * You have to call Save() to make this persistent to the file.
5736         *
5737         * @param pScriptGroup - script group to delete
5738         * @throws gig::Exception if given script group could not be found
5739         */
5740        void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) {
5741            if (!pScriptGroups) LoadScriptGroups();
5742            std::list<ScriptGroup*>::iterator iter =
5743                find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup);
5744            if (iter == pScriptGroups->end())
5745                throw gig::Exception("Could not delete script group, could not find given script group");
5746            pScriptGroups->erase(iter);
5747            for (int i = 0; pScriptGroup->GetScript(i); ++i)
5748                pScriptGroup->DeleteScript(pScriptGroup->GetScript(i));
5749            if (pScriptGroup->pList)
5750                pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList);
5751            delete pScriptGroup;
5752        }
5753    
5754        void File::LoadScriptGroups() {
5755            if (pScriptGroups) return;
5756            pScriptGroups = new std::list<ScriptGroup*>;
5757            RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS);
5758            if (lstLS) {
5759                for (RIFF::List* lst = lstLS->GetFirstSubList(); lst;
5760                     lst = lstLS->GetNextSubList())
5761                {
5762                    if (lst->GetListType() == LIST_TYPE_RTIS) {
5763                        pScriptGroups->push_back(new ScriptGroup(this, lst));
5764                    }
5765                }
5766            }
5767        }
5768    
5769      /**      /**
5770       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
5771       * to the respective RIFF chunks. You have to call Save() to make changes       * to the respective RIFF chunks. You have to call Save() to make changes
# Line 3695  namespace { Line 5774  namespace {
5774       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
5775       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
5776       *       *
5777         * @param pProgress - callback function for progress notification
5778       * @throws Exception - on errors       * @throws Exception - on errors
5779       */       */
5780      void File::UpdateChunks() {      void File::UpdateChunks(progress_t* pProgress) {
5781          bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL;          bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL;
5782    
5783          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;
5784    
5785            // update own gig format extension chunks
5786            // (not part of the GigaStudio 4 format)
5787            //
5788            // This must be performed before writing the chunks for instruments,
5789            // because the instruments' script slots will write the file offsets
5790            // of the respective instrument script chunk as reference.
5791            if (pScriptGroups) {
5792                RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
5793                if (pScriptGroups->empty()) {
5794                    if (lst3LS) pRIFF->DeleteSubChunk(lst3LS);
5795                } else {
5796                    if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
5797    
5798                    // Update instrument script (group) chunks.
5799    
5800                    for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5801                         it != pScriptGroups->end(); ++it)
5802                    {
5803                        (*it)->UpdateChunks(pProgress);
5804                    }
5805                }
5806            }
5807    
5808          // first update base class's chunks          // first update base class's chunks
5809          DLS::File::UpdateChunks();          DLS::File::UpdateChunks(pProgress);
5810    
5811          if (newFile) {          if (newFile) {
5812              // INFO was added by Resource::UpdateChunks - make sure it              // INFO was added by Resource::UpdateChunks - make sure it
# Line 3717  namespace { Line 5820  namespace {
5820    
5821          // update group's chunks          // update group's chunks
5822          if (pGroups) {          if (pGroups) {
5823              std::list<Group*>::iterator iter = pGroups->begin();              // make sure '3gri' and '3gnl' list chunks exist
5824              std::list<Group*>::iterator end  = pGroups->end();              // (before updating the Group chunks)
5825              for (; iter != end; ++iter) {              RIFF::List* _3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);
5826                  (*iter)->UpdateChunks();              if (!_3gri) {
5827                    _3gri = pRIFF->AddSubList(LIST_TYPE_3GRI);
5828                    pRIFF->MoveSubChunk(_3gri, pRIFF->GetSubChunk(CHUNK_ID_PTBL));
5829              }              }
5830                RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);
5831                if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL);
5832    
5833              // v3: make sure the file has 128 3gnm chunks              // v3: make sure the file has 128 3gnm chunks
5834                // (before updating the Group chunks)
5835              if (pVersion && pVersion->major == 3) {              if (pVersion && pVersion->major == 3) {
                 RIFF::List* _3gnl = pRIFF->GetSubList(LIST_TYPE_3GRI)->GetSubList(LIST_TYPE_3GNL);  
5836                  RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk();                  RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk();
5837                  for (int i = 0 ; i < 128 ; i++) {                  for (int i = 0 ; i < 128 ; i++) {
5838                      if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64);                      if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64);
5839                      if (_3gnm) _3gnm = _3gnl->GetNextSubChunk();                      if (_3gnm) _3gnm = _3gnl->GetNextSubChunk();
5840                  }                  }
5841              }              }
5842    
5843                std::list<Group*>::iterator iter = pGroups->begin();
5844                std::list<Group*>::iterator end  = pGroups->end();
5845                for (; iter != end; ++iter) {
5846                    (*iter)->UpdateChunks(pProgress);
5847                }
5848          }          }
5849    
5850          // update einf chunk          // update einf chunk
# Line 3862  namespace { Line 5975  namespace {
5975              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
5976          }          }
5977      }      }
5978        
5979        void File::UpdateFileOffsets() {
5980            DLS::File::UpdateFileOffsets();
5981    
5982            for (Instrument* instrument = GetFirstInstrument(); instrument;
5983                 instrument = GetNextInstrument())
5984            {
5985                instrument->UpdateScriptFileOffsets();
5986            }
5987        }
5988    
5989      /**      /**
5990       * Enable / disable automatic loading. By default this properyt is       * Enable / disable automatic loading. By default this properyt is

Legend:
Removed from v.1524  
changed lines
  Added in v.2682

  ViewVC Help
Powered by ViewVC