r8646: support new windaq format
[wdq2wav.git] / wdq2wav.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          wdq2wav.h
5 **   Purpose:       Header file for wdq2wav.cpp
6 **   Programmer:    Kevin Rosenberg <kevin@rosenberg.net>
7 **   Date Started:  Jan 2003
8 **
9 **  Copyright (c) 2003 Kevin Rosenberg
10 **
11 **  $Id$
12 **
13 **  This program is free software; you can redistribute it and/or modify
14 **  it under the terms of the GNU General Public License (version 2) as
15 **  published by the Free Software Foundation.
16 **
17 **  This program is distributed in the hope that it will be useful,
18 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 **  GNU General Public License for more details.
21 **
22 **  You should have received a copy of the GNU General Public License
23 **  along with this program; if not, write to the Free Software
24 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 ******************************************************************************/
26
27 #include <stdio.h>
28 #include <iostream>
29 #include <cstdio>
30 #include <cstring>
31 #include <string>
32 #include <sstream>
33 #include <ctime>
34 #include <fcntl.h>
35 #include <math.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef WIN32
39 #include <io.h>
40 #include <getopt.h>
41 #else
42 #include <unistd.h>
43 #endif
44
45 #ifdef LINUX
46 #include <endian.h>
47 #if __BYTE_ORDER == __BIG_ENDIAN
48 #define WORDS_BIG_ENDIAN 1
49 #endif
50 #endif
51
52 extern const char* g_szIdStr;
53 extern bool g_quiet;
54 extern bool g_verbose;
55 extern bool g_debug;
56
57 #define MAX_INPUT_STR 256
58
59 void error_msg (const char *msg);
60 void info_msg (const char *msg);
61 void info_msg_sans_newline (const char *msg);
62
63 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname, bool play);
64
65 class WindaqFile
66 {
67 public:
68   WindaqFile (const char* fname);
69   ~WindaqFile ();
70   bool ReadHeader();
71
72   bool m_valid;
73   std::string m_error;
74   int m_fd;
75   int m_format;
76   int m_nMaxChannels;
77   int m_nChannels;
78   unsigned long int m_nSamples;
79   double m_sample_rate;
80   std::string m_strFile;
81   unsigned int m_sr_denom, m_sr_numer;
82   unsigned short int m_nHeader_bytes, m_channel_offset, m_nBytes_channel_header;
83   unsigned int m_nData_bytes;
84   unsigned int m_time_acq_start;
85   unsigned int m_time_acq_stop;
86   double m_time_between_channel_samples;
87 };
88
89 class WindaqChannel 
90 {
91 public:
92   WindaqFile& r_wdq;
93   signed short int *m_data;
94   double m_slope;
95   double m_intercept;
96   unsigned int m_channel;
97   bool m_valid;
98   std::string m_units;
99   signed short int m_min_raw_data;
100   signed short int m_max_raw_data;
101   double m_max_scaled_data;
102   double m_min_scaled_data;
103
104   WindaqChannel (WindaqFile& wdq, const int channel);
105   ~WindaqChannel ();
106
107  private:
108   bool read_channel_data();
109 };
110
111
112 class WavFile 
113 {
114  public:
115   bool m_valid;
116   signed short int* m_data;
117   unsigned long int m_nSamples;
118   std::string m_strFile;
119   int m_fd;
120   double m_rate;
121   unsigned int m_nChannels;
122   unsigned int m_nBitsPerSample;
123   unsigned int m_nBytesPerSample;
124   unsigned long int m_nHeaderBytes;
125   long int m_nDataBytes;
126   long int m_nFileBytes;
127
128   WavFile (WindaqChannel& wdq_channel, const char* fname);
129   ~WavFile ();
130
131   bool WriteFile ();
132
133   bool Play();
134   
135  private:
136   bool fill_header();
137 };
138
139 template<class T>
140 inline T nearest (double x)
141 {
142   return (x > 0 ?
143           static_cast<T>(x+0.5) : static_cast<T>(x-0.5));
144 }