/[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 930 by schoenebeck, Sun Oct 29 17:57:20 2006 UTC revision 1102 by persson, Sun Mar 18 07:13:06 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 364  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 635  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 1338  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 1418  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 1473  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 1729  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 2110  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 2140  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 2248  namespace { Line 2262  namespace {
2262          int dim[8] = { 0 };          int dim[8] = { 0 };
2263          for (int i = 0 ; i < DimensionRegions ; i++) {          for (int i = 0 ; i < DimensionRegions ; i++) {
2264    
2265              if (pDimensionRegions[i]->VelocityUpperLimit) {              if (pDimensionRegions[i]->DimensionUpperLimits[veldim] ||
2266                    pDimensionRegions[i]->VelocityUpperLimit) {
2267                  // create the velocity table                  // create the velocity table
2268                  uint8_t* table = pDimensionRegions[i]->VelocityTable;                  uint8_t* table = pDimensionRegions[i]->VelocityTable;
2269                  if (!table) {                  if (!table) {
# Line 2257  namespace { Line 2272  namespace {
2272                  }                  }
2273                  int tableidx = 0;                  int tableidx = 0;
2274                  int velocityZone = 0;                  int velocityZone = 0;
2275                  for (int k = i ; k < end ; k += step) {                  if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3
2276                      DimensionRegion *d = pDimensionRegions[k];                      for (int k = i ; k < end ; k += step) {
2277                      for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;                          DimensionRegion *d = pDimensionRegions[k];
2278                      velocityZone++;                          for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone;
2279                            velocityZone++;
2280                        }
2281                    } else { // gig2
2282                        for (int k = i ; k < end ; k += step) {
2283                            DimensionRegion *d = pDimensionRegions[k];
2284                            for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2285                            velocityZone++;
2286                        }
2287                  }                  }
2288              } else {              } else {
2289                  if (pDimensionRegions[i]->VelocityTable) {                  if (pDimensionRegions[i]->VelocityTable) {
# Line 2455  namespace { Line 2478  namespace {
2478              } else {              } else {
2479                  switch (pDimensionDefinitions[i].split_type) {                  switch (pDimensionDefinitions[i].split_type) {
2480                      case split_type_normal:                      case split_type_normal:
2481                          bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);                          if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
2482                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
2483                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
2484                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
2485                                }
2486                            } else {
2487                                // gig2: evenly sized zones
2488                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
2489                            }
2490                          break;                          break;
2491                      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
2492                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
# Line 2469  namespace { Line 2500  namespace {
2500          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx];
2501          if (veldim != -1) {          if (veldim != -1) {
2502              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
2503              if (dimreg->VelocityUpperLimit) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
2504                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim]];
2505              else // normal split type              else // normal split type
2506                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);
# Line 2711  namespace { Line 2742  namespace {
2742      }      }
2743    
2744      Group::~Group() {      Group::~Group() {
2745            // remove the chunk associated with this group (if any)
2746            if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk);
2747      }      }
2748    
2749      /** @brief Update chunks with current group settings.      /** @brief Update chunks with current group settings.
2750       *       *
2751       * Apply current Group field values to the respective. You have to call       * Apply current Group field values to the respective chunks. You have
2752       * File::Save() to make changes persistent.       * to call File::Save() to make changes persistent.
2753         *
2754         * Usually there is absolutely no need to call this method explicitly.
2755         * It will be called automatically when File::Save() was called.
2756       */       */
2757      void Group::UpdateChunks() {      void Group::UpdateChunks() {
2758          // make sure <3gri> and <3gnl> list chunks exist          // make sure <3gri> and <3gnl> list chunks exist
# Line 2864  namespace { Line 2900  namespace {
2900          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");
2901          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);
2902          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");
2903            if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation
2904          pSamples->erase(iter);          pSamples->erase(iter);
2905          delete pSample;          delete pSample;
2906      }      }
# Line 3000  namespace { Line 3037  namespace {
3037       * have to call Save() to make this persistent to the file.       * have to call Save() to make this persistent to the file.
3038       *       *
3039       * @param pInstrument - instrument to delete       * @param pInstrument - instrument to delete
3040       * @throws gig::Excption if given instrument could not be found       * @throws gig::Exception if given instrument could not be found
3041       */       */
3042      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
3043          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 3078  namespace { Line 3115  namespace {
3115          return pGroup;          return pGroup;
3116      }      }
3117    
3118        /** @brief Delete a group and its samples.
3119         *
3120         * This will delete the given Group object and all the samples that
3121         * belong to this group from the gig file. You have to call Save() to
3122         * make this persistent to the file.
3123         *
3124         * @param pGroup - group to delete
3125         * @throws gig::Exception if given group could not be found
3126         */
3127      void File::DeleteGroup(Group* pGroup) {      void File::DeleteGroup(Group* pGroup) {
3128          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
3129          std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);          std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3130          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");
3131          if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");          if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3132            // delete all members of this group
3133            for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) {
3134                DeleteSample(pSample);
3135            }
3136            // now delete this group object
3137            pGroups->erase(iter);
3138            delete pGroup;
3139        }
3140    
3141        /** @brief Delete a group.
3142         *
3143         * This will delete the given Group object from the gig file. All the
3144         * samples that belong to this group will not be deleted, but instead
3145         * be moved to another group. You have to call Save() to make this
3146         * persistent to the file.
3147         *
3148         * @param pGroup - group to delete
3149         * @throws gig::Exception if given group could not be found
3150         */
3151        void File::DeleteGroupOnly(Group* pGroup) {
3152            if (!pGroups) LoadGroups();
3153            std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3154            if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");
3155            if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3156          // move all members of this group to another group          // move all members of this group to another group
3157          pGroup->MoveAll();          pGroup->MoveAll();
3158          pGroups->erase(iter);          pGroups->erase(iter);
# Line 3113  namespace { Line 3183  namespace {
3183          }          }
3184      }      }
3185    
3186        /**
3187         * Apply all the gig file's current instruments, samples, groups and settings
3188         * to the respective RIFF chunks. You have to call Save() to make changes
3189         * persistent.
3190         *
3191         * Usually there is absolutely no need to call this method explicitly.
3192         * It will be called automatically when File::Save() was called.
3193         *
3194         * @throws Exception - on errors
3195         */
3196        void File::UpdateChunks() {
3197            // first update base class's chunks
3198            DLS::File::UpdateChunks();
3199    
3200            // update group's chunks
3201            if (pGroups) {
3202                std::list<Group*>::iterator iter = pGroups->begin();
3203                std::list<Group*>::iterator end  = pGroups->end();
3204                for (; iter != end; ++iter) {
3205                    (*iter)->UpdateChunks();
3206                }
3207            }
3208        }
3209    
3210    
3211    
3212  // *************** Exception ***************  // *************** Exception ***************

Legend:
Removed from v.930  
changed lines
  Added in v.1102

  ViewVC Help
Powered by ViewVC