-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from yeatmanlab/adding-audio-buttons-child-su…
…rvey Adding audio buttons child survey
- Loading branch information
Showing
2 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export class BufferLoader { | ||
constructor(context, urlListMap, callback) { | ||
this.context = context; | ||
this.urlListMap = urlListMap; | ||
this.onload = callback; | ||
this.bufferList = new Array(); | ||
this.loadCount = 0; | ||
} | ||
|
||
loadBuffer(url, index) { | ||
const request = new XMLHttpRequest(); | ||
request.open('GET', url, true); | ||
request.responseType = 'arraybuffer'; | ||
request.onload = () => { | ||
// Asynchronously decode the audio file data in request.response | ||
this.context.decodeAudioData( | ||
request.response, | ||
(buffer) => { | ||
if (!buffer) { | ||
alert('error decoding file data: ' + url); | ||
return; | ||
} | ||
this.bufferList[index] = buffer; | ||
this.loadCount += 1; | ||
if (this.loadCount === Object.keys(this.urlListMap).length) this.onload(this.bufferList); | ||
}, | ||
(error) => { | ||
console.error('decodeAudioData error', error); | ||
}, | ||
); | ||
}; | ||
|
||
request.onerror = (error) => { | ||
console.error('Request error', error); | ||
}; | ||
|
||
request.send(); | ||
} | ||
|
||
load() { | ||
Object.keys(this.urlListMap).forEach((key) => { | ||
this.loadBuffer(this.urlListMap[key], key); | ||
}); | ||
} | ||
} | ||
|
||
export const AudioContext = window.AudioContext || window.webkitAudioContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters