diff dsp/rateconversion/Resampler.cpp @ 363:2fe2ab316c8e

Add one-shot resample function
author Chris Cannam <c.cannam@qmul.ac.uk>
date Sun, 13 Oct 2013 12:47:50 +0100
parents 3953f3ef1b62
children 7fe0da91e9c3
line wrap: on
line diff
--- a/dsp/rateconversion/Resampler.cpp	Fri Oct 11 18:00:51 2013 +0100
+++ b/dsp/rateconversion/Resampler.cpp	Sun Oct 13 12:47:50 2013 +0100
@@ -7,6 +7,9 @@
 #include "qm-dsp/base/SincWindow.h"
 
 #include <iostream>
+#include <vector>
+
+using std::vector;
 
 Resampler::Resampler(int sourceRate, int targetRate) :
     m_sourceRate(sourceRate),
@@ -145,3 +148,23 @@
     return m;
 }
 
+std::vector<double>
+Resampler::resample(int sourceRate, int targetRate, const double *data, int n)
+{
+    Resampler r(sourceRate, targetRate);
+
+    int latency = r.getLatency();
+
+    int m = int(ceil((n * targetRate) / sourceRate));
+    int m1 = m + latency;
+    int n1 = int((m1 * sourceRate) / targetRate);
+
+    vector<double> pad(n1 - n, 0.0);
+    vector<double> out(m1, 0.0);
+
+    int got = r.process(data, out.data(), n);
+    got += r.process(pad.data(), out.data() + got, pad.size());
+
+    return vector<double>(out.begin() + latency, out.begin() + got);
+}
+