Completed
Branch v4.x (b91b86)
by Rafael S.
01:40
created

T_EXPORT ➔ encodeSample   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 8
nop 1
dl 0
loc 1
rs 10
c 0
b 0
f 0
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
 * Encode a 16-bit linear PCM sample as 8-bit mu-Law.
38
 * @param {number} pcmSample A 16-bit sample
39
 * @return {number}
40
 */
41
export function encodeSample(pcmSample) {
42
  /** @type {number} */
43
  let mask = 0xFF;
44
  if (pcmSample < 0) {
45
    pcmSample = BIAS - pcmSample;
0 ignored issues
show
Bug introduced by
The local (let) variable BIAS is used before it is defined. This will cause a reference error.
Loading history...
46
    mask = 0x7F;
47
  } else {
48
    pcmSample += BIAS;
49
  }
50
  if (pcmSample > 0x7FFF) {
51
    pcmSample = 0x7FFF;
52
  }
53
  /** @type {number} */
54
  let seg = segmentValue_(pcmSample);
55
  /** @type {number} */
56
  let uval = (seg << 4) | ((pcmSample >> (seg + 3)) & 0xF);
57
  return uval ^ mask;
58
}
59
60
/**
61
 * Decode a 8-bit mu-Law sample as 16-bit linear PCM.
62
 * @param {number} muLawSample The 8-bit mu-Law sample
63
 * @return {number}
64
 */
65
export function decodeSample(muLawSample) {
66
  muLawSample = ~muLawSample;
67
  /** @type {number} */
68
  let t = ((muLawSample & 0xf) << 3) + BIAS;
0 ignored issues
show
Bug introduced by
The local (let) variable BIAS is used before it is defined. This will cause a reference error.
Loading history...
69
  t <<= (muLawSample & 0x70) >> 4;
70
  return ((muLawSample & 0x80) ? (BIAS - t) : (t - BIAS));
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
71
}
72
73
/**
74
 * Encode 16-bit linear PCM samples into 8-bit mu-Law samples.
75
 * @param {!Array<number>} samples A array of 16-bit linear PCM samples.
76
 * @return {!Array<number>}
77
 */
78
export function encode(samples) {
79
  /** @type {!Array<number>} */
80
  let muLawSamples = [];
81
  for (let i=0; i<samples.length; i++) {
82
    muLawSamples.push(encodeSample(samples[i]));
83
  }
84
  return muLawSamples;
85
}
86
87
/**
88
 * Decode 8-bit mu-Law samples into 16-bit linear PCM samples.
89
 * @param {!Array<number>} samples A array of 8-bit mu-Law samples.
90
 * @return {!Array<number>}
91
 */
92
export function decode(samples) {
93
  /** @type {!Array<number>} */
94
  let pcmSamples = [];
95
  for (let i=0; i<samples.length; i++) {
96
    pcmSamples.push(decodeSample(samples[i]));
97
  }
98
  return pcmSamples;
99
}
100
101
/**
102
 * Return the segment value of a PCM sample.
103
 * @param {number} sample
104
 * @return {number}
105
 * @private
106
 */
107
function segmentValue_(sample) {
108
  /** @type {number} */
109
  let segment = 0;
110
  sample >>= 7;
111
  if (sample & 0xf0) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
112
    sample >>= 4;
113
    segment += 4;
114
  }
115
  if (sample & 0x0c) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
116
    sample >>= 2;
117
    segment += 2;
118
  }
119
  if (sample & 0x02) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
120
    segment += 1;
121
  }
122
  return segment;
123
}
124
125
126
/**
127
 * @type {number}
128
 * @private
129
 */
130
const BIAS = 0x84;
0 ignored issues
show
Unused Code introduced by
The constant BIAS seems to be never used. Consider removing it.
Loading history...