File: qwavheaderdump.cc

package info (click to toggle)
quelcom 0.4.0-16
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 1,544 kB
  • sloc: cpp: 4,148; makefile: 158; sh: 15
file content (280 lines) | stat: -rw-r--r-- 7,739 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# include <getopt.h>	// getopt
# include <sys/types.h>	// u_int16_t, u_int32_t
# include <stdint.h>    // uintptr_t, ISO C99 types
# include <string.h>    // strncpy
# include "qfile.hh"
# include "qwavheader.hh"
# include "qexception.hh"
# include "endian.hh"

/* caved in and used C's printf in a few places that don't seem to
 * be expressible with ios
 */
#include <cstdio>
#include <iostream>

#ifdef NLS
  # include <locale.h>
  # include <libintl.h>
  # define _(s) gettext (s)
#else
  # define _(s) (s)
#endif

// see qwavheader.hh for more info about the wav header struct
struct header {

  char riff[4];
  u_int32_t rifflength;
  char wave[4];
     
  char fmt_[4];
  u_int32_t fmtlength;
  u_int16_t format;
  u_int16_t channels;
  u_int32_t samplerate;
  u_int32_t bytespersec;
  u_int16_t bytespersample;
  u_int16_t bitspersample;
    
  char data[4];
  u_int32_t datalength;
} __attribute__((packed));

const unsigned int HEADERSIZE = sizeof(struct header);


void usage () {

  fprintf(stderr,_(" %s: dump (and fix) wav header\n"),APPNAME);
  fprintf(stderr,_(" syntax: %s [option]... file...\n"),APPNAME);
  cerr << _("   -F, --fix: correct header. use with care\n");
  cerr << _("   -h, --help: show this help and exit\n");
  cerr << _("   -q, --quiet: no output messages\n");
  cerr << _("   -V, --version: show version and exit\n");
}


int main (int argc, char **argv) {

  int option;
  bool quiet=false, fix=false;

  static struct option long_options[] = {
    {"fix",no_argument,0,'F'},
    {"help",no_argument,0,'h'},
    {"quiet",no_argument,0,'q'},
    {"version",no_argument,0,'V'},
    {0,0,0,0}
  };

#ifdef NLS
  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);
