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