From 0f6cdbd1625703b666ae5d5fdbbefa5bbd790c8b Mon Sep 17 00:00:00 2001 From: Micrufun Date: Thu, 12 Jan 2023 19:12:30 +0330 Subject: [PATCH 1/4] TODO --- src/track/loader/Loader.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/track/loader/Loader.js b/src/track/loader/Loader.js index 14793440..346d189b 100644 --- a/src/track/loader/Loader.js +++ b/src/track/loader/Loader.js @@ -35,6 +35,8 @@ export default class { fileLoad(e) { const audioData = e.target.response || e.target.result; + // TODO: Here, detect if the `audioData` is of type MIDI. + this.setStateChange(STATE_DECODING); return new Promise((resolve, reject) => { From daaff61db2ae56b678b21ecb89e1ba6e00d98dce Mon Sep 17 00:00:00 2001 From: Micrufun Date: Sun, 29 Jan 2023 17:53:39 +0330 Subject: [PATCH 2/4] Fetch API --- src/track/loader/FetchLoader.js | 18 ++++++++++++++++++ src/track/loader/LoaderFactory.js | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/track/loader/FetchLoader.js diff --git a/src/track/loader/FetchLoader.js b/src/track/loader/FetchLoader.js new file mode 100644 index 00000000..de9eb15e --- /dev/null +++ b/src/track/loader/FetchLoader.js @@ -0,0 +1,18 @@ +import Loader from "./Loader"; + +export default class extends Loader { + /** + * Loads an audio file via fetch API. + */ + load() { + return new Promise((resolve, reject) => { + fetch(this.src) + .then((data) => data.arrayBuffer()) + .then((arrayBuffer) => super.ac.decodeAudioData(arrayBuffer)) + .then((decodedAudio) => resolve(decodedAudio)) + .catch((err) => { + reject(Error(`Track ${this.src} failed to load with error: ${err}`)); + }); + }); + } +} diff --git a/src/track/loader/LoaderFactory.js b/src/track/loader/LoaderFactory.js index 5541436c..310bfc14 100644 --- a/src/track/loader/LoaderFactory.js +++ b/src/track/loader/LoaderFactory.js @@ -1,6 +1,7 @@ import BlobLoader from "./BlobLoader"; import IdentityLoader from "./IdentityLoader"; import XHRLoader from "./XHRLoader"; +import FetchLoader from "./FetchLoader"; export default class { static createLoader(src, audioContext, ee) { @@ -9,7 +10,8 @@ export default class { } else if (src instanceof AudioBuffer) { return new IdentityLoader(src, audioContext, ee); } else if (typeof src === "string") { - return new XHRLoader(src, audioContext, ee); + //return new XHRLoader(src, audioContext, ee); + return new FetchLoader(src, audioContext, ee); } throw new Error("Unsupported src type"); From 2d193cfabb87e59b33793bbdb905787037b8709e Mon Sep 17 00:00:00 2001 From: Micrufun Date: Mon, 30 Jan 2023 15:54:38 +0330 Subject: [PATCH 3/4] Fix error --- src/track/loader/FetchLoader.js | 2 +- src/track/loader/Loader.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/track/loader/FetchLoader.js b/src/track/loader/FetchLoader.js index de9eb15e..689fe6eb 100644 --- a/src/track/loader/FetchLoader.js +++ b/src/track/loader/FetchLoader.js @@ -8,7 +8,7 @@ export default class extends Loader { return new Promise((resolve, reject) => { fetch(this.src) .then((data) => data.arrayBuffer()) - .then((arrayBuffer) => super.ac.decodeAudioData(arrayBuffer)) + .then((arrayBuffer) => super.fetchLoad(arrayBuffer)) .then((decodedAudio) => resolve(decodedAudio)) .catch((err) => { reject(Error(`Track ${this.src} failed to load with error: ${err}`)); diff --git a/src/track/loader/Loader.js b/src/track/loader/Loader.js index 346d189b..d0b0ef6c 100644 --- a/src/track/loader/Loader.js +++ b/src/track/loader/Loader.js @@ -59,4 +59,28 @@ export default class { ); }); } + + fetchLoad(arrayBuffer) { + this.setStateChange(STATE_DECODING); + + return new Promise((resolve, reject) => { + this.ac.decodeAudioData( + arrayBuffer, + (audioBuffer) => { + this.audioBuffer = audioBuffer; + this.setStateChange(STATE_FINISHED); + + resolve(audioBuffer); + }, + (err) => { + if (err === null) { + // Safari issues with null error + reject(Error("MediaDecodeAudioDataUnknownContentType")); + } else { + reject(err); + } + } + ); + }); + } } From b3a790ad92601c063285646ef52494efeeec9248 Mon Sep 17 00:00:00 2001 From: Micrufun Date: Mon, 30 Jan 2023 15:56:29 +0330 Subject: [PATCH 4/4] Revert "TODO" This reverts commit 0f6cdbd1625703b666ae5d5fdbbefa5bbd790c8b. --- src/track/loader/Loader.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/track/loader/Loader.js b/src/track/loader/Loader.js index d0b0ef6c..e3c6a7f1 100644 --- a/src/track/loader/Loader.js +++ b/src/track/loader/Loader.js @@ -35,8 +35,6 @@ export default class { fileLoad(e) { const audioData = e.target.response || e.target.result; - // TODO: Here, detect if the `audioData` is of type MIDI. - this.setStateChange(STATE_DECODING); return new Promise((resolve, reject) => {