1
|
|
|
/* |
2
|
|
|
* alawmulaw: A-Law and mu-Law codecs in JavaScript. |
3
|
|
|
* https://github.com/rochars/alawmulaw |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
8
|
|
|
* a copy of this software and associated documentation files (the |
9
|
|
|
* "Software"), to deal in the Software without restriction, including |
10
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
11
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
12
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
13
|
|
|
* the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be |
16
|
|
|
* included in all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
20
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
22
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
23
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
24
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @fileoverview mu-Law codec. |
30
|
|
|
* References: |
31
|
|
|
* https://github.com/torvalds/linux/blob/master/sound/core/oss/mulaw.c |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** @module alawmulaw/mulaw */ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @type {number} |
38
|
|
|
* @private |
39
|
|
|
*/ |
40
|
|
|
const BIAS = 0x84; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Encode a 16-bit linear PCM sample as 8-bit mu-Law. |
44
|
|
|
* @param {number} pcmSample A 16-bit sample |
45
|
|
|
* @return {number} |
46
|
|
|
*/ |
47
|
|
|
export function encodeSample(pcmSample) { |
48
|
|
|
/** @type {number} */ |
49
|
|
|
let mask = 0xFF; |
50
|
|
|
if (pcmSample < 0) { |
51
|
|
|
pcmSample = BIAS - pcmSample; |
52
|
|
|
mask = 0x7F; |
53
|
|
|
} else { |
54
|
|
|
pcmSample += BIAS; |
55
|
|
|
} |
56
|
|
|
if (pcmSample > 0x7FFF) { |
57
|
|
|
pcmSample = 0x7FFF; |
58
|
|
|
} |
59
|
|
|
/** @type {number} */ |
60
|
|
|
let seg = segmentValue_(pcmSample); |
61
|
|
|
/** @type {number} */ |
62
|
|
|
let uval = (seg << 4) | ((pcmSample >> (seg + 3)) & 0xF); |
63
|
|
|
return uval ^ mask; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Decode a 8-bit mu-Law sample as 16-bit linear PCM. |
68
|
|
|
* @param {number} muLawSample The 8-bit mu-Law sample |
69
|
|
|
* @return {number} |
70
|
|
|
*/ |
71
|
|
|
export function decodeSample(muLawSample) { |
72
|
|
|
muLawSample = ~muLawSample; |
73
|
|
|
/** @type {number} */ |
74
|
|
|
let t = ((muLawSample & 0xf) << 3) + BIAS; |
75
|
|
|
t <<= (muLawSample & 0x70) >> 4; |
76
|
|
|
return ((muLawSample & 0x80) ? (BIAS - t) : (t - BIAS)); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Encode 16-bit linear PCM samples into 8-bit mu-Law samples. |
81
|
|
|
* @param {!Array<number>} samples A array of 16-bit linear PCM samples. |
82
|
|
|
* @return {!Array<number>} |
83
|
|
|
*/ |
84
|
|
|
export function encode(samples) { |
85
|
|
|
/** @type {!Array<number>} */ |
86
|
|
|
let muLawSamples = []; |
87
|
|
|
for (let i=0; i<samples.length; i++) { |
88
|
|
|
muLawSamples.push(encodeSample(samples[i])); |
89
|
|
|
} |
90
|
|
|
return muLawSamples; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Decode 8-bit mu-Law samples into 16-bit linear PCM samples. |
95
|
|
|
* @param {!Array<number>} samples A array of 8-bit mu-Law samples. |
96
|
|
|
* @return {!Array<number>} |
97
|
|
|
*/ |
98
|
|
|
export function decode(samples) { |
99
|
|
|
/** @type {!Array<number>} */ |
100
|
|
|
let pcmSamples = []; |
101
|
|
|
for (let i=0; i<samples.length; i++) { |
102
|
|
|
pcmSamples.push(decodeSample(samples[i])); |
103
|
|
|
} |
104
|
|
|
return pcmSamples; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the segment value of a PCM sample. |
109
|
|
|
* @param {number} sample |
110
|
|
|
* @return {number} |
111
|
|
|
* @private |
112
|
|
|
*/ |
113
|
|
|
function segmentValue_(sample) { |
114
|
|
|
/** @type {number} */ |
115
|
|
|
let segment = 0; |
116
|
|
|
sample >>= 7; |
117
|
|
|
if (sample & 0xf0) { |
|
|
|
|
118
|
|
|
sample >>= 4; |
119
|
|
|
segment += 4; |
120
|
|
|
} |
121
|
|
|
if (sample & 0x0c) { |
|
|
|
|
122
|
|
|
sample >>= 2; |
123
|
|
|
segment += 2; |
124
|
|
|
} |
125
|
|
|
if (sample & 0x02) { |
|
|
|
|
126
|
|
|
segment += 1; |
127
|
|
|
} |
128
|
|
|
return segment; |
129
|
|
|
} |
130
|
|
|
|