/[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 2547 by schoenebeck, Tue May 13 11:17:24 2014 UTC revision 2609 by schoenebeck, Sun Jun 8 19:00:30 2014 UTC
# Line 28  Line 28 
28  #include <algorithm>  #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 3055  namespace { Line 3056  namespace {
3056              memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize);              memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize);
3057    
3058              // move 3prg to last position              // move 3prg to last position
3059              pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0);              pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), (RIFF::Chunk*)NULL);
3060          }          }
3061    
3062          // update dimension definitions in '3lnk' chunk          // update dimension definitions in '3lnk' chunk
# Line 3388  namespace { Line 3389  namespace {
3389          if (pDimDef->dimension == dimension_layer) Layers = 1;          if (pDimDef->dimension == dimension_layer) Layers = 1;
3390      }      }
3391    
3392        /** @brief Delete one split zone of a dimension (decrement zone amount).
3393         *
3394         * Instead of deleting an entire dimensions, this method will only delete
3395         * one particular split zone given by @a zone of the Region's dimension
3396         * given by @a type. So this method will simply decrement the amount of
3397         * zones by one of the dimension in question. To be able to do that, the
3398         * respective dimension must exist on this Region and it must have at least
3399         * 3 zones. All DimensionRegion objects associated with the zone will be
3400         * deleted.
3401         *
3402         * @param type - identifies the dimension where a zone shall be deleted
3403         * @param zone - index of the dimension split zone that shall be deleted
3404         * @throws gig::Exception if requested zone could not be deleted
3405         */
3406        void Region::DeleteDimensionZone(dimension_t type, int zone) {
3407            dimension_def_t* oldDef = GetDimensionDefinition(type);
3408            if (!oldDef)
3409                throw gig::Exception("Could not delete dimension zone, no such dimension of given type");
3410            if (oldDef->zones <= 2)
3411                throw gig::Exception("Could not delete dimension zone, because it would end up with only one zone.");
3412            if (zone < 0 || zone >= oldDef->zones)
3413                throw gig::Exception("Could not delete dimension zone, requested zone index out of bounds.");
3414    
3415            const int newZoneSize = oldDef->zones - 1;
3416    
3417            // create a temporary Region which just acts as a temporary copy
3418            // container and will be deleted at the end of this function and will
3419            // also not be visible through the API during this process
3420            gig::Region* tempRgn = NULL;
3421            {
3422                // adding these temporary chunks is probably not even necessary
3423                Instrument* instr = static_cast<Instrument*>(GetParent());
3424                RIFF::List* pCkInstrument = instr->pCkInstrument;
3425                RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
3426                if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
3427                RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
3428                tempRgn = new Region(instr, rgn);
3429            }
3430    
3431            // copy this region's dimensions (with already the dimension split size
3432            // requested by the arguments of this method call) to the temporary
3433            // region, and don't use Region::CopyAssign() here for this task, since
3434            // it would also alter fast lookup helper variables here and there
3435            dimension_def_t newDef;
3436            for (int i = 0; i < Dimensions; ++i) {
3437                dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3438                // is this the dimension requested by the method arguments? ...
3439                if (def.dimension == type) { // ... if yes, decrement zone amount by one
3440                    def.zones = newZoneSize;
3441                    if ((1 << (def.bits - 1)) == def.zones) def.bits--;
3442                    newDef = def;
3443                }
3444                tempRgn->AddDimension(&def);
3445            }
3446    
3447            // find the dimension index in the tempRegion which is the dimension
3448            // type passed to this method (paranoidly expecting different order)
3449            int tempReducedDimensionIndex = -1;
3450            for (int d = 0; d < tempRgn->Dimensions; ++d) {
3451                if (tempRgn->pDimensionDefinitions[d].dimension == type) {
3452                    tempReducedDimensionIndex = d;
3453                    break;
3454                }
3455            }
3456    
3457            // copy dimension regions from this region to the temporary region
3458            for (int iDst = 0; iDst < 256; ++iDst) {
3459                DimensionRegion* dstDimRgn = tempRgn->pDimensionRegions[iDst];
3460                if (!dstDimRgn) continue;
3461                std::map<dimension_t,int> dimCase;
3462                bool isValidZone = true;
3463                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3464                    const int dstBits = tempRgn->pDimensionDefinitions[d].bits;
3465                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3466                        (iDst >> baseBits) & ((1 << dstBits) - 1);
3467                    baseBits += dstBits;
3468                    // there are also DimensionRegion objects of unused zones, skip them
3469                    if (dimCase[tempRgn->pDimensionDefinitions[d].dimension] >= tempRgn->pDimensionDefinitions[d].zones) {
3470                        isValidZone = false;
3471                        break;
3472                    }
3473                }
3474                if (!isValidZone) continue;
3475                // a bit paranoid: cope with the chance that the dimensions would
3476                // have different order in source and destination regions
3477                const bool isLastZone = (dimCase[type] == newZoneSize - 1);
3478                if (dimCase[type] >= zone) dimCase[type]++;
3479                DimensionRegion* srcDimRgn = GetDimensionRegionByBit(dimCase);
3480                dstDimRgn->CopyAssign(srcDimRgn);
3481                // if this is the upper most zone of the dimension passed to this
3482                // method, then correct (raise) its upper limit to 127
3483                if (newDef.split_type == split_type_normal && isLastZone)
3484                    dstDimRgn->DimensionUpperLimits[tempReducedDimensionIndex] = 127;
3485            }
3486    
3487            // now tempRegion's dimensions and DimensionRegions basically reflect
3488            // what we wanted to get for this actual Region here, so we now just
3489            // delete and recreate the dimension in question with the new amount
3490            // zones and then copy back from tempRegion      
3491            DeleteDimension(oldDef);
3492            AddDimension(&newDef);
3493            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3494                DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc];
3495                if (!srcDimRgn) continue;
3496                std::map<dimension_t,int> dimCase;
3497                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3498                    const int srcBits = tempRgn->pDimensionDefinitions[d].bits;
3499                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3500                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3501                    baseBits += srcBits;
3502                }
3503                // a bit paranoid: cope with the chance that the dimensions would
3504                // have different order in source and destination regions
3505                DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase);
3506                if (!dstDimRgn) continue;
3507                dstDimRgn->CopyAssign(srcDimRgn);
3508            }
3509    
3510            // delete temporary region
3511            delete tempRgn;
3512    
3513            UpdateVelocityTable();
3514        }
3515    
3516        /** @brief Divide split zone of a dimension in two (increment zone amount).
3517         *
3518         * This will increment the amount of zones for the dimension (given by
3519         * @a type) by one. It will do so by dividing the zone (given by @a zone)
3520         * in the middle of its zone range in two. So the two zones resulting from
3521         * the zone being splitted, will be an equivalent copy regarding all their
3522         * articulation informations and sample reference. The two zones will only
3523         * differ in their zone's upper limit
3524         * (DimensionRegion::DimensionUpperLimits).
3525         *
3526         * @param type - identifies the dimension where a zone shall be splitted
3527         * @param zone - index of the dimension split zone that shall be splitted
3528         * @throws gig::Exception if requested zone could not be splitted
3529         */
3530        void Region::SplitDimensionZone(dimension_t type, int zone) {
3531            dimension_def_t* oldDef = GetDimensionDefinition(type);
3532            if (!oldDef)
3533                throw gig::Exception("Could not split dimension zone, no such dimension of given type");
3534            if (zone < 0 || zone >= oldDef->zones)
3535                throw gig::Exception("Could not split dimension zone, requested zone index out of bounds.");
3536    
3537            const int newZoneSize = oldDef->zones + 1;
3538    
3539            // create a temporary Region which just acts as a temporary copy
3540            // container and will be deleted at the end of this function and will
3541            // also not be visible through the API during this process
3542            gig::Region* tempRgn = NULL;
3543            {
3544                // adding these temporary chunks is probably not even necessary
3545                Instrument* instr = static_cast<Instrument*>(GetParent());
3546                RIFF::List* pCkInstrument = instr->pCkInstrument;
3547                RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
3548                if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
3549                RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
3550                tempRgn = new Region(instr, rgn);
3551            }
3552    
3553            // copy this region's dimensions (with already the dimension split size
3554            // requested by the arguments of this method call) to the temporary
3555            // region, and don't use Region::CopyAssign() here for this task, since
3556            // it would also alter fast lookup helper variables here and there
3557            dimension_def_t newDef;
3558            for (int i = 0; i < Dimensions; ++i) {
3559                dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3560                // is this the dimension requested by the method arguments? ...
3561                if (def.dimension == type) { // ... if yes, increment zone amount by one
3562                    def.zones = newZoneSize;
3563                    if ((1 << oldDef->bits) < newZoneSize) def.bits++;
3564                    newDef = def;
3565                }
3566                tempRgn->AddDimension(&def);
3567            }
3568    
3569            // find the dimension index in the tempRegion which is the dimension
3570            // type passed to this method (paranoidly expecting different order)
3571            int tempIncreasedDimensionIndex = -1;
3572            for (int d = 0; d < tempRgn->Dimensions; ++d) {
3573                if (tempRgn->pDimensionDefinitions[d].dimension == type) {
3574                    tempIncreasedDimensionIndex = d;
3575                    break;
3576                }
3577            }
3578    
3579            // copy dimension regions from this region to the temporary region
3580            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3581                DimensionRegion* srcDimRgn = pDimensionRegions[iSrc];
3582                if (!srcDimRgn) continue;
3583                std::map<dimension_t,int> dimCase;
3584                bool isValidZone = true;
3585                for (int d = 0, baseBits = 0; d < Dimensions; ++d) {
3586                    const int srcBits = pDimensionDefinitions[d].bits;
3587                    dimCase[pDimensionDefinitions[d].dimension] =
3588                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3589                    // there are also DimensionRegion objects for unused zones, skip them
3590                    if (dimCase[pDimensionDefinitions[d].dimension] >= pDimensionDefinitions[d].zones) {
3591                        isValidZone = false;
3592                        break;
3593                    }
3594                    baseBits += srcBits;
3595                }
3596                if (!isValidZone) continue;
3597                // a bit paranoid: cope with the chance that the dimensions would
3598                // have different order in source and destination regions            
3599                if (dimCase[type] > zone) dimCase[type]++;
3600                DimensionRegion* dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase);
3601                dstDimRgn->CopyAssign(srcDimRgn);
3602                // if this is the requested zone to be splitted, then also copy
3603                // the source DimensionRegion to the newly created target zone
3604                // and set the old zones upper limit lower
3605                if (dimCase[type] == zone) {
3606                    // lower old zones upper limit
3607                    if (newDef.split_type == split_type_normal) {
3608                        const int high =
3609                            dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex];
3610                        int low = 0;
3611                        if (zone > 0) {
3612                            std::map<dimension_t,int> lowerCase = dimCase;
3613                            lowerCase[type]--;
3614                            DimensionRegion* dstDimRgnLow = tempRgn->GetDimensionRegionByBit(lowerCase);
3615                            low = dstDimRgnLow->DimensionUpperLimits[tempIncreasedDimensionIndex];
3616                        }
3617                        dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex] = low + (high - low) / 2;
3618                    }
3619                    // fill the newly created zone of the divided zone as well
3620                    dimCase[type]++;
3621                    dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase);
3622                    dstDimRgn->CopyAssign(srcDimRgn);
3623                }
3624            }
3625    
3626            // now tempRegion's dimensions and DimensionRegions basically reflect
3627            // what we wanted to get for this actual Region here, so we now just
3628            // delete and recreate the dimension in question with the new amount
3629            // zones and then copy back from tempRegion      
3630            DeleteDimension(oldDef);
3631            AddDimension(&newDef);
3632            for (int iSrc = 0; iSrc < 256; ++iSrc) {
3633                DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc];
3634                if (!srcDimRgn) continue;
3635                std::map<dimension_t,int> dimCase;
3636                for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) {
3637                    const int srcBits = tempRgn->pDimensionDefinitions[d].bits;
3638                    dimCase[tempRgn->pDimensionDefinitions[d].dimension] =
3639                        (iSrc >> baseBits) & ((1 << srcBits) - 1);
3640                    baseBits += srcBits;
3641                }
3642                // a bit paranoid: cope with the chance that the dimensions would
3643                // have different order in source and destination regions
3644                DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase);
3645                if (!dstDimRgn) continue;
3646                dstDimRgn->CopyAssign(srcDimRgn);
3647            }
3648    
3649            // delete temporary region
3650            delete tempRgn;
3651    
3652            UpdateVelocityTable();
3653        }
3654    
3655        DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {
3656            uint8_t bits[8] = {};
3657            for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();
3658                 it != DimCase.end(); ++it)
3659            {
3660                for (int d = 0; d < Dimensions; ++d) {
3661                    if (pDimensionDefinitions[d].dimension == it->first) {
3662                        bits[d] = it->second;
3663                        goto nextDimCaseSlice;
3664                    }
3665                }
3666                assert(false); // do crash ... too harsh maybe ? ignore it instead ?
3667                nextDimCaseSlice:
3668                ; // noop
3669            }
3670            return GetDimensionRegionByBit(bits);
3671        }
3672    
3673      /**      /**
3674       * Searches in the current Region for a dimension of the given dimension       * Searches in the current Region for a dimension of the given dimension
3675       * type and returns the precise configuration of that dimension in this       * type and returns the precise configuration of that dimension in this
# Line 3461  namespace { Line 3743  namespace {
3743              }              }
3744              bitpos += pDimensionDefinitions[i].bits;              bitpos += pDimensionDefinitions[i].bits;
3745          }          }
3746          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx & 255];
3747            if (!dimreg) return NULL;
3748          if (veldim != -1) {          if (veldim != -1) {
3749              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
3750              if (dimreg->VelocityTable) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
3751                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3752              else // normal split type              else // normal split type
3753                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3754    
3755              dimregidx |= bits << velbitpos;              const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3756              dimreg = pDimensionRegions[dimregidx];              dimregidx |= (bits & limiter_mask) << velbitpos;
3757                dimreg = pDimensionRegions[dimregidx & 255];
3758          }          }
3759          return dimreg;          return dimreg;
3760      }      }
3761    
3762        int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) {
3763            uint8_t bits;
3764            int veldim = -1;
3765            int velbitpos;
3766            int bitpos = 0;
3767            int dimregidx = 0;
3768            for (uint i = 0; i < Dimensions; i++) {
3769                if (pDimensionDefinitions[i].dimension == dimension_velocity) {
3770                    // the velocity dimension must be handled after the other dimensions
3771                    veldim = i;
3772                    velbitpos = bitpos;
3773                } else {
3774                    switch (pDimensionDefinitions[i].split_type) {
3775                        case split_type_normal:
3776                            if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
3777                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
3778                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
3779                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
3780                                }
3781                            } else {
3782                                // gig2: evenly sized zones
3783                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
3784                            }
3785                            break;
3786                        case split_type_bit: // the value is already the sought dimension bit number
3787                            const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
3788                            bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed
3789                            break;
3790                    }
3791                    dimregidx |= bits << bitpos;
3792                }
3793                bitpos += pDimensionDefinitions[i].bits;
3794            }
3795            dimregidx &= 255;
3796            DimensionRegion* dimreg = pDimensionRegions[dimregidx];
3797            if (!dimreg) return -1;
3798            if (veldim != -1) {
3799                // (dimreg is now the dimension region for the lowest velocity)
3800                if (dimreg->VelocityTable) // custom defined zone ranges
3801                    bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3802                else // normal split type
3803                    bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3804    
3805                const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3806                dimregidx |= (bits & limiter_mask) << velbitpos;
3807                dimregidx &= 255;
3808            }
3809            return dimregidx;
3810        }
3811    
3812      /**      /**
3813       * Returns the appropriate DimensionRegion for the given dimension bit       * Returns the appropriate DimensionRegion for the given dimension bit
3814       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>
# Line 3741  namespace { Line 4075  namespace {
4075          }          }
4076      }      }
4077    
4078    // *************** Script ***************
4079    // *
4080    
4081        Script::Script(ScriptGroup* group, RIFF::Chunk* ckScri) {
4082            pGroup = group;
4083            pChunk = ckScri;
4084            if (ckScri) { // object is loaded from file ...
4085                // read header
4086                uint32_t headerSize = ckScri->ReadUint32();
4087                Compression = (Compression_t) ckScri->ReadUint32();
4088                Encoding    = (Encoding_t) ckScri->ReadUint32();
4089                Language    = (Language_t) ckScri->ReadUint32();
4090                Bypass      = (Language_t) ckScri->ReadUint32() & 1;
4091                crc         = ckScri->ReadUint32();
4092                uint32_t nameSize = ckScri->ReadUint32();
4093                Name.resize(nameSize, ' ');
4094                for (int i = 0; i < nameSize; ++i)
4095                    Name[i] = ckScri->ReadUint8();
4096                // to handle potential future extensions of the header
4097                ckScri->SetPos(sizeof(int32_t) + headerSize);
4098                // read actual script data
4099                uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();
4100                data.resize(scriptSize);
4101                for (int i = 0; i < scriptSize; ++i)
4102                    data[i] = ckScri->ReadUint8();
4103            } else { // this is a new script object, so just initialize it as such ...
4104                Compression = COMPRESSION_NONE;
4105                Encoding = ENCODING_ASCII;
4106                Language = LANGUAGE_NKSP;
4107                Bypass   = false;
4108                crc      = 0;
4109                Name     = "Unnamed Script";
4110            }
4111        }
4112    
4113        Script::~Script() {
4114        }
4115    
4116        /**
4117         * Returns the current script (i.e. as source code) in text format.
4118         */
4119        String Script::GetScriptAsText() {
4120            String s;
4121            s.resize(data.size(), ' ');
4122            memcpy(&s[0], &data[0], data.size());
4123            return s;
4124        }
4125    
4126        /**
4127         * Replaces the current script with the new script source code text given
4128         * by @a text.
4129         *
4130         * @param text - new script source code
4131         */
4132        void Script::SetScriptAsText(const String& text) {
4133            data.resize(text.size());
4134            memcpy(&data[0], &text[0], text.size());
4135        }
4136    
4137        void Script::UpdateChunks() {
4138            // recalculate CRC32 check sum
4139            __resetCRC(crc);
4140            __calculateCRC(&data[0], data.size(), crc);
4141            __encodeCRC(crc);
4142            // make sure chunk exists and has the required size
4143            const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size();
4144            if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);
4145            else pChunk->Resize(chunkSize);
4146            // fill the chunk data to be written to disk
4147            uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();
4148            int pos = 0;
4149            store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size
4150            pos += sizeof(int32_t);
4151            store32(&pData[pos], Compression);
4152            pos += sizeof(int32_t);
4153            store32(&pData[pos], Encoding);
4154            pos += sizeof(int32_t);
4155            store32(&pData[pos], Language);
4156            pos += sizeof(int32_t);
4157            store32(&pData[pos], Bypass ? 1 : 0);
4158            pos += sizeof(int32_t);
4159            store32(&pData[pos], crc);
4160            pos += sizeof(int32_t);
4161            store32(&pData[pos], Name.size());
4162            pos += sizeof(int32_t);
4163            for (int i = 0; i < Name.size(); ++i, ++pos)
4164                pData[pos] = Name[i];
4165            for (int i = 0; i < data.size(); ++i, ++pos)
4166                pData[pos] = data[i];
4167        }
4168    
4169        /**
4170         * Move this script from its current ScriptGroup to another ScriptGroup
4171         * given by @a pGroup.
4172         *
4173         * @param pGroup - script's new group
4174         */
4175        void Script::SetGroup(ScriptGroup* pGroup) {
4176            if (this->pGroup = pGroup) return;
4177            if (pChunk)
4178                pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList);
4179            this->pGroup = pGroup;
4180        }
4181    
4182        /**
4183         * Returns the script group this script currently belongs to. Each script
4184         * is a member of exactly one ScriptGroup.
4185         *
4186         * @returns current script group
4187         */
4188        ScriptGroup* Script::GetGroup() const {
4189            return pGroup;
4190        }
4191    
4192        void Script::RemoveAllScriptReferences() {
4193            File* pFile = pGroup->pFile;
4194            for (int i = 0; pFile->GetInstrument(i); ++i) {
4195                Instrument* instr = pFile->GetInstrument(i);
4196                instr->RemoveScript(this);
4197            }
4198        }
4199    
4200    // *************** ScriptGroup ***************
4201    // *
4202    
4203        ScriptGroup::ScriptGroup(File* file, RIFF::List* lstRTIS) {
4204            pFile = file;
4205            pList = lstRTIS;
4206            pScripts = NULL;
4207            if (lstRTIS) {
4208                RIFF::Chunk* ckName = lstRTIS->GetSubChunk(CHUNK_ID_LSNM);
4209                ::LoadString(ckName, Name);
4210            } else {
4211                Name = "Default Group";
4212            }
4213        }
4214    
4215        ScriptGroup::~ScriptGroup() {
4216            if (pScripts) {
4217                std::list<Script*>::iterator iter = pScripts->begin();
4218                std::list<Script*>::iterator end  = pScripts->end();
4219                while (iter != end) {
4220                    delete *iter;
4221                    ++iter;
4222                }
4223                delete pScripts;
4224            }
4225        }
4226    
4227        void ScriptGroup::UpdateChunks() {
4228            if (pScripts) {
4229                if (!pList)
4230                    pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS);
4231    
4232                // now store the name of this group as <LSNM> chunk as subchunk of the <RTIS> list chunk
4233                ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64);
4234    
4235                for (std::list<Script*>::iterator it = pScripts->begin();
4236                     it != pScripts->end(); ++it)
4237                {
4238                    (*it)->UpdateChunks();
4239                }
4240            }
4241        }
4242    
4243        /** @brief Get instrument script.
4244         *
4245         * Returns the real-time instrument script with the given index.
4246         *
4247         * @param index - number of the sought script (0..n)
4248         * @returns sought script or NULL if there's no such script
4249         */
4250        Script* ScriptGroup::GetScript(uint index) {
4251            if (!pScripts) LoadScripts();
4252            std::list<Script*>::iterator it = pScripts->begin();
4253            for (uint i = 0; it != pScripts->end(); ++i, ++it)
4254                if (i == index) return *it;
4255            return NULL;
4256        }
4257    
4258        /** @brief Add new instrument script.
4259         *
4260         * Adds a new real-time instrument script to the file. The script is not
4261         * actually used / executed unless it is referenced by an instrument to be
4262         * used. This is similar to samples, which you can add to a file, without
4263         * an instrument necessarily actually using it.
4264         *
4265         * You have to call Save() to make this persistent to the file.
4266         *
4267         * @return new empty script object
4268         */
4269        Script* ScriptGroup::AddScript() {
4270            if (!pScripts) LoadScripts();
4271            Script* pScript = new Script(this, NULL);
4272            pScripts->push_back(pScript);
4273            return pScript;
4274        }
4275    
4276        /** @brief Delete an instrument script.
4277         *
4278         * This will delete the given real-time instrument script. References of
4279         * instruments that are using that script will be removed accordingly.
4280         *
4281         * You have to call Save() to make this persistent to the file.
4282         *
4283         * @param pScript - script to delete
4284         * @throws gig::Exception if given script could not be found
4285         */
4286        void ScriptGroup::DeleteScript(Script* pScript) {
4287            if (!pScripts) LoadScripts();
4288            std::list<Script*>::iterator iter =
4289                find(pScripts->begin(), pScripts->end(), pScript);
4290            if (iter == pScripts->end())
4291                throw gig::Exception("Could not delete script, could not find given script");
4292            pScripts->erase(iter);
4293            pScript->RemoveAllScriptReferences();
4294            if (pScript->pChunk)
4295                pScript->pChunk->GetParent()->DeleteSubChunk(pScript->pChunk);
4296            delete pScript;
4297        }
4298    
4299        void ScriptGroup::LoadScripts() {
4300            if (pScripts) return;
4301            pScripts = new std::list<Script*>;
4302            if (!pList) return;
4303    
4304            for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck;
4305                 ck = pList->GetNextSubChunk())
4306            {
4307                if (ck->GetChunkID() == CHUNK_ID_SCRI) {
4308                    pScripts->push_back(new Script(this, ck));
4309                }
4310            }
4311        }
4312    
4313  // *************** Instrument ***************  // *************** Instrument ***************
4314  // *  // *
4315    
# Line 3763  namespace { Line 4332  namespace {
4332          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
4333          pMidiRules = new MidiRule*[3];          pMidiRules = new MidiRule*[3];
4334          pMidiRules[0] = NULL;          pMidiRules[0] = NULL;
4335            pScriptRefs = NULL;
4336    
4337          // Loading          // Loading
4338          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 3823  namespace { Line 4393  namespace {
4393              }              }
4394          }          }
4395    
4396            // own gig format extensions
4397            RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS);
4398            if (lst3LS) {
4399                RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4400                if (ckSCSL) {
4401                    int headerSize = ckSCSL->ReadUint32();
4402                    int slotCount  = ckSCSL->ReadUint32();
4403                    if (slotCount) {
4404                        int slotSize  = ckSCSL->ReadUint32();
4405                        ckSCSL->SetPos(headerSize); // in case of future header extensions
4406                        int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions
4407                        for (int i = 0; i < slotCount; ++i) {
4408                            _ScriptPooolEntry e;
4409                            e.fileOffset = ckSCSL->ReadUint32();
4410                            e.bypass     = ckSCSL->ReadUint32() & 1;
4411                            if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions
4412                            scriptPoolFileOffsets.push_back(e);
4413                        }
4414                    }
4415                }
4416            }
4417    
4418          __notify_progress(pProgress, 1.0f); // notify done          __notify_progress(pProgress, 1.0f); // notify done
4419      }      }
4420    
# Line 3843  namespace { Line 4435  namespace {
4435              delete pMidiRules[i];              delete pMidiRules[i];
4436          }          }
4437          delete[] pMidiRules;          delete[] pMidiRules;
4438            if (pScriptRefs) delete pScriptRefs;
4439      }      }
4440    
4441      /**      /**
# Line 3898  namespace { Line 4491  namespace {
4491                  pMidiRules[i]->UpdateChunks(pData);                  pMidiRules[i]->UpdateChunks(pData);
4492              }              }
4493          }          }
4494    
4495            // own gig format extensions
4496           if (pScriptRefs) {
4497               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4498               if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4499               const int slotCount = pScriptRefs->size();
4500               const int headerSize = 3 * sizeof(uint32_t);
4501               const int slotSize  = 2 * sizeof(uint32_t);
4502               const int totalChunkSize = headerSize + slotCount * slotSize;
4503               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4504               if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize);
4505               else ckSCSL->Resize(totalChunkSize);
4506               uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();
4507               int pos = 0;
4508               store32(&pData[pos], headerSize);
4509               pos += sizeof(uint32_t);
4510               store32(&pData[pos], slotCount);
4511               pos += sizeof(uint32_t);
4512               store32(&pData[pos], slotSize);
4513               pos += sizeof(uint32_t);
4514               for (int i = 0; i < slotCount; ++i) {
4515                   // arbitrary value, the actual file offset will be updated in
4516                   // UpdateScriptFileOffsets() after the file has been resized
4517                   int bogusFileOffset = 0;
4518                   store32(&pData[pos], bogusFileOffset);
4519                   pos += sizeof(uint32_t);
4520                   store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
4521                   pos += sizeof(uint32_t);
4522               }
4523           }
4524        }
4525    
4526        void Instrument::UpdateScriptFileOffsets() {
4527           // own gig format extensions
4528           if (pScriptRefs) {
4529               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4530               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4531               const int slotCount = pScriptRefs->size();
4532               const int headerSize = 3 * sizeof(uint32_t);
4533               ckSCSL->SetPos(headerSize);
4534               for (int i = 0; i < slotCount; ++i) {
4535                   uint32_t fileOffset =
4536                        (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4537                        (*pScriptRefs)[i].script->pChunk->GetPos() -
4538                        CHUNK_HEADER_SIZE;
4539                   ckSCSL->WriteUint32(&fileOffset);
4540                   // jump over flags entry (containing the bypass flag)
4541                   ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos);
4542               }
4543           }        
4544      }      }
4545    
4546      /**      /**
# Line 4029  namespace { Line 4672  namespace {
4672          pMidiRules[i] = 0;          pMidiRules[i] = 0;
4673      }      }
4674    
4675        void Instrument::LoadScripts() {
4676            if (pScriptRefs) return;
4677            pScriptRefs = new std::vector<_ScriptPooolRef>;
4678            if (scriptPoolFileOffsets.empty()) return;
4679            File* pFile = (File*) GetParent();
4680            for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4681                uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset;
4682                for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4683                    ScriptGroup* group = pFile->GetScriptGroup(i);
4684                    for (uint s = 0; group->GetScript(s); ++s) {
4685                        Script* script = group->GetScript(s);
4686                        if (script->pChunk) {
4687                            uint32_t offset = script->pChunk->GetFilePos() -
4688                                              script->pChunk->GetPos() -
4689                                              CHUNK_HEADER_SIZE;
4690                            if (offset == soughtOffset)
4691                            {
4692                                _ScriptPooolRef ref;
4693                                ref.script = script;
4694                                ref.bypass = scriptPoolFileOffsets[k].bypass;
4695                                pScriptRefs->push_back(ref);
4696                                break;
4697                            }
4698                        }
4699                    }
4700                }
4701            }
4702            // we don't need that anymore
4703            scriptPoolFileOffsets.clear();
4704        }
4705    
4706        /** @brief Get instrument script (gig format extension).
4707         *
4708         * Returns the real-time instrument script of instrument script slot
4709         * @a index.
4710         *
4711         * @note This is an own format extension which did not exist i.e. in the
4712         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4713         * gigedit.
4714         *
4715         * @param index - instrument script slot index
4716         * @returns script or NULL if index is out of bounds
4717         */
4718        Script* Instrument::GetScriptOfSlot(uint index) {
4719            LoadScripts();
4720            if (index >= pScriptRefs->size()) return NULL;
4721            return pScriptRefs->at(index).script;
4722        }
4723    
4724        /** @brief Add new instrument script slot (gig format extension).
4725         *
4726         * Add the given real-time instrument script reference to this instrument,
4727         * which shall be executed by the sampler for for this instrument. The
4728         * script will be added to the end of the script list of this instrument.
4729         * The positions of the scripts in the Instrument's Script list are
4730         * relevant, because they define in which order they shall be executed by
4731         * the sampler. For this reason it is also legal to add the same script
4732         * twice to an instrument, for example you might have a script called
4733         * "MyFilter" which performs an event filter task, and you might have
4734         * another script called "MyNoteTrigger" which triggers new notes, then you
4735         * might for example have the following list of scripts on the instrument:
4736         *
4737         * 1. Script "MyFilter"
4738         * 2. Script "MyNoteTrigger"
4739         * 3. Script "MyFilter"
4740         *
4741         * Which would make sense, because the 2nd script launched new events, which
4742         * you might need to filter as well.
4743         *
4744         * There are two ways to disable / "bypass" scripts. You can either disable
4745         * a script locally for the respective script slot on an instrument (i.e. by
4746         * passing @c false to the 2nd argument of this method, or by calling
4747         * SetScriptBypassed()). Or you can disable a script globally for all slots
4748         * and all instruments by setting Script::Bypass.
4749         *
4750         * @note This is an own format extension which did not exist i.e. in the
4751         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4752         * gigedit.
4753         *
4754         * @param pScript - script that shall be executed for this instrument
4755         * @param bypass  - if enabled, the sampler shall skip executing this
4756         *                  script (in the respective list position)
4757         * @see SetScriptBypassed()
4758         */
4759        void Instrument::AddScriptSlot(Script* pScript, bool bypass) {
4760            LoadScripts();
4761            _ScriptPooolRef ref = { pScript, bypass };
4762            pScriptRefs->push_back(ref);
4763        }
4764    
4765        /** @brief Flip two script slots with each other (gig format extension).
4766         *
4767         * Swaps the position of the two given scripts in the Instrument's Script
4768         * list. The positions of the scripts in the Instrument's Script list are
4769         * relevant, because they define in which order they shall be executed by
4770         * the sampler.
4771         *
4772         * @note This is an own format extension which did not exist i.e. in the
4773         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4774         * gigedit.
4775         *
4776         * @param index1 - index of the first script slot to swap
4777         * @param index2 - index of the second script slot to swap
4778         */
4779        void Instrument::SwapScriptSlots(uint index1, uint index2) {
4780            LoadScripts();
4781            if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size())
4782                return;
4783            _ScriptPooolRef tmp = (*pScriptRefs)[index1];
4784            (*pScriptRefs)[index1] = (*pScriptRefs)[index2];
4785            (*pScriptRefs)[index2] = tmp;
4786        }
4787    
4788        /** @brief Remove script slot.
4789         *
4790         * Removes the script slot with the given slot index.
4791         *
4792         * @param index - index of script slot to remove
4793         */
4794        void Instrument::RemoveScriptSlot(uint index) {
4795            LoadScripts();
4796            if (index >= pScriptRefs->size()) return;
4797            pScriptRefs->erase( pScriptRefs->begin() + index );
4798        }
4799    
4800        /** @brief Remove reference to given Script (gig format extension).
4801         *
4802         * This will remove all script slots on the instrument which are referencing
4803         * the given script.
4804         *
4805         * @note This is an own format extension which did not exist i.e. in the
4806         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4807         * gigedit.
4808         *
4809         * @param pScript - script reference to remove from this instrument
4810         * @see RemoveScriptSlot()
4811         */
4812        void Instrument::RemoveScript(Script* pScript) {
4813            LoadScripts();
4814            for (int i = pScriptRefs->size() - 1; i >= 0; --i) {
4815                if ((*pScriptRefs)[i].script == pScript) {
4816                    pScriptRefs->erase( pScriptRefs->begin() + i );
4817                }
4818            }
4819        }
4820    
4821        /** @brief Instrument's amount of script slots.
4822         *
4823         * This method returns the amount of script slots this instrument currently
4824         * uses.
4825         *
4826         * A script slot is a reference of a real-time instrument script to be
4827         * executed by the sampler. The scripts will be executed by the sampler in
4828         * sequence of the slots. One (same) script may be referenced multiple
4829         * times in different slots.
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        uint Instrument::ScriptSlotCount() const {
4836            return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();
4837        }
4838    
4839        /** @brief Whether script execution shall be skipped.
4840         *
4841         * Defines locally for the Script reference slot in the Instrument's Script
4842         * list, whether the script shall be skipped by the sampler regarding
4843         * execution.
4844         *
4845         * It is also possible to ignore exeuction of the script globally, for all
4846         * slots and for all instruments by setting Script::Bypass.
4847         *
4848         * @note This is an own format extension which did not exist i.e. in the
4849         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4850         * gigedit.
4851         *
4852         * @param index - index of the script slot on this instrument
4853         * @see Script::Bypass
4854         */
4855        bool Instrument::IsScriptSlotBypassed(uint index) {
4856            if (index >= ScriptSlotCount()) return false;
4857            return pScriptRefs ? pScriptRefs->at(index).bypass
4858                               : scriptPoolFileOffsets.at(index).bypass;
4859            
4860        }
4861    
4862        /** @brief Defines whether execution shall be skipped.
4863         *
4864         * You can call this method to define locally whether or whether not the
4865         * given script slot shall be executed by the sampler.
4866         *
4867         * @note This is an own format extension which did not exist i.e. in the
4868         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4869         * gigedit.
4870         *
4871         * @param index - script slot index on this instrument
4872         * @param bBypass - if true, the script slot will be skipped by the sampler
4873         * @see Script::Bypass
4874         */
4875        void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) {
4876            if (index >= ScriptSlotCount()) return;
4877            if (pScriptRefs)
4878                pScriptRefs->at(index).bypass = bBypass;
4879            else
4880                scriptPoolFileOffsets.at(index).bypass = bBypass;
4881        }
4882    
4883      /**      /**
4884       * 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
4885       * and assign it to this object.       * and assign it to this object.
# Line 4062  namespace { Line 4913  namespace {
4913          PitchbendRange = orig->PitchbendRange;          PitchbendRange = orig->PitchbendRange;
4914          PianoReleaseMode = orig->PianoReleaseMode;          PianoReleaseMode = orig->PianoReleaseMode;
4915          DimensionKeyRange = orig->DimensionKeyRange;          DimensionKeyRange = orig->DimensionKeyRange;
4916            scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
4917            pScriptRefs = orig->pScriptRefs;
4918                    
4919          // free old midi rules          // free old midi rules
4920          for (int i = 0 ; pMidiRules[i] ; i++) {          for (int i = 0 ; pMidiRules[i] ; i++) {
# Line 4247  namespace { Line 5100  namespace {
5100          bAutoLoad = true;          bAutoLoad = true;
5101          *pVersion = VERSION_3;          *pVersion = VERSION_3;
5102          pGroups = NULL;          pGroups = NULL;
5103            pScriptGroups = NULL;
5104          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5105          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
5106    
# Line 4262  namespace { Line 5116  namespace {
5116      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
5117          bAutoLoad = true;          bAutoLoad = true;
5118          pGroups = NULL;          pGroups = NULL;
5119            pScriptGroups = NULL;
5120          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5121      }      }
5122    
# Line 4275  namespace { Line 5130  namespace {
5130              }              }
5131              delete pGroups;              delete pGroups;
5132          }          }
5133            if (pScriptGroups) {
5134                std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin();
5135                std::list<ScriptGroup*>::iterator end  = pScriptGroups->end();
5136                while (iter != end) {
5137                    delete *iter;
5138                    ++iter;
5139                }
5140                delete pScriptGroups;
5141            }
5142      }      }
5143    
5144      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 4787  namespace { Line 5651  namespace {
5651          }          }
5652      }      }
5653    
5654        /** @brief Get instrument script group (by index).
5655         *
5656         * Returns the real-time instrument script group with the given index.
5657         *
5658         * @param index - number of the sought group (0..n)
5659         * @returns sought script group or NULL if there's no such group
5660         */
5661        ScriptGroup* File::GetScriptGroup(uint index) {
5662            if (!pScriptGroups) LoadScriptGroups();
5663            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5664            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5665                if (i == index) return *it;
5666            return NULL;
5667        }
5668    
5669        /** @brief Get instrument script group (by name).
5670         *
5671         * Returns the first real-time instrument script group found with the given
5672         * group name. Note that group names may not necessarily be unique.
5673         *
5674         * @param name - name of the sought script group
5675         * @returns sought script group or NULL if there's no such group
5676         */
5677        ScriptGroup* File::GetScriptGroup(const String& name) {
5678            if (!pScriptGroups) LoadScriptGroups();
5679            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5680            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5681                if ((*it)->Name == name) return *it;
5682            return NULL;
5683        }
5684    
5685        /** @brief Add new instrument script group.
5686         *
5687         * Adds a new, empty real-time instrument script group to the file.
5688         *
5689         * You have to call Save() to make this persistent to the file.
5690         *
5691         * @return new empty script group
5692         */
5693        ScriptGroup* File::AddScriptGroup() {
5694            if (!pScriptGroups) LoadScriptGroups();
5695            ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL);
5696            pScriptGroups->push_back(pScriptGroup);
5697            return pScriptGroup;
5698        }
5699    
5700        /** @brief Delete an instrument script group.
5701         *
5702         * This will delete the given real-time instrument script group and all its
5703         * instrument scripts it contains. References inside instruments that are
5704         * using the deleted scripts will be removed from the respective instruments
5705         * accordingly.
5706         *
5707         * You have to call Save() to make this persistent to the file.
5708         *
5709         * @param pScriptGroup - script group to delete
5710         * @throws gig::Exception if given script group could not be found
5711         */
5712        void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) {
5713            if (!pScriptGroups) LoadScriptGroups();
5714            std::list<ScriptGroup*>::iterator iter =
5715                find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup);
5716            if (iter == pScriptGroups->end())
5717                throw gig::Exception("Could not delete script group, could not find given script group");
5718            pScriptGroups->erase(iter);
5719            for (int i = 0; pScriptGroup->GetScript(i); ++i)
5720                pScriptGroup->DeleteScript(pScriptGroup->GetScript(i));
5721            if (pScriptGroup->pList)
5722                pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList);
5723            delete pScriptGroup;
5724        }
5725    
5726        void File::LoadScriptGroups() {
5727            if (pScriptGroups) return;
5728            pScriptGroups = new std::list<ScriptGroup*>;
5729            RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS);
5730            if (lstLS) {
5731                for (RIFF::List* lst = lstLS->GetFirstSubList(); lst;
5732                     lst = lstLS->GetNextSubList())
5733                {
5734                    if (lst->GetListType() == LIST_TYPE_RTIS) {
5735                        pScriptGroups->push_back(new ScriptGroup(this, lst));
5736                    }
5737                }
5738            }
5739        }
5740    
5741      /**      /**
5742       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
5743       * 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 4802  namespace { Line 5753  namespace {
5753    
5754          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;
5755    
5756            // update own gig format extension chunks
5757            // (not part of the GigaStudio 4 format)
5758            //
5759            // This must be performed before writing the chunks for instruments,
5760            // because the instruments' script slots will write the file offsets
5761            // of the respective instrument script chunk as reference.
5762            if (pScriptGroups) {
5763                RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
5764                if (pScriptGroups->empty()) {
5765                    if (lst3LS) pRIFF->DeleteSubChunk(lst3LS);
5766                } else {
5767                    if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
5768    
5769                    // Update instrument script (group) chunks.
5770    
5771                    for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5772                         it != pScriptGroups->end(); ++it)
5773                    {
5774                        (*it)->UpdateChunks();
5775                    }
5776                }
5777            }
5778    
5779          // first update base class's chunks          // first update base class's chunks
5780          DLS::File::UpdateChunks();          DLS::File::UpdateChunks();
5781    
# Line 4972  namespace { Line 5946  namespace {
5946              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
5947          }          }
5948      }      }
5949        
5950        void File::UpdateFileOffsets() {
5951            DLS::File::UpdateFileOffsets();
5952    
5953            for (Instrument* instrument = GetFirstInstrument(); instrument;
5954                 instrument = GetNextInstrument())
5955            {
5956                instrument->UpdateScriptFileOffsets();
5957            }
5958        }
5959    
5960      /**      /**
5961       * Enable / disable automatic loading. By default this properyt is       * Enable / disable automatic loading. By default this properyt is

Legend:
Removed from v.2547  
changed lines
  Added in v.2609

  ViewVC Help
Powered by ViewVC