4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1993-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include <sys/types.h>
37 #include <sys/param.h>
40 #include <AudioFile.h>
41 #include <AudioPipe.h>
42 #include <AudioRawPipe.h>
54 // append contents of buffer to output audio stream.
57 write_output(AudioBuffer
* buf
, AudioStream
* ofp
)
64 pos
= ofp
->GetLength();
65 len
= (size_t)buf
->GetHeader().Time_to_Bytes(buf
->GetLength());
66 cp
= (unsigned char *)buf
->GetAddress();
67 err
= ofp
->WriteData(cp
, len
, pos
);
71 // open input file and return ptr to AudioUnixFile object
72 // path is the path to the file (or set to Stdin if standard input).
73 // ihdr is the input header (only used for openning raw files)
74 // israw flags if it's a raw file. if fflag is set, ignore an
75 // any existing header on raw files. offset indicates where to
76 // start reading the file (raw files only ??).
77 AudioUnixfile
*open_input_file(const char *path
, const AudioHdr ihdr
,
78 int israw
, int fflag
, off_t offset
,
83 int file_type
; // ignore this ...
84 int infosize
; // ignore this ...
86 Audio_hdr ohdr
; // ignore this ...
89 // need to let caller know what format this is. so far, only raw
90 // and sun are supported....
91 fmt
= (israw
? F_RAW
: F_SUN
);
95 // no file? shouldn't happen. bomb out.
96 Err(MGET("no input file specified\n"));
102 if (isatty(fileno(stdin
))) {
104 "Stdin is a tty, please specify an input file\n"));
108 // XXX - need to check if stdin has a file
109 // header and ignore it if fflag not set.
110 ifp
= new AudioRawPipe(fileno(stdin
),
111 (FileAccess
)ReadOnly
, ihdr
, path
,
114 ifp
= new AudioPipe(fileno(stdin
), (FileAccess
)ReadOnly
,
119 Err(MGET("can't open pipe to %s, skipping...\n"),
125 // fall through for real files ...
127 if ((fd
= open(path
, O_RDONLY
)) < 0) {
128 Err(MGET("can't open %s, skipping...\n"), path
);
129 perror(MGET("open"));
133 // check if file already has a hdr.
134 if (hsize
= read(fd
, (char *)&fhdr
, sizeof (fhdr
))
139 if (lseek(fd
, 0, 0) < 0) { // reset
143 if (hsize
!= sizeof (fhdr
)) {
144 // no hdr - file too small,
145 // assume data is ok (tho it
146 // probably won't be) ...
147 ifp
= new AudioRawPipe(fd
, (FileAccess
)ReadOnly
,
150 // Check the validity of the
151 // header and get the size of
153 if (audio_decode_filehdr(fd
,
154 (unsigned char *)&fhdr
, &file_type
, &ohdr
,
155 &infosize
) == AUDIO_SUCCESS
) {
156 close(fd
); // create AudioFile()
159 MGET("%s has a file header, ignoring -i ...\n"),
161 fmt
= F_SUN
; // was raw ...
162 ifp
= new AudioFile(path
,
163 (FileAccess
)ReadOnly
);
165 // no hdr, create AudioRawPipe.
166 ifp
= new AudioRawPipe(fd
,
167 (FileAccess
)ReadOnly
, ihdr
,
172 } else { // force flag - don't even look for header
173 ifp
= new AudioRawPipe(fd
, (FileAccess
)ReadOnly
, ihdr
,
177 ifp
= new AudioFile(path
, (FileAccess
)ReadOnly
);
181 Err(MGET("can't open %s, skipping...\n"), path
);
186 // given a path, find the file it really points to (if it's a
187 // sym-link). return it's stat buf and real path.
189 get_realfile(char *&path
, struct stat
*st
)
191 static char tmpf
[MAXPATHLEN
]; // for reading sym-link
192 int err
; // for stat err
194 // first see if it's a sym-link and find real file
197 if (err
= lstat(path
, st
) < 0) {
201 if (!err
&& S_ISLNK(st
->st_mode
)) {
202 err
= readlink(path
, tmpf
,
203 (sizeof (tmpf
) - 1));
216 // create output audio file. if no path is supplied, use stdout.
217 // returns a ptr to an AudioUnixFile object.
224 const char *infoString
)
226 AudioUnixfile
* ofp
= 0;
227 AudioError err
; // for error msgs
231 if (isatty(fileno(stdout
))) {
233 MGET("Stdout is a tty, please specify an output file\n"));
239 if (!(ofp
= new AudioRawPipe(fileno(stdout
),
240 (FileAccess
)WriteOnly
, ohdr
,
243 MGET("can't create audio raw stdout pipe\n"));
246 } else if (ofmt
== F_SUN
) {
247 if (!(ofp
= new AudioPipe(fileno(stdout
),
248 (FileAccess
)WriteOnly
, path
))) {
250 MGET("can't create audio pipe for stdout\n"));
254 // XXX - should never happen ...
255 Err(MGET("can't create output file, unknown format\n"));
260 // first open file, then attach pipe to it
261 if ((fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
,
263 perror(MGET("open"));
264 Err(MGET("can't create output file %s\n"),
268 if (!(ofp
= new AudioRawPipe(fd
, (FileAccess
)WriteOnly
,
270 Err(MGET("can't create raw audio pipe %s\n"),
274 } else if (ofmt
== F_SUN
) {
275 if (!(ofp
= new AudioFile(path
,
276 (FileAccess
)ReadWrite
))) {
277 Err(MGET("can't create output file %s\n"),
282 // XXX - should never happen ...
283 Err(MGET("can't create output file, unknown format\n"));
288 // set the info string.
289 ofp
->SetInfostring(infoString
, -1);
291 // set the header and create the output audio object
292 if ((err
= ofp
->SetHeader(ohdr
)) != AUDIO_SUCCESS
) {
293 Err(MGET("can't set hdr on output file: %s\n"), err
.msg());
296 if ((err
= ofp
->Create()) != AUDIO_SUCCESS
) {
297 Err(MGET("can't create output file: %s\n"), err
.msg());