From 3f8bcd5a0e063c5a4c3d916e09b9d9826dab0c12 Mon Sep 17 00:00:00 2001 From: Evan Theurer Date: Tue, 5 May 2015 13:45:19 +0200 Subject: [PATCH] Add settings option to disable sound effects. --- static/js/controllers/appcontroller.js | 5 +- static/js/services/playsound.js | 231 +++++++++++++------------ static/partials/settings.html | 13 ++ 3 files changed, 135 insertions(+), 114 deletions(-) diff --git a/static/js/controllers/appcontroller.js b/static/js/controllers/appcontroller.js index 2d3d6dfd..950a9bb0 100644 --- a/static/js/controllers/appcontroller.js +++ b/static/js/controllers/appcontroller.js @@ -55,7 +55,8 @@ define(["jquery", "angular", "underscore"], function($, angular, _) { audioTypingNoiseDetection: true, videoLeakyBucket: true, videoNoiseReduction: false - } + }, + playSoundEffects: true } }; $scope.master = angular.copy($scope.defaults); @@ -119,4 +120,4 @@ define(["jquery", "angular", "underscore"], function($, angular, _) { }]; -}); \ No newline at end of file +}); diff --git a/static/js/services/playsound.js b/static/js/services/playsound.js index eb682416..caf7dbaf 100644 --- a/static/js/services/playsound.js +++ b/static/js/services/playsound.js @@ -22,141 +22,148 @@ "use strict"; define(['underscore', 'Howler', 'require'], function(_, Howler, require) { - var SoundInterval = function(sound, id, time) { - this.sound = sound; - this.id = id; - this.interval = null; - this.time = time; - }; - SoundInterval.prototype.start = function() { - if (this.interval !== null) { - return; - } - var id = this.id; - var player = _.bind(function() { - return this.sound.play(id); - }, this); - player(); - this.interval = setInterval(player, this.time); - }; - SoundInterval.prototype.stop = function() { - clearInterval(this.interval); - this.interval = null; - delete this.sound.intervals[this.id]; - }; - - var Sound = function(options, aliases) { - - this.sound = null; - this.intervals = {}; - if (options) { - this.initialize(options, aliases); - } + // playSound + return ["appData", function(appData) { - }; + var SoundInterval = function(sound, id, time) { + this.sound = sound; + this.id = id; + this.interval = null; + this.time = time; + }; + SoundInterval.prototype.start = function() { + if (this.interval !== null) { + return; + } + var id = this.id; + var player = _.bind(function() { + return this.sound.play(id); + }, this); + player(); + this.interval = setInterval(player, this.time); + }; + SoundInterval.prototype.stop = function() { + clearInterval(this.interval); + this.interval = null; + delete this.sound.intervals[this.id]; + }; - Sound.prototype.initialize = function(options, aliases) { + var Sound = function(options, aliases) { - // Kill all the existing stuff if any. - if (this.sound) { - this.sound.stop(); - } - _.each(this.intervals, function(i) { - i.stop(); - }); - this.intervals = {}; - - // Add error handler. - var onloaderror = options.onloaderror; - options.onloaderror = function(event) { - console.error("Failed to load sounds", event); - if (onloaderror) { - onloaderror.apply(this, arguments); + this.sound = null; + this.intervals = {}; + if (options) { + this.initialize(options, aliases); } + }; - // Replace urls with their require generated URLs. - var urls = options.urls; - if (urls) { - var new_urls = []; - _.each(urls, function(u) { - u = require.toUrl(u); - new_urls.push(u); + Sound.prototype.initialize = function(options, aliases) { + + // Kill all the existing stuff if any. + if (this.sound) { + this.sound.stop(); + } + _.each(this.intervals, function(i) { + i.stop(); }); - options.urls = new_urls; - } + this.intervals = {}; + + // Add error handler. + var onloaderror = options.onloaderror; + options.onloaderror = function(event) { + console.error("Failed to load sounds", event); + if (onloaderror) { + onloaderror.apply(this, arguments); + } + }; + + // Replace urls with their require generated URLs. + var urls = options.urls; + if (urls) { + var new_urls = []; + _.each(urls, function(u) { + u = require.toUrl(u); + new_urls.push(u); + }); + options.urls = new_urls; + } - // Create the new shit. - this.players = {}; - this.aliases = _.extend({}, aliases); - this.sound = new Howler.Howl(options); + // Create the new shit. + this.players = {}; + this.aliases = _.extend({}, aliases); + this.sound = new Howler.Howl(options); - return this; + return this; - }; + }; - Sound.prototype.getId = function(id) { + Sound.prototype.getId = function(id) { - if (this.aliases.hasOwnProperty(id)) { - return this.aliases[id]; - } - return id; + if (this.aliases.hasOwnProperty(id)) { + return this.aliases[id]; + } + return id; - }; + }; + Sound.prototype.play = function(id, interval, autostart) { - Sound.prototype.play = function(id, interval, autostart) { + if (!this.sound) { + console.log("Play sound but not initialized.", id); + return null; + } + if (!this.shouldPlaySound(id)) { + return; + } - if (!this.sound) { - console.log("Play sound but not initialized.", id); - return null; - } + id = this.getId(id); - id = this.getId(id); + if (interval) { - if (interval) { + if (this.intervals.hasOwnProperty(id)) { + return this.intervals[id]; + } + var i = this.intervals[id] = new SoundInterval(this, id, interval); + if (autostart) { + i.start(); + } + return i; + + } else { + + var player = this.players[id]; + var sound = this.sound; + if (!player) { + player = this.players[id] = (function(id) { + var data = {}; + var cb = function(soundId) { + data.soundId = soundId; + }; + var play = _.debounce(function() { + if (data.soundId) { + sound.stop(data.soundId); + data.soundId = null; + } + sound.play(id, cb); + }, 10); + return play; + }(id)); + } + player() - if (this.intervals.hasOwnProperty(id)) { - return this.intervals[id]; } - var i = this.intervals[id] = new SoundInterval(this, id, interval); - if (autostart) { - i.start(); - } - return i; - - } else { - - var player = this.players[id]; - var sound = this.sound; - if (!player) { - player = this.players[id] = (function(id) { - var data = {}; - var cb = function(soundId) { - data.soundId = soundId; - }; - var play = _.debounce(function() { - if (data.soundId) { - sound.stop(data.soundId); - data.soundId = null; - } - sound.play(id, cb); - }, 10); - return play; - }(id)); - } - player() - - } - }; + }; - // Active initialized sound instances are kept here. - var registry = {}; - window.PLAYSOUND = registry; // make available for debug. + Sound.prototype.shouldPlaySound = function (id) { + var data = appData.get(); + return data && data.master.settings.playSoundEffects; + }; - // playSound - return [function() { + // Active initialized sound instances are kept here. + var registry = {}; + window.PLAYSOUND = registry; // make available for debug. return { initialize: function(options, name, aliases) { diff --git a/static/partials/settings.html b/static/partials/settings.html index c894303e..54898383 100644 --- a/static/partials/settings.html +++ b/static/partials/settings.html @@ -265,6 +265,19 @@ +
+ {{_('Sound effects')}} +
+
+
+ +
+
+
+
+