--- libgig/trunk/src/gig.cpp 2007/06/22 09:59:57 1247 +++ libgig/trunk/src/gig.cpp 2007/08/25 09:59:53 1301 @@ -1152,6 +1152,10 @@ * * Note: there is currently no support for writing compressed samples. * + * For 16 bit samples, the data in the source buffer should be + * int16_t (using native endianness). For 24 bit, the buffer + * should contain three bytes per sample, little-endian. + * * @param pBuffer - source buffer * @param SampleCount - number of sample points to write * @throws DLS::Exception if current sample size is too small @@ -1166,7 +1170,14 @@ if (pCkData->GetPos() == 0) { crc.reset(); } - unsigned long res = DLS::Sample::Write(pBuffer, SampleCount); + if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); + unsigned long res; + if (BitDepth == 24) { + res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; + } else { // 16 bit + res = Channels == 2 ? pCkData->Write(pBuffer, SampleCount << 1, 2) >> 1 + : pCkData->Write(pBuffer, SampleCount, 2); + } crc.update((unsigned char *)pBuffer, SampleCount * FrameSize); // if this is the last write, update the checksum chunk in the @@ -1531,6 +1542,28 @@ VelocityTable = 0; } + /* + * 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 + + // 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]; + } + } + /** * Apply dimension region settings to the respective RIFF chunks. You * have to call File::Save() to make changes persistent. @@ -1539,6 +1572,10 @@ * 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(); @@ -1552,6 +1589,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); + } pData = (uint8_t*) _3ewa->LoadChunkData(); // update '3ewa' chunk with DimensionRegion's current settings @@ -1598,7 +1639,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); @@ -1608,7 +1649,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); @@ -1786,23 +1827,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; @@ -2293,7 +2334,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? @@ -2315,7 +2356,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); } @@ -2442,34 +2482,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) { - uint8_t upperLimit = (z + 1) * 128.0 / pDimDef->zones - 1; + 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; } } @@ -2785,14 +2855,21 @@ if (!lart) lart = pCkInstrument->AddSubList(LIST_TYPE_LART); // make sure '3ewg' RIFF chunk exists RIFF::Chunk* _3ewg = lart->GetSubChunk(CHUNK_ID_3EWG); - if (!_3ewg) _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, 12); + if (!_3ewg) { + File* pFile = (File*) GetParent(); + + // 3ewg is bigger in gig3, as it includes the iMIDI rules + int size = (pFile->pVersion && pFile->pVersion->major == 3) ? 16416 : 12; + _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, size); + memset(_3ewg->LoadChunkData(), 0, size); + } // update '3ewg' RIFF chunk uint8_t* pData = (uint8_t*) _3ewg->LoadChunkData(); store16(&pData[0], EffectSend); 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; @@ -2903,6 +2980,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); } @@ -3010,6 +3098,7 @@ }; File::File() : DLS::File() { + *pVersion = VERSION_3; pGroups = NULL; pInfo->FixedStringLengths = FixedStringLengths; pInfo->ArchivalLocation = String(256, ' '); @@ -3078,8 +3167,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 @@ -3091,6 +3181,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() { @@ -3395,6 +3500,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(); @@ -3444,6 +3552,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 @@ -3487,6 +3605,7 @@ int totnbusedchannels = 0; int totnbregions = 0; int totnbdimregions = 0; + int totnbloops = 0; int instrumentIdx = 0; memset(&pData[48], 0, sublen - 48); @@ -3496,6 +3615,7 @@ int nbusedsamples = 0; int nbusedchannels = 0; int nbdimregions = 0; + int nbloops = 0; memset(&pData[(instrumentIdx + 1) * sublen + 48], 0, sublen - 48); @@ -3519,6 +3639,7 @@ } } } + if (d->SampleLoops) nbloops++; } nbdimregions += region->DimensionRegions; } @@ -3529,13 +3650,15 @@ store32(&pData[(instrumentIdx + 1) * sublen + 12], 1); store32(&pData[(instrumentIdx + 1) * sublen + 16], instrument->Regions); store32(&pData[(instrumentIdx + 1) * sublen + 20], nbdimregions); - // next 12 bytes unknown + store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); + // next 8 bytes unknown store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); // next 4 bytes unknown totnbregions += instrument->Regions; totnbdimregions += nbdimregions; + totnbloops += nbloops; instrumentIdx++; } // first 4 bytes unknown - sometimes 0, sometimes length of einf part @@ -3545,8 +3668,9 @@ store32(&pData[12], Instruments); store32(&pData[16], totnbregions); store32(&pData[20], totnbdimregions); - // next 12 bytes unknown - // next 4 bytes unknown, always 0? + store32(&pData[24], totnbloops); + // next 8 bytes unknown + // next 4 bytes unknown, not always 0 store32(&pData[40], pSamples->size()); // next 4 bytes unknown } @@ -3563,6 +3687,9 @@ } else if (newFile) { _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); _3crc->LoadChunkData(); + + // the order of einf and 3crc is not the same in v2 and v3 + if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); } }