r3829: *** empty log message ***
[wdq2wav.git] / wdq2wav.cpp
index bc0ae3b9cc021e719308a6ad5ec7d2ade87cd68b..7a57e9181ddad913ac3674501b30fc4194e59d5e 100644 (file)
 **   Allocates memory equal to the size of channel (2 * number of samples)
 **
 **  BUGS
-**   Need to abstract error checking so it is not so redundant
 **   Need command line options for verbose and debug output
 **   Should comment the reading and writing of files to document the file formats
 **
-**  $Id: wdq2wav.cpp,v 1.1 2003/01/21 01:58:32 kevin Exp $
+**  $Id: wdq2wav.cpp,v 1.7 2003/01/21 04:19:44 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>
 
 static bool g_verbose = true;
-static bool g_debug = true;
+static bool g_debug = false;
 
 void
-error_msg (char *msg)
+error_msg (const char *msg)
 {
   fprintf (stderr, "%s\n",msg);
 }
 
+void
+info_msg (const char* msg)
+{
+  fprintf (stdout, "%s\n", msg);
+}
+
+void
+info_msg_sans_newline (const char* msg)
+{
+  fprintf (stdout, "%s", msg);
+}
+
 void
 usage ()
 {
-  fprintf (stderr, "usage: wdq2wav <wdq-file> <channel-number> <wav-file>\n");
+  error_msg ("usage: wdq2wav <wdq-file> <channel-number> <wav-file>");
 }
 
 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname);
@@ -66,7 +77,9 @@ main (int argc, char *argv[])
   char *wav_fname = argv[3];
 
   if (*channel_endptr != 0) {
-    fprintf (stderr, "Error: Channel %s is not an integer\n", argv[2]);
+    std::ostringstream os;
+    os << "Error: Channel " << argv[2] << " is not an integer";
+    error_msg (os.str().c_str());
     usage();
     return(1);
   }
@@ -77,18 +90,33 @@ main (int argc, char *argv[])
   return 0;
 }
 
-bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
+bool
+wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
 {
   WindaqFile wdq (wdq_fname);
 
   if (! wdq.ReadHeader()) {
-    fprintf (stderr, "Error reading header %s\n", wdq_fname);
     return false;
   }
   if (g_verbose) {
-    printf ("Number of channels: %d\n", wdq.m_nChannels);
-    printf ("Number of Samples: %d\n", wdq.m_nSamples);
-    printf ("Sample Rate: %.1f\n", wdq.m_sample_rate);
+    std::ostringstream os1;
+    os1 << "File " << wdq_fname;
+    info_msg (os1.str().c_str());
+    std::ostringstream os2;
+    time_t time = wdq.m_time_acq_start;
+    struct tm* tm = gmtime (&time);
+    os2 << "  Time File Creation: " << asctime(tm);
+    info_msg_sans_newline (os2.str().c_str());
+    std::ostringstream os3;
+    time = wdq.m_time_acq_stop;
+    tm = gmtime (&time);
+    os3 << "  Time File Written: " << asctime(tm);
+    info_msg_sans_newline (os3.str().c_str());
+    std::ostringstream os4;
+    os4 << "  Samples: " << wdq.m_nSamples <<
+      ", Channels: " << wdq.m_nChannels <<
+      ", Sample Rate: " << wdq.m_sample_rate;
+    info_msg (os4.str().c_str());
   }
   
   WindaqChannel wdq_channel (wdq, channel);
@@ -98,8 +126,24 @@ bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
   }
 
   if (g_verbose) {
-    printf ("Data Min: %f, Data Max: %f\n", 
-           wdq_channel.m_min_raw_data, wdq_channel.m_max_raw_data);
+    std::ostringstream os1;
+    os1 << "Channel " << channel;
+    info_msg (os1.str().c_str());
+    std::ostringstream os2;
+    os2 << "  Units: " << wdq_channel.m_units.c_str();
+    info_msg (os2.str().c_str());
+    std::ostringstream os3;
+    os3 << "  Raw minimum: " << wdq_channel.m_min_raw_data <<
+      ", maximum: " << wdq_channel.m_max_raw_data;
+    info_msg (os3.str().c_str());
+    std::ostringstream os4;
+    os4 << "  Scaled minimum: " << wdq_channel.m_min_scaled_data <<
+      ", maximum: " << wdq_channel.m_max_scaled_data;
+    info_msg (os4.str().c_str());
+    std::ostringstream os5;
+    os5 << "  Slope " <<  wdq_channel.m_slope <<
+      ", Intercept " <<  wdq_channel.m_intercept;
+    info_msg (os5.str().c_str());
   }
 
   WavFile wav (wdq_channel, wav_fname);
