r4121: Auto commit for Debian build
[wdq2wav.git] / wdq2wav.cpp
index a3320b2fe3d1eab06140478ed8b9112cdc69e4ba..81c52a4539c3b0411fd75979fec9fb20255f35e9 100644 (file)
@@ -8,7 +8,7 @@
 **
 **  Copyright (c) 2003 Kevin Rosenberg
 **
-**  $Id: wdq2wav.cpp,v 1.20 2003/02/24 13:01:02 kevin Exp $
+**  $Id: wdq2wav.cpp,v 1.21 2003/02/25 18:14:10 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 
 #include <wdq2wav.h>
 
-const char* g_szIdStr = "$Id: wdq2wav.cpp,v 1.20 2003/02/24 13:01:02 kevin Exp $";
+const char* g_szIdStr = "$Id: wdq2wav.cpp,v 1.21 2003/02/25 18:14:10 kevin Exp $";
 
 bool g_quiet = false;
 bool g_verbose = false;
 bool g_debug = false;
 bool g_ignore_zero = false;
+bool g_demean = false;
 
 
 #ifdef WIN32
@@ -111,6 +112,7 @@ usage (const char* progname)
   std::cout << "  -p   Play channel through audio system\n";
   std::cout << "  -q   Supress all messages\n";
   std::cout << "  -z   Scale output without regard for windaq zero point\n";
+  std::cout << "  -m   Demean the data (subtract the mean value from each sample)\n";
   std::cout << "  -v   Verbose mode\n";
   std::cout << "  -d   Debug mode\n";
   std::cout << "  -r   Print program version\n";
@@ -126,7 +128,7 @@ main (int argc, char *argv[])
 
   const char* progname = argv[0];
   
-  while ((c = getopt (argc, argv, "rqvzdph")) != -1) {
+  while ((c = getopt (argc, argv, "rqvzmdph")) != -1) {
     switch (c) {
     case 'r':
         std::cout << "Version " << g_szIdStr << std::endl;
@@ -134,6 +136,9 @@ main (int argc, char *argv[])
     case 'q':
       g_quiet = true;
       break;
+    case 'm':
+      g_demean = true;
+      break;
     case 'v':
       g_verbose = true;
       break;
@@ -434,6 +439,9 @@ bool get_float8 (int fd, double& f)
   return true;
 }
 
+inline T nearest (double x)
+{ return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
+
 bool
 WindaqChannel::read_channel_data ()
 {
@@ -466,6 +474,7 @@ WindaqChannel::read_channel_data ()
   lseek (fd, r_wdq.m_nHeader_bytes, SEEK_SET);
   unsigned long int i;
   signed short int data_max = 0, data_min = 0;
+  double total_data = 0;
   for (i = 0; i < r_wdq.m_nSamples; i++) {
     if (read (fd, sample_row, row_bytes) != row_bytes) {
       std::ostringstream os;
@@ -483,6 +492,7 @@ WindaqChannel::read_channel_data ()
 
     signed short int value = v >> 2;
     m_data[i] = value;
+    total_data += value;
     
     if (i == 0) {
       data_max = value;
@@ -500,6 +510,13 @@ WindaqChannel::read_channel_data ()
   m_max_scaled_data = (m_slope * data_max) + m_intercept;
   m_min_scaled_data = (m_slope * data_min) + m_intercept;
 
+  if (g_demean) {
+    double dmean = total_data / static_cast<double> wdq.m_nSamples;
+    int mean = nearest<int>(dmean);
+    for (i = 0; i < r_wdq.m_nSamples; i++)
+      m_data[i] -= mean;
+  }
+  
   delete sample_row;
   return true;
 }