lib/parsers/interleave.js   A
last analyzed

Complexity

Total Complexity 9
Complexity/F 4.5

Size

Lines of Code 39
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 9
mnd 7
bc 7
fnc 2
bpm 3.5
cpm 4.5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A interleave.js ➔ deInterleave 0 13 4
1
/*
2
 * Copyright (c) 2017-2019 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The interleave function.
27
 * @see https://github.com/rochars/wavefile
28
 */
29
30
/**
31
 * Interleave de-interleaved samples.
32
 * @param {!(Array|TypedArray)} samples The samples.
33
 * @return {!(Array|TypedArray)}
34
 */
35
export function interleave(samples) {
36
  /** @type {!(Array|TypedArray)} */
37
  let finalSamples = [];
38
  if (samples.length > 0) {
39
    if (samples[0].constructor !== Number) {
40
      finalSamples = new Float64Array(samples[0].length * samples.length);
41
      for (let i = 0, len = samples[0].length, x = 0; i < len; i++) {
42
        for (let j = 0, subLen = samples.length; j < subLen; j++, x++) {
43
          finalSamples[x] = samples[j][i];
44
        }
45
      }
46
    } else {
47
      finalSamples = samples;
48
    }
49
  }
50
  return finalSamples;
51
}
52
53
/**
54
 * De-interleave samples into multiple channels.
55
 * @param {!(Array|TypedArray)} samples The samples.
56
 * @param {number} numChannels The number of channels to split the samples.
57
 * @param {Function} [OutputObject=Float64Array] The type of object to
58
 *   write the de-interleaved samples.
59
 * @return {!(Array|TypedArray)}
60
 */
61
export function deInterleave(samples, numChannels, OutputObject=Float64Array) {
62
  /** @type {!(Array|TypedArray)} */
63
  let finalSamples = [];
64
  for (let i = 0; i < numChannels; i++) {
65
    finalSamples[i] = new OutputObject(samples.length / numChannels);
66
  }
67
  for (let i = 0; i < numChannels; i++) {
68
    for (let j = i, s = 0; j < samples.length; j+= numChannels, s++) {
69
      finalSamples[i][s] = samples[j];
70
    }
71
  }
72
  return finalSamples;
73
}
74