xref: /illumos-gate/usr/src/cmd/audio/utilities/AudioLib.cc (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
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 #include <Audio.h>
28 #include <AudioFile.h>
29 #include <AudioList.h>
30 #include <AudioLib.h>
31 
32 // Generic Audio functions
33 
34 
35 // Open an audio file readonly, and return an AudioList referencing it.
36 AudioError
37 Audio_OpenInputFile(
38 	const char	*path,		// input filename
39 	Audio*&		ap)		// returned AudioList pointer
40 {
41 	AudioFile*	inf;
42 	AudioList*	lp;
43 	AudioError	err;
44 
45 	// Open file and decode the header
46 	inf = new AudioFile(path, (FileAccess)ReadOnly);
47 	if (inf == 0)
48 		return (AUDIO_UNIXERROR);
49 	err = inf->Open();
50 	if (err) {
51 		delete inf;
52 		return (err);
53 	}
54 
55 	// Create a list object and set it up to reference the file
56 	lp = new AudioList;
57 	if (lp == 0) {
58 		delete inf;
59 		return (AUDIO_UNIXERROR);
60 	}
61 	lp->Insert(inf);
62 	ap = lp;
63 	return (AUDIO_SUCCESS);
64 }
65 
66 
67 // Create an output file and copy an input stream to it.
68 // If an error occurs during output, leave a partially written file.
69 AudioError
70 Audio_WriteOutputFile(
71 	const char	*path,		// output filename
72 	const AudioHdr&	hdr,		// output data header
73 	Audio*		input)		// input data stream
74 {
75 	AudioFile*	outf;
76 	AudioError	err;
77 
78 	// Create output file object
79 	outf = new AudioFile(path, (FileAccess)WriteOnly);
80 	if (outf == 0)
81 		return (AUDIO_UNIXERROR);
82 
83 	// Set audio file header and create file
84 	if ((err = outf->SetHeader(hdr)) || (err = outf->Create())) {
85 		delete outf;
86 		return (err);
87 	}
88 
89 	// Copy data to file
90 	err = AudioCopy(input, outf);
91 
92 	// Close output file and clean up.  If error, leave partial file.
93 	delete outf;
94 	return (err);
95 }
96