Update of /cvsroot/audacity/audacity-src/src
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv32012/src

Modified Files:
        AudioIO.cpp AudioIO.h 
Log Message:
Cache results of GetSupported*Rates() and GetBestRate() in AudioIO.


Index: AudioIO.cpp
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/AudioIO.cpp,v
retrieving revision 1.230
retrieving revision 1.231
diff -u -d -r1.230 -r1.231
--- AudioIO.cpp 19 Oct 2009 18:18:16 -0000      1.230
+++ AudioIO.cpp 8 Nov 2009 02:30:45 -0000       1.231
@@ -277,6 +277,10 @@
    mUpdateMeters = false;
    mUpdatingMeters = false;
 
+   mCachedPlaybackIndex = -1;
+   mCachedCaptureIndex = -1;
+   mCachedBestRateIn = 0.0;
+
    PaError err = Pa_Initialize();
 
    if (err != paNoError) {
@@ -451,7 +455,19 @@
    // This should not happen, but it would screw things up if it did.
    if (IsStreamActive())
       return;
-   // this function only does something (at the moment) for portmixer.
+
+   // get the selected record and playback devices
+   int playDeviceNum = getPlayDevIndex();
+   int recDeviceNum = getRecordDevIndex();
+
+   // cache playback/capture rates
+   mCachedPlaybackRates = GetSupportedPlaybackRates(playDeviceNum);
+   mCachedCaptureRates = GetSupportedCaptureRates(recDeviceNum);
+   mCachedSampleRates = GetSupportedSampleRates(playDeviceNum, recDeviceNum);
+   mCachedPlaybackIndex = playDeviceNum;
+   mCachedCaptureIndex = recDeviceNum;
+   mCachedBestRateIn = 0.0;
+
 #if defined(USE_PORTMIXER)
 
    // if we have a PortMixer object, close it down
@@ -468,24 +484,19 @@
       mPortMixer = NULL;
    }
 
-   // get the selected record and playback devices
-   int playDeviceNum = getPlayDevIndex();
-   int recDeviceNum = getRecordDevIndex();
-
-   wxArrayLong supportedSampleRates = GetSupportedSampleRates(playDeviceNum, 
recDeviceNum);
    // that might have given us no rates whatsoever, so we have to guess an
    // answer to do the next bit
-   int numrates = supportedSampleRates.GetCount();
+   int numrates = mCachedSampleRates.GetCount();
    int highestSampleRate;
    if (numrates > 0)
    {
-      highestSampleRate = supportedSampleRates[numrates - 1];
+      highestSampleRate = mCachedSampleRates[numrates - 1];
    }
    else
    {  // we don't actually have any rates that work for Rec and Play. Guess one
       // to use for messing with the mixer, which doesn't actually do either
       highestSampleRate = 44100;
-      // supportedSampleRates is still empty, but it's not used again, so
+      // mCachedSampleRates is still empty, but it's not used again, so
       // can ignore
    }
    mEmulateMixerInputVol = true;
