--- libgig/trunk/src/gig.cpp 2007/05/20 10:11:39 1199 +++ libgig/trunk/src/gig.cpp 2007/07/29 10:51:09 1264 @@ -362,7 +362,7 @@ Manufacturer = 0; Product = 0; SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); - MIDIUnityNote = 64; + MIDIUnityNote = 60; FineTune = 0; SMPTEFormat = smpte_format_no_offset; SMPTEOffset = 0; @@ -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 @@ -1160,12 +1164,24 @@ */ unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { if (Compressed) throw gig::Exception("There is no support for writing compressed gig samples (yet)"); + + // if this is the first write in this sample, reset the + // checksum calculator 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 + // file if (pCkData->GetPos() == pCkData->GetSize()) { File* pFile = static_cast(GetParent()); pFile->SetSampleChecksum(this, crc.getValue()); @@ -1251,7 +1267,9 @@ pSample = NULL; - memcpy(&Crossfade, &SamplerOptions, 4); + if (_3ewl->GetSubChunk(CHUNK_ID_WSMP)) memcpy(&Crossfade, &SamplerOptions, 4); + else memset(&Crossfade, 0, 4); + if (!pVelocityTables) pVelocityTables = new VelocityTableMap; RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); @@ -1415,9 +1433,9 @@ LFO1ControlDepth = 0; LFO3ControlDepth = 0; EG1Attack = 0.0; - EG1Decay1 = 0.0; - EG1Sustain = 0; - EG1Release = 0.0; + EG1Decay1 = 0.005; + EG1Sustain = 1000; + EG1Release = 0.3; EG1Controller.type = eg1_ctrl_t::type_none; EG1Controller.controller_number = 0; EG1ControllerInvert = false; @@ -1432,18 +1450,18 @@ EG2ControllerReleaseInfluence = 0; LFO1Frequency = 1.0; EG2Attack = 0.0; - EG2Decay1 = 0.0; - EG2Sustain = 0; - EG2Release = 0.0; + EG2Decay1 = 0.005; + EG2Sustain = 1000; + EG2Release = 0.3; LFO2ControlDepth = 0; LFO2Frequency = 1.0; LFO2InternalDepth = 0; EG1Decay2 = 0.0; - EG1InfiniteSustain = false; - EG1PreAttack = 1000; + EG1InfiniteSustain = true; + EG1PreAttack = 0; EG2Decay2 = 0.0; - EG2InfiniteSustain = false; - EG2PreAttack = 1000; + EG2InfiniteSustain = true; + EG2PreAttack = 0; VelocityResponseCurve = curve_type_nonlinear; VelocityResponseDepth = 3; ReleaseVelocityResponseCurve = curve_type_nonlinear; @@ -1486,7 +1504,7 @@ VCFVelocityDynamicRange = 0x04; VCFVelocityCurve = curve_type_linear; VCFType = vcf_type_lowpass; - memset(DimensionUpperLimits, 0, 8); + memset(DimensionUpperLimits, 127, 8); } pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, @@ -1532,13 +1550,28 @@ * 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(); + RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); + uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); + pData[12] = Crossfade.in_start; + pData[13] = Crossfade.in_end; + pData[14] = Crossfade.out_start; + pData[15] = Crossfade.out_end; + // make sure '3ewa' chunk exists RIFF::Chunk* _3ewa = pParentList->GetSubChunk(CHUNK_ID_3EWA); if (!_3ewa) _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, 140); - uint8_t* pData = (uint8_t*) _3ewa->LoadChunkData(); + 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 @@ -2242,19 +2275,29 @@ // 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++) { - pDimensionRegions[i]->UpdateChunks(); + 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(); } - File* pFile = (File*) GetParent()->GetParent(); - const int iMaxDimensions = (pFile->pVersion && pFile->pVersion->major == 3) ? 8 : 5; - const int iMaxDimensionRegions = (pFile->pVersion && pFile->pVersion->major == 3) ? 256 : 32; + const int iMaxDimensions = version3 ? 8 : 5; + const int iMaxDimensionRegions = version3 ? 256 : 32; // make sure '3lnk' chunk exists RIFF::Chunk* _3lnk = pCkRegion->GetSubChunk(CHUNK_ID_3LNK); if (!_3lnk) { - const int _3lnkChunkSize = (pFile->pVersion && pFile->pVersion->major == 3) ? 1092 : 172; + const int _3lnkChunkSize = version3 ? 1092 : 172; _3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); @@ -2278,7 +2321,7 @@ } // update wave pool table in '3lnk' chunk - const int iWavePoolOffset = (pFile->pVersion && pFile->pVersion->major == 3) ? 68 : 44; + const int iWavePoolOffset = version3 ? 68 : 44; for (uint i = 0; i < iMaxDimensionRegions; i++) { int iWaveIndex = -1; if (i < DimensionRegions) { @@ -2433,9 +2476,22 @@ 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); + DimensionRegions++; } + // initialize the upper limits for this dimension + for (int z = 0, j = 0 ; z < pDimDef->zones ; z++, j += 1 << iCurrentBits) { + 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; + } + } + Dimensions++; // if this is a layer dimension, update 'Layers' attribute @@ -2476,6 +2532,8 @@ for (int i = iDimensionNr + 1; i < Dimensions; i++) iUpperBits += pDimensionDefinitions[i].bits; + RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); + // delete dimension regions which belong to the given dimension // (that is where the dimension's bit > 0) for (int iUpperBit = 0; iUpperBit < 1 << iUpperBits; iUpperBit++) { @@ -2484,6 +2542,8 @@ int iToDelete = iUpperBit << (pDimensionDefinitions[iDimensionNr].bits + iLowerBits) | iObsoleteBit << iLowerBits | iLowerBit; + + _3prg->DeleteSubChunk(pDimensionRegions[iToDelete]->pParentList); delete pDimensionRegions[iToDelete]; pDimensionRegions[iToDelete] = NULL; DimensionRegions--; @@ -2504,6 +2564,15 @@ } } + // remove the this dimension from the upper limits arrays + for (int j = 0 ; j < 256 && pDimensionRegions[j] ; j++) { + DimensionRegion* d = pDimensionRegions[j]; + for (int i = iDimensionNr + 1; i < Dimensions; i++) { + d->DimensionUpperLimits[i - 1] = d->DimensionUpperLimits[i]; + } + d->DimensionUpperLimits[Dimensions - 1] = 127; + } + // 'remove' dimension definition for (int i = iDimensionNr + 1; i < Dimensions; i++) { pDimensionDefinitions[i - 1] = pDimensionDefinitions[i]; @@ -2735,7 +2804,14 @@ 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); @@ -2960,6 +3036,7 @@ }; File::File() : DLS::File() { + *pVersion = VERSION_3; pGroups = NULL; pInfo->FixedStringLengths = FixedStringLengths; pInfo->ArchivalLocation = String(256, ' '); @@ -2968,6 +3045,9 @@ // order (INFO chunk will be moved to first position later) pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); + pRIFF->AddSubChunk(CHUNK_ID_DLID, 16); + + GenerateDLSID(); } File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { @@ -3164,8 +3244,10 @@ // add mandatory chunks to get the chunks in right order lstInstr->AddSubList(LIST_TYPE_INFO); + lstInstr->AddSubChunk(CHUNK_ID_DLID, 16); Instrument* pInstrument = new Instrument(this, lstInstr); + pInstrument->GenerateDLSID(); lstInstr->AddSubChunk(CHUNK_ID_INSH, 12); @@ -3222,9 +3304,14 @@ } } + /// Updates the 3crc chunk with the checksum of a sample. The + /// update is done directly to disk, as this method is called + /// after File::Save() void File::SetSampleChecksum(Sample* pSample, uint32_t crc) { RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); if (!_3crc) return; + + // get the index of the sample int iWaveIndex = -1; File::SampleList::iterator iter = pSamples->begin(); File::SampleList::iterator end = pSamples->end(); @@ -3236,6 +3323,7 @@ } if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); + // write the CRC-32 checksum to disk _3crc->SetPos(iWaveIndex * 8); uint32_t tmp = 1; _3crc->WriteUint32(&tmp); // unknown, always 1? @@ -3361,6 +3449,8 @@ void File::UpdateChunks() { bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; + b64BitWavePoolOffsets = pVersion && pVersion->major == 3; + // first update base class's chunks DLS::File::UpdateChunks(); @@ -3424,6 +3514,7 @@ int totnbusedchannels = 0; int totnbregions = 0; int totnbdimregions = 0; + int totnbloops = 0; int instrumentIdx = 0; memset(&pData[48], 0, sublen - 48); @@ -3433,6 +3524,7 @@ int nbusedsamples = 0; int nbusedchannels = 0; int nbdimregions = 0; + int nbloops = 0; memset(&pData[(instrumentIdx + 1) * sublen + 48], 0, sublen - 48); @@ -3456,6 +3548,7 @@ } } } + if (d->SampleLoops) nbloops++; } nbdimregions += region->DimensionRegions; } @@ -3466,13 +3559,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 @@ -3482,8 +3577,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 } @@ -3500,6 +3596,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); } }