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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { Destructable } from "../Common/Destructable";
import type { Waveform } from "../Waveform";
import { WaveformAudio, type WaveformAudioOptions } from "./WaveformAudio";
 
export type Options = {
  src: string;
};
 
type MediaResponse = ArrayBuffer | null;
 
export class MediaLoader extends Destructable {
  private wf: Waveform;
  private audio?: WaveformAudio | null;
  private loaded = false;
  private options: Options;
  private cancel: () => void;
  private decoderResolve?: () => void;
  private _duration = 0;
 
  decoderPromise?: Promise<void>;
  loadingProgressType: "determinate" | "indeterminate";
 
  constructor(wf: Waveform, options: Options) {
    super();
    this.wf = wf;
    this.options = options;
    this.cancel = () => {};
    this.loadingProgressType = "determinate";
  }
 
  get duration() {
    return this._duration;
  }
 
  set duration(duration: number) {
    const changed = this._duration !== duration;
 
    this._duration = duration;
 
    if (changed) {
      this.wf.invoke("durationChanged", [duration]);
    }
  }
 
  get sampleRate() {
    return this.audio?.sampleRate || 0;
  }
 
  reset() {
    this.cancel();
    this.loaded = false;
    this.loadingProgressType = "determinate";
    this.decoderResolve = undefined;
    this.decoderPromise = undefined;
  }
 
  async decodeAudioData() {
    if (!this.audio || this.isDestroyed) return null;
 
    return await this.audio.decodeAudioData({
      multiChannel: this.wf.params.splitChannels,
    });
  }
 
  async load(options: WaveformAudioOptions): Promise<WaveformAudio | null> {
    if (this.isDestroyed || this.loaded) {
      return null;
    }
 
    // Special handling for "none" decoder - skip all decoding
    if (this.wf.params.decoderType === "none") {
      return await this.loadWithoutDecoding(options);
    }
 
    // Create this as soon as possible so that we can
    // update the loading progress from the waveform
    this.decoderPromise = new Promise((resolve) => {
      this.decoderResolve = resolve;
    });
 
    this.createAnalyzer({
      ...options,
      src: this.options.src,
      splitChannels: this.wf.params.splitChannels,
      decoderType: this.wf.params.decoderType,
      playerType: this.wf.params.playerType,
    });
 
    // If this failed to allocate an audio decoder, we can't continue
    if (!this.audio) {
      throw new Error("MediaLoader: Failed to allocate audio decoder");
    }
 
    // If there is an existing decoder promise,
    // wait for it to resolve and use the existing
    // audio decoder information
    if (await this.audio.sourceDecoded()) {
      this.duration = this.audio.duration;
      this.decoderResolve?.();
      return this.audio;
    }
 
    // Get the audio data from the url src
    const req = await this.performRequest(this.options.src).catch((err: any) => {
      console.error("An audio loading error occurred", err);
      return null;
    });
 
    if (req) {
      try {
        await this.audio.initDecoder(req);
 
        // Notify the waveform that the audio decoder is ready
        this.decoderResolve?.();
 
        // The audio instance could be removed if it was destroyed
        // while the decoder was being initialized.
        // If this is the case, we can't continue
        if (!this.audio) return null;
 
        // Get the duration from the audio file as soon as it is ready
        this.duration = this.audio.duration;
 
        // Proceed with the rest of the decoding
        await this.decodeAudioData();
 
        return this.audio ?? null;
      } catch (err) {
        this.wf.setError(
          `An error occurred while decoding the audio file. Please select another file or try again. ${err.message}`,
        );
        console.error("An audio decoding error occurred", err);
      }
    }
 
    return null;
  }
 
