1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
28*7c478bd9Sstevel@tonic-gate #include <memory.h>
29*7c478bd9Sstevel@tonic-gate #include <math.h>
30*7c478bd9Sstevel@tonic-gate #include <AudioTypePcm.h>
31*7c478bd9Sstevel@tonic-gate #include <libaudio.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #define	irint(d)	((int)d)
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate // class AudioTypePcm methods
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate // Constructor
39*7c478bd9Sstevel@tonic-gate AudioTypePcm::
AudioTypePcm()40*7c478bd9Sstevel@tonic-gate AudioTypePcm()
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	// Set up fixed header values; the rest are negotiable
43*7c478bd9Sstevel@tonic-gate 	hdr.Clear();
44*7c478bd9Sstevel@tonic-gate 	hdr.samples_per_unit = 1;
45*7c478bd9Sstevel@tonic-gate 	hdr.encoding = LINEAR;
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate // Test conversion possibilities.
49*7c478bd9Sstevel@tonic-gate // Return TRUE if conversion to/from the specified type is possible.
50*7c478bd9Sstevel@tonic-gate Boolean AudioTypePcm::
CanConvert(AudioHdr h) const51*7c478bd9Sstevel@tonic-gate CanConvert(
52*7c478bd9Sstevel@tonic-gate 	AudioHdr	h) const		// target header
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	if (h.samples_per_unit != 1)
55*7c478bd9Sstevel@tonic-gate 		return (FALSE);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	switch (h.encoding) {
58*7c478bd9Sstevel@tonic-gate 	case LINEAR:
59*7c478bd9Sstevel@tonic-gate 		switch (h.bytes_per_unit) {
60*7c478bd9Sstevel@tonic-gate 		case 1: case 2: case 4:
61*7c478bd9Sstevel@tonic-gate 			break;
62*7c478bd9Sstevel@tonic-gate 		default:
63*7c478bd9Sstevel@tonic-gate 			return (FALSE);
64*7c478bd9Sstevel@tonic-gate 		}
65*7c478bd9Sstevel@tonic-gate 		break;
66*7c478bd9Sstevel@tonic-gate 	case FLOAT:
67*7c478bd9Sstevel@tonic-gate 		switch (h.bytes_per_unit) {
68*7c478bd9Sstevel@tonic-gate 		case 4: case 8:
69*7c478bd9Sstevel@tonic-gate 			break;
70*7c478bd9Sstevel@tonic-gate 		default:
71*7c478bd9Sstevel@tonic-gate 			return (FALSE);
72*7c478bd9Sstevel@tonic-gate 		}
73*7c478bd9Sstevel@tonic-gate 		break;
74*7c478bd9Sstevel@tonic-gate 	case ULAW:
75*7c478bd9Sstevel@tonic-gate 	case ALAW:
76*7c478bd9Sstevel@tonic-gate 		switch (h.bytes_per_unit) {
77*7c478bd9Sstevel@tonic-gate 		case 1:
78*7c478bd9Sstevel@tonic-gate 			break;
79*7c478bd9Sstevel@tonic-gate 		default:
80*7c478bd9Sstevel@tonic-gate 			return (FALSE);
81*7c478bd9Sstevel@tonic-gate 		}
82*7c478bd9Sstevel@tonic-gate 		break;
83*7c478bd9Sstevel@tonic-gate 	default:
84*7c478bd9Sstevel@tonic-gate 		return (FALSE);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 	return (TRUE);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate // Clip most negative values and convert to floating-point
90*7c478bd9Sstevel@tonic-gate inline double AudioTypePcm::
char2dbl(char B)91*7c478bd9Sstevel@tonic-gate char2dbl(char B)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	return ((unsigned char)B == 0x80 ? -1. : (double)B / 127.);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate inline double AudioTypePcm::
short2dbl(short S)96*7c478bd9Sstevel@tonic-gate short2dbl(short S)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	return ((unsigned short)S == 0x8000 ? -1. : (double)S / 32767.);
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate inline double AudioTypePcm::
long2dbl(long L)101*7c478bd9Sstevel@tonic-gate long2dbl(long L)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	return ((unsigned long)L == 0x80000000 ? -1. : (double)L / 2147483647.);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate // Convert floating-point to integer, scaled by the appropriate constant
106*7c478bd9Sstevel@tonic-gate inline long AudioTypePcm::
dbl2long(double D,long C)107*7c478bd9Sstevel@tonic-gate dbl2long(double D, long C)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	return (D >= 1. ? C : D <= -1. ? -C : (long)irint(D * (double)C));
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate // Simple type conversions
113*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
char2short(char * & F,short * & T)114*7c478bd9Sstevel@tonic-gate char2short(char *&F, short *&T) { *T++ = ((short)*F++) << 8; }
115*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
char2long(char * & F,long * & T)116*7c478bd9Sstevel@tonic-gate char2long(char *&F, long *&T) { *T++ = ((long)*F++) << 24; }
117*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
char2float(char * & F,float * & T)118*7c478bd9Sstevel@tonic-gate char2float(char *&F, float *&T) { *T++ = char2dbl(*F++); }
119*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
char2double(char * & F,double * & T)120*7c478bd9Sstevel@tonic-gate char2double(char *&F, double *&T) { *T++ = char2dbl(*F++); }
121*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
char2ulaw(char * & F,ulaw * & T)122*7c478bd9Sstevel@tonic-gate char2ulaw(char *&F, ulaw *&T) { *T++ = audio_c2u(*F); F++; }
123*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
char2alaw(char * & F,alaw * & T)124*7c478bd9Sstevel@tonic-gate char2alaw(char *&F, alaw *&T) { *T++ = audio_c2a(*F); F++; }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
short2char(short * & F,char * & T)127*7c478bd9Sstevel@tonic-gate short2char(short *&F, char *&T) { *T++ = (char)(*F++ >> 8); }
128*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
short2long(short * & F,long * & T)129*7c478bd9Sstevel@tonic-gate short2long(short *&F, long *&T) { *T++ = ((long)*F++) << 16; }
130*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
short2float(short * & F,float * & T)131*7c478bd9Sstevel@tonic-gate short2float(short *&F, float *&T) { *T++ = short2dbl(*F++); }
132*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
short2double(short * & F,double * & T)133*7c478bd9Sstevel@tonic-gate short2double(short *&F, double *&T) { *T++ = short2dbl(*F++); }
134*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
short2ulaw(short * & F,ulaw * & T)135*7c478bd9Sstevel@tonic-gate short2ulaw(short *&F, ulaw *&T) { *T++ = audio_s2u(*F); F++; }
136*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
short2alaw(short * & F,alaw * & T)137*7c478bd9Sstevel@tonic-gate short2alaw(short *&F, alaw *&T) { *T++ = audio_s2a(*F); F++; }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
long2char(long * & F,char * & T)140*7c478bd9Sstevel@tonic-gate long2char(long *&F, char *&T) { *T++ = (char)(*F++ >> 24); }
141*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
long2short(long * & F,short * & T)142*7c478bd9Sstevel@tonic-gate long2short(long *&F, short *&T) { *T++ = (short)(*F++ >> 16); }
143*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
long2float(long * & F,float * & T)144*7c478bd9Sstevel@tonic-gate long2float(long *&F, float *&T) { *T++ = long2dbl(*F++); }
145*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
long2double(long * & F,double * & T)146*7c478bd9Sstevel@tonic-gate long2double(long *&F, double *&T) { *T++ = long2dbl(*F++); }
147*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
long2ulaw(long * & F,ulaw * & T)148*7c478bd9Sstevel@tonic-gate long2ulaw(long *&F, ulaw *&T) { *T++ = audio_l2u(*F); F++; }
149*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
long2alaw(long * & F,alaw * & T)150*7c478bd9Sstevel@tonic-gate long2alaw(long *&F, alaw *&T) { *T++ = audio_l2a(*F); F++; }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
float2char(float * & F,char * & T)153*7c478bd9Sstevel@tonic-gate float2char(float *&F, char *&T) { *T++ = (char)dbl2long(*F++, 127); }
154*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
float2short(float * & F,short * & T)155*7c478bd9Sstevel@tonic-gate float2short(float *&F, short *&T) { *T++ = (short)dbl2long(*F++, 32767); }
156*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
float2long(float * & F,long * & T)157*7c478bd9Sstevel@tonic-gate float2long(float *&F, long *&T) { *T++ = dbl2long(*F++, 2147483647); }
158*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
float2double(float * & F,double * & T)159*7c478bd9Sstevel@tonic-gate float2double(float *&F, double *&T) { *T++ = *F++; }
160*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
float2ulaw(float * & F,ulaw * & T)161*7c478bd9Sstevel@tonic-gate float2ulaw(float *&F, ulaw *&T) { *T++ = audio_s2u(dbl2long(*F++, 32767)); }
162*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
float2alaw(float * & F,alaw * & T)163*7c478bd9Sstevel@tonic-gate float2alaw(float *&F, alaw *&T) { *T++ = audio_s2a(dbl2long(*F++, 32767)); }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
double2char(double * & F,char * & T)166*7c478bd9Sstevel@tonic-gate double2char(double *&F, char *&T) { *T++ = (char)dbl2long(*F++, 127); }
167*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
double2short(double * & F,short * & T)168*7c478bd9Sstevel@tonic-gate double2short(double *&F, short *&T) { *T++ = (short)dbl2long(*F++, 32767); }
169*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
double2long(double * & F,long * & T)170*7c478bd9Sstevel@tonic-gate double2long(double *&F, long *&T) { *T++ = dbl2long(*F++, 2147483647); }
171*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
double2float(double * & F,float * & T)172*7c478bd9Sstevel@tonic-gate double2float(double *&F, float *&T) { *T++ = *F++; }
173*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
double2ulaw(double * & F,ulaw * & T)174*7c478bd9Sstevel@tonic-gate double2ulaw(double *&F, ulaw *&T) { *T++ = audio_s2u(dbl2long(*F++, 32767)); }
175*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
double2alaw(double * & F,alaw * & T)176*7c478bd9Sstevel@tonic-gate double2alaw(double *&F, alaw *&T) { *T++ = audio_s2a(dbl2long(*F++, 32767)); }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
ulaw2char(ulaw * & F,char * & T)179*7c478bd9Sstevel@tonic-gate ulaw2char(ulaw *&F, char *&T) { *T++ = audio_u2c(*F); F++; }
180*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
ulaw2alaw(ulaw * & F,alaw * & T)181*7c478bd9Sstevel@tonic-gate ulaw2alaw(ulaw *&F, alaw *&T) { *T++ = audio_u2a(*F); F++; }
182*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
ulaw2short(ulaw * & F,short * & T)183*7c478bd9Sstevel@tonic-gate ulaw2short(ulaw *&F, short *&T) { *T++ = audio_u2s(*F); F++; }
184*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
ulaw2long(ulaw * & F,long * & T)185*7c478bd9Sstevel@tonic-gate ulaw2long(ulaw *&F, long *&T) { *T++ = audio_u2l(*F); F++; }
186*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
ulaw2float(ulaw * & F,float * & T)187*7c478bd9Sstevel@tonic-gate ulaw2float(ulaw *&F, float *&T) { *T++ = short2dbl(audio_u2s(*F)); F++; }
188*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
ulaw2double(ulaw * & F,double * & T)189*7c478bd9Sstevel@tonic-gate ulaw2double(ulaw *&F, double *&T) { *T++ = short2dbl(audio_u2s(*F)); F++; }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
alaw2char(alaw * & F,char * & T)192*7c478bd9Sstevel@tonic-gate alaw2char(alaw *&F, char *&T) { *T++ = audio_a2c(*F); F++; }
193*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
alaw2short(alaw * & F,short * & T)194*7c478bd9Sstevel@tonic-gate alaw2short(alaw *&F, short *&T) { *T++ = audio_a2s(*F); F++; }
195*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
alaw2long(alaw * & F,long * & T)196*7c478bd9Sstevel@tonic-gate alaw2long(alaw *&F, long *&T) { *T++ = audio_a2l(*F); F++; }
197*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
alaw2float(alaw * & F,float * & T)198*7c478bd9Sstevel@tonic-gate alaw2float(alaw *&F, float *&T) { *T++ = short2dbl(audio_a2s(*F)); F++; }
199*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
alaw2double(alaw * & F,double * & T)200*7c478bd9Sstevel@tonic-gate alaw2double(alaw *&F, double *&T) { *T++ = short2dbl(audio_a2s(*F)); F++; }
201*7c478bd9Sstevel@tonic-gate inline void AudioTypePcm::
alaw2ulaw(alaw * & F,ulaw * & T)202*7c478bd9Sstevel@tonic-gate alaw2ulaw(alaw*& F, ulaw*& T) { *T++ = audio_a2u(*F); F++; }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate // Convert buffer to the specified type
206*7c478bd9Sstevel@tonic-gate // May replace the buffer with a new one, if necessary
207*7c478bd9Sstevel@tonic-gate AudioError AudioTypePcm::
Convert(AudioBuffer * & inbuf,AudioHdr outhdr)208*7c478bd9Sstevel@tonic-gate Convert(
209*7c478bd9Sstevel@tonic-gate 	AudioBuffer*&	inbuf,			// data buffer to process
210*7c478bd9Sstevel@tonic-gate 	AudioHdr	outhdr)			// target header
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	AudioBuffer*	outbuf;
213*7c478bd9Sstevel@tonic-gate 	AudioHdr	inhdr;
214*7c478bd9Sstevel@tonic-gate 	Double		length;
215*7c478bd9Sstevel@tonic-gate 	size_t		frames;
216*7c478bd9Sstevel@tonic-gate 	void*		inptr;
217*7c478bd9Sstevel@tonic-gate 	void*		outptr;
218*7c478bd9Sstevel@tonic-gate 	AudioError	err;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	inhdr = inbuf->GetHeader();
221*7c478bd9Sstevel@tonic-gate 	length = inbuf->GetLength();
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	if (Undefined(length))
224*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_BADARG);
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	// Make sure we're not being asked to do the impossible
227*7c478bd9Sstevel@tonic-gate 	// XXX - how do we deal with multi-channel data??
228*7c478bd9Sstevel@tonic-gate 	// XXX - need a better error code
229*7c478bd9Sstevel@tonic-gate 	if ((err = inhdr.Validate()) || (err = outhdr.Validate()))
230*7c478bd9Sstevel@tonic-gate 		return (err);
231*7c478bd9Sstevel@tonic-gate 	if ((inhdr.sample_rate != outhdr.sample_rate) ||
232*7c478bd9Sstevel@tonic-gate 	    (inhdr.samples_per_unit != outhdr.samples_per_unit) ||
233*7c478bd9Sstevel@tonic-gate 	    (inhdr.samples_per_unit != 1) ||
234*7c478bd9Sstevel@tonic-gate 	    (inhdr.channels != outhdr.channels))
235*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_HDRINVAL);
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	// If the buffer is not referenced, and the target size is no bigger
238*7c478bd9Sstevel@tonic-gate 	// than the current size, the conversion can be done in place
239*7c478bd9Sstevel@tonic-gate 	if (!inbuf->isReferenced() &&
240*7c478bd9Sstevel@tonic-gate 	    (outhdr.bytes_per_unit <= inhdr.bytes_per_unit)) {
241*7c478bd9Sstevel@tonic-gate 		outbuf = inbuf;
242*7c478bd9Sstevel@tonic-gate 	} else {
243*7c478bd9Sstevel@tonic-gate 		// Allocate a new buffer
244*7c478bd9Sstevel@tonic-gate 		outbuf = new AudioBuffer(length, "(PCM conversion buffer)");
245*7c478bd9Sstevel@tonic-gate 		if (outbuf == 0)
246*7c478bd9Sstevel@tonic-gate 			return (AUDIO_UNIXERROR);
247*7c478bd9Sstevel@tonic-gate 		if (err = outbuf->SetHeader(outhdr)) {
248*7c478bd9Sstevel@tonic-gate 			delete outbuf;
249*7c478bd9Sstevel@tonic-gate 			return (err);
250*7c478bd9Sstevel@tonic-gate 		}
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	// Convert from the input type to the output type
254*7c478bd9Sstevel@tonic-gate 	inptr = inbuf->GetAddress();
255*7c478bd9Sstevel@tonic-gate 	outptr = outbuf->GetAddress();
256*7c478bd9Sstevel@tonic-gate 	frames = (size_t)inhdr.Time_to_Samples(length)
257*7c478bd9Sstevel@tonic-gate 		* inhdr.channels;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate // Define macro to copy with no data conversion
260*7c478bd9Sstevel@tonic-gate #define	COPY(N)		if (inptr != outptr) memcpy(outptr, inptr, frames * N)
261*7c478bd9Sstevel@tonic-gate // Define macro to translate a buffer
262*7c478bd9Sstevel@tonic-gate // XXX - The temporary pointers are necessary to get the updates
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate // token catenation different for ANSI cpp v.s. old cpp.
265*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
266*7c478bd9Sstevel@tonic-gate #define	MOVE(F, T)	{						\
267*7c478bd9Sstevel@tonic-gate 			    F* ip = (F*)inptr; T* op = (T*)outptr;	\
268*7c478bd9Sstevel@tonic-gate 			    while (frames-- > 0) F ## 2 ## T(ip, op);	\
269*7c478bd9Sstevel@tonic-gate 			}
270*7c478bd9Sstevel@tonic-gate #else
271*7c478bd9Sstevel@tonic-gate #define	MOVE(F, T)	{						\
272*7c478bd9Sstevel@tonic-gate 			    F* ip = (F*)inptr; T* op = (T*)outptr;	\
273*7c478bd9Sstevel@tonic-gate 			    while (frames-- > 0) F /* */ 2 /* */ T(ip, op);\
274*7c478bd9Sstevel@tonic-gate 			}
275*7c478bd9Sstevel@tonic-gate #endif
276*7c478bd9Sstevel@tonic-gate 	switch (inhdr.encoding) {
277*7c478bd9Sstevel@tonic-gate 	case LINEAR:
278*7c478bd9Sstevel@tonic-gate 		switch (outhdr.encoding) {
279*7c478bd9Sstevel@tonic-gate 		case LINEAR:		// Convert linear to linear
280*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
281*7c478bd9Sstevel@tonic-gate 			case 1:
282*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
283*7c478bd9Sstevel@tonic-gate 				case 1: COPY(1); break;
284*7c478bd9Sstevel@tonic-gate 				case 2: MOVE(char, short); break;
285*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(char, long); break;
286*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
287*7c478bd9Sstevel@tonic-gate 				}
288*7c478bd9Sstevel@tonic-gate 				break;
289*7c478bd9Sstevel@tonic-gate 			case 2:
290*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
291*7c478bd9Sstevel@tonic-gate 				case 1: MOVE(short, char); break;
292*7c478bd9Sstevel@tonic-gate 				case 2: COPY(2); break;
293*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(short, long); break;
294*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
295*7c478bd9Sstevel@tonic-gate 				}
296*7c478bd9Sstevel@tonic-gate 				break;
297*7c478bd9Sstevel@tonic-gate 			case 4:
298*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
299*7c478bd9Sstevel@tonic-gate 				case 1: MOVE(long, char); break;
300*7c478bd9Sstevel@tonic-gate 				case 2: MOVE(long, short); break;
301*7c478bd9Sstevel@tonic-gate 				case 4: COPY(4); break;
302*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
303*7c478bd9Sstevel@tonic-gate 				}
304*7c478bd9Sstevel@tonic-gate 				break;
305*7c478bd9Sstevel@tonic-gate 			default:
306*7c478bd9Sstevel@tonic-gate 				err = AUDIO_ERR_HDRINVAL; break;
307*7c478bd9Sstevel@tonic-gate 			}
308*7c478bd9Sstevel@tonic-gate 			break;
309*7c478bd9Sstevel@tonic-gate 		case FLOAT:		// Convert linear to float
310*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
311*7c478bd9Sstevel@tonic-gate 			case 1:
312*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
313*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(char, float); break;
314*7c478bd9Sstevel@tonic-gate 				case 8: MOVE(char, double); break;
315*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
316*7c478bd9Sstevel@tonic-gate 				}
317*7c478bd9Sstevel@tonic-gate 				break;
318*7c478bd9Sstevel@tonic-gate 			case 2:
319*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
320*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(short, float); break;
321*7c478bd9Sstevel@tonic-gate 				case 8: MOVE(short, double); break;
322*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
323*7c478bd9Sstevel@tonic-gate 				}
324*7c478bd9Sstevel@tonic-gate 				break;
325*7c478bd9Sstevel@tonic-gate 			case 4:
326*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
327*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(long, float); break;
328*7c478bd9Sstevel@tonic-gate 				case 8: MOVE(long, double); break;
329*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
330*7c478bd9Sstevel@tonic-gate 				}
331*7c478bd9Sstevel@tonic-gate 				break;
332*7c478bd9Sstevel@tonic-gate 			default:
333*7c478bd9Sstevel@tonic-gate 				err = AUDIO_ERR_HDRINVAL; break;
334*7c478bd9Sstevel@tonic-gate 			}
335*7c478bd9Sstevel@tonic-gate 			break;
336*7c478bd9Sstevel@tonic-gate 		case ULAW:		// Convert linear to u-law
337*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
338*7c478bd9Sstevel@tonic-gate 			case 1: MOVE(char, ulaw); break;
339*7c478bd9Sstevel@tonic-gate 			case 2: MOVE(short, ulaw); break;
340*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(long, ulaw); break;
341*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
342*7c478bd9Sstevel@tonic-gate 			}
343*7c478bd9Sstevel@tonic-gate 			break;
344*7c478bd9Sstevel@tonic-gate 		case ALAW:		// Convert linear to a-law
345*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
346*7c478bd9Sstevel@tonic-gate 			case 1: MOVE(char, alaw); break;
347*7c478bd9Sstevel@tonic-gate 			case 2: MOVE(short, alaw); break;
348*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(long, alaw); break;
349*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
350*7c478bd9Sstevel@tonic-gate 			}
351*7c478bd9Sstevel@tonic-gate 			break;
352*7c478bd9Sstevel@tonic-gate 		default:
353*7c478bd9Sstevel@tonic-gate 			err = AUDIO_ERR_HDRINVAL; break;
354*7c478bd9Sstevel@tonic-gate 		}
355*7c478bd9Sstevel@tonic-gate 		break;
356*7c478bd9Sstevel@tonic-gate 	case FLOAT:
357*7c478bd9Sstevel@tonic-gate 		switch (outhdr.encoding) {
358*7c478bd9Sstevel@tonic-gate 		case LINEAR:		// Convert float to linear
359*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
360*7c478bd9Sstevel@tonic-gate 			case 4:
361*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
362*7c478bd9Sstevel@tonic-gate 				case 1: MOVE(float, char); break;
363*7c478bd9Sstevel@tonic-gate 				case 2: MOVE(float, short); break;
364*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(float, long); break;
365*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
366*7c478bd9Sstevel@tonic-gate 				}
367*7c478bd9Sstevel@tonic-gate 				break;
368*7c478bd9Sstevel@tonic-gate 			case 8:
369*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
370*7c478bd9Sstevel@tonic-gate 				case 1: MOVE(double, char); break;
371*7c478bd9Sstevel@tonic-gate 				case 2: MOVE(double, short); break;
372*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(double, long); break;
373*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
374*7c478bd9Sstevel@tonic-gate 				}
375*7c478bd9Sstevel@tonic-gate 				break;
376*7c478bd9Sstevel@tonic-gate 			default:
377*7c478bd9Sstevel@tonic-gate 				err = AUDIO_ERR_HDRINVAL; break;
378*7c478bd9Sstevel@tonic-gate 			}
379*7c478bd9Sstevel@tonic-gate 			break;
380*7c478bd9Sstevel@tonic-gate 		case FLOAT:		// Convert float to float
381*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
382*7c478bd9Sstevel@tonic-gate 			case 4:
383*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
384*7c478bd9Sstevel@tonic-gate 				case 4: COPY(4); break;
385*7c478bd9Sstevel@tonic-gate 				case 8: MOVE(float, double); break;
386*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
387*7c478bd9Sstevel@tonic-gate 				}
388*7c478bd9Sstevel@tonic-gate 				break;
389*7c478bd9Sstevel@tonic-gate 			case 8:
390*7c478bd9Sstevel@tonic-gate 				switch (outhdr.bytes_per_unit) {
391*7c478bd9Sstevel@tonic-gate 				case 4: MOVE(double, float); break;
392*7c478bd9Sstevel@tonic-gate 				case 8: COPY(8); break;
393*7c478bd9Sstevel@tonic-gate 				default: err = AUDIO_ERR_HDRINVAL; break;
394*7c478bd9Sstevel@tonic-gate 				}
395*7c478bd9Sstevel@tonic-gate 				break;
396*7c478bd9Sstevel@tonic-gate 			default:
397*7c478bd9Sstevel@tonic-gate 				err = AUDIO_ERR_HDRINVAL; break;
398*7c478bd9Sstevel@tonic-gate 			}
399*7c478bd9Sstevel@tonic-gate 			break;
400*7c478bd9Sstevel@tonic-gate 		case ULAW:		// Convert float to u-law
401*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
402*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(float, ulaw); break;
403*7c478bd9Sstevel@tonic-gate 			case 8: MOVE(double, ulaw); break;
404*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
405*7c478bd9Sstevel@tonic-gate 			}
406*7c478bd9Sstevel@tonic-gate 			break;
407*7c478bd9Sstevel@tonic-gate 		case ALAW:		// Convert float to a-law
408*7c478bd9Sstevel@tonic-gate 			switch (inhdr.bytes_per_unit) {
409*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(float, alaw); break;
410*7c478bd9Sstevel@tonic-gate 			case 8: MOVE(double, alaw); break;
411*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
412*7c478bd9Sstevel@tonic-gate 			}
413*7c478bd9Sstevel@tonic-gate 			break;
414*7c478bd9Sstevel@tonic-gate 		default:
415*7c478bd9Sstevel@tonic-gate 			err = AUDIO_ERR_HDRINVAL; break;
416*7c478bd9Sstevel@tonic-gate 		}
417*7c478bd9Sstevel@tonic-gate 		break;
418*7c478bd9Sstevel@tonic-gate 	case ULAW:
419*7c478bd9Sstevel@tonic-gate 		switch (outhdr.encoding) {
420*7c478bd9Sstevel@tonic-gate 		case LINEAR:		// Convert ulaw to linear
421*7c478bd9Sstevel@tonic-gate 			switch (outhdr.bytes_per_unit) {
422*7c478bd9Sstevel@tonic-gate 			case 1: MOVE(ulaw, char); break;
423*7c478bd9Sstevel@tonic-gate 			case 2: MOVE(ulaw, short); break;
424*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(ulaw, long); break;
425*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
426*7c478bd9Sstevel@tonic-gate 			}
427*7c478bd9Sstevel@tonic-gate 			break;
428*7c478bd9Sstevel@tonic-gate 		case FLOAT:		// Convert ulaw to float
429*7c478bd9Sstevel@tonic-gate 			switch (outhdr.bytes_per_unit) {
430*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(ulaw, float); break;
431*7c478bd9Sstevel@tonic-gate 			case 8: MOVE(ulaw, double); break;
432*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
433*7c478bd9Sstevel@tonic-gate 			}
434*7c478bd9Sstevel@tonic-gate 			break;
435*7c478bd9Sstevel@tonic-gate 		case ULAW:		// Convert ulaw to u-law
436*7c478bd9Sstevel@tonic-gate 			COPY(1); break;
437*7c478bd9Sstevel@tonic-gate 		case ALAW:		// Convert ulaw to a-law
438*7c478bd9Sstevel@tonic-gate 			MOVE(ulaw, alaw); break;
439*7c478bd9Sstevel@tonic-gate 		default:
440*7c478bd9Sstevel@tonic-gate 			err = AUDIO_ERR_HDRINVAL; break;
441*7c478bd9Sstevel@tonic-gate 		}
442*7c478bd9Sstevel@tonic-gate 		break;
443*7c478bd9Sstevel@tonic-gate 	case ALAW:
444*7c478bd9Sstevel@tonic-gate 		switch (outhdr.encoding) {
445*7c478bd9Sstevel@tonic-gate 		case LINEAR:		// Convert alaw to linear
446*7c478bd9Sstevel@tonic-gate 			switch (outhdr.bytes_per_unit) {
447*7c478bd9Sstevel@tonic-gate 			case 1: MOVE(alaw, char); break;
448*7c478bd9Sstevel@tonic-gate 			case 2: MOVE(alaw, short); break;
449*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(alaw, long); break;
450*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
451*7c478bd9Sstevel@tonic-gate 			}
452*7c478bd9Sstevel@tonic-gate 			break;
453*7c478bd9Sstevel@tonic-gate 		case FLOAT:		// Convert alaw to float
454*7c478bd9Sstevel@tonic-gate 			switch (outhdr.bytes_per_unit) {
455*7c478bd9Sstevel@tonic-gate 			case 4: MOVE(alaw, float); break;
456*7c478bd9Sstevel@tonic-gate 			case 8: MOVE(alaw, double); break;
457*7c478bd9Sstevel@tonic-gate 			default: err = AUDIO_ERR_HDRINVAL; break;
458*7c478bd9Sstevel@tonic-gate 			}
459*7c478bd9Sstevel@tonic-gate 			break;
460*7c478bd9Sstevel@tonic-gate 		case ALAW:		// Convert alaw to a-law
461*7c478bd9Sstevel@tonic-gate 			COPY(1); break;
462*7c478bd9Sstevel@tonic-gate 		case ULAW:		// Convert alaw to u-law
463*7c478bd9Sstevel@tonic-gate 			MOVE(alaw, ulaw); break;
464*7c478bd9Sstevel@tonic-gate 		default:
465*7c478bd9Sstevel@tonic-gate 			err = AUDIO_ERR_HDRINVAL; break;
466*7c478bd9Sstevel@tonic-gate 		}
467*7c478bd9Sstevel@tonic-gate 		break;
468*7c478bd9Sstevel@tonic-gate 	default:
469*7c478bd9Sstevel@tonic-gate 		err = AUDIO_ERR_HDRINVAL; break;
470*7c478bd9Sstevel@tonic-gate 	}
471*7c478bd9Sstevel@tonic-gate 	if (err) {
472*7c478bd9Sstevel@tonic-gate 		if (outbuf != inbuf)
473*7c478bd9Sstevel@tonic-gate 			delete outbuf;
474*7c478bd9Sstevel@tonic-gate 		return (err);
475*7c478bd9Sstevel@tonic-gate 	}
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	// Finish up
478*7c478bd9Sstevel@tonic-gate 	if (outbuf == inbuf) {
479*7c478bd9Sstevel@tonic-gate 		// If the conversion was in-place, set the new header
480*7c478bd9Sstevel@tonic-gate 		(void) inbuf->SetHeader(outhdr);
481*7c478bd9Sstevel@tonic-gate 	} else {
482*7c478bd9Sstevel@tonic-gate 		// This will delete the buffer
483*7c478bd9Sstevel@tonic-gate 		inbuf->Reference();
484*7c478bd9Sstevel@tonic-gate 		inbuf->Dereference();
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 		// Set the valid data length and replace the pointer
487*7c478bd9Sstevel@tonic-gate 		outbuf->SetLength(length);
488*7c478bd9Sstevel@tonic-gate 		inbuf = outbuf;
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
491*7c478bd9Sstevel@tonic-gate }
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate AudioError AudioTypePcm::
Flush(AudioBuffer * &)494*7c478bd9Sstevel@tonic-gate Flush(
495*7c478bd9Sstevel@tonic-gate 	AudioBuffer*&	/* buf */)
496*7c478bd9Sstevel@tonic-gate {
497*7c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
498*7c478bd9Sstevel@tonic-gate }
499