Created
June 13, 2017 04:47
-
-
Save zcaceres/0810451c9acc7284a352c51c18b18cf2 to your computer and use it in GitHub Desktop.
JS Sample 2
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
const playAudio = (dataArr) => { | |
const audioArrBuff = convertDataStreamToAudioArrayBuffer(dataArr) | |
// ^ converts raw data into an audio array buffer so that audio node can play it | |
const source = audioContext.createBufferSource() | |
// ^ prepares audio context to receive array buffer with a new buffer source | |
// Registers an event listener to play 'Quindar Beep/Tone' at end of transmission | |
source.onended = () => { | |
// When audio source has stopped playing... | |
NASABeep && NASABeep.play() // bullet proofing for some smart phones that do not support all parts of WebAudio API | |
// Hides UI indicator if Pilot | |
toggleHUDIndicatorVisible(transmissionIncomingIndicator, false) | |
if (audioQueue.length > 0) playAudio(audioQueue.shift()) | |
// ^ Call playAudio again if multiple messages are queued for playing | |
} | |
processRadioTransmission(audioContext, source) | |
// ^ Transform raw audio into a 'radio transmission' | |
// Transforms ArrayBuffer into AudioBuffer then plays | |
audioContext.decodeAudioData(audioArrBuff) | |
.then(decodedAudio => { | |
audioSourceIsPlaying = true | |
source.buffer = decodedAudio | |
source.start() | |
// Displays UI indicator if Pilot | |
toggleHUDIndicatorVisible(transmissionIncomingIndicator, true) | |
}) | |
.catch(err => console.error('decodeAudioData threw: ', err)) // your preferred error handling here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment