|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Yomitan Authors |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +/** @type {?AudioContext} */ |
| 20 | +let sharedAudioContext = null; |
| 21 | + |
| 22 | +/** |
| 23 | + * @returns {AudioContext} |
| 24 | + */ |
| 25 | +function getSharedAudioContext() { |
| 26 | + if (!sharedAudioContext || sharedAudioContext.state === 'closed') { |
| 27 | + sharedAudioContext = new AudioContext(); |
| 28 | + } |
| 29 | + return sharedAudioContext; |
| 30 | +} |
| 31 | + |
| 32 | +export class WebAudioLocalAudio { |
| 33 | + /** |
| 34 | + * @param {string} base64Data |
| 35 | + * @param {string} contentType |
| 36 | + */ |
| 37 | + constructor(base64Data, contentType) { |
| 38 | + /** @type {string} */ |
| 39 | + this._base64Data = base64Data; |
| 40 | + /** @type {string} */ |
| 41 | + this._contentType = contentType; |
| 42 | + /** @type {number} */ |
| 43 | + this._volume = 1; |
| 44 | + /** @type {number} */ |
| 45 | + this._currentTime = 0; |
| 46 | + /** @type {AudioContext} */ |
| 47 | + this._audioContext = getSharedAudioContext(); |
| 48 | + /** @type {?AudioBufferSourceNode} */ |
| 49 | + this._bufferSource = null; |
| 50 | + /** @type {?GainNode} */ |
| 51 | + this._gainNode = null; |
| 52 | + /** @type {?AudioBuffer} */ |
| 53 | + this._decodedBuffer = null; |
| 54 | + } |
| 55 | + |
| 56 | + /** @type {number} */ |
| 57 | + get currentTime() { return this._currentTime; } |
| 58 | + |
| 59 | + set currentTime(value) { this._currentTime = value; } |
| 60 | + |
| 61 | + /** @type {number} */ |
| 62 | + get volume() { return this._volume; } |
| 63 | + |
| 64 | + set volume(value) { |
| 65 | + this._volume = value; |
| 66 | + if (this._gainNode) { this._gainNode.gain.value = value; } |
| 67 | + } |
| 68 | + |
| 69 | + /** @type {number} */ |
| 70 | + get duration() { return this._decodedBuffer ? this._decodedBuffer.duration : 0; } |
| 71 | + |
| 72 | + /** */ |
| 73 | + async prepare() { |
| 74 | + const byteCharacters = atob(this._base64Data); |
| 75 | + const byteNumbers = new Array(byteCharacters.length); |
| 76 | + for (let i = 0; i < byteCharacters.length; i++) { |
| 77 | + byteNumbers[i] = byteCharacters.charCodeAt(i); |
| 78 | + } |
| 79 | + const byteArray = new Uint8Array(byteNumbers); |
| 80 | + |
| 81 | + this._decodedBuffer = await this._audioContext.decodeAudioData(byteArray.buffer); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @returns {Promise<void>} |
| 86 | + */ |
| 87 | + async play() { |
| 88 | + if (!this._decodedBuffer || !this._audioContext) { return; } |
| 89 | + if (this._audioContext.state === 'suspended') { |
| 90 | + await this._audioContext.resume(); |
| 91 | + } |
| 92 | + this.pause(); |
| 93 | + |
| 94 | + this._bufferSource = this._audioContext.createBufferSource(); |
| 95 | + this._bufferSource.buffer = this._decodedBuffer; |
| 96 | + |
| 97 | + this._gainNode = this._audioContext.createGain(); |
| 98 | + this._gainNode.gain.value = this._volume; |
| 99 | + |
| 100 | + this._bufferSource.connect(this._gainNode); |
| 101 | + this._gainNode.connect(this._audioContext.destination); |
| 102 | + this._bufferSource.start(0, this._currentTime); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @returns {void} |
| 107 | + */ |
| 108 | + pause() { |
| 109 | + if (this._bufferSource) { |
| 110 | + try { this._bufferSource.stop(); } catch (e) { /* NOP */ } |
| 111 | + this._bufferSource = null; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments