|
1
|
|
|
/* |
|
2
|
|
|
* mulaw.js |
|
3
|
|
|
* Copyright (c) 2018 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/alawmulaw |
|
5
|
|
|
* |
|
6
|
|
|
* Reference: |
|
7
|
|
|
* https://github.com/torvalds/linux/blob/master/sound/core/oss/mulaw.c |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** @private */ |
|
12
|
|
|
const BIAS = 0x84; |
|
13
|
|
|
/** @private */ |
|
14
|
|
|
const SIGN_BIT = 0x80; |
|
15
|
|
|
/** @private */ |
|
16
|
|
|
const QUANT_MASK = 0xf; |
|
17
|
|
|
/** @private */ |
|
18
|
|
|
const SEG_MASK = 0x70; |
|
19
|
|
|
/** @private */ |
|
20
|
|
|
const SEG_SHIFT = 4; |
|
21
|
|
|
|
|
22
|
|
|
/** @private */ |
|
23
|
|
|
function valSeg_(val) { |
|
24
|
|
|
let r = 0; |
|
25
|
|
|
val >>= 7; |
|
26
|
|
|
if (val & 0xf0) { |
|
|
|
|
|
|
27
|
|
|
val >>= 4; |
|
28
|
|
|
r += 4; |
|
29
|
|
|
} |
|
30
|
|
|
if (val & 0x0c) { |
|
|
|
|
|
|
31
|
|
|
val >>= 2; |
|
32
|
|
|
r += 2; |
|
33
|
|
|
} |
|
34
|
|
|
if (val & 0x02) { |
|
|
|
|
|
|
35
|
|
|
r += 1; |
|
36
|
|
|
} |
|
37
|
|
|
return r; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Encode a 16-bit linear PCM sample as 8-bit mu-Law. |
|
42
|
|
|
* @param {number} pcmSample A 16-bit sample |
|
43
|
|
|
* @return {number} |
|
44
|
|
|
*/ |
|
45
|
|
|
function encodeSample(pcmSample) { |
|
46
|
|
|
let mask; |
|
47
|
|
|
let seg; |
|
48
|
|
|
let uval; |
|
49
|
|
|
if (pcmSample < 0) { |
|
50
|
|
|
pcmSample = BIAS - pcmSample; |
|
51
|
|
|
mask = 0x7F; |
|
52
|
|
|
} else { |
|
53
|
|
|
pcmSample += BIAS; |
|
54
|
|
|
mask = 0xFF; |
|
55
|
|
|
} |
|
56
|
|
|
if (pcmSample > 0x7FFF) { |
|
57
|
|
|
pcmSample = 0x7FFF; |
|
58
|
|
|
} |
|
59
|
|
|
seg = valSeg_(pcmSample); |
|
60
|
|
|
uval = (seg << 4) | ((pcmSample >> (seg + 3)) & 0xF); |
|
61
|
|
|
return uval ^ mask; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Decode a 8-bit mu-Law sample as 16-bit linear PCM. |
|
66
|
|
|
* @param {number} muLawSample The 8-bit mu-Law sample |
|
67
|
|
|
* @return {number} |
|
68
|
|
|
*/ |
|
69
|
|
|
function decodeSample(muLawSample) { |
|
70
|
|
|
let t; |
|
71
|
|
|
muLawSample = ~muLawSample; |
|
72
|
|
|
t = ((muLawSample & QUANT_MASK) << 3) + BIAS; |
|
73
|
|
|
t <<= (muLawSample & SEG_MASK) >> SEG_SHIFT; |
|
74
|
|
|
return ((muLawSample & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Encode 16-bit linear PCM samples into 8-bit mu-Law samples. |
|
79
|
|
|
* @param {!Array<number>} samples A array of 16-bit linear PCM samples. |
|
80
|
|
|
* @return {!Array<number>} |
|
81
|
|
|
*/ |
|
82
|
|
|
function encode(samples) { |
|
83
|
|
|
let muLawSamples = []; |
|
84
|
|
|
for (let i=0; i<samples.length; i++) { |
|
85
|
|
|
muLawSamples.push(encodeSample(samples[i])); |
|
86
|
|
|
} |
|
87
|
|
|
return muLawSamples; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Decode 8-bit mu-Law samples into 16-bit linear PCM samples. |
|
92
|
|
|
* @param {!Array<number>} samples A array of 8-bit mu-Law samples. |
|
93
|
|
|
* @return {!Array<number>} |
|
94
|
|
|
*/ |
|
95
|
|
|
function decode(samples) { |
|
96
|
|
|
let pcmSamples = []; |
|
97
|
|
|
for (let i=0; i<samples.length; i++) { |
|
98
|
|
|
pcmSamples.push(decodeSample(samples[i])); |
|
99
|
|
|
} |
|
100
|
|
|
return pcmSamples; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
module.exports.encodeSample = encodeSample; |
|
104
|
|
|
module.exports.decodeSample = decodeSample; |
|
105
|
|
|
module.exports.encode = encode; |
|
106
|
|
|
module.exports.decode = decode; |
|
107
|
|
|
|