xref: /illumos-gate/usr/src/cmd/audio/utilities/AudioStream.cc (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 <AudioStream.h>
28 #include <string.h>
29 
30 // class AudioStream methods
31 
32 
33 // Constructor
34 AudioStream::
AudioStream(const char * path)35 AudioStream(
36 	const char	*path):			// pathname
37 	Audio(path), length(AUDIO_UNKNOWN_TIME)
38 {
39 }
40 
41 // Set the header structure, even if it is already set
42 AudioError AudioStream::
updateheader(const AudioHdr & h)43 updateheader(
44 	const AudioHdr&	h)			// new header to set
45 {
46 	AudioError	err;
47 
48 	// Validate the header before stuffing it in
49 	err = h.Validate();
50 	if (err != AUDIO_SUCCESS)
51 		return (RaiseError(err));
52 
53 	// Copy in the new header
54 	hdr = h;
55 	return (AUDIO_SUCCESS);
56 }
57 
58 // Set the header structure
59 AudioError AudioStream::
SetHeader(const AudioHdr & h)60 SetHeader(
61 	const AudioHdr&	h)			// new header to set
62 {
63 	// Once the header is set and the file is open, it cannot be changed
64 	// XXX - hdrset test might be redundant?
65 	if (hdrset() && opened())
66 		return (RaiseError(AUDIO_ERR_NOEFFECT));
67 
68 	return (updateheader(h));
69 }
70 
71 // Check the endian nature of the data, and change if necessary.
72 AudioError AudioStream::
coerceEndian(unsigned char * buf,size_t len,AudioEndian endian)73 coerceEndian(unsigned char *buf, size_t len,
74 		    AudioEndian endian)
75 {
76 	// If the stream isn't endian sensitive, don't bother.
77 	if (! isEndianSensitive())
78 		return (AUDIO_SUCCESS);
79 
80 	if (hdr.endian == endian) {
81 #ifdef DEBUG
82 		AUDIO_DEBUG((1, "AudioStream: endian swap not needed, byte"
83 		    "order OK.\n"));
84 #endif
85 		return (AUDIO_SUCCESS);
86 	}
87 
88 	// The endians don't match, lets swap bytes.
89 	unsigned char chTemp;
90 	for (int i = 0; i < len - 1; i += 2) {
91 		chTemp = buf[i];
92 		buf[i] = buf[i + 1];
93 		buf[i+1] = chTemp;
94 	}
95 
96 #ifdef DEBUG
97 	AUDIO_DEBUG((1, "AudioStream: converting endian.\n"));
98 	// printf("AudioStream: converting endian.\n");
99 #endif
100 	return (AUDIO_SUCCESS);
101 }
102 
103 // This routine knows if the current format is endian sensitive.
isEndianSensitive() const104 Boolean AudioStream::isEndianSensitive() const
105 {
106 
107 	// Only these encodings have endian problems.
108 	if (hdr.encoding == LINEAR || hdr.encoding == FLOAT)
109 		return (TRUE);
110 
111 	return (FALSE);
112 }
113