/[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 437 by persson, Wed Mar 9 22:02:40 2005 UTC revision 515 by schoenebeck, Sat May 7 20:19:10 2005 UTC
# Line 25  Line 25 
25    
26  #include <iostream>  #include <iostream>
27    
28  namespace gig { namespace {  namespace gig {
29    
30    // *************** progress_t ***************
31    // *
32    
33        progress_t::progress_t() {
34            callback    = NULL;
35            __range_min = 0.0f;
36            __range_max = 1.0f;
37        }
38    
39        // private helper function to convert progress of a subprocess into the global progress
40        static void __notify_progress(progress_t* pProgress, float subprogress) {
41            if (pProgress && pProgress->callback) {
42                const float totalrange    = pProgress->__range_max - pProgress->__range_min;
43                const float totalprogress = pProgress->__range_min + subprogress * totalrange;
44                pProgress->callback(totalprogress); // now actually notify about the progress
45            }
46        }
47    
48        // private helper function to divide a progress into subprogresses
49        static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress, float totalTasks, float currentTask) {
50            if (pParentProgress && pParentProgress->callback) {
51                const float totalrange    = pParentProgress->__range_max - pParentProgress->__range_min;
52                pSubProgress->callback    = pParentProgress->callback;
53                pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
54                pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
55            }
56        }
57    
58    
59  // *************** Internal functions for sample decopmression ***************  // *************** Internal functions for sample decopmression ***************
60  // *  // *
61    
62    namespace {
63    
64      inline int get12lo(const unsigned char* pSrc)      inline int get12lo(const unsigned char* pSrc)
65      {      {
66          const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8;          const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8;
# Line 1551  namespace gig { namespace { Line 1582  namespace gig { namespace {
1582          else         return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex));          else         return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex));
1583      }      }
1584    
1585      Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) {      Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) {
1586          if ((int32_t)WavePoolTableIndex == -1) return NULL;          if ((int32_t)WavePoolTableIndex == -1) return NULL;
1587          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
1588          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];          unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
1589          Sample* sample = file->GetFirstSample();          Sample* sample = file->GetFirstSample(pProgress);
1590          while (sample) {          while (sample) {
1591              if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample);              if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample);
1592              sample = file->GetNextSample();              sample = file->GetNextSample();
# Line 1568  namespace gig { namespace { Line 1599  namespace gig { namespace {
1599  // *************** Instrument ***************  // *************** Instrument ***************
1600  // *  // *
1601    
1602      Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) {      Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) {
1603          // Initialization          // Initialization
1604          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;          for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL;
1605          RegionIndex = -1;          RegionIndex = -1;
# Line 1599  namespace gig { namespace { Line 1630  namespace gig { namespace {
1630          unsigned int iRegion = 0;          unsigned int iRegion = 0;
1631          while (rgn) {          while (rgn) {
1632              if (rgn->GetListType() == LIST_TYPE_RGN) {              if (rgn->GetListType() == LIST_TYPE_RGN) {
1633                    __notify_progress(pProgress, (float) iRegion / (float) Regions);
1634                  pRegions[iRegion] = new Region(this, rgn);                  pRegions[iRegion] = new Region(this, rgn);
1635                  iRegion++;                  iRegion++;
1636              }              }
# Line 1611  namespace gig { namespace { Line 1643  namespace gig { namespace {
1643                  RegionKeyTable[iKey] = pRegions[iReg];                  RegionKeyTable[iKey] = pRegions[iReg];
1644              }              }
1645          }          }
1646    
1647            __notify_progress(pProgress, 1.0f); // notify done
1648      }      }
1649    
1650      Instrument::~Instrument() {      Instrument::~Instrument() {
# Line 1699  namespace gig { namespace { Line 1733  namespace gig { namespace {
1733          }          }
1734      }      }
1735    
1736      Sample* File::GetFirstSample() {      Sample* File::GetFirstSample(progress_t* pProgress) {
1737          if (!pSamples) LoadSamples();          if (!pSamples) LoadSamples(pProgress);
1738          if (!pSamples) return NULL;          if (!pSamples) return NULL;
1739          SamplesIterator = pSamples->begin();          SamplesIterator = pSamples->begin();
1740          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );
# Line 1712  namespace gig { namespace { Line 1746  namespace gig { namespace {
1746          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );          return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL );
1747      }      }
1748    
1749      void File::LoadSamples() {      void File::LoadSamples(progress_t* pProgress) {
1750          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);          RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1751          if (wvpl) {          if (wvpl) {
1752                // just for progress calculation
1753                int iSampleIndex  = 0;
1754                int iTotalSamples = wvpl->CountSubLists(LIST_TYPE_WAVE);
1755    
1756              unsigned long wvplFileOffset = wvpl->GetFilePos();              unsigned long wvplFileOffset = wvpl->GetFilePos();
1757              RIFF::List* wave = wvpl->GetFirstSubList();              RIFF::List* wave = wvpl->GetFirstSubList();
1758              while (wave) {              while (wave) {
1759                  if (wave->GetListType() == LIST_TYPE_WAVE) {                  if (wave->GetListType() == LIST_TYPE_WAVE) {
1760                        // notify current progress
1761                        const float subprogress = (float) iSampleIndex / (float) iTotalSamples;
1762                        __notify_progress(pProgress, subprogress);
1763    
1764                      if (!pSamples) pSamples = new SampleList;                      if (!pSamples) pSamples = new SampleList;
1765                      unsigned long waveFileOffset = wave->GetFilePos();                      unsigned long waveFileOffset = wave->GetFilePos();
1766                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));                      pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1767    
1768                        iSampleIndex++;
1769                  }                  }
1770                  wave = wvpl->GetNextSubList();                  wave = wvpl->GetNextSubList();
1771              }              }
1772                __notify_progress(pProgress, 1.0); // notify done
1773          }          }
1774          else throw gig::Exception("Mandatory <wvpl> chunk not found.");          else throw gig::Exception("Mandatory <wvpl> chunk not found.");
1775      }      }
# Line 1745  namespace gig { namespace { Line 1790  namespace gig { namespace {
1790      /**      /**
1791       * Returns the instrument with the given index.       * Returns the instrument with the given index.
1792       *       *
1793         * @param index     - number of the sought instrument (0..n)
1794         * @param pProgress - optional: callback function for progress notification
1795       * @returns  sought instrument or NULL if there's no such instrument       * @returns  sought instrument or NULL if there's no such instrument
1796       */       */
1797      Instrument* File::GetInstrument(uint index) {      Instrument* File::GetInstrument(uint index, progress_t* pProgress) {
1798          if (!pInstruments) LoadInstruments();          if (!pInstruments) {
1799                // TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM)
1800    
1801                // sample loading subtask
1802                progress_t subprogress;
1803                __divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask
1804                __notify_progress(&subprogress, 0.0f);
1805                GetFirstSample(&subprogress); // now force all samples to be loaded
1806                __notify_progress(&subprogress, 1.0f);
1807    
1808                // instrument loading subtask
1809                if (pProgress && pProgress->callback) {
1810                    subprogress.__range_min = subprogress.__range_max;
1811                    subprogress.__range_max = pProgress->__range_max; // schedule remaining percentage for this subtask
1812                }
1813                __notify_progress(&subprogress, 0.0f);
1814                LoadInstruments(&subprogress);
1815                __notify_progress(&subprogress, 1.0f);
1816            }
1817          if (!pInstruments) return NULL;          if (!pInstruments) return NULL;
1818          InstrumentsIterator = pInstruments->begin();          InstrumentsIterator = pInstruments->begin();
1819          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {          for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) {
# Line 1758  namespace gig { namespace { Line 1823  namespace gig { namespace {
1823          return NULL;          return NULL;
1824      }      }
1825    
1826      void File::LoadInstruments() {      void File::LoadInstruments(progress_t* pProgress) {
1827          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);          RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1828          if (lstInstruments) {          if (lstInstruments) {
1829                int iInstrumentIndex = 0;
1830              RIFF::List* lstInstr = lstInstruments->GetFirstSubList();              RIFF::List* lstInstr = lstInstruments->GetFirstSubList();
1831              while (lstInstr) {              while (lstInstr) {
1832                  if (lstInstr->GetListType() == LIST_TYPE_INS) {                  if (lstInstr->GetListType() == LIST_TYPE_INS) {
1833                        // notify current progress
1834                        const float localProgress = (float) iInstrumentIndex / (float) Instruments;
1835                        __notify_progress(pProgress, localProgress);
1836    
1837                        // divide local progress into subprogress for loading current Instrument
1838                        progress_t subprogress;
1839                        __divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex);
1840    
1841                      if (!pInstruments) pInstruments = new InstrumentList;                      if (!pInstruments) pInstruments = new InstrumentList;
1842                      pInstruments->push_back(new Instrument(this, lstInstr));                      pInstruments->push_back(new Instrument(this, lstInstr, &subprogress));
1843    
1844                        iInstrumentIndex++;
1845                  }                  }
1846                  lstInstr = lstInstruments->GetNextSubList();                  lstInstr = lstInstruments->GetNextSubList();
1847              }              }
1848                __notify_progress(pProgress, 1.0); // notify done
1849          }          }
1850          else throw gig::Exception("Mandatory <lins> list chunk not found.");          else throw gig::Exception("Mandatory <lins> list chunk not found.");
1851      }      }

Legend:
Removed from v.437  
changed lines
  Added in v.515

  ViewVC Help
Powered by ViewVC