xref: /titanic_44/usr/src/cmd/audio/utilities/AudioLib.cc (revision 4bff34e37def8a90f9194d81bc345c52ba20086a)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1990-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <Audio.h>
30 #include <AudioFile.h>
31 #include <AudioList.h>
32 #include <AudioLib.h>
33 
34 // Generic Audio functions
35 
36 
37 // Open an audio file readonly, and return an AudioList referencing it.
38 AudioError
39 Audio_OpenInputFile(
40 	const char	*path,		// input filename
41 	Audio*&		ap)		// returned AudioList pointer
42 {
43 	AudioFile*	inf;
44 	AudioList*	lp;
45 	AudioError	err;
46 
47 	// Open file and decode the header
48 	inf = new AudioFile(path, (FileAccess)ReadOnly);
49 	if (inf == 0)
50 		return (AUDIO_UNIXERROR);
51 	err = inf->Open();
52 	if (err) {
53 		delete inf;
54 		return (err);
55 	}
56 
57 	// Create a list object and set it up to reference the file
58 	lp = new AudioList;
59 	if (lp == 0) {
60 		delete inf;
61 		return (AUDIO_UNIXERROR);
62 	}
63 	lp->Insert(inf);
64 	ap = lp;
65 	return (AUDIO_SUCCESS);
66 }
67 
68 
69 // Create an output file and copy an input stream to it.
70 // If an error occurs during output, leave a partially written file.
71 AudioError
72 Audio_WriteOutputFile(
73 	const char	*path,		// output filename
74 	const AudioHdr&	hdr,		// output data header
75 	Audio*		input)		// input data stream
76 {
77 	AudioFile*	outf;
78 	AudioError	err;
79 
80 	// Create output file object
81 	outf = new AudioFile(path, (FileAccess)WriteOnly);
82 	if (outf == 0)
83 		return (AUDIO_UNIXERROR);
84 
85 	// Set audio file header and create file
86 	if ((err = outf->SetHeader(hdr)) || (err = outf->Create())) {
87 		delete outf;
88 		return (err);
89 	}
90 
91 	// Copy data to file
92 	err = AudioCopy(input, outf);
93 
94 	// Close output file and clean up.  If error, leave partial file.
95 	delete outf;
96 	return (err);
97 }
98