/[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 809 by schoenebeck, Tue Nov 22 11:26:55 2005 UTC revision 858 by persson, Sat May 6 11:29:29 2006 UTC
# Line 51  Line 51 
51    
52  namespace gig {  namespace gig {
53    
 // *************** dimension_def_t ***************  
 // *  
   
     dimension_def_t& dimension_def_t::operator=(const dimension_def_t& arg) {  
         dimension  = arg.dimension;  
         bits       = arg.bits;  
         zones      = arg.zones;  
         split_type = arg.split_type;  
         ranges     = arg.ranges;  
         zone_size  = arg.zone_size;  
         if (ranges) {  
             ranges = new range_t[zones];  
             for (int i = 0; i < zones; i++)  
                 ranges[i] = arg.ranges[i];  
         }  
         return *this;  
     }  
   
   
   
54  // *************** progress_t ***************  // *************** progress_t ***************
55  // *  // *
56    
# Line 1183  namespace { Line 1163  namespace {
1163      DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {      DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {
1164          Instances++;          Instances++;
1165    
1166            pSample = NULL;
1167    
1168          memcpy(&Crossfade, &SamplerOptions, 4);          memcpy(&Crossfade, &SamplerOptions, 4);
1169          if (!pVelocityTables) pVelocityTables = new VelocityTableMap;          if (!pVelocityTables) pVelocityTables = new VelocityTableMap;
1170    
# Line 1447  namespace { Line 1429  namespace {
1429                                                  VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0);                                                  VCFCutoffController <= vcf_cutoff_ctrl_none2 ? VCFVelocityScale : 0);
1430    
1431          SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));          SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360));
1432            VelocityTable = 0;
1433      }      }
1434    
1435      /**      /**
# Line 1966  namespace { Line 1949  namespace {
1949              delete pVelocityTables;              delete pVelocityTables;
1950              pVelocityTables = NULL;              pVelocityTables = NULL;
1951          }          }
1952            if (VelocityTable) delete[] VelocityTable;
1953      }      }
1954    
1955      /**      /**
# Line 2090  namespace { Line 2074  namespace {
2074                      pDimensionDefinitions[i].bits       = 0;                      pDimensionDefinitions[i].bits       = 0;
2075                      pDimensionDefinitions[i].zones      = 0;                      pDimensionDefinitions[i].zones      = 0;
2076                      pDimensionDefinitions[i].split_type = split_type_bit;                      pDimensionDefinitions[i].split_type = split_type_bit;
                     pDimensionDefinitions[i].ranges     = NULL;  
2077                      pDimensionDefinitions[i].zone_size  = 0;                      pDimensionDefinitions[i].zone_size  = 0;
2078                  }                  }
2079                  else { // active dimension                  else { // active dimension
# Line 2103  namespace { Line 2086  namespace {
2086                                                             dimension == dimension_roundrobin ||                                                             dimension == dimension_roundrobin ||
2087                                                             dimension == dimension_random) ? split_type_bit                                                             dimension == dimension_random) ? split_type_bit
2088                                                                                            : split_type_normal;                                                                                            : split_type_normal;
                     pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point  
2089                      pDimensionDefinitions[i].zone_size  =                      pDimensionDefinitions[i].zone_size  =
2090                          (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones                          (pDimensionDefinitions[i].split_type == split_type_normal) ? 128.0 / pDimensionDefinitions[i].zones
2091                                                                                     : 0;                                                                                     : 0;
# Line 2114  namespace { Line 2096  namespace {
2096                  }                  }
2097                  _3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition                  _3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition
2098              }              }
2099                for (int i = dimensionBits ; i < 8 ; i++) pDimensionDefinitions[i].bits = 0;
2100    
2101              // check velocity dimension (if there is one) for custom defined zone ranges              // if there's a velocity dimension and custom velocity zone splits are used,
2102              for (uint i = 0; i < Dimensions; i++) {              // update the VelocityTables in the dimension regions
2103                  dimension_def_t* pDimDef = pDimensionDefinitions + i;              UpdateVelocityTable();
                 if (pDimDef->dimension == dimension_velocity) {  
                     if (pDimensionRegions[0]->VelocityUpperLimit == 0) {  
                         // no custom defined ranges  
                         pDimDef->split_type = split_type_normal;  
                         pDimDef->ranges     = NULL;  
                     }  
                     else { // custom defined ranges  
                         pDimDef->split_type = split_type_customvelocity;  
                         pDimDef->ranges     = new range_t[pDimDef->zones];  
                         UpdateVelocityTable(pDimDef);  
                     }  
                 }  
             }  
2104    
2105              // jump to start of the wave pool indices (if not already there)              // jump to start of the wave pool indices (if not already there)
             File* file = (File*) GetParent()->GetParent();  
2106              if (file->pVersion && file->pVersion->major == 3)              if (file->pVersion && file->pVersion->major == 3)
2107                  _3lnk->SetPos(68); // version 3 has a different 3lnk structure                  _3lnk->SetPos(68); // version 3 has a different 3lnk structure
2108              else              else
# Line 2145  namespace { Line 2114  namespace {
2114                  pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);                  pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);
2115              }              }
2116          }          }
2117    
2118            // make sure there is at least one dimension region
2119            if (!DimensionRegions) {
2120                RIFF::List* _3prg = rgnList->GetSubList(LIST_TYPE_3PRG);
2121                if (!_3prg) _3prg = rgnList->AddSubList(LIST_TYPE_3PRG);
2122                RIFF::List* _3ewl = _3prg->AddSubList(LIST_TYPE_3EWL);
2123                pDimensionRegions[0] = new DimensionRegion(_3ewl);
2124                DimensionRegions = 1;
2125            }
2126      }      }
2127    
2128      /**      /**
# Line 2161  namespace { Line 2139  namespace {
2139          DLS::Region::UpdateChunks();          DLS::Region::UpdateChunks();
2140    
2141          // update dimension region's chunks          // update dimension region's chunks
2142          for (int i = 0; i < Dimensions; i++)          for (int i = 0; i < DimensionRegions; i++) {
2143              pDimensionRegions[i]->UpdateChunks();              pDimensionRegions[i]->UpdateChunks();
2144            }
2145    
2146          File* pFile = (File*) GetParent()->GetParent();          File* pFile = (File*) GetParent()->GetParent();
2147          const int iMaxDimensions = (pFile->pVersion && pFile->pVersion->major == 3) ? 8 : 5;          const int iMaxDimensions = (pFile->pVersion && pFile->pVersion->major == 3) ? 8 : 5;
# Line 2190  namespace { Line 2169  namespace {
2169          for (uint i = 0; i < iMaxDimensionRegions; i++) {          for (uint i = 0; i < iMaxDimensionRegions; i++) {
2170              int iWaveIndex = -1;              int iWaveIndex = -1;
2171              if (i < DimensionRegions) {              if (i < DimensionRegions) {
2172                  if (!pFile->pSamples) throw gig::Exception("Could not update gig::Region, there are no samples");                  if (!pFile->pSamples || !pFile->pSamples->size()) throw gig::Exception("Could not update gig::Region, there are no samples");
2173                  std::list<Sample*>::iterator iter = pFile->pSamples->begin();                  File::SampleList::iterator iter = pFile->pSamples->begin();
2174                  std::list<Sample*>::iterator end  = pFile->pSamples->end();                  File::SampleList::iterator end  = pFile->pSamples->end();
2175                  for (int index = 0; iter != end; ++iter, ++index) {                  for (int index = 0; iter != end; ++iter, ++index) {
2176                      if (*iter == pDimensionRegions[i]->pSample) iWaveIndex = index;                      if (*iter == pDimensionRegions[i]->pSample) {
2177                      break;                          iWaveIndex = index;
2178                            break;
2179                        }
2180                  }                  }
2181                  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");
2182              }              }
# Line 2219  namespace { Line 2200  namespace {
2200          }          }
2201      }      }
2202    
2203      void Region::UpdateVelocityTable(dimension_def_t* pDimDef) {      void Region::UpdateVelocityTable() {
2204          // get dimension's index          // get velocity dimension's index
2205          int iDimensionNr = -1;          int veldim = -1;
2206          for (int i = 0; i < Dimensions; i++) {          for (int i = 0 ; i < Dimensions ; i++) {
2207              if (&pDimensionDefinitions[i] == pDimDef) {              if (pDimensionDefinitions[i].dimension == gig::dimension_velocity) {
2208                  iDimensionNr = i;                  veldim = i;
2209                  break;                  break;
2210              }              }
2211          }          }
2212          if (iDimensionNr < 0) throw gig::Exception("Invalid dimension_def_t pointer");          if (veldim == -1) return;
2213    
2214            int step = 1;
2215            for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits;
2216            int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step;
2217            int end = step * pDimensionDefinitions[veldim].zones;
2218    
2219            // loop through all dimension regions for all dimensions except the velocity dimension
2220            int dim[8] = { 0 };
2221            for (int i = 0 ; i < DimensionRegions ; i++) {
2222    
2223                if (pDimensionRegions[i]->VelocityUpperLimit) {
2224                    // create the velocity table
2225                    uint8_t* table = pDimensionRegions[i]->VelocityTable;
2226                    if (!table) {
2227                        table = new uint8_t[128];
2228                        pDimensionRegions[i]->VelocityTable = table;
2229                    }
2230                    int tableidx = 0;
2231                    int velocityZone = 0;
2232                    for (int k = i ; k < end ; k += step) {
2233                        DimensionRegion *d = pDimensionRegions[k];
2234                        for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone;
2235                        velocityZone++;
2236                    }
2237                } else {
2238                    if (pDimensionRegions[i]->VelocityTable) {
2239                        delete[] pDimensionRegions[i]->VelocityTable;
2240                        pDimensionRegions[i]->VelocityTable = 0;
2241                    }
2242                }
2243    
2244          uint8_t bits[8] = { 0 };              int j;
2245          int previousUpperLimit = -1;              int shift = 0;
2246          for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) {              for (j = 0 ; j < Dimensions ; j++) {
2247              bits[iDimensionNr] = velocityZone;                  if (j == veldim) i += skipveldim; // skip velocity dimension
2248              DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits);                  else {
2249                        dim[j]++;
2250              pDimDef->ranges[velocityZone].low  = previousUpperLimit + 1;                      if (dim[j] < pDimensionDefinitions[j].zones) break;
2251              pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit;                      else {
2252              previousUpperLimit = pDimDef->ranges[velocityZone].high;                          // skip unused dimension regions
2253              // fill velocity table                          dim[j] = 0;
2254              for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) {                          i += ((1 << pDimensionDefinitions[j].bits) -
2255                  VelocityTable[i] = velocityZone;                                pDimensionDefinitions[j].zones) << shift;
2256                        }
2257                    }
2258                    shift += pDimensionDefinitions[j].bits;
2259              }              }
2260                if (j == Dimensions) break;
2261          }          }
2262      }      }
2263    
# Line 2297  namespace { Line 2312  namespace {
2312          // if this is a layer dimension, update 'Layers' attribute          // if this is a layer dimension, update 'Layers' attribute
2313          if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones;          if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones;
2314    
2315          // if this is velocity dimension and got custom defined ranges, update velocity table          UpdateVelocityTable();
         if (pDimDef->dimension  == dimension_velocity &&  
             pDimDef->split_type == split_type_customvelocity) {  
             UpdateVelocityTable(pDimDef);  
         }  
2316      }      }
2317    
2318      /** @brief Delete an existing dimension.      /** @brief Delete an existing dimension.
# Line 2371  namespace { Line 2382  namespace {
2382          pDimensionDefinitions[Dimensions - 1].dimension = dimension_none;          pDimensionDefinitions[Dimensions - 1].dimension = dimension_none;
2383          pDimensionDefinitions[Dimensions - 1].bits      = 0;          pDimensionDefinitions[Dimensions - 1].bits      = 0;
2384          pDimensionDefinitions[Dimensions - 1].zones     = 0;          pDimensionDefinitions[Dimensions - 1].zones     = 0;
         if (pDimensionDefinitions[Dimensions - 1].ranges) {  
             delete[] pDimensionDefinitions[Dimensions - 1].ranges;  
             pDimensionDefinitions[Dimensions - 1].ranges = NULL;  
         }  
2385    
2386          Dimensions--;          Dimensions--;
2387    
# Line 2383  namespace { Line 2390  namespace {
2390      }      }
2391    
2392      Region::~Region() {      Region::~Region() {
         for (uint i = 0; i < Dimensions; i++) {  
             if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges;  
         }  
2393          for (int i = 0; i < 256; i++) {          for (int i = 0; i < 256; i++) {
2394              if (pDimensionRegions[i]) delete pDimensionRegions[i];              if (pDimensionRegions[i]) delete pDimensionRegions[i];
2395          }          }
# Line 2410  namespace { Line 2414  namespace {
2414       * @see             Dimensions       * @see             Dimensions
2415       */       */
2416      DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) {      DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) {
2417          uint8_t bits[8] = { 0 };          uint8_t bits;
2418            int veldim = -1;
2419            int velbitpos;
2420            int bitpos = 0;
2421            int dimregidx = 0;
2422          for (uint i = 0; i < Dimensions; i++) {          for (uint i = 0; i < Dimensions; i++) {
2423              bits[i] = DimValues[i];              if (pDimensionDefinitions[i].dimension == dimension_velocity) {
2424              switch (pDimensionDefinitions[i].split_type) {                  // the velocity dimension must be handled after the other dimensions
2425                  case split_type_normal:                  veldim = i;
2426                      bits[i] = uint8_t(bits[i] / pDimensionDefinitions[i].zone_size);                  velbitpos = bitpos;
2427                      break;              } else {
2428                  case split_type_customvelocity:                  switch (pDimensionDefinitions[i].split_type) {
2429                      bits[i] = VelocityTable[bits[i]];                      case split_type_normal:
2430                      break;                          bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size);
2431                  case split_type_bit: // the value is already the sought dimension bit number                          break;
2432                      const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;                      case split_type_bit: // the value is already the sought dimension bit number
2433                      bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed                          const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff;
2434                      break;                          bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed
2435                            break;
2436                    }
2437                    dimregidx |= bits << bitpos;
2438              }              }
2439                bitpos += pDimensionDefinitions[i].bits;
2440          }          }
2441          return GetDimensionRegionByBit(bits);          DimensionRegion* dimreg = pDimensionRegions[dimregidx];
2442            if (veldim != -1) {
2443                // (dimreg is now the dimension region for the lowest velocity)
2444                if (dimreg->VelocityUpperLimit) // custom defined zone ranges
2445                    bits = dimreg->VelocityTable[DimValues[veldim]];
2446                else // normal split type
2447                    bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size);
2448    
2449                dimregidx |= bits << velbitpos;
2450                dimreg = pDimensionRegions[dimregidx];
2451            }
2452            return dimreg;
2453      }      }
2454    
2455      /**      /**
# Line 2485  namespace { Line 2508  namespace {
2508      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) {
2509          // Initialization          // Initialization
2510          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;
         RegionIndex = -1;  
2511    
2512          // Loading          // Loading
2513          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 2503  namespace { Line 2525  namespace {
2525              }              }
2526          }          }
2527    
2528          pRegions = new Region*[Regions];          if (!pRegions) pRegions = new RegionList;
2529          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);
2530          if (lrgn) {          if (lrgn) {
             for (uint i = 0; i < Regions; i++) pRegions[i] = NULL;  
2531              RIFF::List* rgn = lrgn->GetFirstSubList();              RIFF::List* rgn = lrgn->GetFirstSubList();
             unsigned int iRegion = 0;  
2532              while (rgn) {              while (rgn) {
2533                  if (rgn->GetListType() == LIST_TYPE_RGN) {                  if (rgn->GetListType() == LIST_TYPE_RGN) {
2534                      __notify_progress(pProgress, (float) iRegion / (float) Regions);                      __notify_progress(pProgress, (float) pRegions->size() / (float) Regions);
2535                      pRegions[iRegion] = new Region(this, rgn);                      pRegions->push_back(new Region(this, rgn));
                     iRegion++;  
2536                  }                  }
2537                  rgn = lrgn->GetNextSubList();                  rgn = lrgn->GetNextSubList();
2538              }              }
# Line 2525  namespace { Line 2544  namespace {
2544      }      }
2545    
2546      void Instrument::UpdateRegionKeyTable() {      void Instrument::UpdateRegionKeyTable() {
2547          for (uint iReg = 0; iReg < Regions; iReg++) {          RegionList::iterator iter = pRegions->begin();
2548              for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) {          RegionList::iterator end  = pRegions->end();
2549                  RegionKeyTable[iKey] = pRegions[iReg];          for (; iter != end; ++iter) {
2550                gig::Region* pRegion = static_cast<gig::Region*>(*iter);
2551                for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) {
2552                    RegionKeyTable[iKey] = pRegion;
2553              }              }
2554          }          }
2555      }      }
2556    
2557      Instrument::~Instrument() {      Instrument::~Instrument() {
         for (uint i = 0; i < Regions; i++) {  
             if (pRegions) {  
                 if (pRegions[i]) delete (pRegions[i]);  
             }  
         }  
         if (pRegions) delete[] pRegions;  
2558      }      }
2559    
2560      /**      /**
# Line 2555  namespace { Line 2571  namespace {
2571          DLS::Instrument::UpdateChunks();          DLS::Instrument::UpdateChunks();
2572    
2573          // update Regions' chunks          // update Regions' chunks
2574          for (int i = 0; i < Regions; i++)          {
2575              pRegions[i]->UpdateChunks();              RegionList::iterator iter = pRegions->begin();
2576                RegionList::iterator end  = pRegions->end();
2577                for (; iter != end; ++iter)
2578                    (*iter)->UpdateChunks();
2579            }
2580    
2581          // make sure 'lart' RIFF list chunk exists          // make sure 'lart' RIFF list chunk exists
2582          RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART);
# Line 2584  namespace { Line 2604  namespace {
2604       *             there is no Region defined for the given \a Key       *             there is no Region defined for the given \a Key
2605       */       */
2606      Region* Instrument::GetRegion(unsigned int Key) {      Region* Instrument::GetRegion(unsigned int Key) {
2607          if (!pRegions || Key > 127) return NULL;          if (!pRegions || !pRegions->size() || Key > 127) return NULL;
2608          return RegionKeyTable[Key];          return RegionKeyTable[Key];
2609    
2610          /*for (int i = 0; i < Regions; i++) {          /*for (int i = 0; i < Regions; i++) {
2611              if (Key <= pRegions[i]->KeyRange.high &&              if (Key <= pRegions[i]->KeyRange.high &&
2612                  Key >= pRegions[i]->KeyRange.low) return pRegions[i];                  Key >= pRegions[i]->KeyRange.low) return pRegions[i];
# Line 2601  namespace { Line 2622  namespace {
2622       * @see      GetNextRegion()       * @see      GetNextRegion()
2623       */       */
2624      Region* Instrument::GetFirstRegion() {      Region* Instrument::GetFirstRegion() {
2625          if (!Regions) return NULL;          if (!pRegions) return NULL;
2626          RegionIndex = 1;          RegionsIterator = pRegions->begin();
2627          return pRegions[0];          return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL );
2628      }      }
2629    
2630      /**      /**
# Line 2615  namespace { Line 2636  namespace {
2636       * @see      GetFirstRegion()       * @see      GetFirstRegion()
2637       */       */
2638      Region* Instrument::GetNextRegion() {      Region* Instrument::GetNextRegion() {
2639          if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL;          if (!pRegions) return NULL;
2640          return pRegions[RegionIndex++];          RegionsIterator++;
2641            return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL );
2642      }      }
2643    
2644      Region* Instrument::AddRegion() {      Region* Instrument::AddRegion() {
# Line 2625  namespace { Line 2647  namespace {
2647          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
2648          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
2649          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
2650          // resize 'pRegions' array (increase by one)          pRegions->push_back(pNewRegion);
2651          Region** pNewRegions = new Region*[Regions + 1];          Regions = pRegions->size();
         memcpy(pNewRegions, pRegions, Regions * sizeof(Region*));  
         // add new Region object  
         pNewRegions[Regions] = pNewRegion;  
         // replace old 'pRegions' array by the new increased array  
         if (pRegions) delete[] pRegions;  
         pRegions = pNewRegions;  
         Regions++;  
2652          // update Region key table for fast lookup          // update Region key table for fast lookup
2653          UpdateRegionKeyTable();          UpdateRegionKeyTable();
2654          // done          // done
# Line 2642  namespace { Line 2657  namespace {
2657    
2658      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
2659          if (!pRegions) return;          if (!pRegions) return;
2660          int iOffset = 0;          DLS::Instrument::DeleteRegion((DLS::Region*) pRegion);
         // resize 'pRegions' array (decrease by one)  
         Region** pNewRegions = new Region*[Regions - 1];  
         for (int i = 0; i < Regions; i++) {  
             if (pRegions[i] == pRegion) { // found Region to delete  
                 iOffset = 1;  
                 delete pRegion;  
             }  
             if (i < Regions - 1) pNewRegions[i] = pRegions[i + iOffset];  
         }  
         if (!iOffset) throw gig::Exception("There is no such gig::Region to delete");  
         // replace old 'pRegions' array by the new decreased array  
         if (pRegions) delete[] pRegions;  
         pRegions = pNewRegions;  
         Regions--;  
2661          // update Region key table for fast lookup          // update Region key table for fast lookup
2662          UpdateRegionKeyTable();          UpdateRegionKeyTable();
2663      }      }
# Line 2667  namespace { Line 2668  namespace {
2668  // *  // *
2669    
2670      File::File() : DLS::File() {      File::File() : DLS::File() {
         pSamples     = NULL;  
         pInstruments = NULL;  
2671      }      }
2672    
2673      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {      File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) {
         pSamples     = NULL;  
         pInstruments = NULL;  
     }  
   
     File::~File() {  
         // free samples  
         if (pSamples) {  
             SamplesIterator = pSamples->begin();  
             while (SamplesIterator != pSamples->end() ) {  
                 delete (*SamplesIterator);  
                 SamplesIterator++;  
             }  
             pSamples->clear();  
             delete pSamples;  
   
         }  
         // free instruments  
         if (pInstruments) {  
             InstrumentsIterator = pInstruments->begin();  
             while (InstrumentsIterator != pInstruments->end() ) {  
                 delete (*InstrumentsIterator);  
                 InstrumentsIterator++;  
             }  
             pInstruments->clear();  
             delete pInstruments;  
         }  
         // free extension files  
         for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)  
             delete *i;  
