diff data/fileio/AudioFileSizeEstimator.cpp @ 1527:710e6250a401 zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:14 +0100
parents aadfb395e933
children 70e172e6cc59 f8e3dcbafb4d
line wrap: on
line diff
--- a/data/fileio/AudioFileSizeEstimator.cpp	Mon Dec 12 15:18:52 2016 +0000
+++ b/data/fileio/AudioFileSizeEstimator.cpp	Mon Sep 17 13:51:14 2018 +0100
@@ -18,90 +18,95 @@
 
 #include <QFile>
 
-//#define DEBUG_AUDIO_FILE_SIZE_ESTIMATOR 1
+#include "base/Debug.h"
 
 sv_frame_t
 AudioFileSizeEstimator::estimate(FileSource source,
-				 sv_samplerate_t targetRate)
+                                 sv_samplerate_t targetRate)
 {
     sv_frame_t estimate = 0;
     
+    SVDEBUG << "AudioFileSizeEstimator: Sample count estimate requested for file \""
+            << source.getLocalFilename() << "\"" << endl;
+
     // Most of our file readers don't know the sample count until
     // after they've finished decoding. This is an exception:
 
     WavFileReader *reader = new WavFileReader(source);
     if (reader->isOK() &&
-	reader->getChannelCount() > 0 &&
-	reader->getFrameCount() > 0) {
-	sv_frame_t samples =
-	    reader->getFrameCount() * reader->getChannelCount();
-	sv_samplerate_t rate = reader->getSampleRate();
-	if (targetRate != 0.0 && targetRate != rate) {
-	    samples = sv_frame_t(double(samples) * targetRate / rate);
-	}
-	delete reader;
-	estimate = samples;
+        reader->getChannelCount() > 0 &&
+        reader->getFrameCount() > 0) {
+        sv_frame_t samples =
+            reader->getFrameCount() * reader->getChannelCount();
+        sv_samplerate_t rate = reader->getSampleRate();
+        if (targetRate != 0.0 && targetRate != rate) {
+            samples = sv_frame_t(double(samples) * targetRate / rate);
+        }
+        SVDEBUG << "AudioFileSizeEstimator: WAV file reader accepts this file, reports "
+                << samples << " samples" << endl;
+        estimate = samples;
+    } else {
+        SVDEBUG << "AudioFileSizeEstimator: WAV file reader doesn't like this file, "
+                << "estimating from file size and extension instead" << endl;
     }
 
+    delete reader;
+    reader = 0;
+
     if (estimate == 0) {
 
-	// The remainder just makes an estimate based on the file size
-	// and extension. We don't even know its sample rate at this
-	// point, so the following is a wild guess.
-	
-	double rateRatio = 1.0;
-	if (targetRate != 0.0) {
-	    rateRatio = targetRate / 44100.0;
-	}
+        // The remainder just makes an estimate based on the file size
+        // and extension. We don't even know its sample rate at this
+        // point, so the following is a wild guess.
+        
+        double rateRatio = 1.0;
+        if (targetRate != 0.0) {
+            rateRatio = targetRate / 44100.0;
+        }
     
-	QString extension = source.getExtension();
+        QString extension = source.getExtension();
 
-	source.waitForData();
-	if (!source.isOK()) return 0;
+        source.waitForData();
+        if (!source.isOK()) return 0;
 
-	sv_frame_t sz = 0;
-	{
-	    QFile f(source.getLocalFilename());
-	    if (f.open(QFile::ReadOnly)) {
-#ifdef DEBUG_AUDIO_FILE_SIZE_ESTIMATOR
-		cerr << "opened file, size is "  << f.size() << endl;
-#endif
-		sz = f.size();
-		f.close();
-	    }
-	}
+        sv_frame_t sz = 0;
 
-	if (extension == "ogg" || extension == "oga" ||
-	    extension == "m4a" || extension == "mp3" ||
-	    extension == "wma") {
+        {
+            QFile f(source.getLocalFilename());
+            if (f.open(QFile::ReadOnly)) {
+                SVDEBUG << "AudioFileSizeEstimator: opened file, size is "
+                        << f.size() << endl;
+                sz = f.size();
+                f.close();
+            }
+        }
 
-	    // Usually a lossy file. Compression ratios can vary
-	    // dramatically, but don't usually exceed about 20x compared
-	    // to 16-bit PCM (e.g. a 128kbps mp3 has 11x ratio over WAV at
-	    // 44.1kHz). We can estimate the number of samples to be file
-	    // size x 20, divided by 2 as we're comparing with 16-bit PCM.
+        if (extension == "ogg" || extension == "oga" ||
+            extension == "m4a" || extension == "mp3" ||
+            extension == "wma") {
 
-	    estimate = sv_frame_t(double(sz) * 10 * rateRatio);
-	}
+            // Usually a lossy file. Compression ratios can vary
+            // dramatically, but don't usually exceed about 20x compared
+            // to 16-bit PCM (e.g. a 128kbps mp3 has 11x ratio over WAV at
+            // 44.1kHz). We can estimate the number of samples to be file
+            // size x 20, divided by 2 as we're comparing with 16-bit PCM.
 
-	if (extension == "flac") {
-	
-	    // FLAC usually takes up a bit more than half the space of
-	    // 16-bit PCM. So the number of 16-bit samples is roughly the
-	    // same as the file size in bytes. As above, let's be
-	    // conservative.
+            estimate = sv_frame_t(double(sz) * 10 * rateRatio);
+        }
 
-	    estimate = sv_frame_t(double(sz) * 1.2 * rateRatio);
-	}
+        if (extension == "flac") {
+        
+            // FLAC usually takes up a bit more than half the space of
+            // 16-bit PCM. So the number of 16-bit samples is roughly the
+            // same as the file size in bytes. As above, let's be
+            // conservative.
 
-#ifdef DEBUG_AUDIO_FILE_SIZE_ESTIMATOR
-	cerr << "AudioFileSizeEstimator: for extension " << extension << ", estimate = " << estimate << endl;
-#endif
+            estimate = sv_frame_t(double(sz) * 1.2 * rateRatio);
+        }
+
+        SVDEBUG << "AudioFileSizeEstimator: for extension \""
+                << extension << "\", estimate = " << estimate << " samples" << endl;
     }
-
-#ifdef DEBUG_AUDIO_FILE_SIZE_ESTIMATOR
-    cerr << "estimate = " << estimate << endl;
-#endif
     
     return estimate;
 }