@@ -1431,15 +1442,23 @@
 
 wxArrayLong AudioIO::GetSupportedPlaybackRates(int devIndex, double rate)
 {
+   if (devIndex == -1)
+   {  // weren't given a device index, get the prefs / default one
+      devIndex = getPlayDevIndex();
+   }
+
+   // Check if we can use the cached rates
+   if (mCachedPlaybackIndex != -1 && devIndex == mCachedPlaybackIndex
+         && rate == 0.0)
+   {
+      return mCachedPlaybackRates;
+   }
+
    wxArrayLong supported;
    int irate = (int)rate;
    const PaDeviceInfo* devInfo = NULL;
    int i;
 
-   if (devIndex == -1)
-   {  // weren't given a device index, get the prefs / default one
-      devIndex = getPlayDevIndex();
-   }
    wxLogDebug(wxT("Getting supported playback rates for device %d"), devIndex);
    devInfo = Pa_GetDeviceInfo(devIndex);
    
@@ -1480,15 +1499,23 @@
 
 wxArrayLong AudioIO::GetSupportedCaptureRates(int devIndex, double rate)
 {
+   if (devIndex == -1)
+   {  // not given a device, look up in prefs / default
+      devIndex = getRecordDevIndex();
+   }
+
+   // Check if we can use the cached rates
+   if (mCachedCaptureIndex != -1 && devIndex == mCachedCaptureIndex
+         && rate == 0.0)
+   {
+      return mCachedCaptureRates;
+   }
+
    wxArrayLong supported;
    int irate = (int)rate;
    const PaDeviceInfo* devInfo = NULL;
    int i;
 
-   if (devIndex == -1)
-   {  // not given a device, look up in prefs / default
-      devIndex = getRecordDevIndex();
-   }
    wxLogDebug(wxT("Getting supported capture rates for device %d"), devIndex);
    devInfo = Pa_GetDeviceInfo(devIndex);
 
@@ -1534,6 +1561,23 @@
 
 wxArrayLong AudioIO::GetSupportedSampleRates(int playDevice, int recDevice, 
double rate)
 {
+   // Not given device indices, look up prefs
+   if (playDevice == -1) {
+      playDevice = getPlayDevIndex();
+   }
+   if (recDevice == -1) {
+      recDevice = getRecordDevIndex();
+   }
+
+   // Check if we can use the cached rates
+   if (mCachedPlaybackIndex != -1 && mCachedCaptureIndex != -1 && 
+         playDevice == mCachedPlaybackIndex &&
+         recDevice == mCachedCaptureIndex &&
+         rate == 0.0)
+   {
+      return mCachedSampleRates;
+   }
+
    wxArrayLong playback = GetSupportedPlaybackRates(playDevice, rate);
    wxArrayLong capture = GetSupportedCaptureRates(recDevice, rate);
    int i;
@@ -1581,6 +1625,15 @@
 
 double AudioIO::GetBestRate(bool capturing, bool playing, double sampleRate)
 {
+   // Check if we can use the cached value
+   if (mCachedBestRateIn != 0.0 && mCachedBestRateIn == sampleRate) {
+      return mCachedBestRateOut;
+   }
+
+   // In order to cache the value, all early returns should instead set retval
+   // and jump to finished
+   double retval;
+
    wxArrayLong rates;
    if (capturing) wxLogDebug(wxT("AudioIO::GetBestRate() for capture"));
    if (playing) wxLogDebug(wxT("AudioIO::GetBestRate() for playback"));
@@ -1594,16 +1647,7 @@
    }
    else {   // we assume capturing and playing - the alternative would be a 
             // bit odd
-      wxArrayLong playrates = GetSupportedPlaybackRates(-1, sampleRate);
-      wxArrayLong caprates = GetSupportedCaptureRates(-1, sampleRate);
-      int i;
-      for (i = 0; i < (int)caprates.GetCount(); i++)  // for each capture rate
-         {
-         if (playrates.Index(caprates[i]) != wxNOT_FOUND)
-            rates.Add(caprates[i]);
-         // if the capture rate is also a playback rate, then add to
-         // list of rates available
-         }
+      rates = GetSupportedSampleRates(-1, sampleRate);
    }
    /* rem rates is the array of hardware-supported sample rates (in the current
     * configuration), sampleRate is the Project Rate (desired sample rate) */
@@ -1611,7 +1655,8 @@
    
    if (rates.Index(rate) != wxNOT_FOUND) {
       wxLogDebug(wxT("GetBestRate() Returning %.0ld Hz"), rate);
-      return rate;
+      retval = rate;
+      goto finished;
       /* the easy case - the suggested rate (project rate) is in the list, and
        * we can just accept that and send back to the caller. This should be
        * the case for most users most of the time (all of the time on
@@ -1629,7 +1674,8 @@
    if (rates.IsEmpty()) {
       /* we're stuck - there are no supported rates with this hardware. Error 
*/
       wxLogDebug(wxT("GetBestRate() Error - no supported sample rates"));
-      return 0;
+      retval = 0.0;
+      goto finished;
    }
    int i;
    for (i = 0; i < (int)rates.GetCount(); i++)  // for each supported rate
@@ -1637,12 +1683,19 @@
          if (rates[i] > rate) {
             // supported rate is greater than requested rate
             wxLogDebug(wxT("GetBestRate() Returning next higher rate - %.0ld 
Hz"), rates[i]);
-            return rates[i];
+            retval = rates[i];
+            goto finished;
          }
          }
 
    wxLogDebug(wxT("GetBestRate() Returning highest rate - %.0ld Hz"), 
rates[rates.GetCount() - 1]);
-   return rates[rates.GetCount() - 1]; // the highest available rate
+   retval = rates[rates.GetCount() - 1]; // the highest available rate
+   goto finished;
+
+finished:
+   mCachedBestRateIn = sampleRate;
+   mCachedBestRateOut = retval;
+   return retval;
 }      
 
 

Index: AudioIO.h
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/AudioIO.h,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- AudioIO.h   19 Oct 2009 18:18:16 -0000      1.79
+++ AudioIO.h   8 Nov 2009 02:30:45 -0000       1.80
@@ -198,8 +198,8 @@
     * You may also specify a rate for which to check in addition to the
     * standard rates.
     */
-   static wxArrayLong GetSupportedPlaybackRates(int DevIndex = -1,
-                                                double rate = 0.0);
+   wxArrayLong GetSupportedPlaybackRates(int DevIndex = -1,
+                                         double rate = 0.0);
 
    /** \brief Get a list of sample rates the input (recording) device
     * supports.
@@ -213,8 +213,8 @@
     * You may also specify a rate for which to check in addition to the
     * standard rates.
     */
-   static wxArrayLong GetSupportedCaptureRates(int devIndex = -1,
-                                               double rate = 0.0);
+   wxArrayLong GetSupportedCaptureRates(int devIndex = -1,
+                                        double rate = 0.0);
 
    /** \brief Get a list of sample rates the current input/output device
     * combination supports.
@@ -230,9 +230,9 @@
     * You may also specify a rate for which to check in addition to the
     * standard rates.
     */
-   static wxArrayLong GetSupportedSampleRates(int playDevice = -1,
-                                              int recDevice = -1,
-                                              double rate = 0.0);
+   wxArrayLong GetSupportedSampleRates(int playDevice = -1,
+                                       int recDevice = -1,
+                                       double rate = 0.0);
 
    /** \brief Get a supported sample rate which can be used a an optimal
     * default.
@@ -242,7 +242,7 @@
     * for project rates if one cannot be retrieved from the preferences.
     * So all in all not that useful or important really
     */
-   static int GetOptimalSupportedSampleRate();
+   int GetOptimalSupportedSampleRate();
 
    /** \brief The time the stream has been playing for
     *
@@ -501,6 +501,15 @@
 
    TimeTrack *mTimeTrack;
 
+   // For cacheing supported sample rates
+   int mCachedPlaybackIndex;
+   wxArrayLong mCachedPlaybackRates;
+   int mCachedCaptureIndex;
+   wxArrayLong mCachedCaptureRates;
+   wxArrayLong mCachedSampleRates;
+   double mCachedBestRateIn;
+   double mCachedBestRateOut;
+
    /** brief The function which is called from PortAudio's callback thread
     * context to collect and deliver audio for / from the sound device.
     *


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Audacity-cvs mailing list
Audacity-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/audacity-cvs

Reply via email to