Passed
Push — master ( 753e4e...b53e1e )
by Rafael S.
01:45
created

index.js (6 issues)

Severity
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(t, delta, phase, start, end, phi0) {
0 ignored issues
show
The parameter delta is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter phase is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter end is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter start is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter t is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter phi0 is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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
 * @return {!Array<number>}
68
 */
69
export function sweep(sequence, sampleRate) {
70
  let phi0 = 0;
71
  let phase = 0;
72
  let numSamples = 0;
73
  let duration = 0;
74
  let delta = 0;
75
  let samples = [];
76
  for (let i = 0; i < sequence.length; i++) {
77
    numSamples = Math.round(sampleRate * sequence[i].time);
78
    duration = numSamples / sampleRate;
79
    for (let x = 0; x < numSamples; x++) {
80
      delta = x / numSamples;
81
      samples.push(
82
        WAVES[sequence[i].wave](
83
          duration * delta,
84
          delta, phase,
85
          sequence[i].start,
86
          sequence[i].end,
87
          phi0));
88
    }
89
    phase = 2 * Math.PI * duration *
90
        (sequence[i].start + (sequence[i].end - sequence[i].start) / 2);
91
    phi0 = phi0 + phase;
92
  }
93
  return samples;
94
}
95