r4123: Auto commit for Debian build
[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: wdq2wav.h,v 1.15 2003/02/25 18:18:26 kevin Exp $
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 <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef WIN32
38 #include <io.h>
39 #include <getopt.h>
40 #else
41 #include <unistd.h>
42 #endif
43
44 #ifdef LINUX
45 #include <endian.h>
46 #if __BYTE_ORDER == __BIG_ENDIAN
47 #define WORDS_BIG_ENDIAN 1
48 #endif
49 #endif
50
51 extern const char* g_szIdStr;
52 extern bool g_quiet;
53 extern bool g_verbose;
54 extern bool g_debug;
55
56 #define MAX_INPUT_STR 256
57
58 void error_msg (const char *msg);
59 void info_msg (const char *msg);
60 void info_msg_sans_newline (const char *msg);
61
62 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname, bool play);
63
64 class WindaqFile
65 {
66 public:
67   WindaqFile (const char* fname);
68   ~WindaqFile ();
69   bool ReadHeader();
70
71   bool m_valid;
72   std::string m_error;
73   int m_fd;
74   int m_nChannels;
75   unsigned long int m_nSamples;
76   double m_sample_rate;
77   std::string m_strFile;
78   unsigned int m_sr_denom, m_sr_numer;
79   unsigned short int m_nHeader_bytes, m_channel_offset, m_nBytes_channel_header;
80   unsigned int m_nData_bytes;
81   unsigned int m_time_acq_start;
82   unsigned int m_time_acq_stop;
83 };
84
85 class WindaqChannel 
86 {
87 public:
88   WindaqFile& r_wdq;
89   signed short int *m_data;
90   double m_slope;
91   double m_intercept;
92   unsigned int m_channel;
93   bool m_valid;
94   std::string m_units;
95   signed short int m_min_raw_data;
96   signed short int m_max_raw_data;
97   double m_max_scaled_data;
98   double m_min_scaled_data;
99
100   WindaqChannel (WindaqFile& wdq, const int channel);
101   ~WindaqChannel ();
102
103  private:
104   bool read_channel_data();
105 };
106
107
108 class WavFile 
109 {
110  public:
111   bool m_valid;
112   signed short int* m_data;
113   unsigned long int m_nSamples;
114   std::string m_strFile;
115   int m_fd;
116   double m_rate;
117   unsigned int m_nChannels;
118   unsigned int m_nBitsPerSample;
119   unsigned int m_nBytesPerSample;
120   unsigned long int m_nHeaderBytes;
121   long int m_nDataBytes;
122   long int m_nFileBytes;
123
124   WavFile (WindaqChannel& wdq_channel, const char* fname);
125   ~WavFile ();
126
127   bool WriteFile ();
128
129   bool Play();
130   
131  private:
132   bool fill_header();
133 };
134
135 template<class T>
136 inline T nearest (double x)
137 {
138   return (x > 0 ?
139           static_cast<T>(x+0.5) : static_cast<T>(x-0.5));
140 }