File: maplay.cc

package info (click to toggle)
maplay 1.2b-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 432 kB
  • ctags: 441
  • sloc: cpp: 5,421; sh: 97; makefile: 86
file content (637 lines) | stat: -rw-r--r-- 17,810 bytes parent folder | download | duplicates (2)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 *  @(#) maplay.cc 1.32, last edit: 10 Mar 1995 14:11:06
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *
 *  Many thanks to:
 *  -> Jim Boucher (jboucher@flash.bu.edu)
 *     for his idea and first implementation of 8 kHz u-law output
 *  -> Louis P. Kruger (lpkruger@phoenix.princeton.edu)
 *     for his implementation of the LinuxObuffer class
 *  -> John Brezak (brezak@apollo.hp.com)
 *     for his first HP implementation using the audio server
 *     (the current version does not use the audio server to keep
 *      ethernets on low traffic for playing DOOM :-)
 *  -> Pete French (pete@ohm.york.ac.uk)
 *     for his implementation of the NeXTObuffer class
 *  -> Nick Cremelie (cremelie@ibmsp.elis.rug.ac.be)
 *     for his AIX implementation
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *  Changes from version 1.2 to 1.3:
 *    - minor changes to create and use a HpuxObuffer object
 *    - write_buffer() of an Obuffer object is called only in stdout mode
 *      or if the Obuffer class does not use a signal handler for playback.
 *    - Pete French's NeXT implementation included
 *    - Nick Cremelie's AIX implementation included
 *
 *  Changes from version 1.1 to 1.2:
 *    - minor changes to create a LinuxObuffer object
 *    - minor changes for a u-law version, which creates 8 kHz u-law output
 *      on an amd device or in stdout mode, if compiled with ULAW defined
 *    - option -amd forces maplay to treat /dev/audio as an amd device
 *      in the u-law version. This is helpful on some SPARC clones.
 *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
 *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream.h>
#ifndef NeXT
#include <iomanip.h>
#endif
#include "all.h"
#include "crc.h"
#include "header.h"
#include "ibitstream.h"
#include "obuffer.h"
#include "subband.h"
#include "subband_layer_1.h"
#include "subband_layer_2.h"
#include "synthesis_filter.h"


// data extracted from commandline arguments:
static char *filename;
boil	    verbose_mode = False;		// read in obuffer.cc
static boil filter_check = False;
static boil stdout_mode = False;
static enum e_channels which_channels = both;
static boil use_speaker = False, use_headphone = False, use_line_out = False;
#if defined(SPARC) || defined(HPUX) || defined(AIX)
static float volume = -1.0;			// < 0 means: don't change volume
#endif
#if defined(SPARC) || defined(HPUX) || defined(LINUX)
boil	     wait_if_busy = False;
#endif
#ifdef ULAW
static boil force_amd = False;
#endif
static boil use_own_scalefactor = False;
static real scalefactor;
#ifdef NeXT
char *next_sound_host = NULL;
#endif

// data extracted from header of first frame:
static uint32 layer;
static e_mode mode;
static e_sample_frequency sample_frequency;

// objects needed for playing a file:
static Ibitstream *stream;
static Header *header;
static Crc16 *crc;
static Subband *subbands[32];
static SynthesisFilter *filter1 = NULL, *filter2 = NULL;
static Obuffer *buffer = NULL;


static void Exit (int returncode)
// delete some objects and exit
{
  delete buffer;	// delete on NULL-pointer is harmless
  delete filter1;
  delete filter2;
  delete header;
  delete stream;
  exit (returncode);
}


main (int argc, char *argv[])
{
  int i;
  boil read_ready = False, write_ready = False;

  if (argc < 2 || !strncmp (argv[1], "-h", 2))
  {
usage:
    cerr << "\nusage: " << argv[0]
	 << " [-v] [-s] [-l] [-r] "
#if defined(SPARC) || defined(HPUX)
	    "[-us] [-uh] "
#ifndef SunOS4_1_1
	    "[-ul] "
#endif
#endif
#if defined(SPARC) || defined(HPUX) || defined(AIX)
	    "[-vol volume] "
#endif
#ifdef NeXT
	    "[-H hostname] "
#endif
#if defined(SPARC) || defined(HPUX) || defined(LINUX)
	    "[-w] "
#endif
	    "filename\n"
	    "  filename     filename of a MPEG audio stream or - for stdin\n"
	    "  -v           verbose mode\n"
#ifdef ULAW
	    "  -s           write u-law samples at 8 kHz rate to stdout\n"
#else
	    "  -s           write pcm samples to stdout\n"
#endif
	    "  -l           decode only the left channel\n"
	    "  -r           decode only the right channel\n"
#if defined(SPARC) || defined(HPUX)
	    "  -us          send audio signal to speaker\n"
	    "  -uh          send audio signal to headphone\n"
#ifndef SunOS4_1_1
	    "  -ul          send audio signal to line out\n"
#endif
#endif
#if defined(SPARC) || defined(HPUX) || defined(AIX)
	    "  -vol float   set volume (0.0 = min, 1.0 = max)\n"
#endif
#ifdef ULAW
	    "  -amd         force maplay to treat /dev/audio as an amd device\n"
#endif
#ifdef NeXT
	    "  -H hostname  use sound device on this remote NeXT\n"
#endif
#if defined(SPARC) || defined(HPUX) || defined(LINUX)
	    "  -w           wait if audio device is busy\n"
#endif
	    "\n@(#) MPEG audio player maplay 1.3 "
#ifdef ULAW
	    "(8 kHz u-law "
#else
	    "("
#endif
#ifdef Indigo
	    "SGI IRIX"
#else
#ifdef SunOS4_1_1
	    "SPARC SunOS 4.1.1"
#else
#ifdef SunOS4_1_3
	    "SPARC SunOS 4.1.3"
#else
#ifdef Solaris
	    "SPARC Solaris 2.x"
#else
#ifdef LINUX
	    "Linux"
#else
#ifdef NeXT
	    "NeXTStep 3.2"
#else
#ifdef OSF1
	    "RISC OSF1"
#else
#ifdef ULTRIX
	    "RISC ULTRIX 4.x"
#else
#ifdef HPUX
	    "HP-UX"
#else
#ifdef AIX
	    "AIX"
#else
	    "unknown"
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#endif
	    " version)\n"
	    "@(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)\n"
	    "@(#) Berlin University of Technology\n"
	    "@(#) Created: 24 Mar 1995 11:20:54\n"
	    "@(#) This program is free software. See the GNU General Public License\n"
	    "@(#) in the file COPYING for more details.\n\n";
    exit (0);
  }

  // parse arguments:
  for (i = 1; i < argc; ++i)
    if (argv[i][0] == '-' && argv[i][1])
      switch (argv[i][1])
      {
	case 'v':
#if defined(SPARC) || defined(HPUX) || defined(AIX)
	  if (argv[i][2] == 'o')
	  {
	    if (++i == argc)
	    {
	      cerr << "Please specify a volume, e.g -vol 0.8\n";
	      exit (1);
	    }
	    sscanf (argv[i], "%f", &volume);
	  }
	  else
	    verbose_mode = True;
#else
	  verbose_mode = True;
#endif
	  break;
	case 's':
	  stdout_mode = True;
	  break;
	case 'l':
	  which_channels = left;
	  break;
	case 'r':
	  which_channels = right;
	  break;
#if defined(SPARC) || defined(HPUX)
	case 'u':
	  switch (argv[i][2])
	  {
	    case 's':
	      use_speaker = True;
	      break;
	    case 'h':
	      use_headphone = True;
	      break;
#ifndef SunOS4_1_1
	    case 'l':
	      use_line_out = True;
	      break;
#endif
	    default:
	      goto usage;
	  }
	  break;
#endif
#ifdef ULAW
	case 'a':
	  force_amd = True;
	  break;
#endif
#ifdef NeXT
	case 'H':
	  if (++i == argc)
	  {
	    cerr << "please specify a hostname after the -H option!\n";
	    exit (1);
	  }
	  next_sound_host = argv[i];
	  break;
#endif
#if defined(SPARC) || defined(HPUX) || defined(LINUX)
	case 'w':
	  wait_if_busy = True;
	  break;
#endif
	case 'c':
	  filter_check = True;
	  break;
	case 'f':
	  if (++i == argc)
	  {
	    cerr << "please specify a new scalefactor after the -f option!\n";
	    exit (1);
	  }
	  use_own_scalefactor = True;
	  sscanf (argv[i], "%f", &scalefactor);
	  break;
	default:
	  goto usage;
      }
    else if (!filename)
      filename = argv[i];
    else
      goto usage;

  if (!filename)
    goto usage;

  if (!strcmp (filename, "-"))
    stream = new Ibitstream (0);		// read from stdin
  else
    stream = new Ibitstream (filename);		// read from file

  header = new Header;
  if (!header->read_header (stream, &crc))
  {
    cerr << "no header found!\n";
    Exit (1);
  }

  // get info from header of first frame:
  layer = header->layer ();
  if ((mode = header->mode ()) == single_channel)
    which_channels = left;
  sample_frequency = header->sample_frequency ();

  // create filter(s):
#ifdef ULAW
  if (use_own_scalefactor)
    filter1 = new SynthesisFilter (0, sample_frequency, scalefactor);
  else
    filter1 = new SynthesisFilter (0, sample_frequency);
  if (mode != single_channel && which_channels == both)
    if (use_own_scalefactor)
      filter2 = new SynthesisFilter (1, sample_frequency, scalefactor);
    else
      filter2 = new SynthesisFilter (1, sample_frequency);
#else
  if (use_own_scalefactor)
    filter1 = new SynthesisFilter (0, scalefactor);
  else
    filter1 = new SynthesisFilter (0);
  if (mode != single_channel && which_channels == both)
    if (use_own_scalefactor)
      filter2 = new SynthesisFilter (1, scalefactor);
    else
      filter2 = new SynthesisFilter (1);
#endif	// !ULAW

  // create buffer:
  if (stdout_mode)
    if (mode == single_channel || which_channels != both)
      buffer = new FileObuffer (1);
    else
      buffer = new FileObuffer (2);
  else
#ifdef Indigo
  {
    if (mode == single_channel || which_channels != both)
      buffer = new IndigoObuffer (1, header);
    else
      buffer = new IndigoObuffer (2, header);
  }
#else
#ifdef SPARC
  {
#ifdef ULAW
    if (SparcObuffer::class_suitable ((mode == single_channel || which_channels != both) ? 1 : 2,
				      force_amd))	// amd device available?
      buffer = new SparcObuffer (header, use_speaker, use_headphone, use_line_out, volume);
#else
    if (SparcObuffer::class_suitable ())		// dbri device available?
      if (mode == single_channel || which_channels != both)
	buffer = new SparcObuffer (1, header, use_speaker, use_headphone, use_line_out, volume);
      else
	buffer = new SparcObuffer (2, header, use_speaker, use_headphone, use_line_out, volume);
#endif
    else
      Exit (0);
  }
#else
#ifdef LINUX
  {
    if (LinuxObuffer::class_suitable (mode == single_channel || which_channels != both) ? 1 : 2)
      if (mode == single_channel || which_channels != both)
	buffer = new LinuxObuffer (1, header);
      else
	buffer = new LinuxObuffer (2, header);
    else
      Exit (0);
  }
#else
#ifdef HPUX
  {
    if (HpuxObuffer::class_suitable ())
      if (mode == single_channel || which_channels != both)
	buffer = new HpuxObuffer (1, header, use_speaker, use_headphone, use_line_out, volume);
      else
	buffer = new HpuxObuffer (2, header, use_speaker, use_headphone, use_line_out, volume);
  }
#else
#ifdef NeXT
  {
    if (mode == single_channel || which_channels != both)
      buffer = new NeXTObuffer (1, header);
    else
      buffer = new NeXTObuffer (2, header);
  }
#else
#ifdef AIX
  {
    if (AixObuffer::class_suitable (mode == single_channel || which_channels != both) ? 1 : 2)
      if (mode == single_channel || which_channels != both)
        buffer = new AixObuffer (1, header, volume);
      else
        buffer = new AixObuffer (2, header, volume);
    else
      Exit (0);
  }
#else
//#ifdef your_machine
//  {
//    if (mode == single_channel || which_channels != both)
//      buffer = new your_Obuffer (your_parameters);	// mono
//    else
//      buffer = new your_Obuffer (your_parameters);	// stereo
//  }
//#else
  {
    cerr << "Sorry, I don't know your audio device.\n"
	    "Please use the stdout mode.\n";
    Exit (0);
  }
//#endif	// !your_machine
#endif	// !AIX
#endif	// !NeXT
#endif	// !HPUX
#endif	// !LINUX
#endif	// !SPARC
#endif	// !Indigo

  if (verbose_mode)
  {
    // print informations about the stream
    char *name = strrchr (filename, '/');
    if (name)
      ++name;
    else
      name = filename;
    cerr << name << " is a layer " << header->layer_string () << ' '
	 << header->mode_string () << " MPEG audio stream with";
    if (!header->checksums ())
      cerr << "out";
    cerr << " checksums.\nThe sample frequency is "
	 << header->sample_frequency_string () << " at a bitrate of "
	 << header->bitrate_string () << ".\n"
	    "This stream is ";
    if (header->original ())
      cerr << "an original";
    else
      cerr << "a copy";
    cerr << " and is ";
    if (!header->copyright ())
      cerr << "not ";
    cerr << "copyright protected.\n";
  }

  do
  {
    // is there a change in important parameters?
    // (bitrate switching is allowed)
    if (header->layer () != layer)
    {
      // layer switching is allowed
      if (verbose_mode)
	cerr << "switching to layer " << header->layer_string () << ".\n";
      layer = header->layer ();
    }
    if ((mode == single_channel && header->mode () != single_channel) ||
	(mode != single_channel && header->mode () == single_channel))
    {
      // switching from single channel to stereo or vice versa is not allowed
      cerr << "illegal switch from single channel to stereo or vice versa!\n";
      Exit (1);
    }
    if (header->sample_frequency () != sample_frequency)
    {
      // switching the sample frequency is not allowed
      cerr << "sorry, can't switch the sample frequency in the middle of the stream!\n";
      Exit (1);
    }

    // create subband objects:
    if (header->layer () == 1)
    {
      if (header->mode () == single_channel)
	for (i = 0; i < header->number_of_subbands (); ++i)
	  subbands[i] = new SubbandLayer1 (i);
      else if (header->mode () == joint_stereo)
      {
	for (i = 0; i < header->intensity_stereo_bound (); ++i)
	  subbands[i] = new SubbandLayer1Stereo (i);
	for (; i < header->number_of_subbands (); ++i)
	  subbands[i] = new SubbandLayer1IntensityStereo (i);
      }
      else
	for (i = 0; i < header->number_of_subbands (); ++i)
	  subbands[i] = new SubbandLayer1Stereo (i);
    }
    else if (header->layer () == 2)
    {
      if (header->mode () == single_channel)
	for (i = 0; i < header->number_of_subbands (); ++i)
	  subbands[i] = new SubbandLayer2 (i);
      else if (header->mode () == joint_stereo)
      {
	for (i = 0; i < header->intensity_stereo_bound (); ++i)
	  subbands[i] = new SubbandLayer2Stereo (i);
	for (; i < header->number_of_subbands (); ++i)
	  subbands[i] = new SubbandLayer2IntensityStereo (i);
      }
      else
	for (i = 0; i < header->number_of_subbands (); ++i)
	  subbands[i] = new SubbandLayer2Stereo (i);
    }
    else
    {
      cerr << "sorry, layer 3 not implemented!\n";
      Exit (0);
    }

    // start to read audio data:
    for (i = 0; i < header->number_of_subbands (); ++i)
      subbands[i]->read_allocation (stream, header, crc);

    if (header->layer () == 2)
      for (i = 0; i < header->number_of_subbands (); ++i)
	((SubbandLayer2 *)subbands[i])->read_scalefactor_selection (stream, crc);

    if (!crc || header->checksum_ok ())
    {
      // no checksums or checksum ok, continue reading from stream:
      for (i = 0; i < header->number_of_subbands (); ++i)
	subbands[i]->read_scalefactor (stream, header);

      do
      {
	for (i = 0; i < header->number_of_subbands (); ++i)
	  read_ready = subbands[i]->read_sampledata (stream);

	do
	{
	  for (i = 0; i < header->number_of_subbands (); ++i)
	    write_ready = subbands[i]->put_next_sample (which_channels, filter1, filter2);

	  filter1->calculate_pcm_samples (buffer);
	  if (which_channels == both && header->mode () != single_channel)
	    filter2->calculate_pcm_samples (buffer);
	}
	while (!write_ready);
      }
      while (!read_ready);

#if defined(SPARC) || defined(HPUX) || defined(NeXT)
      // no write_buffer() needed in playback mode,
      // because signal handlers do this job.
      if (stdout_mode)
	buffer->write_buffer (1);
#else
      buffer->write_buffer (1);
#endif
    }
    else
      // Sh*t! Wrong crc checksum in frame!
      cerr << "WARNING: frame contains wrong crc checksum! (throwing frame away)\n";

    for (i = 0; i < header->number_of_subbands (); ++i)
      delete subbands[i];
  }
  while (header->read_header (stream, &crc));

  uint32 range_violations = filter1->violations ();
  if (mode != single_channel && which_channels == both)
   range_violations += filter2->violations ();

  if (filter_check)
  {
    // check whether (one of) the filter(s) produced values not in [-1.0, 1.0]:
    if (range_violations)
    {
      cerr << range_violations << " range violations have occured!\n";
      if (stdout_mode)
	cerr << "If you notice these violations,\n";
      else
	cerr << "If you have noticed these violations,\n";
      cerr << "please use the -f option with the value ";
      if (mode != single_channel && which_channels == both &&
	  filter2->hardest_violation () > filter1->hardest_violation ())
	cerr << filter2->recommended_scalefactor ();
      else
	cerr << filter1->recommended_scalefactor ();
      cerr << "\nor a greater value up to 32768 and try again.\n";
    }
  }
  if (verbose_mode)
  {
    // print playtime of stream:
    real playtime = filter1->seconds_played (Header::frequency (sample_frequency));
    uint32 minutes = (uint32)(playtime / 60.0);
    uint32 seconds = (uint32)playtime - minutes * 60;
    uint32 centiseconds = (uint32)((playtime - (real)(minutes * 60) - (real)seconds) * 100.0);
    cerr << "end of stream, playtime: " << minutes << ':';
    cerr.width (2);
    cerr.fill ('0');
    cerr << seconds << '.';
    cerr.width (2);
    cerr.fill ('0');
    cerr << centiseconds << '\n';
  }

  Exit (0);
}