#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/param.h>
#include <Audio.h>
#include <AudioFile.h>
#include <AudioPipe.h>
#include <AudioRawPipe.h>
#include <AudioLib.h>
#include <AudioHdr.h>
#include <libaudio.h>
#include <audio/au.h>
extern char *Stdin;
extern char *Stdout;
#include <convert.h>
AudioError
write_output(AudioBuffer* buf, AudioStream* ofp)
{
unsigned char *cp;
size_t len;
Double pos;
AudioError err;
pos = ofp->GetLength();
len = (size_t)buf->GetHeader().Time_to_Bytes(buf->GetLength());
cp = (unsigned char *)buf->GetAddress();
err = ofp->WriteData(cp, len, pos);
return (err);
}
AudioUnixfile *open_input_file(const char *path, const AudioHdr ihdr,
int israw, int fflag, off_t offset,
format_type& fmt)
{
AudioUnixfile* ifp;
int fd;
int file_type;
int infosize;
au_filehdr_t fhdr;
Audio_hdr ohdr;
unsigned int hsize;
fmt = (israw ? F_RAW : F_SUN);
if (!path) {
Err(MGET("no input file specified\n"));
exit(1);
}
if (path == Stdin) {
if (isatty(fileno(stdin))) {
Err(MGET(
"Stdin is a tty, please specify an input file\n"));
exit(1);
}
if (israw) {
ifp = new AudioRawPipe(fileno(stdin),
(FileAccess)ReadOnly, ihdr, path,
offset);
} else {
ifp = new AudioPipe(fileno(stdin), (FileAccess)ReadOnly,
path);
}
if (!ifp) {
Err(MGET("can't open pipe to %s, skipping...\n"),
Stdin);
}
return (ifp);
}
if (israw) {
if ((fd = open(path, O_RDONLY)) < 0) {
Err(MGET("can't open %s, skipping...\n"), path);
perror(MGET("open"));
return (NULL);
}
if (!fflag) {
if (hsize = read(fd, (char *)&fhdr, sizeof (fhdr))
< 0) {
perror("read");
exit(1);
}
if (lseek(fd, 0, 0) < 0) {
perror("lseek");
exit(1);
}
if (hsize != sizeof (fhdr)) {
ifp = new AudioRawPipe(fd, (FileAccess)ReadOnly,
ihdr, path, offset);
} else {
if (audio_decode_filehdr(fd,
(unsigned char *)&fhdr, &file_type, &ohdr,
&infosize) == AUDIO_SUCCESS) {
close(fd);
Err(
MGET("%s has a file header, ignoring -i ...\n"),
path);
fmt = F_SUN;
ifp = new AudioFile(path,
(FileAccess)ReadOnly);
} else {
ifp = new AudioRawPipe(fd,
(FileAccess)ReadOnly, ihdr,
path, offset);
}
}
} else {
ifp = new AudioRawPipe(fd, (FileAccess)ReadOnly, ihdr,
path, offset);
}
} else {
ifp = new AudioFile(path, (FileAccess)ReadOnly);
}
if (!ifp) {
Err(MGET("can't open %s, skipping...\n"), path);
}
return (ifp);
}
void
get_realfile(char *&path, struct stat *st)
{
static char tmpf[MAXPATHLEN];
int err;
err = 0;
while (err == 0) {
if (err = lstat(path, st) < 0) {
perror("lstat");
exit(1);
}
if (!err && S_ISLNK(st->st_mode)) {
err = readlink(path, tmpf,
(sizeof (tmpf) - 1));
if (err > 0) {
tmpf[err] = '\0';
path = tmpf;
err = 0;
}
} else {
break;
}
}
}
AudioUnixfile*
create_output_file(
const char *path,
const AudioHdr ohdr,
format_type ofmt,
const char *infoString)
{
AudioUnixfile* ofp = 0;
AudioError err;
int fd;
if (!path) {
if (isatty(fileno(stdout))) {
Err(
MGET("Stdout is a tty, please specify an output file\n"));
exit(1);
}
path = Stdout;
if (ofmt == F_RAW) {
if (!(ofp = new AudioRawPipe(fileno(stdout),
(FileAccess)WriteOnly, ohdr,
path))) {
Err(
MGET("can't create audio raw stdout pipe\n"));
exit(1);
}
} else if (ofmt == F_SUN) {
if (!(ofp = new AudioPipe(fileno(stdout),
(FileAccess)WriteOnly, path))) {
Err(
MGET("can't create audio pipe for stdout\n"));
exit(1);
}
} else {
Err(MGET("can't create output file, unknown format\n"));
exit(1);
}
} else {
if (ofmt == F_RAW) {
if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC,
0666)) < 0) {
perror(MGET("open"));
Err(MGET("can't create output file %s\n"),
path);
exit(1);
}
if (!(ofp = new AudioRawPipe(fd, (FileAccess)WriteOnly,
ohdr, path))) {
Err(MGET("can't create raw audio pipe %s\n"),
path);
exit(1);
}
} else if (ofmt == F_SUN) {
if (!(ofp = new AudioFile(path,
(FileAccess)ReadWrite))) {
Err(MGET("can't create output file %s\n"),
path);
exit(1);
}
} else {
Err(MGET("can't create output file, unknown format\n"));
exit(1);
}
}
ofp->SetInfostring(infoString, -1);
if ((err = ofp->SetHeader(ohdr)) != AUDIO_SUCCESS) {
Err(MGET("can't set hdr on output file: %s\n"), err.msg());
exit(1);
}
if ((err = ofp->Create()) != AUDIO_SUCCESS) {
Err(MGET("can't create output file: %s\n"), err.msg());
exit(1);
}
return (ofp);
}