/[svn]/libgig/trunk/src/gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/gig.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 928 by persson, Tue Oct 24 19:32:47 2006 UTC revision 1180 by persson, Sat May 12 12:39:25 2007 UTC
# Line 1  Line 1 
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  *
# Line 254  namespace { Line 254  namespace {
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    
# Line 279  namespace { Line 303  namespace {
303       *                         is located, 0 otherwise       *                         is located, 0 otherwise
304       */       */
305      Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {      Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) {
306          pInfo->UseFixedLengthStrings = true;          static const DLS::Info::FixedStringLength fixedStringLengths[] = {
307                { CHUNK_ID_INAM, 64 },
308                { 0, 0 }
309            };
310            pInfo->FixedStringLengths = fixedStringLengths;
311          Instances++;          Instances++;
312          FileNo = fileNo;          FileNo = fileNo;
313    
314          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);
315          if (pCk3gix) {          if (pCk3gix) {
316              SampleGroup = pCk3gix->ReadInt16();              uint16_t iSampleGroup = pCk3gix->ReadInt16();
317                pGroup = pFile->GetGroup(iSampleGroup);
318          } else { // '3gix' chunk missing          } else { // '3gix' chunk missing
319              // use default value(s)              // by default assigned to that mandatory "Default Group"
320              SampleGroup = 0;              pGroup = pFile->GetGroup(0);
321          }          }
322    
323          pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);          pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);
# Line 363  namespace { Line 392  namespace {
392       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
393       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
394       *       *
395       * @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
396       *                        was provided yet       *                        was provided yet
397       * @throws gig::Exception if there is any invalid sample setting       * @throws gig::Exception if there is any invalid sample setting
398       */       */
# Line 377  namespace { Line 406  namespace {
406          // update 'smpl' chunk          // update 'smpl' chunk
407          uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData();          uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData();
408          SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5);          SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5);
409          memcpy(&pData[0], &Manufacturer, 4);          store32(&pData[0], Manufacturer);
410          memcpy(&pData[4], &Product, 4);          store32(&pData[4], Product);
411          memcpy(&pData[8], &SamplePeriod, 4);          store32(&pData[8], SamplePeriod);
412          memcpy(&pData[12], &MIDIUnityNote, 4);          store32(&pData[12], MIDIUnityNote);
413          memcpy(&pData[16], &FineTune, 4);          store32(&pData[16], FineTune);
414          memcpy(&pData[20], &SMPTEFormat, 4);          store32(&pData[20], SMPTEFormat);
415          memcpy(&pData[24], &SMPTEOffset, 4);          store32(&pData[24], SMPTEOffset);
416          memcpy(&pData[28], &Loops, 4);          store32(&pData[28], Loops);
417    
418          // we skip 'manufByt' for now (4 bytes)          // we skip 'manufByt' for now (4 bytes)
419    
420          memcpy(&pData[36], &LoopID, 4);          store32(&pData[36], LoopID);
421          memcpy(&pData[40], &LoopType, 4);          store32(&pData[40], LoopType);
422          memcpy(&pData[44], &LoopStart, 4);          store32(&pData[44], LoopStart);
423          memcpy(&pData[48], &LoopEnd, 4);          store32(&pData[48], LoopEnd);
424          memcpy(&pData[52], &LoopFraction, 4);          store32(&pData[52], LoopFraction);
425          memcpy(&pData[56], &LoopPlayCount, 4);          store32(&pData[56], LoopPlayCount);
426    
427          // make sure '3gix' chunk exists          // make sure '3gix' chunk exists
428          pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX);          pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX);
429          if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4);          if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4);
430            // determine appropriate sample group index (to be stored in chunk)
431            uint16_t iSampleGroup = 0; // 0 refers to default sample group
432            File* pFile = static_cast<File*>(pParent);
433            if (pFile->pGroups) {
434                std::list<Group*>::iterator iter = pFile->pGroups->begin();
435                std::list<Group*>::iterator end  = pFile->pGroups->end();
436                for (int i = 0; iter != end; i++, iter++) {
437                    if (*iter == pGroup) {
438                        iSampleGroup = i;
439                        break; // found
440                    }
441                }
442            }
443          // update '3gix' chunk          // update '3gix' chunk
444          pData = (uint8_t*) pCk3gix->LoadChunkData();          pData = (uint8_t*) pCk3gix->LoadChunkData();
445          memcpy(&pData[0], &SampleGroup, 2);          store16(&pData[0], iSampleGroup);
446      }      }
447    
448      /// 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).
# Line 621  namespace { Line 663  namespace {
663       * enlarged samples before calling File::Save() as this might exceed the       * enlarged samples before calling File::Save() as this might exceed the
664       * current sample's boundary!       * current sample's boundary!
665       *       *
666       * Also note: only WAVE_FORMAT_PCM is currently supported, that is       * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
667       * FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
668       * other formats will fail!       * other formats will fail!
669       *       *
670       * @param iNewSize - new sample wave data size in sample points (must be       * @param iNewSize - new sample wave data size in sample points (must be
671       *                   greater than zero)       *                   greater than zero)
672       * @throws DLS::Excecption if FormatTag != WAVE_FORMAT_PCM       * @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM
673       *                         or if \a iNewSize is less than 1       *                         or if \a iNewSize is less than 1
674       * @throws gig::Exception if existing sample is compressed       * @throws gig::Exception if existing sample is compressed
675       * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize,       * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize,
# Line 1138  namespace { Line 1180  namespace {
1180          }          }
1181      }      }
1182    
1183        /**
1184         * Returns pointer to the Group this Sample belongs to. In the .gig
1185         * format a sample always belongs to one group. If it wasn't explicitly
1186         * assigned to a certain group, it will be automatically assigned to a
1187         * default group.
1188         *
1189         * @returns Sample's Group (never NULL)
1190         */
1191        Group* Sample::GetGroup() const {
1192            return pGroup;
1193        }
1194    
1195      Sample::~Sample() {      Sample::~Sample() {
1196          Instances--;          Instances--;
1197          if (!Instances && InternalDecompressionBuffer.Size) {          if (!Instances && InternalDecompressionBuffer.Size) {
# Line 1312  namespace { Line 1366  namespace {
1366                  if (lfo3ctrl & 0x40) // bit 6                  if (lfo3ctrl & 0x40) // bit 6
1367                      VCFType = vcf_type_lowpassturbo;                      VCFType = vcf_type_lowpassturbo;
1368              }              }
1369                if (_3ewa->RemainingBytes() >= 8) {
1370                    _3ewa->Read(DimensionUpperLimits, 1, 8);
1371                } else {
1372                    memset(DimensionUpperLimits, 0, 8);
1373                }
1374          } else { // '3ewa' chunk does not exist yet          } else { // '3ewa' chunk does not exist yet
1375              // use default values              // use default values
1376              LFO3Frequency                   = 1.0;              LFO3Frequency                   = 1.0;
# Line 1392  namespace { Line 1451  namespace {
1451              VCFVelocityDynamicRange         = 0x04;              VCFVelocityDynamicRange         = 0x04;
1452              VCFVelocityCurve                = curve_type_linear;              VCFVelocityCurve                = curve_type_linear;
1453              VCFType                         = vcf_type_lowpass;              VCFType                         = vcf_type_lowpass;
1454                memset(DimensionUpperLimits, 0, 8);
1455          }          }
1456    
1457          pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve,          pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve,
# Line 1447  namespace { Line 1507  namespace {
1507    
1508          // update '3ewa' chunk with DimensionRegion's current settings          // update '3ewa' chunk with DimensionRegion's current settings
1509    
1510          const uint32_t unknown = _3ewa->GetSize(); // unknown, always chunk size ?          const uint32_t chunksize = _3ewa->GetSize();
1511          memcpy(&pData[0], &unknown, 4);          store32(&pData[0], chunksize); // unknown, always chunk size?
1512    
1513          const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency);          const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency);
1514          memcpy(&pData[4], &lfo3freq, 4);          store32(&pData[4], lfo3freq);
1515    
1516          const int32_t eg3attack = (int32_t) GIG_EXP_ENCODE(EG3Attack);          const int32_t eg3attack = (int32_t) GIG_EXP_ENCODE(EG3Attack);
1517          memcpy(&pData[8], &eg3attack, 4);          store32(&pData[8], eg3attack);
1518    
1519          // next 2 bytes unknown          // next 2 bytes unknown
1520    
1521          memcpy(&pData[14], &LFO1InternalDepth, 2);          store16(&pData[14], LFO1InternalDepth);
1522    
1523          // next 2 bytes unknown          // next 2 bytes unknown
1524    
1525          memcpy(&pData[18], &LFO3InternalDepth, 2);          store16(&pData[18], LFO3InternalDepth);
1526    
1527          // next 2 bytes unknown          // next 2 bytes unknown
1528    
1529          memcpy(&pData[22], &LFO1ControlDepth, 2);          store16(&pData[22], LFO1ControlDepth);
1530    
1531          // next 2 bytes unknown          // next 2 bytes unknown
1532    
1533          memcpy(&pData[26], &LFO3ControlDepth, 2);          store16(&pData[26], LFO3ControlDepth);
1534    
1535          const int32_t eg1attack = (int32_t) GIG_EXP_ENCODE(EG1Attack);          const int32_t eg1attack = (int32_t) GIG_EXP_ENCODE(EG1Attack);
1536          memcpy(&pData[28], &eg1attack, 4);          store32(&pData[28], eg1attack);
1537    
1538          const int32_t eg1decay1 = (int32_t) GIG_EXP_ENCODE(EG1Decay1);          const int32_t eg1decay1 = (int32_t) GIG_EXP_ENCODE(EG1Decay1);
1539          memcpy(&pData[32], &eg1decay1, 4);          store32(&pData[32], eg1decay1);
1540    
1541          // next 2 bytes unknown          // next 2 bytes unknown
1542    
1543          memcpy(&pData[38], &EG1Sustain, 2);          store16(&pData[38], EG1Sustain);
1544    
1545          const int32_t eg1release = (int32_t) GIG_EXP_ENCODE(EG1Release);          const int32_t eg1release = (int32_t) GIG_EXP_ENCODE(EG1Release);
1546          memcpy(&pData[40], &eg1release, 4);          store32(&pData[40], eg1release);
1547    
1548          const uint8_t eg1ctl = (uint8_t) EncodeLeverageController(EG1Controller);          const uint8_t eg1ctl = (uint8_t) EncodeLeverageController(EG1Controller);
1549          memcpy(&pData[44], &eg1ctl, 1);          pData[44] = eg1ctl;
1550    
1551          const uint8_t eg1ctrloptions =          const uint8_t eg1ctrloptions =
1552              (EG1ControllerInvert) ? 0x01 : 0x00 |              (EG1ControllerInvert) ? 0x01 : 0x00 |
1553              GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG1ControllerAttackInfluence) |              GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG1ControllerAttackInfluence) |
1554              GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG1ControllerDecayInfluence) |              GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG1ControllerDecayInfluence) |
1555              GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG1ControllerReleaseInfluence);              GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG1ControllerReleaseInfluence);
1556          memcpy(&pData[45], &eg1ctrloptions, 1);          pData[45] = eg1ctrloptions;
1557    
1558          const uint8_t eg2ctl = (uint8_t) EncodeLeverageController(EG2Controller);          const uint8_t eg2ctl = (uint8_t) EncodeLeverageController(EG2Controller);
1559          memcpy(&pData[46], &eg2ctl, 1);          pData[46] = eg2ctl;
1560    
1561          const uint8_t eg2ctrloptions =          const uint8_t eg2ctrloptions =
1562              (EG2ControllerInvert) ? 0x01 : 0x00 |              (EG2ControllerInvert) ? 0x01 : 0x00 |
1563              GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG2ControllerAttackInfluence) |              GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG2ControllerAttackInfluence) |
1564              GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG2ControllerDecayInfluence) |              GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG2ControllerDecayInfluence) |
1565              GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG2ControllerReleaseInfluence);              GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG2ControllerReleaseInfluence);
1566          memcpy(&pData[47], &eg2ctrloptions, 1);          pData[47] = eg2ctrloptions;
1567    
1568          const int32_t lfo1freq = (int32_t) GIG_EXP_ENCODE(LFO1Frequency);          const int32_t lfo1freq = (int32_t) GIG_EXP_ENCODE(LFO1Frequency);
1569          memcpy(&pData[48], &lfo1freq, 4);          store32(&pData[48], lfo1freq);
1570    
1571          const int32_t eg2attack = (int32_t) GIG_EXP_ENCODE(EG2Attack);          const int32_t eg2attack = (int32_t) GIG_EXP_ENCODE(EG2Attack);
1572          memcpy(&pData[52], &eg2attack, 4);          store32(&pData[52], eg2attack);
1573    
1574          const int32_t eg2decay1 = (int32_t) GIG_EXP_ENCODE(EG2Decay1);          const int32_t eg2decay1 = (int32_t) GIG_EXP_ENCODE(EG2Decay1);
1575          memcpy(&pData[56], &eg2decay1, 4);          store32(&pData[56], eg2decay1);
1576    
1577          // next 2 bytes unknown          // next 2 bytes unknown
1578    
1579          memcpy(&pData[62], &EG2Sustain, 2);          store16(&pData[62], EG2Sustain);
1580    
1581          const int32_t eg2release = (int32_t) GIG_EXP_ENCODE(EG2Release);          const int32_t eg2release = (int32_t) GIG_EXP_ENCODE(EG2Release);
1582          memcpy(&pData[64], &eg2release, 4);          store32(&pData[64], eg2release);
1583    
1584          // next 2 bytes unknown          // next 2 bytes unknown
1585    
1586          memcpy(&pData[70], &LFO2ControlDepth, 2);          store16(&pData[70], LFO2ControlDepth);
1587    
1588          const int32_t lfo2freq = (int32_t) GIG_EXP_ENCODE(LFO2Frequency);          const int32_t lfo2freq = (int32_t) GIG_EXP_ENCODE(LFO2Frequency);
1589          memcpy(&pData[72], &lfo2freq, 4);          store32(&pData[72], lfo2freq);
1590    
1591          // next 2 bytes unknown          // next 2 bytes unknown
1592    
1593          memcpy(&pData[78], &LFO2InternalDepth, 2);          store16(&pData[78], LFO2InternalDepth);
1594    
1595          const int32_t eg1decay2 = (int32_t) (EG1InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG1Decay2);          const int32_t eg1decay2 = (int32_t) (EG1InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG1Decay2);
1596          memcpy(&pData[80], &eg1decay2, 4);          store32(&pData[80], eg1decay2);
1597    
1598          // next 2 bytes unknown          // next 2 bytes unknown
1599    
1600          memcpy(&pData[86], &EG1PreAttack, 2);          store16(&pData[86], EG1PreAttack);
1601    
1602          const int32_t eg2decay2 = (int32_t) (EG2InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG2Decay2);          const int32_t eg2decay2 = (int32_t) (EG2InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG2Decay2);
1603          memcpy(&pData[88], &eg2decay2, 4);          store32(&pData[88], eg2decay2);
1604    
1605          // next 2 bytes unknown          // next 2 bytes unknown
1606    
1607          memcpy(&pData[94], &EG2PreAttack, 2);          store16(&pData[94], EG2PreAttack);
1608    
1609          {          {
1610              if (VelocityResponseDepth > 4) throw Exception("VelocityResponseDepth must be between 0 and 4");              if (VelocityResponseDepth > 4) throw Exception("VelocityResponseDepth must be between 0 and 4");
# Line 1562  namespace { Line 1622  namespace {
1622                  default:                  default:
1623                      throw Exception("Could not update DimensionRegion's chunk, unknown VelocityResponseCurve selected");                      throw Exception("Could not update DimensionRegion's chunk, unknown VelocityResponseCurve selected");
1624              }              }
1625              memcpy(&pData[96], &velocityresponse, 1);              pData[96] = velocityresponse;
1626          }          }
1627    
1628          {          {
# Line 1581  namespace { Line 1641  namespace {
1641                  default:                  default:
1642                      throw Exception("Could not update DimensionRegion's chunk, unknown ReleaseVelocityResponseCurve selected");                      throw Exception("Could not update DimensionRegion's chunk, unknown ReleaseVelocityResponseCurve selected");
1643              }              }
1644              memcpy(&pData[97], &releasevelocityresponse, 1);              pData[97] = releasevelocityresponse;
1645          }          }
1646    
1647          memcpy(&pData[98], &VelocityResponseCurveScaling, 1);          pData[98] = VelocityResponseCurveScaling;
1648    
1649          memcpy(&pData[99], &AttenuationControllerThreshold, 1);          pData[99] = AttenuationControllerThreshold;
1650    
1651          // next 4 bytes unknown          // next 4 bytes unknown
1652    
1653          memcpy(&pData[104], &SampleStartOffset, 2);          store16(&pData[104], SampleStartOffset);
1654    
1655          // next 2 bytes unknown          // next 2 bytes unknown
1656    
# Line 1609  namespace { Line 1669  namespace {
1669                  default:                  default:
1670                      throw Exception("Could not update DimensionRegion's chunk, unknown DimensionBypass selected");                      throw Exception("Could not update DimensionRegion's chunk, unknown DimensionBypass selected");
1671              }              }
1672              memcpy(&pData[108], &pitchTrackDimensionBypass, 1);              pData[108] = pitchTrackDimensionBypass;
1673          }          }
1674    
1675          const uint8_t pan = (Pan >= 0) ? Pan : ((-Pan) + 63); // signed 8 bit -> signed 7 bit          const uint8_t pan = (Pan >= 0) ? Pan : ((-Pan) + 63); // signed 8 bit -> signed 7 bit
1676          memcpy(&pData[109], &pan, 1);          pData[109] = pan;
1677    
1678          const uint8_t selfmask = (SelfMask) ? 0x01 : 0x00;          const uint8_t selfmask = (SelfMask) ? 0x01 : 0x00;
1679          memcpy(&pData[110], &selfmask, 1);          pData[110] = selfmask;
1680    
1681          // next byte unknown          // next byte unknown
1682    
# Line 1625  namespace { Line 1685  namespace {
1685              if (LFO3Sync) lfo3ctrl |= 0x20; // bit 5              if (LFO3Sync) lfo3ctrl |= 0x20; // bit 5
1686              if (InvertAttenuationController) lfo3ctrl |= 0x80; // bit 7              if (InvertAttenuationController) lfo3ctrl |= 0x80; // bit 7
1687              if (VCFType == vcf_type_lowpassturbo) lfo3ctrl |= 0x40; // bit 6              if (VCFType == vcf_type_lowpassturbo) lfo3ctrl |= 0x40; // bit 6
1688              memcpy(&pData[112], &lfo3ctrl, 1);              pData[112] = lfo3ctrl;
1689          }          }
1690    
1691          const uint8_t attenctl = EncodeLeverageController(AttenuationController);          const uint8_t attenctl = EncodeLeverageController(AttenuationController);
1692          memcpy(&pData[113], &attenctl, 1);          pData[113] = attenctl;
1693    
1694          {          {
1695              uint8_t lfo2ctrl = LFO2Controller & 0x07; // lower 3 bits              uint8_t lfo2ctrl = LFO2Controller & 0x07; // lower 3 bits
1696              if (LFO2FlipPhase) lfo2ctrl |= 0x80; // bit 7              if (LFO2FlipPhase) lfo2ctrl |= 0x80; // bit 7
1697              if (LFO2Sync)      lfo2ctrl |= 0x20; // bit 5              if (LFO2Sync)      lfo2ctrl |= 0x20; // bit 5
1698              if (VCFResonanceController != vcf_res_ctrl_none) lfo2ctrl |= 0x40; // bit 6              if (VCFResonanceController != vcf_res_ctrl_none) lfo2ctrl |= 0x40; // bit 6
1699              memcpy(&pData[114], &lfo2ctrl, 1);              pData[114] = lfo2ctrl;
1700          }          }
1701    
1702          {          {
# Line 1645  namespace { Line 1705  namespace {
1705              if (LFO1Sync)      lfo1ctrl |= 0x40; // bit 6              if (LFO1Sync)      lfo1ctrl |= 0x40; // bit 6
1706              if (VCFResonanceController != vcf_res_ctrl_none)              if (VCFResonanceController != vcf_res_ctrl_none)
1707                  lfo1ctrl |= GIG_VCF_RESONANCE_CTRL_ENCODE(VCFResonanceController);                  lfo1ctrl |= GIG_VCF_RESONANCE_CTRL_ENCODE(VCFResonanceController);
1708              memcpy(&pData[115], &lfo1ctrl, 1);              pData[115] = lfo1ctrl;
1709          }          }
1710    
1711          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth          const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth
1712                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */                                                    : uint16_t(((-EG3Depth) - 1) ^ 0xffff); /* binary complementary for negatives */
1713          memcpy(&pData[116], &eg3depth, 1);          pData[116] = eg3depth;
1714    
1715          // next 2 bytes unknown          // next 2 bytes unknown
1716    
1717          const uint8_t channeloffset = ChannelOffset * 4;          const uint8_t channeloffset = ChannelOffset * 4;
1718          memcpy(&pData[120], &channeloffset, 1);          pData[120] = channeloffset;
1719    
1720          {          {
1721              uint8_t regoptions = 0;              uint8_t regoptions = 0;
1722              if (MSDecode)      regoptions |= 0x01; // bit 0              if (MSDecode)      regoptions |= 0x01; // bit 0
1723              if (SustainDefeat) regoptions |= 0x02; // bit 1              if (SustainDefeat) regoptions |= 0x02; // bit 1
1724              memcpy(&pData[121], &regoptions, 1);              pData[121] = regoptions;
1725          }          }
1726    
1727          // next 2 bytes unknown          // next 2 bytes unknown
1728    
1729          memcpy(&pData[124], &VelocityUpperLimit, 1);          pData[124] = VelocityUpperLimit;
1730    
1731          // next 3 bytes unknown          // next 3 bytes unknown
1732    
1733          memcpy(&pData[128], &ReleaseTriggerDecay, 1);          pData[128] = ReleaseTriggerDecay;
1734    
1735          // next 2 bytes unknown          // next 2 bytes unknown
1736    
1737          const uint8_t eg1hold = (EG1Hold) ? 0x80 : 0x00; // bit 7          const uint8_t eg1hold = (EG1Hold) ? 0x80 : 0x00; // bit 7
1738          memcpy(&pData[131], &eg1hold, 1);          pData[131] = eg1hold;
1739    
1740          const uint8_t vcfcutoff = (VCFEnabled) ? 0x80 : 0x00 |  /* bit 7 */          const uint8_t vcfcutoff = (VCFEnabled) ? 0x80 : 0x00 |  /* bit 7 */
1741                                    (VCFCutoff & 0x7f);   /* lower 7 bits */                                    (VCFCutoff & 0x7f);   /* lower 7 bits */
1742          memcpy(&pData[132], &vcfcutoff, 1);          pData[132] = vcfcutoff;
1743    
1744          memcpy(&pData[133], &VCFCutoffController, 1);          pData[133] = VCFCutoffController;
1745    
1746          const uint8_t vcfvelscale = (VCFCutoffControllerInvert) ? 0x80 : 0x00 | /* bit 7 */          const uint8_t vcfvelscale = (VCFCutoffControllerInvert) ? 0x80 : 0x00 | /* bit 7 */
1747                                      (VCFVelocityScale & 0x7f); /* lower 7 bits */                                      (VCFVelocityScale & 0x7f); /* lower 7 bits */
1748          memcpy(&pData[134], &vcfvelscale, 1);          pData[134] = vcfvelscale;
1749    
1750          // next byte unknown          // next byte unknown
1751    
1752          const uint8_t vcfresonance = (VCFResonanceDynamic) ? 0x00 : 0x80 | /* bit 7 */          const uint8_t vcfresonance = (VCFResonanceDynamic) ? 0x00 : 0x80 | /* bit 7 */
1753                                       (VCFResonance & 0x7f); /* lower 7 bits */                                       (VCFResonance & 0x7f); /* lower 7 bits */
1754          memcpy(&pData[136], &vcfresonance, 1);          pData[136] = vcfresonance;
1755    
1756          const uint8_t vcfbreakpoint = (VCFKeyboardTracking) ? 0x80 : 0x00 | /* bit 7 */          const uint8_t vcfbreakpoint = (VCFKeyboardTracking) ? 0x80 : 0x00 | /* bit 7 */
1757                                        (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */                                        (VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */
1758          memcpy(&pData[137], &vcfbreakpoint, 1);          pData[137] = vcfbreakpoint;
1759    
1760          const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 |          const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 |
1761                                      VCFVelocityCurve * 5;                                      VCFVelocityCurve * 5;
1762          memcpy(&pData[138], &vcfvelocity, 1);          pData[138] = vcfvelocity;
1763    
1764          const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType;          const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType;
1765          memcpy(&pData[139], &vcftype, 1);          pData[139] = vcftype;
1766    
1767            if (chunksize >= 148) {
1768                memcpy(&pData[140], DimensionUpperLimits, 8);
1769            }
1770      }      }
1771    
1772      // 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
# Line 2044  namespace { Line 2108  namespace {
2108  // *  // *
2109    
2110      Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) {      Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) {
         pInfo->UseFixedLengthStrings = true;  
   
2111          // Initialization          // Initialization
2112          Dimensions = 0;          Dimensions = 0;
2113          for (int i = 0; i < 256; i++) {          for (int i = 0; i < 256; i++) {
# Line 2079  namespace { Line 2141  namespace {
2141                      pDimensionDefinitions[i].dimension = dimension;                      pDimensionDefinitions[i].dimension = dimension;
2142                      pDimensionDefinitions[i].bits      = bits;                      pDimensionDefinitions[i].bits      = bits;
2143                      pDimensionDefinitions[i].zones     = zones ? zones : 0x01 << bits; // = pow(2,bits)                      pDimensionDefinitions[i].zones     = zones ? zones : 0x01 << bits; // = pow(2,bits)
2144                      pDimensionDefinitions[i].split_type = (dimension == dimension_layer ||                      pDimensionDefinitions[i].split_type = __resolveSplitType(dimension);
2145                                                             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;  
2146                      Dimensions++;                      Dimensions++;
2147    
2148                      // if this is a layer dimension, remember the amount of layers                      // if this is a layer dimension, remember the amount of layers
# Line 2114  namespace { Line 2168  namespace {
2168                  if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);                  if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);
2169              }              }
2170              GetSample(); // load global region sample reference              GetSample(); // load global region sample reference
2171            } else {
2172                DimensionRegions = 0;
2173          }          }
2174    
2175          // make sure there is at least one dimension region          // make sure there is at least one dimension region
# Line 2136  namespace { Line 2192  namespace {
2192       * @throws gig::Exception if samples cannot be dereferenced       * @throws gig::Exception if samples cannot be dereferenced
2193       */       */
2194      void Region::UpdateChunks() {      void Region::UpdateChunks() {
2195            // in the gig format we don't care about the Region's sample reference
2196            // but we still have to provide some existing one to not corrupt the
2197            // file, so to avoid the latter we simply always assign the sample of
2198            // the first dimension region of this region
2199            pSample = pDimensionRegions[0]->pSample;
2200    
2201          // first update base class's chunks          // first update base class's chunks
2202          DLS::Region::UpdateChunks();          DLS::Region::UpdateChunks();
2203    
# Line 2157  namespace { Line 2219  namespace {
2219    
2220          // update dimension definitions in '3lnk' chunk          // update dimension definitions in '3lnk' chunk
2221          uint8_t* pData = (uint8_t*) _3lnk->LoadChunkData();          uint8_t* pData = (uint8_t*) _3lnk->LoadChunkData();
2222          memcpy(&pData[0], &DimensionRegions, 4);          store32(&pData[0], DimensionRegions);
2223          for (int i = 0; i < iMaxDimensions; i++) {          for (int i = 0; i < iMaxDimensions; i++) {
2224              pData[4 + i * 8] = (uint8_t) pDimensionDefinitions[i].dimension;              pData[4 + i * 8] = (uint8_t) pDimensionDefinitions[i].dimension;
2225              pData[5 + i * 8] = pDimensionDefinitions[i].bits;              pData[5 + i * 8] = pDimensionDefinitions[i].bits;
# Line 2182  namespace { Line 2244  namespace {
2244                  }                  }
2245                  if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample");                  if (iWaveIndex < 0) throw gig::Exception("Could not update gig::Region, could not find DimensionRegion's sample");
2246              }              }
2247              memcpy(&pData[iWavePoolOffset + i * 4], &iWaveIndex, 4);              store32(&pData[iWavePoolOffset + i * 4], iWaveIndex);
2248          }          }
2249      }      }
2250    
# Line 2222  namespace { Line 2284  namespace {
2284          int dim[8] = { 0 };          int dim[8] = { 0 };
2285          for (int i = 0 ; i < DimensionRegions ; i++) {          for (int i = 0 ; i < DimensionRegions ; i++) {
2286    
2287              if (pDimensionRegions[i]->VelocityUpperLimit) {              if (pDimensionRegions[i]->DimensionUpperLimits[veldim] ||
2288                    pDimensionRegions[i]->VelocityUpperLimit) {
2289                  // create the velocity table                  // create the velocity table
2290                  uint8_t* table = pDimensionRegions[i]->VelocityTable;                  uint8_t* table = pDimensionRegions[i]->VelocityTable;
2291                  if (!table) {                  if (!table) {
# Line 2231  namespace { Line 2294  namespace {
2294                  }                  }
2295                  int tableidx = 0;                  int tableidx = 0;
2296                  int velocityZone = 0;                  int velocityZone = 0;
2297                  for (int k = i ; k < end ; k += step) {                  if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3
2298                      DimensionRegion *d = pDimensionRegions[k];                      for (int k = i ; k < end ; k += step) {
2299                      for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;                          DimensionRegion *d = pDimensionRegions[k];
2300                      velocityZone++;                          for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone;
2301                            velocityZone++;
2302                        }
2303                    } else { // gig2
2304                        for (int k = i ; k < end ; k += step) {
2305                            DimensionRegion *d = pDimensionRegions[k];
2306                            for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2307                            velocityZone++;
2308                        }
2309                  }                  }
2310              } else {              } else {
2311                  if (pDimensionRegions[i]->VelocityTable) {                  if (pDimensionRegions[i]->VelocityTable) {
# Line 2301  namespace { Line 2372  namespace {
2372          // assign definition of new dimension          // assign definition of new dimension
2373          pDimensionDefinitions[Dimensions] = *pDimDef;          pDimensionDefinitions[Dimensions] = *pDimDef;
2374    
2375            // auto correct certain dimension definition fields (where possible)
2376            pDimensionDefinitions[Dimensions].split_type  =
2377                __resolveSplitType(pDimensionDefinitions[Dimensions].dimension);
2378            pDimensionDefinitions[Dimensions].zone_size =
2379                __resolveZoneSize(pDimensionDefinitions[Dimensions]);
2380    
2381          // create new dimension region(s) for this new dimension          // create new dimension region(s) for this new dimension
2382          for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) {          for (int i = 1 << iCurrentBits; i < 1 << iNewBits; i++) {
2383              //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
# Line 2429  namespace { Line 2506  namespace {
2506              } else {              } else {
2507                  switch (pDimensionDefinitions[i].split_type) {                  switch (pDimensionDefinitions[i].split_type) {
2508                      case split_type_normal:                      case split_type_normal:
2509                          bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);                          if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
2510                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
2511                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
2512                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
2513                                }
2514                            } else {
2515                                // gig2: evenly sized zones
2516                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
2517                            }
2518                          break;                          break;
2519                      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
2520                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
# Line 2443  namespace { Line 2528  namespace {
2528          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx];
2529          if (veldim != -1) {          if (veldim != -1) {
2530              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
2531              if (dimreg->VelocityUpperLimit) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
2532                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim]];
2533              else // normal split type              else // normal split type
2534                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);
# Line 2509  namespace { Line 2594  namespace {
2594  // *  // *
2595    
2596      Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) {      Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) {
2597          pInfo->UseFixedLengthStrings = true;          static const DLS::Info::FixedStringLength fixedStringLengths[] = {
2598                { CHUNK_ID_INAM, 64 },
2599                { CHUNK_ID_ISFT, 12 },
2600                { 0, 0 }
2601            };
2602            pInfo->FixedStringLengths = fixedStringLengths;
2603    
2604          // Initialization          // Initialization
2605          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;
# Line 2591  namespace { Line 2681  namespace {
2681          if (!_3ewg)  _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, 12);          if (!_3ewg)  _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, 12);
2682          // update '3ewg' RIFF chunk          // update '3ewg' RIFF chunk
2683          uint8_t* pData = (uint8_t*) _3ewg->LoadChunkData();          uint8_t* pData = (uint8_t*) _3ewg->LoadChunkData();
2684          memcpy(&pData[0], &EffectSend, 2);          store16(&pData[0], EffectSend);
2685          memcpy(&pData[2], &Attenuation, 4);          store32(&pData[2], Attenuation);
2686          memcpy(&pData[6], &FineTune, 2);          store16(&pData[6], FineTune);
2687          memcpy(&pData[8], &PitchbendRange, 2);          store16(&pData[8], PitchbendRange);
2688          const uint8_t dimkeystart = (PianoReleaseMode) ? 0x01 : 0x00 |          const uint8_t dimkeystart = (PianoReleaseMode) ? 0x01 : 0x00 |
2689                                      DimensionKeyRange.low << 1;                                      DimensionKeyRange.low << 1;
2690          memcpy(&pData[10], &dimkeystart, 1);          pData[10] = dimkeystart;
2691          memcpy(&pData[11], &DimensionKeyRange.high, 1);          pData[11] = DimensionKeyRange.high;
2692      }      }
2693    
2694      /**      /**
# Line 2669  namespace { Line 2759  namespace {
2759    
2760    
2761    
2762    // *************** Group ***************
2763    // *
2764    
2765        /** @brief Constructor.
2766         *
2767         * @param file   - pointer to the gig::File object
2768         * @param ck3gnm - pointer to 3gnm chunk associated with this group or
2769         *                 NULL if this is a new Group
2770         */
2771        Group::Group(File* file, RIFF::Chunk* ck3gnm) {
2772            pFile      = file;
2773            pNameChunk = ck3gnm;
2774            ::LoadString(pNameChunk, Name);
2775        }
2776    
2777        Group::~Group() {
2778            // remove the chunk associated with this group (if any)
2779            if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk);
2780        }
2781    
2782        /** @brief Update chunks with current group settings.
2783         *
2784         * Apply current Group field values to the respective chunks. You have
2785         * to call File::Save() to make changes persistent.
2786         *
2787         * Usually there is absolutely no need to call this method explicitly.
2788         * It will be called automatically when File::Save() was called.
2789         */
2790        void Group::UpdateChunks() {
2791            // make sure <3gri> and <3gnl> list chunks exist
2792            RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);
2793            if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI);
2794            RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);
2795            if (!_3gnl) _3gnl = pFile->pRIFF->AddSubList(LIST_TYPE_3GNL);
2796            // now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk
2797            ::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64);
2798        }
2799    
2800        /**
2801         * Returns the first Sample of this Group. You have to call this method
2802         * once before you use GetNextSample().
2803         *
2804         * <b>Notice:</b> this method might block for a long time, in case the
2805         * samples of this .gig file were not scanned yet
2806         *
2807         * @returns  pointer address to first Sample or NULL if there is none
2808         *           applied to this Group
2809         * @see      GetNextSample()
2810         */
2811        Sample* Group::GetFirstSample() {
2812            // FIXME: lazy und unsafe implementation, should be an autonomous iterator
2813            for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) {
2814                if (pSample->GetGroup() == this) return pSample;
2815            }
2816            return NULL;
2817        }
2818    
2819        /**
2820         * Returns the next Sample of the Group. You have to call
2821         * GetFirstSample() once before you can use this method. By calling this
2822         * method multiple times it iterates through the Samples assigned to
2823         * this Group.
2824         *
2825         * @returns  pointer address to the next Sample of this Group or NULL if
2826         *           end reached
2827         * @see      GetFirstSample()
2828         */
2829        Sample* Group::GetNextSample() {
2830            // FIXME: lazy und unsafe implementation, should be an autonomous iterator
2831            for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) {
2832                if (pSample->GetGroup() == this) return pSample;
2833            }
2834            return NULL;
2835        }
2836    
2837        /**
2838         * Move Sample given by \a pSample from another Group to this Group.
2839         */
2840        void Group::AddSample(Sample* pSample) {
2841            pSample->pGroup = this;
2842        }
2843    
2844        /**
2845         * Move all members of this group to another group (preferably the 1st
2846         * one except this). This method is called explicitly by
2847         * File::DeleteGroup() thus when a Group was deleted. This code was
2848         * intentionally not placed in the destructor!
2849         */
2850        void Group::MoveAll() {
2851            // get "that" other group first
2852            Group* pOtherGroup = NULL;
2853            for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) {
2854                if (pOtherGroup != this) break;
2855            }
2856            if (!pOtherGroup) throw Exception(
2857                "Could not move samples to another group, since there is no "
2858                "other Group. This is a bug, report it!"
2859            );
2860            // now move all samples of this group to the other group
2861            for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) {
2862                pOtherGroup->AddSample(pSample);
2863            }
2864        }
2865    
2866    
2867    
2868  // *************** File ***************  // *************** File ***************
2869  // *  // *
2870    
2871        const DLS::Info::FixedStringLength File::FixedStringLengths[] = {
2872            { CHUNK_ID_IARL, 256 },
2873            { CHUNK_ID_IART, 128 },
2874            { CHUNK_ID_ICMS, 128 },
2875            { CHUNK_ID_ICMT, 1024 },
2876            { CHUNK_ID_ICOP, 128 },
2877            { CHUNK_ID_ICRD, 128 },
2878            { CHUNK_ID_IENG, 128 },
2879            { CHUNK_ID_IGNR, 128 },
2880            { CHUNK_ID_IKEY, 128 },
2881            { CHUNK_ID_IMED, 128 },
2882            { CHUNK_ID_INAM, 128 },
2883            { CHUNK_ID_IPRD, 128 },
2884            { CHUNK_ID_ISBJ, 128 },
2885            { CHUNK_ID_ISFT, 128 },
2886            { CHUNK_ID_ISRC, 128 },
2887            { CHUNK_ID_ISRF, 128 },
2888            { CHUNK_ID_ITCH, 128 },
2889            { 0, 0 }
2890        };
2891    
2892      File::File() : DLS::File() {      File::File() : DLS::File() {
2893          pInfo->UseFixedLengthStrings = true;          pGroups = NULL;
2894            pInfo->FixedStringLengths = FixedStringLengths;
2895      }      }
2896    
2897      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
2898          pInfo->UseFixedLengthStrings = true;          pGroups = NULL;
2899            pInfo->FixedStringLengths = FixedStringLengths;
2900        }
2901    
2902        File::~File() {
2903            if (pGroups) {
2904                std::list<Group*>::iterator iter = pGroups->begin();
2905                std::list<Group*>::iterator end  = pGroups->end();
2906                while (iter != end) {
2907                    delete *iter;
2908                    ++iter;
2909                }
2910                delete pGroups;
2911            }
2912      }      }
2913    
2914      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 2723  namespace { Line 2954  namespace {
2954          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");
2955          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);
2956          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");
2957            if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation
2958          pSamples->erase(iter);          pSamples->erase(iter);
2959          delete pSample;          delete pSample;
2960      }      }
# Line 2732  namespace { Line 2964  namespace {
2964      }      }
2965    
2966      void File::LoadSamples(progress_t* pProgress) {      void File::LoadSamples(progress_t* pProgress) {
2967            // Groups must be loaded before samples, because samples will try
2968            // to resolve the group they belong to
2969            if (!pGroups) LoadGroups();
2970    
2971          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
2972    
2973          RIFF::File* file = pRIFF;          RIFF::File* file = pRIFF;
# Line 2855  namespace { Line 3091  namespace {
3091       * have to call Save() to make this persistent to the file.       * have to call Save() to make this persistent to the file.
3092       *       *
3093       * @param pInstrument - instrument to delete       * @param pInstrument - instrument to delete
3094       * @throws gig::Excption if given instrument could not be found       * @throws gig::Exception if given instrument could not be found
3095       */       */
3096      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
3097          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");
# Line 2895  namespace { Line 3131  namespace {
3131          }          }
3132      }      }
3133    
3134        Group* File::GetFirstGroup() {
3135            if (!pGroups) LoadGroups();
3136            // there must always be at least one group
3137            GroupsIterator = pGroups->begin();
3138            return *GroupsIterator;
3139        }
3140    
3141        Group* File::GetNextGroup() {
3142            if (!pGroups) return NULL;
3143            ++GroupsIterator;
3144            return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator;
3145        }
3146    
3147        /**
3148         * Returns the group with the given index.
3149         *
3150         * @param index - number of the sought group (0..n)
3151         * @returns sought group or NULL if there's no such group
3152         */
3153        Group* File::GetGroup(uint index) {
3154            if (!pGroups) LoadGroups();
3155            GroupsIterator = pGroups->begin();
3156            for (uint i = 0; GroupsIterator != pGroups->end(); i++) {
3157                if (i == index) return *GroupsIterator;
3158                ++GroupsIterator;
3159            }
3160            return NULL;
3161        }
3162    
3163        Group* File::AddGroup() {
3164            if (!pGroups) LoadGroups();
3165            // there must always be at least one group
3166            __ensureMandatoryChunksExist();
3167            Group* pGroup = new Group(this, NULL);
3168            pGroups->push_back(pGroup);
3169            return pGroup;
3170        }
3171    
3172        /** @brief Delete a group and its samples.
3173         *
3174         * This will delete the given Group object and all the samples that
3175         * belong to this group from the gig file. You have to call Save() to
3176         * make this persistent to the file.
3177         *
3178         * @param pGroup - group to delete
3179         * @throws gig::Exception if given group could not be found
3180         */
3181        void File::DeleteGroup(Group* pGroup) {
3182            if (!pGroups) LoadGroups();
3183            std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3184            if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");
3185            if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3186            // delete all members of this group
3187            for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) {
3188                DeleteSample(pSample);
3189            }
3190            // now delete this group object
3191            pGroups->erase(iter);
3192            delete pGroup;
3193        }
3194    
3195        /** @brief Delete a group.
3196         *
3197         * This will delete the given Group object from the gig file. All the
3198         * samples that belong to this group will not be deleted, but instead
3199         * be moved to another group. You have to call Save() to make this
3200         * persistent to the file.
3201         *
3202         * @param pGroup - group to delete
3203         * @throws gig::Exception if given group could not be found
3204         */
3205        void File::DeleteGroupOnly(Group* pGroup) {
3206            if (!pGroups) LoadGroups();
3207            std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3208            if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");
3209            if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3210            // move all members of this group to another group
3211            pGroup->MoveAll();
3212            pGroups->erase(iter);
3213            delete pGroup;
3214        }
3215    
3216        void File::LoadGroups() {
3217            if (!pGroups) pGroups = new std::list<Group*>;
3218            // try to read defined groups from file
3219            RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);
3220            if (lst3gri) {
3221                RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL);
3222                if (lst3gnl) {
3223                    RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk();
3224                    while (ck) {
3225                        if (ck->GetChunkID() == CHUNK_ID_3GNM) {
3226                            pGroups->push_back(new Group(this, ck));
3227                        }
3228                        ck = lst3gnl->GetNextSubChunk();
3229                    }
3230                }
3231            }
3232            // if there were no group(s), create at least the mandatory default group
3233            if (!pGroups->size()) {
3234                Group* pGroup = new Group(this, NULL);
3235                pGroup->Name = "Default Group";
3236                pGroups->push_back(pGroup);
3237            }
3238        }
3239    
3240        /**
3241         * Apply all the gig file's current instruments, samples, groups and settings
3242         * to the respective RIFF chunks. You have to call Save() to make changes
3243         * persistent.
3244         *
3245         * Usually there is absolutely no need to call this method explicitly.
3246         * It will be called automatically when File::Save() was called.
3247         *
3248         * @throws Exception - on errors
3249         */
3250        void File::UpdateChunks() {
3251            // first update base class's chunks
3252            DLS::File::UpdateChunks();
3253    
3254            // update group's chunks
3255            if (pGroups) {
3256                std::list<Group*>::iterator iter = pGroups->begin();
3257                std::list<Group*>::iterator end  = pGroups->end();
3258                for (; iter != end; ++iter) {
3259                    (*iter)->UpdateChunks();
3260                }
3261            }
3262        }
3263    
3264    
3265    
3266  // *************** Exception ***************  // *************** Exception ***************

Legend:
Removed from v.928  
changed lines
  Added in v.1180

  ViewVC Help
Powered by ViewVC