Annotation of driverkit/libDriver/Kernel/audio_mulaw.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: /*
                     25:  * audio_mulaw.c
                     26:  *
                     27:  * Copyright (c) 1992, NeXT Computer, Inc.  All rights reserved.
                     28:  *
                     29:  *     Mulaw conversion.
                     30:  *
                     31:  * HISTORY
                     32:  *     05/20/93/mtm    Original coding from sound library.
                     33:  */
                     34: 
                     35: #import <driverkit/generalFuncs.h>
                     36: 
                     37: /* FIXME: no prototype for kernel qsort */
                     38: extern qsort();
                     39: 
                     40: /*
                     41:  * Mulaw to 16 bit linear conversion table.
                     42:  */
                     43: #define        MULAWTABLEN     256
                     44: const short audio_muLaw[MULAWTABLEN] = {
                     45:  0x8284, 0x8684, 0x8a84, 0x8e84, 0x9284, 0x9684, 0x9a84, 0x9e84, 
                     46:  0xa284, 0xa684, 0xaa84, 0xae84, 0xb284, 0xb684, 0xba84, 0xbe84, 
                     47:  0xc184, 0xc384, 0xc584, 0xc784, 0xc984, 0xcb84, 0xcd84, 0xcf84, 
                     48:  0xd184, 0xd384, 0xd584, 0xd784, 0xd984, 0xdb84, 0xdd84, 0xdf84, 
                     49:  0xe104, 0xe204, 0xe304, 0xe404, 0xe504, 0xe604, 0xe704, 0xe804, 
                     50:  0xe904, 0xea04, 0xeb04, 0xec04, 0xed04, 0xee04, 0xef04, 0xf004, 
                     51:  0xf0c4, 0xf144, 0xf1c4, 0xf244, 0xf2c4, 0xf344, 0xf3c4, 0xf444, 
                     52:  0xf4c4, 0xf544, 0xf5c4, 0xf644, 0xf6c4, 0xf744, 0xf7c4, 0xf844, 
                     53:  0xf8a4, 0xf8e4, 0xf924, 0xf964, 0xf9a4, 0xf9e4, 0xfa24, 0xfa64, 
                     54:  0xfaa4, 0xfae4, 0xfb24, 0xfb64, 0xfba4, 0xfbe4, 0xfc24, 0xfc64, 
                     55:  0xfc94, 0xfcb4, 0xfcd4, 0xfcf4, 0xfd14, 0xfd34, 0xfd54, 0xfd74, 
                     56:  0xfd94, 0xfdb4, 0xfdd4, 0xfdf4, 0xfe14, 0xfe34, 0xfe54, 0xfe74, 
                     57:  0xfe8c, 0xfe9c, 0xfeac, 0xfebc, 0xfecc, 0xfedc, 0xfeec, 0xfefc, 
                     58:  0xff0c, 0xff1c, 0xff2c, 0xff3c, 0xff4c, 0xff5c, 0xff6c, 0xff7c, 
                     59:  0xff88, 0xff90, 0xff98, 0xffa0, 0xffa8, 0xffb0, 0xffb8, 0xffc0, 
                     60:  0xffc8, 0xffd0, 0xffd8, 0xffe0, 0xffe8, 0xfff0, 0xfff8, 0x0, 
                     61:  0x7d7c, 0x797c, 0x757c, 0x717c, 0x6d7c, 0x697c, 0x657c, 0x617c, 
                     62:  0x5d7c, 0x597c, 0x557c, 0x517c, 0x4d7c, 0x497c, 0x457c, 0x417c, 
                     63:  0x3e7c, 0x3c7c, 0x3a7c, 0x387c, 0x367c, 0x347c, 0x327c, 0x307c, 
                     64:  0x2e7c, 0x2c7c, 0x2a7c, 0x287c, 0x267c, 0x247c, 0x227c, 0x207c, 
                     65:  0x1efc, 0x1dfc, 0x1cfc, 0x1bfc, 0x1afc, 0x19fc, 0x18fc, 0x17fc, 
                     66:  0x16fc, 0x15fc, 0x14fc, 0x13fc, 0x12fc, 0x11fc, 0x10fc, 0xffc, 
                     67:  0xf3c, 0xebc, 0xe3c, 0xdbc, 0xd3c, 0xcbc, 0xc3c, 0xbbc, 
                     68:  0xb3c, 0xabc, 0xa3c, 0x9bc, 0x93c, 0x8bc, 0x83c, 0x7bc, 
                     69:  0x75c, 0x71c, 0x6dc, 0x69c, 0x65c, 0x61c, 0x5dc, 0x59c, 
                     70:  0x55c, 0x51c, 0x4dc, 0x49c, 0x45c, 0x41c, 0x3dc, 0x39c, 
                     71:  0x36c, 0x34c, 0x32c, 0x30c, 0x2ec, 0x2cc, 0x2ac, 0x28c, 
                     72:  0x26c, 0x24c, 0x22c, 0x20c, 0x1ec, 0x1cc, 0x1ac, 0x18c, 
                     73:  0x174, 0x164, 0x154, 0x144, 0x134, 0x124, 0x114, 0x104, 
                     74:  0xf4, 0xe4, 0xd4, 0xc4, 0xb4, 0xa4, 0x94, 0x84, 
                     75:  0x78, 0x70, 0x68, 0x60, 0x58, 0x50, 0x48, 0x40, 
                     76:  0x38, 0x30, 0x28, 0x20, 0x18, 0x10, 0x8, 0x0
                     77: };
                     78: 
                     79: #define IMULAWOFFSET   8192
                     80: #define        IMULAWTABLEN    16384
                     81: #define IMULAWMASK     16383
                     82: 
                     83: static unsigned char *iMuLaw = 0;
                     84: 
                     85: struct mu {
                     86:     short mu, linear;
                     87: };
                     88: 
                     89: static int compar(struct mu **p1, struct mu **p2)
                     90: {
                     91:     if ((*p1)->linear > (*p2)->linear) return 1;
                     92:     else if ((*p1)->linear == (*p2)->linear) return 0;
                     93:     else return -1;
                     94: }
                     95: 
                     96: void audio_makeIMuLawTab(void)
                     97: {
                     98:     int i,j,k, d1, d2;
                     99:     struct mu *mutab[256], mus[256];
                    100: 
                    101:     if (iMuLaw)
                    102:        return;
                    103: 
                    104:     iMuLaw = (unsigned char *)IOMalloc(IMULAWTABLEN * sizeof(unsigned char));
                    105: 
                    106:     for (i = 0; i < 256; ++i)
                    107:     {
                    108:        mutab[i] = &mus[i];
                    109:        mus[i].mu = i;
                    110:        mus[i].linear = audio_muLaw[i] >> 2;
                    111:     }
                    112:     qsort(mutab, 256, sizeof(struct mu *), compar);
                    113: 
                    114:     for (i = 0, j = 0, k = -8192; i < 16384; ++i, ++k)
                    115:     {
                    116:        if (j < 255)
                    117:        {
                    118:            d1 = k - mutab[j]->linear;
                    119:            d2 = mutab[j+1]->linear - k;
                    120:            if (d1 > 0 && d1 > d2)
                    121:                ++j;
                    122:        }
                    123:        iMuLaw[i] = mutab[j]->mu;
                    124:     }
                    125: }
                    126: 
                    127: void audio_freeIMuLawTab(void)
                    128: {
                    129:     if (iMuLaw)
                    130:        IOFree(iMuLaw, IMULAWTABLEN * sizeof(unsigned char));
                    131: }
                    132: 
                    133: unsigned char audio_shortToMulaw(short p)
                    134: {
                    135:     p >>= 2; /* scale input; table size is for 14 bit number! */
                    136: 
                    137:     if (p >= (IMULAWTABLEN/2))
                    138:        return iMuLaw[IMULAWTABLEN-1];
                    139:     if (p < (-IMULAWTABLEN/2))
                    140:        return iMuLaw[0];
                    141:     else
                    142:        return iMuLaw[p + IMULAWTABLEN/2];
                    143: }
                    144: 
                    145: unsigned char audio_byteToMulaw(char p)
                    146: {
                    147:     /* FIXME: shift up to 14 bits?? */
                    148: 
                    149:     return iMuLaw[p + IMULAWTABLEN/2];
                    150: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.