/[svn]/libgig/trunk/src/gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/gig.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2557 by schoenebeck, Sat May 17 23:31:20 2014 UTC revision 2602 by schoenebeck, Sat Jun 7 22:28:04 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 3743  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 4023  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 4045  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 4105  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 slotCount = ckSCSL->ReadUint32();
4402                    int slotSize  = ckSCSL->ReadUint32();
4403                    int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future extensions
4404                    for (int i = 0; i < slotCount; ++i) {
4405                        _ScriptPooolEntry e;
4406                        e.fileOffset = ckSCSL->ReadUint32();
4407                        e.bypass     = ckSCSL->ReadUint32() & 1;
4408                        if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions
4409                        scriptPoolFileOffsets.push_back(e);
4410                    }
4411                }
4412            }
4413    
4414          __notify_progress(pProgress, 1.0f); // notify done          __notify_progress(pProgress, 1.0f); // notify done
4415      }      }
4416    
# Line 4125  namespace { Line 4431  namespace {
4431              delete pMidiRules[i];              delete pMidiRules[i];
4432          }          }
4433          delete[] pMidiRules;          delete[] pMidiRules;
4434            if (pScriptRefs) delete pScriptRefs;
4435      }      }
4436    
4437      /**      /**
# Line 4180  namespace { Line 4487  namespace {
4487                  pMidiRules[i]->UpdateChunks(pData);                  pMidiRules[i]->UpdateChunks(pData);
4488              }              }
4489          }          }
4490    
4491            // own gig format extensions
4492           if (pScriptRefs) {
4493               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4494               if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4495               const int totalSize = pScriptRefs->size() * 2*sizeof(uint32_t);
4496               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4497               if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalSize);
4498               else ckSCSL->Resize(totalSize);
4499               uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();
4500               for (int i = 0, pos = 0; i < pScriptRefs->size(); ++i) {
4501                   int fileOffset =
4502                        (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4503                        (*pScriptRefs)[i].script->pChunk->GetPos() -
4504                        CHUNK_HEADER_SIZE;
4505                   store32(&pData[pos], fileOffset);
4506                   pos += sizeof(uint32_t);
4507                   store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
4508                   pos += sizeof(uint32_t);
4509               }
4510           }
4511      }      }
4512    
4513      /**      /**
# Line 4311  namespace { Line 4639  namespace {
4639          pMidiRules[i] = 0;          pMidiRules[i] = 0;
4640      }      }
4641    
4642        void Instrument::LoadScripts() {
4643            if (pScriptRefs) return;
4644            pScriptRefs = new std::vector<_ScriptPooolRef>;
4645            if (scriptPoolFileOffsets.empty()) return;
4646            File* pFile = (File*) GetParent();
4647            for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4648                uint32_t offset = scriptPoolFileOffsets[k].fileOffset;
4649                for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4650                    ScriptGroup* group = pFile->GetScriptGroup(i);
4651                    for (uint s = 0; group->GetScript(s); ++s) {
4652                        Script* script = group->GetScript(s);
4653                        if (script->pChunk) {
4654                            script->pChunk->SetPos(0);
4655                            if (script->pChunk->GetFilePos() -
4656                                script->pChunk->GetPos() -
4657                                CHUNK_HEADER_SIZE == offset)
4658                            {
4659                                _ScriptPooolRef ref;
4660                                ref.script = script;
4661                                ref.bypass = scriptPoolFileOffsets[k].bypass;
4662                                pScriptRefs->push_back(ref);
4663                                break;
4664                            }
4665                        }
4666                    }
4667                }
4668            }
4669            // we don't need that anymore
4670            scriptPoolFileOffsets.clear();
4671        }
4672    
4673        /** @brief Get instrument script (gig format extension).
4674         *
4675         * Returns the real-time instrument script of instrument script slot
4676         * @a index.
4677         *
4678         * @note This is an own format extension which did not exist i.e. in the
4679         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4680         * gigedit.
4681         *
4682         * @param index - instrument script slot index
4683         * @returns script or NULL if index is out of bounds
4684         */
4685        Script* Instrument::GetScriptOfSlot(uint index) {
4686            LoadScripts();
4687            if (index >= pScriptRefs->size()) return NULL;
4688            return pScriptRefs->at(index).script;
4689        }
4690    
4691        /** @brief Add new instrument script slot (gig format extension).
4692         *
4693         * Add the given real-time instrument script reference to this instrument,
4694         * which shall be executed by the sampler for for this instrument. The
4695         * script will be added to the end of the script list of this instrument.
4696         * The positions of the scripts in the Instrument's Script list are
4697         * relevant, because they define in which order they shall be executed by
4698         * the sampler. For this reason it is also legal to add the same script
4699         * twice to an instrument, for example you might have a script called
4700         * "MyFilter" which performs an event filter task, and you might have
4701         * another script called "MyNoteTrigger" which triggers new notes, then you
4702         * might for example have the following list of scripts on the instrument:
4703         *
4704         * 1. Script "MyFilter"
4705         * 2. Script "MyNoteTrigger"
4706         * 3. Script "MyFilter"
4707         *
4708         * Which would make sense, because the 2nd script launched new events, which
4709         * you might need to filter as well.
4710         *
4711         * There are two ways to disable / "bypass" scripts. You can either disable
4712         * a script locally for the respective script slot on an instrument (i.e. by
4713         * passing @c false to the 2nd argument of this method, or by calling
4714         * SetScriptBypassed()). Or you can disable a script globally for all slots
4715         * and all instruments by setting Script::Bypass.
4716         *
4717         * @note This is an own format extension which did not exist i.e. in the
4718         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4719         * gigedit.
4720         *
4721         * @param pScript - script that shall be executed for this instrument
4722         * @param bypass  - if enabled, the sampler shall skip executing this
4723         *                  script (in the respective list position)
4724         * @see SetScriptBypassed()
4725         */
4726        void Instrument::AddScriptSlot(Script* pScript, bool bypass) {
4727            LoadScripts();
4728            _ScriptPooolRef ref = { pScript, bypass };
4729            pScriptRefs->push_back(ref);
4730        }
4731    
4732        /** @brief Flip two script slots with each other (gig format extension).
4733         *
4734         * Swaps the position of the two given scripts in the Instrument's Script
4735         * list. The positions of the scripts in the Instrument's Script list are
4736         * relevant, because they define in which order they shall be executed by
4737         * the sampler.
4738         *
4739         * @note This is an own format extension which did not exist i.e. in the
4740         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4741         * gigedit.
4742         *
4743         * @param index1 - index of the first script slot to swap
4744         * @param index2 - index of the second script slot to swap
4745         */
4746        void Instrument::SwapScriptSlots(uint index1, uint index2) {
4747            LoadScripts();
4748            if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size())
4749                return;
4750            _ScriptPooolRef tmp = (*pScriptRefs)[index1];
4751            (*pScriptRefs)[index1] = (*pScriptRefs)[index2];
4752            (*pScriptRefs)[index2] = tmp;
4753        }
4754    
4755        /** @brief Remove script slot.
4756         *
4757         * Removes the script slot with the given slot index.
4758         *
4759         * @param index - index of script slot to remove
4760         */
4761        void Instrument::RemoveScriptSlot(uint index) {
4762            LoadScripts();
4763            if (index >= pScriptRefs->size()) return;
4764            pScriptRefs->erase( pScriptRefs->begin() + index );
4765        }
4766    
4767        /** @brief Remove reference to given Script (gig format extension).
4768         *
4769         * This will remove all script slots on the instrument which are referencing
4770         * the given script.
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 pScript - script reference to remove from this instrument
4777         * @see RemoveScriptSlot()
4778         */
4779        void Instrument::RemoveScript(Script* pScript) {
4780            LoadScripts();
4781            for (int i = pScriptRefs->size() - 1; i >= 0; --i) {
4782                if ((*pScriptRefs)[i].script == pScript) {
4783                    pScriptRefs->erase( pScriptRefs->begin() + i );
4784                }
4785            }
4786        }
4787    
4788        /** @brief Instrument's amount of script slots.
4789         *
4790         * This method returns the amount of script slots this instrument currently
4791         * uses.
4792         *
4793         * A script slot is a reference of a real-time instrument script to be
4794         * executed by the sampler. The scripts will be executed by the sampler in
4795         * sequence of the slots. One (same) script may be referenced multiple
4796         * times in different slots.
4797         *
4798         * @note This is an own format extension which did not exist i.e. in the
4799         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4800         * gigedit.
4801         */
4802        uint Instrument::ScriptSlotCount() const {
4803            return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();
4804        }
4805    
4806        /** @brief Whether script execution shall be skipped.
4807         *
4808         * Defines locally for the Script reference slot in the Instrument's Script
4809         * list, whether the script shall be skipped by the sampler regarding
4810         * execution.
4811         *
4812         * It is also possible to ignore exeuction of the script globally, for all
4813         * slots and for all instruments by setting Script::Bypass.
4814         *
4815         * @note This is an own format extension which did not exist i.e. in the
4816         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4817         * gigedit.
4818         *
4819         * @param index - index of the script slot on this instrument
4820         * @see Script::Bypass
4821         */
4822        bool Instrument::IsScriptSlotBypassed(uint index) {
4823            if (index >= ScriptSlotCount()) return false;
4824            return pScriptRefs ? pScriptRefs->at(index).bypass
4825                               : scriptPoolFileOffsets.at(index).bypass;
4826            
4827        }
4828    
4829        /** @brief Defines whether execution shall be skipped.
4830         *
4831         * You can call this method to define locally whether or whether not the
4832         * given script slot shall be executed by the sampler.
4833         *
4834         * @note This is an own format extension which did not exist i.e. in the
4835         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4836         * gigedit.
4837         *
4838         * @param index - script slot index on this instrument
4839         * @param bBypass - if true, the script slot will be skipped by the sampler
4840         * @see Script::Bypass
4841         */
4842        void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) {
4843            if (index >= ScriptSlotCount()) return;
4844            if (pScriptRefs)
4845                pScriptRefs->at(index).bypass = bBypass;
4846            else
4847                scriptPoolFileOffsets.at(index).bypass = bBypass;
4848        }
4849    
4850      /**      /**
4851       * 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
4852       * and assign it to this object.       * and assign it to this object.
# Line 4344  namespace { Line 4880  namespace {
4880          PitchbendRange = orig->PitchbendRange;          PitchbendRange = orig->PitchbendRange;
4881          PianoReleaseMode = orig->PianoReleaseMode;          PianoReleaseMode = orig->PianoReleaseMode;
4882          DimensionKeyRange = orig->DimensionKeyRange;          DimensionKeyRange = orig->DimensionKeyRange;
4883            scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
4884            pScriptRefs = orig->pScriptRefs;
4885                    
4886          // free old midi rules          // free old midi rules
4887          for (int i = 0 ; pMidiRules[i] ; i++) {          for (int i = 0 ; pMidiRules[i] ; i++) {
# Line 4529  namespace { Line 5067  namespace {
5067          bAutoLoad = true;          bAutoLoad = true;
5068          *pVersion = VERSION_3;          *pVersion = VERSION_3;
5069          pGroups = NULL;          pGroups = NULL;
5070            pScriptGroups = NULL;
5071          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5072          pInfo->ArchivalLocation = String(256, ' ');          pInfo->ArchivalLocation = String(256, ' ');
5073    
# Line 4544  namespace { Line 5083  namespace {
5083      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
5084          bAutoLoad = true;          bAutoLoad = true;
5085          pGroups = NULL;          pGroups = NULL;
5086            pScriptGroups = NULL;
5087          pInfo->SetFixedStringLengths(_FileFixedStringLengths);          pInfo->SetFixedStringLengths(_FileFixedStringLengths);
5088      }      }
5089    
# Line 4557  namespace { Line 5097  namespace {
5097              }              }
5098              delete pGroups;              delete pGroups;
5099          }          }
5100            if (pScriptGroups) {
5101                std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin();
5102                std::list<ScriptGroup*>::iterator end  = pScriptGroups->end();
5103                while (iter != end) {
5104                    delete *iter;
5105                    ++iter;
5106                }
5107                delete pScriptGroups;
5108            }
5109      }      }
5110    
5111      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 5069  namespace { Line 5618  namespace {
5618          }          }
5619      }      }
5620    
5621        /** @brief Get instrument script group (by index).
5622         *
5623         * Returns the real-time instrument script group with the given index.
5624         *
5625         * @param index - number of the sought group (0..n)
5626         * @returns sought script group or NULL if there's no such group
5627         */
5628        ScriptGroup* File::GetScriptGroup(uint index) {
5629            if (!pScriptGroups) LoadScriptGroups();
5630            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5631            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5632                if (i == index) return *it;
5633            return NULL;
5634        }
5635    
5636        /** @brief Get instrument script group (by name).
5637         *
5638         * Returns the first real-time instrument script group found with the given
5639         * group name. Note that group names may not necessarily be unique.
5640         *
5641         * @param name - name of the sought script group
5642         * @returns sought script group or NULL if there's no such group
5643         */
5644        ScriptGroup* File::GetScriptGroup(const String& name) {
5645            if (!pScriptGroups) LoadScriptGroups();
5646            std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5647            for (uint i = 0; it != pScriptGroups->end(); ++i, ++it)
5648                if ((*it)->Name == name) return *it;
5649            return NULL;
5650        }
5651    
5652        /** @brief Add new instrument script group.
5653         *
5654         * Adds a new, empty real-time instrument script group to the file.
5655         *
5656         * You have to call Save() to make this persistent to the file.
5657         *
5658         * @return new empty script group
5659         */
5660        ScriptGroup* File::AddScriptGroup() {
5661            if (!pScriptGroups) LoadScriptGroups();
5662            ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL);
5663            pScriptGroups->push_back(pScriptGroup);
5664            return pScriptGroup;
5665        }
5666    
5667        /** @brief Delete an instrument script group.
5668         *
5669         * This will delete the given real-time instrument script group and all its
5670         * instrument scripts it contains. References inside instruments that are
5671         * using the deleted scripts will be removed from the respective instruments
5672         * accordingly.
5673         *
5674         * You have to call Save() to make this persistent to the file.
5675         *
5676         * @param pScriptGroup - script group to delete
5677         * @throws gig::Exception if given script group could not be found
5678         */
5679        void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) {
5680            if (!pScriptGroups) LoadScriptGroups();
5681            std::list<ScriptGroup*>::iterator iter =
5682                find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup);
5683            if (iter == pScriptGroups->end())
5684                throw gig::Exception("Could not delete script group, could not find given script group");
5685            pScriptGroups->erase(iter);
5686            for (int i = 0; pScriptGroup->GetScript(i); ++i)
5687                pScriptGroup->DeleteScript(pScriptGroup->GetScript(i));
5688            if (pScriptGroup->pList)
5689                pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList);
5690            delete pScriptGroup;
5691        }
5692    
5693        void File::LoadScriptGroups() {
5694            if (pScriptGroups) return;
5695            pScriptGroups = new std::list<ScriptGroup*>;
5696            RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS);
5697            if (lstLS) {
5698                for (RIFF::List* lst = lstLS->GetFirstSubList(); lst;
5699                     lst = lstLS->GetNextSubList())
5700                {
5701                    if (lst->GetListType() == LIST_TYPE_RTIS) {
5702                        pScriptGroups->push_back(new ScriptGroup(this, lst));
5703                    }
5704                }
5705            }
5706        }
5707    
5708      /**      /**
5709       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
5710       * 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 5084  namespace { Line 5720  namespace {
5720    
5721          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;          b64BitWavePoolOffsets = pVersion && pVersion->major == 3;
5722    
5723            // update own gig format extension chunks
5724            // (not part of the GigaStudio 4 format)
5725            //
5726            // This must be performed before writing the chunks for instruments,
5727            // because the instruments' script slots will write the file offsets
5728            // of the respective instrument script chunk as reference.
5729            if (pScriptGroups) {
5730                RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
5731                if (pScriptGroups->empty()) {
5732                    if (lst3LS) pRIFF->DeleteSubChunk(lst3LS);
5733                } else {
5734                    if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
5735    
5736                    // Update instrument script (group) chunks.
5737    
5738                    for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin();
5739                         it != pScriptGroups->end(); ++it)
5740                    {
5741                        (*it)->UpdateChunks();
5742                    }
5743                }
5744            }
5745    
5746          // first update base class's chunks          // first update base class's chunks
5747          DLS::File::UpdateChunks();          DLS::File::UpdateChunks();
5748    

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

  ViewVC Help
Powered by ViewVC