Home | History | Annotate | Line # | Download | only in ic
msm6258.c revision 1.1
      1  1.1  minoura /*	$NetBSD: msm6258.c,v 1.1 2001/05/02 12:53:31 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.1  minoura #include <dev/ic/msm6258var.h>
     47  1.1  minoura 
     48  1.1  minoura 
     49  1.1  minoura static inline u_char pcm2adpcm_step(short, short *, char *);
     50  1.1  minoura static inline void adpcm2pcm_step(u_char, short *, char *);
     51  1.1  minoura 
     52  1.1  minoura 
     53  1.1  minoura static int adpcm_estimindex[16] = {
     54  1.1  minoura 	 125,  375,  625,  875,  1125,  1375,  1625,  1875,
     55  1.1  minoura 	-125, -375, -625, -875, -1125, -1375, -1625, -1875
     56  1.1  minoura };
     57  1.1  minoura 
     58  1.1  minoura static int adpcm_estim[49] = {
     59  1.1  minoura 	 16,  17,  19,  21,  23,  25,  28,  31,  34,  37,
     60  1.1  minoura 	 41,  45,  50,  55,  60,  66,  73,  80,  88,  97,
     61  1.1  minoura 	107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
     62  1.1  minoura 	279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
     63  1.1  minoura 	724, 796, 875, 963, 1060, 1166, 1282, 1411, 1552
     64  1.1  minoura };
     65  1.1  minoura 
     66  1.1  minoura static u_char adpcm_estimindex_correct[16] = {
     67  1.1  minoura 	-1, -1, -1, -1, 2, 4, 6, 8,
     68  1.1  minoura 	-1, -1, -1, -1, 2, 4, 6, 8
     69  1.1  minoura };
     70  1.1  minoura 
     71  1.1  minoura struct msm6258_codecvar {
     72  1.1  minoura 	short	mc_amp;
     73  1.1  minoura 	char	mc_estim;
     74  1.1  minoura };
     75  1.1  minoura 
     76  1.1  minoura struct msm6258_softc {
     77  1.1  minoura 	struct device sc_dev;
     78  1.1  minoura 	struct msm6258_codecvar *sc_mc;
     79  1.1  minoura 	/* MD vars follow */
     80  1.1  minoura };
     81  1.1  minoura 
     82  1.1  minoura void *
     83  1.1  minoura msm6258_codec_init (void)
     84  1.1  minoura {
     85  1.1  minoura 	struct msm6258_codecvar *r;
     86  1.1  minoura 
     87  1.1  minoura 	r = malloc (sizeof(*r), M_DEVBUF, M_NOWAIT);
     88  1.1  minoura 	if (r == 0)
     89  1.1  minoura 		return 0;
     90  1.1  minoura 	r->mc_amp = r->mc_estim = 0;
     91  1.1  minoura 
     92  1.1  minoura 	return r;
     93  1.1  minoura }
     94  1.1  minoura 
     95  1.1  minoura static inline u_char
     96  1.1  minoura pcm2adpcm_step(short a, short *y, char *x)
     97  1.1  minoura {
     98  1.1  minoura 	int c, d;
     99  1.1  minoura 	register unsigned char b;
    100  1.1  minoura 
    101  1.1  minoura 	a -= *y;
    102  1.1  minoura 	d = adpcm_estim[*x];
    103  1.1  minoura 	c = a * 4 * 1000;
    104  1.1  minoura 	c /= d;
    105  1.1  minoura 
    106  1.1  minoura 	if (c < 0) {
    107  1.1  minoura 		b = (unsigned char)(-c/1000);
    108  1.1  minoura 		if (b >= 8)
    109  1.1  minoura 			b = 7;
    110  1.1  minoura 		b |= 0x08;
    111  1.1  minoura 	} else {
    112  1.1  minoura 		b = (unsigned char)(c/1000);
    113  1.1  minoura 		if (b >= 8)
    114  1.1  minoura 			b = 7;
    115  1.1  minoura 	}
    116  1.1  minoura 
    117  1.1  minoura 	*y += (short)(adpcm_estimindex[b] * d / 1000);
    118  1.1  minoura 	*x += adpcm_estimindex_correct[b];
    119  1.1  minoura 	if (*x < 0)
    120  1.1  minoura 		*x = 0;
    121  1.1  minoura 	else if (*x > 48)
    122  1.1  minoura 		*x = 48;
    123  1.1  minoura 	return b;
    124  1.1  minoura }
    125  1.1  minoura 
    126  1.1  minoura void
    127  1.1  minoura msm6258_ulinear8_to_adpcm(void *hdl, u_char *p, int cc)
    128  1.1  minoura {
    129  1.1  minoura 	struct msm6258_softc *sc = hdl;
    130  1.1  minoura 	struct msm6258_codecvar *mc = sc->sc_mc;
    131  1.1  minoura 	char *x = &(mc->mc_estim);
    132  1.1  minoura 	short *y = &(mc->mc_amp);
    133  1.1  minoura 	register int i;
    134  1.1  minoura 	u_char f;
    135  1.1  minoura 
    136  1.1  minoura if ((int) p % 2) printf("p: %p\n", p);
    137  1.1  minoura 	for (i = 0; i < cc; i += 2) {
    138  1.1  minoura 		f = pcm2adpcm_step(p[i], y, x);
    139  1.1  minoura 		p[i] = f + (pcm2adpcm_step(p[i+1], y, x) << 4);
    140  1.1  minoura 		p[i+1] = 0;
    141  1.1  minoura 	}
    142  1.1  minoura }
    143  1.1  minoura 
    144  1.1  minoura void
    145  1.1  minoura msm6258_mulaw_to_adpcm(void *hdl, u_char *p, int cc)
    146  1.1  minoura {
    147  1.1  minoura 	mulaw_to_ulinear8(hdl, p, cc);
    148  1.1  minoura 	msm6258_ulinear8_to_adpcm(hdl, p, cc);
    149  1.1  minoura }
    150  1.1  minoura 
    151  1.1  minoura static inline void
    152  1.1  minoura adpcm2pcm_step(u_char b, short *y, char *x)
    153  1.1  minoura {
    154  1.1  minoura 	*y += (short)(adpcm_estimindex[b] * adpcm_estim[*x]);
    155  1.1  minoura 	*x += adpcm_estimindex_correct[b];
    156  1.1  minoura 	if (*x < 0)
    157  1.1  minoura 		*x = 0;
    158  1.1  minoura 	else if (*x > 48)
    159  1.1  minoura 		*x = 48;
    160  1.1  minoura }
    161  1.1  minoura 
    162  1.1  minoura /* ADPCM stream must be converted in order. */
    163  1.1  minoura u_char tmpbuf[AU_RING_SIZE]; /* XXX */
    164  1.1  minoura 
    165  1.1  minoura void
    166  1.1  minoura msm6258_adpcm_to_ulinear8(void *hdl, u_char *p, int cc)
    167  1.1  minoura {
    168  1.1  minoura 	struct msm6258_softc *sc = hdl;
    169  1.1  minoura 	struct msm6258_codecvar *mc = sc->sc_mc;
    170  1.1  minoura 	char *x = &(mc->mc_estim);
    171  1.1  minoura 	short *y = &(mc->mc_amp);
    172  1.1  minoura 	u_char a, b;
    173  1.1  minoura 	int i;
    174  1.1  minoura 
    175  1.1  minoura 	for (i = 0; i < cc;) {
    176  1.1  minoura 		a = *p;
    177  1.1  minoura 		p++;
    178  1.1  minoura 		b = a & 0x0f;
    179  1.1  minoura 		adpcm2pcm_step(b, y, x);
    180  1.1  minoura 		tmpbuf[i++] = *y;
    181  1.1  minoura 		b = a >> 4;
    182  1.1  minoura 		adpcm2pcm_step(b, y, x);
    183  1.1  minoura 		tmpbuf[i++] = *y;
    184  1.1  minoura 	}
    185  1.1  minoura 	memcpy(p, tmpbuf, cc*2);
    186  1.1  minoura }
    187  1.1  minoura 
    188  1.1  minoura void
    189  1.1  minoura msm6258_adpcm_to_mulaw(void *hdl, u_char *p, int cc)
    190  1.1  minoura {
    191  1.1  minoura 	msm6258_adpcm_to_ulinear8(hdl, p, cc);
    192  1.1  minoura 	ulinear8_to_mulaw(hdl, p, cc*2);
    193  1.1  minoura }
    194