Passed
Push — main ( b18130...ad5880 )
by Dylan
04:17
created

src/Tone.ts   A

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 45
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 24
mnd 2
bc 2
fnc 2
dl 0
loc 45
bpm 1
cpm 2
noi 0
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A Tone.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