| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static char *jukebox_version = "JUKEBOX 1.3"; |
| 24 |
|
| 25 |
#include <rplay.h> |
| 26 |
#include <conf.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <strings.h> |
| 29 |
#include <dirent.h> |
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/param.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <sys/time.h> |
| 34 |
#include <signal.h> |
| 35 |
|
| 36 |
extern char *optarg; |
| 37 |
extern int optind; |
| 38 |
|
| 39 |
|
| 40 |
typedef struct |
| 41 |
{ |
| 42 |
int idx; |
| 43 |
char *name; |
| 44 |
int length; |
| 45 |
} song; |
| 46 |
|
| 47 |
|
| 48 |
song *songs; |
| 49 |
int nsongs = 0; |
| 50 |
char *progname; |
| 51 |
int stop_song = 0; |
| 52 |
unsigned long total_time = 0; |
| 53 |
int opt_quiet; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
usage() |
| 62 |
{ |
| 63 |
printf("\n%s - Copyright (c) 1993 by Raphael Quinet ([email protected])\n\n", jukebox_version); |
| 64 |
printf("usage: %s [options] [directory ...]\n", progname); |
| 65 |
printf("\t-a\tplay songs in alphabetical order (file names)\n"); |
| 66 |
printf("\t-f name\tload the list of songs from a file\n"); |
| 67 |
printf("\t-h host\tuse this instead of the default host (%s)\n", |
| 68 |
rplay_default_host()); |
| 69 |
printf("\t-l\tloop mode - no exit\n"); |
| 70 |
printf("\t-q\tquiet - only error messages\n"); |
| 71 |
printf("\t-r\tplay songs in a random order\n"); |
| 72 |
printf("\t-s n\tsleep n milliseconds between songs (n may be negative)\n"); |
| 73 |
printf("\t-v n\tvolume n (%d <= n <= %d), default = %d\n", |
| 74 |
RPLAY_MIN_VOLUME, RPLAY_MAX_VOLUME, RPLAY_DEFAULT_VOLUME); |
| 75 |
printf("No options will play all songs (*.au) from the current\n"); |
| 76 |
printf("directory at the default volume\n\n"); |
| 77 |
exit(1); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#ifdef __STDC__ |
| 88 |
set_timer(int msec) |
| 89 |
#else |
| 90 |
set_timer(msec) |
| 91 |
int msec; |
| 92 |
#endif |
| 93 |
{ |
| 94 |
struct itimerval it; |
| 95 |
|
| 96 |
it.it_value.tv_sec = msec / 1000; |
| 97 |
it.it_value.tv_usec = (msec % 1000) * 1000; |
| 98 |
it.it_interval.tv_sec = 0; |
| 99 |
it.it_interval.tv_usec = 0; |
| 100 |
setitimer(ITIMER_REAL, &it, (struct itimerval *)0); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
catch_sigtstp() |
| 110 |
{ |
| 111 |
signal(SIGTSTP, catch_sigtstp); |
| 112 |
stop_song = 2; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
catch_sigint() |
| 122 |
{ |
| 123 |
signal(SIGINT, catch_sigint); |
| 124 |
stop_song = 1; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
catch_sigalrm() |
| 134 |
{ |
| 135 |
signal(SIGALRM, catch_sigalrm); |
| 136 |
stop_song = 0; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
#ifdef __STDC__ |
| 147 |
readsongs(char *dirname) |
| 148 |
#else |
| 149 |
readsongs(dirname) |
| 150 |
char *dirname; |
| 151 |
#endif |
| 152 |
{ |
| 153 |
DIR *dirp; |
| 154 |
struct dirent *dp; |
| 155 |
struct stat statbuf; |
| 156 |
char filename[MAXPATHLEN]; |
| 157 |
|
| 158 |
strcpy(filename, dirname); |
| 159 |
strcat(filename, "/"); |
| 160 |
|
| 161 |
dirp = opendir(dirname); |
| 162 |
if (dirp == NULL) |
| 163 |
{ |
| 164 |
fprintf(stderr, "%s: can't open directory %s\n", progname, dirname); |
| 165 |
exit(1); |
| 166 |
} |
| 167 |
|
| 168 |
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) |
| 169 |
if ((strlen(dp->d_name) > 3) && |
| 170 |
!strcmp(dp->d_name + strlen(dp->d_name) - 3, ".au")) |
| 171 |
{ |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
strcat(filename, dp->d_name); |
| 177 |
if (stat(filename, &statbuf) == -1) |
| 178 |
{ |
| 179 |
if (!opt_quiet) |
| 180 |
fprintf(stderr, "%s warning: can't stat %s\n", |
| 181 |
progname, filename); |
| 182 |
continue; |
| 183 |
} |
| 184 |
filename[strlen(filename) - dp->d_namlen] = '\0'; |
| 185 |
|
| 186 |
if (nsongs == 0) |
| 187 |
songs = (song *)malloc(sizeof(song) * ++nsongs); |
| 188 |
else |
| 189 |
songs = (song *)realloc(songs, sizeof(song) * ++nsongs); |
| 190 |
if (songs == NULL) |
| 191 |
{ |
| 192 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 193 |
exit(1); |
| 194 |
} |
| 195 |
|
| 196 |
songs[nsongs - 1].name = (char *)malloc(dp->d_namlen + 1); |
| 197 |
if (songs[nsongs - 1].name == NULL) |
| 198 |
{ |
| 199 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 200 |
exit(1); |
| 201 |
} |
| 202 |
|
| 203 |
strcpy(songs[nsongs - 1].name, dp->d_name); |
| 204 |
songs[nsongs - 1].length = statbuf.st_size / 8 - 10; |
| 205 |
total_time += songs[nsongs - 1].length; |
| 206 |
} |
| 207 |
closedir (dirp); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#ifdef __STDC__ |
| 217 |
readlist(char *listname) |
| 218 |
#else |
| 219 |
readlist(listname) |
| 220 |
char *listname; |
| 221 |
#endif |
| 222 |
{ |
| 223 |
FILE *fp; |
| 224 |
struct stat statbuf; |
| 225 |
char buf[MAXPATHLEN], filename[MAXPATHLEN], *s; |
| 226 |
|
| 227 |
fp = fopen(listname, "r"); |
| 228 |
if (fp == NULL) |
| 229 |
{ |
| 230 |
fprintf(stderr, "%s: cannot open %s\n", progname, listname); |
| 231 |
exit(1); |
| 232 |
} |
| 233 |
|
| 234 |
while (fgets(buf, sizeof(buf), fp) != NULL) |
| 235 |
{ |
| 236 |
switch (buf[0]) |
| 237 |
{ |
| 238 |
case '#': |
| 239 |
case ' ': |
| 240 |
case '\t': |
| 241 |
case '\n': |
| 242 |
continue; |
| 243 |
} |
| 244 |
|
| 245 |
sscanf(buf, "%[^\n]", filename); |
| 246 |
if (stat(filename, &statbuf) == -1) |
| 247 |
{ |
| 248 |
if (!opt_quiet) |
| 249 |
fprintf(stderr, "%s warning: can't stat %s\n", |
| 250 |
progname, filename); |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
if (nsongs == 0) |
| 255 |
songs = (song *)malloc(sizeof(song) * ++nsongs); |
| 256 |
else |
| 257 |
songs = (song *)realloc(songs, sizeof(song) * ++nsongs); |
| 258 |
if (songs == NULL) |
| 259 |
{ |
| 260 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 261 |
exit(1); |
| 262 |
} |
| 263 |
|
| 264 |
if (s = (char *)strrchr(filename, '/')) |
| 265 |
s++; |
| 266 |
else |
| 267 |
s = filename; |
| 268 |
|
| 269 |
songs[nsongs - 1].name = (char *)malloc(strlen(s) + 1); |
| 270 |
if (songs[nsongs - 1].name == NULL) |
| 271 |
{ |
| 272 |
fprintf(stderr, "%s: out of memory\n", progname); |
| 273 |
exit(1); |
| 274 |
} |
| 275 |
|
| 276 |
strcpy(songs[nsongs - 1].name, s); |
| 277 |
songs[nsongs - 1].length = statbuf.st_size / 8 - 10; |
| 278 |
total_time += songs[nsongs - 1].length; |
| 279 |
} |
| 280 |
fclose(fp); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#ifdef __STDC__ |
| 290 |
idxcompare(song *i, song *j) |
| 291 |
#else |
| 292 |
idxcompare(i,j) |
| 293 |
song *i, *j; |
| 294 |
#endif |
| 295 |
{ |
| 296 |
return (i->idx - j->idx); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
#ifdef __STDC__ |
| 306 |
alphacompare(song *i, song *j) |
| 307 |
#else |
| 308 |
alphacompare(i,j) |
| 309 |
song *i, *j; |
| 310 |
#endif |
| 311 |
{ |
| 312 |
return strcmp(i->name, j->name); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#ifdef __STDC__ |
| 323 |
main(int argc, char **argv) |
| 324 |
#else |
| 325 |
main(argc, argv) |
| 326 |
int argc; |
| 327 |
char **argv; |
| 328 |
#endif |
| 329 |
{ |
| 330 |
int rplay_fd, c, i; |
| 331 |
int opt_volume, opt_sleep, opt_loop, opt_random, opt_sort; |
| 332 |
char *host; |
| 333 |
RPLAY *rpp, *rps; |
| 334 |
|
| 335 |
if (progname = (char *)strrchr(*argv, '/')) |
| 336 |
progname++; |
| 337 |
else |
| 338 |
progname = *argv; |
| 339 |
|
| 340 |
host = rplay_default_host(); |
| 341 |
|
| 342 |
opt_loop = 0; |
| 343 |
opt_quiet = 0; |
| 344 |
opt_random = 0; |
| 345 |
opt_sort = 0; |
| 346 |
opt_sleep = 0; |
| 347 |
opt_volume = RPLAY_DEFAULT_VOLUME; |
| 348 |
|
| 349 |
while ((c = getopt(argc, argv, "af:h:lqrs:v:")) != -1) |
| 350 |
{ |
| 351 |
switch(c) |
| 352 |
{ |
| 353 |
case 'a': |
| 354 |
if (opt_random && !opt_quiet) |
| 355 |
fprintf(stderr, "%s warning: option -a overrides previous option -r\n", progname); |
| 356 |
opt_sort = 1; |
| 357 |
break; |
| 358 |
|
| 359 |
case 'f': |
| 360 |
readlist(optarg); |
| 361 |
break; |
| 362 |
|
| 363 |
case 'h': |
| 364 |
host = optarg; |
| 365 |
if (host[0] == '-') |
| 366 |
usage(); |
| 367 |
break; |
| 368 |
|
| 369 |
case 'l': |
| 370 |
opt_loop = 1; |
| 371 |
break; |
| 372 |
|
| 373 |
case 'q': |
| 374 |
opt_quiet = 1; |
| 375 |
break; |
| 376 |
|
| 377 |
case 'r': |
| 378 |
if (opt_sort && !opt_quiet) |
| 379 |
fprintf(stderr, "%s warning: option -r overrides previous option -a\n", progname); |
| 380 |
opt_random = 1; |
| 381 |
srandom(getpid()); |
| 382 |
break; |
| 383 |
|
| 384 |
case 's': |
| 385 |
opt_sleep = atoi(optarg); |
| 386 |
break; |
| 387 |
|
| 388 |
case 'v': |
| 389 |
opt_volume = atoi(optarg); |
| 390 |
|
| 391 |
default: |
| 392 |
usage(); |
| 393 |
exit(1); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (argc == optind) |
| 398 |
readsongs("."); |
| 399 |
else |
| 400 |
while (optind < argc) |
| 401 |
readsongs(argv[optind++]); |
| 402 |
|
| 403 |
if (nsongs == 0) |
| 404 |
{ |
| 405 |
fprintf(stderr, "%s: no songs (*.au) to play\n", progname); |
| 406 |
exit(1); |
| 407 |
} |
| 408 |
|
| 409 |
rplay_fd = rplay_open(host); |
| 410 |
if (rplay_fd < 0) |
| 411 |
{ |
| 412 |
rplay_perror(host); |
| 413 |
exit(1); |
| 414 |
} |
| 415 |
|
| 416 |
rpp = rplay_create(RPLAY_PLAY); |
| 417 |
if (rpp == NULL) |
| 418 |
{ |
| 419 |
rplay_perror("rplay_create"); |
| 420 |
exit(1); |
| 421 |
} |
| 422 |
|
| 423 |
c = rplay_set(rpp, RPLAY_APPEND, |
| 424 |
RPLAY_SOUND, "", |
| 425 |
RPLAY_VOLUME, opt_volume, |
| 426 |
NULL); |
| 427 |
if (c < 0) |
| 428 |
{ |
| 429 |
rplay_perror("rplay_set"); |
| 430 |
exit(1); |
| 431 |
} |
| 432 |
|
| 433 |
rps = rplay_create(RPLAY_STOP); |
| 434 |
if (rps == NULL) |
| 435 |
{ |
| 436 |
rplay_perror("rplay_create"); |
| 437 |
exit(1); |
| 438 |
} |
| 439 |
|
| 440 |
c = rplay_set(rps, RPLAY_APPEND, |
| 441 |
RPLAY_SOUND, "", |
| 442 |
NULL); |
| 443 |
if (c < 0) |
| 444 |
{ |
| 445 |
rplay_perror("rplay_set"); |
| 446 |
exit(1); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
signal(SIGINT, catch_sigint); |
| 454 |
signal(SIGTSTP, catch_sigtstp); |
| 455 |
signal(SIGALRM, catch_sigalrm); |
| 456 |
|
| 457 |
if (!opt_quiet) |
| 458 |
{ |
| 459 |
printf("\n%s - Welcome !\n\n", jukebox_version); |
| 460 |
printf("I will play %d songs just for you.\n", nsongs); |
| 461 |
if (opt_sleep) |
| 462 |
printf("(Estimated total time : %lu%+ld seconds).\n\n", |
| 463 |
total_time / 1000L, ((long)opt_sleep * (long)nsongs) / 1000L); |
| 464 |
else |
| 465 |
printf("(Estimated total time : %lu seconds).\n\n", |
| 466 |
total_time / 1000L); |
| 467 |
} |
| 468 |
|
| 469 |
if (opt_sort) |
| 470 |
qsort((char *)songs, nsongs, sizeof(song), alphacompare); |
| 471 |
|
| 472 |
do |
| 473 |
{ |
| 474 |
|
| 475 |
if (opt_random) |
| 476 |
{ |
| 477 |
if (!opt_quiet && opt_loop) |
| 478 |
printf("Building random list ...\n\n"); |
| 479 |
for (i = 0; i < nsongs; i++) |
| 480 |
songs[i].idx = (int)random(); |
| 481 |
qsort((char *)songs, nsongs, sizeof(song), idxcompare); |
| 482 |
} |
| 483 |
|
| 484 |
for (i = 0; i < nsongs; i++) |
| 485 |
{ |
| 486 |
if (!opt_quiet) |
| 487 |
{ |
| 488 |
if (opt_sleep) |
| 489 |
printf("Playing %s ... (%d%+d seconds)\n", songs[i].name, |
| 490 |
songs[i].length / 1000, opt_sleep / 1000); |
| 491 |
else |
| 492 |
printf("Playing %s ... (%d seconds)\n", songs[i].name, |
| 493 |
songs[i].length / 1000); |
| 494 |
} |
| 495 |
c = rplay_set(rpp, RPLAY_CHANGE, 0, |
| 496 |
RPLAY_SOUND, songs[i].name, |
| 497 |
NULL); |
| 498 |
if (c < 0) |
| 499 |
{ |
| 500 |
rplay_perror("rplay_set"); |
| 501 |
exit(1); |
| 502 |
} |
| 503 |
|
| 504 |
if (rplay(rplay_fd, rpp) < 0) |
| 505 |
{ |
| 506 |
rplay_perror(host); |
| 507 |
exit(1); |
| 508 |
} |
| 509 |
|
| 510 |
if (songs[i].length + opt_sleep > 100) |
| 511 |
set_timer(songs[i].length + opt_sleep); |
| 512 |
else |
| 513 |
set_timer(100); |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
pause(); |
| 520 |
|
| 521 |
if (stop_song != 0) |
| 522 |
{ |
| 523 |
if (!opt_quiet) |
| 524 |
printf(" Ouch !\n"); |
| 525 |
c = rplay_set(rps, RPLAY_CHANGE, 0, |
| 526 |
RPLAY_SOUND, songs[i].name, |
| 527 |
NULL); |
| 528 |
if (c < 0) |
| 529 |
{ |
| 530 |
rplay_perror("rplay_set"); |
| 531 |
exit(1); |
| 532 |
} |
| 533 |
|
| 534 |
if (rplay(rplay_fd, rps) < 0) |
| 535 |
{ |
| 536 |
rplay_perror(host); |
| 537 |
exit(1); |
| 538 |
} |
| 539 |
|
| 540 |
if (stop_song == 2) |
| 541 |
{ |
| 542 |
signal(SIGTSTP, SIG_DFL); |
| 543 |
kill(getpid(), SIGTSTP); |
| 544 |
if (!opt_quiet) |
| 545 |
printf("\nI still have %d songs to play.\n", |
| 546 |
nsongs - i - 1); |
| 547 |
signal(SIGTSTP, catch_sigtstp); |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
} while (opt_loop > 0); |
| 553 |
|
| 554 |
if (!opt_quiet) |
| 555 |
printf("\nPlease insert coins to play another song.\n"); |
| 556 |
|
| 557 |
rplay_close(rplay_fd); |
| 558 |
exit(0); |
| 559 |
} |
| 560 |
|