/[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 2555 by schoenebeck, Fri May 16 23:08:42 2014 UTC revision 2667 by schoenebeck, Mon Jul 7 15:20:22 2014 UTC
# Line 3056  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 3509  namespace { Line 3509  namespace {
3509    
3510          // delete temporary region          // delete temporary region
3511          delete tempRgn;          delete tempRgn;
3512    
3513            UpdateVelocityTable();
3514      }      }
3515    
3516      /** @brief Divide split zone of a dimension in two (increment zone amount).      /** @brief Divide split zone of a dimension in two (increment zone amount).
# Line 3646  namespace { Line 3648  namespace {
3648    
3649          // delete temporary region          // delete temporary region
3650          delete tempRgn;          delete tempRgn;
3651    
3652            UpdateVelocityTable();
3653        }
3654    
3655        /** @brief Change type of an existing dimension.
3656         *
3657         * Alters the dimension type of a dimension already existing on this
3658         * region. If there is currently no dimension on this Region with type
3659         * @a oldType, then this call with throw an Exception. Likewise there are
3660         * cases where the requested dimension type cannot be performed. For example
3661         * if the new dimension type shall be gig::dimension_samplechannel, and the
3662         * current dimension has more than 2 zones. In such cases an Exception is
3663         * thrown as well.
3664         *
3665         * @param oldType - identifies the existing dimension to be changed
3666         * @param newType - to which dimension type it should be changed to
3667         * @throws gig::Exception if requested change cannot be performed
3668         */
3669        void Region::SetDimensionType(dimension_t oldType, dimension_t newType) {
3670            if (oldType == newType) return;
3671            dimension_def_t* def = GetDimensionDefinition(oldType);
3672            if (!def)
3673                throw gig::Exception("No dimension with provided old dimension type exists on this region");
3674            if (newType == dimension_samplechannel && def->zones != 2)
3675                throw gig::Exception("Cannot change to dimension type 'sample channel', because existing dimension does not have 2 zones");
3676            if (GetDimensionDefinition(newType))
3677                throw gig::Exception("There is already a dimension with requested new dimension type on this region");
3678            def->dimension  = newType;
3679            def->split_type = __resolveSplitType(newType);
3680      }      }
3681    
3682      DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {      DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {
# Line 3739  namespace { Line 3770  namespace {
3770              }              }
3771              bitpos += pDimensionDefinitions[i].bits;              bitpos += pDimensionDefinitions[i].bits;
3772          }          }
3773          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx & 255];
3774            if (!dimreg) return NULL;
3775          if (veldim != -1) {          if (veldim != -1) {
3776              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
3777              if (dimreg->VelocityTable) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
3778                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3779              else // normal split type              else // normal split type
3780                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3781    
3782              dimregidx |= bits << velbitpos;              const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3783              dimreg = pDimensionRegions[dimregidx];              dimregidx |= (bits & limiter_mask) << velbitpos;
3784                dimreg = pDimensionRegions[dimregidx & 255];
3785          }          }
3786          return dimreg;          return dimreg;
3787      }      }
3788    
3789        int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) {
3790            uint8_t bits;
3791            int veldim = -1;
3792            int velbitpos;
3793            int bitpos = 0;
3794            int dimregidx = 0;
3795            for (uint i = 0; i < Dimensions; i++) {
3796                if (pDimensionDefinitions[i].dimension == dimension_velocity) {
3797                    // the velocity dimension must be handled after the other dimensions
3798                    veldim = i;
3799                    velbitpos = bitpos;
3800                } else {
3801                    switch (pDimensionDefinitions[i].split_type) {
3802                        case split_type_normal:
3803                            if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
3804                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
3805                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
3806                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
3807                                }
3808                            } else {
3809                                // gig2: evenly sized zones
3810                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
3811                            }
3812                            break;
3813                        case split_type_bit: // the value is already the sought dimension bit number
3814                            const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
3815                            bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed
3816                            break;
3817                    }
3818                    dimregidx |= bits << bitpos;
3819                }
3820                bitpos += pDimensionDefinitions[i].bits;
3821            }
3822            dimregidx &= 255;
3823            DimensionRegion* dimreg = pDimensionRegions[dimregidx];
3824            if (!dimreg) return -1;
3825            if (veldim != -1) {
3826                // (dimreg is now the dimension region for the lowest velocity)
3827                if (dimreg->VelocityTable) // custom defined zone ranges
3828                    bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3829                else // normal split type
3830                    bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3831    
3832                const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3833                dimregidx |= (bits & limiter_mask) << velbitpos;
3834                dimregidx &= 255;
3835            }
3836            return dimregidx;
3837        }
3838    
3839      /**      /**
3840       * Returns the appropriate DimensionRegion for the given dimension bit       * Returns the appropriate DimensionRegion for the given dimension bit
3841       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>
# Line 4019  namespace { Line 4102  namespace {
4102          }          }
4103      }      }
4104    
4105    // *************** Script ***************
4106    // *
4107    
4108        Script::Script(ScriptGroup* group, RIFF::Chunk* ckScri) {
4109            pGroup = group;
4110            pChunk = ckScri;
4111            if (ckScri) { // object is loaded from file ...
4112                // read header
4113                uint32_t headerSize = ckScri->ReadUint32();
4114                Compression = (Compression_t) ckScri->ReadUint32();
4115                Encoding    = (Encoding_t) ckScri->ReadUint32();
4116                Language    = (Language_t) ckScri->ReadUint32();
4117                Bypass      = (Language_t) ckScri->ReadUint32() & 1;
4118                crc         = ckScri->ReadUint32();
4119                uint32_t nameSize = ckScri->ReadUint32();
4120                Name.resize(nameSize, ' ');
4121                for (int i = 0; i < nameSize; ++i)
4122                    Name[i] = ckScri->ReadUint8();
4123                // to handle potential future extensions of the header
4124                ckScri->SetPos(sizeof(int32_t) + headerSize);
4125                // read actual script data
4126                uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();
4127                data.resize(scriptSize);
4128                for (int i = 0; i < scriptSize; ++i)
4129                    data[i] = ckScri->ReadUint8();
4130            } else { // this is a new script object, so just initialize it as such ...
4131                Compression = COMPRESSION_NONE;
4132                Encoding = ENCODING_ASCII;
4133                Language = LANGUAGE_NKSP;
4134                Bypass   = false;
4135                crc      = 0;
4136                Name     = "Unnamed Script";
4137            }
4138        }
4139    
4140        Script::~Script() {
4141        }
4142    
4143        /**
4144         * Returns the current script (i.e. as source code) in text format.
4145         */
4146        String Script::GetScriptAsText() {
4147            String s;
4148            s.resize(data.size(), ' ');
4149            memcpy(&s[0], &data[0], data.size());
4150            return s;
4151        }
4152    
4153        /**
4154         * Replaces the current script with the new script source code text given
4155         * by @a text.
4156         *
4157         * @param text - new script source code
4158         */
4159        void Script::SetScriptAsText(const String& text) {
4160            data.resize(text.size());
4161            memcpy(&data[0], &text[0], text.size());
4162        }
4163    
4164        void Script::UpdateChunks() {
4165            // recalculate CRC32 check sum
4166            __resetCRC(crc);
4167            __calculateCRC(&data[0], data.size(), crc);
4168            __encodeCRC(crc);
4169            // make sure chunk exists and has the required size
4170            const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size();
4171            if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);
4172            else pChunk->Resize(chunkSize);
4173            // fill the chunk data to be written to disk
4174            uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();
4175            int pos = 0;
4176            store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size
4177            pos += sizeof(int32_t);
4178            store32(&pData[pos], Compression);
4179            pos += sizeof(int32_t);
4180            store32(&pData[pos], Encoding);
4181            pos += sizeof(int32_t);
4182            store32(&pData[pos], Language);
4183            pos += sizeof(int32_t);
4184            store32(&pData[pos], Bypass ? 1 : 0);
4185            pos += sizeof(int32_t);
4186            store32(&pData[pos], crc);
4187            pos += sizeof(int32_t);
4188            store32(&pData[pos], Name.size());
4189            pos += sizeof(int32_t);
4190            for (int i = 0; i < Name.size(); ++i, ++pos)
4191                pData[pos] = Name[i];
4192            for (int i = 0; i < data.size(); ++i, ++pos)
4193                pData[pos] = data[i];
4194        }
4195    
4196        /**
4197         * Move this script from its current ScriptGroup to another ScriptGroup
4198         * given by @a pGroup.
4199         *
4200         * @param pGroup - script's new group
4201         */
4202        void Script::SetGroup(ScriptGroup* pGroup) {
4203            if (this->pGroup = pGroup) return;
4204            if (pChunk)
4205                pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList);
4206            this->pGroup = pGroup;
4207        }
4208    
4209        /**
4210         * Returns the script group this script currently belongs to. Each script
4211         * is a member of exactly one ScriptGroup.
4212         *
4213         * @returns current script group
4214         */
4215        ScriptGroup* Script::GetGroup() const {
4216            return pGroup;
4217        }
4218    
4219        void Script::RemoveAllScriptReferences() {
4220            File* pFile = pGroup->pFile;
4221            for (int i = 0; pFile->GetInstrument(i); ++i) {
4222                Instrument* instr = pFile->GetInstrument(i);
4223                instr->RemoveScript(this);
4224            }
4225        }
4226    
4227    // *************** ScriptGroup ***************
4228    // *
4229    
4230        ScriptGroup::ScriptGroup(File* file, RIFF::List* lstRTIS) {
4231            pFile = file;
4232            pList = lstRTIS;
4233            pScripts = NULL;
4234            if (lstRTIS) {
4235                RIFF::Chunk* ckName = lstRTIS->GetSubChunk(CHUNK_ID_LSNM);
4236                ::LoadString(ckName, Name);
4237            } else {
4238                Name = "Default Group";
4239            }
4240        }
4241    
4242        ScriptGroup::~ScriptGroup() {
4243            if (pScripts) {
4244                std::list<Script*>::iterator iter = pScripts->begin();
4245                std::list<Script*>::iterator end  = pScripts->end();
4246                while (iter != end) {
4247                    delete *iter;
4248                    ++iter;
4249                }
4250                delete pScripts;
4251            }
4252        }
4253    
4254        void ScriptGroup::UpdateChunks() {
4255            if (pScripts) {
4256                if (!pList)
4257                    pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS);
4258    
4259                // now store the name of this group as <LSNM> chunk as subchunk of the <RTIS> list chunk
4260                ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64);
4261    
4262                for (std::list<Script*>::iterator it = pScripts->begin();
4263                     it != pScripts->end(); ++it)
4264                {
4265                    (*it)->UpdateChunks();
4266                }
4267            }
4268        }
4269    
4270        /** @brief Get instrument script.
4271         *
4272         * Returns the real-time instrument script with the given index.
4273         *
4274         * @param index - number of the sought script (0..n)
4275         * @returns sought script or NULL if there's no such script
4276         */
4277        Script* ScriptGroup::GetScript(uint index) {
4278            if (!pScripts) LoadScripts();
4279            std::list<Script*>::iterator it = pScripts->begin();
4280            for (uint i = 0; it != pScripts->end(); ++i, ++it)
4281                if (i == index) return *it;
4282            return NULL;
4283        }
4284    
4285        /** @brief Add new instrument script.
4286         *
4287         * Adds a new real-time instrument script to the file. The script is not
4288         * actually used / executed unless it is referenced by an instrument to be
4289         * used. This is similar to samples, which you can add to a file, without
4290         * an instrument necessarily actually using it.
4291         *
4292         * You have to call Save() to make this persistent to the file.
4293         *
4294         * @return new empty script object
4295         */
4296        Script* ScriptGroup::AddScript() {
4297            if (!pScripts) LoadScripts();
4298            Script* pScript = new Script(this, NULL);
4299            pScripts->push_back(pScript);
4300            return pScript;
4301        }
4302    
4303        /** @brief Delete an instrument script.
4304         *
4305         * This will delete the given real-time instrument script. References of
4306         * instruments that are using that script will be removed accordingly.
4307         *
4308         * You have to call Save() to make this persistent to the file.
4309         *
4310         * @param pScript - script to delete
4311         * @throws gig::Exception if given script could not be found
4312         */
4313        void ScriptGroup::DeleteScript(Script* pScript) {
4314            if (!pScripts) LoadScripts();
4315            std::list<Script*>::iterator iter =
4316                find(pScripts->begin(), pScripts->end(), pScript);
4317            if (iter == pScripts->end())
4318                throw gig::Exception("Could not delete script, could not find given script");
4319            pScripts->erase(iter);
4320            pScript->RemoveAllScriptReferences();
4321            if (pScript->pChunk)
4322                pScript->pChunk->GetParent()->DeleteSubChunk(pScript->pChunk);
4323            delete pScript;
4324        }
4325    
4326        void ScriptGroup::LoadScripts() {
4327            if (pScripts) return;
4328            pScripts = new std::list<Script*>;
4329            if (!pList) return;
4330    
4331            for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck;
4332                 ck = pList->GetNextSubChunk())
4333            {
4334                if (ck->GetChunkID() == CHUNK_ID_SCRI) {
4335                    pScripts->push_back(new Script(this, ck));
4336                }
4337            }
4338        }
4339    
4340  // *************** Instrument ***************  // *************** Instrument ***************
4341  // *  // *
4342    
# Line 4041  namespace { Line 4359  namespace {
4359          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
4360          pMidiRules = new MidiRule*[3];          pMidiRules = new MidiRule*[3];
4361          pMidiRules[0] = NULL;          pMidiRules[0] = NULL;
4362            pScriptRefs = NULL;
4363    
4364          // Loading          // Loading
4365          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 4101  namespace { Line 4420  namespace {
4420              }              }
4421          }          }
4422    
4423            // own gig format extensions
4424            RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS);
4425            if (lst3LS) {
4426                RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4427                if (ckSCSL) {
4428                    int headerSize = ckSCSL->ReadUint32();
4429                    int slotCount  = ckSCSL->ReadUint32();
4430                    if (slotCount) {
4431                        int slotSize  = ckSCSL->ReadUint32();
4432                        ckSCSL->SetPos(headerSize); // in case of future header extensions
4433                        int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions
4434                        for (int i = 0; i < slotCount; ++i) {
4435                            _ScriptPooolEntry e;
4436                            e.fileOffset = ckSCSL->ReadUint32();
4437                            e.bypass     = ckSCSL->ReadUint32() & 1;
4438                            if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions
4439                            scriptPoolFileOffsets.push_back(e);
4440                        }
4441                    }
4442                }
4443            }
4444    
4445          __notify_progress(pProgress, 1.0f); // notify done          __notify_progress(pProgress, 1.0f); // notify done
4446      }      }
4447    
# Line 4121  namespace { Line 4462  namespace {
4462              delete pMidiRules[i];              delete pMidiRules[i];
4463          }          }
4464          delete[] pMidiRules;          delete[] pMidiRules;
4465            if (pScriptRefs) delete pScriptRefs;
4466      }      }
4467    
4468      /**      /**
# Line 4176  namespace { Line 4518  namespace {
4518                  pMidiRules[i]->UpdateChunks(pData);                  pMidiRules[i]->UpdateChunks(pData);
4519              }              }
4520          }          }
4521    
4522            // own gig format extensions
4523           if (ScriptSlotCount()) {
4524               // make sure we have converted the original loaded script file
4525               // offsets into valid Script object pointers
4526               LoadScripts();
4527    
4528               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4529               if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4530               const int slotCount = pScriptRefs->size();
4531               const int headerSize = 3 * sizeof(uint32_t);
4532               const int slotSize  = 2 * sizeof(uint32_t);
4533               const int totalChunkSize = headerSize + slotCount * slotSize;
4534               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4535               if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize);
4536               else ckSCSL->Resize(totalChunkSize);
4537               uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();
4538               int pos = 0;
4539               store32(&pData[pos], headerSize);
4540               pos += sizeof(uint32_t);
4541               store32(&pData[pos], slotCount);
4542               pos += sizeof(uint32_t);
4543               store32(&pData[pos], slotSize);
4544               pos += sizeof(uint32_t);
4545               for (int i = 0; i < slotCount; ++i) {
4546                   // arbitrary value, the actual file offset will be updated in
4547                   // UpdateScriptFileOffsets() after the file has been resized
4548                   int bogusFileOffset = 0;
4549                   store32(&pData[pos], bogusFileOffset);
4550                   pos += sizeof(uint32_t);
4551                   store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
4552                   pos += sizeof(uint32_t);
4553               }
4554           } else {
4555               // no script slots, so get rid of any LS custom RIFF chunks (if any)
4556               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4557               if (lst3LS) pCkInstrument->DeleteSubChunk(lst3LS);
4558           }
4559        }
4560    
4561        void Instrument::UpdateScriptFileOffsets() {
4562           // own gig format extensions
4563           if (pScriptRefs && pScriptRefs->size() > 0) {
4564               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4565               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4566               const int slotCount = pScriptRefs->size();
4567               const int headerSize = 3 * sizeof(uint32_t);
4568               ckSCSL->SetPos(headerSize);
4569               for (int i = 0; i < slotCount; ++i) {
4570                   uint32_t fileOffset =
4571                        (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4572                        (*pScriptRefs)[i].script->pChunk->GetPos() -
4573                        CHUNK_HEADER_SIZE;
4574                   ckSCSL->WriteUint32(&fileOffset);
4575                   // jump over flags entry (containing the bypass flag)
4576                   ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos);
4577               }
4578           }        
4579      }      }
4580    
4581      /**      /**
# Line 4307  namespace { Line 4707  namespace {
4707          pMidiRules[i] = 0;          pMidiRules[i] = 0;
4708      }      }
4709    
4710        void Instrument::LoadScripts() {
4711            if (pScriptRefs) return;
4712            pScriptRefs = new std::vector<_ScriptPooolRef>;
4713            if (scriptPoolFileOffsets.empty()) return;
4714            File* pFile = (File*) GetParent();
4715            for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4716                uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset;
4717                for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4718                    ScriptGroup* group = pFile->GetScriptGroup(i);
4719                    for (uint s = 0; group->GetScript(s); ++s) {
4720                        Script* script = group->GetScript(s);
4721                        if (script->pChunk) {
4722                            uint32_t offset = script->pChunk->GetFilePos() -
4723                                              script->pChunk->GetPos() -
4724                                              CHUNK_HEADER_SIZE;
4725                            if (offset == soughtOffset)
4726                            {
4727                                _ScriptPooolRef ref;
4728                                ref.script = script;
4729                                ref.bypass = scriptPoolFileOffsets[k].bypass;
4730                                pScriptRefs->push_back(ref);
4731                                break;
4732                            }
4733                        }
4734                    }
4735                }
4736            }
4737            // we don't need that anymore
4738            scriptPoolFileOffsets.clear();
4739        }
4740    
4741        /** @brief Get instrument script (gig format extension).
4742         *
4743         * Returns the real-time instrument script of instrument script slot
4744         * @a index.
4745         *
4746         * @note This is an own format extension which did not exist i.e. in the
4747         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4748         * gigedit.
4749         *
4750         * @param index - instrument script slot index
4751         * @returns script or NULL if index is out of bounds
4752         */
4753        Script* Instrument::GetScriptOfSlot(uint index) {
4754            LoadScripts();
4755            if (index >= pScriptRefs->size()) return NULL;
4756            return pScriptRefs->at(index).script;
4757        }
4758    
4759        /** @brief Add new instrument script slot (gig format extension).
4760         *
4761         * Add the given real-time instrument script reference to this instrument,
4762         * which shall be executed by the sampler for for this instrument. The
4763         * script will be added to the end of the script list of this instrument.
4764         * The positions of the scripts in the Instrument's Script list are
4765         * relevant, because they define in which order they shall be executed by
4766         * the sampler. For this reason it is also legal to add the same script
4767         * twice to an instrument, for example you might have a script called
4768         * "MyFilter" which performs an event filter task, and you might have
4769         * another script called "MyNoteTrigger" which triggers new notes, then you
4770         * might for example have the following list of scripts on the instrument:
4771         *
4772         * 1. Script "MyFilter"
4773         * 2. Script "MyNoteTrigger"
4774         * 3. Script "MyFilter"
4775         *
4776         * Which would make sense, because the 2nd script launched new events, which
4777         * you might need to filter as well.
4778         *
4779         * There are two ways to disable / "bypass" scripts. You can either disable
4780         * a script locally for the respective script slot on an instrument (i.e. by
4781         * passing @c false to the 2nd argument of this method, or by calling
4782         * SetScriptBypassed()). Or you can disable a script globally for all slots
4783         * and all instruments by setting Script::Bypass.
4784         *
4785         * @note This is an own format extension which did not exist i.e. in the
4786         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4787         * gigedit.
4788         *
4789         * @param pScript - script that shall be executed for this instrument
4790         * @param bypass  - if enabled, the sampler shall skip executing this
4791         *                  script (in the respective list position)
4792         * @see SetScriptBypassed()
4793         */
4794        void Instrument::AddScriptSlot(Script* pScript, bool bypass) {
4795            LoadScripts();
4796            _ScriptPooolRef ref = { pScript, bypass };
4797            pScriptRefs->push_back(ref);
4798        }
4799    
4800        /** @brief Flip two script slots with each other (gig format extension).
4801         *
4802         * Swaps the position of the two given scripts in the Instrument's Script
4803         * list. The positions of the scripts in the Instrument's Script list are
4804         * relevant, because they define in which order they shall be executed by
4805         * the sampler.
4806         *
4807         * @note This is an own format extension which did not exist i.e. in the
4808         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4809         * gigedit.
4810         *
4811         * @param index1 - index of the first script slot to swap
4812         * @param index2 - index of the second script slot to swap
4813         */
4814        void Instrument::SwapScriptSlots(uint index1, uint index2) {
4815            LoadScripts();
4816            if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size())
4817                return;
4818            _ScriptPooolRef tmp = (*pScriptRefs)[index1];
4819            (*pScriptRefs)[index1] = (*pScriptRefs)[index2];
4820            (*pScriptRefs)[index2] = tmp;
4821        }
4822    
4823        /** @brief Remove script slot.
4824         *
4825         * Removes the script slot with the given slot index.
4826         *
4827         * @param index - index of script slot to remove
4828         */
4829        void Instrument::RemoveScriptSlot(uint index) {
4830            LoadScripts();
4831            if (index >= pScriptRefs->size()) return;
4832            pScriptRefs->erase( pScriptRefs->begin() + index );
4833        }
4834    
4835        /** @brief Remove reference to given Script (gig format extension).
4836         *
4837         * This will remove all script slots on the instrument which are referencing
4838         * the given script.
4839         *
4840         * @note This is an own format extension which did not exist i.e. in the
4841         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4842         * gigedit.
4843         *
4844         * @param pScript - script reference to remove from this instrument
4845         * @see RemoveScriptSlot()
4846         */
4847        void Instrument::RemoveScript(Script* pScript) {
4848            LoadScripts();
4849            for (int i = pScriptRefs->size() - 1; i >= 0; --i) {
4850                if ((*pScriptRefs)[i].script == pScript) {
4851                    pScriptRefs->erase( pScriptRefs->begin() + i );
4852                }
4853            }
4854        }
4855    
4856        /** @brief Instrument's amount of script slots.
4857         *
4858         * This method returns the amount of script slots this instrument currently
4859         * uses.
4860         *
4861         * A script slot is a reference of a real-time instrument script to be
4862         * executed by the sampler. The scripts will be executed by the sampler in
4863         * sequence of the slots. One (same) script may be referenced multiple
4864         * times in different slots.
4865         *
4866         * @note This is an own format extension which did not exist i.e. in the
4867         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4868         * gigedit.
4869         */
4870        uint Instrument::ScriptSlotCount() const {
4871            return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();
4872        }
4873    
4874        /** @brief Whether script execution shall be skipped.
4875         *
4876         * Defines locally for the Script reference slot in the Instrument's Script
4877         * list, whether the script shall be skipped by the sampler regarding
4878         * execution.
4879         *
4880         * It is also possible to ignore exeuction of the script globally, for all
4881         * slots and for all instruments by setting Script::Bypass.
4882         *
4883         * @note This is an own format extension which did not exist i.e. in the
4884         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4885         * gigedit.
4886         *
4887         * @param index - index of the script slot on this instrument
4888         * @see Script::Bypass
4889         */
4890        bool Instrument::IsScriptSlotBypassed(uint index) {
4891            if (index >= ScriptSlotCount()) return false;
4892            return pScriptRefs ? pScriptRefs->at(index).bypass
4893                               : scriptPoolFileOffsets.at(index).bypass;
4894            
4895        }
4896    
4897        /** @brief Defines whether execution shall be skipped.
4898         *
4899         * You can call this method to define locally whether or whether not the
4900         * given script slot shall be executed by the sampler.
4901         *
4902         * @note This is an own format extension which did not exist i.e. in the
4903         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4904         * gigedit.
4905         *
4906         * @param index - script slot index on this instrument
4907         * @param bBypass - if true, the script slot will be skipped by the sampler
4908         * @see Script::Bypass
4909         */
4910        void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) {
4911            if (index >= ScriptSlotCount()) return;
4912            if (pScriptRefs)
4913                pScriptRefs->at(index).bypass = bBypass;
4914            else
4915                scriptPoolFileOffsets.at(index).bypass = bBypass;
4916        }
4917    
4918      /**      /**
4919       * 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
4920       * and assign it to this object.       * and assign it to this object.
# Line 4340  namespace { Line 4948  namespace {
4948          PitchbendRange = orig->PitchbendRange;          PitchbendRange = orig->PitchbendRange;
4949          PianoReleaseMode = orig->PianoReleaseMode;          PianoReleaseMode = orig->PianoReleaseMode;
4950          DimensionKeyRange = orig->DimensionKeyRange;          DimensionKeyRange = orig->DimensionKeyRange;
4951            scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
4952            pScriptRefs = orig->pScriptRefs;
4953                    
4954          // free old midi rules          // free old midi rules
4955          for (int i = 0 ; pMidiRules[i] ; i++) {          for (int i = 0 ; pMidiRules[i] ; i++) {
# Line 4525  namespace { Line 5135  namespace {
5135          bAutoLoad = true;          bAutoLoad = true;
5136          *pVersion = VERSION_3;          *pVersion = VERSION_3;
5137          pGroups = NULL;          pGroups = NULL;
5138            pScriptGroups = NULL;
5139          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5140          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
5141    
# Line 4540  namespace { Line 5151  namespace {
5151      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
5152          bAutoLoad = true;          bAutoLoad = true;
5153          pGroups = NULL;          pGroups = NULL;
5154            pScriptGroups = NULL;
5155          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5156      }      }
5157    
# Line 4553  namespace { Line 5165  namespace {
5165              }              }
5166              delete pGroups;              delete pGroups;
5167          }          }
5168            if (pScriptGroups) {
5169                std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin();
5170                std::list<ScriptGroup*>::iterator end  = pScriptGroups->end();
5171                while (iter != end) {
5172                    delete *iter;
5173                    ++iter;
5174                }
5175                delete pScriptGroups;
5176            }
5177      }      }
5178    
5179      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 5065  namespace { Line 5686  namespace {
5686          }          }
5687      }      }
5688    
5689        /** @brief Get instrument script group (by index).
5690         *
5691         * Returns the real-time instrument script group with the given index.
5692         *
5693         * @param index - number of the sought group (0..n)
5694         * @returns sought script group or NULL if there's no such group
5695         */
5696        ScriptGroup* File::GetScriptGroup(uint index) {
5697            if (!pScriptGroups) LoadScriptGroups();
5698            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5699            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5700                if (i == index) return *it;
5701            return NULL;
5702        }
5703    
5704        /** @brief Get instrument script group (by name).
5705         *
5706         * Returns the first real-time instrument script group found with the given
5707         * group name. Note that group names may not necessarily be unique.
5708         *
5709         * @param name - name of the sought script group
5710         * @returns sought script group or NULL if there's no such group
5711         */
5712        ScriptGroup* File::GetScriptGroup(const String& name) {
5713            if (!pScriptGroups) LoadScriptGroups();
5714            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5715            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5716                if ((*it)->Name == name) return *it;
5717            return NULL;
5718        }
5719    
5720        /** @brief Add new instrument script group.
5721         *
5722         * Adds a new, empty real-time instrument script group to the file.
5723         *
5724         * You have to call Save() to make this persistent to the file.
5725         *
5726         * @return new empty script group
5727         */
5728        ScriptGroup* File::AddScriptGroup() {
5729            if (!pScriptGroups) LoadScriptGroups();
5730            ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL);
5731            pScriptGroups->push_back(pScriptGroup);
5732            return pScriptGroup;
5733        }
5734    
5735        /** @brief Delete an instrument script group.
5736         *
5737         * This will delete the given real-time instrument script group and all its
5738         * instrument scripts it contains. References inside instruments that are
5739         * using the deleted scripts will be removed from the respective instruments
5740         * accordingly.
5741         *
5742         * You have to call Save() to make this persistent to the file.
5743         *
5744         * @param pScriptGroup - script group to delete
5745         * @throws gig::Exception if given script group could not be found
5746         */
5747        void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) {
5748            if (!pScriptGroups) LoadScriptGroups();
5749            std::list<ScriptGroup*>::iterator iter =
5750                find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup);
5751            if (iter == pScriptGroups->end())
5752                throw gig::Exception("Could not delete script group, could not find given script group");
5753            pScriptGroups->erase(iter);
5754            for (int i = 0; pScriptGroup->GetScript(i); ++i)
5755                pScriptGroup->DeleteScript(pScriptGroup->GetScript(i));
5756            if (pScriptGroup->pList)
5757                pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList);
5758            delete pScriptGroup;
5759        }
5760    
5761        void File::LoadScriptGroups() {
5762            if (pScriptGroups) return;
5763            pScriptGroups = new std::list<ScriptGroup*>;
5764            RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS);
5765            if (lstLS) {
5766                for (RIFF::List* lst = lstLS->GetFirstSubList(); lst;
5767                     lst = lstLS->GetNextSubList())
5768                {
5769                    if (lst->GetListType() == LIST_TYPE_RTIS) {
5770                        pScriptGroups->push_back(new ScriptGroup(this, lst));
5771                    }
5772                }
5773            }
5774        }
5775    
5776      /**      /**
5777       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
5778       * 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 5080  namespace { Line 5788  namespace {
5788    
5789          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;
5790    
5791            // update own gig format extension chunks
5792            // (not part of the GigaStudio 4 format)
5793            //
5794            // This must be performed before writing the chunks for instruments,
5795            // because the instruments' script slots will write the file offsets
5796            // of the respective instrument script chunk as reference.
5797            if (pScriptGroups) {
5798                RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
5799                if (pScriptGroups->empty()) {
5800                    if (lst3LS) pRIFF->DeleteSubChunk(lst3LS);
5801                } else {
5802                    if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
5803    
5804                    // Update instrument script (group) chunks.
5805    
5806                    for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5807                         it != pScriptGroups->end(); ++it)
5808                    {
5809                        (*it)->UpdateChunks();
5810                    }
5811                }
5812            }
5813    
5814          // first update base class's chunks          // first update base class's chunks
5815          DLS::File::UpdateChunks();          DLS::File::UpdateChunks();
5816    
# Line 5250  namespace { Line 5981  namespace {
5981              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
5982          }          }
5983      }      }
5984        
5985        void File::UpdateFileOffsets() {
5986            DLS::File::UpdateFileOffsets();
5987    
5988            for (Instrument* instrument = GetFirstInstrument(); instrument;
5989                 instrument = GetNextInstrument())
5990            {
5991                instrument->UpdateScriptFileOffsets();
5992            }
5993        }
5994    
5995      /**      /**
5996       * Enable / disable automatic loading. By default this properyt is       * Enable / disable automatic loading. By default this properyt is

Legend:
Removed from v.2555  
changed lines
  Added in v.2667

  ViewVC Help
Powered by ViewVC