1*88447a05SGarrett D'Amore /*
2*88447a05SGarrett D'Amore  * CDDL HEADER START
3*88447a05SGarrett D'Amore  *
4*88447a05SGarrett D'Amore  * The contents of this file are subject to the terms of the
5*88447a05SGarrett D'Amore  * Common Development and Distribution License (the "License").
6*88447a05SGarrett D'Amore  * You may not use this file except in compliance with the License.
7*88447a05SGarrett D'Amore  *
8*88447a05SGarrett D'Amore  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*88447a05SGarrett D'Amore  * or http://www.opensolaris.org/os/licensing.
10*88447a05SGarrett D'Amore  * See the License for the specific language governing permissions
11*88447a05SGarrett D'Amore  * and limitations under the License.
12*88447a05SGarrett D'Amore  *
13*88447a05SGarrett D'Amore  * When distributing Covered Code, include this CDDL HEADER in each
14*88447a05SGarrett D'Amore  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*88447a05SGarrett D'Amore  * If applicable, add the following below this CDDL HEADER, with the
16*88447a05SGarrett D'Amore  * fields enclosed by brackets "[]" replaced with your own identifying
17*88447a05SGarrett D'Amore  * information: Portions Copyright [yyyy] [name of copyright owner]
18*88447a05SGarrett D'Amore  *
19*88447a05SGarrett D'Amore  * CDDL HEADER END
20*88447a05SGarrett D'Amore  */
21*88447a05SGarrett D'Amore /*
22*88447a05SGarrett D'Amore  * Copyright (C) 4Front Technologies 1996-2008.
23*88447a05SGarrett D'Amore  *
24*88447a05SGarrett D'Amore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25*88447a05SGarrett D'Amore  * Use is subject to license terms.
26*88447a05SGarrett D'Amore  */
27*88447a05SGarrett D'Amore /*
28*88447a05SGarrett D'Amore  * Purpose: Test sounds for osstest
29*88447a05SGarrett D'Amore  *
30*88447a05SGarrett D'Amore  * Nodoc:
31*88447a05SGarrett D'Amore  */
32*88447a05SGarrett D'Amore 
33*88447a05SGarrett D'Amore #include <string.h>
34*88447a05SGarrett D'Amore 
35*88447a05SGarrett D'Amore #include "wavedata.h"
36*88447a05SGarrett D'Amore 
37*88447a05SGarrett D'Amore static int
le_int(const unsigned char * p,int l)38*88447a05SGarrett D'Amore le_int(const unsigned char *p, int l)
39*88447a05SGarrett D'Amore {
40*88447a05SGarrett D'Amore 	int i, val;
41*88447a05SGarrett D'Amore 
42*88447a05SGarrett D'Amore 	val = 0;
43*88447a05SGarrett D'Amore 
44*88447a05SGarrett D'Amore 	for (i = l - 1; i >= 0; i--) {
45*88447a05SGarrett D'Amore 		val = (val << 8) | p[i];
46*88447a05SGarrett D'Amore 	}
47*88447a05SGarrett D'Amore 
48*88447a05SGarrett D'Amore 	return (val);
49*88447a05SGarrett D'Amore }
50*88447a05SGarrett D'Amore 
51*88447a05SGarrett D'Amore int
uncompress_wave(short * outbuf)52*88447a05SGarrett D'Amore uncompress_wave(short *outbuf)
53*88447a05SGarrett D'Amore {
54*88447a05SGarrett D'Amore #define	WAVE_FORMAT_ADPCM		0x0002
55*88447a05SGarrett D'Amore 
56*88447a05SGarrett D'Amore 	int i, n, dataleft, x, l = sizeof (inbuf);
57*88447a05SGarrett D'Amore 	const unsigned char *hdr = inbuf;
58*88447a05SGarrett D'Amore 	typedef struct {
59*88447a05SGarrett D'Amore 		int coeff1, coeff2;
60*88447a05SGarrett D'Amore 	}
61*88447a05SGarrett D'Amore 	adpcm_coeff;
62*88447a05SGarrett D'Amore 
63*88447a05SGarrett D'Amore 	adpcm_coeff coeff[32];
64*88447a05SGarrett D'Amore 	static int AdaptionTable[] = { 230, 230, 230, 230, 307, 409, 512, 614,
65*88447a05SGarrett D'Amore 	    768, 614, 512, 409, 307, 230, 230, 230
66*88447a05SGarrett D'Amore 	};
67*88447a05SGarrett D'Amore 
68*88447a05SGarrett D'Amore 	unsigned char buf[4096];
69*88447a05SGarrett D'Amore 
70*88447a05SGarrett D'Amore 	int channels = 1;
71*88447a05SGarrett D'Amore 	int p = 12, outp = 0;
72*88447a05SGarrett D'Amore 	int nBlockAlign = 2048;
73*88447a05SGarrett D'Amore 	int wSamplesPerBlock = 2036, wNumCoeff = 7;
74*88447a05SGarrett D'Amore 	int nib;
75*88447a05SGarrett D'Amore 	int ppp;
76*88447a05SGarrett D'Amore 
77*88447a05SGarrett D'Amore 	/* filelen = le_int(&hdr[4], 4); */
78*88447a05SGarrett D'Amore 
79*88447a05SGarrett D'Amore 	while (p < l - 16 && memcmp(&hdr[p], "data", 4) != 0) {
80*88447a05SGarrett D'Amore 		n = le_int(&hdr[p + 4], 4);
81*88447a05SGarrett D'Amore 
82*88447a05SGarrett D'Amore 		if (memcmp(&hdr[p], "fmt ", 4) == 0) {
83*88447a05SGarrett D'Amore 
84*88447a05SGarrett D'Amore 			/* fmt = le_int(&hdr[p + 8], 2); */
85*88447a05SGarrett D'Amore 			channels = le_int(&hdr[p + 10], 2);
86*88447a05SGarrett D'Amore 			/* speed = le_int(&hdr[p + 12], 4); */
87*88447a05SGarrett D'Amore 			nBlockAlign = le_int(&hdr[p + 20], 2);
88*88447a05SGarrett D'Amore 			/* bytes_per_sample = le_int(&hdr[p + 20], 2); */
89*88447a05SGarrett D'Amore 
90*88447a05SGarrett D'Amore 			wSamplesPerBlock = le_int(&hdr[p + 26], 2);
91*88447a05SGarrett D'Amore 			wNumCoeff = le_int(&hdr[p + 28], 2);
92*88447a05SGarrett D'Amore 
93*88447a05SGarrett D'Amore 			x = p + 30;
94*88447a05SGarrett D'Amore 
95*88447a05SGarrett D'Amore 			for (i = 0; i < wNumCoeff; i++) {
96*88447a05SGarrett D'Amore 				coeff[i].coeff1 = (short)le_int(&hdr[x], 2);
97*88447a05SGarrett D'Amore 				x += 2;
98*88447a05SGarrett D'Amore 				coeff[i].coeff2 = (short)le_int(&hdr[x], 2);
99*88447a05SGarrett D'Amore 				x += 2;
100*88447a05SGarrett D'Amore 			}
101*88447a05SGarrett D'Amore 		}
102*88447a05SGarrett D'Amore 
103*88447a05SGarrett D'Amore 		p += n + 8;
104*88447a05SGarrett D'Amore 	}
105*88447a05SGarrett D'Amore 
106*88447a05SGarrett D'Amore 	if (p < l - 16 && memcmp(&hdr[p], "data", 4) == 0) {
107*88447a05SGarrett D'Amore 
108*88447a05SGarrett D'Amore 		dataleft = n = le_int(&hdr[p + 4], 4);
109*88447a05SGarrett D'Amore 		p += 8;
110*88447a05SGarrett D'Amore 
111*88447a05SGarrett D'Amore /*
112*88447a05SGarrett D'Amore  * Playback procedure
113*88447a05SGarrett D'Amore  */
114*88447a05SGarrett D'Amore #define	OUT_SAMPLE(s) {				\
115*88447a05SGarrett D'Amore 		if (s > 32767)			\
116*88447a05SGarrett D'Amore 			s = 32767;		\
117*88447a05SGarrett D'Amore 		else if (s < -32768)		\
118*88447a05SGarrett D'Amore 			s = -32768;		\
119*88447a05SGarrett D'Amore 		outbuf[outp++] = s;		\
120*88447a05SGarrett D'Amore 		n += 2;				\
121*88447a05SGarrett D'Amore 		}
122*88447a05SGarrett D'Amore 
123*88447a05SGarrett D'Amore #define	GETNIBBLE					\
124*88447a05SGarrett D'Amore 		((nib == 0) ?				\
125*88447a05SGarrett D'Amore 		(buf[x + nib++] >> 4) & 0x0f : buf[x++ + --nib] & 0x0f)
126*88447a05SGarrett D'Amore 
127*88447a05SGarrett D'Amore 		outp = 0;
128*88447a05SGarrett D'Amore 
129*88447a05SGarrett D'Amore 		ppp = p;
130*88447a05SGarrett D'Amore 		while (dataleft > nBlockAlign) {
131*88447a05SGarrett D'Amore 			int predictor[2], delta[2], samp1[2], samp2[2];
132*88447a05SGarrett D'Amore 
133*88447a05SGarrett D'Amore 			int x = 0;
134*88447a05SGarrett D'Amore 
135*88447a05SGarrett D'Amore 			(void) memcpy(buf, &inbuf[ppp], nBlockAlign);
136*88447a05SGarrett D'Amore 			ppp += nBlockAlign;
137*88447a05SGarrett D'Amore 			dataleft -= nBlockAlign;
138*88447a05SGarrett D'Amore 
139*88447a05SGarrett D'Amore 			nib = 0;
140*88447a05SGarrett D'Amore 			n = 0;
141*88447a05SGarrett D'Amore 
142*88447a05SGarrett D'Amore 			for (i = 0; i < channels; i++) {
143*88447a05SGarrett D'Amore 				predictor[i] = buf[x];
144*88447a05SGarrett D'Amore 				x++;
145*88447a05SGarrett D'Amore 			}
146*88447a05SGarrett D'Amore 
147*88447a05SGarrett D'Amore 			for (i = 0; i < channels; i++) {
148*88447a05SGarrett D'Amore 				delta[i] = (short)le_int(&buf[x], 2);
149*88447a05SGarrett D'Amore 				x += 2;
150*88447a05SGarrett D'Amore 			}
151*88447a05SGarrett D'Amore 
152*88447a05SGarrett D'Amore 			for (i = 0; i < channels; i++) {
153*88447a05SGarrett D'Amore 				samp1[i] = (short)le_int(&buf[x], 2);
154*88447a05SGarrett D'Amore 				x += 2;
155*88447a05SGarrett D'Amore 				OUT_SAMPLE(samp1[i]);
156*88447a05SGarrett D'Amore 			}
157*88447a05SGarrett D'Amore 
158*88447a05SGarrett D'Amore 			for (i = 0; i < channels; i++) {
159*88447a05SGarrett D'Amore 				samp2[i] = (short)le_int(&buf[x], 2);
160*88447a05SGarrett D'Amore 				x += 2;
161*88447a05SGarrett D'Amore 				OUT_SAMPLE(samp2[i]);
162*88447a05SGarrett D'Amore 			}
163*88447a05SGarrett D'Amore 
164*88447a05SGarrett D'Amore 			while (n < (wSamplesPerBlock * 2 * channels))
165*88447a05SGarrett D'Amore 				for (i = 0; i < channels; i++) {
166*88447a05SGarrett D'Amore 					int pred, new, error_delta, i_delta;
167*88447a05SGarrett D'Amore 
168*88447a05SGarrett D'Amore 					pred = ((samp1[i] *
169*88447a05SGarrett D'Amore 					    coeff[predictor[i]].coeff1)
170*88447a05SGarrett D'Amore 					    + (samp2[i] *
171*88447a05SGarrett D'Amore 					    coeff[predictor[i]].coeff2)) / 256;
172*88447a05SGarrett D'Amore 					i_delta = error_delta = GETNIBBLE;
173*88447a05SGarrett D'Amore 
174*88447a05SGarrett D'Amore 					/* Convert to signed */
175*88447a05SGarrett D'Amore 					if (i_delta & 0x08)
176*88447a05SGarrett D'Amore 						i_delta -= 0x10;
177*88447a05SGarrett D'Amore 
178*88447a05SGarrett D'Amore 					new = pred + (delta[i] * i_delta);
179*88447a05SGarrett D'Amore 					OUT_SAMPLE(new);
180*88447a05SGarrett D'Amore 
181*88447a05SGarrett D'Amore 					delta[i] = delta[i] *
182*88447a05SGarrett D'Amore 					    AdaptionTable[error_delta] / 256;
183*88447a05SGarrett D'Amore 					if (delta[i] < 16)
184*88447a05SGarrett D'Amore 						delta[i] = 16;
185*88447a05SGarrett D'Amore 
186*88447a05SGarrett D'Amore 					samp2[i] = samp1[i];
187*88447a05SGarrett D'Amore 					samp1[i] = new;
188*88447a05SGarrett D'Amore 				}
189*88447a05SGarrett D'Amore 		}
190*88447a05SGarrett D'Amore 
191*88447a05SGarrett D'Amore 	}
192*88447a05SGarrett D'Amore 
193*88447a05SGarrett D'Amore 	return (outp * 2);
194*88447a05SGarrett D'Amore }
195