1 /*****************************************************************************
5 ** Purpose: Converts a channel of WinDAQ file to .WAV format
6 ** Programmer: Kevin Rosenberg <kevin@rosenberg.net>
7 ** Date Started: Jan 2003
9 ** Copyright (c) 2003 Kevin Rosenberg
12 ** Quick & Dirty hack, but efficient and good error checking
13 ** Allocates memory equal to the size of channel (2 * number of samples)
16 ** Need command line options for verbose and debug output
17 ** Should comment the reading and writing of files to document the file formats
19 ** $Id: wdq2wav.cpp,v 1.7 2003/01/21 04:19:44 kevin Exp $
21 ** This program is free software; you can redistribute it and/or modify
22 ** it under the terms of the GNU General Public License (version 2) as
23 ** published by the Free Software Foundation.
25 ** This program is distributed in the hope that it will be useful,
26 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ** GNU General Public License for more details.
30 ** You should have received a copy of the GNU General Public License
31 ** along with this program; if not, write to the Free Software
32 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 ******************************************************************************/
37 static bool g_verbose = true;
38 static bool g_debug = false;
41 error_msg (const char *msg)
43 fprintf (stderr, "%s\n",msg);
47 info_msg (const char* msg)
49 fprintf (stdout, "%s\n", msg);
53 info_msg_sans_newline (const char* msg)
55 fprintf (stdout, "%s", msg);
61 error_msg ("usage: wdq2wav <wdq-file> <channel-number> <wav-file>");
64 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname);
67 main (int argc, char *argv[])
74 char *wdq_fname = argv[1];
76 int channel = (int) strtol (argv[2], &channel_endptr, 10);
77 char *wav_fname = argv[3];
79 if (*channel_endptr != 0) {
80 std::ostringstream os;
81 os << "Error: Channel " << argv[2] << " is not an integer";
82 error_msg (os.str().c_str());
87 if (! wdq2wav (wdq_fname, channel, wav_fname))
94 wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
96 WindaqFile wdq (wdq_fname);
98 if (! wdq.ReadHeader()) {
102 std::ostringstream os1;
103 os1 << "File " << wdq_fname;
104 info_msg (os1.str().c_str());
105 std::ostringstream os2;
106 time_t time = wdq.m_time_acq_start;
107 struct tm* tm = gmtime (&time);
108 os2 << " Time File Creation: " << asctime(tm);
109 info_msg_sans_newline (os2.str().c_str());
110 std::ostringstream os3;
111 time = wdq.m_time_acq_stop;
113 os3 << " Time File Written: " << asctime(tm);
114 info_msg_sans_newline (os3.str().c_str());
115 std::ostringstream os4;
116 os4 << " Samples: " << wdq.m_nSamples <<
117 ", Channels: " << wdq.m_nChannels <<
118 ", Sample Rate: " << wdq.m_sample_rate;
119 info_msg (os4.str().c_str());
122 WindaqChannel wdq_channel (wdq, channel);
123 if (! wdq_channel.m_valid) {
124 error_msg ("Error reading data from channel");
129 std::ostringstream os1;
130 os1 << "Channel " << channel;
131 info_msg (os1.str().c_str());
132 std::ostringstream os2;
133 os2 << " Units: " << wdq_channel.m_units.c_str();
134 info_msg (os2.str().c_str());
135 std::ostringstream os3;
136 os3 << " Raw minimum: " << wdq_channel.m_min_raw_data <<
137 ", maximum: " << wdq_channel.m_max_raw_data;
138 info_msg (os3.str().c_str());
139 std::ostringstream os4;
140 os4 << " Scaled minimum: " << wdq_channel.m_min_scaled_data <<
141 ", maximum: " << wdq_channel.m_max_scaled_data;
142 info_msg (os4.str().c_str());
143 std::ostringstream os5;
144 os5 << " Slope " << wdq_channel.m_slope <<
145 ", Intercept " << wdq_channel.m_intercept;
146 info_msg (os5.str().c_str());
149 WavFile wav (wdq_channel, wav_fname);
152 error_msg ("Error extracting wav from channel");
156 if (! wav.WriteFile ()) {
157 error_msg ("Error writing file");
165 WindaqFile::WindaqFile (const char* fname)
166 : m_fd(0), m_nChannels(0), m_nSamples(0), m_sample_rate(0), m_valid(false),
171 WindaqFile::~WindaqFile ()
178 WindaqFile::ReadHeader ()
180 unsigned short int tmp2;
184 if ((m_fd = open (m_strFile.c_str(), O_RDONLY)) == 0)
187 lseek (0, 0, SEEK_SET);
188 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
189 error_msg ("Error reading file");
192 m_nChannels = tmp2 & 0x1f;
193 m_sr_denom = (tmp2 & 0x7fff) >> 5;
194 m_sr_numer = (tmp2 & 0x8000) << 1;
196 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
197 error_msg ("Error reading file");
201 m_sample_rate = (double) m_sr_numer / (double) (m_sr_denom * m_nChannels);
203 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
204 error_msg ("Error reading file");
207 m_channel_offset = tmp2 & 0xFF;
208 m_nBytes_channel_header = tmp2 >> 8;
210 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
211 error_msg ("Error reading file");
214 m_nHeader_bytes = tmp2;
216 if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
217 error_msg ("Error reading file");
220 m_nData_bytes = tmp4;
221 m_nSamples = (m_nData_bytes / m_nChannels) / 2;
223 lseek (m_fd, 36, SEEK_SET);
224 if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
225 error_msg ("Error reading file");
228 m_time_acq_start = tmp4;
229 if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
230 error_msg ("Error reading file");
233 m_time_acq_stop = tmp4;
235 // Verify Windaq signature
236 lseek (m_fd, m_nHeader_bytes - 2, SEEK_SET);
237 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
238 error_msg ("Error reading file");
241 if (tmp2 != 0x8001) {
242 std::ostringstream os;
243 os << m_strFile << " is not a WinDAQ file";
244 error_msg (os.str().c_str());
253 WindaqChannel::WindaqChannel (WindaqFile& wdq, const int channel)
254 : r_wdq(wdq), m_data(0), m_slope(0), m_intercept (0), m_channel(channel),
258 if (channel >= 1 && channel <= wdq.m_nChannels) {
262 std::ostringstream os;
263 os << "Channel " << channel << " is invalid, valid range 1-" <<
265 error_msg (os.str().c_str());
270 WindaqChannel::~WindaqChannel ()
277 WindaqChannel::read_channel_data ()
279 unsigned short int tmp2;
288 m_data = new signed short int [r_wdq.m_nSamples * 2];
290 lseek (fd, r_wdq.m_channel_offset + 8 +
291 (m_channel - 1) * r_wdq.m_nBytes_channel_header,
293 if (read (fd, &float8, sizeof(float8)) != sizeof(float8)) {
294 error_msg ("Error reading file");
298 if (read (fd, &float8, sizeof(float8)) != sizeof(float8)) {
299 error_msg ("Error reading file");
302 m_intercept = float8;
306 if (read (fd, units, 6) != 6) {
307 error_msg ("Error reading file");
312 unsigned int row_bytes = 2 * r_wdq.m_nChannels;
313 signed short int sample_row [row_bytes];
315 signed short int* psample = &sample_row[m_channel - 1];
317 lseek (fd, r_wdq.m_nHeader_bytes, SEEK_SET);
319 signed short int data_max, data_min;
320 for (i = 0; i < r_wdq.m_nSamples; i++) {
321 if (read (fd, sample_row, row_bytes) != row_bytes) {
322 std::ostringstream os;
323 os << "Error reading file at " << i;
324 error_msg (os.str().c_str());
328 signed short int value = *psample >> 2;
335 if (value > data_max)
337 else if (value < data_min)
342 m_max_raw_data = data_max;
343 m_min_raw_data = data_min;
344 m_max_scaled_data = (m_slope * data_max) + m_intercept;
345 m_min_scaled_data = (m_slope * data_min) + m_intercept;
351 WavFile::WavFile (WindaqChannel& wdq_channel, const char* fname)
352 : m_valid(false), m_data(0), m_nSamples(0), m_strFile(fname), m_fd(0)
354 if (wdq_channel.m_valid) {
355 m_nSamples = wdq_channel.r_wdq.m_nSamples;
357 m_nBitsPerSample = 16;
358 m_nBytesPerSample = 2;
359 m_rate = wdq_channel.r_wdq.m_sample_rate;
360 m_data = new signed short int [m_nSamples];
362 double data_offset = -wdq_channel.m_min_scaled_data;
363 double data_scale = 0.;
364 if (wdq_channel.m_max_scaled_data != wdq_channel.m_min_scaled_data)
365 data_scale = 65535. / (wdq_channel.m_max_scaled_data -
366 wdq_channel.m_min_scaled_data);
369 printf ("data_scale: %f, data_offset: %f\n", data_scale, data_offset);
371 signed short int* input = wdq_channel.m_data;
372 double slope = wdq_channel.m_slope;
373 double intercept = wdq_channel.m_intercept;
376 for (i = 0; i < m_nSamples; i++) {
377 double value = input[i];
378 value = (slope * value) + intercept;
379 value = (value + data_offset) * data_scale;
380 value += 0.5 - 32768;
381 m_data[i] = static_cast<signed short int>(value);
399 WavFile::WriteFile ()
401 unsigned short int tmp2;
403 unsigned long data_bytes = m_nSamples * 2;
409 if ((m_fd = open (m_strFile.c_str(), O_WRONLY | O_TRUNC | O_CREAT,
410 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == 0) {
411 std::ostringstream os;
412 os << "Error opening output file " << m_strFile.c_str();
413 error_msg (os.str().c_str());
417 lseek (m_fd, 0, SEEK_SET);
418 if (write (m_fd, "RIFF", 4) != 4) {
419 error_msg ("Error writing file");
423 /* Length of file post 8 byte header */
424 tmp4 = 36 + data_bytes;
425 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
426 error_msg ("Error writing file");
430 if (write (m_fd, "WAVEfmt ", 8) != 8) {
431 error_msg ("Error writing file");
435 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
436 error_msg ("Error writing file");
440 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
441 error_msg ("Error writing file");
444 /* Number of channels */
446 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
447 error_msg ("Error writing file");
450 tmp4 = static_cast<int> (m_rate + 0.5);
452 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
453 error_msg ("Error writing file");
456 tmp4 = static_cast<int> (m_rate * m_nBytesPerSample + 0.5);
457 /* Bytes per second */
458 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
459 error_msg ("Error writing file");
462 tmp2 = m_nBytesPerSample * m_nChannels;
463 /* Bytes per sample */
464 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
465 error_msg ("Error writing file");
468 tmp2 = m_nBitsPerSample;
469 /* Bits per sample */
470 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
471 error_msg ("Error writing file");
474 if (write (m_fd, "data", 4) != 4) {
475 error_msg ("Error writing file");
480 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
481 error_msg ("Error writing file");
486 for (i = 0; i < m_nSamples; i++) {
487 signed short int stmp2 = m_data[i];
488 if (write (m_fd, &stmp2, sizeof(stmp2)) != sizeof(stmp2)) {
489 error_msg ("Error writing file");
494 if (close (m_fd) < 0)
495 error_msg ("Error closing output file");