/[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 834 by persson, Mon Feb 6 17:58:21 2006 UTC
# Line 1183  namespace { Line 1183  namespace {
1183      DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {      DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {
1184          Instances++;          Instances++;
1185    
1186            pSample = NULL;
1187    
1188          memcpy(&Crossfade, &SamplerOptions, 4);          memcpy(&Crossfade, &SamplerOptions, 4);
1189          if (!pVelocityTables) pVelocityTables = new VelocityTableMap;          if (!pVelocityTables) pVelocityTables = new VelocityTableMap;
1190    
# Line 2114  namespace { Line 2116  namespace {
2116                  }                  }
2117                  _3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition                  _3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition
2118              }              }
2119                for (int i = dimensionBits ; i < 8 ; i++) pDimensionDefinitions[i].bits = 0;
2120    
2121              // check velocity dimension (if there is one) for custom defined zone ranges              // check velocity dimension (if there is one) for custom defined zone ranges
2122              for (uint i = 0; i < Dimensions; i++) {              for (uint i = 0; i < Dimensions; i++) {
# Line 2145  namespace { Line 2148  namespace {
2148                  pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);                  pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex);
2149              }              }
2150          }          }
2151    
2152            // make sure there is at least one dimension region
2153            if (!DimensionRegions) {
2154                RIFF::List* _3prg = rgnList->GetSubList(LIST_TYPE_3PRG);
2155                if (!_3prg) _3prg = rgnList->AddSubList(LIST_TYPE_3PRG);
2156                RIFF::List* _3ewl = _3prg->AddSubList(LIST_TYPE_3EWL);
2157                pDimensionRegions[0] = new DimensionRegion(_3ewl);
2158                DimensionRegions = 1;
2159            }
2160      }      }
2161    
2162      /**      /**
# Line 2161  namespace { Line 2173  namespace {
2173          DLS::Region::UpdateChunks();          DLS::Region::UpdateChunks();
2174    
2175          // update dimension region's chunks          // update dimension region's chunks
2176          for (int i = 0; i < Dimensions; i++)          for (int i = 0; i < DimensionRegions; i++) {
2177              pDimensionRegions[i]->UpdateChunks();              pDimensionRegions[i]->UpdateChunks();
2178            }
2179    
2180          File* pFile = (File*) GetParent()->GetParent();          File* pFile = (File*) GetParent()->GetParent();
2181          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 2203  namespace {
2203          for (uint i = 0; i < iMaxDimensionRegions; i++) {          for (uint i = 0; i < iMaxDimensionRegions; i++) {
2204              int iWaveIndex = -1;              int iWaveIndex = -1;
2205              if (i < DimensionRegions) {              if (i < DimensionRegions) {
2206                  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");
2207                  std::list<Sample*>::iterator iter = pFile->pSamples->begin();                  File::SampleList::iterator iter = pFile->pSamples->begin();
2208                  std::list<Sample*>::iterator end  = pFile->pSamples->end();                  File::SampleList::iterator end  = pFile->pSamples->end();
2209                  for (int index = 0; iter != end; ++iter, ++index) {                  for (int index = 0; iter != end; ++iter, ++index) {
2210                      if (*iter == pDimensionRegions[i]->pSample) iWaveIndex = index;                      if (*iter == pDimensionRegions[i]->pSample) {
2211                      break;                          iWaveIndex = index;
2212                            break;
2213                        }
2214                  }                  }
2215                  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");
2216              }              }
# Line 2485  namespace { Line 2500  namespace {
2500      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) {
2501          // Initialization          // Initialization
2502          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;
         RegionIndex = -1;  
2503    
2504          // Loading          // Loading
2505          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART);
# Line 2503  namespace { Line 2517  namespace {
2517              }              }
2518          }          }
2519    
2520          pRegions = new Region*[Regions];          if (!pRegions) pRegions = new RegionList;
2521          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);          RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN);
2522          if (lrgn) {          if (lrgn) {
             for (uint i = 0; i < Regions; i++) pRegions[i] = NULL;  
2523              RIFF::List* rgn = lrgn->GetFirstSubList();              RIFF::List* rgn = lrgn->GetFirstSubList();
             unsigned int iRegion = 0;  
2524              while (rgn) {              while (rgn) {
2525                  if (rgn->GetListType() == LIST_TYPE_RGN) {                  if (rgn->GetListType() == LIST_TYPE_RGN) {
2526                      __notify_progress(pProgress, (float) iRegion / (float) Regions);                      __notify_progress(pProgress, (float) pRegions->size() / (float) Regions);
2527                      pRegions[iRegion] = new Region(this, rgn);                      pRegions->push_back(new Region(this, rgn));
                     iRegion++;  
2528                  }                  }
2529                  rgn = lrgn->GetNextSubList();                  rgn = lrgn->GetNextSubList();
2530              }              }
# Line 2525  namespace { Line 2536  namespace {
2536      }      }
2537    
2538      void Instrument::UpdateRegionKeyTable() {      void Instrument::UpdateRegionKeyTable() {
2539          for (uint iReg = 0; iReg < Regions; iReg++) {          RegionList::iterator iter = pRegions->begin();
2540              for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) {          RegionList::iterator end  = pRegions->end();
2541                  RegionKeyTable[iKey] = pRegions[iReg];          for (; iter != end; ++iter) {
2542                gig::Region* pRegion = static_cast<gig::Region*>(*iter);
2543                for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) {
2544                    RegionKeyTable[iKey] = pRegion;
2545              }              }
2546          }          }
2547      }      }
2548    
2549      Instrument::~Instrument() {      Instrument::~Instrument() {
         for (uint i = 0; i < Regions; i++) {  
             if (pRegions) {  
                 if (pRegions[i]) delete (pRegions[i]);  
             }  
         }  
         if (pRegions) delete[] pRegions;  
2550      }      }
2551    
2552      /**      /**
# Line 2555  namespace { Line 2563  namespace {
2563          DLS::Instrument::UpdateChunks();          DLS::Instrument::UpdateChunks();
2564    
2565          // update Regions' chunks          // update Regions' chunks
2566          for (int i = 0; i < Regions; i++)          {
2567              pRegions[i]->UpdateChunks();              RegionList::iterator iter = pRegions->begin();
2568                RegionList::iterator end  = pRegions->end();
2569                for (; iter != end; ++iter)
2570                    (*iter)->UpdateChunks();
2571            }
2572    
2573          // make sure 'lart' RIFF list chunk exists          // make sure 'lart' RIFF list chunk exists
2574          RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART);          RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART);
# Line 2584  namespace { Line 2596  namespace {
2596       *             there is no Region defined for the given \a Key       *             there is no Region defined for the given \a Key
2597       */       */
2598      Region* Instrument::GetRegion(unsigned int Key) {      Region* Instrument::GetRegion(unsigned int Key) {
2599          if (!pRegions || Key > 127) return NULL;          if (!pRegions || !pRegions->size() || Key > 127) return NULL;
2600          return RegionKeyTable[Key];          return RegionKeyTable[Key];
2601    
2602          /*for (int i = 0; i < Regions; i++) {          /*for (int i = 0; i < Regions; i++) {
2603              if (Key <= pRegions[i]->KeyRange.high &&              if (Key <= pRegions[i]->KeyRange.high &&
2604                  Key >= pRegions[i]->KeyRange.low) return pRegions[i];                  Key >= pRegions[i]->KeyRange.low) return pRegions[i];
# Line 2601  namespace { Line 2614  namespace {
2614       * @see      GetNextRegion()       * @see      GetNextRegion()
2615       */       */
2616      Region* Instrument::GetFirstRegion() {      Region* Instrument::GetFirstRegion() {
2617          if (!Regions) return NULL;          if (!pRegions) return NULL;
2618          RegionIndex = 1;          RegionsIterator = pRegions->begin();
2619          return pRegions[0];          return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL );
2620      }      }
2621    
2622      /**      /**
# Line 2615  namespace { Line 2628  namespace {
2628       * @see      GetFirstRegion()       * @see      GetFirstRegion()
2629       */       */
2630      Region* Instrument::GetNextRegion() {      Region* Instrument::GetNextRegion() {
2631          if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL;          if (!pRegions) return NULL;
2632          return pRegions[RegionIndex++];          RegionsIterator++;
2633            return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL );
2634      }      }
2635    
2636      Region* Instrument::AddRegion() {      Region* Instrument::AddRegion() {
# Line 2625  namespace { Line 2639  namespace {
2639          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);          if (!lrgn)  lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
2640          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
2641          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
2642          // resize 'pRegions' array (increase by one)          pRegions->push_back(pNewRegion);
2643          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++;  
2644          // update Region key table for fast lookup          // update Region key table for fast lookup
2645          UpdateRegionKeyTable();          UpdateRegionKeyTable();
2646          // done          // done
# Line 2642  namespace { Line 2649  namespace {
2649    
2650      void Instrument::DeleteRegion(Region* pRegion) {      void Instrument::DeleteRegion(Region* pRegion) {
2651          if (!pRegions) return;          if (!pRegions) return;
2652          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--;  
2653          // update Region key table for fast lookup          // update Region key table for fast lookup
2654          UpdateRegionKeyTable();          UpdateRegionKeyTable();
2655      }      }
# Line 2667  namespace { Line 2660  namespace {
2660  // *  // *
2661    
2662      File::File() : DLS::File() {      File::File() : DLS::File() {
         pSamples     = NULL;  
         pInstruments = NULL;  
2663      }      }
2664    
2665      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;  
2666      }      }
2667    
2668      Sample* File::GetFirstSample(progress_t* pProgress) {      Sample* File::GetFirstSample(progress_t* pProgress) {
# Line 2728  namespace { Line 2690  namespace {
2690         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
2691         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);         RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
2692         // 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;  
2693         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);         RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
2694         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*/);
2695         pSamples->push_back(pSample);         pSamples->push_back(pSample);
# Line 2744  namespace { Line 2705  namespace {
2705       * @throws gig::Exception if given sample could not be found       * @throws gig::Exception if given sample could not be found
2706       */       */
2707      void File::DeleteSample(Sample* pSample) {      void File::DeleteSample(Sample* pSample) {
2708          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");
2709          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);          SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample);
2710          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");
2711          pSamples->erase(iter);          pSamples->erase(iter);
2712          delete pSample;          delete pSample;
2713      }      }
2714    
2715        void File::LoadSamples() {
2716            LoadSamples(NULL);
2717        }
2718    
2719      void File::LoadSamples(progress_t* pProgress) {      void File::LoadSamples(progress_t* pProgress) {
2720            if (!pSamples) pSamples = new SampleList;
2721    
2722          RIFF::File* file = pRIFF;          RIFF::File* file = pRIFF;
2723    
2724          // just for progress calculation          // just for progress calculation
# Line 2779  namespace { Line 2746  namespace {
2746                          const float subprogress = (float) iSampleIndex / (float) iTotalSamples;                          const float subprogress = (float) iSampleIndex / (float) iTotalSamples;
2747                          __notify_progress(pProgress, subprogress);                          __notify_progress(pProgress, subprogress);
2748    
                         if (!pSamples) pSamples = new SampleList;  
2749                          unsigned long waveFileOffset = wave->GetFilePos();                          unsigned long waveFileOffset = wave->GetFilePos();
2750                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));
2751    
# Line 2796  namespace { Line 2762  namespace {
2762                  name.replace(nameLen, 5, suffix);                  name.replace(nameLen, 5, suffix);
2763                  file = new RIFF::File(name);                  file = new RIFF::File(name);
2764                  ExtensionFiles.push_back(file);                  ExtensionFiles.push_back(file);
2765              }              } else break;
             else throw gig::Exception("Mandatory <wvpl> chunk not found.");  
2766          }          }
2767    
2768          __notify_progress(pProgress, 1.0); // notify done          __notify_progress(pProgress, 1.0); // notify done
# Line 2807  namespace { Line 2772  namespace {
2772          if (!pInstruments) LoadInstruments();          if (!pInstruments) LoadInstruments();
2773          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2774          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
2775          return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;          return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL );
2776      }      }
2777    
2778      Instrument* File::GetNextInstrument() {      Instrument* File::GetNextInstrument() {
2779          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2780          InstrumentsIterator++;          InstrumentsIterator++;
2781          return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;          return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL );
2782      }      }
2783    
2784      /**      /**
# Line 2846  namespace { Line 2811  namespace {
2811          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
2812          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
2813          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {
2814              if (i == index) return *InstrumentsIterator;              if (i == index) return static_cast<gig::Instrument*>( *InstrumentsIterator );
2815              InstrumentsIterator++;              InstrumentsIterator++;
2816          }          }
2817          return NULL;          return NULL;
# Line 2862  namespace { Line 2827  namespace {
2827      Instrument* File::AddInstrument() {      Instrument* File::AddInstrument() {
2828         if (!pInstruments) LoadInstruments();         if (!pInstruments) LoadInstruments();
2829         __ensureMandatoryChunksExist();         __ensureMandatoryChunksExist();
        if (!pInstruments) pInstruments = new InstrumentList;  
2830         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);         RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2831         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);         RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
2832         Instrument* pInstrument = new Instrument(this, lstInstr);         Instrument* pInstrument = new Instrument(this, lstInstr);
# Line 2880  namespace { Line 2844  namespace {
2844       */       */
2845      void File::DeleteInstrument(Instrument* pInstrument) {      void File::DeleteInstrument(Instrument* pInstrument) {
2846          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");
2847          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);          InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), (DLS::Instrument*) pInstrument);
2848          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");
2849          pInstruments->erase(iter);          pInstruments->erase(iter);
2850          delete pInstrument;          delete pInstrument;
2851      }      }
2852    
2853        void File::LoadInstruments() {
2854            LoadInstruments(NULL);
2855        }
2856    
2857      void File::LoadInstruments(progress_t* pProgress) {      void File::LoadInstruments(progress_t* pProgress) {
2858            if (!pInstruments) pInstruments = new InstrumentList;
2859          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
2860          if (lstInstruments) {          if (lstInstruments) {
2861              int iInstrumentIndex = 0;              int iInstrumentIndex = 0;
# Line 2901  namespace { Line 2870  namespace {
2870                      progress_t subprogress;                      progress_t subprogress;
2871                      __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);                      __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);
2872    
                     if (!pInstruments) pInstruments = new InstrumentList;  
2873                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));
2874    
2875                      iInstrumentIndex++;                      iInstrumentIndex++;
# Line 2910  namespace { Line 2878  namespace {
2878              }              }
2879              __notify_progress(pProgress, 1.0); // notify done              __notify_progress(pProgress, 1.0); // notify done
2880          }          }
         else throw gig::Exception("Mandatory <lins> list chunk not found.");  
2881      }      }
2882    
2883    

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

  ViewVC Help
Powered by ViewVC