2674      }      }
2675    
2676      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 2728  namespace { Line 2698  namespace {
2698         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
2699         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2700         // create new Sample object and its respective 'wave' list chunk         // create new Sample object and its respective 'wave' list chunk
        if (!pSamples) pSamples = new SampleList;  
2701         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
2702         Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);         Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
2703         pSamples->push_back(pSample);         pSamples->push_back(pSample);
# Line 2744  namespace { Line 2713  namespace {
2713       * @throws gig::Exception if given sample could not be found       * @throws gig::Exception if given sample could not be found
2714       */       */
2715      void File::DeleteSample(Sample* pSample) {      void File::DeleteSample(Sample* pSample) {
2716          if (!pSamples) 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");
2717          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);
2718          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");
2719          pSamples->erase(iter);          pSamples->erase(iter);
2720          delete pSample;          delete pSample;
2721      }      }
2722    
2723        void File::LoadSamples() {
2724            LoadSamples(NULL);
2725        }
2726    
2727      void File::LoadSamples(progress_t* pProgress) {      void File::LoadSamples(progress_t* pProgress) {
2728            if (!pSamples) pSamples = new SampleList;
2729    
2730          RIFF::File* file = pRIFF;          RIFF::File* file = pRIFF;
2731    
2732          // just for progress calculation          // just for progress calculation
# Line 2779  namespace { Line 2754  namespace {
2754                          const float subprogress = (float) iSampleIndex / (float) iTotalSamples;                          const float subprogress = (float) iSampleIndex / (float) iTotalSamples;
2755                          __notify_progress(pProgress, subprogress);                          __notify_progress(pProgress, subprogress);
2756    
                         if (!pSamples) pSamples = new SampleList;  
2757                          unsigned long waveFileOffset = wave->GetFilePos();                          unsigned long waveFileOffset = wave->GetFilePos();
2758                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));
2759    
# Line 2796  namespace { Line 2770  namespace {
2770                  name.replace(nameLen, 5, suffix);                  name.replace(nameLen, 5, suffix);
2771                  file = new RIFF::File(name);                  file = new RIFF::File(name);
2772                  ExtensionFiles.push_back(file);                  ExtensionFiles.push_back(file);
2773              }              } else break;
             else throw gig::Exception("Mandatory <wvpl> chunk not found.");  
