/[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 2557 by schoenebeck, Sat May 17 23:31:20 2014 UTC revision 2700 by schoenebeck, Mon Jan 12 23:22:29 2015 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-2014 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2015 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 53  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 528  namespace { Line 496  namespace {
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 1752  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 3026  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 3036  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 3056  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 3652  namespace { Line 3624  namespace {
3624          UpdateVelocityTable();          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) {      DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {
3655          uint8_t bits[8] = {};          uint8_t bits[8] = {};
3656          for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();          for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();
# Line 3743  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 4023  namespace { Line 4074  namespace {
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  // *  // *
4332    
# Line 4045  namespace { Line 4349  namespace {
4349          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
4350          pMidiRules = new MidiRule*[3];          pMidiRules = new MidiRule*[3];
4351          pMidiRules[0] = NULL;          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 4105  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 4125  namespace { Line 4452  namespace {
4452              delete pMidiRules[i];              delete pMidiRules[i];
4453          }          }
4454          delete[] pMidiRules;          delete[] pMidiRules;
4455            if (pScriptRefs) delete pScriptRefs;
4456      }      }
4457    
4458      /**      /**
# Line 4134  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 4180  namespace { Line 4509  namespace {
4509                  pMidiRules[i]->UpdateChunks(pData);                  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 4249  namespace { Line 4636  namespace {
4636      }      }
4637    
4638      /**      /**
4639         * Move this instrument at the position before @arg dst.
4640         *
4641         * This method can be used to reorder the sequence of instruments in a
4642         * .gig file. This might be helpful especially on large .gig files which
4643         * contain a large number of instruments within the same .gig file. So
4644         * grouping such instruments to similar ones, can help to keep track of them
4645         * when working with such complex .gig files.
4646         *
4647         * When calling this method, this instrument will be removed from in its
4648         * current position in the instruments list and moved to the requested
4649         * target position provided by @param dst. You may also pass NULL as
4650         * argument to this method, in that case this intrument will be moved to the
4651         * very end of the .gig file's instrument list.
4652         *
4653         * You have to call Save() to make the order change persistent to the .gig
4654         * file.
4655         *
4656         * Currently this method is limited to moving the instrument within the same
4657         * .gig file. Trying to move it to another .gig file by calling this method
4658         * will throw an exception.
4659         *
4660         * @param dst - destination instrument at which this instrument will be
4661         *              moved to, or pass NULL for moving to end of list
4662         * @throw gig::Exception if this instrument and target instrument are not
4663         *                       part of the same file
4664         */
4665        void Instrument::MoveTo(Instrument* dst) {
4666            if (dst && GetParent() != dst->GetParent())
4667                throw Exception(
4668                    "gig::Instrument::MoveTo() can only be used for moving within "
4669                    "the same gig file."
4670                );
4671    
4672            File* pFile = (File*) GetParent();
4673    
4674            // move this instrument within the instrument list
4675            {
4676                DLS::File::InstrumentList& list = *pFile->pInstruments;
4677    
4678                DLS::File::InstrumentList::iterator itFrom =
4679                    std::find(list.begin(), list.end(), static_cast<DLS::Instrument*>(this));
4680    
4681                DLS::File::InstrumentList::iterator itTo =
4682                    std::find(list.begin(), list.end(), static_cast<DLS::Instrument*>(dst));
4683    
4684                list.splice(itTo, list, itFrom);
4685            }
4686    
4687            // move the instrument's actual list RIFF chunk appropriately
4688            RIFF::List* lstCkInstruments = pFile->pRIFF->GetSubList(LIST_TYPE_LINS);
4689            lstCkInstruments->MoveSubChunk(
4690                this->pCkInstrument,
4691                (dst) ? dst->pCkInstrument : NULL
4692            );
4693        }
4694    
4695        /**
4696       * Returns a MIDI rule of the instrument.       * Returns a MIDI rule of the instrument.
4697       *       *
4698       * The list of MIDI rules, at least in gig v3, always contains at       * The list of MIDI rules, at least in gig v3, always contains at
# Line 4311  namespace { Line 4755  namespace {
4755          pMidiRules[i] = 0;          pMidiRules[i] = 0;
4756      }      }
4757    
4758        void Instrument::LoadScripts() {
4759            if (pScriptRefs) return;
4760            pScriptRefs = new std::vector<_ScriptPooolRef>;
4761            if (scriptPoolFileOffsets.empty()) return;
4762            File* pFile = (File*) GetParent();
4763            for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4764                uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset;
4765                for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4766                    ScriptGroup* group = pFile->GetScriptGroup(i);
4767                    for (uint s = 0; group->GetScript(s); ++s) {
4768                        Script* script = group->GetScript(s);
4769                        if (script->pChunk) {
4770                            uint32_t offset = script->pChunk->GetFilePos() -
4771                                              script->pChunk->GetPos() -
4772                                              CHUNK_HEADER_SIZE;
4773                            if (offset == soughtOffset)
4774                            {
4775                                _ScriptPooolRef ref;
4776                                ref.script = script;
4777                                ref.bypass = scriptPoolFileOffsets[k].bypass;
4778                                pScriptRefs->push_back(ref);
4779                                break;
4780                            }
4781                        }
4782                    }
4783                }
4784            }
4785            // we don't need that anymore
4786            scriptPoolFileOffsets.clear();
4787        }
4788    
4789        /** @brief Get instrument script (gig format extension).
4790         *
4791         * Returns the real-time instrument script of instrument script slot
4792         * @a index.
4793         *
4794         * @note This is an own format extension which did not exist i.e. in the
4795         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4796         * gigedit.
4797         *
4798         * @param index - instrument script slot index
4799         * @returns script or NULL if index is out of bounds
4800         */
4801        Script* Instrument::GetScriptOfSlot(uint index) {
4802            LoadScripts();
4803            if (index >= pScriptRefs->size()) return NULL;
4804            return pScriptRefs->at(index).script;
4805        }
4806    
4807        /** @brief Add new instrument script slot (gig format extension).
4808         *
4809         * Add the given real-time instrument script reference to this instrument,
4810         * which shall be executed by the sampler for for this instrument. The
4811         * script will be added to the end of the script list of this instrument.
4812         * The positions of the scripts in the Instrument's Script list are
4813         * relevant, because they define in which order they shall be executed by
4814         * the sampler. For this reason it is also legal to add the same script
4815         * twice to an instrument, for example you might have a script called
4816         * "MyFilter" which performs an event filter task, and you might have
4817         * another script called "MyNoteTrigger" which triggers new notes, then you
4818         * might for example have the following list of scripts on the instrument:
4819         *
4820         * 1. Script "MyFilter"
4821         * 2. Script "MyNoteTrigger"
4822         * 3. Script "MyFilter"
4823         *
4824         * Which would make sense, because the 2nd script launched new events, which
4825         * you might need to filter as well.
4826         *
4827         * There are two ways to disable / "bypass" scripts. You can either disable
4828         * a script locally for the respective script slot on an instrument (i.e. by
4829         * passing @c false to the 2nd argument of this method, or by calling
4830         * SetScriptBypassed()). Or you can disable a script globally for all slots
4831         * and all instruments by setting Script::Bypass.
4832         *
4833         * @note This is an own format extension which did not exist i.e. in the
4834         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4835         * gigedit.
4836         *
4837         * @param pScript - script that shall be executed for this instrument
4838         * @param bypass  - if enabled, the sampler shall skip executing this
4839         *                  script (in the respective list position)
4840         * @see SetScriptBypassed()
4841         */
4842        void Instrument::AddScriptSlot(Script* pScript, bool bypass) {
4843            LoadScripts();
4844            _ScriptPooolRef ref = { pScript, bypass };
4845            pScriptRefs->push_back(ref);
4846        }
4847    
4848        /** @brief Flip two script slots with each other (gig format extension).
4849         *
4850         * Swaps the position of the two given scripts in the Instrument's Script
4851         * list. The positions of the scripts in the Instrument's Script list are
4852         * relevant, because they define in which order they shall be executed by
4853         * the sampler.
4854         *
4855         * @note This is an own format extension which did not exist i.e. in the
4856         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4857         * gigedit.
4858         *
4859         * @param index1 - index of the first script slot to swap
4860         * @param index2 - index of the second script slot to swap
4861         */
4862        void Instrument::SwapScriptSlots(uint index1, uint index2) {
4863            LoadScripts();
4864            if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size())
4865                return;
4866            _ScriptPooolRef tmp = (*pScriptRefs)[index1];
4867            (*pScriptRefs)[index1] = (*pScriptRefs)[index2];
4868            (*pScriptRefs)[index2] = tmp;
4869        }
4870    
4871        /** @brief Remove script slot.
4872         *
4873         * Removes the script slot with the given slot index.
4874         *
4875         * @param index - index of script slot to remove
4876         */
4877        void Instrument::RemoveScriptSlot(uint index) {
4878            LoadScripts();
4879            if (index >= pScriptRefs->size()) return;
4880            pScriptRefs->erase( pScriptRefs->begin() + index );
4881        }
4882    
4883        /** @brief Remove reference to given Script (gig format extension).
4884         *
4885         * This will remove all script slots on the instrument which are referencing
4886         * the given script.
4887         *
4888         * @note This is an own format extension which did not exist i.e. in the
4889         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4890         * gigedit.
4891         *
4892         * @param pScript - script reference to remove from this instrument
4893         * @see RemoveScriptSlot()
4894         */
4895        void Instrument::RemoveScript(Script* pScript) {
4896            LoadScripts();
4897            for (int i = pScriptRefs->size() - 1; i >= 0; --i) {
4898                if ((*pScriptRefs)[i].script == pScript) {
4899                    pScriptRefs->erase( pScriptRefs->begin() + i );
4900                }
4901            }
4902        }
4903    
4904        /** @brief Instrument's amount of script slots.
4905         *
4906         * This method returns the amount of script slots this instrument currently
4907         * uses.
4908         *
4909         * A script slot is a reference of a real-time instrument script to be
4910         * executed by the sampler. The scripts will be executed by the sampler in
4911         * sequence of the slots. One (same) script may be referenced multiple
4912         * times in different slots.
4913         *
4914         * @note This is an own format extension which did not exist i.e. in the
4915         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4916         * gigedit.
4917         */
4918        uint Instrument::ScriptSlotCount() const {
4919            return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();
4920        }
4921    
4922        /** @brief Whether script execution shall be skipped.
4923         *
4924         * Defines locally for the Script reference slot in the Instrument's Script
4925         * list, whether the script shall be skipped by the sampler regarding
4926         * execution.
4927         *
4928         * It is also possible to ignore exeuction of the script globally, for all
4929         * slots and for all instruments by setting Script::Bypass.
4930         *
4931         * @note This is an own format extension which did not exist i.e. in the
4932         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4933         * gigedit.
4934         *
4935         * @param index - index of the script slot on this instrument
4936         * @see Script::Bypass
4937         */
4938        bool Instrument::IsScriptSlotBypassed(uint index) {
4939            if (index >= ScriptSlotCount()) return false;
4940            return pScriptRefs ? pScriptRefs->at(index).bypass
4941                               : scriptPoolFileOffsets.at(index).bypass;
4942            
4943        }
4944    
4945        /** @brief Defines whether execution shall be skipped.
4946         *
4947         * You can call this method to define locally whether or whether not the
4948         * given script slot shall be executed by the sampler.
4949         *
4950         * @note This is an own format extension which did not exist i.e. in the
4951         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4952         * gigedit.
4953         *
4954         * @param index - script slot index on this instrument
4955         * @param bBypass - if true, the script slot will be skipped by the sampler
4956         * @see Script::Bypass
4957         */
4958        void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) {
4959            if (index >= ScriptSlotCount()) return;
4960            if (pScriptRefs)
4961                pScriptRefs->at(index).bypass = bBypass;
4962            else
4963                scriptPoolFileOffsets.at(index).bypass = bBypass;
4964        }
4965    
4966      /**      /**
4967       * Make a (semi) deep copy of the Instrument object given by @a orig       * Make a (semi) deep copy of the Instrument object given by @a orig
4968       * and assign it to this object.       * and assign it to this object.
# Line 4344  namespace { Line 4996  namespace {
4996          PitchbendRange = orig->PitchbendRange;          PitchbendRange = orig->PitchbendRange;
4997          PianoReleaseMode = orig->PianoReleaseMode;          PianoReleaseMode = orig->PianoReleaseMode;
4998          DimensionKeyRange = orig->DimensionKeyRange;          DimensionKeyRange = orig->DimensionKeyRange;
4999            scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
5000            pScriptRefs = orig->pScriptRefs;
5001                    
5002          // free old midi rules          // free old midi rules
5003          for (int i = 0 ; pMidiRules[i] ; i++) {          for (int i = 0 ; pMidiRules[i] ; i++) {
# Line 4398  namespace { Line 5052  namespace {
5052       *       *
5053       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
5054       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
5055         *
5056         * @param pProgress - callback function for progress notification
5057       */       */
5058      void Group::UpdateChunks() {      void Group::UpdateChunks(progress_t* pProgress) {
5059          // make sure <3gri> and <3gnl> list chunks exist          // make sure <3gri> and <3gnl> list chunks exist
5060          RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);          RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);
5061          if (!_3gri) {          if (!_3gri) {
# Line 4529  namespace { Line 5185  namespace {
5185          bAutoLoad = true;          bAutoLoad = true;
5186          *pVersion = VERSION_3;          *pVersion = VERSION_3;
5187          pGroups = NULL;          pGroups = NULL;
5188            pScriptGroups = NULL;
5189          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5190          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
5191    
# Line 4544  namespace { Line 5201  namespace {
5201      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
5202          bAutoLoad = true;          bAutoLoad = true;
5203          pGroups = NULL;          pGroups = NULL;
5204            pScriptGroups = NULL;
5205          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5206      }      }
5207    
# Line 4557  namespace { Line 5215  namespace {
5215              }              }
5216              delete pGroups;              delete pGroups;
5217          }          }
5218            if (pScriptGroups) {
5219                std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin();
5220                std::list<ScriptGroup*>::iterator end  = pScriptGroups->end();
5221                while (iter != end) {
5222                    delete *iter;
5223                    ++iter;
5224                }
5225                delete pScriptGroups;
5226            }
5227      }      }
5228    
5229      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 5069  namespace { Line 5736  namespace {
5736          }          }
5737      }      }
5738    
5739        /** @brief Get instrument script group (by index).
5740         *
5741         * Returns the real-time instrument script group with the given index.
5742         *
5743         * @param index - number of the sought group (0..n)
5744         * @returns sought script group or NULL if there's no such group
5745         */
5746        ScriptGroup* File::GetScriptGroup(uint index) {
5747            if (!pScriptGroups) LoadScriptGroups();
5748            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5749            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5750                if (i == index) return *it;
5751            return NULL;
5752        }
5753    
5754        /** @brief Get instrument script group (by name).
5755         *
5756         * Returns the first real-time instrument script group found with the given
5757         * group name. Note that group names may not necessarily be unique.
5758         *
5759         * @param name - name of the sought script group
5760         * @returns sought script group or NULL if there's no such group
5761         */
5762        ScriptGroup* File::GetScriptGroup(const String& name) {
5763            if (!pScriptGroups) LoadScriptGroups();
5764            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5765            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5766                if ((*it)->Name == name) return *it;
5767            return NULL;
5768        }
5769    
5770        /** @brief Add new instrument script group.
5771         *
5772         * Adds a new, empty real-time instrument script group to the file.
5773         *
5774         * You have to call Save() to make this persistent to the file.
5775         *
5776         * @return new empty script group
5777         */
5778        ScriptGroup* File::AddScriptGroup() {
5779            if (!pScriptGroups) LoadScriptGroups();
5780            ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL);
5781            pScriptGroups->push_back(pScriptGroup);
5782            return pScriptGroup;
5783        }
5784    
5785        /** @brief Delete an instrument script group.
5786         *
5787         * This will delete the given real-time instrument script group and all its
5788         * instrument scripts it contains. References inside instruments that are
5789         * using the deleted scripts will be removed from the respective instruments
5790         * accordingly.
5791         *
5792         * You have to call Save() to make this persistent to the file.
5793         *
5794         * @param pScriptGroup - script group to delete
5795         * @throws gig::Exception if given script group could not be found
5796         */
5797        void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) {
5798            if (!pScriptGroups) LoadScriptGroups();
5799            std::list<ScriptGroup*>::iterator iter =
5800                find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup);
5801            if (iter == pScriptGroups->end())
5802                throw gig::Exception("Could not delete script group, could not find given script group");
5803            pScriptGroups->erase(iter);
5804            for (int i = 0; pScriptGroup->GetScript(i); ++i)
5805                pScriptGroup->DeleteScript(pScriptGroup->GetScript(i));
5806            if (pScriptGroup->pList)
5807                pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList);
5808            delete pScriptGroup;
5809        }
5810    
5811        void File::LoadScriptGroups() {
5812            if (pScriptGroups) return;
5813            pScriptGroups = new std::list<ScriptGroup*>;
5814            RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS);
5815            if (lstLS) {
5816                for (RIFF::List* lst = lstLS->GetFirstSubList(); lst;
5817                     lst = lstLS->GetNextSubList())
5818                {
5819                    if (lst->GetListType() == LIST_TYPE_RTIS) {
5820                        pScriptGroups->push_back(new ScriptGroup(this, lst));
5821                    }
5822                }
5823            }
5824        }
5825    
5826      /**      /**
5827       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
5828       * 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 5077  namespace { Line 5831  namespace {
5831       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
5832       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
5833       *       *
5834         * @param pProgress - callback function for progress notification
5835       * @throws Exception - on errors       * @throws Exception - on errors
5836       */       */
5837      void File::UpdateChunks() {      void File::UpdateChunks(progress_t* pProgress) {
5838          bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL;          bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL;
5839    
5840          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;
5841    
5842            // update own gig format extension chunks
5843            // (not part of the GigaStudio 4 format)
5844            //
5845            // This must be performed before writing the chunks for instruments,
5846            // because the instruments' script slots will write the file offsets
5847            // of the respective instrument script chunk as reference.
5848            if (pScriptGroups) {
5849                RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
5850                if (pScriptGroups->empty()) {
5851                    if (lst3LS) pRIFF->DeleteSubChunk(lst3LS);
5852                } else {
5853                    if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
5854    
5855                    // Update instrument script (group) chunks.
5856    
5857                    for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5858                         it != pScriptGroups->end(); ++it)
5859                    {
5860                        (*it)->UpdateChunks(pProgress);
5861                    }
5862                }
5863            }
5864    
5865          // first update base class's chunks          // first update base class's chunks
5866          DLS::File::UpdateChunks();          DLS::File::UpdateChunks(pProgress);
5867    
5868          if (newFile) {          if (newFile) {
5869              // INFO was added by Resource::UpdateChunks - make sure it              // INFO was added by Resource::UpdateChunks - make sure it
# Line 5122  namespace { Line 5900  namespace {
5900              std::list<Group*>::iterator iter = pGroups->begin();              std::list<Group*>::iterator iter = pGroups->begin();
5901              std::list<Group*>::iterator end  = pGroups->end();              std::list<Group*>::iterator end  = pGroups->end();
5902              for (; iter != end; ++iter) {              for (; iter != end; ++iter) {
5903                  (*iter)->UpdateChunks();                  (*iter)->UpdateChunks(pProgress);
5904              }              }
5905          }          }
5906    
# Line 5254  namespace { Line 6032  namespace {
6032              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
6033          }          }
6034      }      }
6035        
6036        void File::UpdateFileOffsets() {
6037            DLS::File::UpdateFileOffsets();
6038    
6039            for (Instrument* instrument = GetFirstInstrument(); instrument;
6040                 instrument = GetNextInstrument())
6041            {
6042                instrument->UpdateScriptFileOffsets();
6043            }
6044        }
6045    
6046      /**      /**
6047       * Enable / disable automatic loading. By default this properyt is       * Enable / disable automatic loading. By default this properyt is

Legend:
Removed from v.2557  
changed lines
  Added in v.2700

  ViewVC Help
Powered by ViewVC