changeset 97:e599e0b13388

Add ability to write an extra sound file with the difference between input and output
author Chris Cannam <c.cannam@qmul.ac.uk>
date Sat, 10 May 2014 11:48:21 +0100
parents 5ceb37190bd9
children fb5bcb44277a
files cpp-qm-dsp/processfile.cpp
diffstat 1 files changed, 45 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cpp-qm-dsp/processfile.cpp	Fri May 09 17:43:00 2014 +0100
+++ b/cpp-qm-dsp/processfile.cpp	Sat May 10 11:48:21 2014 +0100
@@ -17,18 +17,22 @@
 
 int main(int argc, char **argv)
 {
-    if (argc != 3) {
-	cerr << "Usage: " << argv[0] << " infile.wav outfile.wav" << endl;
+    if (argc < 3 || argc > 4) {
+	cerr << "Usage: " << argv[0] << " infile.wav outfile.wav [differencefile.wav]" << endl;
 	return 2;
     }
 
     char *fileName = strdup(argv[1]);
     char *fileNameOut = strdup(argv[2]);
+    char *diffFileName = (argc == 4 ? strdup(argv[3]) : 0);
+    bool doDiff = (diffFileName != 0);
 
     SNDFILE *sndfile;
     SNDFILE *sndfileOut;
+    SNDFILE *sndDiffFile = 0;
     SF_INFO sfinfo;
     SF_INFO sfinfoOut;
+    SF_INFO sfinfoDiff;
     memset(&sfinfo, 0, sizeof(SF_INFO));
 
     sndfile = sf_open(fileName, SFM_READ, &sfinfo);
@@ -52,6 +56,16 @@
 	return 1;
     }
 
+    if (doDiff) {
+	sfinfoDiff = sfinfoOut;
+	sndDiffFile = sf_open(diffFileName, SFM_WRITE, &sfinfoDiff);
+	if (!sndDiffFile) {
+	    cerr << "ERROR: Failed to open diff output file \"" << diffFileName << "\" for writing: "
+		 << sf_strerror(sndDiffFile) << endl;
+	    return 1;
+	}
+    }
+
     int ibs = 1024;
     int channels = sfinfo.channels;
     float *fbuf = new float[channels * ibs];
@@ -68,6 +82,8 @@
     int outframe = 0;
     int latency = cq.getLatency() + cqi.getLatency();
 
+    vector<double> buffer;
+
     cerr << "forward latency = " << cq.getLatency() << ", inverse latency = " 
 	 << cqi.getLatency() << ", total = " << latency << endl;
 
@@ -93,6 +109,10 @@
 	    }
 	    cqin.push_back(v);
 	}
+
+	if (doDiff) {
+	    buffer.insert(buffer.end(), cqin.begin(), cqin.end());
+	}
 	
 	vector<double> cqout = cqi.process(cq.process(cqin));
 
@@ -110,6 +130,26 @@
 			     cqout.size() - offset);
 	}
 
+	if (doDiff) {
+	    for (int i = 0; i < (int)cqout.size(); ++i) {
+		cqout[i] -= buffer[outframe + i - latency];
+	    }
+	    
+	    if (outframe >= latency) {
+
+		sf_writef_double(sndDiffFile, 
+				 cqout.data(), 
+				 cqout.size());
+
+	    } else if (outframe + (int)cqout.size() >= latency) {
+
+		int offset = latency - outframe;
+		sf_writef_double(sndDiffFile, 
+				 cqout.data() + offset,
+				 cqout.size() - offset);
+	    }
+	}
+
 	inframe += count;
 	outframe += cqout.size();
     }
@@ -125,6 +165,9 @@
 
     sf_close(sndfile);
     sf_close(sndfileOut);
+    if (doDiff) {
+	sf_close(sndDiffFile);
+    }
 
     cerr << "in: " << inframe << ", out: " << outframe - latency << endl;