Sat Aug 6 00:39:19 2011

Asterisk developer's documentation


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  * \author Mark Spencer <markster@digium.com> 
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 80166 $")
00029 
00030 #include "asterisk/alaw.h"
00031 
00032 #define AMI_MASK 0x55
00033 
00034 static inline unsigned char linear2alaw (short int linear)
00035 {
00036     int mask;
00037     int seg;
00038     int pcm_val;
00039     static int seg_end[8] =
00040     {
00041          0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
00042     };
00043     
00044     pcm_val = linear;
00045     if (pcm_val >= 0)
00046     {
00047         /* Sign (7th) bit = 1 */
00048         mask = AMI_MASK | 0x80;
00049     }
00050     else
00051     {
00052         /* Sign bit = 0 */
00053         mask = AMI_MASK;
00054         pcm_val = -pcm_val;
00055     }
00056 
00057     /* Convert the scaled magnitude to segment number. */
00058     for (seg = 0;  seg < 8;  seg++)
00059     {
00060         if (pcm_val <= seg_end[seg])
00061        break;
00062     }
00063     /* Combine the sign, segment, and quantization bits. */
00064     return  ((seg << 4) | ((pcm_val >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask;
00065 }
00066 /*- End of function --------------------------------------------------------*/
00067 
00068 static inline short int alaw2linear (unsigned char alaw)
00069 {
00070     int i;
00071     int seg;
00072 
00073     alaw ^= AMI_MASK;
00074     i = ((alaw & 0x0F) << 4) + 8 /* rounding error */;
00075     seg = (((int) alaw & 0x70) >> 4);
00076     if (seg)
00077         i = (i + 0x100) << (seg - 1);
00078     return (short int) ((alaw & 0x80)  ?  i  :  -i);
00079 }
00080 
00081 unsigned char __ast_lin2a[8192];
00082 short __ast_alaw[256];
00083 
00084 void ast_alaw_init(void)
00085 {
00086    int i;
00087    /* 
00088     *  Set up mu-law conversion table
00089     */
00090    for(i = 0;i < 256;i++)
00091       {
00092            __ast_alaw[i] = alaw2linear(i);
00093       }
00094      /* set up the reverse (mu-law) conversion table */
00095    for(i = -32768; i < 32768; i++)
00096       {
00097       __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);
00098       }
00099 
00100 }
00101 

Generated on Sat Aug 6 00:39:19 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7