@@ -176,6 +220,18 @@ WindaqFile::ReadHeader ()
   m_nData_bytes = tmp4;
   m_nSamples = (m_nData_bytes / m_nChannels) / 2;
 
+  lseek (m_fd, 36, SEEK_SET);
+  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
+    error_msg ("Error reading file");
+    return false;
+  }
+  m_time_acq_start = tmp4;
+  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
+    error_msg ("Error reading file");
+    return false;
+  }
+  m_time_acq_stop = tmp4;
+  
   // Verify Windaq signature
   lseek (m_fd, m_nHeader_bytes - 2, SEEK_SET);
   if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
@@ -183,7 +239,9 @@ WindaqFile::ReadHeader ()
     return false;
   }
   if (tmp2 != 0x8001) {
-    fprintf (stderr, "%s is not a WinDAQ file\n", m_strFile.c_str());
+    std::ostringstream os;
+    os << m_strFile << " is not a WinDAQ file";
+    error_msg (os.str().c_str());
     return false;
   }
 
@@ -200,9 +258,12 @@ WindaqChannel::WindaqChannel (WindaqFile& wdq, const int channel)
     if (channel >= 1 && channel <= wdq.m_nChannels) {
       m_valid = true;
       read_channel_data();
-    } else
-      fprintf (stderr, "Channel %d is invalid, valid range 1-%d\n",
-             channel, wdq.m_nChannels);
+    } else {
+      std::ostringstream os;
+      os << "Channel " << channel << " is invalid, valid range 1-" <<
+       wdq.m_nChannels;
+      error_msg (os.str().c_str());
+    }
   }
 }
 
@@ -240,9 +301,6 @@ WindaqChannel::read_channel_data ()
   }
   m_intercept = float8;
 
-  if (g_verbose)
-    printf ("Slope: %f, Intercept: %f\n", m_slope, m_intercept);
-
   char units[7];
   units[6] = 0;
   if (read (fd, units, 6) != 6) {
@@ -250,8 +308,6 @@ WindaqChannel::read_channel_data ()
     return false;
   }
   m_units = units;
-  if (g_verbose)
-    printf ("Units: %s\n", units);
  
   unsigned int row_bytes = 2 * r_wdq.m_nChannels;
   signed short int sample_row [row_bytes];
@@ -263,7 +319,9 @@ WindaqChannel::read_channel_data ()
   signed short int data_max, data_min;
   for (i = 0; i < r_wdq.m_nSamples; i++) {
     if (read (fd, sample_row, row_bytes) != row_bytes) {
-      fprintf (stderr, "Error reading file at %d\n", i);
+      std::ostringstream os;
+      os << "Error reading file at " << i;
+      error_msg (os.str().c_str());
       return false;
     }
     
@@ -350,7 +408,9 @@ WavFile::WriteFile ()
   if (m_fd == 0)
     if ((m_fd = open (m_strFile.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 
                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == 0) {
-      fprintf (stderr, "Error opening output file %s\n", m_strFile.c_str());
+      std::ostringstream os;
+      os << "Error opening output file " << m_strFile.c_str();
+      error_msg (os.str().c_str());
       return false;
     }