1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
254 |
} |
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
// *************** Other Internal functions *************** |
259 |
|
// * |
260 |
|
|
261 |
|
static split_type_t __resolveSplitType(dimension_t dimension) { |
262 |
|
return ( |
263 |
|
dimension == dimension_layer || |
264 |
|
dimension == dimension_samplechannel || |
265 |
|
dimension == dimension_releasetrigger || |
266 |
|
dimension == dimension_keyboard || |
267 |
|
dimension == dimension_roundrobin || |
268 |
|
dimension == dimension_random || |
269 |
|
dimension == dimension_smartmidi || |
270 |
|
dimension == dimension_roundrobinkeyboard |
271 |
|
) ? split_type_bit : split_type_normal; |
272 |
|
} |
273 |
|
|
274 |
|
static int __resolveZoneSize(dimension_def_t& dimension_definition) { |
275 |
|
return (dimension_definition.split_type == split_type_normal) |
276 |
|
? int(128.0 / dimension_definition.zones) : 0; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
// *************** Sample *************** |
// *************** Sample *************** |
282 |
// * |
// * |
283 |
|
|
309 |
|
|
310 |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
311 |
if (pCk3gix) { |
if (pCk3gix) { |
312 |
SampleGroup = pCk3gix->ReadInt16(); |
uint16_t iSampleGroup = pCk3gix->ReadInt16(); |
313 |
|
pGroup = pFile->GetGroup(iSampleGroup); |
314 |
} else { // '3gix' chunk missing |
} else { // '3gix' chunk missing |
315 |
// use default value(s) |
// by default assigned to that mandatory "Default Group" |
316 |
SampleGroup = 0; |
pGroup = pFile->GetGroup(0); |
317 |
} |
} |
318 |
|
|
319 |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
337 |
// use default values |
// use default values |
338 |
Manufacturer = 0; |
Manufacturer = 0; |
339 |
Product = 0; |
Product = 0; |
340 |
SamplePeriod = 1 / SamplesPerSecond; |
SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); |
341 |
MIDIUnityNote = 64; |
MIDIUnityNote = 64; |
342 |
FineTune = 0; |
FineTune = 0; |
343 |
SMPTEOffset = 0; |
SMPTEOffset = 0; |
388 |
* Usually there is absolutely no need to call this method explicitly. |
* Usually there is absolutely no need to call this method explicitly. |
389 |
* It will be called automatically when File::Save() was called. |
* It will be called automatically when File::Save() was called. |
390 |
* |
* |
391 |
* @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 |
392 |
* was provided yet |
* was provided yet |
393 |
* @throws gig::Exception if there is any invalid sample setting |
* @throws gig::Exception if there is any invalid sample setting |
394 |
*/ |
*/ |
423 |
// make sure '3gix' chunk exists |
// make sure '3gix' chunk exists |
424 |
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
425 |
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
426 |
|
// determine appropriate sample group index (to be stored in chunk) |
427 |
|
uint16_t iSampleGroup = 0; // 0 refers to default sample group |
428 |
|
File* pFile = static_cast<File*>(pParent); |
429 |
|
if (pFile->pGroups) { |
430 |
|
std::list<Group*>::iterator iter = pFile->pGroups->begin(); |
431 |
|
std::list<Group*>::iterator end = pFile->pGroups->end(); |
432 |
|
for (int i = 0; iter != end; i++, iter++) { |
433 |
|
if (*iter == pGroup) { |
434 |
|
iSampleGroup = i; |
435 |
|
break; // found |
436 |
|
} |
437 |
|
} |
438 |
|
} |
439 |
// update '3gix' chunk |
// update '3gix' chunk |
440 |
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
441 |
memcpy(&pData[0], &SampleGroup, 2); |
memcpy(&pData[0], &iSampleGroup, 2); |
442 |
} |
} |
443 |
|
|
444 |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
659 |
* enlarged samples before calling File::Save() as this might exceed the |
* enlarged samples before calling File::Save() as this might exceed the |
660 |
* current sample's boundary! |
* current sample's boundary! |
661 |
* |
* |
662 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
663 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
664 |
* other formats will fail! |
* other formats will fail! |
665 |
* |
* |
666 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
667 |
* greater than zero) |
* greater than zero) |
668 |
* @throws DLS::Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
669 |
* or if \a iNewSize is less than 1 |
* or if \a iNewSize is less than 1 |
670 |
* @throws gig::Exception if existing sample is compressed |
* @throws gig::Exception if existing sample is compressed |
671 |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
1176 |
} |
} |
1177 |
} |
} |
1178 |
|
|
1179 |
|
/** |
1180 |
|
* Returns pointer to the Group this Sample belongs to. In the .gig |
1181 |
|
* format a sample always belongs to one group. If it wasn't explicitly |
1182 |
|
* assigned to a certain group, it will be automatically assigned to a |
1183 |
|
* default group. |
1184 |
|
* |
1185 |
|
* @returns Sample's Group (never NULL) |
1186 |
|
*/ |
1187 |
|
Group* Sample::GetGroup() const { |
1188 |
|
return pGroup; |
1189 |
|
} |
1190 |
|
|
1191 |
Sample::~Sample() { |
Sample::~Sample() { |
1192 |
Instances--; |
Instances--; |
1193 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
1362 |
if (lfo3ctrl & 0x40) // bit 6 |
if (lfo3ctrl & 0x40) // bit 6 |
1363 |
VCFType = vcf_type_lowpassturbo; |
VCFType = vcf_type_lowpassturbo; |
1364 |
} |
} |
1365 |
|
if (_3ewa->RemainingBytes() >= 8) { |
1366 |
|
_3ewa->Read(DimensionUpperLimits, 1, 8); |
1367 |
|
} else { |
1368 |
|
memset(DimensionUpperLimits, 0, 8); |
1369 |
|
} |
1370 |
} else { // '3ewa' chunk does not exist yet |
} else { // '3ewa' chunk does not exist yet |
1371 |
// use default values |
// use default values |
1372 |
LFO3Frequency = 1.0; |
LFO3Frequency = 1.0; |
1447 |
VCFVelocityDynamicRange = 0x04; |
VCFVelocityDynamicRange = 0x04; |
1448 |
VCFVelocityCurve = curve_type_linear; |
VCFVelocityCurve = curve_type_linear; |
1449 |
VCFType = vcf_type_lowpass; |
VCFType = vcf_type_lowpass; |
1450 |
|
memset(DimensionUpperLimits, 0, 8); |
1451 |
} |
} |
1452 |
|
|
1453 |
pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, |
pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, |
1503 |
|
|
1504 |
// update '3ewa' chunk with DimensionRegion's current settings |
// update '3ewa' chunk with DimensionRegion's current settings |
1505 |
|
|
1506 |
const uint32_t unknown = _3ewa->GetSize(); // unknown, always chunk size ? |
const uint32_t chunksize = _3ewa->GetSize(); |
1507 |
memcpy(&pData[0], &unknown, 4); |
memcpy(&pData[0], &chunksize, 4); // unknown, always chunk size? |
1508 |
|
|
1509 |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
1510 |
memcpy(&pData[4], &lfo3freq, 4); |
memcpy(&pData[4], &lfo3freq, 4); |
1759 |
|
|
1760 |
const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType; |
const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType; |
1761 |
memcpy(&pData[139], &vcftype, 1); |
memcpy(&pData[139], &vcftype, 1); |
1762 |
|
|
1763 |
|
if (chunksize >= 148) { |
1764 |
|
memcpy(&pData[140], DimensionUpperLimits, 8); |
1765 |
|
} |
1766 |
} |
} |
1767 |
|
|
1768 |
// get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet |
// get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet |
2139 |
pDimensionDefinitions[i].dimension = dimension; |
pDimensionDefinitions[i].dimension = dimension; |
2140 |
pDimensionDefinitions[i].bits = bits; |
pDimensionDefinitions[i].bits = bits; |
2141 |
pDimensionDefinitions[i].zones = zones ? zones : 0x01 << bits; // = pow(2,bits) |
pDimensionDefinitions[i].zones = zones ? zones : 0x01 << bits; // = pow(2,bits) |
2142 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
pDimensionDefinitions[i].split_type = __resolveSplitType(dimension); |
2143 |
dimension == dimension_samplechannel || |
pDimensionDefinitions[i].zone_size = __resolveZoneSize(pDimensionDefinitions[i]); |
|
dimension == dimension_releasetrigger || |
|
|
dimension == dimension_keyboard || |
|
|
dimension == dimension_roundrobin || |
|
|
dimension == dimension_random) ? split_type_bit |
|
|
: split_type_normal; |
|
|
pDimensionDefinitions[i].zone_size = |
|
|
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones |
|
|
: 0; |
|
2144 |
Dimensions++; |
Dimensions++; |
2145 |
|
|
2146 |
// if this is a layer dimension, remember the amount of layers |
// if this is a layer dimension, remember the amount of layers |
2166 |
if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
2167 |
} |
} |
2168 |
GetSample(); // load global region sample reference |
GetSample(); // load global region sample reference |
2169 |
|
} else { |
2170 |
|
DimensionRegions = 0; |
2171 |
} |
} |
2172 |
|
|
2173 |
// make sure there is at least one dimension region |
// make sure there is at least one dimension region |
2190 |
* @throws gig::Exception if samples cannot be dereferenced |
* @throws gig::Exception if samples cannot be dereferenced |
2191 |
*/ |
*/ |
2192 |
void Region::UpdateChunks() { |
void Region::UpdateChunks() { |
2193 |
|
// in the gig format we don't care about the Region's sample reference |
2194 |
|
// but we still have to provide some existing one to not corrupt the |
2195 |
|
// file, so to avoid the latter we simply always assign the sample of |
2196 |
|
// the first dimension region of this region |
2197 |
|
pSample = pDimensionRegions[0]->pSample; |
2198 |
|
|
2199 |
// first update base class's chunks |
// first update base class's chunks |
2200 |
DLS::Region::UpdateChunks(); |
DLS::Region::UpdateChunks(); |
2201 |
|
|
2282 |
int dim[8] = { 0 }; |
int dim[8] = { 0 }; |
2283 |
for (int i = 0 ; i < DimensionRegions ; i++) { |
for (int i = 0 ; i < DimensionRegions ; i++) { |
2284 |
|
|
2285 |
if (pDimensionRegions[i]->VelocityUpperLimit) { |
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
2286 |
|
pDimensionRegions[i]->VelocityUpperLimit) { |
2287 |
// create the velocity table |
// create the velocity table |
2288 |
uint8_t* table = pDimensionRegions[i]->VelocityTable; |
uint8_t* table = pDimensionRegions[i]->VelocityTable; |
2289 |
if (!table) { |
if (!table) { |
2292 |
} |
} |
2293 |
int tableidx = 0; |
int tableidx = 0; |
2294 |
int velocityZone = 0; |
int velocityZone = 0; |
2295 |
for (int k = i ; k < end ; k += step) { |
if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3 |
2296 |
DimensionRegion *d = pDimensionRegions[k]; |
for (int k = i ; k < end ; k += step) { |
2297 |
for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone; |
DimensionRegion *d = pDimensionRegions[k]; |
2298 |
velocityZone++; |
for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone; |
2299 |
|
velocityZone++; |
2300 |
|
} |
2301 |
|
} else { // gig2 |
2302 |
|
for (int k = i ; k < end ; k += step) { |
2303 |
|
DimensionRegion *d = pDimensionRegions[k]; |
2304 |
|
for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone; |
2305 |
|
velocityZone++; |
2306 |
|
} |
2307 |
} |
} |
2308 |
} else { |
} else { |
2309 |
if (pDimensionRegions[i]->VelocityTable) { |
if (pDimensionRegions[i]->VelocityTable) { |
2370 |
// assign definition of new dimension |
// assign definition of new dimension |
2371 |
pDimensionDefinitions[Dimensions] = *pDimDef; |
pDimensionDefinitions[Dimensions] = *pDimDef; |
2372 |
|
|
2373 |
|
// auto correct certain dimension definition fields (where possible) |
2374 |
|
pDimensionDefinitions[Dimensions].split_type = |
2375 |
|
__resolveSplitType(pDimensionDefinitions[Dimensions].dimension); |
2376 |
|
pDimensionDefinitions[Dimensions].zone_size = |
2377 |
|
__resolveZoneSize(pDimensionDefinitions[Dimensions]); |
2378 |
|
|
2379 |
// create new dimension region(s) for this new dimension |
// create new dimension region(s) for this new dimension |
2380 |
for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) { |
for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) { |
2381 |
//TODO: maybe we should copy existing dimension regions if possible instead of simply creating new ones with default values |
//TODO: maybe we should copy existing dimension regions if possible instead of simply creating new ones with default values |
2504 |
} else { |
} else { |
2505 |
switch (pDimensionDefinitions[i].split_type) { |
switch (pDimensionDefinitions[i].split_type) { |
2506 |
case split_type_normal: |
case split_type_normal: |
2507 |
bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); |
if (pDimensionRegions[0]->DimensionUpperLimits[i]) { |
2508 |
|
// gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges |
2509 |
|
for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) { |
2510 |
|
if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break; |
2511 |
|
} |
2512 |
|
} else { |
2513 |
|
// gig2: evenly sized zones |
2514 |
|
bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); |
2515 |
|
} |
2516 |
break; |
break; |
2517 |
case split_type_bit: // the value is already the sought dimension bit number |
case split_type_bit: // the value is already the sought dimension bit number |
2518 |
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
2526 |
DimensionRegion* dimreg = pDimensionRegions[dimregidx]; |
DimensionRegion* dimreg = pDimensionRegions[dimregidx]; |
2527 |
if (veldim != -1) { |
if (veldim != -1) { |
2528 |
// (dimreg is now the dimension region for the lowest velocity) |
// (dimreg is now the dimension region for the lowest velocity) |
2529 |
if (dimreg->VelocityUpperLimit) // custom defined zone ranges |
if (dimreg->VelocityTable) // custom defined zone ranges |
2530 |
bits = dimreg->VelocityTable[DimValues[veldim]]; |
bits = dimreg->VelocityTable[DimValues[veldim]]; |
2531 |
else // normal split type |
else // normal split type |
2532 |
bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); |
bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); |
2752 |
|
|
2753 |
|
|
2754 |
|
|
2755 |
|
// *************** Group *************** |
2756 |
|
// * |
2757 |
|
|
2758 |
|
/** @brief Constructor. |
2759 |
|
* |
2760 |
|
* @param file - pointer to the gig::File object |
2761 |
|
* @param ck3gnm - pointer to 3gnm chunk associated with this group or |
2762 |
|
* NULL if this is a new Group |
2763 |
|
*/ |
2764 |
|
Group::Group(File* file, RIFF::Chunk* ck3gnm) { |
2765 |
|
pFile = file; |
2766 |
|
pNameChunk = ck3gnm; |
2767 |
|
::LoadString(pNameChunk, Name); |
2768 |
|
} |
2769 |
|
|
2770 |
|
Group::~Group() { |
2771 |
|
// remove the chunk associated with this group (if any) |
2772 |
|
if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk); |
2773 |
|
} |
2774 |
|
|
2775 |
|
/** @brief Update chunks with current group settings. |
2776 |
|
* |
2777 |
|
* Apply current Group field values to the respective chunks. You have |
2778 |
|
* to call File::Save() to make changes persistent. |
2779 |
|
* |
2780 |
|
* Usually there is absolutely no need to call this method explicitly. |
2781 |
|
* It will be called automatically when File::Save() was called. |
2782 |
|
*/ |
2783 |
|
void Group::UpdateChunks() { |
2784 |
|
// make sure <3gri> and <3gnl> list chunks exist |
2785 |
|
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
2786 |
|
if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); |
2787 |
|
RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); |
2788 |
|
if (!_3gnl) _3gnl = pFile->pRIFF->AddSubList(LIST_TYPE_3GNL); |
2789 |
|
// now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk |
2790 |
|
::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64); |
2791 |
|
} |
2792 |
|
|
2793 |
|
/** |
2794 |
|
* Returns the first Sample of this Group. You have to call this method |
2795 |
|
* once before you use GetNextSample(). |
2796 |
|
* |
2797 |
|
* <b>Notice:</b> this method might block for a long time, in case the |
2798 |
|
* samples of this .gig file were not scanned yet |
2799 |
|
* |
2800 |
|
* @returns pointer address to first Sample or NULL if there is none |
2801 |
|
* applied to this Group |
2802 |
|
* @see GetNextSample() |
2803 |
|
*/ |
2804 |
|
Sample* Group::GetFirstSample() { |
2805 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
2806 |
|
for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) { |
2807 |
|
if (pSample->GetGroup() == this) return pSample; |
2808 |
|
} |
2809 |
|
return NULL; |
2810 |
|
} |
2811 |
|
|
2812 |
|
/** |
2813 |
|
* Returns the next Sample of the Group. You have to call |
2814 |
|
* GetFirstSample() once before you can use this method. By calling this |
2815 |
|
* method multiple times it iterates through the Samples assigned to |
2816 |
|
* this Group. |
2817 |
|
* |
2818 |
|
* @returns pointer address to the next Sample of this Group or NULL if |
2819 |
|
* end reached |
2820 |
|
* @see GetFirstSample() |
2821 |
|
*/ |
2822 |
|
Sample* Group::GetNextSample() { |
2823 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
2824 |
|
for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) { |
2825 |
|
if (pSample->GetGroup() == this) return pSample; |
2826 |
|
} |
2827 |
|
return NULL; |
2828 |
|
} |
2829 |
|
|
2830 |
|
/** |
2831 |
|
* Move Sample given by \a pSample from another Group to this Group. |
2832 |
|
*/ |
2833 |
|
void Group::AddSample(Sample* pSample) { |
2834 |
|
pSample->pGroup = this; |
2835 |
|
} |
2836 |
|
|
2837 |
|
/** |
2838 |
|
* Move all members of this group to another group (preferably the 1st |
2839 |
|
* one except this). This method is called explicitly by |
2840 |
|
* File::DeleteGroup() thus when a Group was deleted. This code was |
2841 |
|
* intentionally not placed in the destructor! |
2842 |
|
*/ |
2843 |
|
void Group::MoveAll() { |
2844 |
|
// get "that" other group first |
2845 |
|
Group* pOtherGroup = NULL; |
2846 |
|
for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) { |
2847 |
|
if (pOtherGroup != this) break; |
2848 |
|
} |
2849 |
|
if (!pOtherGroup) throw Exception( |
2850 |
|
"Could not move samples to another group, since there is no " |
2851 |
|
"other Group. This is a bug, report it!" |
2852 |
|
); |
2853 |
|
// now move all samples of this group to the other group |
2854 |
|
for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { |
2855 |
|
pOtherGroup->AddSample(pSample); |
2856 |
|
} |
2857 |
|
} |
2858 |
|
|
2859 |
|
|
2860 |
|
|
2861 |
// *************** File *************** |
// *************** File *************** |
2862 |
// * |
// * |
2863 |
|
|
2864 |
File::File() : DLS::File() { |
File::File() : DLS::File() { |
2865 |
|
pGroups = NULL; |
2866 |
pInfo->UseFixedLengthStrings = true; |
pInfo->UseFixedLengthStrings = true; |
2867 |
} |
} |
2868 |
|
|
2869 |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
2870 |
|
pGroups = NULL; |
2871 |
pInfo->UseFixedLengthStrings = true; |
pInfo->UseFixedLengthStrings = true; |
2872 |
} |
} |
2873 |
|
|
2874 |
|
File::~File() { |
2875 |
|
if (pGroups) { |
2876 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
2877 |
|
std::list<Group*>::iterator end = pGroups->end(); |
2878 |
|
while (iter != end) { |
2879 |
|
delete *iter; |
2880 |
|
++iter; |
2881 |
|
} |
2882 |
|
delete pGroups; |
2883 |
|
} |
2884 |
|
} |
2885 |
|
|
2886 |
Sample* File::GetFirstSample(progress_t* pProgress) { |
Sample* File::GetFirstSample(progress_t* pProgress) { |
2887 |
if (!pSamples) LoadSamples(pProgress); |
if (!pSamples) LoadSamples(pProgress); |
2888 |
if (!pSamples) return NULL; |
if (!pSamples) return NULL; |
2926 |
if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); |
if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); |
2927 |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); |
2928 |
if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); |
if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); |
2929 |
|
if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation |
2930 |
pSamples->erase(iter); |
pSamples->erase(iter); |
2931 |
delete pSample; |
delete pSample; |
2932 |
} |
} |
2936 |
} |
} |
2937 |
|
|
2938 |
void File::LoadSamples(progress_t* pProgress) { |
void File::LoadSamples(progress_t* pProgress) { |
2939 |
|
// Groups must be loaded before samples, because samples will try |
2940 |
|
// to resolve the group they belong to |
2941 |
|
LoadGroups(); |
2942 |
|
|
2943 |
if (!pSamples) pSamples = new SampleList; |
if (!pSamples) pSamples = new SampleList; |
2944 |
|
|
2945 |
RIFF::File* file = pRIFF; |
RIFF::File* file = pRIFF; |
3063 |
* have to call Save() to make this persistent to the file. |
* have to call Save() to make this persistent to the file. |
3064 |
* |
* |
3065 |
* @param pInstrument - instrument to delete |
* @param pInstrument - instrument to delete |
3066 |
* @throws gig::Excption if given instrument could not be found |
* @throws gig::Exception if given instrument could not be found |
3067 |
*/ |
*/ |
3068 |
void File::DeleteInstrument(Instrument* pInstrument) { |
void File::DeleteInstrument(Instrument* pInstrument) { |
3069 |
if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments"); |
if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments"); |
3103 |
} |
} |
3104 |
} |
} |
3105 |
|
|
3106 |
|
Group* File::GetFirstGroup() { |
3107 |
|
if (!pGroups) LoadGroups(); |
3108 |
|
// there must always be at least one group |
3109 |
|
GroupsIterator = pGroups->begin(); |
3110 |
|
return *GroupsIterator; |
3111 |
|
} |
3112 |
|
|
3113 |
|
Group* File::GetNextGroup() { |
3114 |
|
if (!pGroups) return NULL; |
3115 |
|
++GroupsIterator; |
3116 |
|
return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator; |
3117 |
|
} |
3118 |
|
|
3119 |
|
/** |
3120 |
|
* Returns the group with the given index. |
3121 |
|
* |
3122 |
|
* @param index - number of the sought group (0..n) |
3123 |
|
* @returns sought group or NULL if there's no such group |
3124 |
|
*/ |
3125 |
|
Group* File::GetGroup(uint index) { |
3126 |
|
if (!pGroups) LoadGroups(); |
3127 |
|
GroupsIterator = pGroups->begin(); |
3128 |
|
for (uint i = 0; GroupsIterator != pGroups->end(); i++) { |
3129 |
|
if (i == index) return *GroupsIterator; |
3130 |
|
++GroupsIterator; |
3131 |
|
} |
3132 |
|
return NULL; |
3133 |
|
} |
3134 |
|
|
3135 |
|
Group* File::AddGroup() { |
3136 |
|
if (!pGroups) LoadGroups(); |
3137 |
|
// there must always be at least one group |
3138 |
|
__ensureMandatoryChunksExist(); |
3139 |
|
Group* pGroup = new Group(this, NULL); |
3140 |
|
pGroups->push_back(pGroup); |
3141 |
|
return pGroup; |
3142 |
|
} |
3143 |
|
|
3144 |
|
/** @brief Delete a group and its samples. |
3145 |
|
* |
3146 |
|
* This will delete the given Group object and all the samples that |
3147 |
|
* belong to this group from the gig file. You have to call Save() to |
3148 |
|
* make this persistent to the file. |
3149 |
|
* |
3150 |
|
* @param pGroup - group to delete |
3151 |
|
* @throws gig::Exception if given group could not be found |
3152 |
|
*/ |
3153 |
|
void File::DeleteGroup(Group* pGroup) { |
3154 |
|
if (!pGroups) LoadGroups(); |
3155 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
3156 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
3157 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
3158 |
|
// delete all members of this group |
3159 |
|
for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) { |
3160 |
|
DeleteSample(pSample); |
3161 |
|
} |
3162 |
|
// now delete this group object |
3163 |
|
pGroups->erase(iter); |
3164 |
|
delete pGroup; |
3165 |
|
} |
3166 |
|
|
3167 |
|
/** @brief Delete a group. |
3168 |
|
* |
3169 |
|
* This will delete the given Group object from the gig file. All the |
3170 |
|
* samples that belong to this group will not be deleted, but instead |
3171 |
|
* be moved to another group. You have to call Save() to make this |
3172 |
|
* persistent to the file. |
3173 |
|
* |
3174 |
|
* @param pGroup - group to delete |
3175 |
|
* @throws gig::Exception if given group could not be found |
3176 |
|
*/ |
3177 |
|
void File::DeleteGroupOnly(Group* pGroup) { |
3178 |
|
if (!pGroups) LoadGroups(); |
3179 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
3180 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
3181 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
3182 |
|
// move all members of this group to another group |
3183 |
|
pGroup->MoveAll(); |
3184 |
|
pGroups->erase(iter); |
3185 |
|
delete pGroup; |
3186 |
|
} |
3187 |
|
|
3188 |
|
void File::LoadGroups() { |
3189 |
|
if (!pGroups) pGroups = new std::list<Group*>; |
3190 |
|
// try to read defined groups from file |
3191 |
|
RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); |
3192 |
|
if (lst3gri) { |
3193 |
|
RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); |
3194 |
|
if (lst3gnl) { |
3195 |
|
RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); |
3196 |
|
while (ck) { |
3197 |
|
if (ck->GetChunkID() == CHUNK_ID_3GNM) { |
3198 |
|
pGroups->push_back(new Group(this, ck)); |
3199 |
|
} |
3200 |
|
ck = lst3gnl->GetNextSubChunk(); |
3201 |
|
} |
3202 |
|
} |
3203 |
|
} |
3204 |
|
// if there were no group(s), create at least the mandatory default group |
3205 |
|
if (!pGroups->size()) { |
3206 |
|
Group* pGroup = new Group(this, NULL); |
3207 |
|
pGroup->Name = "Default Group"; |
3208 |
|
pGroups->push_back(pGroup); |
3209 |
|
} |
3210 |
|
} |
3211 |
|
|
3212 |
|
/** |
3213 |
|
* Apply all the gig file's current instruments, samples, groups and settings |
3214 |
|
* to the respective RIFF chunks. You have to call Save() to make changes |
3215 |
|
* persistent. |
3216 |
|
* |
3217 |
|
* Usually there is absolutely no need to call this method explicitly. |
3218 |
|
* It will be called automatically when File::Save() was called. |
3219 |
|
* |
3220 |
|
* @throws Exception - on errors |
3221 |
|
*/ |
3222 |
|
void File::UpdateChunks() { |
3223 |
|
// first update base class's chunks |
3224 |
|
DLS::File::UpdateChunks(); |
3225 |
|
|
3226 |
|
// update group's chunks |
3227 |
|
if (pGroups) { |
3228 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
3229 |
|
std::list<Group*>::iterator end = pGroups->end(); |
3230 |
|
for (; iter != end; ++iter) { |
3231 |
|
(*iter)->UpdateChunks(); |
3232 |
|
} |
3233 |
|
} |
3234 |
|
} |
3235 |
|
|
3236 |
|
|
3237 |
|
|
3238 |
// *************** Exception *************** |
// *************** Exception *************** |