Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched from XMLHttpRequest to fetch with try/catch #995

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/parse/abc_parse_key_voice.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,10 @@ var parseKeyVoice = {};
warn("Expected value for stems in voice: " + attr.warn, line, start);
else if (attr.err !== undefined)
warn("Expected value for stems in voice: " + attr.err, line, start);
else if (attr.token === 'up' || attr.token === 'down')
else if (attr.token === 'up' || attr.token === 'down' || attr.token === 'auto')
multilineVars.voices[id].stem = attr.token;
else
warn("Expected up or down for voice stem", line, start);
warn("Expected up, down, or auto for voice stem", line, start);
start += attr.len;
break;
case 'up':
Expand Down
65 changes: 38 additions & 27 deletions src/synth/load-note.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,45 @@ var getNote = function (url, instrument, name, audioContext) {

if (!instrumentCache[name])
instrumentCache[name] = new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
let noteUrl = url + instrument + "-mp3/" + name + ".mp3";
xhr.open("GET", noteUrl, true);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
if (xhr.status !== 200) {
reject(Error("Can't load sound at " + noteUrl + ' status=' + xhr.status));
return
}
var noteDecoded = function(audioBuffer) {
resolve({instrument: instrument, name: name, status: "loaded", audioBuffer: audioBuffer})
}
var maybePromise = audioContext.decodeAudioData(xhr.response, noteDecoded, function () {
reject(Error("Can't decode sound at " + noteUrl));
});
// In older browsers `BaseAudioContext.decodeAudio()` did not return a promise
if (maybePromise && typeof maybePromise.catch === "function") maybePromise.catch(reject);
};
xhr.onerror = function () {
reject(Error("Can't load sound at " + noteUrl));
};
xhr.send();
})
.catch(err => {
console.error("Didn't load note", instrument, name, ":", err.message);
throw err;
});
// Use the fetch API instead of XMLHttpRequest
fetch(noteUrl)
.then(response => {

try{

if (!response.ok){
throw new Error(`HTTP error, status = ${response.status}`);
}

response.arrayBuffer().then(theBuffer => {
var noteDecoded = function noteDecoded(audioBuffer) {
resolve({
instrument: instrument,
name: name,
status: "loaded",
audioBuffer: audioBuffer
});
};

var maybePromise = audioContext.decodeAudioData(theBuffer, noteDecoded, function () {
reject(Error("Can't decode sound at " + noteUrl));
});

});

}
catch(error){
//console.log("note fetch error: "+error);
reject(Error("Can't load sound at " + noteUrl + ' status=' + error));
}

})
.catch(error => {
reject(Error("Can't load sound at " + noteUrl + ' status=' + error));
throw error;
});

});
return instrumentCache[name];
};

Expand Down