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 |
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). |
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 |
|
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) { |
3767 |
} |
} |
3768 |
bitpos += pDimensionDefinitions[i].bits; |
bitpos += pDimensionDefinitions[i].bits; |
3769 |
} |
} |
3770 |
DimensionRegion* dimreg = pDimensionRegions[dimregidx]; |
DimensionRegion* dimreg = pDimensionRegions[dimregidx & 255]; |
3771 |
|
if (!dimreg) return NULL; |
3772 |
if (veldim != -1) { |
if (veldim != -1) { |
3773 |
// (dimreg is now the dimension region for the lowest velocity) |
// (dimreg is now the dimension region for the lowest velocity) |
3774 |
if (dimreg->VelocityTable) // custom defined zone ranges |
if (dimreg->VelocityTable) // custom defined zone ranges |
3775 |
bits = dimreg->VelocityTable[DimValues[veldim]]; |
bits = dimreg->VelocityTable[DimValues[veldim] & 127]; |
3776 |
else // normal split type |
else // normal split type |
3777 |
bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); |
bits = uint8_t((DimValues[veldim] & 127) / pDimensionDefinitions[veldim].zone_size); |
3778 |
|
|
3779 |
dimregidx |= bits << velbitpos; |
const uint8_t limiter_mask = (1 << pDimensionDefinitions[veldim].bits) - 1; |
3780 |
dimreg = pDimensionRegions[dimregidx]; |
dimregidx |= (bits & limiter_mask) << velbitpos; |
3781 |
|
dimreg = pDimensionRegions[dimregidx & 255]; |
3782 |
} |
} |
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> |
4099 |
} |
} |
4100 |
} |
} |
4101 |
|
|
4102 |
|
// *************** Script *************** |
4103 |
|
// * |
4104 |
|
|
4105 |
|
Script::Script(ScriptGroup* group, RIFF::Chunk* ckScri) { |
4106 |
|
pGroup = group; |
4107 |
|
pChunk = ckScri; |
4108 |
|
if (ckScri) { // object is loaded from file ... |
4109 |
|
// read header |
4110 |
|
uint32_t headerSize = ckScri->ReadUint32(); |
4111 |
|
Compression = (Compression_t) ckScri->ReadUint32(); |
4112 |
|
Encoding = (Encoding_t) ckScri->ReadUint32(); |
4113 |
|
Language = (Language_t) ckScri->ReadUint32(); |
4114 |
|
Bypass = (Language_t) ckScri->ReadUint32() & 1; |
4115 |
|
crc = ckScri->ReadUint32(); |
4116 |
|
uint32_t nameSize = ckScri->ReadUint32(); |
4117 |
|
Name.resize(nameSize, ' '); |
4118 |
|
for (int i = 0; i < nameSize; ++i) |
4119 |
|
Name[i] = ckScri->ReadUint8(); |
4120 |
|
// to handle potential future extensions of the header |
4121 |
|
ckScri->SetPos(sizeof(int32_t) + headerSize); |
4122 |
|
// read actual script data |
4123 |
|
uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos(); |
4124 |
|
data.resize(scriptSize); |
4125 |
|
for (int i = 0; i < scriptSize; ++i) |
4126 |
|
data[i] = ckScri->ReadUint8(); |
4127 |
|
} else { // this is a new script object, so just initialize it as such ... |
4128 |
|
Compression = COMPRESSION_NONE; |
4129 |
|
Encoding = ENCODING_ASCII; |
4130 |
|
Language = LANGUAGE_NKSP; |
4131 |
|
Bypass = false; |
4132 |
|
crc = 0; |
4133 |
|
Name = "Unnamed Script"; |
4134 |
|
} |
4135 |
|
} |
4136 |
|
|
4137 |
|
Script::~Script() { |
4138 |
|
} |
4139 |
|
|
4140 |
|
/** |
4141 |
|
* Returns the current script (i.e. as source code) in text format. |
4142 |
|
*/ |
4143 |
|
String Script::GetScriptAsText() { |
4144 |
|
String s; |
4145 |
|
s.resize(data.size(), ' '); |
4146 |
|
memcpy(&s[0], &data[0], data.size()); |
4147 |
|
return s; |
4148 |
|
} |
4149 |
|
|
4150 |
|
/** |
4151 |
|
* Replaces the current script with the new script source code text given |
4152 |
|
* by @a text. |
4153 |
|
* |
4154 |
|
* @param text - new script source code |
4155 |
|
*/ |
4156 |
|
void Script::SetScriptAsText(const String& text) { |
4157 |
|
data.resize(text.size()); |
4158 |
|
memcpy(&data[0], &text[0], text.size()); |
4159 |
|
} |
4160 |
|
|
4161 |
|
void Script::UpdateChunks() { |
4162 |
|
// recalculate CRC32 check sum |
4163 |
|
__resetCRC(crc); |
4164 |
|
__calculateCRC(&data[0], data.size(), crc); |
4165 |
|
__encodeCRC(crc); |
4166 |
|
// make sure chunk exists and has the required size |
4167 |
|
const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size(); |
4168 |
|
if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize); |
4169 |
|
else pChunk->Resize(chunkSize); |
4170 |
|
// fill the chunk data to be written to disk |
4171 |
|
uint8_t* pData = (uint8_t*) pChunk->LoadChunkData(); |
4172 |
|
int pos = 0; |
4173 |
|
store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size |
4174 |
|
pos += sizeof(int32_t); |
4175 |
|
store32(&pData[pos], Compression); |
4176 |
|
pos += sizeof(int32_t); |
4177 |
|
store32(&pData[pos], Encoding); |
4178 |
|
pos += sizeof(int32_t); |
4179 |
|
store32(&pData[pos], Language); |
4180 |
|
pos += sizeof(int32_t); |
4181 |
|
store32(&pData[pos], Bypass ? 1 : 0); |
4182 |
|
pos += sizeof(int32_t); |
4183 |
|
store32(&pData[pos], crc); |
4184 |
|
pos += sizeof(int32_t); |
4185 |
|
store32(&pData[pos], Name.size()); |
4186 |
|
pos += sizeof(int32_t); |
4187 |
|
for (int i = 0; i < Name.size(); ++i, ++pos) |
4188 |
|
pData[pos] = Name[i]; |
4189 |
|
for (int i = 0; i < data.size(); ++i, ++pos) |
4190 |
|
pData[pos] = data[i]; |
4191 |
|
} |
4192 |
|
|
4193 |
|
/** |
4194 |
|
* Move this script from its current ScriptGroup to another ScriptGroup |
4195 |
|
* given by @a pGroup. |
4196 |
|
* |
4197 |
|
* @param pGroup - script's new group |
4198 |
|
*/ |
4199 |
|
void Script::SetGroup(ScriptGroup* pGroup) { |
4200 |
|
if (this->pGroup = pGroup) return; |
4201 |
|
if (pChunk) |
4202 |
|
pChunk->GetParent()->MoveSubChunk(pChunk, pGroup->pList); |
4203 |
|
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() { |
4217 |
|
File* pFile = pGroup->pFile; |
4218 |
|
for (int i = 0; pFile->GetInstrument(i); ++i) { |
4219 |
|
Instrument* instr = pFile->GetInstrument(i); |
4220 |
|
instr->RemoveScript(this); |
4221 |
|
} |
4222 |
|
} |
4223 |
|
|
4224 |
|
// *************** ScriptGroup *************** |
4225 |
|
// * |
4226 |
|
|
4227 |
|
ScriptGroup::ScriptGroup(File* file, RIFF::List* lstRTIS) { |
4228 |
|
pFile = file; |
4229 |
|
pList = lstRTIS; |
4230 |
|
pScripts = NULL; |
4231 |
|
if (lstRTIS) { |
4232 |
|
RIFF::Chunk* ckName = lstRTIS->GetSubChunk(CHUNK_ID_LSNM); |
4233 |
|
::LoadString(ckName, Name); |
4234 |
|
} else { |
4235 |
|
Name = "Default Group"; |
4236 |
|
} |
4237 |
|
} |
4238 |
|
|
4239 |
|
ScriptGroup::~ScriptGroup() { |
4240 |
|
if (pScripts) { |
4241 |
|
std::list<Script*>::iterator iter = pScripts->begin(); |
4242 |
|
std::list<Script*>::iterator end = pScripts->end(); |
4243 |
|
while (iter != end) { |
4244 |
|
delete *iter; |
4245 |
|
++iter; |
4246 |
|
} |
4247 |
|
delete pScripts; |
4248 |
|
} |
4249 |
|
} |
4250 |
|
|
4251 |
|
void ScriptGroup::UpdateChunks() { |
4252 |
|
if (pScripts) { |
4253 |
|
if (!pList) |
4254 |
|
pList = pFile->pRIFF->GetSubList(LIST_TYPE_3LS)->AddSubList(LIST_TYPE_RTIS); |
4255 |
|
|
4256 |
|
// now store the name of this group as <LSNM> chunk as subchunk of the <RTIS> list chunk |
4257 |
|
::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64); |
4258 |
|
|
4259 |
|
for (std::list<Script*>::iterator it = pScripts->begin(); |
4260 |
|
it != pScripts->end(); ++it) |
4261 |
|
{ |
4262 |
|
(*it)->UpdateChunks(); |
4263 |
|
} |
4264 |
|
} |
4265 |
|
} |
4266 |
|
|
4267 |
|
/** @brief Get instrument script. |
4268 |
|
* |
4269 |
|
* Returns the real-time instrument script with the given index. |
4270 |
|
* |
4271 |
|
* @param index - number of the sought script (0..n) |
4272 |
|
* @returns sought script or NULL if there's no such script |
4273 |
|
*/ |
4274 |
|
Script* ScriptGroup::GetScript(uint index) { |
4275 |
|
if (!pScripts) LoadScripts(); |
4276 |
|
std::list<Script*>::iterator it = pScripts->begin(); |
4277 |
|
for (uint i = 0; it != pScripts->end(); ++i, ++it) |
4278 |
|
if (i == index) return *it; |
4279 |
|
return NULL; |
4280 |
|
} |
4281 |
|
|
4282 |
|
/** @brief Add new instrument script. |
4283 |
|
* |
4284 |
|
* Adds a new real-time instrument script to the file. The script is not |
4285 |
|
* actually used / executed unless it is referenced by an instrument to be |
4286 |
|
* used. This is similar to samples, which you can add to a file, without |
4287 |
|
* an instrument necessarily actually using it. |
4288 |
|
* |
4289 |
|
* You have to call Save() to make this persistent to the file. |
4290 |
|
* |
4291 |
|
* @return new empty script object |
4292 |
|
*/ |
4293 |
|
Script* ScriptGroup::AddScript() { |
4294 |
|
if (!pScripts) LoadScripts(); |
4295 |
|
Script* pScript = new Script(this, NULL); |
4296 |
|
pScripts->push_back(pScript); |
4297 |
|
return pScript; |
4298 |
|
} |
4299 |
|
|
4300 |
|
/** @brief Delete an instrument script. |
4301 |
|
* |
4302 |
|
* This will delete the given real-time instrument script. References of |
4303 |
|
* instruments that are using that script will be removed accordingly. |
4304 |
|
* |
4305 |
|
* You have to call Save() to make this persistent to the file. |
4306 |
|
* |
4307 |
|
* @param pScript - script to delete |
4308 |
|
* @throws gig::Exception if given script could not be found |
4309 |
|
*/ |
4310 |
|
void ScriptGroup::DeleteScript(Script* pScript) { |
4311 |
|
if (!pScripts) LoadScripts(); |
4312 |
|
std::list<Script*>::iterator iter = |
4313 |
|
find(pScripts->begin(), pScripts->end(), pScript); |
4314 |
|
if (iter == pScripts->end()) |
4315 |
|
throw gig::Exception("Could not delete script, could not find given script"); |
4316 |
|
pScripts->erase(iter); |
4317 |
|
pScript->RemoveAllScriptReferences(); |
4318 |
|
if (pScript->pChunk) |
4319 |
|
pScript->pChunk->GetParent()->DeleteSubChunk(pScript->pChunk); |
4320 |
|
delete pScript; |
4321 |
|
} |
4322 |
|
|
4323 |
|
void ScriptGroup::LoadScripts() { |
4324 |
|
if (pScripts) return; |
4325 |
|
pScripts = new std::list<Script*>; |
4326 |
|
if (!pList) return; |
4327 |
|
|
4328 |
|
for (RIFF::Chunk* ck = pList->GetFirstSubChunk(); ck; |
4329 |
|
ck = pList->GetNextSubChunk()) |
4330 |
|
{ |
4331 |
|
if (ck->GetChunkID() == CHUNK_ID_SCRI) { |
4332 |
|
pScripts->push_back(new Script(this, ck)); |
4333 |
|
} |
4334 |
|
} |
4335 |
|
} |
4336 |
|
|
4337 |
// *************** Instrument *************** |
// *************** Instrument *************** |
4338 |
// * |
// * |
4339 |
|
|
4356 |
DimensionKeyRange.high = 0; |
DimensionKeyRange.high = 0; |
4357 |
pMidiRules = new MidiRule*[3]; |
pMidiRules = new MidiRule*[3]; |
4358 |
pMidiRules[0] = NULL; |
pMidiRules[0] = NULL; |
4359 |
|
pScriptRefs = NULL; |
4360 |
|
|
4361 |
// Loading |
// Loading |
4362 |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
4417 |
} |
} |
4418 |
} |
} |
4419 |
|
|
4420 |
|
// own gig format extensions |
4421 |
|
RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS); |
4422 |
|
if (lst3LS) { |
4423 |
|
RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL); |
4424 |
|
if (ckSCSL) { |
4425 |
|
int headerSize = ckSCSL->ReadUint32(); |
4426 |
|
int slotCount = ckSCSL->ReadUint32(); |
4427 |
|
if (slotCount) { |
4428 |
|
int slotSize = ckSCSL->ReadUint32(); |
4429 |
|
ckSCSL->SetPos(headerSize); // in case of future header extensions |
4430 |
|
int unknownSpace = slotSize - 2*sizeof(uint32_t); // in case of future slot extensions |
4431 |
|
for (int i = 0; i < slotCount; ++i) { |
4432 |
|
_ScriptPooolEntry e; |
4433 |
|
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 |
|
} |
4441 |
|
|
4442 |
__notify_progress(pProgress, 1.0f); // notify done |
__notify_progress(pProgress, 1.0f); // notify done |
4443 |
} |
} |
4444 |
|
|
4459 |
delete pMidiRules[i]; |
delete pMidiRules[i]; |
4460 |
} |
} |
4461 |
delete[] pMidiRules; |
delete[] pMidiRules; |
4462 |
|
if (pScriptRefs) delete pScriptRefs; |
4463 |
} |
} |
4464 |
|
|
4465 |
/** |
/** |
4515 |
pMidiRules[i]->UpdateChunks(pData); |
pMidiRules[i]->UpdateChunks(pData); |
4516 |
} |
} |
4517 |
} |
} |
4518 |
|
|
4519 |
|
// own gig format extensions |
4520 |
|
if (pScriptRefs) { |
4521 |
|
RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS); |
4522 |
|
if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS); |
4523 |
|
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); |
4528 |
|
if (!ckSCSL) ckSCSL = lst3LS->AddSubChunk(CHUNK_ID_SCSL, totalChunkSize); |
4529 |
|
else ckSCSL->Resize(totalChunkSize); |
4530 |
|
uint8_t* pData = (uint8_t*) ckSCSL->LoadChunkData(); |
4531 |
|
int pos = 0; |
4532 |
|
store32(&pData[pos], headerSize); |
4533 |
|
pos += sizeof(uint32_t); |
4534 |
|
store32(&pData[pos], slotCount); |
4535 |
|
pos += sizeof(uint32_t); |
4536 |
|
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); |
4544 |
|
store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0); |
4545 |
|
pos += sizeof(uint32_t); |
4546 |
|
} |
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 |
/** |
/** |
4696 |
pMidiRules[i] = 0; |
pMidiRules[i] = 0; |
4697 |
} |
} |
4698 |
|
|
4699 |
|
void Instrument::LoadScripts() { |
4700 |
|
if (pScriptRefs) return; |
4701 |
|
pScriptRefs = new std::vector<_ScriptPooolRef>; |
4702 |
|
if (scriptPoolFileOffsets.empty()) return; |
4703 |
|
File* pFile = (File*) GetParent(); |
4704 |
|
for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) { |
4705 |
|
uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset; |
4706 |
|
for (uint i = 0; pFile->GetScriptGroup(i); ++i) { |
4707 |
|
ScriptGroup* group = pFile->GetScriptGroup(i); |
4708 |
|
for (uint s = 0; group->GetScript(s); ++s) { |
4709 |
|
Script* script = group->GetScript(s); |
4710 |
|
if (script->pChunk) { |
4711 |
|
uint32_t offset = script->pChunk->GetFilePos() - |
4712 |
|
script->pChunk->GetPos() - |
4713 |
|
CHUNK_HEADER_SIZE; |
4714 |
|
if (offset == soughtOffset) |
4715 |
|
{ |
4716 |
|
_ScriptPooolRef ref; |
4717 |
|
ref.script = script; |
4718 |
|
ref.bypass = scriptPoolFileOffsets[k].bypass; |
4719 |
|
pScriptRefs->push_back(ref); |
4720 |
|
break; |
4721 |
|
} |
4722 |
|
} |
4723 |
|
} |
4724 |
|
} |
4725 |
|
} |
4726 |
|
// we don't need that anymore |
4727 |
|
scriptPoolFileOffsets.clear(); |
4728 |
|
} |
4729 |
|
|
4730 |
|
/** @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, |
4751 |
|
* which shall be executed by the sampler for for this instrument. The |
4752 |
|
* script will be added to the end of the script list of this instrument. |
4753 |
|
* The positions of the scripts in the Instrument's Script list are |
4754 |
|
* relevant, because they define in which order they shall be executed by |
4755 |
|
* the sampler. For this reason it is also legal to add the same script |
4756 |
|
* twice to an instrument, for example you might have a script called |
4757 |
|
* "MyFilter" which performs an event filter task, and you might have |
4758 |
|
* another script called "MyNoteTrigger" which triggers new notes, then you |
4759 |
|
* might for example have the following list of scripts on the instrument: |
4760 |
|
* |
4761 |
|
* 1. Script "MyFilter" |
4762 |
|
* 2. Script "MyNoteTrigger" |
4763 |
|
* 3. Script "MyFilter" |
4764 |
|
* |
4765 |
|
* Which would make sense, because the 2nd script launched new events, which |
4766 |
|
* you might need to filter as well. |
4767 |
|
* |
4768 |
|
* There are two ways to disable / "bypass" scripts. You can either disable |
4769 |
|
* a script locally for the respective script slot on an instrument (i.e. by |
4770 |
|
* passing @c false to the 2nd argument of this method, or by calling |
4771 |
|
* SetScriptBypassed()). Or you can disable a script globally for all slots |
4772 |
|
* and all instruments by setting Script::Bypass. |
4773 |
|
* |
4774 |
|
* @note This is an own format extension which did not exist i.e. in the |
4775 |
|
* GigaStudio 4 software. It will currently only work with LinuxSampler and |
4776 |
|
* gigedit. |
4777 |
|
* |
4778 |
|
* @param pScript - script that shall be executed for this instrument |
4779 |
|
* @param bypass - if enabled, the sampler shall skip executing this |
4780 |
|
* script (in the respective list position) |
4781 |
|
* @see SetScriptBypassed() |
4782 |
|
*/ |
4783 |
|
void Instrument::AddScriptSlot(Script* pScript, bool bypass) { |
4784 |
|
LoadScripts(); |
4785 |
|
_ScriptPooolRef ref = { pScript, bypass }; |
4786 |
|
pScriptRefs->push_back(ref); |
4787 |
|
} |
4788 |
|
|
4789 |
|
/** @brief Flip two script slots with each other (gig format extension). |
4790 |
|
* |
4791 |
|
* Swaps the position of the two given scripts in the Instrument's Script |
4792 |
|
* list. The positions of the scripts in the Instrument's Script list are |
4793 |
|
* relevant, because they define in which order they shall be executed by |
4794 |
|
* the sampler. |
4795 |
|
* |
4796 |
|
* @note This is an own format extension which did not exist i.e. in the |
4797 |
|
* GigaStudio 4 software. It will currently only work with LinuxSampler and |
4798 |
|
* gigedit. |
4799 |
|
* |
4800 |
|
* @param index1 - index of the first script slot to swap |
4801 |
|
* @param index2 - index of the second script slot to swap |
4802 |
|
*/ |
4803 |
|
void Instrument::SwapScriptSlots(uint index1, uint index2) { |
4804 |
|
LoadScripts(); |
4805 |
|
if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size()) |
4806 |
|
return; |
4807 |
|
_ScriptPooolRef tmp = (*pScriptRefs)[index1]; |
4808 |
|
(*pScriptRefs)[index1] = (*pScriptRefs)[index2]; |
4809 |
|
(*pScriptRefs)[index2] = tmp; |
4810 |
|
} |
4811 |
|
|
4812 |
|
/** @brief Remove script slot. |
4813 |
|
* |
4814 |
|
* Removes the script slot with the given slot index. |
4815 |
|
* |
4816 |
|
* @param index - index of script slot to remove |
4817 |
|
*/ |
4818 |
|
void Instrument::RemoveScriptSlot(uint index) { |
4819 |
|
LoadScripts(); |
4820 |
|
if (index >= pScriptRefs->size()) return; |
4821 |
|
pScriptRefs->erase( pScriptRefs->begin() + index ); |
4822 |
|
} |
4823 |
|
|
4824 |
|
/** @brief Remove reference to given Script (gig format extension). |
4825 |
|
* |
4826 |
|
* This will remove all script slots on the instrument which are referencing |
4827 |
|
* the given script. |
4828 |
|
* |
4829 |
|
* @note This is an own format extension which did not exist i.e. in the |
4830 |
|
* GigaStudio 4 software. It will currently only work with LinuxSampler and |
4831 |
|
* gigedit. |
4832 |
|
* |
4833 |
|
* @param pScript - script reference to remove from this instrument |
4834 |
|
* @see RemoveScriptSlot() |
4835 |
|
*/ |
4836 |
|
void Instrument::RemoveScript(Script* pScript) { |
4837 |
|
LoadScripts(); |
4838 |
|
for (int i = pScriptRefs->size() - 1; i >= 0; --i) { |
4839 |
|
if ((*pScriptRefs)[i].script == pScript) { |
4840 |
|
pScriptRefs->erase( pScriptRefs->begin() + i ); |
4841 |
|
} |
4842 |
|
} |
4843 |
|
} |
4844 |
|
|
4845 |
|
/** @brief Instrument's amount of script slots. |
4846 |
|
* |
4847 |
|
* This method returns the amount of script slots this instrument currently |
4848 |
|
* uses. |
4849 |
|
* |
4850 |
|
* A script slot is a reference of a real-time instrument script to be |
4851 |
|
* executed by the sampler. The scripts will be executed by the sampler in |
4852 |
|
* sequence of the slots. One (same) script may be referenced multiple |
4853 |
|
* times in different slots. |
4854 |
|
* |
4855 |
|
* @note This is an own format extension which did not exist i.e. in the |
4856 |
|
* GigaStudio 4 software. It will currently only work with LinuxSampler and |
4857 |
|
* gigedit. |
4858 |
|
*/ |
4859 |
|
uint Instrument::ScriptSlotCount() const { |
4860 |
|
return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size(); |
4861 |
|
} |
4862 |
|
|
4863 |
|
/** @brief Whether script execution shall be skipped. |
4864 |
|
* |
4865 |
|
* Defines locally for the Script reference slot in the Instrument's Script |
4866 |
|
* list, whether the script shall be skipped by the sampler regarding |
4867 |
|
* execution. |
4868 |
|
* |
4869 |
|
* It is also possible to ignore exeuction of the script globally, for all |
4870 |
|
* slots and for all instruments by setting Script::Bypass. |
4871 |
|
* |
4872 |
|
* @note This is an own format extension which did not exist i.e. in the |
4873 |
|
* GigaStudio 4 software. It will currently only work with LinuxSampler and |
4874 |
|
* gigedit. |
4875 |
|
* |
4876 |
|
* @param index - index of the script slot on this instrument |
4877 |
|
* @see Script::Bypass |
4878 |
|
*/ |
4879 |
|
bool Instrument::IsScriptSlotBypassed(uint index) { |
4880 |
|
if (index >= ScriptSlotCount()) return false; |
4881 |
|
return pScriptRefs ? pScriptRefs->at(index).bypass |
4882 |
|
: scriptPoolFileOffsets.at(index).bypass; |
4883 |
|
|
4884 |
|
} |
4885 |
|
|
4886 |
|
/** @brief Defines whether execution shall be skipped. |
4887 |
|
* |
4888 |
|
* You can call this method to define locally whether or whether not the |
4889 |
|
* given script slot shall be executed by the sampler. |
4890 |
|
* |
4891 |
|
* @note This is an own format extension which did not exist i.e. in the |
4892 |
|
* GigaStudio 4 software. It will currently only work with LinuxSampler and |
4893 |
|
* gigedit. |
4894 |
|
* |
4895 |
|
* @param index - script slot index on this instrument |
4896 |
|
* @param bBypass - if true, the script slot will be skipped by the sampler |
4897 |
|
* @see Script::Bypass |
4898 |
|
*/ |
4899 |
|
void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) { |
4900 |
|
if (index >= ScriptSlotCount()) return; |
4901 |
|
if (pScriptRefs) |
4902 |
|
pScriptRefs->at(index).bypass = bBypass; |
4903 |
|
else |
4904 |
|
scriptPoolFileOffsets.at(index).bypass = bBypass; |
4905 |
|
} |
4906 |
|
|
4907 |
/** |
/** |
4908 |
* 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 |
4909 |
* and assign it to this object. |
* and assign it to this object. |
4937 |
PitchbendRange = orig->PitchbendRange; |
PitchbendRange = orig->PitchbendRange; |
4938 |
PianoReleaseMode = orig->PianoReleaseMode; |
PianoReleaseMode = orig->PianoReleaseMode; |
4939 |
DimensionKeyRange = orig->DimensionKeyRange; |
DimensionKeyRange = orig->DimensionKeyRange; |
4940 |
|
scriptPoolFileOffsets = orig->scriptPoolFileOffsets; |
4941 |
|
pScriptRefs = orig->pScriptRefs; |
4942 |
|
|
4943 |
// free old midi rules |
// free old midi rules |
4944 |
for (int i = 0 ; pMidiRules[i] ; i++) { |
for (int i = 0 ; pMidiRules[i] ; i++) { |
5124 |
bAutoLoad = true; |
bAutoLoad = true; |
5125 |
*pVersion = VERSION_3; |
*pVersion = VERSION_3; |
5126 |
pGroups = NULL; |
pGroups = NULL; |
5127 |
|
pScriptGroups = NULL; |
5128 |
pInfo->SetFixedStringLengths(_FileFixedStringLengths); |
pInfo->SetFixedStringLengths(_FileFixedStringLengths); |
5129 |
pInfo->ArchivalLocation = String(256, ' '); |
pInfo->ArchivalLocation = String(256, ' '); |
5130 |
|
|
5140 |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
5141 |
bAutoLoad = true; |
bAutoLoad = true; |
5142 |
pGroups = NULL; |
pGroups = NULL; |
5143 |
|
pScriptGroups = NULL; |
5144 |
pInfo->SetFixedStringLengths(_FileFixedStringLengths); |
pInfo->SetFixedStringLengths(_FileFixedStringLengths); |
5145 |
} |
} |
5146 |
|
|
5154 |
} |
} |
5155 |
delete pGroups; |
delete pGroups; |
5156 |
} |
} |
5157 |
|
if (pScriptGroups) { |
5158 |
|
std::list<ScriptGroup*>::iterator iter = pScriptGroups->begin(); |
5159 |
|
std::list<ScriptGroup*>::iterator end = pScriptGroups->end(); |
5160 |
|
while (iter != end) { |
5161 |
|
delete *iter; |
5162 |
|
++iter; |
5163 |
|
} |
5164 |
|
delete pScriptGroups; |
5165 |
|
} |
5166 |
} |
} |
5167 |
|
|
5168 |
Sample* File::GetFirstSample(progress_t* pProgress) { |
Sample* File::GetFirstSample(progress_t* pProgress) { |
5675 |
} |
} |
5676 |
} |
} |
5677 |
|
|
5678 |
|
/** @brief Get instrument script group (by index). |
5679 |
|
* |
5680 |
|
* Returns the real-time instrument script group with the given index. |
5681 |
|
* |
5682 |
|
* @param index - number of the sought group (0..n) |
5683 |
|
* @returns sought script group or NULL if there's no such group |
5684 |
|
*/ |
5685 |
|
ScriptGroup* File::GetScriptGroup(uint index) { |
5686 |
|
if (!pScriptGroups) LoadScriptGroups(); |
5687 |
|
std::list<ScriptGroup*>::iterator it = pScriptGroups->begin(); |
5688 |
|
for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) |
5689 |
|
if (i == index) return *it; |
5690 |
|
return NULL; |
5691 |
|
} |
5692 |
|
|
5693 |
|
/** @brief Get instrument script group (by name). |
5694 |
|
* |
5695 |
|
* Returns the first real-time instrument script group found with the given |
5696 |
|
* group name. Note that group names may not necessarily be unique. |
5697 |
|
* |
5698 |
|
* @param name - name of the sought script group |
5699 |
|
* @returns sought script group or NULL if there's no such group |
5700 |
|
*/ |
5701 |
|
ScriptGroup* File::GetScriptGroup(const String& name) { |
5702 |
|
if (!pScriptGroups) LoadScriptGroups(); |
5703 |
|
std::list<ScriptGroup*>::iterator it = pScriptGroups->begin(); |
5704 |
|
for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) |
5705 |
|
if ((*it)->Name == name) return *it; |
5706 |
|
return NULL; |
5707 |
|
} |
5708 |
|
|
5709 |
|
/** @brief Add new instrument script group. |
5710 |
|
* |
5711 |
|
* Adds a new, empty real-time instrument script group to the file. |
5712 |
|
* |
5713 |
|
* You have to call Save() to make this persistent to the file. |
5714 |
|
* |
5715 |
|
* @return new empty script group |
5716 |
|
*/ |
5717 |
|
ScriptGroup* File::AddScriptGroup() { |
5718 |
|
if (!pScriptGroups) LoadScriptGroups(); |
5719 |
|
ScriptGroup* pScriptGroup = new ScriptGroup(this, NULL); |
5720 |
|
pScriptGroups->push_back(pScriptGroup); |
5721 |
|
return pScriptGroup; |
5722 |
|
} |
5723 |
|
|
5724 |
|
/** @brief Delete an instrument script group. |
5725 |
|
* |
5726 |
|
* This will delete the given real-time instrument script group and all its |
5727 |
|
* instrument scripts it contains. References inside instruments that are |
5728 |
|
* using the deleted scripts will be removed from the respective instruments |
5729 |
|
* accordingly. |
5730 |
|
* |
5731 |
|
* You have to call Save() to make this persistent to the file. |
5732 |
|
* |
5733 |
|
* @param pScriptGroup - script group to delete |
5734 |
|
* @throws gig::Exception if given script group could not be found |
5735 |
|
*/ |
5736 |
|
void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) { |
5737 |
|
if (!pScriptGroups) LoadScriptGroups(); |
5738 |
|
std::list<ScriptGroup*>::iterator iter = |
5739 |
|
find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup); |
5740 |
|
if (iter == pScriptGroups->end()) |
5741 |
|
throw gig::Exception("Could not delete script group, could not find given script group"); |
5742 |
|
pScriptGroups->erase(iter); |
5743 |
|
for (int i = 0; pScriptGroup->GetScript(i); ++i) |
5744 |
|
pScriptGroup->DeleteScript(pScriptGroup->GetScript(i)); |
5745 |
|
if (pScriptGroup->pList) |
5746 |
|
pScriptGroup->pList->GetParent()->DeleteSubChunk(pScriptGroup->pList); |
5747 |
|
delete pScriptGroup; |
5748 |
|
} |
5749 |
|
|
5750 |
|
void File::LoadScriptGroups() { |
5751 |
|
if (pScriptGroups) return; |
5752 |
|
pScriptGroups = new std::list<ScriptGroup*>; |
5753 |
|
RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS); |
5754 |
|
if (lstLS) { |
5755 |
|
for (RIFF::List* lst = lstLS->GetFirstSubList(); lst; |
5756 |
|
lst = lstLS->GetNextSubList()) |
5757 |
|
{ |
5758 |
|
if (lst->GetListType() == LIST_TYPE_RTIS) { |
5759 |
|
pScriptGroups->push_back(new ScriptGroup(this, lst)); |
5760 |
|
} |
5761 |
|
} |
5762 |
|
} |
5763 |
|
} |
5764 |
|
|
5765 |
/** |
/** |
5766 |
* Apply all the gig file's current instruments, samples, groups and settings |
* Apply all the gig file's current instruments, samples, groups and settings |
5767 |
* 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 |
5777 |
|
|
5778 |
b64BitWavePoolOffsets = pVersion && pVersion->major == 3; |
b64BitWavePoolOffsets = pVersion && pVersion->major == 3; |
5779 |
|
|
5780 |
|
// update own gig format extension chunks |
5781 |
|
// (not part of the GigaStudio 4 format) |
5782 |
|
// |
5783 |
|
// This must be performed before writing the chunks for instruments, |
5784 |
|
// because the instruments' script slots will write the file offsets |
5785 |
|
// of the respective instrument script chunk as reference. |
5786 |
|
if (pScriptGroups) { |
5787 |
|
RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS); |
5788 |
|
if (pScriptGroups->empty()) { |
5789 |
|
if (lst3LS) pRIFF->DeleteSubChunk(lst3LS); |
5790 |
|
} else { |
5791 |
|
if (!lst3LS) lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS); |
5792 |
|
|
5793 |
|
// Update instrument script (group) chunks. |
5794 |
|
|
5795 |
|
for (std::list<ScriptGroup*>::iterator it = pScriptGroups->begin(); |
5796 |
|
it != pScriptGroups->end(); ++it) |
5797 |
|
{ |
5798 |
|
(*it)->UpdateChunks(); |
5799 |
|
} |
5800 |
|
} |
5801 |
|
} |
5802 |
|
|
5803 |
// first update base class's chunks |
// first update base class's chunks |
5804 |
DLS::File::UpdateChunks(); |
DLS::File::UpdateChunks(); |
5805 |
|
|
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 |