changeset 16:66abf86c864d

Add bandwidth, snr parameters
author Chris Cannam
date Fri, 18 Oct 2013 14:57:48 +0100
parents e35d83f7b4f0
children f8efad075df0
files garage-resampler/Resampler.cpp garage-resampler/Resampler.h garage-resampler/resample.cpp
diffstat 3 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/garage-resampler/Resampler.cpp	Fri Oct 18 13:08:00 2013 +0100
+++ b/garage-resampler/Resampler.cpp	Fri Oct 18 14:57:48 2013 +0100
@@ -21,7 +21,15 @@
     m_sourceRate(sourceRate),
     m_targetRate(targetRate)
 {
-    initialise();
+    initialise(100, 0.02);
+}
+
+Resampler::Resampler(int sourceRate, int targetRate, 
+                     double snr, double bandwidth) :
+    m_sourceRate(sourceRate),
+    m_targetRate(targetRate)
+{
+    initialise(snr, bandwidth);
 }
 
 Resampler::~Resampler()
@@ -37,7 +45,7 @@
 knownFilterMutex;
 
 void
-Resampler::initialise()
+Resampler::initialise(double snr, double bandwidth)
 {
     int higher = std::max(m_sourceRate, m_targetRate);
     int lower = std::min(m_sourceRate, m_targetRate);
@@ -47,7 +55,7 @@
     int peakToPole = higher / m_gcd;
 
     KaiserWindow::Parameters params =
-	KaiserWindow::parametersForBandwidth(100, 0.02, peakToPole);
+	KaiserWindow::parametersForBandwidth(snr, bandwidth, peakToPole);
 
     params.length =
 	(params.length % 2 == 0 ? params.length + 1 : params.length);
@@ -269,6 +277,7 @@
 	// NB gcc can only vectorize this with -ffast-math
 	v += buf[i] * filt[i];
     }
+
     m_bufferOrigin += pd.drop;
     m_phase = pd.nextPhase;
     return v;
--- a/garage-resampler/Resampler.h	Fri Oct 18 13:08:00 2013 +0100
+++ b/garage-resampler/Resampler.h	Fri Oct 18 14:57:48 2013 +0100
@@ -13,6 +13,14 @@
      * targetRate.
      */
     Resampler(int sourceRate, int targetRate);
+
+    /**
+     * Construct a Resampler to resample from sourceRate to
+     * targetRate, using the given filter parameters.
+     */
+    Resampler(int sourceRate, int targetRate,
+              double snr, double bandwidth);
+
     virtual ~Resampler();
 
     /**
@@ -57,7 +65,7 @@
     std::vector<double> m_buffer;
     int m_bufferOrigin;
 
-    void initialise();
+    void initialise(double, double);
     double reconstructOne();
 };
 
--- a/garage-resampler/resample.cpp	Fri Oct 18 13:08:00 2013 +0100
+++ b/garage-resampler/resample.cpp	Fri Oct 18 14:57:48 2013 +0100
@@ -20,6 +20,8 @@
     int targetRate = 0;
     bool version = false;
     bool help = false;
+    double snr = 100;
+    double bandwidth = 0.02;
     int c = 0;
 
     while (1) {
@@ -29,11 +31,13 @@
             { "help",          0, 0, 'h' },
             { "version",       0, 0, 'V' },
             { "to",            1, 0, 't' },
+	    { "snr",           1, 0, 's' },
+	    { "bandwidth",     1, 0, 'b' },
             { 0, 0, 0, 0 }
         };
 
         c = getopt_long(argc, argv,
-                        "t:hV",
+                        "t:hVs:b:",
                         longOpts, &optionIndex);
         if (c == -1) break;
 
@@ -41,6 +45,8 @@
         case 'h': help = true; break;
         case 'V': version = true; break;
         case 't': targetRate = atoi(optarg); break;
+	case 's': snr = atof(optarg); break;
+	case 'b': bandwidth = atof(optarg); break;
         default:  help = true; break;
         }
     }
@@ -52,7 +58,7 @@
 
     if (help || targetRate == 0 || optind + 2 != argc) {
 	cerr << endl;
-	cerr << "usage: " << argv[0] << " --to <rate> <infile.wav> <outfile.wav>" << endl;
+	cerr << "usage: " << argv[0] << " --to <rate> [--snr <s>] [--bandwidth <b>] <infile.wav> <outfile.wav>" << endl;
 	cerr << endl;
 	return 2;
     }
@@ -96,8 +102,13 @@
     int channels = sfinfo.channels;
     vector<Resampler *> resamplers; // one per channel
 
+    cerr << "Resampling from " << sourceRate << " to " << targetRate
+	 << " Hz [with snr " << snr << ", bandwidth " << bandwidth << "]"
+	 << endl;
+
     for (int c = 0; c < channels; ++c) {
-	resamplers.push_back(new Resampler(sourceRate, targetRate));
+	resamplers.push_back
+	    (new Resampler(sourceRate, targetRate, snr, bandwidth));
     }
 
     int outputLatency = resamplers[0]->getLatency();
@@ -199,7 +210,11 @@
     etv.tv_usec -= tv.tv_usec;
         
     double sec = double(etv.tv_sec) + (double(etv.tv_usec) / 1000000.0);
-    cerr << "elapsed time: " << sec << " sec, in frames/sec: " << sfinfo.frames/sec << ", out frames/sec: " << n/sec << endl;
+    cerr << sfinfo.frames << " frames in, " << n << " frames out"
+	 << ", nominal ratio " << double(targetRate)/double(sourceRate)
+	 << ", actual " << double(n)/double(sfinfo.frames)
+	 << endl << "Elapsed time: " << sec << " sec, in frames/sec: "
+	 << int(sfinfo.frames/sec) << ", out frames/sec: " << int(n/sec) << endl;
 
     return 0;
 }