| Total Complexity | 2 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 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 | } |
||
| 45 |