msm6258.c revision 1.1 1 /* $NetBSD: msm6258.c,v 1.1 2001/05/02 12:53:31 minoura Exp $ */
2
3 /*
4 * Copyright (c) 2001 Tetsuya Isaki. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Tetsuya Isaki.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * OKI MSM6258 ADPCM voice synthesizer codec.
35 */
36
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/select.h>
41 #include <sys/audioio.h>
42
43 #include <dev/audio_if.h>
44 #include <dev/audiovar.h>
45 #include <dev/auconv.h>
46 #include <dev/ic/msm6258var.h>
47
48
49 static inline u_char pcm2adpcm_step(short, short *, char *);
50 static inline void adpcm2pcm_step(u_char, short *, char *);
51
52
53 static int adpcm_estimindex[16] = {
54 125, 375, 625, 875, 1125, 1375, 1625, 1875,
55 -125, -375, -625, -875, -1125, -1375, -1625, -1875
56 };
57
58 static int adpcm_estim[49] = {
59 16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
60 41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
61 107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
62 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
63 724, 796, 875, 963, 1060, 1166, 1282, 1411, 1552
64 };
65
66 static u_char adpcm_estimindex_correct[16] = {
67 -1, -1, -1, -1, 2, 4, 6, 8,
68 -1, -1, -1, -1, 2, 4, 6, 8
69 };
70
71 struct msm6258_codecvar {
72 short mc_amp;
73 char mc_estim;
74 };
75
76 struct msm6258_softc {
77 struct device sc_dev;
78 struct msm6258_codecvar *sc_mc;
79 /* MD vars follow */
80 };
81
82 void *
83 msm6258_codec_init (void)
84 {
85 struct msm6258_codecvar *r;
86
87 r = malloc (sizeof(*r), M_DEVBUF, M_NOWAIT);
88 if (r == 0)
89 return 0;
90 r->mc_amp = r->mc_estim = 0;
91
92 return r;
93 }
94
95 static inline u_char
96 pcm2adpcm_step(short a, short *y, char *x)
97 {
98 int c, d;
99 register unsigned char b;
100
101 a -= *y;
102 d = adpcm_estim[*x];
103 c = a * 4 * 1000;
104 c /= d;
105
106 if (c < 0) {
107 b = (unsigned char)(-c/1000);
108 if (b >= 8)
109 b = 7;
110 b |= 0x08;
111 } else {
112 b = (unsigned char)(c/1000);
113 if (b >= 8)
114 b = 7;
115 }
116
117 *y += (short)(adpcm_estimindex[b] * d / 1000);
118 *x += adpcm_estimindex_correct[b];
119 if (*x < 0)
120 *x = 0;
121 else if (*x > 48)
122 *x = 48;
123 return b;
124 }
125
126 void
127 msm6258_ulinear8_to_adpcm(void *hdl, u_char *p, int cc)
128 {
129 struct msm6258_softc *sc = hdl;
130 struct msm6258_codecvar *mc = sc->sc_mc;
131 char *x = &(mc->mc_estim);
132 short *y = &(mc->mc_amp);
133 register int i;
134 u_char f;
135
136 if ((int) p % 2) printf("p: %p\n", p);
137 for (i = 0; i < cc; i += 2) {
138 f = pcm2adpcm_step(p[i], y, x);
139 p[i] = f + (pcm2adpcm_step(p[i+1], y, x) << 4);
140 p[i+1] = 0;
141 }
142 }
143
144 void
145 msm6258_mulaw_to_adpcm(void *hdl, u_char *p, int cc)
146 {
147 mulaw_to_ulinear8(hdl, p, cc);
148 msm6258_ulinear8_to_adpcm(hdl, p, cc);
149 }
150
151 static inline void
152 adpcm2pcm_step(u_char b, short *y, char *x)
153 {
154 *y += (short)(adpcm_estimindex[b] * adpcm_estim[*x]);
155 *x += adpcm_estimindex_correct[b];
156 if (*x < 0)
157 *x = 0;
158 else if (*x > 48)
159 *x = 48;
160 }
161
162 /* ADPCM stream must be converted in order. */
163 u_char tmpbuf[AU_RING_SIZE]; /* XXX */
164
165 void
166 msm6258_adpcm_to_ulinear8(void *hdl, u_char *p, int cc)
167 {
168 struct msm6258_softc *sc = hdl;
169 struct msm6258_codecvar *mc = sc->sc_mc;
170 char *x = &(mc->mc_estim);
171 short *y = &(mc->mc_amp);
172 u_char a, b;
173 int i;
174
175 for (i = 0; i < cc;) {
176 a = *p;
177 p++;
178 b = a & 0x0f;
179 adpcm2pcm_step(b, y, x);
180 tmpbuf[i++] = *y;
181 b = a >> 4;
182 adpcm2pcm_step(b, y, x);
183 tmpbuf[i++] = *y;
184 }
185 memcpy(p, tmpbuf, cc*2);
186 }
187
188 void
189 msm6258_adpcm_to_mulaw(void *hdl, u_char *p, int cc)
190 {
191 msm6258_adpcm_to_ulinear8(hdl, p, cc);
192 ulinear8_to_mulaw(hdl, p, cc*2);
193 }
194