--- libgig/trunk/src/gig.cpp 2007/10/05 11:26:53 1384 +++ libgig/trunk/src/gig.cpp 2013/01/07 23:23:58 2394 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2007 by Christian Schoenebeck * + * Copyright (C) 2003-2013 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -25,6 +25,7 @@ #include "helper.h" +#include #include #include @@ -366,11 +367,11 @@ * is located, 0 otherwise */ Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { - static const DLS::Info::FixedStringLength fixedStringLengths[] = { + static const DLS::Info::string_length_t fixedStringLengths[] = { { CHUNK_ID_INAM, 64 }, { 0, 0 } }; - pInfo->FixedStringLengths = fixedStringLengths; + pInfo->SetFixedStringLengths(fixedStringLengths); Instances++; FileNo = fileNo; @@ -676,6 +677,7 @@ if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; + SetPos(0); // reset read position to begin of sample RAMCache.pStart = new int8_t[allocationsize]; RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; RAMCache.NullExtensionSize = allocationsize - RAMCache.Size; @@ -713,6 +715,7 @@ if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; RAMCache.pStart = NULL; RAMCache.Size = 0; + RAMCache.NullExtensionSize = 0; } /** @brief Resize sample. @@ -907,7 +910,8 @@ } // reverse the sample frames for backward playback - SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); + if (totalreadsamples > swapareastart) //FIXME: this if() is just a crash workaround for now (#102), but totalreadsamples <= swapareastart should never be the case, so there's probably still a bug above! + SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); } } while (samplestoread && readsamples); break; @@ -1577,6 +1581,7 @@ */ DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) { Instances++; + //NOTE: I think we cannot call CopyAssign() here (in a constructor) as long as its a virtual method *this = src; // default memberwise shallow copy of all parameters pParentList = _3ewl; // restore the chunk pointer @@ -1592,6 +1597,41 @@ pSampleLoops[k] = src.pSampleLoops[k]; } } + + /** + * Make a (semi) deep copy of the DimensionRegion object given by @a orig + * and assign it to this object. + * + * Note that all sample pointers referenced by @a orig are simply copied as + * memory address. Thus the respective samples are shared, not duplicated! + * + * @param orig - original DimensionRegion object to be copied from + */ + void DimensionRegion::CopyAssign(const DimensionRegion* orig) { + // delete all allocated data first + if (VelocityTable) delete [] VelocityTable; + if (pSampleLoops) delete [] pSampleLoops; + + // backup parent list pointer + RIFF::List* p = pParentList; + + //NOTE: copy code copied from assignment constructor above, see comment there as well + + *this = *orig; // default memberwise shallow copy of all parameters + pParentList = p; // restore the chunk pointer + + // deep copy of owned structures + if (orig->VelocityTable) { + VelocityTable = new uint8_t[128]; + for (int k = 0 ; k < 128 ; k++) + VelocityTable[k] = orig->VelocityTable[k]; + } + if (orig->pSampleLoops) { + pSampleLoops = new DLS::sample_loop_t[orig->SampleLoops]; + for (int k = 0 ; k < orig->SampleLoops ; k++) + pSampleLoops[k] = orig->pSampleLoops[k]; + } + } /** * Updates the respective member variable and updates @c SampleAttenuation @@ -1834,7 +1874,7 @@ const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth : uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */ - pData[116] = eg3depth; + store16(&pData[116], eg3depth); // next 2 bytes unknown @@ -1881,7 +1921,7 @@ (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */ pData[137] = vcfbreakpoint; - const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 | + const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 + VCFVelocityCurve * 5; pData[138] = vcfvelocity; @@ -2372,6 +2412,8 @@ // Actual Loading + if (!file->GetAutoLoad()) return; + LoadDimensionRegions(rgnList); RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); @@ -2415,12 +2457,14 @@ else _3lnk->SetPos(44); - // load sample references - for (uint i = 0; i < DimensionRegions; i++) { - uint32_t wavepoolindex = _3lnk->ReadUint32(); - if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); + // load sample references (if auto loading is enabled) + if (file->GetAutoLoad()) { + for (uint i = 0; i < DimensionRegions; i++) { + uint32_t wavepoolindex = _3lnk->ReadUint32(); + if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); + } + GetSample(); // load global region sample reference } - GetSample(); // load global region sample reference } else { DimensionRegions = 0; for (int i = 0 ; i < 8 ; i++) { @@ -2917,19 +2961,74 @@ } return NULL; } + + /** + * Make a (semi) deep copy of the Region object given by @a orig + * and assign it to this object. + * + * Note that all sample pointers referenced by @a orig are simply copied as + * memory address. Thus the respective samples are shared, not duplicated! + * + * @param orig - original Region object to be copied from + */ + void Region::CopyAssign(const Region* orig) { + // handle base classes + DLS::Region::CopyAssign(orig); + + // handle own member variables + for (int i = Dimensions - 1; i >= 0; --i) { + DeleteDimension(&pDimensionDefinitions[i]); + } + Layers = 0; // just to be sure + for (int i = 0; i < orig->Dimensions; i++) { + // we need to copy the dim definition here, to avoid the compiler + // complaining about const-ness issue + dimension_def_t def = orig->pDimensionDefinitions[i]; + AddDimension(&def); + } + for (int i = 0; i < 256; i++) { + if (pDimensionRegions[i] && orig->pDimensionRegions[i]) { + pDimensionRegions[i]->CopyAssign( + orig->pDimensionRegions[i] + ); + } + } + Layers = orig->Layers; + } + +// *************** MidiRule *************** +// * + +MidiRuleCtrlTrigger::MidiRuleCtrlTrigger(RIFF::Chunk* _3ewg) { + _3ewg->SetPos(36); + Triggers = _3ewg->ReadUint8(); + _3ewg->SetPos(40); + ControllerNumber = _3ewg->ReadUint8(); + _3ewg->SetPos(46); + for (int i = 0 ; i < Triggers ; i++) { + pTriggers[i].TriggerPoint = _3ewg->ReadUint8(); + pTriggers[i].Descending = _3ewg->ReadUint8(); + pTriggers[i].VelSensitivity = _3ewg->ReadUint8(); + pTriggers[i].Key = _3ewg->ReadUint8(); + pTriggers[i].NoteOff = _3ewg->ReadUint8(); + pTriggers[i].Velocity = _3ewg->ReadUint8(); + pTriggers[i].OverridePedal = _3ewg->ReadUint8(); + _3ewg->ReadUint8(); + } +} // *************** Instrument *************** // * Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) { - static const DLS::Info::FixedStringLength fixedStringLengths[] = { + static const DLS::Info::string_length_t fixedStringLengths[] = { { CHUNK_ID_INAM, 64 }, { CHUNK_ID_ISFT, 12 }, { 0, 0 } }; - pInfo->FixedStringLengths = fixedStringLengths; + pInfo->SetFixedStringLengths(fixedStringLengths); // Initialization for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; @@ -2940,6 +3039,8 @@ PianoReleaseMode = false; DimensionKeyRange.low = 0; DimensionKeyRange.high = 0; + pMidiRules = new MidiRule*[3]; + pMidiRules[0] = NULL; // Loading RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); @@ -2954,22 +3055,39 @@ PianoReleaseMode = dimkeystart & 0x01; DimensionKeyRange.low = dimkeystart >> 1; DimensionKeyRange.high = _3ewg->ReadUint8(); + + if (_3ewg->GetSize() > 32) { + // read MIDI rules + int i = 0; + _3ewg->SetPos(32); + uint8_t id1 = _3ewg->ReadUint8(); + uint8_t id2 = _3ewg->ReadUint8(); + + if (id1 == 4 && id2 == 16) { + pMidiRules[i++] = new MidiRuleCtrlTrigger(_3ewg); + } + //TODO: all the other types of rules + + pMidiRules[i] = NULL; + } } } - if (!pRegions) pRegions = new RegionList; - RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); - if (lrgn) { - RIFF::List* rgn = lrgn->GetFirstSubList(); - while (rgn) { - if (rgn->GetListType() == LIST_TYPE_RGN) { - __notify_progress(pProgress, (float) pRegions->size() / (float) Regions); - pRegions->push_back(new Region(this, rgn)); + if (pFile->GetAutoLoad()) { + if (!pRegions) pRegions = new RegionList; + RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); + if (lrgn) { + RIFF::List* rgn = lrgn->GetFirstSubList(); + while (rgn) { + if (rgn->GetListType() == LIST_TYPE_RGN) { + __notify_progress(pProgress, (float) pRegions->size() / (float) Regions); + pRegions->push_back(new Region(this, rgn)); + } + rgn = lrgn->GetNextSubList(); } - rgn = lrgn->GetNextSubList(); + // Creating Region Key Table for fast lookup + UpdateRegionKeyTable(); } - // Creating Region Key Table for fast lookup - UpdateRegionKeyTable(); } __notify_progress(pProgress, 1.0f); // notify done @@ -2988,6 +3106,10 @@ } Instrument::~Instrument() { + for (int i = 0 ; pMidiRules[i] ; i++) { + delete pMidiRules[i]; + } + delete[] pMidiRules; } /** @@ -3102,6 +3224,65 @@ UpdateRegionKeyTable(); } + /** + * Returns a MIDI rule of the instrument. + * + * The list of MIDI rules, at least in gig v3, always contains at + * most two rules. The second rule can only be the DEF filter + * (which currently isn't supported by libgig). + * + * @param i - MIDI rule number + * @returns pointer address to MIDI rule number i or NULL if there is none + */ + MidiRule* Instrument::GetMidiRule(int i) { + return pMidiRules[i]; + } + + /** + * Make a (semi) deep copy of the Instrument object given by @a orig + * and assign it to this object. + * + * Note that all sample pointers referenced by @a orig are simply copied as + * memory address. Thus the respective samples are shared, not duplicated! + * + * @param orig - original Instrument object to be copied from + */ + void Instrument::CopyAssign(const Instrument* orig) { + // handle base class + // (without copying DLS region stuff) + DLS::Instrument::CopyAssignCore(orig); + + // handle own member variables + Attenuation = orig->Attenuation; + EffectSend = orig->EffectSend; + FineTune = orig->FineTune; + PitchbendRange = orig->PitchbendRange; + PianoReleaseMode = orig->PianoReleaseMode; + DimensionKeyRange = orig->DimensionKeyRange; + + // free old midi rules + for (int i = 0 ; pMidiRules[i] ; i++) { + delete pMidiRules[i]; + } + //TODO: MIDI rule copying + pMidiRules[0] = NULL; + + // delete all old regions + while (Regions) DeleteRegion(GetFirstRegion()); + // create new regions and copy them from original + { + RegionList::const_iterator it = orig->pRegions->begin(); + for (int i = 0; i < orig->Regions; ++i, ++it) { + Region* dstRgn = AddRegion(); + //NOTE: Region does semi-deep copy ! + dstRgn->CopyAssign( + static_cast(*it) + ); + } + } + + UpdateRegionKeyTable(); + } // *************** Group *************** @@ -3237,7 +3418,7 @@ 0, 3, 20030331 & 0xffff, 20030331 >> 16 }; - const DLS::Info::FixedStringLength File::FixedStringLengths[] = { + static const DLS::Info::string_length_t _FileFixedStringLengths[] = { { CHUNK_ID_IARL, 256 }, { CHUNK_ID_IART, 128 }, { CHUNK_ID_ICMS, 128 }, @@ -3259,9 +3440,10 @@ }; File::File() : DLS::File() { + bAutoLoad = true; *pVersion = VERSION_3; pGroups = NULL; - pInfo->FixedStringLengths = FixedStringLengths; + pInfo->SetFixedStringLengths(_FileFixedStringLengths); pInfo->ArchivalLocation = String(256, ' '); // add some mandatory chunks to get the file chunks in right @@ -3274,8 +3456,9 @@ } File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { + bAutoLoad = true; pGroups = NULL; - pInfo->FixedStringLengths = FixedStringLengths; + pInfo->SetFixedStringLengths(_FileFixedStringLengths); } File::~File() { @@ -3343,6 +3526,7 @@ pSamples->erase(iter); delete pSample; + SampleList::iterator tmp = SamplesIterator; // remove all references to the sample for (Instrument* instrument = GetFirstInstrument() ; instrument ; instrument = GetNextInstrument()) { @@ -3357,6 +3541,7 @@ } } } + SamplesIterator = tmp; // restore iterator } void File::LoadSamples() { @@ -3447,7 +3632,8 @@ progress_t subprogress; __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask __notify_progress(&subprogress, 0.0f); - GetFirstSample(&subprogress); // now force all samples to be loaded + if (GetAutoLoad()) + GetFirstSample(&subprogress); // now force all samples to be loaded __notify_progress(&subprogress, 1.0f); // instrument loading subtask @@ -3496,6 +3682,27 @@ pInstruments->push_back(pInstrument); return pInstrument; } + + /** @brief Add a duplicate of an existing instrument. + * + * Duplicates the instrument definition given by @a orig and adds it + * to this file. This allows in an instrument editor application to + * easily create variations of an instrument, which will be stored in + * the same .gig file, sharing i.e. the same samples. + * + * Note that all sample pointers referenced by @a orig are simply copied as + * memory address. Thus the respective samples are shared, not duplicated! + * + * You have to call Save() to make this persistent to the file. + * + * @param orig - original instrument to be copied + * @returns duplicated copy of the given instrument + */ + Instrument* File::AddDuplicateInstrument(const Instrument* orig) { + Instrument* instr = AddInstrument(); + instr->CopyAssign(orig); + return instr; + } /** @brief Delete an instrument. * @@ -3854,6 +4061,33 @@ } } + /** + * Enable / disable automatic loading. By default this properyt is + * enabled and all informations are loaded automatically. However + * loading all Regions, DimensionRegions and especially samples might + * take a long time for large .gig files, and sometimes one might only + * be interested in retrieving very superficial informations like the + * amount of instruments and their names. In this case one might disable + * automatic loading to avoid very slow response times. + * + * @e CAUTION: by disabling this property many pointers (i.e. sample + * references) and informations will have invalid or even undefined + * data! This feature is currently only intended for retrieving very + * superficial informations in a very fast way. Don't use it to retrieve + * details like synthesis informations or even to modify .gig files! + */ + void File::SetAutoLoad(bool b) { + bAutoLoad = b; + } + + /** + * Returns whether automatic loading is enabled. + * @see SetAutoLoad() + */ + bool File::GetAutoLoad() { + return bAutoLoad; + } + // *************** Exception ***************