3 from __future__
import print_function
5 SIGN_BIT
= (0x80) # Sign bit for a A-law byte.
6 QUANT_MASK
= (0xf) # Quantization field mask.
7 SEG_SHIFT
= (4) # Left shift for segment number.
8 SEG_MASK
= (0x70) # Segment field mask.
9 BIAS
= (0x84) # Bias for linear code.
12 def alaw2linear(a_val
):
15 t
= a_val
& QUANT_MASK
16 seg
= (a_val
& SEG_MASK
) >> SEG_SHIFT
18 t
= (t
+ t
+ 1 + 32) << (seg
+ 2)
28 def ulaw2linear(u_val
):
29 # Complement to obtain normal u-law value.
32 # Extract and bias the quantization bits. Then
33 # shift up by the segment number and subtract out the bias.
34 t
= ((u_val
& QUANT_MASK
) << 3) + BIAS
35 t
<<= (u_val
& SEG_MASK
) >> SEG_SHIFT
50 def tableToString(name
, table
):
51 result
= 'const int16_t ' + name
+ '[256] = { '
52 result
+= ', '.join(str(i
) for i
in table
)
57 print(tableToString('alawTable', pcmTable(alaw2linear
)))
58 print(tableToString('ulawTable', pcmTable(ulaw2linear
)))