#endif

 // un altre dia, per defecte llegir *.wav ...
  if (argc==1) {
    usage();
    return 1;
  }

 // supress getopt error message
  opterr = 0;	

  while ((option = getopt_long(argc, argv, "FhqV",long_options,0)) != EOF) 
    switch (option) {
      case 'F':
        fix=true;
      break;
      case 'h':
        usage();
        return 0;
      break;
      case 'q':
        quiet=true;
      break;
      case 'V':
        cerr << APPNAME << " - " << _("version") << ' ' << VERSION
	     << _("build") << ' ' << __DATE__ << '\n';
        return 0;
      break;      
      case '?':
      default: 
        cerr << APPNAME << ": " << _("option") << " '" << argv[optind-1]
	     << "' " << _("is not recognized or bad used") << '\n';
        usage();
        return 1;
    } 

  if (argc<=optind) {
   // haurem de fer *.wav ...
    cerr << APPNAME << _(": no input file(s)") << endl;
    usage();
    return 1;
  }

  u_int32_t openmode = fix?qfile::READWRITE:qfile::READ;

  while (argv[optind]) {

    try {

      qfile f(argv[optind],openmode);
      
      if (f.getSize()<HEADERSIZE) {
        cerr << APPNAME << ": '" << f.getName()
	     << _("' has not enough room for a wav header\n");
        optind++;
        continue;
      }

	  if ((uintptr_t)f.getMap() % sizeof(u_int32_t) != 0) {
		// should never happen with a mapping at offset 0, but if it does,
		// we need to catch it rather than tossing subtle memory misread
		// bugs.  32-bit alignment is adequate since no integer in a WAV header
		// is longer
		cerr << APPNAME << _(": mapping of header in '") << f.getName()
			 << _("' is not 32-bit aligned\n");
		optind++;
		continue;
	  }

      struct header *header = (struct header*) f.getMap();

      cout << f.getName() << " (" << f.getSize() << " bytes):" << endl;

     // the file should have an integer number of samples...

      printf("\triff: '%.4s'\n",header->riff);
      if (strncmp(header->riff,"RIFF",4)) {
        if (!quiet) 
          fprintf(stderr,_("\t\triff field should be 'RIFF'\n"));
        if (fix) {
          strncpy(header->riff,"RIFF",4);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }
      
      printf(_("\triff length: %d\n"),letohl(header->rifflength));
      if (letohl(header->rifflength)!=f.getSize()-8) {
        if (!quiet)
          fprintf(stderr,_("\t\triff length field should be %d\n"),f.getSize()-8);
        if (fix) {
          header->rifflength = htolel(f.getSize()-8);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }

      printf("\twave: '%.4s'\n",header->wave);
      if (strncmp(header->wave,"WAVE",4)) {
        if (!quiet) 
          fprintf(stderr,_("\t\twave field should be 'WAVE'\n"));
        if (fix) {
          strncpy(header->wave,"WAVE",4);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }
      
      printf("\tfmt: '%.4s'\n",header->fmt_);
      if (strncmp(header->fmt_,"fmt ",4)) {
        if (!quiet) 
          fprintf(stderr,_("\t\tfmt  field should be 'fmt '\n"));
        if (fix) {
          strncpy(header->fmt_,"fmt ",4);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }

      printf(_("\tfmt length: %d\n"),letohl(header->fmtlength));
      if (letohl(header->fmtlength)!=16) {
        if (!quiet)
          fprintf(stderr,_("\t\tfmt length field should be %d\n"),16);
        if (fix) {
          header->fmtlength = htolel(16);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }

      printf("\tformat: %d\n",letohs(header->format));
      if (letohs(header->format)!=1) {
        if (!quiet)
          fprintf(stderr,_("\t\tformat field should 1 (pcm tag)\n"));
        if (fix) {
          header->format = htoles(1);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }

      printf(_("\tchannels: %d\n"),letohs(header->channels));
      if (letohs(header->channels)!=2 &&
		  letohs(header->channels)!=1) {
        if (!quiet)
          fprintf(stderr,_("\t\tchannels field should be 1 (mono) or 2 (stereo)\n"));
        if (fix) {
          cerr << _("\t\tdon't know which value must be set...\n") << endl;
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }

      printf(_("\tsample rate: %d\n"),letohl(header->samplerate));
      if (letohl(header->samplerate)>48000 ||
		  letohl(header->samplerate)<8000) {
        if (!quiet)
          fprintf(stderr,_("\t\tsample rate field should be between 8000 and 48000\n"));
        if (fix)
          cerr << _("\t\tdon't know which value must be set...\n") << endl;
      }

      printf(_("\tbytes/second: %d\n"),letohl(header->bytespersec));


      printf(_("\tbytes/sample: %d\n"),letohs(header->bytespersample));
      if (letohs(header->bytespersample)!=1 &&
          letohs(header->bytespersample)!=2 &&
          letohs(header->bytespersample)!=4) {
        if (!quiet)
          fprintf(stderr,_("\t\t bytes/sample field should be 1, 2 or 4\n"));
        if (fix)
          cerr << _("\t\tdon't know which value must be set...\n") << endl;
      }

      printf(_("\tbits/sample: %d\n"),letohs(header->bitspersample));

      printf("\tdata: '%.4s'\n",header->data);
      if (strncmp(header->data,"data",4)) {
        if (!quiet) 
          fprintf(stderr,_("\t\tdata field should be 'data'\n"));
        if (fix) {
          strncpy(header->data,"data",4);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }

      printf(_("\tdata length: %d\n"),letohl(header->datalength));
      if (letohl(header->datalength)!=f.getSize()-44) {
        if (!quiet)
          fprintf(stderr,_("\t\tdata length field should be %d\n"),f.getSize()-44);
        if (fix) {
          header->datalength = htolel(f.getSize()-44);
          if (!quiet)
            cerr << _("\t\tfixed\n");
        }
      }
    }
    catch (qexception e) {
      cerr << argv[optind] << ": " << e << endl;
    }

    optind++;
  }
}