msm6258.c revision 1.25.2.1 1 1.25.2.1 isaki /* $NetBSD: msm6258.c,v 1.25.2.1 2019/04/21 14:00:19 isaki 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 *
15 1.1 minoura * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 minoura * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 minoura * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 minoura * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 minoura * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 minoura * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 minoura * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 minoura * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 minoura * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 minoura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 minoura * SUCH DAMAGE.
26 1.1 minoura */
27 1.1 minoura
28 1.1 minoura /*
29 1.1 minoura * OKI MSM6258 ADPCM voice synthesizer codec.
30 1.1 minoura */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.25.2.1 isaki __KERNEL_RCSID(0, "$NetBSD: msm6258.c,v 1.25.2.1 2019/04/21 14:00:19 isaki Exp $");
34 1.1 minoura
35 1.1 minoura #include <sys/systm.h>
36 1.1 minoura #include <sys/device.h>
37 1.17 jmcneill #include <sys/kmem.h>
38 1.1 minoura #include <sys/select.h>
39 1.1 minoura #include <sys/audioio.h>
40 1.1 minoura
41 1.1 minoura #include <dev/audio_if.h>
42 1.1 minoura #include <dev/audiovar.h>
43 1.1 minoura #include <dev/ic/msm6258var.h>
44 1.1 minoura
45 1.15 perry static inline uint8_t pcm2adpcm_step(struct msm6258_codecvar *, int16_t);
46 1.15 perry static inline int16_t adpcm2pcm_step(struct msm6258_codecvar *, uint8_t);
47 1.1 minoura
48 1.12 kent static const int adpcm_estimindex[16] = {
49 1.9 isaki 2, 6, 10, 14, 18, 22, 26, 30,
50 1.9 isaki -2, -6, -10, -14, -18, -22, -26, -30
51 1.1 minoura };
52 1.1 minoura
53 1.12 kent static const int adpcm_estim[49] = {
54 1.1 minoura 16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
55 1.1 minoura 41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
56 1.1 minoura 107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
57 1.1 minoura 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
58 1.10 isaki 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
59 1.1 minoura };
60 1.1 minoura
61 1.12 kent static const int adpcm_estimstep[16] = {
62 1.1 minoura -1, -1, -1, -1, 2, 4, 6, 8,
63 1.1 minoura -1, -1, -1, -1, 2, 4, 6, 8
64 1.1 minoura };
65 1.1 minoura
66 1.9 isaki /*
67 1.9 isaki * signed 16bit linear PCM -> OkiADPCM
68 1.9 isaki */
69 1.15 perry static inline uint8_t
70 1.12 kent pcm2adpcm_step(struct msm6258_codecvar *mc, int16_t a)
71 1.9 isaki {
72 1.9 isaki int estim = (int)mc->mc_estim;
73 1.9 isaki int df;
74 1.9 isaki short dl, c;
75 1.12 kent uint8_t b;
76 1.12 kent uint8_t s;
77 1.9 isaki
78 1.9 isaki df = a - mc->mc_amp;
79 1.9 isaki dl = adpcm_estim[estim];
80 1.9 isaki c = (df / 16) * 8 / dl;
81 1.9 isaki if (df < 0) {
82 1.9 isaki b = (unsigned char)(-c) / 2;
83 1.9 isaki s = 0x08;
84 1.9 isaki } else {
85 1.9 isaki b = (unsigned char)(c) / 2;
86 1.9 isaki s = 0;
87 1.9 isaki }
88 1.9 isaki if (b > 7)
89 1.9 isaki b = 7;
90 1.9 isaki s |= b;
91 1.9 isaki mc->mc_amp += (short)(adpcm_estimindex[(int)s] * dl);
92 1.9 isaki estim += adpcm_estimstep[b];
93 1.9 isaki if (estim < 0)
94 1.9 isaki estim = 0;
95 1.9 isaki else if (estim > 48)
96 1.9 isaki estim = 48;
97 1.9 isaki
98 1.9 isaki mc->mc_estim = estim;
99 1.9 isaki return s;
100 1.9 isaki }
101 1.9 isaki
102 1.25.2.1 isaki void
103 1.25.2.1 isaki msm6258_internal_to_adpcm(audio_filter_arg_t *arg)
104 1.12 kent {
105 1.12 kent struct msm6258_codecvar *mc;
106 1.25.2.1 isaki const aint_t *src;
107 1.25.2.1 isaki uint8_t *dst;
108 1.25.2.1 isaki u_int sample_count;
109 1.25.2.1 isaki u_int i;
110 1.25.2.1 isaki
111 1.25.2.1 isaki KASSERT((arg->count & 1) == 0);
112 1.25.2.1 isaki
113 1.25.2.1 isaki mc = arg->context;
114 1.25.2.1 isaki src = arg->src;
115 1.25.2.1 isaki dst = arg->dst;
116 1.25.2.1 isaki sample_count = arg->count * arg->srcfmt->channels;
117 1.25.2.1 isaki for (i = 0; i < sample_count / 2; i++) {
118 1.25.2.1 isaki aint_t s;
119 1.25.2.1 isaki uint8_t f;
120 1.25.2.1 isaki
121 1.25.2.1 isaki s = *src++;
122 1.25.2.1 isaki s >>= AUDIO_INTERNAL_BITS - 16;
123 1.25.2.1 isaki f = pcm2adpcm_step(mc, s);
124 1.25.2.1 isaki
125 1.25.2.1 isaki s = *src++;
126 1.25.2.1 isaki s >>= AUDIO_INTERNAL_BITS - 16;
127 1.25.2.1 isaki f |= pcm2adpcm_step(mc, s) << 4;
128 1.1 minoura
129 1.25.2.1 isaki *dst++ = (uint8_t)f;
130 1.18 isaki }
131 1.1 minoura }
132 1.1 minoura
133 1.25.2.1 isaki
134 1.9 isaki /*
135 1.9 isaki * OkiADPCM -> signed 16bit linear PCM
136 1.9 isaki */
137 1.15 perry static inline int16_t
138 1.12 kent adpcm2pcm_step(struct msm6258_codecvar *mc, uint8_t b)
139 1.1 minoura {
140 1.9 isaki int estim = (int)mc->mc_estim;
141 1.7 isaki
142 1.9 isaki mc->mc_amp += adpcm_estim[estim] * adpcm_estimindex[b];
143 1.9 isaki estim += adpcm_estimstep[b];
144 1.7 isaki
145 1.7 isaki if (estim < 0)
146 1.7 isaki estim = 0;
147 1.9 isaki else if (estim > 48)
148 1.7 isaki estim = 48;
149 1.9 isaki
150 1.9 isaki mc->mc_estim = estim;
151 1.9 isaki
152 1.9 isaki return mc->mc_amp;
153 1.1 minoura }
154 1.1 minoura
155 1.25.2.1 isaki void
156 1.25.2.1 isaki msm6258_adpcm_to_internal(audio_filter_arg_t *arg)
157 1.9 isaki {
158 1.12 kent struct msm6258_codecvar *mc;
159 1.25.2.1 isaki const uint8_t *src;
160 1.25.2.1 isaki aint_t *dst;
161 1.25.2.1 isaki u_int sample_count;
162 1.25.2.1 isaki u_int i;
163 1.25.2.1 isaki
164 1.25.2.1 isaki KASSERT((arg->count & 1) == 0);
165 1.25.2.1 isaki
166 1.25.2.1 isaki mc = arg->context;
167 1.25.2.1 isaki src = arg->src;
168 1.25.2.1 isaki dst = arg->dst;
169 1.25.2.1 isaki sample_count = arg->count * arg->srcfmt->channels;
170 1.25.2.1 isaki for (i = 0; i < sample_count / 2; i++) {
171 1.25.2.1 isaki uint8_t a = *src++;
172 1.25.2.1 isaki aint_t s;
173 1.25.2.1 isaki
174 1.25.2.1 isaki s = adpcm2pcm_step(mc, a & 0x0f);
175 1.25.2.1 isaki s <<= AUDIO_INTERNAL_BITS - 16;
176 1.25.2.1 isaki *dst++ = s;
177 1.25.2.1 isaki
178 1.25.2.1 isaki s = adpcm2pcm_step(mc, a >> 4);
179 1.25.2.1 isaki s <<= AUDIO_INTERNAL_BITS - 16;
180 1.25.2.1 isaki *dst++ = s;
181 1.18 isaki }
182 1.1 minoura }
183