Total Complexity | 4 |
Complexity/F | 2 |
Lines of Code | 44 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
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 |