28 |
#include <algorithm> |
#include <algorithm> |
29 |
#include <math.h> |
#include <math.h> |
30 |
#include <iostream> |
#include <iostream> |
31 |
|
#include <assert.h> |
32 |
|
|
33 |
/// Initial size of the sample buffer which is used for decompression of |
/// Initial size of the sample buffer which is used for decompression of |
34 |
/// compressed sample wave streams - this value should always be bigger than |
/// compressed sample wave streams - this value should always be bigger than |
1709 |
//NOTE: copy code copied from assignment constructor above, see comment there as well |
//NOTE: copy code copied from assignment constructor above, see comment there as well |
1710 |
|
|
1711 |
*this = *orig; // default memberwise shallow copy of all parameters |
*this = *orig; // default memberwise shallow copy of all parameters |
1712 |
|
|
1713 |
|
// restore members that shall not be altered |
1714 |
pParentList = p; // restore the chunk pointer |
pParentList = p; // restore the chunk pointer |
1715 |
|
pRegion = pOriginalRegion; |
1716 |
|
|
1717 |
// only take the raw sample reference & parent region reference if the |
// only take the raw sample reference reference if the |
1718 |
// two DimensionRegion objects are part of the same file |
// two DimensionRegion objects are part of the same file |
1719 |
if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) { |
if (pOriginalRegion->GetParent()->GetParent() != orig->pRegion->GetParent()->GetParent()) { |
|
pRegion = pOriginalRegion; |
|
1720 |
pSample = pOriginalSample; |
pSample = pOriginalSample; |
1721 |
} |
} |
1722 |
|
|
3202 |
* dimension bits limit is violated |
* dimension bits limit is violated |
3203 |
*/ |
*/ |
3204 |
void Region::AddDimension(dimension_def_t* pDimDef) { |
void Region::AddDimension(dimension_def_t* pDimDef) { |
3205 |
|
// some initial sanity checks of the given dimension definition |
3206 |
|
if (pDimDef->zones < 2) |
3207 |
|
throw gig::Exception("Could not add new dimension, amount of requested zones must always be at least two"); |
3208 |
|
if (pDimDef->bits < 1) |
3209 |
|
throw gig::Exception("Could not add new dimension, amount of requested requested zone bits must always be at least one"); |
3210 |
|
if (pDimDef->dimension == dimension_samplechannel) { |
3211 |
|
if (pDimDef->zones != 2) |
3212 |
|
throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zones must always be 2 for this dimension type"); |
3213 |
|
if (pDimDef->bits != 1) |
3214 |
|
throw gig::Exception("Could not add new 'sample channel' dimensions, the requested amount of zone bits must always be 1 for this dimension type"); |
3215 |
|
} |
3216 |
|
|
3217 |
// check if max. amount of dimensions reached |
// check if max. amount of dimensions reached |
3218 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
3219 |
const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
3389 |
if (pDimDef->dimension == dimension_layer) Layers = 1; |
if (pDimDef->dimension == dimension_layer) Layers = 1; |
3390 |
} |
} |
3391 |
|
|
3392 |
|
/** @brief Delete one split zone of a dimension (decrement zone amount). |
3393 |
|
* |
3394 |
|
* Instead of deleting an entire dimensions, this method will only delete |
3395 |
|
* one particular split zone given by @a zone of the Region's dimension |
3396 |
|
* given by @a type. So this method will simply decrement the amount of |
3397 |
|
* zones by one of the dimension in question. To be able to do that, the |
3398 |
|
* respective dimension must exist on this Region and it must have at least |
3399 |
|
* 3 zones. All DimensionRegion objects associated with the zone will be |
3400 |
|
* deleted. |
3401 |
|
* |
3402 |
|
* @param type - identifies the dimension where a zone shall be deleted |
3403 |
|
* @param zone - index of the dimension split zone that shall be deleted |
3404 |
|
* @throws gig::Exception if requested zone could not be deleted |
3405 |
|
*/ |
3406 |
|
void Region::DeleteDimensionZone(dimension_t type, int zone) { |
3407 |
|
dimension_def_t* oldDef = GetDimensionDefinition(type); |
3408 |
|
if (!oldDef) |
3409 |
|
throw gig::Exception("Could not delete dimension zone, no such dimension of given type"); |
3410 |
|
if (oldDef->zones <= 2) |
3411 |
|
throw gig::Exception("Could not delete dimension zone, because it would end up with only one zone."); |
3412 |
|
if (zone < 0 || zone >= oldDef->zones) |
3413 |
|
throw gig::Exception("Could not delete dimension zone, requested zone index out of bounds."); |
3414 |
|
|
3415 |
|
const int newZoneSize = oldDef->zones - 1; |
3416 |
|
|
3417 |
|
// create a temporary Region which just acts as a temporary copy |
3418 |
|
// container and will be deleted at the end of this function and will |
3419 |
|
// also not be visible through the API during this process |
3420 |
|
gig::Region* tempRgn = NULL; |
3421 |
|
{ |
3422 |
|
// adding these temporary chunks is probably not even necessary |
3423 |
|
Instrument* instr = static_cast<Instrument*>(GetParent()); |
3424 |
|
RIFF::List* pCkInstrument = instr->pCkInstrument; |
3425 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
3426 |
|
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
3427 |
|
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
3428 |
|
tempRgn = new Region(instr, rgn); |
3429 |
|
} |
3430 |
|
|
3431 |
|
// copy this region's dimensions (with already the dimension split size |
3432 |
|
// requested by the arguments of this method call) to the temporary |
3433 |
|
// region, and don't use Region::CopyAssign() here for this task, since |
3434 |
|
// it would also alter fast lookup helper variables here and there |
3435 |
|
dimension_def_t newDef; |
3436 |
|
for (int i = 0; i < Dimensions; ++i) { |
3437 |
|
dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference |
3438 |
|
// is this the dimension requested by the method arguments? ... |
3439 |
|
if (def.dimension == type) { // ... if yes, decrement zone amount by one |
3440 |
|
def.zones = newZoneSize; |
3441 |
|
if ((1 << (def.bits - 1)) == def.zones) def.bits--; |
3442 |
|
newDef = def; |
3443 |
|
} |
3444 |
|
tempRgn->AddDimension(&def); |
3445 |
|
} |
3446 |
|
|
3447 |
|
// find the dimension index in the tempRegion which is the dimension |
3448 |
|
// type passed to this method (paranoidly expecting different order) |
3449 |
|
int tempReducedDimensionIndex = -1; |
3450 |
|
for (int d = 0; d < tempRgn->Dimensions; ++d) { |
3451 |
|
if (tempRgn->pDimensionDefinitions[d].dimension == type) { |
3452 |
|
tempReducedDimensionIndex = d; |
3453 |
|
break; |
3454 |
|
} |
3455 |
|
} |
3456 |
|
|
3457 |
|
// copy dimension regions from this region to the temporary region |
3458 |
|
for (int iDst = 0; iDst < 256; ++iDst) { |
3459 |
|
DimensionRegion* dstDimRgn = tempRgn->pDimensionRegions[iDst]; |
3460 |
|
if (!dstDimRgn) continue; |
3461 |
|
std::map<dimension_t,int> dimCase; |
3462 |
|
bool isValidZone = true; |
3463 |
|
for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) { |
3464 |
|
const int dstBits = tempRgn->pDimensionDefinitions[d].bits; |
3465 |
|
dimCase[tempRgn->pDimensionDefinitions[d].dimension] = |
3466 |
|
(iDst >> baseBits) & ((1 << dstBits) - 1); |
3467 |
|
baseBits += dstBits; |
3468 |
|
// there are also DimensionRegion objects of unused zones, skip them |
3469 |
|
if (dimCase[tempRgn->pDimensionDefinitions[d].dimension] >= tempRgn->pDimensionDefinitions[d].zones) { |
3470 |
|
isValidZone = false; |
3471 |
|
break; |
3472 |
|
} |
3473 |
|
} |
3474 |
|
if (!isValidZone) continue; |
3475 |
|
// a bit paranoid: cope with the chance that the dimensions would |
3476 |
|
// have different order in source and destination regions |
3477 |
|
const bool isLastZone = (dimCase[type] == newZoneSize - 1); |
3478 |
|
if (dimCase[type] >= zone) dimCase[type]++; |
3479 |
|
DimensionRegion* srcDimRgn = GetDimensionRegionByBit(dimCase); |
3480 |
|
dstDimRgn->CopyAssign(srcDimRgn); |
3481 |
|
// if this is the upper most zone of the dimension passed to this |
3482 |
|
// method, then correct (raise) its upper limit to 127 |
3483 |
|
if (newDef.split_type == split_type_normal && isLastZone) |
3484 |
|
dstDimRgn->DimensionUpperLimits[tempReducedDimensionIndex] = 127; |
3485 |
|
} |
3486 |
|
|
3487 |
|
// now tempRegion's dimensions and DimensionRegions basically reflect |
3488 |
|
// what we wanted to get for this actual Region here, so we now just |
3489 |
|
// delete and recreate the dimension in question with the new amount |
3490 |
|
// zones and then copy back from tempRegion |
3491 |
|
DeleteDimension(oldDef); |
3492 |
|
AddDimension(&newDef); |
3493 |
|
for (int iSrc = 0; iSrc < 256; ++iSrc) { |
3494 |
|
DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc]; |
3495 |
|
if (!srcDimRgn) continue; |
3496 |
|
std::map<dimension_t,int> dimCase; |
3497 |
|
for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) { |
3498 |
|
const int srcBits = tempRgn->pDimensionDefinitions[d].bits; |
3499 |
|
dimCase[tempRgn->pDimensionDefinitions[d].dimension] = |
3500 |
|
(iSrc >> baseBits) & ((1 << srcBits) - 1); |
3501 |
|
baseBits += srcBits; |
3502 |
|
} |
3503 |
|
// a bit paranoid: cope with the chance that the dimensions would |
3504 |
|
// have different order in source and destination regions |
3505 |
|
DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase); |
3506 |
|
if (!dstDimRgn) continue; |
3507 |
|
dstDimRgn->CopyAssign(srcDimRgn); |
3508 |
|
} |
3509 |
|
|
3510 |
|
// delete temporary region |
3511 |
|
delete tempRgn; |
3512 |
|
|
3513 |
|
UpdateVelocityTable(); |
3514 |
|
} |
3515 |
|
|
3516 |
|
/** @brief Divide split zone of a dimension in two (increment zone amount). |
3517 |
|
* |
3518 |
|
* This will increment the amount of zones for the dimension (given by |
3519 |
|
* @a type) by one. It will do so by dividing the zone (given by @a zone) |
3520 |
|
* in the middle of its zone range in two. So the two zones resulting from |
3521 |
|
* the zone being splitted, will be an equivalent copy regarding all their |
3522 |
|
* articulation informations and sample reference. The two zones will only |
3523 |
|
* differ in their zone's upper limit |
3524 |
|
* (DimensionRegion::DimensionUpperLimits). |
3525 |
|
* |
3526 |
|
* @param type - identifies the dimension where a zone shall be splitted |
3527 |
|
* @param zone - index of the dimension split zone that shall be splitted |
3528 |
|
* @throws gig::Exception if requested zone could not be splitted |
3529 |
|
*/ |
3530 |
|
void Region::SplitDimensionZone(dimension_t type, int zone) { |
3531 |
|
dimension_def_t* oldDef = GetDimensionDefinition(type); |
3532 |
|
if (!oldDef) |
3533 |
|
throw gig::Exception("Could not split dimension zone, no such dimension of given type"); |
3534 |
|
if (zone < 0 || zone >= oldDef->zones) |
3535 |
|
throw gig::Exception("Could not split dimension zone, requested zone index out of bounds."); |
3536 |
|
|
3537 |
|
const int newZoneSize = oldDef->zones + 1; |
3538 |
|
|
3539 |
|
// create a temporary Region which just acts as a temporary copy |
3540 |
|
// container and will be deleted at the end of this function and will |
3541 |
|
// also not be visible through the API during this process |
3542 |
|
gig::Region* tempRgn = NULL; |
3543 |
|
{ |
3544 |
|
// adding these temporary chunks is probably not even necessary |
3545 |
|
Instrument* instr = static_cast<Instrument*>(GetParent()); |
3546 |
|
RIFF::List* pCkInstrument = instr->pCkInstrument; |
3547 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
3548 |
|
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
3549 |
|
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
3550 |
|
tempRgn = new Region(instr, rgn); |
3551 |
|
} |
3552 |
|
|
3553 |
|
// copy this region's dimensions (with already the dimension split size |
3554 |
|
// requested by the arguments of this method call) to the temporary |
3555 |
|
// region, and don't use Region::CopyAssign() here for this task, since |
3556 |
|
// it would also alter fast lookup helper variables here and there |
3557 |
|
dimension_def_t newDef; |
3558 |
|
for (int i = 0; i < Dimensions; ++i) { |
3559 |
|
dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference |
3560 |
|
// is this the dimension requested by the method arguments? ... |
3561 |
|
if (def.dimension == type) { // ... if yes, increment zone amount by one |
3562 |
|
def.zones = newZoneSize; |
3563 |
|
if ((1 << oldDef->bits) < newZoneSize) def.bits++; |
3564 |
|
newDef = def; |
3565 |
|
} |
3566 |
|
tempRgn->AddDimension(&def); |
3567 |
|
} |
3568 |
|
|
3569 |
|
// find the dimension index in the tempRegion which is the dimension |
3570 |
|
// type passed to this method (paranoidly expecting different order) |
3571 |
|
int tempIncreasedDimensionIndex = -1; |
3572 |
|
for (int d = 0; d < tempRgn->Dimensions; ++d) { |
3573 |
|
if (tempRgn->pDimensionDefinitions[d].dimension == type) { |
3574 |
|
tempIncreasedDimensionIndex = d; |
3575 |
|
break; |
3576 |
|
} |
3577 |
|
} |
3578 |
|
|
3579 |
|
// copy dimension regions from this region to the temporary region |
3580 |
|
for (int iSrc = 0; iSrc < 256; ++iSrc) { |
3581 |
|
DimensionRegion* srcDimRgn = pDimensionRegions[iSrc]; |
3582 |
|
if (!srcDimRgn) continue; |
3583 |
|
std::map<dimension_t,int> dimCase; |
3584 |
|
bool isValidZone = true; |
3585 |
|
for (int d = 0, baseBits = 0; d < Dimensions; ++d) { |
3586 |
|
const int srcBits = pDimensionDefinitions[d].bits; |
3587 |
|
dimCase[pDimensionDefinitions[d].dimension] = |
3588 |
|
(iSrc >> baseBits) & ((1 << srcBits) - 1); |
3589 |
|
// there are also DimensionRegion objects for unused zones, skip them |
3590 |
|
if (dimCase[pDimensionDefinitions[d].dimension] >= pDimensionDefinitions[d].zones) { |
3591 |
|
isValidZone = false; |
3592 |
|
break; |
3593 |
|
} |
3594 |
|
baseBits += srcBits; |
3595 |
|
} |
3596 |
|
if (!isValidZone) continue; |
3597 |
|
// a bit paranoid: cope with the chance that the dimensions would |
3598 |
|
// have different order in source and destination regions |
3599 |
|
if (dimCase[type] > zone) dimCase[type]++; |
3600 |
|
DimensionRegion* dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase); |
3601 |
|
dstDimRgn->CopyAssign(srcDimRgn); |
3602 |
|
// if this is the requested zone to be splitted, then also copy |
3603 |
|
// the source DimensionRegion to the newly created target zone |
3604 |
|
// and set the old zones upper limit lower |
3605 |
|
if (dimCase[type] == zone) { |
3606 |
|
// lower old zones upper limit |
3607 |
|
if (newDef.split_type == split_type_normal) { |
3608 |
|
const int high = |
3609 |
|
dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex]; |
3610 |
|
int low = 0; |
3611 |
|
if (zone > 0) { |
3612 |
|
std::map<dimension_t,int> lowerCase = dimCase; |
3613 |
|
lowerCase[type]--; |
3614 |
|
DimensionRegion* dstDimRgnLow = tempRgn->GetDimensionRegionByBit(lowerCase); |
3615 |
|
low = dstDimRgnLow->DimensionUpperLimits[tempIncreasedDimensionIndex]; |
3616 |
|
} |
3617 |
|
dstDimRgn->DimensionUpperLimits[tempIncreasedDimensionIndex] = low + (high - low) / 2; |
3618 |
|
} |
3619 |
|
// fill the newly created zone of the divided zone as well |
3620 |
|
dimCase[type]++; |
3621 |
|
dstDimRgn = tempRgn->GetDimensionRegionByBit(dimCase); |
3622 |
|
dstDimRgn->CopyAssign(srcDimRgn); |
3623 |
|
} |
3624 |
|
} |
3625 |
|
|
3626 |
|
// now tempRegion's dimensions and DimensionRegions basically reflect |
3627 |
|
// what we wanted to get for this actual Region here, so we now just |
3628 |
|
// delete and recreate the dimension in question with the new amount |
3629 |
|
// zones and then copy back from tempRegion |
3630 |
|
DeleteDimension(oldDef); |
3631 |
|
AddDimension(&newDef); |
3632 |
|
for (int iSrc = 0; iSrc < 256; ++iSrc) { |
3633 |
|
DimensionRegion* srcDimRgn = tempRgn->pDimensionRegions[iSrc]; |
3634 |
|
if (!srcDimRgn) continue; |
3635 |
|
std::map<dimension_t,int> dimCase; |
3636 |
|
for (int d = 0, baseBits = 0; d < tempRgn->Dimensions; ++d) { |
3637 |
|
const int srcBits = tempRgn->pDimensionDefinitions[d].bits; |
3638 |
|
dimCase[tempRgn->pDimensionDefinitions[d].dimension] = |
3639 |
|
(iSrc >> baseBits) & ((1 << srcBits) - 1); |
3640 |
|
baseBits += srcBits; |
3641 |
|
} |
3642 |
|
// a bit paranoid: cope with the chance that the dimensions would |
3643 |
|
// have different order in source and destination regions |
3644 |
|
DimensionRegion* dstDimRgn = GetDimensionRegionByBit(dimCase); |
3645 |
|
if (!dstDimRgn) continue; |
3646 |
|
dstDimRgn->CopyAssign(srcDimRgn); |
3647 |
|
} |
3648 |
|
|
3649 |
|
// delete temporary region |
3650 |
|
delete tempRgn; |
3651 |
|
|
3652 |
|
UpdateVelocityTable(); |
3653 |
|
} |
3654 |
|
|
3655 |
|
DimensionRegion* Region::GetDimensionRegionByBit(const std::map<dimension_t,int>& DimCase) { |
3656 |
|
uint8_t bits[8] = {}; |
3657 |
|
for (std::map<dimension_t,int>::const_iterator it = DimCase.begin(); |
3658 |
|
it != DimCase.end(); ++it) |
3659 |
|
{ |
3660 |
|
for (int d = 0; d < Dimensions; ++d) { |
3661 |
|
if (pDimensionDefinitions[d].dimension == it->first) { |
3662 |
|
bits[d] = it->second; |
3663 |
|
goto nextDimCaseSlice; |
3664 |
|
} |
3665 |
|
} |
3666 |
|
assert(false); // do crash ... too harsh maybe ? ignore it instead ? |
3667 |
|
nextDimCaseSlice: |
3668 |
|
; // noop |
3669 |
|
} |
3670 |
|
return GetDimensionRegionByBit(bits); |
3671 |
|
} |
3672 |
|
|
3673 |
|
/** |
3674 |
|
* Searches in the current Region for a dimension of the given dimension |
3675 |
|
* type and returns the precise configuration of that dimension in this |
3676 |
|
* Region. |
3677 |
|
* |
3678 |
|
* @param type - dimension type of the sought dimension |
3679 |
|
* @returns dimension definition or NULL if there is no dimension with |
3680 |
|
* sought type in this Region. |
3681 |
|
*/ |
3682 |
|
dimension_def_t* Region::GetDimensionDefinition(dimension_t type) { |
3683 |
|
for (int i = 0; i < Dimensions; ++i) |
3684 |
|
if (pDimensionDefinitions[i].dimension == type) |
3685 |
|
return &pDimensionDefinitions[i]; |
3686 |
|
return NULL; |
3687 |
|
} |
3688 |
|
|
3689 |
Region::~Region() { |
Region::~Region() { |
3690 |
for (int i = 0; i < 256; i++) { |
for (int i = 0; i < 256; i++) { |
3691 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
4971 |
return NULL; |
return NULL; |
4972 |
} |
} |
4973 |
|
|
4974 |
|
/** |
4975 |
|
* Returns the group with the given group name. |
4976 |
|
* |
4977 |
|
* Note: group names don't have to be unique in the gig format! So there |
4978 |
|
* can be multiple groups with the same name. This method will simply |
4979 |
|
* return the first group found with the given name. |
4980 |
|
* |
4981 |
|
* @param name - name of the sought group |
4982 |
|
* @returns sought group or NULL if there's no group with that name |
4983 |
|
*/ |
4984 |
|
Group* File::GetGroup(String name) { |
4985 |
|
if (!pGroups) LoadGroups(); |
4986 |
|
GroupsIterator = pGroups->begin(); |
4987 |
|
for (uint i = 0; GroupsIterator != pGroups->end(); ++GroupsIterator, ++i) |
4988 |
|
if ((*GroupsIterator)->Name == name) return *GroupsIterator; |
4989 |
|
return NULL; |
4990 |
|
} |
4991 |
|
|
4992 |
Group* File::AddGroup() { |
Group* File::AddGroup() { |
4993 |
if (!pGroups) LoadGroups(); |
if (!pGroups) LoadGroups(); |
4994 |
// there must always be at least one group |
// there must always be at least one group |