Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { WaveformAudio } from "../Media/WaveformAudio";
import type { Waveform } from "../Waveform";
import { Player } from "./Player";
 
export class WebAudioPlayer extends Player {
  private audioContext?: AudioContext;
  private audioBufferSource?: AudioBufferSourceNode;
  private gainNode?: GainNode;
 
  constructor(wf: Waveform) {
    super(wf);
 
    this.audioContext = new AudioContext();
    this.gainNode = this.audioContext.createGain();
    this.gainNode.connect(this.audioContext.destination);
  }
 
  async init(audio: WaveformAudio) {
    super.init(audio);
 
    if (!this.audioContext) return;
 
    if (this.audioContext.state === "suspended") {
      await this.audioContext.resume();
    }
  }
 
  /**
   * Get current playback speed
   */
  get rate() {
    // restore the correct rate
    if (this.audioBufferSource?.playbackRate && this._rate !== this.audioBufferSource.playbackRate.value) {
      this.audioBufferSource.playbackRate.value = this._rate;
    }
    return this._rate;
  }
 
  /**
   * Set playback speed
   */
  set rate(value: number) {
    const rateChanged = this._rate !== value;
 
    this._rate = value;
 
    if (rateChanged) {
      if (this.audioBufferSource?.playbackRate) {
        this.audioBufferSource.playbackRate.value = this._rate;
      }
      this.wf.invoke("rateChanged", [value]);
    }
  }
 
  protected adjustVolume(): void {
    if (this.gainNode) {
      this.gainNode.gain.value = this.volume;
    }
  }
 
  destroy() {
    super.destroy();
 
    if (this.audioContext) {
      this.audioContext.close().finally(() => {
        delete this.audioContext;
      });
    }
  }
 
  protected playAudio(start?: number, _duration?: number) {
    if (!this.audioBufferSource) return;
 
    try {
      if (start) {
        this.audioBufferSource.start(0, start);
      } else {
        this.audioBufferSource.start(0);
      }
    } catch (err: any) {
      // InvalidStateError is thrown when the audio is already playing
      if (err.name !== "InvalidStateError") throw err;
    }
 
    this.timestamp = performance.now();
    this.watch();
  }
 
  protected connectSource() {
    if (this.isDestroyed || !this.audioContext || !this.audio?.buffer || !this.gainNode || this.connected) return;
    this.connected = true;
    this.audioBufferSource = this.audioContext.createBufferSource();
    this.audioBufferSource.buffer = this.audio.buffer;
    this.audioBufferSource.connect(this.gainNode);
    this.audioBufferSource.onended = this.handleEnded;
  }
 
  protected disconnectSource(): boolean {
    if (this.isDestroyed || !this.connected || !this.audioBufferSource) return false;
    this.connected = false;
 
    try {
      this.audioBufferSource.stop();
    } catch (err: any) {
      // InvalidStateError is thrown when the audio is already stopped
      if (err.name !== "InvalidStateError") throw err;
    }
    this.audioBufferSource.disconnect();
    this.audioBufferSource.onended = null;
    this.audioBufferSource = undefined;
 
    return true;
  }
 
  protected playSource(start?: number, end?: number) {
    this.disconnectSource();
    super.playSource(start, end);
  }
 
  protected updateCurrentSourceTime(timeChanged: boolean) {
    if (timeChanged && this.audioBufferSource) {
      this.disconnectSource();
      this.connectSource();
      this.audioBufferSource.start(0, this.time);
    }
  }
 
  protected cleanupSource() {
    super.cleanupSource();
    this.audioBufferSource = undefined;
  }
}