/[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 918 by persson, Sat Sep 2 08:45:37 2006 UTC revision 1076 by persson, Tue Mar 6 18:33:30 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 285  namespace { Line 285  namespace {
285    
286          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);          pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX);
287          if (pCk3gix) {          if (pCk3gix) {
288              SampleGroup = pCk3gix->ReadInt16();              uint16_t iSampleGroup = pCk3gix->ReadInt16();
289                pGroup = pFile->GetGroup(iSampleGroup);
290          } else { // '3gix' chunk missing          } else { // '3gix' chunk missing
291              // use default value(s)              // by default assigned to that mandatory "Default Group"
292              SampleGroup = 0;              pGroup = pFile->GetGroup(0);
293          }          }
294    
295          pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);          pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL);
# Line 312  namespace { Line 313  namespace {
313              // use default values              // use default values
314              Manufacturer  = 0;              Manufacturer  = 0;
315              Product       = 0;              Product       = 0;
316              SamplePeriod  = 1 / SamplesPerSecond;              SamplePeriod  = uint32_t(1000000000.0 / SamplesPerSecond + 0.5);
317              MIDIUnityNote = 64;              MIDIUnityNote = 64;
318              FineTune      = 0;              FineTune      = 0;
319              SMPTEOffset   = 0;              SMPTEOffset   = 0;
# Line 363  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 398  namespace { Line 399  namespace {
399          // make sure '3gix' chunk exists          // make sure '3gix' chunk exists
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)
403            uint16_t iSampleGroup = 0; // 0 refers to default sample group
404            File* pFile = static_cast<File*>(pParent);
405            if (pFile->pGroups) {
406                std::list<Group*>::iterator iter = pFile->pGroups->begin();
407                std::list<Group*>::iterator end  = pFile->pGroups->end();
408                for (int i = 0; iter != end; i++, iter++) {
409                    if (*iter == pGroup) {
410                        iSampleGroup = i;
411                        break; // found
412                    }
413                }
414            }
415          // update '3gix' chunk          // update '3gix' chunk
416          pData = (uint8_t*) pCk3gix->LoadChunkData();          pData = (uint8_t*) pCk3gix->LoadChunkData();
417          memcpy(&pData[0], &SampleGroup, 2);          memcpy(&pData[0], &iSampleGroup, 2);
418      }      }
419    
420      /// 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 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 1138  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 1312  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 1392  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 1447  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 1703  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 2084  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 2222  namespace { Line 2260  namespace {
2260          int dim[8] = { 0 };          int dim[8] = { 0 };
2261          for (int i = 0 ; i < DimensionRegions ; i++) {          for (int i = 0 ; i < DimensionRegions ; i++) {
2262    
2263              if (pDimensionRegions[i]->VelocityUpperLimit) {              if (pDimensionRegions[i]->DimensionUpperLimits[veldim] ||
2264                    pDimensionRegions[i]->VelocityUpperLimit) {
2265                  // create the velocity table                  // create the velocity table
2266                  uint8_t* table = pDimensionRegions[i]->VelocityTable;                  uint8_t* table = pDimensionRegions[i]->VelocityTable;
2267                  if (!table) {                  if (!table) {
# Line 2231  namespace { Line 2270  namespace {
2270                  }                  }
2271                  int tableidx = 0;                  int tableidx = 0;
2272                  int velocityZone = 0;                  int velocityZone = 0;
2273                  for (int k = i ; k < end ; k += step) {                  if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3
2274                      DimensionRegion *d = pDimensionRegions[k];                      for (int k = i ; k < end ; k += step) {
2275                      for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;                          DimensionRegion *d = pDimensionRegions[k];
2276                      velocityZone++;                          for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone;
2277                            velocityZone++;
2278                        }
2279                    } else { // gig2
2280                        for (int k = i ; k < end ; k += step) {
2281                            DimensionRegion *d = pDimensionRegions[k];
2282                            for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2283                            velocityZone++;
2284                        }
2285                  }                  }
2286              } else {              } else {
2287                  if (pDimensionRegions[i]->VelocityTable) {                  if (pDimensionRegions[i]->VelocityTable) {
# Line 2429  namespace { Line 2476  namespace {
2476              } else {              } else {
2477                  switch (pDimensionDefinitions[i].split_type) {                  switch (pDimensionDefinitions[i].split_type) {
2478                      case split_type_normal:                      case split_type_normal:
2479                          bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);                          if (pDimensionRegions[0]->DimensionUpperLimits[i]) {
2480                                // gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges
2481                                for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) {
2482                                    if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break;
2483                                }
2484                            } else {
2485                                // gig2: evenly sized zones
2486                                bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
2487                            }
2488                          break;                          break;
2489                      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
2490                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
# Line 2443  namespace { Line 2498  namespace {
2498          DimensionRegion* dimreg = pDimensionRegions[dimregidx];          DimensionRegion* dimreg = pDimensionRegions[dimregidx];
2499          if (veldim != -1) {          if (veldim != -1) {
2500              // (dimreg is now the dimension region for the lowest velocity)              // (dimreg is now the dimension region for the lowest velocity)
2501              if (dimreg->VelocityUpperLimit) // custom defined zone ranges              if (dimreg->VelocityTable) // custom defined zone ranges
2502                  bits = dimreg->VelocityTable[DimValues[veldim]];                  bits = dimreg->VelocityTable[DimValues[veldim]];
2503              else // normal split type              else // normal split type
2504                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);                  bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);
# Line 2669  namespace { Line 2724  namespace {
2724    
2725    
2726    
2727    // *************** Group ***************
2728    // *
2729    
2730        /** @brief Constructor.
2731         *
2732         * @param file   - pointer to the gig::File object
2733         * @param ck3gnm - pointer to 3gnm chunk associated with this group or
2734         *                 NULL if this is a new Group
2735         */
2736        Group::Group(File* file, RIFF::Chunk* ck3gnm) {
2737            pFile      = file;
2738            pNameChunk = ck3gnm;
2739            ::LoadString(pNameChunk, Name);
2740        }
2741    
2742        Group::~Group() {
2743        }
2744    
2745        /** @brief Update chunks with current group settings.
2746         *
2747         * Apply current Group field values to the respective. You have to call
2748         * File::Save() to make changes persistent.
2749         */
2750        void Group::UpdateChunks() {
2751            // make sure <3gri> and <3gnl> list chunks exist
2752            RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI);
2753            if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI);
2754            RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL);
2755            if (!_3gnl) _3gnl = pFile->pRIFF->AddSubList(LIST_TYPE_3GNL);
2756            // now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk
2757            ::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64);
2758        }
2759    
2760        /**
2761         * Returns the first Sample of this Group. You have to call this method
2762         * once before you use GetNextSample().
2763         *
2764         * <b>Notice:</b> this method might block for a long time, in case the
2765         * samples of this .gig file were not scanned yet
2766         *
2767         * @returns  pointer address to first Sample or NULL if there is none
2768         *           applied to this Group
2769         * @see      GetNextSample()
2770         */
2771        Sample* Group::GetFirstSample() {
2772            // FIXME: lazy und unsafe implementation, should be an autonomous iterator
2773            for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) {
2774                if (pSample->GetGroup() == this) return pSample;
2775            }
2776            return NULL;
2777        }
2778    
2779        /**
2780         * Returns the next Sample of the Group. You have to call
2781         * GetFirstSample() once before you can use this method. By calling this
2782         * method multiple times it iterates through the Samples assigned to
2783         * this Group.
2784         *
2785         * @returns  pointer address to the next Sample of this Group or NULL if
2786         *           end reached
2787         * @see      GetFirstSample()
2788         */
2789        Sample* Group::GetNextSample() {
2790            // FIXME: lazy und unsafe implementation, should be an autonomous iterator
2791            for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) {
2792                if (pSample->GetGroup() == this) return pSample;
2793            }
2794            return NULL;
2795        }
2796    
2797        /**
2798         * Move Sample given by \a pSample from another Group to this Group.
2799         */
2800        void Group::AddSample(Sample* pSample) {
2801            pSample->pGroup = this;
2802        }
2803    
2804        /**
2805         * Move all members of this group to another group (preferably the 1st
2806         * one except this). This method is called explicitly by
2807         * File::DeleteGroup() thus when a Group was deleted. This code was
2808         * intentionally not placed in the destructor!
2809         */
2810        void Group::MoveAll() {
2811            // get "that" other group first
2812            Group* pOtherGroup = NULL;
2813            for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) {
2814                if (pOtherGroup != this) break;
2815            }
2816            if (!pOtherGroup) throw Exception(
2817                "Could not move samples to another group, since there is no "
2818                "other Group. This is a bug, report it!"
2819            );
2820            // now move all samples of this group to the other group
2821            for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) {
2822                pOtherGroup->AddSample(pSample);
2823            }
2824        }
2825    
2826    
2827    
2828  // *************** File ***************  // *************** File ***************
2829  // *  // *
2830    
2831      File::File() : DLS::File() {      File::File() : DLS::File() {
2832            pGroups = NULL;
2833          pInfo->UseFixedLengthStrings = true;          pInfo->UseFixedLengthStrings = true;
2834      }      }
2835    
2836      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
2837            pGroups = NULL;
2838          pInfo->UseFixedLengthStrings = true;          pInfo->UseFixedLengthStrings = true;
2839      }      }
2840    
2841        File::~File() {
2842            if (pGroups) {
2843                std::list<Group*>::iterator iter = pGroups->begin();
2844                std::list<Group*>::iterator end  = pGroups->end();
2845                while (iter != end) {
2846                    delete *iter;
2847                    ++iter;
2848                }
2849                delete pGroups;
2850            }
2851        }
2852    
2853      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
2854          if (!pSamples) LoadSamples(pProgress);          if (!pSamples) LoadSamples(pProgress);
2855          if (!pSamples) return NULL;          if (!pSamples) return NULL;
# Line 2732  namespace { Line 2902  namespace {
2902      }      }
2903    
2904      void File::LoadSamples(progress_t* pProgress) {      void File::LoadSamples(progress_t* pProgress) {
2905            // Groups must be loaded before samples, because samples will try
2906            // to resolve the group they belong to
2907            LoadGroups();
2908    
2909          if (!pSamples) pSamples = new SampleList;          if (!pSamples) pSamples = new SampleList;
2910    
2911          RIFF::File* file = pRIFF;          RIFF::File* file = pRIFF;
# Line 2895  namespace { Line 3069  namespace {
3069          }          }
3070      }      }
3071    
3072        Group* File::GetFirstGroup() {
3073            if (!pGroups) LoadGroups();
3074            // there must always be at least one group
3075            GroupsIterator = pGroups->begin();
3076            return *GroupsIterator;
3077        }
3078    
3079        Group* File::GetNextGroup() {
3080            if (!pGroups) return NULL;
3081            ++GroupsIterator;
3082            return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator;
3083        }
3084    
3085        /**
3086         * Returns the group with the given index.
3087         *
3088         * @param index - number of the sought group (0..n)
3089         * @returns sought group or NULL if there's no such group
3090         */
3091        Group* File::GetGroup(uint index) {
3092            if (!pGroups) LoadGroups();
3093            GroupsIterator = pGroups->begin();
3094            for (uint i = 0; GroupsIterator != pGroups->end(); i++) {
3095                if (i == index) return *GroupsIterator;
3096                ++GroupsIterator;
3097            }
3098            return NULL;
3099        }
3100    
3101        Group* File::AddGroup() {
3102            if (!pGroups) LoadGroups();
3103            // there must always be at least one group
3104            __ensureMandatoryChunksExist();
3105            Group* pGroup = new Group(this, NULL);
3106            pGroups->push_back(pGroup);
3107            return pGroup;
3108        }
3109    
3110        void File::DeleteGroup(Group* pGroup) {
3111            if (!pGroups) LoadGroups();
3112            std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup);
3113            if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group");
3114            if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!");
3115            // move all members of this group to another group
3116            pGroup->MoveAll();
3117            pGroups->erase(iter);
3118            delete pGroup;
3119        }
3120    
3121        void File::LoadGroups() {
3122            if (!pGroups) pGroups = new std::list<Group*>;
3123            // try to read defined groups from file
3124            RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI);
3125            if (lst3gri) {
3126                RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL);
3127                if (lst3gnl) {
3128                    RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk();
3129                    while (ck) {
3130                        if (ck->GetChunkID() == CHUNK_ID_3GNM) {
3131                            pGroups->push_back(new Group(this, ck));
3132                        }
3133                        ck = lst3gnl->GetNextSubChunk();
3134                    }
3135                }
3136            }
3137            // if there were no group(s), create at least the mandatory default group
3138            if (!pGroups->size()) {
3139                Group* pGroup = new Group(this, NULL);
3140                pGroup->Name = "Default Group";
3141                pGroups->push_back(pGroup);
3142            }
3143        }
3144    
3145    
3146    
3147  // *************** Exception ***************  // *************** Exception ***************

Legend:
Removed from v.918  
changed lines
  Added in v.1076

  ViewVC Help
Powered by ViewVC