diff --git a/client/assets/beep.wav b/client/assets/beep.wav new file mode 100644 index 0000000..6074514 Binary files /dev/null and b/client/assets/beep.wav differ diff --git a/client/stores/clicks.js b/client/stores/clicks.js index 1d5b2f6..1b05922 100644 --- a/client/stores/clicks.js +++ b/client/stores/clicks.js @@ -3,6 +3,7 @@ const animate = require('../utils/animate') const { start, checkForAccount } = require('../../src/') const validUrl = require('valid-url') const uuid = require('uuid/v1') +const AudioMachine = require('../utils/audio-machine') const { OBJECT, STRING, UNDEFINED, ARRAY, INTEGER, BOOL, FUNCTION } = require('../../src/utils') @@ -85,6 +86,11 @@ function store (state, emitter) { state.proofForm = { username: '', service: '', text: '' } state.navAnimation = true + // set up sound things + const audio = new AudioMachine(); + // http://soundbible.com/419-Tiny-Button-Push.html + audio.addSound('peerOnEnter', 'assets/beep.wav') + emitter.on('DOMContentLoaded', function () { animate.startAnimation('nav-animation') @@ -139,6 +145,7 @@ function store (state, emitter) { } emitter.emit('updatePeerProfile', profile) + audio.playSound('peerOnEnter') }, 'peer left': (message) => { diff --git a/client/utils/audio-machine.js b/client/utils/audio-machine.js new file mode 100644 index 0000000..8cdbd5d --- /dev/null +++ b/client/utils/audio-machine.js @@ -0,0 +1,37 @@ +const { isNode } = require('../../src/utils') +module.exports = class AudioMachine { + constructor () { + // enviornment check, currently node isn't supported + // TODO: make this work with node + if (isNode()){ + throw new Error('Can not instantiate AudioMachine in node env. Currently unsupported.') + } + + this.playlist = {} + } + + addSound (name, src) { + const newSound = {} + const soundElement = document.createElement('audio') + + soundElement.setAttribute('src', src); + newSound[name] = soundElement + + this.playlist = Object.assign( + newSound, + this.playlist + ) + } + + playSound (name) { + if (typeof name !== 'string') { + throw new TypeError('AudioMachine.playSound type of name is not string.') + } + + if (!this.playlist.hasOwnProperty(name)) { + throw new ReferenceError(`AudioMachine.playlist does not have sound ${name}`) + } + + this.playlist[name].play() + } +} diff --git a/src/utils.js b/src/utils.js index 0f8ad4a..51aeddb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -9,7 +9,15 @@ function a2t (arrayBuffer) { return new TextDecoder("utf-8").decode(arrayBuffer) } +function isNode () { + if (typeof process === 'undefined') { + return false + } + return true +} + module.exports = { + isNode: isNode, a2t: a2t, t2a: t2a, STRING: 'string',