dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / audio / audiotest / wavedata.c
blob94a05f51ea8f149b6525440d7fcc7174f40130cc
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (C) 4Front Technologies 1996-2008.
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 * Purpose: Test sounds for osstest
30 * Nodoc:
33 #include <string.h>
35 #include "wavedata.h"
37 static int
38 le_int(const unsigned char *p, int l)
40 int i, val;
42 val = 0;
44 for (i = l - 1; i >= 0; i--) {
45 val = (val << 8) | p[i];
48 return (val);
51 int
52 uncompress_wave(short *outbuf)
54 #define WAVE_FORMAT_ADPCM 0x0002
56 int i, n, dataleft, x, l = sizeof (inbuf);
57 const unsigned char *hdr = inbuf;
58 typedef struct {
59 int coeff1, coeff2;
61 adpcm_coeff;
63 adpcm_coeff coeff[32];
64 static int AdaptionTable[] = { 230, 230, 230, 230, 307, 409, 512, 614,
65 768, 614, 512, 409, 307, 230, 230, 230
68 unsigned char buf[4096];
70 int channels = 1;
71 int p = 12, outp = 0;
72 int nBlockAlign = 2048;
73 int wSamplesPerBlock = 2036, wNumCoeff = 7;
74 int nib;
75 int ppp;
77 /* filelen = le_int(&hdr[4], 4); */
79 while (p < l - 16 && memcmp(&hdr[p], "data", 4) != 0) {
80 n = le_int(&hdr[p + 4], 4);
82 if (memcmp(&hdr[p], "fmt ", 4) == 0) {
84 /* fmt = le_int(&hdr[p + 8], 2); */
85 channels = le_int(&hdr[p + 10], 2);
86 /* speed = le_int(&hdr[p + 12], 4); */
87 nBlockAlign = le_int(&hdr[p + 20], 2);
88 /* bytes_per_sample = le_int(&hdr[p + 20], 2); */
90 wSamplesPerBlock = le_int(&hdr[p + 26], 2);
91 wNumCoeff = le_int(&hdr[p + 28], 2);
93 x = p + 30;
95 for (i = 0; i < wNumCoeff; i++) {
96 coeff[i].coeff1 = (short)le_int(&hdr[x], 2);
97 x += 2;
98 coeff[i].coeff2 = (short)le_int(&hdr[x], 2);
99 x += 2;
103 p += n + 8;
106 if (p < l - 16 && memcmp(&hdr[p], "data", 4) == 0) {
108 dataleft = n = le_int(&hdr[p + 4], 4);
109 p += 8;
112 * Playback procedure
114 #define OUT_SAMPLE(s) { \
115 if (s > 32767) \
116 s = 32767; \
117 else if (s < -32768) \
118 s = -32768; \
119 outbuf[outp++] = s; \
120 n += 2; \
123 #define GETNIBBLE \
124 ((nib == 0) ? \
125 (buf[x + nib++] >> 4) & 0x0f : buf[x++ + --nib] & 0x0f)
127 outp = 0;
129 ppp = p;
130 while (dataleft > nBlockAlign) {
131 int predictor[2], delta[2], samp1[2], samp2[2];
133 int x = 0;
135 (void) memcpy(buf, &inbuf[ppp], nBlockAlign);
136 ppp += nBlockAlign;
137 dataleft -= nBlockAlign;
139 nib = 0;
140 n = 0;
142 for (i = 0; i < channels; i++) {
143 predictor[i] = buf[x];
144 x++;
147 for (i = 0; i < channels; i++) {
148 delta[i] = (short)le_int(&buf[x], 2);
149 x += 2;
152 for (i = 0; i < channels; i++) {
153 samp1[i] = (short)le_int(&buf[x], 2);
154 x += 2;
155 OUT_SAMPLE(samp1[i]);
158 for (i = 0; i < channels; i++) {
159 samp2[i] = (short)le_int(&buf[x], 2);
160 x += 2;
161 OUT_SAMPLE(samp2[i]);
164 while (n < (wSamplesPerBlock * 2 * channels))
165 for (i = 0; i < channels; i++) {
166 int pred, new, error_delta, i_delta;
168 pred = ((samp1[i] *
169 coeff[predictor[i]].coeff1)
170 + (samp2[i] *
171 coeff[predictor[i]].coeff2)) / 256;
172 i_delta = error_delta = GETNIBBLE;
174 /* Convert to signed */
175 if (i_delta & 0x08)
176 i_delta -= 0x10;
178 new = pred + (delta[i] * i_delta);
179 OUT_SAMPLE(new);
181 delta[i] = delta[i] *
182 AdaptionTable[error_delta] / 256;
183 if (delta[i] < 16)
184 delta[i] = 16;
186 samp2[i] = samp1[i];
187 samp1[i] = new;
193 return (outp * 2);