/[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 929 by schoenebeck, Tue Oct 24 22:24:45 2006 UTC revision 1106 by schoenebeck, Sun Mar 18 19:38:47 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-2006 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 286  namespace { Line 286  namespace {
286          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);
287          if (pCk3gix) {          if (pCk3gix) {
288              uint16_t iSampleGroup = pCk3gix->ReadInt16();              uint16_t iSampleGroup = pCk3gix->ReadInt16();
289              // caution: sample groups in .gig files are indexed (1..n) whereas Groups in libgig (0..n-1)              pGroup = pFile->GetGroup(iSampleGroup);
             pGroup = pFile->GetGroup(iSampleGroup - 1);  
290          } else { // '3gix' chunk missing          } else { // '3gix' chunk missing
291              // not assigned to a group by default              // by default assigned to that mandatory "Default Group"
292              pGroup = NULL;              pGroup = pFile->GetGroup(0);
293          }          }
294    
295          pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);          pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);
# Line 365  namespace { Line 364  namespace {
364       * Usually there is absolutely no need to call this method explicitly.       * Usually there is absolutely no need to call this method explicitly.
365       * It will be called automatically when File::Save() was called.       * It will be called automatically when File::Save() was called.
366       *       *
367       * @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
368       *                        was provided yet       *                        was provided yet
369       * @throws gig::Exception if there is any invalid sample setting       * @throws gig::Exception if there is any invalid sample setting
370       */       */
# Line 401  namespace { Line 400  namespace {
400          pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX);          pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX);
401          if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4);          if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4);
402          // determine appropriate sample group index (to be stored in chunk)          // determine appropriate sample group index (to be stored in chunk)
403          uint16_t iSampleGroup = 0; // no sample group by default          uint16_t iSampleGroup = 0; // 0 refers to default sample group
404          File* pFile = static_cast<File*>(pParent);          File* pFile = static_cast<File*>(pParent);
405          if (pFile->pGroups) {          if (pFile->pGroups) {
406              std::list<Group*>::iterator iter = pFile->pGroups->begin();              std::list<Group*>::iterator iter = pFile->pGroups->begin();
407              std::list<Group*>::iterator end  = pFile->pGroups->end();              std::list<Group*>::iterator end  = pFile->pGroups->end();
408              // caution: sample groups in .gig files are indexed (1..n) whereas Groups in libgig (0..n-1)              for (int i = 0; iter != end; i++, iter++) {
             for (int i = 1; iter != end; i++, iter++) {  
409                  if (*iter == pGroup) {                  if (*iter == pGroup) {
410                      iSampleGroup = i;                      iSampleGroup = i;
411                      break; // found                      break; // found
# Line 637  namespace { Line 635  namespace {
635       * enlarged samples before calling File::Save() as this might exceed the       * enlarged samples before calling File::Save() as this might exceed the
636       * current sample's boundary!       * current sample's boundary!
637       *       *
638       * Also note: only WAVE_FORMAT_PCM is currently supported, that is       * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
639       * FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
640       * other formats will fail!       * other formats will fail!
641       *       *
642       * @param iNewSize - new sample wave data size in sample points (must be       * @param iNewSize - new sample wave data size in sample points (must be
643       *                   greater than zero)       *                   greater than zero)
644       * @throws DLS::Excecption if FormatTag != WAVE_FORMAT_PCM       * @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM
645       *                         or if \a iNewSize is less than 1       *                         or if \a iNewSize is less than 1
646       * @throws gig::Exception if existing sample is compressed       * @throws gig::Exception if existing sample is compressed
647       * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize,       * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize,
# Line 1154  namespace { Line 1152  namespace {
1152          }          }
1153      }      }
1154    
1155        /**
1156         * Returns pointer to the Group this Sample belongs to. In the .gig
1157         * format a sample always belongs to one group. If it wasn't explicitly
1158         * assigned to a certain group, it will be automatically assigned to a
1159         * default group.
1160         *
1161         * @returns Sample's Group (never NULL)
1162         */
1163        Group* Sample::GetGroup() const {
1164            return pGroup;
1165        }
1166    
1167      Sample::~Sample() {      Sample::~Sample() {
1168          Instances--;          Instances--;
1169          if (!Instances && InternalDecompressionBuffer.Size) {          if (!Instances && InternalDecompressionBuffer.Size) {
# Line 1328  namespace { Line 1338  namespace {
1338                  if (lfo3ctrl & 0x40) // bit 6                  if (lfo3ctrl & 0x40) // bit 6
1339                      VCFType = vcf_type_lowpassturbo;                      VCFType = vcf_type_lowpassturbo;
1340              }              }
1341                if (_3ewa->RemainingBytes() >= 8) {
1342                    _3ewa->Read(DimensionUpperLimits, 1, 8);
1343                } else {
1344                    memset(DimensionUpperLimits, 0, 8);
1345                }
1346          } else { // '3ewa' chunk does not exist yet          } else { // '3ewa' chunk does not exist yet
1347              // use default values              // use default values
1348              LFO3Frequency                   = 1.0;              LFO3Frequency                   = 1.0;
# Line 1408  namespace { Line 1423  namespace {
1423              VCFVelocityDynamicRange         = 0x04;              VCFVelocityDynamicRange         = 0x04;
1424              VCFVelocityCurve                = curve_type_linear;              VCFVelocityCurve                = curve_type_linear;
1425              VCFType                         = vcf_type_lowpass;              VCFType                         = vcf_type_lowpass;
1426                memset(DimensionUpperLimits, 0, 8);
1427          }          }
1428    
1429          pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve,          pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve,
# Line 1463  namespace { Line 1479  namespace {
1479    
1480          // update '3ewa' chunk with DimensionRegion's current settings          // update '3ewa' chunk with DimensionRegion's current settings
1481    
1482          const uint32_t unknown = _3ewa->GetSize(); // unknown, always chunk size ?          const uint32_t chunksize = _3ewa->GetSize();
1483          memcpy(&pData[0], &unknown, 4);          memcpy(&pData[0], &chunksize, 4); // unknown, always chunk size?
1484    
1485          const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency);          const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency);
1486          memcpy(&pData[4], &lfo3freq, 4);          memcpy(&pData[4], &lfo3freq, 4);
# Line 1719  namespace { Line 1735  namespace {
1735    
1736          const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType;          const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType;
1737          memcpy(&pData[139], &vcftype, 1);          memcpy(&pData[139], &vcftype, 1);
1738    
1739            if (chunksize >= 148) {
1740                memcpy(&pData[140], DimensionUpperLimits, 8);
1741            }
1742      }      }
1743    
1744      // 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 2100  namespace { Line 2120  namespace {
2120                                                             dimension == dimension_releasetrigger ||                                                             dimension == dimension_releasetrigger ||
2121                                                             dimension == dimension_keyboard ||                                                             dimension == dimension_keyboard ||
2122                                                             dimension == dimension_roundrobin ||                                                             dimension == dimension_roundrobin ||
2123                                                             dimension == dimension_random) ? split_type_bit                                                             dimension == dimension_random ||
2124                                                                                            : split_type_normal;                                                             dimension == dimension_smartmidi ||
2125                                                               dimension == dimension_roundrobinkeyboard) ? split_type_bit
2126                                                                                                          : split_type_normal;
2127                      pDimensionDefinitions[i].zone_size  =                      pDimensionDefinitions[i].zone_size  =
2128                          (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones                          (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones
2129                                                                                     : 0;                                                                                     : 0;
# Line 2130  namespace { Line 2152  namespace {
2152                  if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);                  if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);
2153              }              }
2154              GetSample(); // load global region sample reference              GetSample(); // load global region sample reference
2155            } else {
2156                DimensionRegions = 0;
2157          }          }
2158    
2159          // make sure there is at least one dimension region          // make sure there is at least one dimension region
# Line 2152  namespace { Line 2176  namespace {
2176       * @throws gig::Exception if samples cannot be dereferenced       * @throws gig::Exception if samples cannot be dereferenced
2177       */       */
2178      void Region::UpdateChunks() {      void Region::UpdateChunks() {
2179            // in the gig format we don't care about the Region's sample reference
2180            // but we still have to provide some existing one to not corrupt the
2181            // file, so to avoid the latter we simply always assign the sample of
2182            // the first dimension region of this region
2183            pSample = pDimensionRegions[0]->pSample;
2184    
2185          // first update base class's chunks          // first update base class's chunks
2186          DLS::Region::UpdateChunks();          DLS::Region::UpdateChunks();
2187    
# Line 2238  namespace { Line 2268  namespace {
2268          int dim[8] = { 0 };          int dim[8] = { 0 };
2269          for (int i = 0 ; i < DimensionRegions ; i++) {          for (int i = 0 ; i < DimensionRegions ; i++) {
2270    
2271              if (pDimensionRegions[i]->VelocityUpperLimit) {              if (pDimensionRegions[i]->DimensionUpperLimits[veldim] ||
2272                    pDimensionRegions[i]->VelocityUpperLimit) {
2273                  // create the velocity table                  // create the velocity table
2274                  uint8_t* table = pDimensionRegions[i]->VelocityTable;                  uint8_t* table = pDimensionRegions[i]->VelocityTable;
2275                  if (!table) {                  if (!table) {
# Line 2247  namespace { Line 2278  namespace {
2278                  }                  }
2279                  int tableidx = 0;                  int tableidx = 0;
2280                  int velocityZone = 0;                  int velocityZone = 0;
2281                  for (int k = i ; k < end ; k += step) {                  if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3
2282                      DimensionRegion *d = pDimensionRegions[k];                      for (int k = i ; k < end ; k += step) {
2283                      for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;                          DimensionRegion *d = pDimensionRegions[k];
2284                      velocityZone++;                          for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone;
2285                            velocityZone++;
2286                        }
2287                    } else { // gig2
2288                        for (int k = i ; k < end ; k += step) {
2289                            DimensionRegion *d = pDimensionRegions[k];
2290                            for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2291                            velocityZone++;
2292                        }
2293                  }                  }
2294              } else {              } else {
2295                  if (pDimensionRegions[i]->VelocityTable) {                  if (pDimensionRegions[i]->VelocityTable) {
# Line 2445  namespace { Line 2484  namespace {
2484              } else {              } else {
2485                  switch (pDimensionDefinitions[i].split_type) {                  switch (pDimensionDefinitions[i].split_type) {
2486                      case split_type_normal:                      case split_type_normal:
2487                          bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);                          if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
2488                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
2489                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
2490                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
2491                                }
2492                            } else {
2493                                // gig2: evenly sized zones
2494                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
2495                            }
2496                          break;                          break;
2497                      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
2498                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
# Line 2459  namespace { Line 2506  namespace {
2506          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx];
2507          if (veldim != -1) {          if (veldim != -1) {
2508              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
2509              if (dimreg->VelocityUpperLimit) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
2510                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim]];
2511              else // normal split type              else // normal split type
2512                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);
# Line 2690  namespace { Line 2737  namespace {
2737    
2738      /** @brief Constructor.      /** @brief Constructor.
2739       *       *
2740       * @param file   - pointer to the RIFF::File object of this .gig file       * @param file   - pointer to the gig::File object
2741       * @param ck3gnm - pointer to 3gnm chunk associated with this group       * @param ck3gnm - pointer to 3gnm chunk associated with this group or
2742         *                 NULL if this is a new Group
2743       */       */
2744      Group::Group(RIFF::File* file, RIFF::Chunk* ck3gnm) {      Group::Group(File* file, RIFF::Chunk* ck3gnm) {
2745          pFile      = file;          pFile      = file;
2746          pNameChunk = ck3gnm;          pNameChunk = ck3gnm;
2747          ::LoadString(pNameChunk, Name);          ::LoadString(pNameChunk, Name);
2748      }      }
2749    
2750      Group::~Group() {      Group::~Group() {
2751            // remove the chunk associated with this group (if any)
2752            if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk);
2753      }      }
2754    
2755      /** @brief Update chunks with current group settings.      /** @brief Update chunks with current group settings.
2756       *       *
2757       * Apply current Group field values to the respective. You have to call       * Apply current Group field values to the respective chunks. You have
2758       * File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
2759         *
2760         * Usually there is absolutely no need to call this method explicitly.
2761         * It will be called automatically when File::Save() was called.
2762       */       */
2763      void Group::UpdateChunks() {      void Group::UpdateChunks() {
2764          // make sure <3gri> and <3gnl> list chunks exist          // make sure <3gri> and <3gnl> list chunks exist
2765          RIFF::List* _3gri = pFile->GetSubList(LIST_TYPE_3GRI);          RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);
2766          if (!_3gri) _3gri = pFile->AddSubList(LIST_TYPE_3GRI);          if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI);
2767          RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);          RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);
2768          if (!_3gnl) _3gnl = pFile->AddSubList(LIST_TYPE_3GNL);          if (!_3gnl) _3gnl = pFile->pRIFF->AddSubList(LIST_TYPE_3GNL);
2769          // now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk          // now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk
2770          ::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64);          ::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64);
2771      }      }
2772    
2773        /**
2774         * Returns the first Sample of this Group. You have to call this method
2775         * once before you use GetNextSample().
2776         *
2777         * <b>Notice:</b> this method might block for a long time, in case the
2778         * samples of this .gig file were not scanned yet
2779         *
2780         * @returns  pointer address to first Sample or NULL if there is none
2781         *           applied to this Group
2782         * @see      GetNextSample()
2783         */
2784        Sample* Group::GetFirstSample() {
2785            // FIXME: lazy und unsafe implementation, should be an autonomous iterator
2786            for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) {
2787                if (pSample->GetGroup() == this) return pSample;
2788            }
2789            return NULL;
2790        }
2791    
2792        /**
2793         * Returns the next Sample of the Group. You have to call
2794         * GetFirstSample() once before you can use this method. By calling this
2795         * method multiple times it iterates through the Samples assigned to
2796         * this Group.
2797         *
2798         * @returns  pointer address to the next Sample of this Group or NULL if
2799         *           end reached
2800         * @see      GetFirstSample()
2801         */
2802        Sample* Group::GetNextSample() {
2803            // FIXME: lazy und unsafe implementation, should be an autonomous iterator
2804            for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) {
2805                if (pSample->GetGroup() == this) return pSample;
2806            }
2807            return NULL;
2808        }
2809    
2810        /**
2811         * Move Sample given by \a pSample from another Group to this Group.
2812         */
2813        void Group::AddSample(Sample* pSample) {
2814            pSample->pGroup = this;
2815        }
2816    
2817        /**
2818         * Move all members of this group to another group (preferably the 1st
2819         * one except this). This method is called explicitly by
2820         * File::DeleteGroup() thus when a Group was deleted. This code was
2821         * intentionally not placed in the destructor!
2822         */
2823        void Group::MoveAll() {
2824            // get "that" other group first
2825            Group* pOtherGroup = NULL;
2826            for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) {
2827                if (pOtherGroup != this) break;
2828            }
2829            if (!pOtherGroup) throw Exception(
2830                "Could not move samples to another group, since there is no "
2831                "other Group. This is a bug, report it!"
2832            );
2833            // now move all samples of this group to the other group
2834            for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) {
2835                pOtherGroup->AddSample(pSample);
2836            }
2837        }
2838    
2839    
2840    
2841  // *************** File ***************  // *************** File ***************
# Line 2787  namespace { Line 2906  namespace {
2906          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");
2907          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);
2908          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");
2909            if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation
2910          pSamples->erase(iter);          pSamples->erase(iter);
2911          delete pSample;          delete pSample;
2912      }      }
# Line 2796  namespace { Line 2916  namespace {
2916      }      }
2917    
2918      void File::LoadSamples(progress_t* pProgress) {      void File::LoadSamples(progress_t* pProgress) {
2919            // Groups must be loaded before samples, because samples will try
2920            // to resolve the group they belong to
2921            LoadGroups();
2922    
2923          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
2924    
2925          RIFF::File* file = pRIFF;          RIFF::File* file = pRIFF;
# Line 2919  namespace { Line 3043  namespace {
3043       * have to call Save() to make this persistent to the file.       * have to call Save() to make this persistent to the file.
3044       *       *
3045       * @param pInstrument - instrument to delete       * @param pInstrument - instrument to delete
3046       * @throws gig::Excption if given instrument could not be found       * @throws gig::Exception if given instrument could not be found
3047       */       */
3048      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
3049          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 2961  namespace { Line 3085  namespace {
3085    
3086      Group* File::GetFirstGroup() {      Group* File::GetFirstGroup() {
3087          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
3088          if (!pGroups) return NULL;          // there must always be at least one group
3089          GroupsIterator = pGroups->begin();          GroupsIterator = pGroups->begin();
3090          return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator;          return *GroupsIterator;
3091      }      }
3092    
3093      Group* File::GetNextGroup() {      Group* File::GetNextGroup() {
# Line 2980  namespace { Line 3104  namespace {
3104       */       */
3105      Group* File::GetGroup(uint index) {      Group* File::GetGroup(uint index) {
3106          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
         if (!pGroups) return NULL;  
3107          GroupsIterator = pGroups->begin();          GroupsIterator = pGroups->begin();
3108          for (uint i = 0; GroupsIterator != pGroups->end(); i++) {          for (uint i = 0; GroupsIterator != pGroups->end(); i++) {
3109              if (i == index) return *GroupsIterator;              if (i == index) return *GroupsIterator;
# Line 2991  namespace { Line 3114  namespace {
3114    
3115      Group* File::AddGroup() {      Group* File::AddGroup() {
3116          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
3117          if (!pGroups) pGroups = new std::list<Group*>;          // there must always be at least one group
3118          __ensureMandatoryChunksExist();          __ensureMandatoryChunksExist();
3119          Group* pGroup = new Group(pRIFF, NULL);          Group* pGroup = new Group(this, NULL);
3120          pGroups->push_back(pGroup);          pGroups->push_back(pGroup);
3121          return pGroup;          return pGroup;
3122      }      }
3123    
3124        /** @brief Delete a group and its samples.
3125         *
3126         * This will delete the given Group object and all the samples that
3127         * belong to this group from the gig file. You have to call Save() to
3128         * make this persistent to the file.
3129         *
3130         * @param pGroup - group to delete
3131         * @throws gig::Exception if given group could not be found
3132         */
3133      void File::DeleteGroup(Group* pGroup) {      void File::DeleteGroup(Group* pGroup) {
3134          if (!pGroups) throw gig::Exception("Could not delete group as there are no groups");          if (!pGroups) LoadGroups();
3135            std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3136            if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");
3137            if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3138            // delete all members of this group
3139            for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) {
3140                DeleteSample(pSample);
3141            }
3142            // now delete this group object
3143            pGroups->erase(iter);
3144            delete pGroup;
3145        }
3146    
3147        /** @brief Delete a group.
3148         *
3149         * This will delete the given Group object from the gig file. All the
3150         * samples that belong to this group will not be deleted, but instead
3151         * be moved to another group. You have to call Save() to make this
3152         * persistent to the file.
3153         *
3154         * @param pGroup - group to delete
3155         * @throws gig::Exception if given group could not be found
3156         */
3157        void File::DeleteGroupOnly(Group* pGroup) {
3158            if (!pGroups) LoadGroups();
3159          std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);          std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3160          if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");          if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");
3161            if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3162            // move all members of this group to another group
3163            pGroup->MoveAll();
3164          pGroups->erase(iter);          pGroups->erase(iter);
3165          delete pGroup;          delete pGroup;
3166      }      }
3167    
3168      void File::LoadGroups() {      void File::LoadGroups() {
3169          if (!pGroups) pGroups = new std::list<Group*>;          if (!pGroups) pGroups = new std::list<Group*>;
3170            // try to read defined groups from file
3171          RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);          RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);
3172          if (!lst3gri) return;          if (lst3gri) {
3173          RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL);              RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL);
3174          if (!lst3gnl) return;              if (lst3gnl) {
3175          {                  RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk();
3176              RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk();                  while (ck) {
3177              while (ck) {                      if (ck->GetChunkID() == CHUNK_ID_3GNM) {
3178                  if (ck->GetChunkID() == CHUNK_ID_3GNM) {                          pGroups->push_back(new Group(this, ck));
3179                      pGroups->push_back(new Group(pRIFF, ck));                      }
3180                        ck = lst3gnl->GetNextSubChunk();
3181                  }                  }
3182                  ck = lst3gnl->GetNextSubChunk();              }
3183            }
3184            // if there were no group(s), create at least the mandatory default group
3185            if (!pGroups->size()) {
3186                Group* pGroup = new Group(this, NULL);
3187                pGroup->Name = "Default Group";
3188                pGroups->push_back(pGroup);
3189            }
3190        }
3191    
3192        /**
3193         * Apply all the gig file's current instruments, samples, groups and settings
3194         * to the respective RIFF chunks. You have to call Save() to make changes
3195         * persistent.
3196         *
3197         * Usually there is absolutely no need to call this method explicitly.
3198         * It will be called automatically when File::Save() was called.
3199         *
3200         * @throws Exception - on errors
3201         */
3202        void File::UpdateChunks() {
3203            // first update base class's chunks
3204            DLS::File::UpdateChunks();
3205    
3206            // update group's chunks
3207            if (pGroups) {
3208                std::list<Group*>::iterator iter = pGroups->begin();
3209                std::list<Group*>::iterator end  = pGroups->end();
3210                for (; iter != end; ++iter) {
3211                    (*iter)->UpdateChunks();
3212              }              }
3213          }          }
3214      }      }

Legend:
Removed from v.929  
changed lines
  Added in v.1106

  ViewVC Help
Powered by ViewVC