Passed
Push — main ( eaf03b...1b309e )
by Dylan
04:42
created

Tone   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 25
dl 0
loc 39
c 0
b 0
f 0
ccs 1
cts 1
cp 1
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A beepOnBeepOff 0 12 2
1
/**
2
 * @class Tone player
3
 * @name Tone
4
 */
5 3
export class Tone {
6
  ctx: AudioContext
7
  gainNode: GainNode
8
  outNode: AudioDestinationNode
9
10
  /**
11
   * Initialize a tone beeper.
12
   */
13
  /* istanbul ignore next */
14
  constructor() {
15
    if (
16
      typeof window !== 'undefined' &&
17
      typeof window.AudioContext !== 'undefined'
18
    ) {
19
      this.ctx = new window.AudioContext()
20
      this.gainNode = this.ctx.createGain()
21
      this.gainNode.gain.value = 1 / 8
22
      this.outNode = this.ctx.destination
23
      this.gainNode.connect(this.outNode)
24
    } else {
25
      throw 'AudioContext is required'
26
    }
27
  }
28
29
  /**
30
   * Play a beep after wait seconds, stopping after length seconds.
31
   */
32
  /* istanbul ignore next */
33
  beepOnBeepOff(frequency: number, length: number, wait: number): void {
34
    const osc = this.ctx.createOscillator()
35
    osc.type = 'square'
36
    osc.frequency.value = frequency
37
    osc.connect(this.gainNode)
38
    osc.start(wait)
39
    osc.stop(wait + length)
40
  }
41
}
42
43
export default Tone
44