I try to create an audiobuffer whose length depends on the length/duration of the audiofile.
I use the web Audio API https://webaudio.github.io/web-audio-api/#offlineaudiocontext
function getDuration(filename) {
// Send XHR request for audio file to server.
const request = new XMLHttpRequest();
request.open('GET', gorilla.stimuliURL(filename));
request.responseType = 'arraybuffer';
request.onload = function() {
// Decode the audio.
audioContext.decodeAudioData(request.response, function(buffer) {
// Load and arm the source buffer.
var tStimNew = buffer.duration;
});
}
request.send();
}
I get the duration of the audiofile using this function. I tested it and tStimNew is exactly what I expected. The problem seems to be that the variable tStimNew is only local to the function. But I would like to have the variable global to be able to use it further.
Where/how exactly do I have to include a return? The nesting of the three functions makes it too complex for me.
Thank you in advance!