**
** Copyright (c) 2003 Kevin Rosenberg
**
-** $Id: wdq2wav.cpp,v 1.21 2003/02/25 18:14:10 kevin Exp $
+** $Id$
**
** 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.21 2003/02/25 18:14:10 kevin Exp $";
+const char* g_szIdStr = "$Id$";
bool g_quiet = false;
bool g_verbose = false;
bool g_debug = false;
bool g_ignore_zero = false;
-bool g_demean = false;
+bool g_dont_demean = false;
#ifdef WIN32
std::cout << "OPTIONS\n";
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 << " -z Scale output without preserving zero point\n";
+ std::cout << " -m Do not demean the data (don't 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";
g_quiet = true;
break;
case 'm':
- g_demean = true;
+ g_dont_demean = true;
+ break;
+ case 'z':
+ g_ignore_zero = true;
break;
case 'v':
g_verbose = true;
case 'p':
play = true;
break;
- case 'z':
- g_ignore_zero = true;
- break;
case 'h':
usage (progname);
return (0);
return true;
}
+bool read_float8 (int fd, double& f)
+{
+ unsigned char buf[8];
+ if (read (fd, &buf, 8) != 8)
+ return false;
+
+#if WORDS_BIG_ENDIAN
+ unsigned char c;
+ c = buf[0]; buf[0] = buf[7]; buf[7] = c;
+ c = buf[1]; buf[1] = buf[6]; buf[6] = c;
+ c = buf[2]; buf[2] = buf[5]; buf[5] = c;
+ c = buf[3]; buf[3] = buf[4]; buf[4] = c;
+#endif
+
+ f = *(reinterpret_cast<double*>(buf));
+
+ return true;
+}
+
bool
WindaqFile::ReadHeader ()
{
lseek (0, 0, SEEK_SET);
if (! read_int2 (m_fd, tmp2))
return false;
-
+ m_format = (tmp2 & 0xFF00) >> 8;
+ printf ("format=%d\n",m_format);
+ printf ("tmp2=%4x\n", tmp2);
m_nChannels = tmp2 & 0x1f;
m_sr_denom = (tmp2 & 0x7fff) >> 5;
m_sr_numer = (tmp2 & 0x8000) << 1;
return false;
m_sr_numer |= tmp2;
- m_sample_rate = (double) m_sr_numer / (double) (m_sr_denom * m_nChannels);
-
if (! read_int2 (m_fd, tmp2))
return false;
if (! read_int2 (m_fd, m_nHeader_bytes))
return false;
-
+
+ m_nMaxChannels = (m_nHeader_bytes - 112) / 36;
+ printf ("m_nMaxChannels = %d\n", m_nMaxChannels);
+
if (! read_int4 (m_fd, m_nData_bytes))
return false;
m_nSamples = (m_nData_bytes / m_nChannels) / 2;
+ lseek (m_fd, 28, SEEK_SET);
+ if (! read_float8 (m_fd, m_time_between_channel_samples))
+ return false;
+ printf ("time_between_channel_samples=%lf\n", m_time_between_channel_samples);
+
+ if (m_format == 0 || m_format == 1)
+ m_sample_rate = (double) m_nChannels / m_time_between_channel_samples;
+ else
+ m_sample_rate = (double) m_sr_numer / (double) (m_sr_denom * m_nChannels);
+
lseek (m_fd, 36, SEEK_SET);
if (! read_int4 (m_fd, m_time_acq_start))
return false;
m_valid = true;
return true;
}
-
WindaqChannel::WindaqChannel (WindaqFile& wdq, const int channel)
: r_wdq(wdq), m_data(0), m_slope(0), m_intercept (0), m_channel(channel),
delete m_data;
}
-bool get_float8 (int fd, double& f)
-{
- unsigned char buf[8];
- if (read (fd, &buf, 8) != 8)
- return false;
-
-#if WORDS_BIG_ENDIAN
- unsigned char c;
- c = buf[0]; buf[0] = buf[7]; buf[7] = c;
- c = buf[1]; buf[1] = buf[6]; buf[6] = c;
- c = buf[2]; buf[2] = buf[5]; buf[5] = c;
- c = buf[3]; buf[3] = buf[4]; buf[4] = c;
-#endif
-
- f = *(reinterpret_cast<double*>(buf));
-
- 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 ()
{
lseek (fd, r_wdq.m_channel_offset + 8 +
(m_channel - 1) * r_wdq.m_nBytes_channel_header,
SEEK_SET);
- if (! get_float8 (fd, m_slope))
+ if (! read_float8 (fd, m_slope))
return false;
- if (! get_float8 (fd, m_intercept))
+ if (! read_float8 (fd, m_intercept))
return false;
char units[7];
m_units = units;
long int row_bytes = 2 * r_wdq.m_nChannels;
+
signed short int *sample_row = new signed short int [row_bytes];
signed short int* psample = &sample_row[m_channel - 1];
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;
+ if (! g_dont_demean) {
+ double dmean = total_data / static_cast<double>(r_wdq.m_nSamples);
int mean = nearest<int>(dmean);
+ std::cout << "Removing mean: " << (dmean * m_slope) + m_intercept <<
+ " " << m_units << std::endl;
+
for (i = 0; i < r_wdq.m_nSamples; i++)
m_data[i] -= mean;
}
if (g_ignore_zero) {
value = (value + data_offset) * data_scale;
value += 0.5 - 32768;
- } else
+ } else {
value = value * data_scale;
+ }
signed short int v = static_cast<signed short int>(value);
#if WORDS_BIG_ENDIAN