|
1 | 1 | import { Sound } from '@/definitions/sound' |
2 | 2 | import { Vue as _Vue } from 'vue/types/vue' |
3 | 3 | import { WebsocketOptions } from '@/plugins/websocket' |
| 4 | +import { AxiosInstance, AxiosResponse } from 'axios' |
| 5 | + |
| 6 | +class AsyncBlockingQueue { |
| 7 | + private promises: Promise<Sound>[] = []; |
| 8 | + private resolvers: ((t: Sound) => void)[] = []; |
| 9 | + |
| 10 | + private add () { |
| 11 | + this.promises.push(new Promise(resolve => { |
| 12 | + this.resolvers.push(resolve) |
| 13 | + })) |
| 14 | + } |
| 15 | + |
| 16 | + public enqueue (sound: Sound) { |
| 17 | + if (!this.resolvers.length) this.add() |
| 18 | + const resolve = this.resolvers.shift()! |
| 19 | + resolve(sound) |
| 20 | + } |
| 21 | + |
| 22 | + public dequeue (): Promise<Sound> { |
| 23 | + if (!this.promises.length) this.add() |
| 24 | + return this.promises.shift()! |
| 25 | + } |
| 26 | + |
| 27 | + public isEmpty (): boolean { |
| 28 | + return !this.promises.length |
| 29 | + } |
| 30 | +} |
4 | 31 |
|
5 | 32 | export default class Player { |
6 | 33 | private enabled = false |
7 | | - private audio!: HTMLAudioElement; |
8 | | - private queue: Sound[] = []; |
9 | | - private isPlaying = false; |
| 34 | + private isPlaying = false |
| 35 | + private queue: AsyncBlockingQueue = new AsyncBlockingQueue() |
10 | 36 |
|
11 | | - public constructor () { |
| 37 | + private ctx!: AudioContext |
| 38 | + private api!: AxiosInstance |
| 39 | + |
| 40 | + public constructor (api: AxiosInstance) { |
12 | 41 | this.install = this.install.bind(this) |
13 | 42 | this.OnPlayMessage = this.OnPlayMessage.bind(this) |
14 | 43 | this.EnableSound = this.EnableSound.bind(this) |
15 | 44 | this.playNextSound = this.playNextSound.bind(this) |
16 | 45 |
|
17 | | - this.audio = new Audio() |
| 46 | + this.api = api |
18 | 47 | } |
19 | 48 |
|
20 | 49 | public install (Vue: typeof _Vue, _options?: WebsocketOptions) { |
21 | 50 | Vue.prototype.$player = this |
22 | 51 | } |
23 | 52 |
|
24 | | - public async OnPlayMessage (sound: any) { |
| 53 | + public async OnPlayMessage (message: any) { |
25 | 54 | if (!this.enabled) { |
26 | 55 | return |
27 | 56 | } |
28 | 57 |
|
29 | | - this.queue.push(sound.sound) |
| 58 | + // add this sound to the queue |
| 59 | + this.queue.enqueue(message.sound) |
30 | 60 |
|
31 | | - await this.playNextSound() |
| 61 | + if (!this.isPlaying) { |
| 62 | + await this.playNextSound() |
| 63 | + } |
32 | 64 | } |
33 | 65 |
|
34 | 66 | public async EnableSound () { |
35 | | - this.audio.src = '' |
36 | | - |
37 | | - try { |
38 | | - await this.audio.play() |
39 | | - } catch {} |
| 67 | + this.ctx = this.ctx = new window.AudioContext() |
40 | 68 |
|
41 | 69 | this.enabled = true |
42 | 70 | } |
43 | 71 |
|
44 | 72 | private async playNextSound () { |
45 | | - if (this.isPlaying) { |
46 | | - return |
47 | | - } |
| 73 | + this.isPlaying = true |
48 | 74 |
|
49 | | - while (true) { |
50 | | - const sound = this.queue.pop() |
| 75 | + const sound = await this.queue.dequeue() |
51 | 76 |
|
52 | | - if (sound === undefined) { |
53 | | - return |
54 | | - } |
| 77 | + const resp: AxiosResponse = await this.api.get(`/sound/sounds/${sound.id}/download/`, { responseType: 'arraybuffer' }) |
55 | 78 |
|
56 | | - this.audio.src = `/api/sound/sounds/${sound.id}/download/` |
57 | | - |
58 | | - this.isPlaying = true |
59 | | - |
60 | | - await this.audio.play() |
61 | | - |
62 | | - this.isPlaying = false |
63 | | - } |
| 79 | + const buf = await this.ctx.decodeAudioData(resp.data) |
| 80 | + const source = this.ctx.createBufferSource() |
| 81 | + source.buffer = buf |
| 82 | + source.connect(this.ctx.destination) |
| 83 | + source.start() |
| 84 | + source.onended = this.playNextSound |
64 | 85 | } |
65 | 86 | } |
0 commit comments