2000-12-13 11:19:54 +03:00
|
|
|
/* $NetBSD: play.c,v 1.18 2000/12/13 08:19:54 mrg Exp $ */
|
1999-03-26 18:46:22 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Matthew R. Green
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1999-03-26 17:02:39 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/audioio.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <paths.h>
|
|
|
|
|
|
|
|
#include "libaudio.h"
|
|
|
|
|
|
|
|
int main __P((int, char *[]));
|
|
|
|
void usage __P((void));
|
1999-04-13 11:21:45 +04:00
|
|
|
void play_fd __P((int, char *));
|
1999-03-28 14:35:05 +04:00
|
|
|
ssize_t audioctl_write_fromhdr __P((void *, size_t, int));
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
audio_info_t info;
|
1999-03-28 14:35:05 +04:00
|
|
|
int volume;
|
|
|
|
int balance;
|
|
|
|
int port;
|
|
|
|
int fflag;
|
2000-02-27 11:20:01 +03:00
|
|
|
int qflag;
|
1999-03-28 14:35:05 +04:00
|
|
|
int sample_rate;
|
|
|
|
int encoding;
|
|
|
|
char *encoding_str;
|
|
|
|
int precision;
|
|
|
|
int channels;
|
|
|
|
|
1999-03-26 17:02:39 +03:00
|
|
|
char const *play_errstring = NULL;
|
1999-04-13 11:21:45 +04:00
|
|
|
size_t bufsize;
|
|
|
|
int audiofd, ctlfd;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
1999-04-13 11:21:45 +04:00
|
|
|
size_t len;
|
1999-03-26 17:02:39 +03:00
|
|
|
off_t filesize;
|
1999-04-13 11:21:45 +04:00
|
|
|
int ch;
|
1999-11-06 18:05:45 +03:00
|
|
|
int exitstatus = EXIT_SUCCESS;
|
1999-03-26 17:02:39 +03:00
|
|
|
int iflag = 0;
|
|
|
|
int verbose = 0;
|
|
|
|
char *device = 0;
|
|
|
|
char *ctldev = 0;
|
|
|
|
|
1999-03-28 14:35:05 +04:00
|
|
|
while ((ch = getopt(argc, argv, "b:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
|
1999-03-26 17:02:39 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'b':
|
|
|
|
decode_int(optarg, &balance);
|
|
|
|
if (balance < 0 || balance > 64)
|
1999-03-28 14:35:05 +04:00
|
|
|
errx(1, "balance must be between 0 and 64");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
1999-03-28 14:35:05 +04:00
|
|
|
case 'c':
|
|
|
|
decode_int(optarg, &channels);
|
|
|
|
if (channels < 0)
|
|
|
|
errx(1, "channels must be positive");
|
1999-05-17 06:03:36 +04:00
|
|
|
break;
|
1999-03-26 17:02:39 +03:00
|
|
|
case 'C':
|
|
|
|
ctldev = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
device = optarg;
|
|
|
|
break;
|
1999-03-28 14:35:05 +04:00
|
|
|
case 'e':
|
|
|
|
encoding_str = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
1999-03-26 17:02:39 +03:00
|
|
|
case 'i':
|
|
|
|
iflag++;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
qflag++;
|
|
|
|
break;
|
1999-03-28 14:35:05 +04:00
|
|
|
case 'P':
|
|
|
|
decode_int(optarg, &precision);
|
|
|
|
if (precision != 8 && precision != 16 &&
|
|
|
|
precision != 24 && precision != 32)
|
|
|
|
errx(1, "precision must be between 8, 16, 24 or 32");
|
|
|
|
break;
|
1999-03-26 17:02:39 +03:00
|
|
|
case 'p':
|
|
|
|
len = strlen(optarg);
|
|
|
|
|
|
|
|
if (strncmp(optarg, "speaker", len) == 0)
|
|
|
|
port |= AUDIO_SPEAKER;
|
|
|
|
else if (strncmp(optarg, "headphone", len) == 0)
|
|
|
|
port |= AUDIO_HEADPHONE;
|
|
|
|
else if (strncmp(optarg, "line", len) == 0)
|
|
|
|
port |= AUDIO_LINE_OUT;
|
|
|
|
else
|
|
|
|
errx(1,
|
|
|
|
"port must be `speaker', `headphone', or `line'");
|
|
|
|
break;
|
1999-03-28 14:35:05 +04:00
|
|
|
case 's':
|
|
|
|
decode_int(optarg, &sample_rate);
|
|
|
|
if (sample_rate < 0 || sample_rate > 48000 * 2) /* XXX */
|
|
|
|
errx(1, "sample rate must be between 0 and 96000\n");
|
|
|
|
break;
|
1999-03-26 17:02:39 +03:00
|
|
|
case 'V':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
volume = atoi(optarg);
|
|
|
|
if (volume < 0 || volume > 255)
|
|
|
|
errx(1, "volume must be between 0 and 255\n");
|
|
|
|
break;
|
|
|
|
/* case 'h': */
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1999-03-28 14:35:05 +04:00
|
|
|
if (encoding_str) {
|
|
|
|
encoding = audio_enc_to_val(encoding_str);
|
|
|
|
if (encoding == -1)
|
|
|
|
errx(1, "unknown encoding, bailing...");
|
|
|
|
}
|
|
|
|
|
1999-07-13 18:02:33 +04:00
|
|
|
if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
|
|
|
|
(device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
|
1999-03-26 17:02:39 +03:00
|
|
|
device = _PATH_AUDIO;
|
1999-07-13 18:02:33 +04:00
|
|
|
if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == NULL)
|
1999-03-26 17:02:39 +03:00
|
|
|
ctldev = _PATH_AUDIOCTL;
|
|
|
|
|
|
|
|
audiofd = open(device, O_WRONLY);
|
|
|
|
#ifdef _PATH_OAUDIO
|
1999-04-13 11:21:45 +04:00
|
|
|
/* Allow the non-unit device to be used. */
|
|
|
|
if (audiofd < 0 && device == _PATH_AUDIO) {
|
|
|
|
device = _PATH_OAUDIO;
|
|
|
|
ctldev = _PATH_OAUDIOCTL;
|
|
|
|
audiofd = open(device, O_WRONLY);
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (audiofd < 0)
|
|
|
|
err(1, "failed to open %s", device);
|
|
|
|
ctlfd = open(ctldev, O_RDWR);
|
|
|
|
if (ctlfd < 0)
|
|
|
|
err(1, "failed to open %s", ctldev);
|
|
|
|
|
|
|
|
if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
|
|
|
|
err(1, "failed to get audio info");
|
|
|
|
bufsize = info.play.buffer_size;
|
|
|
|
if (bufsize < 32 * 1024)
|
|
|
|
bufsize = 32 * 1024;
|
|
|
|
|
|
|
|
if (*argv) {
|
|
|
|
int fd;
|
|
|
|
struct stat sb;
|
|
|
|
void *addr, *oaddr;
|
|
|
|
|
|
|
|
do {
|
1999-04-13 11:21:45 +04:00
|
|
|
ssize_t hdrlen;
|
|
|
|
|
1999-03-26 17:02:39 +03:00
|
|
|
fd = open(*argv, O_RDONLY);
|
1999-11-06 18:05:45 +03:00
|
|
|
if (fd < 0) {
|
|
|
|
warn("could not open %s", *argv);
|
|
|
|
exitstatus = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
if (fstat(fd, &sb) < 0)
|
|
|
|
err(1, "could not fstat %s", *argv);
|
|
|
|
filesize = sb.st_size;
|
|
|
|
|
|
|
|
oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
|
1999-04-13 11:21:45 +04:00
|
|
|
/*
|
|
|
|
* if we failed to mmap the file, try to read it
|
|
|
|
* instead, so that filesystems, etc, that do not
|
|
|
|
* support mmap() work
|
|
|
|
*/
|
1999-11-06 18:05:45 +03:00
|
|
|
if (addr == MAP_FAILED) {
|
1999-04-13 11:21:45 +04:00
|
|
|
play_fd(fd, *argv);
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* give the VM system a bit of a hint about the type
|
|
|
|
* of accesses we will make.
|
|
|
|
*/
|
2000-02-27 11:20:01 +03:00
|
|
|
if (madvise(addr, filesize, MADV_SEQUENTIAL) < 0 &&
|
|
|
|
!qflag)
|
1999-04-13 11:21:45 +04:00
|
|
|
warn("madvise failed, ignoring");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the header length and set up the audio device
|
|
|
|
*/
|
1999-03-26 17:02:39 +03:00
|
|
|
if ((hdrlen = audioctl_write_fromhdr(addr,
|
1999-03-28 14:35:05 +04:00
|
|
|
(size_t)filesize, ctlfd)) < 0) {
|
1999-03-26 17:02:39 +03:00
|
|
|
if (play_errstring)
|
|
|
|
errx(1, "%s: %s", play_errstring, *argv);
|
|
|
|
else
|
|
|
|
errx(1, "unknown audio file: %s", *argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
filesize -= hdrlen;
|
1999-11-06 18:05:45 +03:00
|
|
|
addr = (char *)addr + hdrlen;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
while (filesize > bufsize) {
|
|
|
|
if (write(audiofd, addr, bufsize) != bufsize)
|
|
|
|
err(1, "write failed");
|
1999-11-06 18:05:45 +03:00
|
|
|
addr = (char *)addr + bufsize;
|
1999-03-26 17:02:39 +03:00
|
|
|
filesize -= bufsize;
|
|
|
|
}
|
|
|
|
if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
|
|
|
|
err(1, "final write failed");
|
|
|
|
|
2000-02-27 11:20:01 +03:00
|
|
|
if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
|
1999-04-13 11:21:45 +04:00
|
|
|
warn("audio drain ioctl failed");
|
2000-02-27 11:20:01 +03:00
|
|
|
if (munmap(oaddr, (size_t)filesize) < 0 && !qflag)
|
1999-03-26 17:02:39 +03:00
|
|
|
err(1, "munmap failed");
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
} while (*++argv);
|
|
|
|
} else {
|
1999-04-13 11:21:45 +04:00
|
|
|
play_fd(STDIN_FILENO, "standard input");
|
|
|
|
}
|
1999-03-27 08:14:37 +03:00
|
|
|
|
1999-11-06 18:05:45 +03:00
|
|
|
exit(exitstatus);
|
1999-04-13 11:21:45 +04:00
|
|
|
}
|
1999-03-27 08:14:37 +03:00
|
|
|
|
1999-04-13 11:21:45 +04:00
|
|
|
/*
|
|
|
|
* play the file on on the file descriptor fd
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
play_fd(fd, file)
|
|
|
|
int fd;
|
|
|
|
char *file;
|
|
|
|
{
|
|
|
|
char *buffer = malloc(bufsize);
|
|
|
|
ssize_t hdrlen;
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
err(1, "malloc of read buffer failed");
|
|
|
|
|
|
|
|
n = read(fd, buffer, bufsize);
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
err(1, "read of standard input failed");
|
|
|
|
if (n == 0)
|
|
|
|
errx(1, "EOF on standard input");
|
|
|
|
|
|
|
|
hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd);
|
|
|
|
if (hdrlen < 0) {
|
|
|
|
if (play_errstring)
|
|
|
|
errx(1, "%s: %s", play_errstring, file);
|
|
|
|
else
|
|
|
|
errx(1, "unknown audio file: %s", file);
|
|
|
|
}
|
1999-03-27 08:14:37 +03:00
|
|
|
|
1999-04-13 11:21:45 +04:00
|
|
|
/* advance the buffer if we have to */
|
|
|
|
if (hdrlen > 0) {
|
|
|
|
/* shouldn't happen */
|
|
|
|
if (hdrlen > n)
|
|
|
|
err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
|
1999-03-27 08:14:37 +03:00
|
|
|
|
1999-04-13 11:21:45 +04:00
|
|
|
memmove(buffer, buffer + hdrlen, n - hdrlen);
|
1999-03-27 08:14:37 +03:00
|
|
|
|
1999-04-13 11:21:45 +04:00
|
|
|
m = read(fd, buffer + n, hdrlen);
|
|
|
|
n += m;
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
1999-04-13 11:21:45 +04:00
|
|
|
/* read until EOF or error */
|
|
|
|
do {
|
|
|
|
if (n == -1)
|
|
|
|
err(1, "read of standard input failed");
|
|
|
|
if (write(audiofd, buffer, n) != n)
|
|
|
|
err(1, "write failed");
|
|
|
|
} while ((n = read(fd, buffer, bufsize)));
|
1999-03-26 17:02:39 +03:00
|
|
|
|
2000-02-27 11:20:01 +03:00
|
|
|
if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
|
1999-04-13 11:21:45 +04:00
|
|
|
warn("audio drain ioctl failed");
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* only support sun and wav audio files so far ...
|
|
|
|
*
|
|
|
|
* XXX this should probably be mostly part of libaudio, but it
|
|
|
|
* uses the local "info" variable. blah... fix me!
|
|
|
|
*/
|
|
|
|
ssize_t
|
1999-03-28 14:35:05 +04:00
|
|
|
audioctl_write_fromhdr(hdr, fsz, fd)
|
1999-03-26 17:02:39 +03:00
|
|
|
void *hdr;
|
|
|
|
size_t fsz;
|
|
|
|
int fd;
|
|
|
|
{
|
|
|
|
sun_audioheader *sunhdr;
|
|
|
|
ssize_t hdr_len;
|
|
|
|
|
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
sunhdr = hdr;
|
|
|
|
if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
|
2000-12-13 11:19:54 +03:00
|
|
|
if (audio_sun_to_encoding(ntohl(sunhdr->encoding),
|
1999-03-26 17:02:39 +03:00
|
|
|
&info.play.encoding, &info.play.precision)) {
|
1999-09-27 09:00:21 +04:00
|
|
|
warnx("unknown unsupported Sun audio encoding format %d",
|
|
|
|
ntohl(sunhdr->encoding));
|
|
|
|
if (fflag)
|
|
|
|
goto set_audio_mode;
|
1999-03-26 17:02:39 +03:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
1999-04-02 20:05:55 +04:00
|
|
|
info.play.sample_rate = ntohl(sunhdr->sample_rate);
|
|
|
|
info.play.channels = ntohl(sunhdr->channels);
|
1999-03-28 14:35:05 +04:00
|
|
|
hdr_len = ntohl(sunhdr->hdr_size);
|
1999-03-26 17:02:39 +03:00
|
|
|
|
1999-03-28 14:35:05 +04:00
|
|
|
goto set_audio_mode;
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
|
|
|
|
&info.play.precision, &info.play.sample_rate, &info.play.channels);
|
|
|
|
|
|
|
|
switch (hdr_len) {
|
|
|
|
case AUDIO_ESHORTHDR:
|
|
|
|
case AUDIO_EWAVUNSUPP:
|
|
|
|
case AUDIO_EWAVBADPCM:
|
|
|
|
case AUDIO_EWAVNODATA:
|
1999-03-28 14:35:05 +04:00
|
|
|
play_errstring = audio_errstring(hdr_len);
|
1999-03-26 17:02:39 +03:00
|
|
|
/* FALL THROUGH */
|
|
|
|
case AUDIO_ENOENT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (hdr_len < 1)
|
|
|
|
break;
|
1999-03-28 14:35:05 +04:00
|
|
|
goto set_audio_mode;
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
1999-03-28 14:35:05 +04:00
|
|
|
/*
|
|
|
|
* if we don't know it, bail unless we are forcing.
|
|
|
|
*/
|
|
|
|
if (fflag == 0)
|
|
|
|
return (-1);
|
|
|
|
set_audio_mode:
|
|
|
|
if (port)
|
|
|
|
info.play.port = port;
|
|
|
|
if (volume)
|
|
|
|
info.play.gain = volume;
|
|
|
|
if (balance)
|
|
|
|
info.play.balance = balance;
|
|
|
|
if (fflag) {
|
|
|
|
if (sample_rate)
|
1999-04-02 20:05:55 +04:00
|
|
|
info.play.sample_rate = sample_rate;
|
1999-03-28 14:35:05 +04:00
|
|
|
if (channels)
|
1999-04-02 20:05:55 +04:00
|
|
|
info.play.channels = channels;
|
1999-03-28 14:35:05 +04:00
|
|
|
if (encoding)
|
|
|
|
info.play.encoding = encoding;
|
|
|
|
if (precision)
|
1999-09-13 21:18:21 +04:00
|
|
|
info.play.precision = precision;
|
1999-10-23 08:20:14 +04:00
|
|
|
hdr_len = 0;
|
1999-03-28 14:35:05 +04:00
|
|
|
}
|
|
|
|
info.mode = AUMODE_PLAY_ALL;
|
|
|
|
|
|
|
|
if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
|
|
|
|
err(1, "failed to set audio info");
|
|
|
|
|
1999-10-23 08:20:14 +04:00
|
|
|
return (hdr_len);
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
extern char *__progname;
|
|
|
|
|
1999-03-27 21:16:23 +03:00
|
|
|
fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
|
|
|
|
fprintf(stderr, "Options:\n\t"
|
|
|
|
"-C audio control device\n\t"
|
|
|
|
"-b balance (0-63)\n\t"
|
|
|
|
"-d audio device\n\t"
|
1999-03-28 14:35:05 +04:00
|
|
|
"-f force settings\n\t"
|
|
|
|
"\t-c forced channels\n\t"
|
|
|
|
"\t-e forced encoding\n\t"
|
|
|
|
"\t-P forced precision\n\t"
|
|
|
|
"\t-s forced sample rate\n\t"
|
1999-03-27 21:16:23 +03:00
|
|
|
"-i header information\n\t"
|
|
|
|
"-m monitor volume\n\t"
|
|
|
|
"-p output port\n\t"
|
|
|
|
"-v volume\n");
|
1999-11-08 13:21:20 +03:00
|
|
|
exit(EXIT_FAILURE);
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|