Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:47 2009

## Asterisk developer's documentation

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Directories

# alaw.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2005, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * See http://www.asterisk.org for more information about
    00009  * the Asterisk project. Please do not directly contact
    00010  * any of the maintainers of this project for assistance;
    00011  * the project provides a web site, mailing lists and IRC
    00012  * channels for your use.
    00013  *
    00014  * This program is free software, distributed under the terms of
    00015  * the GNU General Public License Version 2. See the LICENSE file
    00016  * at the top of the source tree.
    00017  */
    00018
    00019 /*! \file
    00020  *
    00021  * \brief u-Law to Signed linear conversion
    00022  *
    00023  */
    00024
    00025 #include "asterisk.h"
    00026
    00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00028
    00029 #include "asterisk/alaw.h"
    00030
    00031 #define AMI_MASK 0x55
    00032
    00033 static inline unsigned char linear2alaw (short int linear)
    00034 {
    00035     int mask;
    00036     int seg;
    00037     int pcm_val;
    00038     static int seg_end[8] =
    00039     {
    00040          0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
    00041     };
    00042
    00043     pcm_val = linear;
    00044     if (pcm_val >= 0)
    00045     {
    00046         /* Sign (7th) bit = 1 */
    00047         mask = AMI_MASK | 0x80;
    00048     }
    00049     else
    00050     {
    00051         /* Sign bit = 0 */
    00052         mask = AMI_MASK;
    00053         pcm_val = -pcm_val;
    00054     }
    00055
    00056     /* Convert the scaled magnitude to segment number. */
    00057     for (seg = 0;  seg < 8;  seg++)
    00058     {
    00059         if (pcm_val <= seg_end[seg])
    00060        break;
    00061     }
    00062     /* Combine the sign, segment, and quantization bits. */
    00063     return  ((seg << 4) | ((pcm_val >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask;
    00064 }
    00065 /*- End of function --------------------------------------------------------*/
    00066
    00067 static inline short int alaw2linear (unsigned char alaw)
    00068 {
    00069     int i;
    00070     int seg;
    00071
    00072     alaw ^= AMI_MASK;
    00073     i = ((alaw & 0x0F) << 4);
    00074     seg = (((int) alaw & 0x70) >> 4);
    00075     if (seg)
    00076         i = (i + 0x100) << (seg - 1);
    00077     return (short int) ((alaw & 0x80)  ?  i  :  -i);
    00078 }
    00079
    00080 unsigned char __ast_lin2a[8192];
    00081 short __ast_alaw[256];
    00082
    00083 void ast_alaw_init(void)
    00084 {
    00085    int i;
    00086    /*
    00087     *  Set up mu-law conversion table
    00088     */
    00089    for(i = 0;i < 256;i++)
    00090       {
    00091            __ast_alaw[i] = alaw2linear(i);
    00092       }
    00093      /* set up the reverse (mu-law) conversion table */
    00094    for(i = -32768; i < 32768; i++)
    00095       {
    00096       __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);
    00097       }
    00098
    00099 }
    00100