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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
/*
* @(#) maplay.cc 1.20, last edit: 6/22/94 12:32:55
* @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
* @(#) Berlin University of Technology
*
* Many thanks for ideas and implementations 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
*
* 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.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.
*/
/*
* Playlists added and other minor changes by Timo Jantunen (jeti@iki.fi).
*
* Added MPEG-1 Layer III and MPEG-2 routines from maplay 1.2+ 1.81 for
* Windows by Jeff Tsay.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream.h>
#include <iomanip.h>
#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"
//L3
//#include "bit_res.h"
//#include "scalefactors.h"
//#include "l3type.h"
#include "layer3.h"
#include <ctype.h>
#include <time.h>
// space for storing file names & comments
#define MAXFILES 1024
#define BIGBSIZE 80*MAXFILES
static char *fname[MAXFILES],*fcomment[MAXFILES],fnbuffer[BIGBSIZE];
static char *nullcomment="";
int numfiles=0;
// data extracted from commandline arguments:
static bool verbose_mode = False, quiet_mode = False, filter_check = False;
static bool stdout_mode = False, random_order = False;
static enum e_channels which_channels = both, which_channels_save=both;
static bool use_speaker = False, use_headphone = False, use_line_out = False;
#ifdef ULAW
static bool force_amd = False;
#endif
static bool use_own_scalefactor = False;
static real scalefactor;
// 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 Crc16 *crc;
SynthesisFilter *filter1 = NULL, *filter2 = NULL;
Obuffer *buffer = NULL;
Header *header = NULL;
Subband *subbands[32] = {NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL};
LayerIII_Decoder *l3decoder = NULL;
static void Exit (int returncode)
// delete some objects and exit
{
int i;
if ((returncode == 1) && buffer) {
if (header)
for (i = 0; i < header->number_of_subbands (); ++i) {
delete subbands[i];
subbands[i] = NULL;
}
}
delete buffer; // delete on NULL-pointers are harmless
delete filter1;
delete filter2;
delete header;
delete stream;
exit (returncode);
}
void addPlaylist(char *lname) {// add files (and comments) from a file
static int pos=0;
int escape=0;
FILE *fp;
char tmp[1024],*c,*c0;
fp=fopen(lname,"ra");
if(fp==NULL) {
fprintf(stderr,"Error opening playlist file '%s'!\n",lname);
exit(1);
}
while(!feof(fp)) {
tmp[0]=0;
fgets(tmp,1022,fp);
c=tmp;while(isspace(*c)) c++; // skip leading blanks
if(!strncmp("escape='",c,8) && c[9]=='\'') { // new escape character
escape=c[8];*c='#';
}
if(*c!='#' && *c!=';' && *c!=0) { // skip comments and empty lines
if(strlen(c)+pos>BIGBSIZE) {
fprintf(stderr,"Buffer overflow!\nDecrease number of files or size of comments.\n");
exit(1);
}
fname[numfiles]=&fnbuffer[pos]; // start new filename
while((!isspace(*c)&&!escape) || (escape&&*c!=escape))
fnbuffer[pos++]=*c++; // copy name
fnbuffer[pos++]=0;
c0=c+1;// start of comment (if it exists) one char after last of filename
while(isspace(*c)) c++; // skip blanks
if(*c) { // comment exists
c=c0; // skip only one space!
fcomment[numfiles]=&fnbuffer[pos]; // start new comment
while(*c&&*c!='\r'&&*c!='\n') fnbuffer[pos++]=*c++;// copy comment
fnbuffer[pos++]=0;
}
else
fcomment[numfiles]=nullcomment;
numfiles++;
if (numfiles==MAXFILES) {
fprintf(stderr,"Too many files! Maximum is %d\n",MAXFILES);
exit(0);
}
}
}
fclose(fp);
}
main (int argc, char *argv[])
{
Subband *subbands[32];
uint32 layer;
e_mode mode;
e_sample_frequency sample_frequency, old_freq;
bool stereo=False, old_stereo=True;
bool read_ready = False, write_ready = False;
int32 i;
int fnro;
if (argc < 2 || !strncmp (argv[1], "-h", 2))
{
usage:
cerr << "\nusage: " << argv[0]
<< " [option(s)] [filename(s)]\n"
" filename filename of a MPEG audio stream or - for stdin\n"
" -v verbose mode\n"
" -q quiet mode (don't print filenames, when playing multiple files)\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"
#ifdef SPARC
" -us send audio signal to speaker (default)\n"
" -uh send audio signal to headphone\n"
#ifndef SunOS4_1_1
" -ul send audio signal to line out\n"
#endif
#endif
#ifdef ULAW
" -amd force maplay to treat /dev/audio as an amd device\n"
#endif
" -c check for filter range violations\n"
" -f ushort use this scalefactor instead of the default value 32768\n"
" -@ list get files (and comments) from file 'list'\n"
" -a randomize list order\n\n"
"@(#) MPEG Audio Player maplay3 1.1 "
#ifdef ULAW
"(8 kHz u-law "
#else
"("
#endif
#ifdef Indigo
"Indigo 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 ULTRIX
"RISC ULTRIX 4.x"
#else
"unknown"
#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: 6/23/94 14:12:46\n"
"@(#) This program is free software. See the GNU General Public License\n"
"@(#) in the file COPYING for more details.\n"
"@(#) Layer III routines Copyright 1991 MPEG/audio software simulation group\n"
"@(#) and Jeff Tsay (ctsay@pasteur.eecs.berkeley.edu).\n"
"@(#) Playlist modifications and integration of Layer III routines\n"
"@(#) by Timo Jantunen (jeti@iki.fi).\n"
"@(#) Compiled " << __TIME__ << " " << __DATE__ << "\n";
exit (0);
}
// parse arguments:
for (i = 1; i < argc; ++i)
if (argv[i][0] == '-' && argv[i][1])
switch ((int)argv[i][1])
{
case 'v':
verbose_mode = True;
quiet_mode = False;
break;
case 'q':
quiet_mode = True;
verbose_mode = False;
break;
case 's':
stdout_mode = True;
break;
case 'l':
which_channels = left;
break;
case 'r':
which_channels = right;
break;
#ifdef SPARC
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
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;
case '@':
if (++i == (unsigned)argc)
{
fprintf(stderr,"please specify a filename after the -@ option!\n");
exit (1);
}
addPlaylist(argv[i]);
break;
case 'a':
random_order = True;
break;
default:
goto usage;
}
else if (numfiles==MAXFILES) {
fprintf(stderr,"Too many files! Maximum is %d\n",MAXFILES);
exit(0);
}
else {
fcomment[numfiles]=nullcomment;
fname[numfiles++]=argv[i];
}
if(numfiles==0)
goto usage;
if (!(use_speaker || use_headphone || use_line_out))
use_speaker = True;
if(!quiet_mode && numfiles>1)
fprintf(stderr,"Playing %d files...\n",numfiles);
// randomize order
if(numfiles>1 && random_order) {
int t1,t2,nro;char *t;
srand(time(NULL)*101);
for(fnro=0;fnro<numfiles*17;fnro++) {
t1=(rand()/13)%numfiles;t2=fnro%numfiles;
t= fname[t1]; fname[t1]= fname[t2]; fname[t2]=t;
t=fcomment[t1];fcomment[t1]=fcomment[t2];fcomment[t2]=t;
}
}
which_channels_save=which_channels; // needed to remember original settings
// if there is a mono stream in the middle of playlist...
// ******** BEGIN MULTIPLE FILE LOOP *******
// ugly bastard, but works... I think.
for(fnro=0;fnro<numfiles;fnro++) {
if (!strcmp (fname[fnro], "-"))
stream = new Ibitstream (0); // read from stdin
else
stream = new Ibitstream (fname[fnro]); // 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;
else
which_channels=which_channels_save;
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
// this mpeg stream has same sample frequency and number of channels as last one?
stereo=(mode == single_channel || which_channels != both)? False:True;
if(buffer!=NULL && (old_freq!=sample_frequency || old_stereo!=stereo)) {
delete buffer;
buffer=NULL;
//cerr << "deleted obuffer\n";
}
old_freq=sample_frequency;old_stereo=stereo;
// create buffer:
if(buffer==NULL) // if it don't already exists
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);
#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);
else
buffer = new SparcObuffer (2, header, use_speaker, use_headphone, use_line_out);
#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 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 // !LINUX
#endif // !SPARC
#endif // !Indigo
char *name = strrchr (fname[fnro], '/');
if (name)
++name;
else
name = fname[fnro];
if(!quiet_mode && numfiles>1) {
if(fcomment[fnro]!=nullcomment)
fprintf(stderr,"%s %s\n",name,fcomment[fnro]);
else
if(!verbose_mode) fprintf(stderr,"%s\n",name);
}
if (verbose_mode)
{
// print informations about the stream
cerr << name << " is a layer " << header->layer_string () << ' '
<< header->mode_string () << ' ' << header->version_string() << " 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";
}
// Layer III : initialize decoder
if (layer == 3)
l3decoder = new LayerIII_Decoder(stream, header, filter1, filter2, buffer, which_channels);
do
{
// is there a change in important parameters?
// (bitrate switching is allowed)
if (header->layer () != layer)
{
// layer switching is allowed
if (header->layer() == 3) {
l3decoder = new LayerIII_Decoder(stream, header, filter1, filter2, buffer, which_channels);
} else if (layer == 3) {
delete l3decoder;
l3decoder = NULL;
}
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);
}
if (header->layer() != 3) { // Layer III
// 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);
}
// 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);
buffer->write_buffer (1); // write to stdout
}
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];
subbands[i] = NULL;
}
} else { // Layer III
l3decoder->decode();
}
}
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 ());
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';
}
//delete buffer;
//buffer = NULL;
delete filter1;
filter1 = NULL;
delete filter2;
filter2 = NULL;
delete l3decoder;
l3decoder = NULL;
delete stream;
stream=NULL;
delete header;
header=NULL;
} // for fnro (multifile loop)
delete buffer;
return 0;
}
|