changeset 311:99af9557dfc1

Pull out applyEM function
author Chris Cannam
date Tue, 28 Apr 2015 09:41:40 +0100
parents 37d2d04d94db
children 796d403dc83b
files Makefile.linux src/Silvet.cpp src/Silvet.h
diffstat 3 files changed, 69 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.linux	Tue Apr 28 09:17:12 2015 +0100
+++ b/Makefile.linux	Tue Apr 28 09:41:40 2015 +0100
@@ -1,12 +1,12 @@
 
-CFLAGS := -Wall -O3 -fopenmp -ffast-math -msse -msse2 -mfpmath=sse -ftree-vectorize -fPIC -I../vamp-plugin-sdk/ -DUSE_PTHREADS
+CFLAGS := -Wall -O3 -ffast-math -msse -msse2 -mfpmath=sse -ftree-vectorize -fPIC -I../vamp-plugin-sdk/ -DUSE_PTHREADS
 
 #CFLAGS := -g -fPIC -I../vamp-plugin-sdk
 
-CXXFLAGS := $(CFLAGS)
+CXXFLAGS := $(CFLAGS) -std=c++11
 
 VAMPSDK_DIR := ../vamp-plugin-sdk
-PLUGIN_LDFLAGS := -lgomp -shared -Wl,-Bsymbolic -Wl,-z,defs -Wl,--version-script=vamp-plugin.map -lpthread
+PLUGIN_LDFLAGS := -shared -Wl,-Bsymbolic -Wl,-z,defs -Wl,--version-script=vamp-plugin.map -lpthread
 
 PLUGIN_EXT := .so
 
--- a/src/Silvet.cpp	Tue Apr 28 09:17:12 2015 +0100
+++ b/src/Silvet.cpp	Tue Apr 28 09:41:40 2015 +0100
@@ -30,6 +30,7 @@
 using std::cout;
 using std::cerr;
 using std::endl;
+using std::pair;
 using Vamp::RealTime;
 
 static int processingSampleRate = 44100;
@@ -541,9 +542,7 @@
 
     int width = filtered.size();
 
-    int iterations = m_hqMode ? 20 : 10;
-
-    Grid localPitches(width, vector<double>(pack.templateNoteCount, 0.0));
+    Grid localPitches(width);
 
     bool wantShifts = m_hqMode && m_fineTuning;
     int shiftCount = 1;
@@ -553,50 +552,13 @@
 
     vector<vector<int> > localBestShifts;
     if (wantShifts) {
-        localBestShifts = 
-            vector<vector<int> >(width, vector<int>(pack.templateNoteCount, 0));
+        localBestShifts = vector<vector<int> >(width);
     }
 
-    double columnThreshold = 1e-5;
-    
-#pragma omp parallel for
     for (int i = 0; i < width; ++i) {
-
-        double sum = 0.0;
-        for (int j = 0; j < pack.templateHeight; ++j) {
-            sum += filtered.at(i).at(j);
-        }
-        if (sum < columnThreshold) continue;
-
-        EM em(&pack, m_hqMode);
-
-        em.setPitchSparsity(pack.pitchSparsity);
-        em.setSourceSparsity(pack.sourceSparsity);
-
-        for (int j = 0; j < iterations; ++j) {
-            em.iterate(filtered.at(i).data());
-        }
-
-        const float *pitchDist = em.getPitchDistribution();
-        const float *const *shiftDist = em.getShifts();
-
-        for (int j = 0; j < pack.templateNoteCount; ++j) {
-
-            localPitches[i][j] = pitchDist[j] * sum;
-
-            int bestShift = 0;
-            float bestShiftValue = 0.0;
-            if (wantShifts) {
-                for (int k = 0; k < shiftCount; ++k) {
-                    float value = shiftDist[k][j];
-                    if (k == 0 || value > bestShiftValue) {
-                        bestShiftValue = value;
-                        bestShift = k;
-                    }
-                }
-                localBestShifts[i][j] = bestShift;
-            }                
-        }
+        auto out = applyEM(pack, filtered.at(i), wantShifts);
+        localPitches[i] = out.first;
+        if (wantShifts) localBestShifts[i] = out.second;
     }
         
     for (int i = 0; i < width; ++i) {
@@ -635,6 +597,62 @@
     return fs;
 }
 
+pair<vector<double>, vector<int> >
+Silvet::applyEM(const InstrumentPack &pack,
+                const vector<double> &column,
+                bool wantShifts)
+{
+    double columnThreshold = 1e-5;
+    
+    vector<double> pitches(pack.templateNoteCount, 0.0);
+    vector<int> bestShifts;
+    
+    double sum = 0.0;
+    for (int j = 0; j < pack.templateHeight; ++j) {
+        sum += column.at(j);
+    }
+    if (sum < columnThreshold) return { pitches, bestShifts };
+
+    EM em(&pack, m_hqMode);
+
+    em.setPitchSparsity(pack.pitchSparsity);
+    em.setSourceSparsity(pack.sourceSparsity);
+
+    int iterations = m_hqMode ? 20 : 10;
+
+    for (int j = 0; j < iterations; ++j) {
+        em.iterate(column.data());
+    }
+
+    const float *pitchDist = em.getPitchDistribution();
+    const float *const *shiftDist = em.getShifts();
+
+    int shiftCount = 1;
+    if (wantShifts) {
+        shiftCount = pack.templateMaxShift * 2 + 1;
+    }
+    
+    for (int j = 0; j < pack.templateNoteCount; ++j) {
+
+        pitches[j] = pitchDist[j] * sum;
+
+        int bestShift = 0;
+        float bestShiftValue = 0.0;
+        if (wantShifts) {
+            for (int k = 0; k < shiftCount; ++k) {
+                float value = shiftDist[k][j];
+                if (k == 0 || value > bestShiftValue) {
+                    bestShiftValue = value;
+                    bestShift = k;
+                }
+            }
+            bestShifts.push_back(bestShift);
+        }                
+    }
+
+    return { pitches, bestShifts };
+}
+
 Silvet::Grid
 Silvet::preProcess(const Grid &in)
 {
--- a/src/Silvet.h	Tue Apr 28 09:17:12 2015 +0100
+++ b/src/Silvet.h	Tue Apr 28 09:41:40 2015 +0100
@@ -92,6 +92,10 @@
 
     Grid preProcess(const Grid &);
 
+    std::pair<vector<double>, vector<int> > applyEM(const InstrumentPack &pack,
+                                                    const vector<double> &column,
+                                                    bool wantShifts);
+    
     vector<double> postProcess(const vector<double> &pitches,
                                const vector<int> &bestShifts,
                                bool wantShifts); // -> piano roll column