2774          }          }
2775    
2776          __notify_progress(pProgress, 1.0); // notify done          __notify_progress(pProgress, 1.0); // notify done
# Line 2807  namespace { Line 2780  namespace {
2780          if (!pInstruments) LoadInstruments();          if (!pInstruments) LoadInstruments();
2781          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2782          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
2783          return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;          return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL );
2784      }      }
2785    
2786      Instrument* File::GetNextInstrument() {      Instrument* File::GetNextInstrument() {
2787          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2788          InstrumentsIterator++;          InstrumentsIterator++;
2789          return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;          return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL );
2790      }      }
2791    
2792      /**      /**
# Line 2846  namespace { Line 2819  namespace {
2819          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2820          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
2821          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {
2822              if (i == index) return *InstrumentsIterator;              if (i == index) return static_cast<gig::Instrument*>( *InstrumentsIterator );
2823              InstrumentsIterator++;              InstrumentsIterator++;
2824          }          }
2825          return NULL;          return NULL;
# Line 2862  namespace { Line 2835  namespace {
2835      Instrument* File::AddInstrument() {      Instrument* File::AddInstrument() {
2836         if (!pInstruments) LoadInstruments();         if (!pInstruments) LoadInstruments();
2837         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
        if (!pInstruments) pInstruments = new InstrumentList;  
2838         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2839         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
2840         Instrument* pInstrument = new Instrument(this, lstInstr);         Instrument* pInstrument = new Instrument(this, lstInstr);
# Line 2880  namespace { Line 2852  namespace {
2852       */       */
2853      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
2854          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");
2855          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), (DLS::Instrument*) pInstrument);
2856          if (iter == pInstruments->end()) throw gig::Exception("Could not delete instrument, could not find given instrument");          if (iter == pInstruments->end()) throw gig::Exception("Could not delete instrument, could not find given instrument");
2857          pInstruments->erase(iter);          pInstruments->erase(iter);
2858          delete pInstrument;          delete pInstrument;
2859      }      }
2860    
2861        void File::LoadInstruments() {
2862            LoadInstruments(NULL);
2863        }
2864    
2865      void File::LoadInstruments(progress_t* pProgress) {      void File::LoadInstruments(progress_t* pProgress) {
2866            if (!pInstruments) pInstruments = new InstrumentList;
2867          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2868          if (lstInstruments) {          if (lstInstruments) {
2869              int iInstrumentIndex = 0;              int iInstrumentIndex = 0;
# Line 2901  namespace { Line 2878  namespace {
2878                      progress_t subprogress;                      progress_t subprogress;
2879                      __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);                      __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);
2880    
                     if (!pInstruments) pInstruments = new InstrumentList;  
2881                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));
2882    
2883                      iInstrumentIndex++;                      iInstrumentIndex++;
# Line 2910  namespace { Line 2886  namespace {
2886              }              }
2887              __notify_progress(pProgress, 1.0); // notify done              __notify_progress(pProgress, 1.0); // notify done
2888          }          }
         else throw gig::Exception("Mandatory <lins> list chunk not found.");  
2889      }      }
2890    
2891    

Legend:
Removed from v.809  
changed lines
  Added in v.858

  ViewVC Help
Powered by ViewVC