From d47f30448e401185895ab7e631ac5f1a448ea8ab Mon Sep 17 00:00:00 2001 From: William Toohey Date: Tue, 15 Dec 2015 16:18:37 +1000 Subject: [PATCH] AutoSong! + other minor fixes --- css/style.css | 1 + js/HuesCore.js | 98 +++++++++++++++++----------------------------- js/HuesSettings.js | 6 +++ js/SoundManager.js | 21 +++++++--- 4 files changed, 60 insertions(+), 66 deletions(-) diff --git a/css/style.css b/css/style.css index 6d3a134..191d2bb 100644 --- a/css/style.css +++ b/css/style.css @@ -258,6 +258,7 @@ label.settings-label:hover { font-family: 'PetMe64Web'; font-size: 7pt; padding: 3px; + margin: -6px 0; background: rgba(127,127,127, 0.5); border-color: rgb(0,0,0); border-width: 1px; diff --git a/js/HuesCore.js b/js/HuesCore.js index e303acf..ee7b1de 100644 --- a/js/HuesCore.js +++ b/js/HuesCore.js @@ -26,19 +26,16 @@ function HuesCore(defaults) { // Bunch-o-initialisers this.version = "0x01"; this.beatIndex = 0; - this.beatLength=-1; + this.beatLength = -1; this.currentSong = null; this.currentImage = null; - this.songIndex=-1; - this.colourIndex=0x3f; - this.imageIndex=-1; + this.songIndex = -1; + this.colourIndex = 0x3f; + this.imageIndex = -1; this.isFullAuto = true; this.invert = false; - this.loopCount=0; - this.fadeOut=false; - this.fadeDirection=false; - this.loadedFiles=0; - this.doBuildup=true; + this.loopCount = 0; + this.doBuildup = true; this.userInterface = null; var that = this; @@ -54,7 +51,7 @@ function HuesCore(defaults) { this.lastSongArray = []; this.lastImageArray = []; this.settings = new HuesSettings(defaults); - //this.autoSong = this.settings.autosong; + this.autoSong = localStorage["autoSong"]; this.resourceManager = new Resources(this); this.soundManager = new SoundManager(this); if(!this.soundManager.canUse) { @@ -181,6 +178,9 @@ HuesCore.prototype.animationLoop = function() { var beat = this.getBeat(this.beatIndex); this.beater(beat); } + if(Math.floor(now / this.soundManager.loopLength) > this.loopCount) { + this.onLoop(); + } requestAnimationFrame(function() {that.animationLoop();}); }; @@ -230,6 +230,7 @@ HuesCore.prototype.setSong = function(index) { if(this.currentSong == this.resourceManager.enabledSongs[index]) { return; } + this.lastSongArray.push(index); this.songIndex = index; this.currentSong = this.resourceManager.enabledSongs[this.songIndex]; if (this.currentSong === undefined) { @@ -284,37 +285,48 @@ HuesCore.prototype.randomSong = function() { } else { console.log("Randoming a song!"); this.setSong(index); - this.lastSongArray.push(index); var noRepeat = Math.min(5, Math.floor((this.resourceManager.enabledSongs.length / 2))); while (this.lastSongArray.length > noRepeat && noRepeat >= 0) { this.lastSongArray.shift(); } } }; -/* + HuesCore.prototype.onLoop = function() { - this.loopCount = this.loopCount + 1; - switch (this.settings.autosong) { + this.loopCount++; + switch (localStorage["autoSong"]) { case "loop": console.log("Checking loops"); - if (this.loopCount >= this.settings.autosongDelay) { - this.startSongChangeFade(); + if (this.loopCount >= localStorage["autoSongDelay"]) { + this.doAutoSong(); } break; case "time": console.log("Checking times"); - if (this.currentSong.sound && this.calculateSongLength(this.currentSong.sound) / 1000 * this.loopCount >= this.settings.autosongDelay * 60) { - this.startSongChangeFade(); + if (this.soundManager.loopLength * this.loopCount >= localStorage["autoSongDelay"] * 60) { + this.doAutoSong(); } break; } } -HuesCore.prototype.startSongChangeFade = function() { - this.fadeDirection = true; - this.fadeOut = true; +HuesCore.prototype.doAutoSong = function() { + var func = null; + var that = this; + if(localStorage["autoSongShuffle"] == "on") { + func = this.randomSong; + } else { + func = this.nextSong; + } + if(localStorage["autoSongFadeout"] == "on") { + this.soundManager.fadeOut(function() { + func.call(that); + }); + } else { + func.call(that); + } } -*/ + HuesCore.prototype.songDataUpdated = function() { if (this.currentSong) { this.beatLength = 0; @@ -538,46 +550,10 @@ HuesCore.prototype.toggleInvert = function() { this.setInvert(!this.invert); } -/*HuesCore.prototype.enterFrame = function() { - this.setTexts(); - if (this.fadeOut) { - // 30fps frame locked, TODO - delta = this.fadeDirection ? -2 : 2; - if (this.soundChannel) { - fadeTo = Math.max(0, Math.min(this.currentVolume, soundManager.volume + delta)); - this.soundManager.setVolume(fadeTo); - if (fadeTo == 0) { - this.fadeOut = false; - this.fadeDirection = false; - if (this.settings.autosongShuffle) { - this.randomSong(); - } else { - this.nextSong(); - } - } - } - } -}*/ - HuesCore.prototype.respackLoaded = function() { this.init(); }; -/*HuesCore.prototype.rightClickListener = function(event) { - switch (event) { - case flash.events.MouseEvent.RIGHT_CLICK: - this.toggleSettingsWindow(); - break; - case flash.events.MouseEvent.MOUSE_WHEEL: - if (event.delta > 0) { - this.soundManager.increaseVolume(); - } else { - this.soundManager.decreaseVolume(); - } - break; - } -}*/ - HuesCore.prototype.changeUI = function(index) { if (index >= 0 && this.uiArray.length > index && this.userInterface != this.uiArray[index]) { this.hideLists(); @@ -647,11 +623,11 @@ HuesCore.prototype.settingsUpdated = function() { } break; } - /*if (this.autoSong == "off" && !(this.settings.autosong == "off")) { + if (this.autoSong == "off" && localStorage["autoSong"] != "off") { console.log("Resetting loopCount since AutoSong was enabled"); this.loopCount = 0; - this.autoSong = this.settings.autosong; - }*/ + } + this.autoSong = localStorage["autoSong"]; }; HuesCore.prototype.enabledChanged = function() { diff --git a/js/HuesSettings.js b/js/HuesSettings.js index 0ae04da..20a7760 100644 --- a/js/HuesSettings.js +++ b/js/HuesSettings.js @@ -55,6 +55,7 @@ HuesSettings.prototype.defaultSettings = { autoSong: "off", autoSongDelay: 5, // loops or minutes depending on autoSong value autoSongShuffle: "on", + autoSongFadeout: "on", volume: 0.7 }; @@ -79,6 +80,7 @@ HuesSettings.prototype.settingsCategories = { "Functionality" : [ "autoSong", "autoSongShuffle", + "autoSongFadeout", "smartAlign" ], "Interface" : [ @@ -169,6 +171,10 @@ HuesSettings.prototype.settingsOptions = { autoSongShuffle : { name : "AutoSong shuffle", options : ["off", "on"] + }, + autoSongFadeout : { + name : "AutoSong fade out", + options : ["off", "on"] } }; diff --git a/js/SoundManager.js b/js/SoundManager.js index 63fd15a..b8e111e 100644 --- a/js/SoundManager.js +++ b/js/SoundManager.js @@ -109,6 +109,11 @@ SoundManager.prototype.playSong = function(song, playBuild, callback) { if(!song || (!song.sound)) { // null song return; } + + // if there's a fadeout happening from AutoSong, kill it + this.gainNode.gain.cancelScheduledValues(0); + // Reset original volume + this.setVolume(this.lastVol); this.loadBuffer(song, function() { // To prevent race condition if you press "next" twice fast @@ -442,18 +447,24 @@ SoundManager.prototype.toggleMute = function() { SoundManager.prototype.decreaseVolume = function() { this.setMute(false); var val = Math.max(this.gainNode.gain.value - 0.1, 0); - this.gainNode.gain.value = val; - this.core.userInterface.updateVolume(val); + this.setVolume(val); }; SoundManager.prototype.increaseVolume = function() { this.setMute(false); var val = Math.min(this.gainNode.gain.value + 0.1, 1); - this.gainNode.gain.value = val; - this.core.userInterface.updateVolume(val); + this.setVolume(val); }; SoundManager.prototype.setVolume = function(vol) { this.gainNode.gain.value = vol; + this.lastVol = vol; this.core.userInterface.updateVolume(vol); -}; \ No newline at end of file +}; + +SoundManager.prototype.fadeOut = function(callback) { + // Firefox hackery + this.gainNode.gain.setValueAtTime(this.lastVol, this.context.currentTime); + this.gainNode.gain.exponentialRampToValueAtTime(0.01, this.context.currentTime + 2); + setTimeout(callback, 2000); +}