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