Annotation of driverkit/libDriver/Kernel/audio_peak.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_peak.c
                     26:  *
                     27:  * Copyright (c) 1992, NeXT Computer, Inc.  All rights reserved.
                     28:  *
                     29:  *     Peak calculation routines.
                     30:  *
                     31:  * HISTORY
                     32:  *     12/08/92/mtm    Original coding from 68k version.
                     33:  */
                     34: 
                     35: #import "audio_peak.h"
                     36: #import "audio_mulaw.h"
                     37: 
                     38: /*
                     39:  * Calculate peaks in a mulaw 8-bit buffer.
                     40:  * Returns peak as 16-bit linear int.
                     41:  */
                     42: void audio_mulaw8_peak(u_int chans, unsigned char *buf, u_int count,
                     43:                 u_int *peak_left, u_int *peak_right)
                     44: {
                     45:     int i;
                     46:     short samp;
                     47: 
                     48:     *peak_left = *peak_right = 0;
                     49:     if (chans == 1) {
                     50:        for (i = 0; i < count/4; i++) {
                     51:            samp = audio_muLaw[*buf++];
                     52:            if (samp < 0)
                     53:                samp = -samp;
                     54:            if (samp > *peak_left)
                     55:                *peak_left = samp;
                     56:            buf += 3;
                     57:        }
                     58:        *peak_right = *peak_left;
                     59:     } else {
                     60:        for (i = 0; i < count/4; i++) {
                     61:            samp = audio_muLaw[*buf++];
                     62:            if (samp < 0)
                     63:                samp = -samp;
                     64:            if (samp > *peak_left)
                     65:                *peak_left = samp;
                     66:            buf++;
                     67:            samp = audio_muLaw[*buf++];
                     68:            if (samp < 0)
                     69:                samp = -samp;
                     70:            if (samp > *peak_right)
                     71:                *peak_right = samp;
                     72:            buf++;
                     73:        }
                     74:     }
                     75: }
                     76: 
                     77: /*
                     78:  * Calculate peaks in a linear 16-bit buffer.
                     79:  */
                     80: void audio_linear16_peak(u_int chans, short *buf, u_int count,
                     81:                         u_int *peak_left, u_int *peak_right)
                     82: {
                     83:     int i;
                     84:     short samp;
                     85: 
                     86:     *peak_left = *peak_right = 0;
                     87:     if (chans == 1) {
                     88:        for (i = 0; i < count/2; i++) {
                     89:            samp = *buf++;
                     90:            if (samp < 0)
                     91:                samp = -samp;
                     92:            if (samp > *peak_left)
                     93:                *peak_left = samp;
                     94:            buf++;
                     95:        }
                     96:        *peak_right = *peak_left;
                     97:     } else {
                     98:        for (i = 0; i < count/2; i++) {
                     99:            samp = *buf++;
                    100:            if (samp < 0)
                    101:                samp = -samp;
                    102:            if (samp > *peak_left)
                    103:                *peak_left = samp;
                    104:            samp = *buf++;
                    105:            if (samp < 0)
                    106:                samp = -samp;
                    107:            if (samp > *peak_right)
                    108:                *peak_right = samp;
                    109:        }
                    110:     }
                    111: }
                    112: /*
                    113:  * Calculate peaks in a linear 8-bit buffer.
                    114:  * Returns peak as 16-bit linear int.
                    115:  */
                    116: void audio_linear8_peak(u_int chans, char *buf, u_int count, u_int *peak_left,
                    117:                        u_int *peak_right)
                    118: {
                    119:     int i;
                    120:     char samp;
                    121: 
                    122:     *peak_left = *peak_right = 0;
                    123:     if (chans == 1) {
                    124:        for (i = 0; i < count/2; i++) {
                    125:            samp = *buf++;
                    126:            /* FIXME: iff devIsUnary */
                    127:            /* convert to 2's comp. by xor'ing the high bit */
                    128:            samp = (samp ^ 0x80) | (samp & 0x7f);
                    129:            if (samp < 0)
                    130:                samp = -samp;
                    131:            if (samp > *peak_left)
                    132:                *peak_left = samp;
                    133:            buf++;
                    134:        }
                    135:        *peak_right = *peak_left;
                    136:     } else {
                    137:        for (i = 0; i < count/2; i++) {
                    138:            samp = *buf++;
                    139:            /* FIXME: iff devIsUnary */
                    140:            /* convert to 2's comp. by xor'ing the high bit */
                    141:            samp = (samp ^ 0x80) | (samp & 0x7f);
                    142:            if (samp < 0)
                    143:                samp = -samp;
                    144:            if (samp > *peak_left)
                    145:                *peak_left = samp;
                    146:            samp = *buf++;
                    147:            /* FIXME: iff devIsUnary */
                    148:            /* convert to 2's comp. by xor'ing the high bit */
                    149:            samp = (samp ^ 0x80) | (samp & 0x7f);
                    150:            if (samp < 0)
                    151:                samp = -samp;
                    152:            if (samp > *peak_right)
                    153:                *peak_right = samp;
                    154:        }
                    155:     }
                    156:     *peak_right <<= 8;
                    157:     *peak_left <<= 8;
                    158: }
                    159: 
                    160: /*
                    161:  * Clear peaks in peak buffer.
                    162:  */
                    163: void audio_clear_peaks(u_int *peak_buf, u_int count)
                    164: {
                    165:     while (count--)
                    166:        *peak_buf++ = 0;
                    167: }
                    168: 
                    169: /*
                    170:  * Return max peak in peak buffer.
                    171:  */
                    172: u_int audio_max_peak(u_int *peak_buf, u_int count)
                    173: {
                    174:     u_int peak, max = 0;
                    175: 
                    176:     while (count--) {
                    177:        peak = *peak_buf++;
                    178:        if (peak > max)
                    179:            max = peak;
                    180:     }
                    181:     return max;
                    182: }
                    183: 
                    184: /*
                    185:  * Add a peak to circular peak buffer and bump cur index.
                    186:  */
                    187: void audio_add_peak(u_int *peak_buf, u_int peak, u_int *cur, u_int count)
                    188: {
                    189:     if (count == 0)
                    190:        return;
                    191:     peak_buf[*cur] = peak;
                    192:     *cur = *cur + 1;
                    193:     if (*cur == count)
                    194:        *cur = 0;
                    195: }

unix.superglobalmegacorp.com

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