index.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 1.57

Size

Lines of Code 85
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 11
mnd 4
bc 4
fnc 7
bpm 0.5714
cpm 1.5713
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B ➔ sweep 0 29 8
A ➔ getOutputObject_ 0 11 3
1
/*
2
 * Copyright (c) 2018-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 Function to create multi-stage frequency sweeps.
27
 * @see https://github.com/rochars/frequency-sweep
28
 */
29
30
/**
31
 * Waveform functions.
32
 * @private
33
 */
34
const WAVES = {
35
  noise: function() {
36
    return Math.random() * (1 - (-1)) + (-1);
37
  },
38
  sine: function(t, delta, phase, start, end, phi0) {
39
    phase = 2 * Math.PI * t * (start + (end - start) * delta / 2);
40
    return Math.sin(phase + phi0);
41
  },
42
  triangle: function(t, delta, phase, start, end, phi0) {
43
    phase = 2 * Math.PI * t * (start + (end - start) * delta / 2);
44
    return (2 / Math.PI) * Math.asin(Math.sin(phase + phi0));
45
  },
46
  sawtooth: function(t, delta, phase, start, end, phi0) {
47
    phase = 2 * Math.PI * t * (start + (end - start) * (delta / 2)) / 2;
48
    return (2 / Math.PI) * Math.atan(Math.tan(phase + phi0));
49
  },
50
  square: function(t, delta, phase, start, end, phi0) {
51
    phase = 2 * Math.PI * t * (start + (end - start) * delta / 2);
52
    return Math.sign(Math.sin(phase + phi0));
53
  }
54
};
55
56
/**
57
 * Return the samples of a frequency sweep. The sweep may be divided
58
 * in segments, each using a different waveform (or noise), and each with
59
 * a start and end frequency.
60
 * @param {!Array<Object<string, string|number>>} sequence The sequence.
61
 *   Each item in the array must have the properties:
62
 *     start: Integer value, the start frequency of the segment.
63
 *     end: Integer value, the end frequency of the segment.
64
 *     time: Float value, the duration of the segment. 1 = 1 second.
65
 *     wave: String, "sine", "square", "triangle", "sawtooth" or "noise".
66
 * @param {number} sampleRate The sample rate.
67
 * @param {?boolean} outputTyped True to return Float64Array, false for Array.
68
 * @return {!Array<number>|!Float64Array}
69
 */
70
export function sweep(sequence, sampleRate, outputTyped=false) {
71
  /** @type {number} */
72
  let phi0 = 0;
73
  /** @type {number} */
74
  let phase = 0;
75
  /** @type {number} */
76
  let numSamples = 0;
77
  /** @type {number} */
78
  let duration = 0;
79
  /** @type {number} */
80
  let delta = 0;
81
  /** @type {!Array<number>|!Float64Array} */
82
  let samples = getOutputObject_(sequence, sampleRate, outputTyped);
83
  for (let i = 0, x = 0; i < sequence.length; i++) {
84
    numSamples = Math.round(sampleRate * sequence[i].time);
85
    duration = numSamples / sampleRate;
86
    /** @type {number} */
87
    let limit = numSamples + x;
88
    for (; x < limit; x++) {
89
      delta = x / numSamples;
90
      samples[x] = WAVES[sequence[i].wave](
91
        duration*delta, delta, phase, sequence[i].start, sequence[i].end, phi0);
92
    }
93
    phase = 2 * Math.PI * duration *
94
        (sequence[i].start + (sequence[i].end - sequence[i].start) / 2);
95
    phi0 = phi0 + phase;
96
  }
97
  return samples;
98
}
99
100
/**
101
 * Return the output object, Array or Float64Array.
102
 * @param {!Array<Object<string, string|number>>} sequence The sequence.
103
 * @param {number} sampleRate The sample rate.
104
 * @param {boolean|null} outputTyped True for Float64Array, false for Array.
105
 * @return {!Array<number>|!Float64Array}
106
 * @private
107
 */
108
function getOutputObject_(sequence, sampleRate, outputTyped) {
109
  if (outputTyped) {
110
    /** @type {number} */
111
    let numSamples = 0;
112
    for (let i = 0; i < sequence.length; i++) {
113
      numSamples += Math.round(sampleRate * sequence[i].time);
114
    }
115
    return new Float64Array(numSamples);
116
  }
117
  return [];
118
}
119