~ubuntu-branches/ubuntu/karmic/asterisk/karmic

« back to all changes in this revision

Viewing changes to alaw.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2002-04-27 21:19:32 UTC
  • Revision ID: james.westby@ubuntu.com-20020427211932-kqaertc4bg7ss5mc
Tags: upstream-0.1.11
ImportĀ upstreamĀ versionĀ 0.1.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Asterisk -- A telephony toolkit for Linux.
 
3
 *
 
4
 * u-Law to Signed linear conversion
 
5
 * 
 
6
 * Copyright (C) 1999, Mark Spencer
 
7
 *
 
8
 * Mark Spencer <markster@linux-support.net>
 
9
 *
 
10
 * This program is free software, distributed under the terms of
 
11
 * the GNU General Public License
 
12
 */
 
13
 
 
14
#include <asterisk/alaw.h>
 
15
 
 
16
#define AMI_MASK 0x55
 
17
 
 
18
static inline unsigned char linear2alaw (short int linear)
 
19
{
 
20
    int mask;
 
21
    int seg;
 
22
    int pcm_val;
 
23
    static int seg_end[8] =
 
24
    {
 
25
         0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
 
26
    };
 
27
    
 
28
    pcm_val = linear;
 
29
    if (pcm_val >= 0)
 
30
    {
 
31
        /* Sign (7th) bit = 1 */
 
32
        mask = AMI_MASK | 0x80;
 
33
    }
 
34
    else
 
35
    {
 
36
        /* Sign bit = 0 */
 
37
        mask = AMI_MASK;
 
38
        pcm_val = -pcm_val;
 
39
    }
 
40
 
 
41
    /* Convert the scaled magnitude to segment number. */
 
42
    for (seg = 0;  seg < 8;  seg++)
 
43
    {
 
44
        if (pcm_val <= seg_end[seg])
 
45
            break;
 
46
    }
 
47
    /* Combine the sign, segment, and quantization bits. */
 
48
    return  ((seg << 4) | ((pcm_val >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask;
 
49
}
 
50
/*- End of function --------------------------------------------------------*/
 
51
 
 
52
static inline short int alaw2linear (unsigned char alaw)
 
53
{
 
54
    int i;
 
55
    int seg;
 
56
 
 
57
    alaw ^= AMI_MASK;
 
58
    i = ((alaw & 0x0F) << 4);
 
59
    seg = (((int) alaw & 0x70) >> 4);
 
60
    if (seg)
 
61
        i = (i + 0x100) << (seg - 1);
 
62
    return (short int) ((alaw & 0x80)  ?  i  :  -i);
 
63
}
 
64
 
 
65
unsigned char __ast_lin2a[8192];
 
66
short __ast_alaw[256];
 
67
 
 
68
void ast_alaw_init(void)
 
69
{
 
70
        int i;
 
71
        /* 
 
72
         *  Set up mu-law conversion table
 
73
         */
 
74
        for(i = 0;i < 256;i++)
 
75
           {
 
76
                __ast_alaw[i] = alaw2linear(i);
 
77
           }
 
78
          /* set up the reverse (mu-law) conversion table */
 
79
        for(i = -32768; i < 32768; i++)
 
80
           {
 
81
                __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);
 
82
           }
 
83
 
 
84
}
 
85