--- libgig/trunk/src/gig.cpp 2007/05/12 12:39:25 1180 +++ libgig/trunk/src/gig.cpp 2007/05/27 13:54:24 1209 @@ -278,6 +278,26 @@ +// *************** 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 *************** // * @@ -344,9 +364,11 @@ SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); MIDIUnityNote = 64; FineTune = 0; + SMPTEFormat = smpte_format_no_offset; SMPTEOffset = 0; Loops = 0; LoopID = 0; + LoopType = loop_type_normal; LoopStart = 0; LoopEnd = 0; LoopFraction = 0; @@ -402,7 +424,10 @@ // make sure 'smpl' chunk exists pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); - if (!pCkSmpl) pCkSmpl = pWaveList->AddSubChunk(CHUNK_ID_SMPL, 60); + if (!pCkSmpl) { + pCkSmpl = pWaveList->AddSubChunk(CHUNK_ID_SMPL, 60); + memset(pCkSmpl->LoadChunkData(), 0, 60); + } // update 'smpl' chunk uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData(); SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); @@ -1135,7 +1160,22 @@ */ unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { if (Compressed) throw gig::Exception("There is no support for writing compressed gig samples (yet)"); - return DLS::Sample::Write(pBuffer, SampleCount); + + // 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); + 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()); + } + return res; } /** @@ -1507,7 +1547,7 @@ // update '3ewa' chunk with DimensionRegion's current settings - const uint32_t chunksize = _3ewa->GetSize(); + const uint32_t chunksize = _3ewa->GetNewSize(); store32(&pData[0], chunksize); // unknown, always chunk size? const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); @@ -1991,6 +2031,7 @@ default: throw gig::Exception("leverage controller number is not supported by the gig format"); } + break; default: throw gig::Exception("Unknown leverage controller type."); } @@ -2127,8 +2168,8 @@ for (int i = 0; i < dimensionBits; i++) { dimension_t dimension = static_cast(_3lnk->ReadUint8()); uint8_t bits = _3lnk->ReadUint8(); - _3lnk->ReadUint8(); // probably the position of the dimension - _3lnk->ReadUint8(); // unknown + _3lnk->ReadUint8(); // bit position of the dimension (bits[0] + bits[1] + ... + bits[i-1]) + _3lnk->ReadUint8(); // (1 << bit position of next dimension) - (1 << bit position of this dimension) uint8_t zones = _3lnk->ReadUint8(); // new for v3: number of zones doesn't have to be == pow(2,bits) if (dimension == dimension_none) { // inactive dimension pDimensionDefinitions[i].dimension = dimension_none; @@ -2170,6 +2211,11 @@ GetSample(); // load global region sample reference } else { DimensionRegions = 0; + for (int i = 0 ; i < 8 ; i++) { + pDimensionDefinitions[i].dimension = dimension_none; + pDimensionDefinitions[i].bits = 0; + pDimensionDefinitions[i].zones = 0; + } } // make sure there is at least one dimension region @@ -2215,17 +2261,25 @@ if (!_3lnk) { const int _3lnkChunkSize = (pFile->pVersion && pFile->pVersion->major == 3) ? 1092 : 172; _3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); + memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); + + // move 3prg to last position + pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0); } // update dimension definitions in '3lnk' chunk uint8_t* pData = (uint8_t*) _3lnk->LoadChunkData(); store32(&pData[0], DimensionRegions); + int shift = 0; for (int i = 0; i < iMaxDimensions; i++) { pData[4 + i * 8] = (uint8_t) pDimensionDefinitions[i].dimension; pData[5 + i * 8] = pDimensionDefinitions[i].bits; - // next 2 bytes unknown + pData[6 + i * 8] = shift; + pData[7 + i * 8] = (1 << (shift + pDimensionDefinitions[i].bits)) - (1 << shift); pData[8 + i * 8] = pDimensionDefinitions[i].zones; - // next 3 bytes unknown + // next 3 bytes unknown, always zero? + + shift += pDimensionDefinitions[i].bits; } // update wave pool table in '3lnk' chunk @@ -2381,7 +2435,8 @@ // 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* pNewDimRgnListChunk = pCkRegion->AddSubList(LIST_TYPE_3EWL); + RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); + RIFF::List* pNewDimRgnListChunk = _3prg->AddSubList(LIST_TYPE_3EWL); pDimensionRegions[i] = new DimensionRegion(pNewDimRgnListChunk); DimensionRegions++; } @@ -2603,6 +2658,13 @@ // Initialization for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; + EffectSend = 0; + Attenuation = 0; + FineTune = 0; + PitchbendRange = 0; + PianoReleaseMode = false; + DimensionKeyRange.low = 0; + DimensionKeyRange.high = 0; // Loading RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); @@ -2790,9 +2852,12 @@ void Group::UpdateChunks() { // make sure <3gri> and <3gnl> list chunks exist RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); - if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); + if (!_3gri) { + _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); + pFile->pRIFF->MoveSubChunk(_3gri, pFile->pRIFF->GetSubChunk(CHUNK_ID_PTBL)); + } RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); - if (!_3gnl) _3gnl = pFile->pRIFF->AddSubList(LIST_TYPE_3GNL); + if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); // 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); } @@ -2868,6 +2933,16 @@ // *************** File *************** // * + // File version 2.0, 1998-06-28 + const DLS::version_t File::VERSION_2 = { + 0, 2, 19980628 & 0xffff, 19980628 >> 16 + }; + + // File version 3.0, 2003-03-31 + const DLS::version_t File::VERSION_3 = { + 0, 3, 20030331 & 0xffff, 20030331 >> 16 + }; + const DLS::Info::FixedStringLength File::FixedStringLengths[] = { { CHUNK_ID_IARL, 256 }, { CHUNK_ID_IART, 128 }, @@ -2892,6 +2967,15 @@ File::File() : DLS::File() { pGroups = NULL; pInfo->FixedStringLengths = FixedStringLengths; + pInfo->ArchivalLocation = String(256, ' '); + + // add some mandatory chunks to get the file chunks in right + // 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) { @@ -2938,6 +3022,11 @@ // create new Sample object and its respective 'wave' list chunk RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); + + // add mandatory chunks to get the chunks in right order + wave->AddSubChunk(CHUNK_ID_FMT, 16); + wave->AddSubList(LIST_TYPE_INFO); + pSamples->push_back(pSample); return pSample; } @@ -3080,7 +3169,19 @@ __ensureMandatoryChunksExist(); RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); + + // 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); + + // this string is needed for the gig to be loadable in GSt: + pInstrument->pInfo->Software = "Endless Wave"; + pInstruments->push_back(pInstrument); return pInstrument; } @@ -3131,6 +3232,32 @@ } } + /// 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(); + for (int index = 0; iter != end; ++iter, ++index) { + if (*iter == pSample) { + iWaveIndex = index; + break; + } + } + 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? + _3crc->WriteUint32(&crc); + } + Group* File::GetFirstGroup() { if (!pGroups) LoadGroups(); // there must always be at least one group @@ -3248,9 +3375,21 @@ * @throws Exception - on errors */ void File::UpdateChunks() { + bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; + // first update base class's chunks DLS::File::UpdateChunks(); + if (newFile) { + // INFO was added by Resource::UpdateChunks - make sure it + // is placed first in file + RIFF::Chunk* info = pRIFF->GetSubList(LIST_TYPE_INFO); + RIFF::Chunk* first = pRIFF->GetFirstSubChunk(); + if (first != info) { + pRIFF->MoveSubChunk(info, first); + } + } + // update group's chunks if (pGroups) { std::list::iterator iter = pGroups->begin(); @@ -3259,6 +3398,125 @@ (*iter)->UpdateChunks(); } } + + // update einf chunk + + // The einf chunk contains statistics about the gig file, such + // as the number of regions and samples used by each + // instrument. It is divided in equally sized parts, where the + // first part contains information about the whole gig file, + // and the rest of the parts map to each instrument in the + // file. + // + // At the end of each part there is a bit map of each sample + // in the file, where a set bit means that the sample is used + // by the file/instrument. + // + // Note that there are several fields with unknown use. These + // are set to zero. + + int sublen = pSamples->size() / 8 + 49; + int einfSize = (Instruments + 1) * sublen; + + RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); + if (einf) { + if (einf->GetSize() != einfSize) { + einf->Resize(einfSize); + memset(einf->LoadChunkData(), 0, einfSize); + } + } else if (newFile) { + einf = pRIFF->AddSubChunk(CHUNK_ID_EINF, einfSize); + } + if (einf) { + uint8_t* pData = (uint8_t*) einf->LoadChunkData(); + + std::map sampleMap; + int sampleIdx = 0; + for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { + sampleMap[pSample] = sampleIdx++; + } + + int totnbusedsamples = 0; + int totnbusedchannels = 0; + int totnbregions = 0; + int totnbdimregions = 0; + int instrumentIdx = 0; + + memset(&pData[48], 0, sublen - 48); + + for (Instrument* instrument = GetFirstInstrument() ; instrument ; + instrument = GetNextInstrument()) { + int nbusedsamples = 0; + int nbusedchannels = 0; + int nbdimregions = 0; + + memset(&pData[(instrumentIdx + 1) * sublen + 48], 0, sublen - 48); + + for (Region* region = instrument->GetFirstRegion() ; region ; + region = instrument->GetNextRegion()) { + for (int i = 0 ; i < region->DimensionRegions ; i++) { + gig::DimensionRegion *d = region->pDimensionRegions[i]; + if (d->pSample) { + int sampleIdx = sampleMap[d->pSample]; + int byte = 48 + sampleIdx / 8; + int bit = 1 << (sampleIdx & 7); + if ((pData[(instrumentIdx + 1) * sublen + byte] & bit) == 0) { + pData[(instrumentIdx + 1) * sublen + byte] |= bit; + nbusedsamples++; + nbusedchannels += d->pSample->Channels; + + if ((pData[byte] & bit) == 0) { + pData[byte] |= bit; + totnbusedsamples++; + totnbusedchannels += d->pSample->Channels; + } + } + } + } + nbdimregions += region->DimensionRegions; + } + // first 4 bytes unknown - sometimes 0, sometimes length of einf part + // store32(&pData[(instrumentIdx + 1) * sublen], sublen); + store32(&pData[(instrumentIdx + 1) * sublen + 4], nbusedchannels); + store32(&pData[(instrumentIdx + 1) * sublen + 8], nbusedsamples); + 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 + 36], instrumentIdx); + store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); + // next 4 bytes unknown + + totnbregions += instrument->Regions; + totnbdimregions += nbdimregions; + instrumentIdx++; + } + // first 4 bytes unknown - sometimes 0, sometimes length of einf part + // store32(&pData[0], sublen); + store32(&pData[4], totnbusedchannels); + store32(&pData[8], totnbusedsamples); + store32(&pData[12], Instruments); + store32(&pData[16], totnbregions); + store32(&pData[20], totnbdimregions); + // next 12 bytes unknown + // next 4 bytes unknown, always 0? + store32(&pData[40], pSamples->size()); + // next 4 bytes unknown + } + + // update 3crc chunk + + // The 3crc chunk contains CRC-32 checksums for the + // samples. The actual checksum values will be filled in + // later, by Sample::Write. + + RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); + if (_3crc) { + _3crc->Resize(pSamples->size() * 8); + } else if (newFile) { + _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); + _3crc->LoadChunkData(); + } }