This repository has been archived by the owner on May 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Load sounds as buffers via XHR - Generate buttons (temp style pending #8) - Play sound when button pushed - Temp sounds pending #6
- Loading branch information
1 parent
530ee53
commit 062b010
Showing
5 changed files
with
111 additions
and
0 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
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
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,75 @@ | ||
'use strict'; | ||
|
||
function SoundEffect(options) { | ||
const buttonContainer = typeof options.buttonContainer === 'string' ? | ||
document.querySelector(options.buttonContainer) : | ||
options.buttonContainer; | ||
|
||
const src = options.src; | ||
const context = options.context; | ||
|
||
let buffer = null; | ||
|
||
const sources = []; | ||
|
||
// load audio file | ||
// todo: load appropriate format based on browser support | ||
const xhr = new XMLHttpRequest(); | ||
xhr.responseType = 'arraybuffer'; | ||
xhr.onload = () => { | ||
context.decodeAudioData(xhr.response, decodedBuffer => { | ||
buffer = decodedBuffer; | ||
console.log('loaded buffer', src, buffer); | ||
}); | ||
}; | ||
xhr.onerror = e => { | ||
// keep trying | ||
console.warn('Error loading audio', src, e); | ||
}; | ||
xhr.open('GET', src, true); | ||
xhr.send(); | ||
|
||
let stopSource; | ||
function stopEvent(evt) { | ||
stopSource(evt.target); | ||
} | ||
|
||
stopSource = function (source) { | ||
try { | ||
source.removeEventListener('ended', stopEvent); | ||
source.disconnect(context.destination); | ||
source.stop(0); | ||
} catch (e) {} | ||
const i = sources.indexOf(source); | ||
if (i >= 0) { | ||
sources.splice(i, 1); | ||
} | ||
}; | ||
|
||
this.play = () => { | ||
if (buffer) { | ||
const source = context.createBufferSource(); | ||
source.buffer = buffer; | ||
source.connect(context.destination); | ||
source.addEventListener('ended', stopEvent); | ||
source.start(0); | ||
sources.push(source); | ||
} | ||
}; | ||
|
||
this.stop = () => { | ||
while (sources.length) { | ||
stopSource(sources[0]); | ||
} | ||
}; | ||
|
||
/* | ||
Use better-looking button design #8 | ||
*/ | ||
const button = document.createElement('button'); | ||
button.appendChild(document.createTextNode(options.name || 'Sound')); | ||
button.addEventListener('click', this.play); | ||
buttonContainer.appendChild(button); | ||
} | ||
|
||
export default SoundEffect; |
Binary file not shown.
Binary file not shown.