Browse Source

Merge pull request #208 from theurere/disable-sound-setting

Settings option to control app sounds
pull/217/head
Simon Eisenmann 11 years ago
parent
commit
be26aea188
  1. 5
      static/js/controllers/appcontroller.js
  2. 231
      static/js/services/playsound.js
  3. 13
      static/partials/settings.html

5
static/js/controllers/appcontroller.js

@ -55,7 +55,8 @@ define(["jquery", "angular", "underscore"], function($, angular, _) {
audioTypingNoiseDetection: true, audioTypingNoiseDetection: true,
videoLeakyBucket: true, videoLeakyBucket: true,
videoNoiseReduction: false videoNoiseReduction: false
} },
playSoundEffects: true
} }
}; };
$scope.master = angular.copy($scope.defaults); $scope.master = angular.copy($scope.defaults);
@ -119,4 +120,4 @@ define(["jquery", "angular", "underscore"], function($, angular, _) {
}]; }];
}); });

231
static/js/services/playsound.js

@ -22,141 +22,148 @@
"use strict"; "use strict";
define(['underscore', 'Howler', 'require'], function(_, Howler, require) { define(['underscore', 'Howler', 'require'], function(_, Howler, require) {
var SoundInterval = function(sound, id, time) { // playSound
this.sound = sound; return ["appData", function(appData) {
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);
}
}; 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. this.sound = null;
if (this.sound) { this.intervals = {};
this.sound.stop(); if (options) {
} this.initialize(options, aliases);
_.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);
} }
}; };
// Replace urls with their require generated URLs. Sound.prototype.initialize = function(options, aliases) {
var urls = options.urls;
if (urls) { // Kill all the existing stuff if any.
var new_urls = []; if (this.sound) {
_.each(urls, function(u) { this.sound.stop();
u = require.toUrl(u); }
new_urls.push(u); _.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. // Create the new shit.
this.players = {}; this.players = {};
this.aliases = _.extend({}, aliases); this.aliases = _.extend({}, aliases);
this.sound = new Howler.Howl(options); 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)) { if (this.aliases.hasOwnProperty(id)) {
return this.aliases[id]; return this.aliases[id];
} }
return 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) { id = this.getId(id);
console.log("Play sound but not initialized.", id);
return null;
}
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. Sound.prototype.shouldPlaySound = function (id) {
var registry = {}; var data = appData.get();
window.PLAYSOUND = registry; // make available for debug. return data && data.master.settings.playSoundEffects;
};
// playSound // Active initialized sound instances are kept here.
return [function() { var registry = {};
window.PLAYSOUND = registry; // make available for debug.
return { return {
initialize: function(options, name, aliases) { initialize: function(options, name, aliases) {

13
static/partials/settings.html

@ -265,6 +265,19 @@
</div> </div>
</div> </div>
<div>
<legend>{{_('Sound effects')}}</legend>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="user.settings.playSoundEffects"> {{_('Play Sound Effects')}}
</label>
</div>
</div>
</div>
</div>
<hr/> <hr/>
<div class="form-group"> <div class="form-group">

Loading…
Cancel
Save