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