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 to abstract error checking so it is not so redundant
17 ** Need command line options for verbose and debug output
18 ** Should comment the reading and writing of files to document the file formats
20 ** $Id: wdq2wav.cpp,v 1.1 2003/01/21 01:58:32 kevin Exp $
22 ** This program is free software; you can redistribute it and/or modify
23 ** it under the terms of the GNU General Public License (version 2) as
24 ** published by the Free Software Foundation.
26 ** This program is distributed in the hope that it will be useful,
27 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ** GNU General Public License for more details.
31 ** You should have received a copy of the GNU General Public License
32 ** along with this program; if not, write to the Free Software
33 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 ******************************************************************************/
38 static bool g_verbose = true;
39 static bool g_debug = true;
44 fprintf (stderr, "%s\n",msg);
50 fprintf (stderr, "usage: wdq2wav <wdq-file> <channel-number> <wav-file>\n");
53 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname);
56 main (int argc, char *argv[])
63 char *wdq_fname = argv[1];
65 int channel = (int) strtol (argv[2], &channel_endptr, 10);
66 char *wav_fname = argv[3];
68 if (*channel_endptr != 0) {
69 fprintf (stderr, "Error: Channel %s is not an integer\n", argv[2]);
74 if (! wdq2wav (wdq_fname, channel, wav_fname))
80 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
82 WindaqFile wdq (wdq_fname);
84 if (! wdq.ReadHeader()) {
85 fprintf (stderr, "Error reading header %s\n", wdq_fname);
89 printf ("Number of channels: %d\n", wdq.m_nChannels);
90 printf ("Number of Samples: %d\n", wdq.m_nSamples);
91 printf ("Sample Rate: %.1f\n", wdq.m_sample_rate);
94 WindaqChannel wdq_channel (wdq, channel);
95 if (! wdq_channel.m_valid) {
96 error_msg ("Error reading data from channel");
101 printf ("Data Min: %f, Data Max: %f\n",
102 wdq_channel.m_min_raw_data, wdq_channel.m_max_raw_data);
105 WavFile wav (wdq_channel, wav_fname);
108 error_msg ("Error extracting wav from channel");
112 if (! wav.WriteFile ()) {
113 error_msg ("Error writing file");
121 WindaqFile::WindaqFile (const char* fname)
122 : m_fd(0), m_nChannels(0), m_nSamples(0), m_sample_rate(0), m_valid(false),
127 WindaqFile::~WindaqFile ()
134 WindaqFile::ReadHeader ()
136 unsigned short int tmp2;
140 if ((m_fd = open (m_strFile.c_str(), O_RDONLY)) == 0)
143 lseek (0, 0, SEEK_SET);
144 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
145 error_msg ("Error reading file");
148 m_nChannels = tmp2 & 0x1f;
149 m_sr_denom = (tmp2 & 0x7fff) >> 5;
150 m_sr_numer = (tmp2 & 0x8000) << 1;
152 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
153 error_msg ("Error reading file");
157 m_sample_rate = (double) m_sr_numer / (double) (m_sr_denom * m_nChannels);
159 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
160 error_msg ("Error reading file");
163 m_channel_offset = tmp2 & 0xFF;
164 m_nBytes_channel_header = tmp2 >> 8;
166 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
167 error_msg ("Error reading file");
170 m_nHeader_bytes = tmp2;
172 if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
173 error_msg ("Error reading file");
176 m_nData_bytes = tmp4;
177 m_nSamples = (m_nData_bytes / m_nChannels) / 2;
179 // Verify Windaq signature
180 lseek (m_fd, m_nHeader_bytes - 2, SEEK_SET);
181 if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
182 error_msg ("Error reading file");
185 if (tmp2 != 0x8001) {
186 fprintf (stderr, "%s is not a WinDAQ file\n", m_strFile.c_str());
195 WindaqChannel::WindaqChannel (WindaqFile& wdq, const int channel)
196 : r_wdq(wdq), m_data(0), m_slope(0), m_intercept (0), m_channel(channel),
200 if (channel >= 1 && channel <= wdq.m_nChannels) {
204 fprintf (stderr, "Channel %d is invalid, valid range 1-%d\n",
205 channel, wdq.m_nChannels);
209 WindaqChannel::~WindaqChannel ()
216 WindaqChannel::read_channel_data ()
218 unsigned short int tmp2;
227 m_data = new signed short int [r_wdq.m_nSamples * 2];
229 lseek (fd, r_wdq.m_channel_offset + 8 +
230 (m_channel - 1) * r_wdq.m_nBytes_channel_header,
232 if (read (fd, &float8, sizeof(float8)) != sizeof(float8)) {
233 error_msg ("Error reading file");
237 if (read (fd, &float8, sizeof(float8)) != sizeof(float8)) {
238 error_msg ("Error reading file");
241 m_intercept = float8;
244 printf ("Slope: %f, Intercept: %f\n", m_slope, m_intercept);
248 if (read (fd, units, 6) != 6) {
249 error_msg ("Error reading file");
254 printf ("Units: %s\n", units);
256 unsigned int row_bytes = 2 * r_wdq.m_nChannels;
257 signed short int sample_row [row_bytes];
259 signed short int* psample = &sample_row[m_channel - 1];
261 lseek (fd, r_wdq.m_nHeader_bytes, SEEK_SET);
263 signed short int data_max, data_min;
264 for (i = 0; i < r_wdq.m_nSamples; i++) {
265 if (read (fd, sample_row, row_bytes) != row_bytes) {
266 fprintf (stderr, "Error reading file at %d\n", i);
270 signed short int value = *psample >> 2;
277 if (value > data_max)
279 else if (value < data_min)
284 m_max_raw_data = data_max;
285 m_min_raw_data = data_min;
286 m_max_scaled_data = (m_slope * data_max) + m_intercept;
287 m_min_scaled_data = (m_slope * data_min) + m_intercept;
293 WavFile::WavFile (WindaqChannel& wdq_channel, const char* fname)
294 : m_valid(false), m_data(0), m_nSamples(0), m_strFile(fname), m_fd(0)
296 if (wdq_channel.m_valid) {
297 m_nSamples = wdq_channel.r_wdq.m_nSamples;
299 m_nBitsPerSample = 16;
300 m_nBytesPerSample = 2;
301 m_rate = wdq_channel.r_wdq.m_sample_rate;
302 m_data = new signed short int [m_nSamples];
304 double data_offset = -wdq_channel.m_min_scaled_data;
305 double data_scale = 0.;
306 if (wdq_channel.m_max_scaled_data != wdq_channel.m_min_scaled_data)
307 data_scale = 65535. / (wdq_channel.m_max_scaled_data -
308 wdq_channel.m_min_scaled_data);
311 printf ("data_scale: %f, data_offset: %f\n", data_scale, data_offset);
313 signed short int* input = wdq_channel.m_data;
314 double slope = wdq_channel.m_slope;
315 double intercept = wdq_channel.m_intercept;
318 for (i = 0; i < m_nSamples; i++) {
319 double value = input[i];
320 value = (slope * value) + intercept;
321 value = (value + data_offset) * data_scale;
322 value += 0.5 - 32768;
323 m_data[i] = static_cast<signed short int>(value);
341 WavFile::WriteFile ()
343 unsigned short int tmp2;
345 unsigned long data_bytes = m_nSamples * 2;
351 if ((m_fd = open (m_strFile.c_str(), O_WRONLY | O_TRUNC | O_CREAT,
352 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == 0) {
353 fprintf (stderr, "Error opening output file %s\n", m_strFile.c_str());
357 lseek (m_fd, 0, SEEK_SET);
358 if (write (m_fd, "RIFF", 4) != 4) {
359 error_msg ("Error writing file");
363 /* Length of file post 8 byte header */
364 tmp4 = 36 + data_bytes;
365 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
366 error_msg ("Error writing file");
370 if (write (m_fd, "WAVEfmt ", 8) != 8) {
371 error_msg ("Error writing file");
375 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
376 error_msg ("Error writing file");
380 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
381 error_msg ("Error writing file");
384 /* Number of channels */
386 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
387 error_msg ("Error writing file");
390 tmp4 = static_cast<int> (m_rate + 0.5);
392 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
393 error_msg ("Error writing file");
396 tmp4 = static_cast<int> (m_rate * m_nBytesPerSample + 0.5);
397 /* Bytes per second */
398 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
399 error_msg ("Error writing file");
402 tmp2 = m_nBytesPerSample * m_nChannels;
403 /* Bytes per sample */
404 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
405 error_msg ("Error writing file");
408 tmp2 = m_nBitsPerSample;
409 /* Bits per sample */
410 if (write (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
411 error_msg ("Error writing file");
414 if (write (m_fd, "data", 4) != 4) {
415 error_msg ("Error writing file");
420 if (write (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
421 error_msg ("Error writing file");
426 for (i = 0; i < m_nSamples; i++) {
427 signed short int stmp2 = m_data[i];
428 if (write (m_fd, &stmp2, sizeof(stmp2)) != sizeof(stmp2)) {
429 error_msg ("Error writing file");
434 if (close (m_fd) < 0)
435 error_msg ("Error closing output file");