summaryrefslogtreecommitdiffstats
path: root/alaw.c
blob: 41a45d0267b38638ef5a891c517d691d7871c74a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// code from xanim sources...
// (I hope that not hurt copyright :o)

#define xaLONG long
#define xaULONG unsigned long
#define xaBYTE char
#define xaUBYTE unsigned char

//xaULONG long xa_alaw_2_sign[256];
xaULONG xa_alaw_2_sign[256];
xaULONG xa_ulaw_2_sign[256];

/*
** This routine converts from ulaw to 16 bit linear.
**
** Craig Reese: IDA/Supercomputing Research Center
** 29 September 1989
**
** References:
** 1) CCITT Recommendation G.711  (very difficult to follow)
** 2) MIL-STD-188-113,"Interoperability and Performance Standards
**     for Analog-to_Digital Conversion Techniques,"
**     17 February 1987
**
** Input: 8 bit ulaw sample
** Output: signed 16 bit linear sample
*/

xaLONG XA_uLaw_to_Signed( ulawbyte )
xaUBYTE ulawbyte;
{
  static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
  int sign, exponent, mantissa, sample;
 
  ulawbyte = ~ ulawbyte;
  sign = ( ulawbyte & 0x80 );
  exponent = ( ulawbyte >> 4 ) & 0x07;
  mantissa = ulawbyte & 0x0F;
  sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
  if ( sign != 0 ) sample = -sample;
 
  return sample;
}

void Gen_uLaw_2_Signed()
{ xaULONG i;
  for(i=0;i<256;i++)
  { xaUBYTE data = (xaUBYTE)(i);
    xaLONG d = XA_uLaw_to_Signed( data );
    xa_ulaw_2_sign[i] = (xaULONG)((xaULONG)(d) & 0xffff);
  }
}



void Gen_aLaw_2_Signed()
{ xaULONG i;
  for(i=0;i<256;i++)
  { xaUBYTE data = (xaUBYTE)(i);
    xaLONG d, t, seg;

    data ^= 0x55;

    t = (data & 0xf) << 4;
    seg = (data & 0x70) >> 4;
    if (seg == 0)	t += 8;
    else if (seg == 1)	t += 0x108;
    else	{ t += 108; t <<= seg - 1; }

    d =  (data & 0x80)?(t):(-t);
    xa_alaw_2_sign[i] = (xaULONG)((xaULONG)(d) & 0xffff);
  }
}