/[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 2584 by schoenebeck, Sat May 31 20:54:39 2014 UTC revision 2639 by schoenebeck, Mon Jun 16 13:22:50 2014 UTC
# Line 3652  namespace { Line 3652  namespace {
3652          UpdateVelocityTable();          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            def->split_type = __resolveSplitType(newType);
3677        }
3678    
3679      DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {      DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) {
3680          uint8_t bits[8] = {};          uint8_t bits[8] = {};
3681          for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();          for (std::map<dimension_t,int>::const_iterator it = DimCase.begin();
# Line 3759  namespace { Line 3783  namespace {
3783          return dimreg;          return dimreg;
3784      }      }
3785    
3786        int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) {
3787            uint8_t bits;
3788            int veldim = -1;
3789            int velbitpos;
3790            int bitpos = 0;
3791            int dimregidx = 0;
3792            for (uint i = 0; i < Dimensions; i++) {
3793                if (pDimensionDefinitions[i].dimension == dimension_velocity) {
3794                    // the velocity dimension must be handled after the other dimensions
3795                    veldim = i;
3796                    velbitpos = bitpos;
3797                } else {
3798                    switch (pDimensionDefinitions[i].split_type) {
3799                        case split_type_normal:
3800                            if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
3801                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
3802                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
3803                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
3804                                }
3805                            } else {
3806                                // gig2: evenly sized zones
3807                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
3808                            }
3809                            break;
3810                        case split_type_bit: // the value is already the sought dimension bit number
3811                            const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
3812                            bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed
3813                            break;
3814                    }
3815                    dimregidx |= bits << bitpos;
3816                }
3817                bitpos += pDimensionDefinitions[i].bits;
3818            }
3819            dimregidx &= 255;
3820            DimensionRegion* dimreg = pDimensionRegions[dimregidx];
3821            if (!dimreg) return -1;
3822            if (veldim != -1) {
3823                // (dimreg is now the dimension region for the lowest velocity)
3824                if (dimreg->VelocityTable) // custom defined zone ranges
3825                    bits = dimreg->VelocityTable[DimValues[veldim] & 127];
3826                else // normal split type
3827                    bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size);
3828    
3829                const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1;
3830                dimregidx |= (bits & limiter_mask) << velbitpos;
3831                dimregidx &= 255;
3832            }
3833            return dimregidx;
3834        }
3835    
3836      /**      /**
3837       * Returns the appropriate DimensionRegion for the given dimension bit       * Returns the appropriate DimensionRegion for the given dimension bit
3838       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>       * numbers (zone index). You usually use <i>GetDimensionRegionByValue</i>
# Line 4044  namespace { Line 4118  namespace {
4118              for (int i = 0; i < nameSize; ++i)              for (int i = 0; i < nameSize; ++i)
4119                  Name[i] = ckScri->ReadUint8();                  Name[i] = ckScri->ReadUint8();
4120              // to handle potential future extensions of the header              // to handle potential future extensions of the header
4121              ckScri->SetPos(headerSize - 6*sizeof(int32_t) + nameSize, RIFF::stream_curpos);              ckScri->SetPos(sizeof(int32_t) + headerSize);
4122              // read actual script data              // read actual script data
4123              uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();              uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();
4124              data.resize(scriptSize);              data.resize(scriptSize);
# Line 4129  namespace { Line 4203  namespace {
4203          this->pGroup = pGroup;          this->pGroup = pGroup;
4204      }      }
4205    
4206        /**
4207         * Returns the script group this script currently belongs to. Each script
4208         * is a member of exactly one ScriptGroup.
4209         *
4210         * @returns current script group
4211         */
4212        ScriptGroup* Script::GetGroup() const {
4213            return pGroup;
4214        }
4215    
4216      void Script::RemoveAllScriptReferences() {      void Script::RemoveAllScriptReferences() {
4217          File* pFile = pGroup->pFile;          File* pFile = pGroup->pFile;
4218          for (int i = 0; pFile->GetInstrument(i); ++i) {          for (int i = 0; pFile->GetInstrument(i); ++i) {
# Line 4338  namespace { Line 4422  namespace {
4422          if (lst3LS) {          if (lst3LS) {
4423              RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);              RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4424              if (ckSCSL) {              if (ckSCSL) {
4425                  int slotCount = ckSCSL->ReadUint32();                  int headerSize = ckSCSL->ReadUint32();
4426                  int slotSize  = ckSCSL->ReadUint32();                  int slotCount  = ckSCSL->ReadUint32();
4427                  int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future extensions                  if (slotCount) {
4428                  for (int i = 0; i < slotCount; ++i) {                      int slotSize  = ckSCSL->ReadUint32();
4429                      _ScriptPooolEntry e;                      ckSCSL->SetPos(headerSize); // in case of future header extensions
4430                      e.fileOffset = ckSCSL->ReadUint32();                      int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions
4431                      e.bypass     = ckSCSL->ReadUint32() & 1;                      for (int i = 0; i < slotCount; ++i) {
4432                      if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions                          _ScriptPooolEntry e;
4433                      scriptPoolFileOffsets.push_back(e);                          e.fileOffset = ckSCSL->ReadUint32();
4434                            e.bypass     = ckSCSL->ReadUint32() & 1;
4435                            if (unknownSpace) ckSCSL->SetPos(unknownSpace, RIFF::stream_curpos); // in case of future extensions
4436                            scriptPoolFileOffsets.push_back(e);
4437                        }
4438                  }                  }
4439              }              }
4440          }          }
# Line 4432  namespace { Line 4520  namespace {
4520         if (pScriptRefs) {         if (pScriptRefs) {
4521             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4522             if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);             if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4523             const int totalSize = pScriptRefs->size() * 2*sizeof(uint32_t);             const int slotCount = pScriptRefs->size();
4524               const int headerSize = 3 * sizeof(uint32_t);
4525               const int slotSize  = 2 * sizeof(uint32_t);
4526               const int totalChunkSize = headerSize + slotCount * slotSize;
4527             RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);             RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4528             if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalSize);             if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize);
4529             else ckSCSL->Resize(totalSize);             else ckSCSL->Resize(totalChunkSize);
4530             uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();             uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData();
4531             for (int i = 0, pos = 0; i < pScriptRefs->size(); ++i) {             int pos = 0;
4532                 int fileOffset =             store32(&pData[pos], headerSize);
4533                      (*pScriptRefs)[i].script->pChunk->GetFilePos() -             pos += sizeof(uint32_t);
4534                      (*pScriptRefs)[i].script->pChunk->GetPos() -             store32(&pData[pos], slotCount);
4535                      CHUNK_HEADER_SIZE;             pos += sizeof(uint32_t);
4536                 store32(&pData[pos], fileOffset);             store32(&pData[pos], slotSize);
4537               pos += sizeof(uint32_t);
4538               for (int i = 0; i < slotCount; ++i) {
4539                   // arbitrary value, the actual file offset will be updated in
4540                   // UpdateScriptFileOffsets() after the file has been resized
4541                   int bogusFileOffset = 0;
4542                   store32(&pData[pos], bogusFileOffset);
4543                 pos += sizeof(uint32_t);                 pos += sizeof(uint32_t);
4544                 store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);                 store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
4545                 pos += sizeof(uint32_t);                 pos += sizeof(uint32_t);
# Line 4450  namespace { Line 4547  namespace {
4547         }         }
4548      }      }
4549    
4550        void Instrument::UpdateScriptFileOffsets() {
4551           // own gig format extensions
4552           if (pScriptRefs) {
4553               RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4554               RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4555               const int slotCount = pScriptRefs->size();
4556               const int headerSize = 3 * sizeof(uint32_t);
4557               ckSCSL->SetPos(headerSize);
4558               for (int i = 0; i < slotCount; ++i) {
4559                   uint32_t fileOffset =
4560                        (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4561                        (*pScriptRefs)[i].script->pChunk->GetPos() -
4562                        CHUNK_HEADER_SIZE;
4563                   ckSCSL->WriteUint32(&fileOffset);
4564                   // jump over flags entry (containing the bypass flag)
4565                   ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos);
4566               }
4567           }        
4568        }
4569    
4570      /**      /**
4571       * Returns the appropriate Region for a triggered note.       * Returns the appropriate Region for a triggered note.
4572       *       *
# Line 4585  namespace { Line 4702  namespace {
4702          if (scriptPoolFileOffsets.empty()) return;          if (scriptPoolFileOffsets.empty()) return;
4703          File* pFile = (File*) GetParent();          File* pFile = (File*) GetParent();
4704          for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {          for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) {
4705              uint32_t offset = scriptPoolFileOffsets[k].fileOffset;              uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset;
4706              for (uint i = 0; pFile->GetScriptGroup(i); ++i) {              for (uint i = 0; pFile->GetScriptGroup(i); ++i) {
4707                  ScriptGroup* group = pFile->GetScriptGroup(i);                  ScriptGroup* group = pFile->GetScriptGroup(i);
4708                  for (uint s = 0; group->GetScript(s); ++s) {                  for (uint s = 0; group->GetScript(s); ++s) {
4709                      Script* script = group->GetScript(s);                      Script* script = group->GetScript(s);
4710                      if (script->pChunk) {                      if (script->pChunk) {
4711                          script->pChunk->SetPos(0);                          uint32_t offset = script->pChunk->GetFilePos() -
4712                          if (script->pChunk->GetFilePos() -                                            script->pChunk->GetPos() -
4713                              script->pChunk->GetPos() -                                            CHUNK_HEADER_SIZE;
4714                              CHUNK_HEADER_SIZE == offset)                          if (offset == soughtOffset)
4715                          {                          {
4716                              _ScriptPooolRef ref;                              _ScriptPooolRef ref;
4717                              ref.script = script;                              ref.script = script;
# Line 4610  namespace { Line 4727  namespace {
4727          scriptPoolFileOffsets.clear();          scriptPoolFileOffsets.clear();
4728      }      }
4729    
4730      /** @brief Add new instrument script slot (gig format extension)      /** @brief Get instrument script (gig format extension).
4731         *
4732         * Returns the real-time instrument script of instrument script slot
4733         * @a index.
4734         *
4735         * @note This is an own format extension which did not exist i.e. in the
4736         * GigaStudio 4 software. It will currently only work with LinuxSampler and
4737         * gigedit.
4738         *
4739         * @param index - instrument script slot index
4740         * @returns script or NULL if index is out of bounds
4741         */
4742        Script* Instrument::GetScriptOfSlot(uint index) {
4743            LoadScripts();
4744            if (index >= pScriptRefs->size()) return NULL;
4745            return pScriptRefs->at(index).script;
4746        }
4747    
4748        /** @brief Add new instrument script slot (gig format extension).
4749       *       *
4750       * Add the given real-time instrument script reference to this instrument,       * Add the given real-time instrument script reference to this instrument,
4751       * which shall be executed by the sampler for for this instrument. The       * which shall be executed by the sampler for for this instrument. The
# Line 5835  namespace { Line 5970  namespace {
5970              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
5971          }          }
5972      }      }
5973        
5974        void File::UpdateFileOffsets() {
5975            DLS::File::UpdateFileOffsets();
5976    
5977            for (Instrument* instrument = GetFirstInstrument(); instrument;
5978                 instrument = GetNextInstrument())
5979            {
5980                instrument->UpdateScriptFileOffsets();
5981            }
5982        }
5983    
5984      /**      /**
5985       * Enable / disable automatic loading. By default this properyt is       * Enable / disable automatic loading. By default this properyt is

Legend:
Removed from v.2584  
changed lines
  Added in v.2639

  ViewVC Help
Powered by ViewVC