From d05c5c8d51d1c9ca8e1625adba06643a04d43629 Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Fri, 17 Jan 2020 16:14:53 -0600 Subject: Feature: Fast-Forward and Rewind buttons plus refactoring --- audio/button-press.mp3 | Bin 15512 -> 15772 bytes audio/ff.mp3 | Bin 0 -> 49483 bytes deck-button.js | 26 ++++++++++++++++++ deck-controls.js | 51 +++++++++++++++++++++++++++++++++++ images/famicom-recorder.jpeg | Bin 0 -> 178267 bytes images/ff-button-down.jpg | Bin 0 -> 13070 bytes images/ff-button-up.jpg | Bin 0 -> 12648 bytes images/play-button-down.jpg | Bin 0 -> 12818 bytes images/play-button-up.jpg | Bin 0 -> 12408 bytes images/rewind-button-down.jpg | Bin 0 -> 12936 bytes images/rewind-button-up.jpg | Bin 0 -> 12504 bytes tape-deck.html | 10 +++++-- tape-deck.js | 60 +++++++++++++++++++++++++----------------- 13 files changed, 121 insertions(+), 26 deletions(-) create mode 100644 audio/ff.mp3 create mode 100644 deck-button.js create mode 100644 deck-controls.js create mode 100644 images/famicom-recorder.jpeg create mode 100644 images/ff-button-down.jpg create mode 100644 images/ff-button-up.jpg create mode 100644 images/play-button-down.jpg create mode 100644 images/play-button-up.jpg create mode 100644 images/rewind-button-down.jpg create mode 100644 images/rewind-button-up.jpg diff --git a/audio/button-press.mp3 b/audio/button-press.mp3 index 91ae629..acf0ea8 100644 Binary files a/audio/button-press.mp3 and b/audio/button-press.mp3 differ diff --git a/audio/ff.mp3 b/audio/ff.mp3 new file mode 100644 index 0000000..3fd2226 Binary files /dev/null and b/audio/ff.mp3 differ diff --git a/deck-button.js b/deck-button.js new file mode 100644 index 0000000..03cd4de --- /dev/null +++ b/deck-button.js @@ -0,0 +1,26 @@ +const sfx = new Audio("audio/button-press.mp3"); + +export function DeckButton(name) { + const elem = document.getElementById(`${name}-button`); + const images = { up: `images/${name}-button-up.jpg`, down: `images/${name}-button-down.jpg` }; + + let state = "off"; + + const turnOn = function () { + state = "on"; + elem.src = images.down; + }; + + const turnOff = function () { + state = "off"; + elem.src = images.up; + }; + + return { + state: () => state, + toggle: (opts = {sound: true}) => { + state === "off" ? turnOn(opts) : turnOff(opts); + if (opts.sound) sfx.play(); + } + }; +} diff --git a/deck-controls.js b/deck-controls.js new file mode 100644 index 0000000..91d4494 --- /dev/null +++ b/deck-controls.js @@ -0,0 +1,51 @@ +import { DeckButton } from './deck-button.js'; +const pressSound = new Audio("audio/button-press.mp3"); + +export function DeckControls(params) { + const { deck } = params; + + return { + go: function () { + deck.elem.src = deck.images.go; + deck.isPlaying = true; + deck.playButton.toggle(); + }, + start: function () { + deck.hasStarted = true; + this.playButton.initSound.onended = (event) => { deck.cassette.play(); }; + this.playButton.initSound.play(); + this.go(); + }, + pause: function () { + this.playButton.toggle(); + this.playButton.pressSound.play(); + deck.cassette.pause(); + deck.elem.src = deck.images.stop; + deck.isPlaying = false; + }, + resume: function () { + this.playButton.pressSound.play(); + this.go(); + deck.cassette.play(); + }, + playButton: { + elem: document.getElementById("play-button"), + state: "off", + up: "images/play-button-up.jpg", + down: "images/play-button-down.jpg", + initSound: new Audio("audio/tape-start.mp3"), + pressSound: pressSound, + toggle: function () { + if (this.state === "off") { + this.elem.src = this.down; + this.state = "on"; + } else { + this.elem.src = this.up; + this.state = "off"; + } + } + }, + ffButton: DeckButton("ff"), + rewindButton: DeckButton("rewind"), + }; +} diff --git a/images/famicom-recorder.jpeg b/images/famicom-recorder.jpeg new file mode 100644 index 0000000..a184937 Binary files /dev/null and b/images/famicom-recorder.jpeg differ diff --git a/images/ff-button-down.jpg b/images/ff-button-down.jpg new file mode 100644 index 0000000..5216ae9 Binary files /dev/null and b/images/ff-button-down.jpg differ diff --git a/images/ff-button-up.jpg b/images/ff-button-up.jpg new file mode 100644 index 0000000..60b6bf6 Binary files /dev/null and b/images/ff-button-up.jpg differ diff --git a/images/play-button-down.jpg b/images/play-button-down.jpg new file mode 100644 index 0000000..86daa53 Binary files /dev/null and b/images/play-button-down.jpg differ diff --git a/images/play-button-up.jpg b/images/play-button-up.jpg new file mode 100644 index 0000000..5b76e0b Binary files /dev/null and b/images/play-button-up.jpg differ diff --git a/images/rewind-button-down.jpg b/images/rewind-button-down.jpg new file mode 100644 index 0000000..941dcc5 Binary files /dev/null and b/images/rewind-button-down.jpg differ diff --git a/images/rewind-button-up.jpg b/images/rewind-button-up.jpg new file mode 100644 index 0000000..048e4cb Binary files /dev/null and b/images/rewind-button-up.jpg differ diff --git a/tape-deck.html b/tape-deck.html index 1366fe5..ae8a5b7 100644 --- a/tape-deck.html +++ b/tape-deck.html @@ -11,8 +11,14 @@
+ +
@@ -20,6 +26,6 @@
- + diff --git a/tape-deck.js b/tape-deck.js index 14524de..057c96b 100644 --- a/tape-deck.js +++ b/tape-deck.js @@ -1,6 +1,9 @@ +import { DeckButton } from './deck-button.js'; + const urlParams = new URLSearchParams(window.location.search); const currentSide = urlParams.get("side"); const Cassette = new Audio(`audio/2019-mix-side-${currentSide}.mp3`); +const ffSound = new Audio("audio/ff.mp3"); let TapeDeck = { elem: document.getElementById("tape-deck"), @@ -10,56 +13,65 @@ let TapeDeck = { hasStarted: false, isPlaying: false, images: { stop: "images/cool-deck.png", go: "images/cool-deck.gif" }, + initSound: new Audio("audio/tape-start.mp3"), + playButton: DeckButton("play"), + ffButton: DeckButton("ff"), + rewindButton: DeckButton("rewind"), go: function () { this.elem.src = this.images.go; this.isPlaying = true; - this.playButton.toggle(); }, start: function () { this.hasStarted = true; - this.playButton.initSound.onended = (event) => { Cassette.play(); }; - this.playButton.initSound.play(); + this.initSound.onended = (event) => { Cassette.play(); }; + this.initSound.play(); this.go(); }, pause: function () { this.playButton.toggle(); - this.playButton.pressSound.play(); Cassette.pause(); this.elem.src = this.images.stop; this.isPlaying = false; }, resume: function () { - this.playButton.pressSound.play(); this.go(); Cassette.play(); }, - playButton: { - elem: document.getElementById("play-button"), - state: "off", - up: "images/play-button-up.png", - down: "images/play-button-down.png", - initSound: new Audio("audio/tape-start.mp3"), - pressSound: new Audio("audio/button-press.mp3"), - toggle: function () { - if (this.state === "off") { - this.elem.src = this.down; - this.state = "on"; - } else { - this.elem.src = this.up; - this.state = "off"; - } - } +}; + +ffSound.onended = (event) => { + if (TapeDeck.ffButton.state() === "on") { + TapeDeck.ffButton.toggle(); + } else { + TapeDeck.rewindButton.toggle(); } + TapeDeck.playButton.toggle({sound: false}); + TapeDeck.resume(); }; // button onclick points here -function play() { +window.play = function () { if (TapeDeck.isPlaying) { TapeDeck.pause(); return; } - + // figure out better way of handling play button + TapeDeck.playButton.toggle(); TapeDeck.hasStarted ? TapeDeck.resume() : TapeDeck.start(); } -function reset() { +window.ff = function () { + if (TapeDeck.isPlaying) TapeDeck.pause(); + TapeDeck.ffButton.toggle(); + Cassette.currentTime = Cassette.currentTime + 20; + ffSound.play(); +} + +window.rewind = function () { + if (TapeDeck.isPlaying) TapeDeck.pause(); + TapeDeck.rewindButton.toggle(); + Cassette.currentTime = Cassette.currentTime - 20; + ffSound.play(); +} + +window.reset = function () { Cassette.currentTime = 0; } -- cgit v1.2.3