--- libgig/trunk/src/gig.cpp 2007/07/29 10:51:09 1264 +++ libgig/trunk/src/gig.cpp 2007/10/04 18:57:10 1381 @@ -255,6 +255,69 @@ +// *************** Internal CRC-32 (Cyclic Redundancy Check) functions *************** +// * + + static uint32_t* __initCRCTable() { + static uint32_t res[256]; + + for (int i = 0 ; i < 256 ; i++) { + uint32_t c = i; + for (int j = 0 ; j < 8 ; j++) { + c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1; + } + res[i] = c; + } + return res; + } + + static const uint32_t* __CRCTable = __initCRCTable(); + + /** + * Initialize a CRC variable. + * + * @param crc - variable to be initialized + */ + inline static void __resetCRC(uint32_t& crc) { + crc = 0xffffffff; + } + + /** + * Used to calculate checksums of the sample data in a gig file. The + * checksums are stored in the 3crc chunk of the gig file and + * automatically updated when a sample is written with Sample::Write(). + * + * One should call __resetCRC() to initialize the CRC variable to be + * used before calling this function the first time. + * + * After initializing the CRC variable one can call this function + * arbitrary times, i.e. to split the overall CRC calculation into + * steps. + * + * Once the whole data was processed by __calculateCRC(), one should + * call __encodeCRC() to get the final CRC result. + * + * @param buf - pointer to data the CRC shall be calculated of + * @param bufSize - size of the data to be processed + * @param crc - variable the CRC sum shall be stored to + */ + static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) { + for (int i = 0 ; i < bufSize ; i++) { + crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); + } + } + + /** + * Returns the final CRC result. + * + * @param crc - variable previously passed to __calculateCRC() + */ + inline static uint32_t __encodeCRC(const uint32_t& crc) { + return crc ^ 0xffffffff; + } + + + // *************** Other Internal functions *************** // * @@ -278,26 +341,6 @@ -// *************** CRC *************** -// * - - const uint32_t* CRC::table(initTable()); - - uint32_t* CRC::initTable() { - uint32_t* res = new uint32_t[256]; - - for (int i = 0 ; i < 256 ; i++) { - uint32_t c = i; - for (int j = 0 ; j < 8 ; j++) { - c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1; - } - res[i] = c; - } - return res; - } - - - // *************** Sample *************** // * @@ -331,6 +374,8 @@ Instances++; FileNo = fileNo; + __resetCRC(crc); + pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); if (pCk3gix) { uint16_t iSampleGroup = pCk3gix->ReadInt16(); @@ -1168,7 +1213,7 @@ // if this is the first write in this sample, reset the // checksum calculator if (pCkData->GetPos() == 0) { - crc.reset(); + __resetCRC(crc); } if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); unsigned long res; @@ -1178,13 +1223,13 @@ res = Channels == 2 ? pCkData->Write(pBuffer, SampleCount << 1, 2) >> 1 : pCkData->Write(pBuffer, SampleCount, 2); } - crc.update((unsigned char *)pBuffer, SampleCount * FrameSize); + __calculateCRC((unsigned char *)pBuffer, SampleCount * FrameSize, crc); // if this is the last write, update the checksum chunk in the // file if (pCkData->GetPos() == pCkData->GetSize()) { File* pFile = static_cast(GetParent()); - pFile->SetSampleChecksum(this, crc.getValue()); + pFile->SetSampleChecksum(this, __encodeCRC(crc)); } return res; } @@ -1262,10 +1307,11 @@ uint DimensionRegion::Instances = 0; DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; - DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { + DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { Instances++; pSample = NULL; + pRegion = pParent; if (_3ewl->GetSubChunk(CHUNK_ID_WSMP)) memcpy(&Crossfade, &SamplerOptions, 4); else memset(&Crossfade, 0, 4); @@ -1511,35 +1557,49 @@ VelocityResponseDepth, VelocityResponseCurveScaling); - curve_type_t curveType = ReleaseVelocityResponseCurve; - uint8_t depth = ReleaseVelocityResponseDepth; + pVelocityReleaseTable = GetReleaseVelocityTable( + ReleaseVelocityResponseCurve, + ReleaseVelocityResponseDepth + ); + + pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, + VCFVelocityDynamicRange, + VCFVelocityScale, + VCFCutoffController); - // this models a strange behaviour or bug in GSt: two of the - // velocity response curves for release time are not used even - // if specified, instead another curve is chosen. - if ((curveType == curve_type_nonlinear && depth == 0) || - (curveType == curve_type_special && depth == 4)) { - curveType = curve_type_nonlinear; - depth = 3; - } - pVelocityReleaseTable = GetVelocityTable(curveType, depth, 0); + SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); + VelocityTable = 0; + } - curveType = VCFVelocityCurve; - depth = VCFVelocityDynamicRange; + /* + * Constructs a DimensionRegion by copying all parameters from + * another DimensionRegion + */ + DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) { + Instances++; + *this = src; // default memberwise shallow copy of all parameters + pParentList = _3ewl; // restore the chunk pointer - // even stranger GSt: two of the velocity response curves for - // filter cutoff are not used, instead another special curve - // is chosen. This curve is not used anywhere else. - if ((curveType == curve_type_nonlinear && depth == 0) || - (curveType == curve_type_special && depth == 4)) { - curveType = curve_type_special; - depth = 5; + // deep copy of owned structures + if (src.VelocityTable) { + VelocityTable = new uint8_t[128]; + for (int k = 0 ; k < 128 ; k++) + VelocityTable[k] = src.VelocityTable[k]; + } + if (src.pSampleLoops) { + pSampleLoops = new DLS::sample_loop_t[src.SampleLoops]; + for (int k = 0 ; k < src.SampleLoops ; k++) + pSampleLoops[k] = src.pSampleLoops[k]; } - pVelocityCutoffTable = GetVelocityTable(curveType, depth, - VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0); + } + /** + * Updates the respective member variable and updates @c SampleAttenuation + * which depends on this value. + */ + void DimensionRegion::SetGain(int32_t gain) { + DLS::Sampler::SetGain(gain); SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); - VelocityTable = 0; } /** @@ -1550,10 +1610,6 @@ * It will be called automatically when File::Save() was called. */ void DimensionRegion::UpdateChunks() { - // check if wsmp is going to be created by - // DLS::Sampler::UpdateChunks - bool wsmp_created = !pParentList->GetSubChunk(CHUNK_ID_WSMP); - // first update base class's chunk DLS::Sampler::UpdateChunks(); @@ -1566,10 +1622,10 @@ // make sure '3ewa' chunk exists RIFF::Chunk* _3ewa = pParentList->GetSubChunk(CHUNK_ID_3EWA); - if (!_3ewa) _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, 140); - else if (wsmp_created) { - // make sure the chunk order is: wsmp, 3ewa - pParentList->MoveSubChunk(_3ewa, 0); + if (!_3ewa) { + File* pFile = (File*) GetParent()->GetParent()->GetParent(); + bool version3 = pFile->pVersion && pFile->pVersion->major == 3; + _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, version3 ? 148 : 140); } pData = (uint8_t*) _3ewa->LoadChunkData(); @@ -1617,7 +1673,7 @@ pData[44] = eg1ctl; const uint8_t eg1ctrloptions = - (EG1ControllerInvert) ? 0x01 : 0x00 | + (EG1ControllerInvert ? 0x01 : 0x00) | GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG1ControllerAttackInfluence) | GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG1ControllerDecayInfluence) | GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG1ControllerReleaseInfluence); @@ -1627,7 +1683,7 @@ pData[46] = eg2ctl; const uint8_t eg2ctrloptions = - (EG2ControllerInvert) ? 0x01 : 0x00 | + (EG2ControllerInvert ? 0x01 : 0x00) | GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG2ControllerAttackInfluence) | GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG2ControllerDecayInfluence) | GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG2ControllerReleaseInfluence); @@ -1805,23 +1861,23 @@ const uint8_t eg1hold = (EG1Hold) ? 0x80 : 0x00; // bit 7 pData[131] = eg1hold; - const uint8_t vcfcutoff = (VCFEnabled) ? 0x80 : 0x00 | /* bit 7 */ + const uint8_t vcfcutoff = (VCFEnabled ? 0x80 : 0x00) | /* bit 7 */ (VCFCutoff & 0x7f); /* lower 7 bits */ pData[132] = vcfcutoff; pData[133] = VCFCutoffController; - const uint8_t vcfvelscale = (VCFCutoffControllerInvert) ? 0x80 : 0x00 | /* bit 7 */ + const uint8_t vcfvelscale = (VCFCutoffControllerInvert ? 0x80 : 0x00) | /* bit 7 */ (VCFVelocityScale & 0x7f); /* lower 7 bits */ pData[134] = vcfvelscale; // next byte unknown - const uint8_t vcfresonance = (VCFResonanceDynamic) ? 0x00 : 0x80 | /* bit 7 */ + const uint8_t vcfresonance = (VCFResonanceDynamic ? 0x00 : 0x80) | /* bit 7 */ (VCFResonance & 0x7f); /* lower 7 bits */ pData[136] = vcfresonance; - const uint8_t vcfbreakpoint = (VCFKeyboardTracking) ? 0x80 : 0x00 | /* bit 7 */ + const uint8_t vcfbreakpoint = (VCFKeyboardTracking ? 0x80 : 0x00) | /* bit 7 */ (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */ pData[137] = vcfbreakpoint; @@ -1837,6 +1893,40 @@ } } + double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) { + curve_type_t curveType = releaseVelocityResponseCurve; + uint8_t depth = releaseVelocityResponseDepth; + // this models a strange behaviour or bug in GSt: two of the + // velocity response curves for release time are not used even + // if specified, instead another curve is chosen. + if ((curveType == curve_type_nonlinear && depth == 0) || + (curveType == curve_type_special && depth == 4)) { + curveType = curve_type_nonlinear; + depth = 3; + } + return GetVelocityTable(curveType, depth, 0); + } + + double* DimensionRegion::GetCutoffVelocityTable(curve_type_t vcfVelocityCurve, + uint8_t vcfVelocityDynamicRange, + uint8_t vcfVelocityScale, + vcf_cutoff_ctrl_t vcfCutoffController) + { + curve_type_t curveType = vcfVelocityCurve; + uint8_t depth = vcfVelocityDynamicRange; + // even stranger GSt: two of the velocity response curves for + // filter cutoff are not used, instead another special curve + // is chosen. This curve is not used anywhere else. + if ((curveType == curve_type_nonlinear && depth == 0) || + (curveType == curve_type_special && depth == 4)) { + curveType = curve_type_special; + depth = 5; + } + return GetVelocityTable(curveType, depth, + (vcfCutoffController <= vcf_cutoff_ctrl_none2) + ? vcfVelocityScale : 0); + } + // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { @@ -1852,6 +1942,10 @@ return table; } + Region* DimensionRegion::GetParent() const { + return pRegion; + } + leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { leverage_ctrl_t decodedcontroller; switch (EncodedController) { @@ -2105,6 +2199,96 @@ return pVelocityCutoffTable[MIDIKeyVelocity]; } + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVelocityResponseCurve(curve_type_t curve) { + pVelocityAttenuationTable = + GetVelocityTable( + curve, VelocityResponseDepth, VelocityResponseCurveScaling + ); + VelocityResponseCurve = curve; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVelocityResponseDepth(uint8_t depth) { + pVelocityAttenuationTable = + GetVelocityTable( + VelocityResponseCurve, depth, VelocityResponseCurveScaling + ); + VelocityResponseDepth = depth; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVelocityResponseCurveScaling(uint8_t scaling) { + pVelocityAttenuationTable = + GetVelocityTable( + VelocityResponseCurve, VelocityResponseDepth, scaling + ); + VelocityResponseCurveScaling = scaling; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetReleaseVelocityResponseCurve(curve_type_t curve) { + pVelocityReleaseTable = GetReleaseVelocityTable(curve, ReleaseVelocityResponseDepth); + ReleaseVelocityResponseCurve = curve; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetReleaseVelocityResponseDepth(uint8_t depth) { + pVelocityReleaseTable = GetReleaseVelocityTable(ReleaseVelocityResponseCurve, depth); + ReleaseVelocityResponseDepth = depth; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVCFCutoffController(vcf_cutoff_ctrl_t controller) { + pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, VCFVelocityDynamicRange, VCFVelocityScale, controller); + VCFCutoffController = controller; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVCFVelocityCurve(curve_type_t curve) { + pVelocityCutoffTable = GetCutoffVelocityTable(curve, VCFVelocityDynamicRange, VCFVelocityScale, VCFCutoffController); + VCFVelocityCurve = curve; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVCFVelocityDynamicRange(uint8_t range) { + pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, range, VCFVelocityScale, VCFCutoffController); + VCFVelocityDynamicRange = range; + } + + /** + * Updates the respective member variable and the lookup table / cache + * that depends on this value. + */ + void DimensionRegion::SetVCFVelocityScale(uint8_t scaling) { + pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, VCFVelocityDynamicRange, scaling, VCFCutoffController); + VCFVelocityScale = scaling; + } + double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { // line-segment approximations of the 15 velocity curves @@ -2251,7 +2435,7 @@ RIFF::List* _3prg = rgnList->GetSubList(LIST_TYPE_3PRG); if (!_3prg) _3prg = rgnList->AddSubList(LIST_TYPE_3PRG); RIFF::List* _3ewl = _3prg->AddSubList(LIST_TYPE_3EWL); - pDimensionRegions[0] = new DimensionRegion(_3ewl); + pDimensionRegions[0] = new DimensionRegion(this, _3ewl); DimensionRegions = 1; } } @@ -2275,22 +2459,13 @@ // first update base class's chunks DLS::Region::UpdateChunks(); - File* pFile = (File*) GetParent()->GetParent(); - bool version3 = pFile->pVersion && pFile->pVersion->major == 3; - // update dimension region's chunks for (int i = 0; i < DimensionRegions; i++) { - DimensionRegion* d = pDimensionRegions[i]; - - // make sure '3ewa' chunk exists (we need to this before - // calling DimensionRegion::UpdateChunks, as - // DimensionRegion doesn't know which file version it is) - RIFF::Chunk* _3ewa = d->pParentList->GetSubChunk(CHUNK_ID_3EWA); - if (!_3ewa) d->pParentList->AddSubChunk(CHUNK_ID_3EWA, version3 ? 148 : 140); - - d->UpdateChunks(); + pDimensionRegions[i]->UpdateChunks(); } + File* pFile = (File*) GetParent()->GetParent(); + bool version3 = pFile->pVersion && pFile->pVersion->major == 3; const int iMaxDimensions = version3 ? 8 : 5; const int iMaxDimensionRegions = version3 ? 256 : 32; @@ -2312,7 +2487,7 @@ for (int i = 0; i < iMaxDimensions; i++) { pData[4 + i * 8] = (uint8_t) pDimensionDefinitions[i].dimension; pData[5 + i * 8] = pDimensionDefinitions[i].bits; - pData[6 + i * 8] = shift; + pData[6 + i * 8] = pDimensionDefinitions[i].dimension == dimension_none ? 0 : shift; pData[7 + i * 8] = (1 << (shift + pDimensionDefinitions[i].bits)) - (1 << shift); pData[8 + i * 8] = pDimensionDefinitions[i].zones; // next 3 bytes unknown, always zero? @@ -2334,7 +2509,6 @@ break; } } - if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample"); } store32(&pData[iWavePoolOffset + i * 4], iWaveIndex); } @@ -2347,7 +2521,7 @@ RIFF::List* _3ewl = _3prg->GetFirstSubList(); while (_3ewl) { if (_3ewl->GetListType() == LIST_TYPE_3EWL) { - pDimensionRegions[dimensionRegionNr] = new DimensionRegion(_3ewl); + pDimensionRegions[dimensionRegionNr] = new DimensionRegion(this, _3ewl); dimensionRegionNr++; } _3ewl = _3prg->GetNextSubList(); @@ -2356,6 +2530,13 @@ } } + void Region::SetKeyRange(uint16_t Low, uint16_t High) { + // update KeyRange struct and make sure regions are in correct order + DLS::Region::SetKeyRange(Low, High); + // update Region key table for fast lookup + ((gig::Instrument*)GetParent())->UpdateRegionKeyTable(); + } + void Region::UpdateVelocityTable() { // get velocity dimension's index int veldim = -1; @@ -2461,34 +2642,64 @@ if (pDimensionDefinitions[i].dimension == pDimDef->dimension) throw gig::Exception("Could not add new dimension, there is already a dimension of the same type"); + // pos is where the new dimension should be placed, normally + // last in list, except for the samplechannel dimension which + // has to be first in list + int pos = pDimDef->dimension == dimension_samplechannel ? 0 : Dimensions; + int bitpos = 0; + for (int i = 0 ; i < pos ; i++) + bitpos += pDimensionDefinitions[i].bits; + + // make room for the new dimension + for (int i = Dimensions ; i > pos ; i--) pDimensionDefinitions[i] = pDimensionDefinitions[i - 1]; + for (int i = 0 ; i < (1 << iCurrentBits) ; i++) { + for (int j = Dimensions ; j > pos ; j--) { + pDimensionRegions[i]->DimensionUpperLimits[j] = + pDimensionRegions[i]->DimensionUpperLimits[j - 1]; + } + } + // assign definition of new dimension - pDimensionDefinitions[Dimensions] = *pDimDef; + pDimensionDefinitions[pos] = *pDimDef; // auto correct certain dimension definition fields (where possible) - pDimensionDefinitions[Dimensions].split_type = - __resolveSplitType(pDimensionDefinitions[Dimensions].dimension); - pDimensionDefinitions[Dimensions].zone_size = - __resolveZoneSize(pDimensionDefinitions[Dimensions]); - - // create new dimension region(s) for this new dimension - for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) { - //TODO: maybe we should copy existing dimension regions if possible instead of simply creating new ones with default values - RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); - RIFF::List* pNewDimRgnListChunk = _3prg->AddSubList(LIST_TYPE_3EWL); - pDimensionRegions[i] = new DimensionRegion(pNewDimRgnListChunk); - - // copy the upper limits for the other dimensions - memcpy(pDimensionRegions[i]->DimensionUpperLimits, - pDimensionRegions[i & ((1 << iCurrentBits) - 1)]->DimensionUpperLimits, 8); + pDimensionDefinitions[pos].split_type = + __resolveSplitType(pDimensionDefinitions[pos].dimension); + pDimensionDefinitions[pos].zone_size = + __resolveZoneSize(pDimensionDefinitions[pos]); + + // create new dimension region(s) for this new dimension, and make + // sure that the dimension regions are placed correctly in both the + // RIFF list and the pDimensionRegions array + RIFF::Chunk* moveTo = NULL; + RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); + for (int i = (1 << iCurrentBits) - (1 << bitpos) ; i >= 0 ; i -= (1 << bitpos)) { + for (int k = 0 ; k < (1 << bitpos) ; k++) { + pDimensionRegions[(i << pDimDef->bits) + k] = pDimensionRegions[i + k]; + } + for (int j = 1 ; j < (1 << pDimDef->bits) ; j++) { + for (int k = 0 ; k < (1 << bitpos) ; k++) { + RIFF::List* pNewDimRgnListChunk = _3prg->AddSubList(LIST_TYPE_3EWL); + if (moveTo) _3prg->MoveSubChunk(pNewDimRgnListChunk, moveTo); + // create a new dimension region and copy all parameter values from + // an existing dimension region + pDimensionRegions[(i << pDimDef->bits) + (j << bitpos) + k] = + new DimensionRegion(pNewDimRgnListChunk, *pDimensionRegions[i + k]); - DimensionRegions++; + DimensionRegions++; + } + } + moveTo = pDimensionRegions[i]->pParentList; } // initialize the upper limits for this dimension - for (int z = 0, j = 0 ; z < pDimDef->zones ; z++, j += 1 << iCurrentBits) { + int mask = (1 << bitpos) - 1; + for (int z = 0 ; z < pDimDef->zones ; z++) { uint8_t upperLimit = uint8_t((z + 1) * 128.0 / pDimDef->zones - 1); for (int i = 0 ; i < 1 << iCurrentBits ; i++) { - pDimensionRegions[j + i]->DimensionUpperLimits[Dimensions] = upperLimit; + pDimensionRegions[((i & ~mask) << pDimDef->bits) | + (z << bitpos) | + (i & mask)]->DimensionUpperLimits[pos] = upperLimit; } } @@ -2765,6 +2976,7 @@ } void Instrument::UpdateRegionKeyTable() { + for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; RegionList::iterator iter = pRegions->begin(); RegionList::iterator end = pRegions->end(); for (; iter != end; ++iter) { @@ -2818,7 +3030,7 @@ store32(&pData[2], Attenuation); store16(&pData[6], FineTune); store16(&pData[8], PitchbendRange); - const uint8_t dimkeystart = (PianoReleaseMode) ? 0x01 : 0x00 | + const uint8_t dimkeystart = (PianoReleaseMode ? 0x01 : 0x00) | DimensionKeyRange.low << 1; pData[10] = dimkeystart; pData[11] = DimensionKeyRange.high; @@ -2832,7 +3044,7 @@ * there is no Region defined for the given \a Key */ Region* Instrument::GetRegion(unsigned int Key) { - if (!pRegions || !pRegions->size() || Key > 127) return NULL; + if (!pRegions || pRegions->empty() || Key > 127) return NULL; return RegionKeyTable[Key]; /*for (int i = 0; i < Regions; i++) { @@ -2929,6 +3141,17 @@ } RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); + + if (!pNameChunk && pFile->pVersion && pFile->pVersion->major == 3) { + // v3 has a fixed list of 128 strings, find a free one + for (RIFF::Chunk* ck = _3gnl->GetFirstSubChunk() ; ck ; ck = _3gnl->GetNextSubChunk()) { + if (strcmp(static_cast(ck->LoadChunkData()), "") == 0) { + pNameChunk = ck; + break; + } + } + } + // now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk ::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64); } @@ -3105,8 +3328,9 @@ /** @brief Delete a sample. * - * This will delete the given Sample object from the gig file. You have - * to call Save() to make this persistent to the file. + * This will delete the given Sample object from the gig file. Any + * references to this sample from Regions and DimensionRegions will be + * removed. You have to call Save() to make this persistent to the file. * * @param pSample - sample to delete * @throws gig::Exception if given sample could not be found @@ -3118,6 +3342,21 @@ if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation pSamples->erase(iter); delete pSample; + + // remove all references to the sample + for (Instrument* instrument = GetFirstInstrument() ; instrument ; + instrument = GetNextInstrument()) { + for (Region* region = instrument->GetFirstRegion() ; region ; + region = instrument->GetNextRegion()) { + + if (region->GetSample() == pSample) region->SetSample(NULL); + + for (int i = 0 ; i < region->DimensionRegions ; i++) { + gig::DimensionRegion *d = region->pDimensionRegions[i]; + if (d->pSample == pSample) d->pSample = NULL; + } + } + } } void File::LoadSamples() { @@ -3422,6 +3661,9 @@ RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); while (ck) { if (ck->GetChunkID() == CHUNK_ID_3GNM) { + if (pVersion && pVersion->major == 3 && + strcmp(static_cast(ck->LoadChunkData()), "") == 0) break; + pGroups->push_back(new Group(this, ck)); } ck = lst3gnl->GetNextSubChunk(); @@ -3471,6 +3713,16 @@ for (; iter != end; ++iter) { (*iter)->UpdateChunks(); } + + // v3: make sure the file has 128 3gnm chunks + if (pVersion && pVersion->major == 3) { + RIFF::List* _3gnl = pRIFF->GetSubList(LIST_TYPE_3GRI)->GetSubList(LIST_TYPE_3GNL); + RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk(); + for (int i = 0 ; i < 128 ; i++) { + if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64); + if (_3gnm) _3gnm = _3gnl->GetNextSubChunk(); + } + } } // update einf chunk