* Plays a beep at the given frequency. * * @param tone The frequency of the beep to play, in hertz. * @param duration The duration of the beep, in seconds. Defaults to 0.2.
(tone: number, duration = 0.2)
| 120 | * @param duration The duration of the beep, in seconds. Defaults to 0.2. |
| 121 | */ |
| 122 | async beep(tone: number, duration = 0.2) { |
| 123 | if (!this.isPlayingAllowed()) return; |
| 124 | await this.prepareToPlay(); |
| 125 | |
| 126 | const oscillator = this.context.createOscillator(); |
| 127 | oscillator.type = 'sine'; |
| 128 | oscillator.frequency.setValueAtTime(tone, this.context.currentTime); |
| 129 | |
| 130 | const gainNode = this.context.createGain(); |
| 131 | gainNode.gain.setValueAtTime(0, this.context.currentTime); |
| 132 | // Fade in |
| 133 | gainNode.gain.linearRampToValueAtTime(0.5, this.context.currentTime + 0.01); |
| 134 | // Fade out |
| 135 | gainNode.gain.linearRampToValueAtTime( |
| 136 | 0, |
| 137 | this.context.currentTime + duration, |
| 138 | ); |
| 139 | |
| 140 | oscillator.connect(gainNode); |
| 141 | gainNode.connect(this.context.destination); |
| 142 | |
| 143 | oscillator.start(this.context.currentTime); |
| 144 | oscillator.stop(this.context.currentTime + duration); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Plays a standard error beep. |
no test coverage detected