Passed
Push — main ( b18130...ad5880 )
by Dylan
04:17
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 22
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
// import {DEFAULT_LENGTH, DEFAULT_FREQUENCY} from './config'
2
3
/**
4
 * @class Tone player
5
 * @name Tone
6
 */
7 3
export class Tone {
8
  ctx: AudioContext
9
  gainNode: GainNode
10
  outNode: AudioDestinationNode
11
  
12
  /**
13
   * Initialize a tone beeper.
14
   */
15
  /* istanbul ignore next */
16
  constructor() {
17
    if (typeof window !== 'undefined' && typeof window.AudioContext !== 'undefined') {
18
      this.ctx = new window.AudioContext()
19
      this.gainNode = this.ctx.createGain()
20
      this.gainNode.gain.value = 1/8
21
      this.outNode = this.ctx.destination
22
      this.gainNode.connect(this.outNode)
23
    }
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
44
export default Tone
45