--- libgig/trunk/src/gig.cpp 2006/10/24 22:24:45 929 +++ libgig/trunk/src/gig.cpp 2007/03/18 19:38:47 1106 @@ -1,8 +1,8 @@ /*************************************************************************** * * - * libgig - C++ cross-platform Gigasampler format file loader library * + * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2006 by Christian Schoenebeck * + * Copyright (C) 2003-2007 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -286,11 +286,10 @@ pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); if (pCk3gix) { uint16_t iSampleGroup = pCk3gix->ReadInt16(); - // caution: sample groups in .gig files are indexed (1..n) whereas Groups in libgig (0..n-1) - pGroup = pFile->GetGroup(iSampleGroup - 1); + pGroup = pFile->GetGroup(iSampleGroup); } else { // '3gix' chunk missing - // not assigned to a group by default - pGroup = NULL; + // by default assigned to that mandatory "Default Group" + pGroup = pFile->GetGroup(0); } pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); @@ -365,7 +364,7 @@ * Usually there is absolutely no need to call this method explicitly. * It will be called automatically when File::Save() was called. * - * @throws DLS::Exception if FormatTag != WAVE_FORMAT_PCM or no sample data + * @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data * was provided yet * @throws gig::Exception if there is any invalid sample setting */ @@ -401,13 +400,12 @@ pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); // determine appropriate sample group index (to be stored in chunk) - uint16_t iSampleGroup = 0; // no sample group by default + uint16_t iSampleGroup = 0; // 0 refers to default sample group File* pFile = static_cast(pParent); if (pFile->pGroups) { std::list::iterator iter = pFile->pGroups->begin(); std::list::iterator end = pFile->pGroups->end(); - // caution: sample groups in .gig files are indexed (1..n) whereas Groups in libgig (0..n-1) - for (int i = 1; iter != end; i++, iter++) { + for (int i = 0; iter != end; i++, iter++) { if (*iter == pGroup) { iSampleGroup = i; break; // found @@ -637,13 +635,13 @@ * enlarged samples before calling File::Save() as this might exceed the * current sample's boundary! * - * Also note: only WAVE_FORMAT_PCM is currently supported, that is - * FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with + * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is + * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with * other formats will fail! * * @param iNewSize - new sample wave data size in sample points (must be * greater than zero) - * @throws DLS::Excecption if FormatTag != WAVE_FORMAT_PCM + * @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM * or if \a iNewSize is less than 1 * @throws gig::Exception if existing sample is compressed * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, @@ -1154,6 +1152,18 @@ } } + /** + * Returns pointer to the Group this Sample belongs to. In the .gig + * format a sample always belongs to one group. If it wasn't explicitly + * assigned to a certain group, it will be automatically assigned to a + * default group. + * + * @returns Sample's Group (never NULL) + */ + Group* Sample::GetGroup() const { + return pGroup; + } + Sample::~Sample() { Instances--; if (!Instances && InternalDecompressionBuffer.Size) { @@ -1328,6 +1338,11 @@ if (lfo3ctrl & 0x40) // bit 6 VCFType = vcf_type_lowpassturbo; } + if (_3ewa->RemainingBytes() >= 8) { + _3ewa->Read(DimensionUpperLimits, 1, 8); + } else { + memset(DimensionUpperLimits, 0, 8); + } } else { // '3ewa' chunk does not exist yet // use default values LFO3Frequency = 1.0; @@ -1408,6 +1423,7 @@ VCFVelocityDynamicRange = 0x04; VCFVelocityCurve = curve_type_linear; VCFType = vcf_type_lowpass; + memset(DimensionUpperLimits, 0, 8); } pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, @@ -1463,8 +1479,8 @@ // update '3ewa' chunk with DimensionRegion's current settings - const uint32_t unknown = _3ewa->GetSize(); // unknown, always chunk size ? - memcpy(&pData[0], &unknown, 4); + const uint32_t chunksize = _3ewa->GetSize(); + memcpy(&pData[0], &chunksize, 4); // unknown, always chunk size? const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); memcpy(&pData[4], &lfo3freq, 4); @@ -1719,6 +1735,10 @@ const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType; memcpy(&pData[139], &vcftype, 1); + + if (chunksize >= 148) { + memcpy(&pData[140], DimensionUpperLimits, 8); + } } // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet @@ -2100,8 +2120,10 @@ dimension == dimension_releasetrigger || dimension == dimension_keyboard || dimension == dimension_roundrobin || - dimension == dimension_random) ? split_type_bit - : split_type_normal; + dimension == dimension_random || + dimension == dimension_smartmidi || + dimension == dimension_roundrobinkeyboard) ? split_type_bit + : split_type_normal; pDimensionDefinitions[i].zone_size = (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones : 0; @@ -2130,6 +2152,8 @@ if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); } GetSample(); // load global region sample reference + } else { + DimensionRegions = 0; } // make sure there is at least one dimension region @@ -2152,6 +2176,12 @@ * @throws gig::Exception if samples cannot be dereferenced */ void Region::UpdateChunks() { + // in the gig format we don't care about the Region's sample reference + // but we still have to provide some existing one to not corrupt the + // file, so to avoid the latter we simply always assign the sample of + // the first dimension region of this region + pSample = pDimensionRegions[0]->pSample; + // first update base class's chunks DLS::Region::UpdateChunks(); @@ -2238,7 +2268,8 @@ int dim[8] = { 0 }; for (int i = 0 ; i < DimensionRegions ; i++) { - if (pDimensionRegions[i]->VelocityUpperLimit) { + if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || + pDimensionRegions[i]->VelocityUpperLimit) { // create the velocity table uint8_t* table = pDimensionRegions[i]->VelocityTable; if (!table) { @@ -2247,10 +2278,18 @@ } int tableidx = 0; int velocityZone = 0; - for (int k = i ; k < end ; k += step) { - DimensionRegion *d = pDimensionRegions[k]; - for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone; - velocityZone++; + if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3 + for (int k = i ; k < end ; k += step) { + DimensionRegion *d = pDimensionRegions[k]; + for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone; + velocityZone++; + } + } else { // gig2 + for (int k = i ; k < end ; k += step) { + DimensionRegion *d = pDimensionRegions[k]; + for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone; + velocityZone++; + } } } else { if (pDimensionRegions[i]->VelocityTable) { @@ -2445,7 +2484,15 @@ } else { switch (pDimensionDefinitions[i].split_type) { case split_type_normal: - bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); + if (pDimensionRegions[0]->DimensionUpperLimits[i]) { + // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges + for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) { + if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break; + } + } else { + // gig2: evenly sized zones + bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); + } break; case split_type_bit: // the value is already the sought dimension bit number const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; @@ -2459,7 +2506,7 @@ DimensionRegion* dimreg = pDimensionRegions[dimregidx]; if (veldim != -1) { // (dimreg is now the dimension region for the lowest velocity) - if (dimreg->VelocityUpperLimit) // custom defined zone ranges + if (dimreg->VelocityTable) // custom defined zone ranges bits = dimreg->VelocityTable[DimValues[veldim]]; else // normal split type bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); @@ -2690,33 +2737,105 @@ /** @brief Constructor. * - * @param file - pointer to the RIFF::File object of this .gig file - * @param ck3gnm - pointer to 3gnm chunk associated with this group + * @param file - pointer to the gig::File object + * @param ck3gnm - pointer to 3gnm chunk associated with this group or + * NULL if this is a new Group */ - Group::Group(RIFF::File* file, RIFF::Chunk* ck3gnm) { + Group::Group(File* file, RIFF::Chunk* ck3gnm) { pFile = file; pNameChunk = ck3gnm; ::LoadString(pNameChunk, Name); } Group::~Group() { + // remove the chunk associated with this group (if any) + if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk); } /** @brief Update chunks with current group settings. * - * Apply current Group field values to the respective. You have to call - * File::Save() to make changes persistent. + * Apply current Group field values to the respective chunks. You have + * to call File::Save() to make changes persistent. + * + * Usually there is absolutely no need to call this method explicitly. + * It will be called automatically when File::Save() was called. */ void Group::UpdateChunks() { // make sure <3gri> and <3gnl> list chunks exist - RIFF::List* _3gri = pFile->GetSubList(LIST_TYPE_3GRI); - if (!_3gri) _3gri = pFile->AddSubList(LIST_TYPE_3GRI); + RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); + if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); - if (!_3gnl) _3gnl = pFile->AddSubList(LIST_TYPE_3GNL); + if (!_3gnl) _3gnl = pFile->pRIFF->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); } + /** + * Returns the first Sample of this Group. You have to call this method + * once before you use GetNextSample(). + * + * Notice: this method might block for a long time, in case the + * samples of this .gig file were not scanned yet + * + * @returns pointer address to first Sample or NULL if there is none + * applied to this Group + * @see GetNextSample() + */ + Sample* Group::GetFirstSample() { + // FIXME: lazy und unsafe implementation, should be an autonomous iterator + for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) { + if (pSample->GetGroup() == this) return pSample; + } + return NULL; + } + + /** + * Returns the next Sample of the Group. You have to call + * GetFirstSample() once before you can use this method. By calling this + * method multiple times it iterates through the Samples assigned to + * this Group. + * + * @returns pointer address to the next Sample of this Group or NULL if + * end reached + * @see GetFirstSample() + */ + Sample* Group::GetNextSample() { + // FIXME: lazy und unsafe implementation, should be an autonomous iterator + for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) { + if (pSample->GetGroup() == this) return pSample; + } + return NULL; + } + + /** + * Move Sample given by \a pSample from another Group to this Group. + */ + void Group::AddSample(Sample* pSample) { + pSample->pGroup = this; + } + + /** + * Move all members of this group to another group (preferably the 1st + * one except this). This method is called explicitly by + * File::DeleteGroup() thus when a Group was deleted. This code was + * intentionally not placed in the destructor! + */ + void Group::MoveAll() { + // get "that" other group first + Group* pOtherGroup = NULL; + for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) { + if (pOtherGroup != this) break; + } + if (!pOtherGroup) throw Exception( + "Could not move samples to another group, since there is no " + "other Group. This is a bug, report it!" + ); + // now move all samples of this group to the other group + for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { + pOtherGroup->AddSample(pSample); + } + } + // *************** File *************** @@ -2787,6 +2906,7 @@ if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); + if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation pSamples->erase(iter); delete pSample; } @@ -2796,6 +2916,10 @@ } void File::LoadSamples(progress_t* pProgress) { + // Groups must be loaded before samples, because samples will try + // to resolve the group they belong to + LoadGroups(); + if (!pSamples) pSamples = new SampleList; RIFF::File* file = pRIFF; @@ -2919,7 +3043,7 @@ * have to call Save() to make this persistent to the file. * * @param pInstrument - instrument to delete - * @throws gig::Excption if given instrument could not be found + * @throws gig::Exception if given instrument could not be found */ void File::DeleteInstrument(Instrument* pInstrument) { if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments"); @@ -2961,9 +3085,9 @@ Group* File::GetFirstGroup() { if (!pGroups) LoadGroups(); - if (!pGroups) return NULL; + // there must always be at least one group GroupsIterator = pGroups->begin(); - return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator; + return *GroupsIterator; } Group* File::GetNextGroup() { @@ -2980,7 +3104,6 @@ */ Group* File::GetGroup(uint index) { if (!pGroups) LoadGroups(); - if (!pGroups) return NULL; GroupsIterator = pGroups->begin(); for (uint i = 0; GroupsIterator != pGroups->end(); i++) { if (i == index) return *GroupsIterator; @@ -2991,34 +3114,101 @@ Group* File::AddGroup() { if (!pGroups) LoadGroups(); - if (!pGroups) pGroups = new std::list; + // there must always be at least one group __ensureMandatoryChunksExist(); - Group* pGroup = new Group(pRIFF, NULL); + Group* pGroup = new Group(this, NULL); pGroups->push_back(pGroup); return pGroup; } + /** @brief Delete a group and its samples. + * + * This will delete the given Group object and all the samples that + * belong to this group from the gig file. You have to call Save() to + * make this persistent to the file. + * + * @param pGroup - group to delete + * @throws gig::Exception if given group could not be found + */ void File::DeleteGroup(Group* pGroup) { - if (!pGroups) throw gig::Exception("Could not delete group as there are no groups"); + if (!pGroups) LoadGroups(); + std::list::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); + if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); + if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); + // delete all members of this group + for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) { + DeleteSample(pSample); + } + // now delete this group object + pGroups->erase(iter); + delete pGroup; + } + + /** @brief Delete a group. + * + * This will delete the given Group object from the gig file. All the + * samples that belong to this group will not be deleted, but instead + * be moved to another group. You have to call Save() to make this + * persistent to the file. + * + * @param pGroup - group to delete + * @throws gig::Exception if given group could not be found + */ + void File::DeleteGroupOnly(Group* pGroup) { + if (!pGroups) LoadGroups(); std::list::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); + if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); + // move all members of this group to another group + pGroup->MoveAll(); pGroups->erase(iter); delete pGroup; } void File::LoadGroups() { if (!pGroups) pGroups = new std::list; + // try to read defined groups from file RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); - if (!lst3gri) return; - RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); - if (!lst3gnl) return; - { - RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); - while (ck) { - if (ck->GetChunkID() == CHUNK_ID_3GNM) { - pGroups->push_back(new Group(pRIFF, ck)); + if (lst3gri) { + RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); + if (lst3gnl) { + RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); + while (ck) { + if (ck->GetChunkID() == CHUNK_ID_3GNM) { + pGroups->push_back(new Group(this, ck)); + } + ck = lst3gnl->GetNextSubChunk(); } - ck = lst3gnl->GetNextSubChunk(); + } + } + // if there were no group(s), create at least the mandatory default group + if (!pGroups->size()) { + Group* pGroup = new Group(this, NULL); + pGroup->Name = "Default Group"; + pGroups->push_back(pGroup); + } + } + + /** + * Apply all the gig file's current instruments, samples, groups and settings + * to the respective RIFF chunks. You have to call Save() to make changes + * persistent. + * + * Usually there is absolutely no need to call this method explicitly. + * It will be called automatically when File::Save() was called. + * + * @throws Exception - on errors + */ + void File::UpdateChunks() { + // first update base class's chunks + DLS::File::UpdateChunks(); + + // update group's chunks + if (pGroups) { + std::list::iterator iter = pGroups->begin(); + std::list::iterator end = pGroups->end(); + for (; iter != end; ++iter) { + (*iter)->UpdateChunks(); } } }