  /**
   * Load audio without decoding for fast loading of large files.
   * Only loads metadata to get duration and basic info.
   */
  private async loadWithoutDecoding(options: WaveformAudioOptions): Promise<WaveformAudio | null> {
    try {
      // Force HTML5 player when decoder is "none"
      const playerType = "html5";
 
      if (this.wf.params.playerType !== playerType) {
        console.warn(`Decoder "none" requires HTML5 player, switching from "${this.wf.params.playerType}"`);
      }
 
      // Create a simple HTML5 audio element to get metadata
      const audioElement = new Audio();
      audioElement.preload = "metadata";
 
      // Wait for metadata to load
      const metadataPromise = new Promise<void>((resolve, reject) => {
        audioElement.addEventListener("loadedmetadata", () => resolve(), {
          once: true,
        });
        audioElement.addEventListener("error", (e) => reject(e), {
          once: true,
        });
      });
 
      audioElement.src = this.options.src;
      await metadataPromise;
 
      // Create WaveformAudio instance with minimal setup
      this.createAnalyzer({
        ...options,
        src: this.options.src,
        decoderType: "none",
        playerType,
      });
 
      if (!this.audio) {
        throw new Error("MediaLoader: Failed to create audio instance");
      }
 
      // Set duration from HTML5 audio element
      this.duration = audioElement.duration;
      this.audio.setDurationWithoutDecoding(audioElement.duration);
 
      // Mark as loaded
      this.loaded = true;
 
      // Resolve decoder promise immediately since there's no decoding
      this.decoderPromise = Promise.resolve();
      this.decoderResolve?.();
 
      // Complete loading progress
      this.wf.setLoadingProgress(undefined, undefined, true);
 
      return this.audio;
    } catch (err) {
      this.wf.setError(
        `An error occurred while loading the audio file. Please select another file or try again. ${err.message}`,
      );
      console.error("An audio loading error occurred", err);
      return null;
    }
  }
 
  destroy() {
    if (this.isDestroyed) return;
 
    super.destroy();
    this.reset();
 
    if (this.audio) {
      this.audio.destroy();
      this.audio = null;
    }
  }
 
  private async performRequest(url: string): Promise<MediaResponse> {
    const xhr = new XMLHttpRequest();
 
    this.cancel = () => {
      xhr?.abort();
      this.cancel = () => {};
    };
 
    return new Promise<MediaResponse>((resolve, reject) => {
      xhr.responseType = "arraybuffer";
 
      const errorHandler = () => {
        const error = new Error(`HTTP error status: ${xhr.status}`);
 
        error.name = "HTTPError";
 
        this.wf.setError(`HTTP error status: ${xhr.status}`, error);
        reject(xhr);
      };
 
      xhr.addEventListener("progress", (e) => {
        if (e.lengthComputable) {
          this.loadingProgressType = "determinate";
          this.wf.setLoadingProgress(e.loaded, e.total);
        } else {
          this.loadingProgressType = "indeterminate";
          this.wf.setLoadingProgress(e.loaded, -1);
        }
      });
 
      xhr.addEventListener("load", async () => {
        this.wf.setLoadingProgress(undefined, undefined, true);
        resolve(xhr.response);
      });
 
      xhr.addEventListener("error", () => {
        errorHandler();
      });
 
      xhr.addEventListener("readystatechange", () => {
        if (xhr.readyState === 4 && xhr.status >= 400 && xhr.status !== 0) {
          errorHandler();
        }
      });
 
      // Handle relative urls, by converting them to absolute so any query params can be preserved
      const newUrl = new URL(url, /^https?/.exec(url) ? undefined : window.location.href);
 
      const signedUrlParams = [
        "X-Goog-Signature", // Google Cloud Storage
        "X-Amz-Signature", // S3|Minio|DigitalOcean|Backblaze
        "sig", // Azure
      ];
 
      // If the url is signed, we need to preserve the query params otherwise the signature will be invalid
      if (!signedUrlParams.some((p) => newUrl.searchParams.has(p))) {
        // Arbitrary setting of query param to stop caching from reusing any media requests which may have less headers
        // cached than this request. This is to prevent a CORS error when the headers are different between partial
        // content and full content requests.
        newUrl.searchParams.set("lsref", "1");
      }
 
      xhr.open("GET", newUrl.toString(), true);
      xhr.send();
    });
  }
 
  private createAnalyzer(options: WaveformAudioOptions): WaveformAudio {
    if (this.audio) return this.audio;
 
    this.audio = new WaveformAudio(options);
 
    this.audio.on("decodingProgress", (chunk, total) => {
      this.wf.setDecodingProgress(chunk, total);
    });
 
    return this.audio;
  }
}