Skip to content

Commit

Permalink
Simplify audio controller, fix bg loop
Browse files Browse the repository at this point in the history
  • Loading branch information
milcktoast committed Sep 21, 2015
1 parent d55a3ea commit d25bbfc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
34 changes: 12 additions & 22 deletions static/js/controllers/AudioController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ var Tweens = App.Tweens;
App.AudioController = AudioController;
function AudioController(config) {
this.baseUrl = config.baseUrl;

this._loops = [];
this._tweens = { volume : 0 };
this._volume = 0;
this.volume = 0;
this.distance = 0;
this.tween = Tweens.factorTween({ volume : 0 } , 0.05);

Howler.volume(0);
this.tween = Tweens.factorTween(this._tweens, 0.05);
}

AudioController.create = App.ctor(AudioController);
App.Dispatcher.extend(AudioController.prototype);
AudioController.prototype.VOLUME_ZERO = 0.001;

AudioController.prototype.createSound = function (path, params) {
params = params || {};
Expand All @@ -26,34 +25,25 @@ AudioController.prototype.createSound = function (path, params) {
return new Howl(params);
};

AudioController.prototype.setVolume = function (volume) {
this._volume = volume;
};

AudioController.prototype.setDistance = function (dist) {
this._dist = dist;
};

var ZERO = 0.001;
AudioController.prototype.update = function () {
var tweenFactor = this._volume > 0 ? 0.005 : 0.1;
var volume = this.tween('volume', this._volume, tweenFactor);
var tweenFactor = this.volume > 0 ? 0.005 : 0.1;
var volume = this.tween('volume', this.volume, tweenFactor);

volume *= (1 - this._dist);
volume *= (1 - this.distance);

if (volume !== this._volume) {
if (volume !== this.volume) {
Howler.volume(volume);
}

if (this._isMuted && volume > ZERO) {
if (this.isMuted && volume > this.VOLUME_ZERO) {
Howler.unmute();
this.triggerListeners('unmute');
this._isMuted = false;
this.isMuted = false;
}

if (!this._isMuted && volume <= ZERO) {
if (!this.isMuted && volume <= this.VOLUME_ZERO) {
Howler.mute();
this.triggerListeners('mute');
this._isMuted = true;
this.isMuted = true;
}
};
56 changes: 39 additions & 17 deletions static/js/scenes/MainScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,24 @@ MainScene.prototype.initAudio = function () {
this.createWaveSounds();
this.createBubbleSounds();

audio.addListener('mute', this, 'muteSounds');
audio.addListener('unmute', this, 'unmuteSounds');

this.medusae.addListener('phase:top', this, 'audioPhaseTop');

this.sounds.bg.on('end', this.createBackgroundSound.bind(this));
this.sounds.bg.on('load', this.triggerListeners.bind(this, 'load:audio', null));
this.sounds.bg.on('load', function () {
audio.addListener('mute', this, 'muteSounds');
audio.addListener('unmute', this, 'unmuteSounds');
this.medusae.addListener('phase:top', this, 'audioPhaseTop');
this.triggerListeners('load:audio');
}.bind(this));
};

MainScene.prototype.createBackgroundSound = function () {
this.sounds.bg = this.audio.createSound('bg-loop', { loop : false });
this.sounds.bg.play();
var bg = this.sounds.bg = this.audio.createSound('bg-loop');

bg.__pos = 0;
bg.__duration = Infinity;

bg.on('load', function () {
bg.__duration = Math.floor(bg._duration) * 1000;
bg.play();
}.bind(this));
};

MainScene.prototype.createWaveSounds = function () {
Expand All @@ -379,25 +385,25 @@ MainScene.prototype.createBubbleSounds = function () {
};

MainScene.prototype.muteSounds = function () {
this.sounds.bg.stop();
this.sounds.bg.pause();
};

MainScene.prototype.unmuteSounds = function () {
this.createBackgroundSound(); // FIXME
this.sounds.bg.play();
};

MainScene.prototype.beginAudio = function () {
this.audio.setVolume(0.8);
this._audioIsPlaying = true;
this.audio.volume = 0.8;
this.audioIsPlaying = true;
};

MainScene.prototype.pauseAudio = function () {
this.audio.setVolume(0);
this._audioIsPlaying = false;
this.audio.volume = 0;
this.audioIsPlaying = false;
};

MainScene.prototype.toggleAudio = function () {
if (this._audioIsPlaying) {
if (this.audioIsPlaying) {
this.pauseAudio();
} else {
this.beginAudio();
Expand Down Expand Up @@ -476,12 +482,28 @@ MainScene.prototype.update = function (delta) {
this.lensDirtPass.update(delta);
}

this.audio.setDistance(distSound);
if (!this.audio.isMuted) {
this.updateSounds(delta);
}

this.audio.distance = distSound;
this.audio.update(delta);

if (DEBUG_NUDGE) { this.updateDebugNudge(delta); }
};

MainScene.prototype.updateSounds = function (delta) {
var sounds = this.sounds;
var bg = sounds.bg;

bg.__pos += delta;

if (bg.__pos > bg.__duration) {
bg.pos(0);
bg.__pos = 0;
}
};

MainScene.prototype.preRender = function (delta, stepProgress) {
this.controls.update();
this.medusae.updateTweens(delta);
Expand Down

0 comments on commit d25bbfc

Please # to comment.