diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..57195033 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.DS_Store +node_modules/ +package-lock.json diff --git a/README.md b/README.md index b61d3e2b..f7e6c8d0 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,26 @@ Assignment 4 - Creative Coding: Interactive Multimedia Experiences === +Jordan Stoessel -Due: October 11th, by 11:59 PM. +For this assignment we will focus on client-side development using popular audio technologies; the server requirements are minimal. The goal of this assignment is to refine our JavaScript knowledge while exploring the multimedia capabilities of the browser. -For this assignment we will focus on client-side development using popular audio/graphics/visualization technologies; the server requirements are minimal. The goal of this assignment is to refine our JavaScript knowledge while exploring the multimedia capabilities of the browser. - -Baseline Requirements --- -Your application is required to implement the following functionalities: - -- A server created using Express. This server can be as simple as needed. -- A client-side interactive experience using at least one of the following web technologies frameworks. - - [Three.js](https://threejs.org/): A library for 3D graphics / VR experiences - - [D3.js](https://d3js.org): A library that is primarily used for interactive data visualizations - - [Canvas](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API): A 2D raster drawing API included in all modern browsers - - [SVG](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API): A 2D vector drawing framework that enables shapes to be defined via XML. - - [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API): An API for audio synthesis, analysis, processing, and file playback. -- A user interface for interaction with your project, which must expose at least six parameters for user control. [dat.gui](https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage) is highly recommended for this. You might also explore interaction by tracking mouse movement via the `window.onmousemove` event handler in tandem with the `event.clientX` and `event.clientY` properties. Consider using the [Pointer Events API](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events) to ensure that that mouse and touch events will both be supported in your app. -- Your application should display basic documentation for the user interface when the application first loads. This documentation should be dismissable, however, users should be able to redisplay it via either a help buton (this could, for example, be inside a dat.gui interface) or via a keyboard shortcut (commonly the question mark). -- Your HTML and CSS should validate. There are options/plugins for most IDEs to check validation. - -The interactive experience should possess a reasonable level of complexity. Some examples: -### Three.js -- A generative algorithm creates simple agents that move through a virtual world. Your interface controls the behavior / appearance of these agents. -- A simple 3D game -- An 3D audio visualization of a song of your choosing. User interaction should control aspects of the visualization. -### Canvas -- Implement a generative algorithm such as [Conway's Game of Life](https://bitstorm.org/gameoflife/) (or 1D cellular automata) and provide interactive controls. Note that the Game of Life has been created by 100s of people using ; we'll be checking to ensure that your implementation is not a copy of these. -- Design a 2D audio visualizer of a song of your choosing. User interaction should control visual aspects of the experience. -### Web Audio API -- Create a screen-based musical instrument using the Web Audio API. You can use projects such as [Interface.js](http://charlie-roberts.com/interface/) or [Nexus UI](https://nexus-js.github.io/ui/api/#Piano) to provide common musical interface elements, or use dat.GUI in combination with mouse/touch events (use the Pointer Events API). Your GUI should enable users to control aspects of sound synthesis. -### D3.js -- Create visualizations using the datasets found at [Awesome JSON Datasets](https://github.com/jdorfman/Awesome-JSON-Datasets). Experiment with providing different visualizations of the same data set, and providing users interactive control over visualization parameters and/or data filtering. Alternatively, create a single visualization with using one of the more complicated techniques shown at [d3js.org](d3js.org) and provide meaningful points of interaction for users. - -Deliverables ---- -Do the following to complete this assignment: +## Virtual Piano using Web Audio API -1. Implement your project with the above requirements. -3. Test your project to make sure that when someone goes to your main page on Glitch/Heroku/etc., it displays correctly. -4. Ensure that your project has the proper naming scheme `a4-firstname-lastname` so we can find it. -5. Fork this repository and modify the README to the specifications below. *NOTE: If you don't use Glitch for hosting (where we can see the files) then you must include all project files that you author in your repo for this assignment*. -6. Create and submit a Pull Request to the original repo. Name the pull request using the following template: `a4-firstname-lastname`. +https://a4-jordan-stoessel.glitch.me -Sample Readme (delete the above when you're ready to submit, and modify the below so with your links and descriptions) ---- +The Virtual Piano app was made to allow a Piano to be played on a computer's physical keyboard. It makes use of Web Audio API as seen in https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API. Virtual Piano features a Piano interface which was created using Web Dev Simplified's HTML & CSS tutorial. Playing notes can be done by pressing the key on the note that you want to play. There are also user parameters that can be controlled such as a volume bar, filter frequency bar, waveforms, and pass filter types. The volume and frequency bars are synced with the numberal input next to it and vice versa; this was done using a sync function to read changes in input from either inputs. The user can also save their current settings, load their last saved settings, or reset their current settings to default. This was done by creating a simple server that holds the default settings of 0.5 volume, sine waveform, low-pass pass filter type, and a frequency of 1,000. The server utilizes express and the body-parser middleware for ease of use. There is a modal that can be opened and closed at will for those who are confused about using the app. This modal can be found after clicking the "Help!" button at the bottom of the application. The Virtual Piano app can be seen below. +![Virtual Piano](https://github.com/jstoessel/a4-creative-coding/blob/master/virtual_piano.PNG?raw=true) -## Your Web Application Title +There were multiple challenges with creating this application. At first, I made use of .mp3 files to create and test the Piano while also trying to generate different amounts of octaves. This proved to be difficult as the Piano became unresponsive when generated with javascript into HTML. As such, I had to redo the project and salvage code while implementing the Web Audio API for the next attempt. Key presses and sound are disconnected, but synced to the same key presses. As such, pressing Z will both play the "C1" note while also making the lower C key highlighted. Adding and removing key presses proved to be difficult since it required a separate function to not give an error. Clicking was working for the broken code, but a click function would prove to be unecessary and also inefficient to add. Heroku deployment did not work, therefore I deployed to Glitch instead. The application makes use of Adobe's color palette which then led me to changing the sliders to stand out on the blue background. The color palette can be seen below. +![Color Palette](https://github.com/jstoessel/a4-creative-coding/blob/master/color_palette.png?raw=true) -your hosting link e.g. http://a4-charlieroberts.glitch.me -Include a very brief summary of your project here. Images are encouraged when needed, along with concise, high-level text. Be sure to include: +### Credits to: -- the goal of the application -- challenges you faced in realizing the application -- the instructions you present in the website should be clear enough to use the application, but if you feel any need to provide additional instructions please do so here. +Web Dev Simplified for Piano visual tutorial and modal tutorial +css-tricks.com for slider customization options +https://pages.mtu.edu/~suits/notefreqs.html for note frequencies +https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API for Web Audio API tutorial diff --git a/color_palette.png b/color_palette.png new file mode 100644 index 00000000..96c77906 Binary files /dev/null and b/color_palette.png differ diff --git a/index.html b/index.html new file mode 100644 index 00000000..29570e40 --- /dev/null +++ b/index.html @@ -0,0 +1,107 @@ + + + + + Virtual Piano with Audio API + + + + + + +

Virtual Piano

+ + + + +
+ +
+ +
Z
+
S
+
X
+
D
+
C
+
V
+
G
+
B
+
H
+
N
+
J
+
M
+ + +
Q
+
2
+
W
+
3
+
E
+
R
+
5
+
T
+
6
+
Y
+
7
+
U
+ + +
I
+
+ +
+ + +
+ Volume: + + +
+
+ + Waveform: + +
+
+ + Pass Filter Type: + +
+
+ Filter Frequency: + + +
+
+ +
+ +
+ + + +
+
+ + + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 00000000..77a13861 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "a4-jordanstoessel", + "version": "1.0", + "description": "The fourth assignment of my WebWare course that helps teach creative coding.", + "author": "Jordan Stoessel", + "scripts": { + "start": "node server.js" + }, + "dependencies": { + "body-parser": "^1.19.0", + "express": "^4.17.1", + "nodejs": "0.0.0" + } +} diff --git a/scripts.js b/scripts.js new file mode 100644 index 00000000..861ab27a --- /dev/null +++ b/scripts.js @@ -0,0 +1,325 @@ +//key press buttons for UI keyboard +const WHITE_KEYS = ['z', 'x', 'c', 'v', 'b', 'n', 'm', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i'] +const BLACK_KEYS = ['s', 'd', 'g', 'h', 'j', '2', '3', '5', '6', '7'] + +//Keycode to musical frequency hashmap +const keyboardFrequencyMap = { + '90': 130.81, //Z - note: C3 + '83': 138.59, //S - C3# + '88': 146.83, //X - D3 + '68': 155.56, //D - D3# + '67': 164.81, //C - E3 + '86': 174.61, //V - F3 + '71': 185.00, //G - F3# + '66': 196.00, //B - G3 + '72': 207.65, //H - G3# + '78': 220.00, //N - A3 + '74': 233.08, //J - A3# + '77': 246.94, //M - B3 + '81': 261.63, //Q - C4 + '50': 277.18, //2 - C4# + '87': 293.66, //W - D4 + '51': 311.13, //3 - D4# + '69': 329.63, //E - E4 + '82': 349.23, //R - F4 + '53': 369.99, //5 - F4# + '84': 392.00, //T - G4 + '54': 415.30, //6 - G4# + '89': 440.00, //Y - A4 + '55': 466.16, //7 - A4# + '85': 493.88, //U - B4 + '73': 523.25, //I - C5 +} + +//default waveform, this is to remove undefined warning +let waveform = 'sine'; + +//Web Audio API utilization +const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); + +//Process volume and filter +const volume = audioCtx.createGain(); +const filter = audioCtx.createBiquadFilter(); +const activeOscillators = {}; + +const keys = document.querySelectorAll('.key'); +const whiteKeys = document.querySelectorAll('.key.white'); +const blackKeys = document.querySelectorAll('.key.black'); + +//sync range and number +const freqLevelRange = document.getElementById('freqLevelRange'); +const freqLevelNumber = document.getElementById('freqLevelNumber'); +freqLevelRange.addEventListener('input', syncFreqAmount) +freqLevelNumber.addEventListener('input', syncFreqAmount) +function syncFreqAmount(e) { + const val = e.target.value + freqLevelRange.value = val; + freqLevelNumber.value = val; +} + +//syncs volume range with volume number +const volLevelRange = document.getElementById('volLevelRange'); +const volLevelNumber = document.getElementById('volLevelNumber'); +volLevelRange.addEventListener('input', syncVolAmount) +volLevelNumber.addEventListener('input', syncVolAmount) +function syncVolAmount(e) { + const val = e.target.value + volLevelRange.value = val; + volLevelNumber.value = val; +} + + +//event listeners to add active effect to keyboard +document.addEventListener('keydown', e => { + if (e.repeat) return + const key = e.key; + const blackKeyIndex = BLACK_KEYS.indexOf(key) + const whiteKeyIndex = WHITE_KEYS.indexOf(key) + + if (whiteKeyIndex > -1) + whiteKeys[whiteKeyIndex].classList.add('active'); + if (blackKeyIndex > -1) + blackKeys[blackKeyIndex].classList.add('active'); +}); + +//event listeners to remove active effect from keyboard +document.addEventListener('keyup', e => { + if (e.repeat) return + const key = e.key; + const blackKeyIndex = BLACK_KEYS.indexOf(key) + const whiteKeyIndex = WHITE_KEYS.indexOf(key) + + if (whiteKeyIndex > -1) + whiteKeys[whiteKeyIndex].classList.remove('active') + if (blackKeyIndex > -1) + blackKeys[blackKeyIndex].classList.remove('active'); +}); + +//____________________________________WEB_AUDIO_API_CONTROLLING____________________________________ +volume.connect(filter); +filter.connect(audioCtx.destination); + +//Listens for changes to the volume on range and number +const volumeRangeControl = document.getElementById('volLevelRange') +const volumeNumControl = document.getElementById('volLevelNumber'); +volumeRangeControl.addEventListener('change', function (e) { + volume.gain.setValueAtTime(e.target.value, audioCtx.currentTime) +}); +volumeNumControl.addEventListener('change', function (e) { + volume.gain.setValueAtTime(e.target.value, audioCtx.currentTime); +}); + +//Listens for changes to the waveform +const waveformControl = document.getElementById('waveform') +waveformControl.addEventListener('change', function (event) { + waveform = event.target.value +}); + +//Listens for change in pass type +const passTypeControl = document.getElementById('passType') +passTypeControl.addEventListener('change', function (event) { + filter.type = event.target.value +}); + +//Listens for change in frequency on range and number +const frequencyRangeControl = document.getElementById('freqLevelRange') +const frequencyNumControl = document.getElementById('freqLevelNumber'); +frequencyRangeControl.addEventListener('change', function (e) { + filter.frequency.setValueAtTime(e.target.value, audioCtx.currentTime) +}); +frequencyNumControl.addEventListener('change', function (e) { + filter.frequency.setValueAtTime(e.target.value, audioCtx.currentTime); +}) + +//Listen for keypresses to play notes +window.addEventListener('keydown', keyDown, false); +window.addEventListener('keyup', keyUp, false); + +//When key is pressed, if it's the right key and key is not pressed, play note +function keyDown(event) { + const key = (event.detail || event.which).toString(); + if (keyboardFrequencyMap[key] && !activeOscillators[key]) { + playNote(key); + } +} + +//When key is released, delete key's oscillator +function keyUp(event) { + const key = (event.detail || event.which).toString(); + if (keyboardFrequencyMap[key] && activeOscillators[key]) { + activeOscillators[key].stop(); + delete activeOscillators[key]; + } +} + +//Creates an oscillation for a specific key +function playNote(key) { + const osc = audioCtx.createOscillator(); + osc.frequency.setValueAtTime(keyboardFrequencyMap[key], audioCtx.currentTime) + osc.type = waveform + activeOscillators[key] = osc + activeOscillators[key].connect(volume) + activeOscillators[key].start(); +} + + + +//____________________________________GET AND POST METHODS____________________________________ +//gets default settings from server +const reset = function (e) { + //prevent default form action from being carried out + // e.preventDefault(); + + fetch('/reset', { + method: 'GET' + }) + .then(function (res) { + //response + res.json().then(function (data) { + //data + console.log("Submit Response:", res); + console.log("Returned data: ", data); + + setSettings(data); + }) + }) + + return false; +} + +//gets last saved settings from server +const load = function (e) { + //prevent default form action from being carried out + e.preventDefault(); + + fetch('/load', { + method: 'GET' + }) + .then(function (res) { + //response + res.json().then(function (data) { + //data + console.log("Submit Response:", res); + console.log("Returned data: ", data); + + setSettings(data); + }) + }) + + return false; +} + +//posts current settings to server +const save = function (e) { + //prevent default form action from being carried out + e.preventDefault(); + + if (document.getElementById('volLevelNumber').value > 100) { + alert("Volume cannot be that high"); + document.getElementById('volLevelNumber').value = 100; + document.getElementById('volLevelRange').value = 100; + } + + const userSettings = { + volume: document.getElementById('volLevelNumber').value, + waveform: document.getElementById('waveform').value, + passtype: document.getElementById('passType').value, + frequency: document.getElementById('freqLevelNumber').value + } + console.log("User settings: " + userSettings) + + fetch('/save', { + method: 'POST', + body: JSON.stringify(userSettings), + headers: { + "Content-Type": "application/json" + } + }) + .then(function (res) { + //response + res.json().then(function (data) { + //data + console.log("Submit Response:", res); + console.log("Returned data: ", data); + alert('Settings saved!'); + }) + }) + + return false; +} +//____________________________________POPUP_MODAL____________________________________ +const openModalButtons = document.querySelectorAll('[data-modal-target]'); +const closeModalButtons = document.querySelectorAll('[data-close-button]'); +const overlay = document.getElementById("overlay"); + +openModalButtons.forEach(button => { + button.addEventListener('click', () => { + const modal = document.querySelector(button.dataset.modalTarget) + openModal(modal) + }) +}) + +overlay.addEventListener('click', () => { + const modals = document.querySelectorAll('.modal.active'); + modals.forEach(modal => { + closeModal(modal); + }) +}) + +closeModalButtons.forEach(button => { + button.addEventListener('click', () => { + const modal = button.closest('.modal'); + closeModal(modal) + }) +}) + + +function openModal(modal) { + if (modal == null) return; + modal.classList.add('active'); + overlay.classList.add('active'); +} + +function closeModal(modal) { + if (modal == null) return; + modal.classList.remove('active'); + overlay.classList.remove('active'); + +} + +//____________________________________APPLY_AND_CHANGE_SETTINGS____________________________________ +//sets html values to server settings +function setSettings(settings) { + + //set data values from settings + document.getElementById('volLevelNumber').value = settings.volume; + document.getElementById('volLevelRange').value = settings.volume; + document.getElementById('waveform').value = settings.waveform; + document.getElementById('passType').value = settings.passtype; + document.getElementById('freqLevelNumber').value = settings.frequency; + document.getElementById('freqLevelRange').value = settings.frequency; + + //updates sound settings with html settings + applySettings(settings); + console.log("Settings applied!"); +} + +//helper function for dealing with 4 parts of json +function applySettings(settings) { + volume.gain.setValueAtTime(settings.volume, audioCtx.currentTime); + waveform = settings.waveform; + filter.type = settings.passtype; + filter.frequency.setValueAtTime(settings.frequency, audioCtx.currentTime); +} + +//run functions on window load +window.onload = function () { + //initialize buttons + const savebtn = document.getElementById('savebtn'); + savebtn.onclick = save; + const loadbtn = document.getElementById('loadbtn'); + loadbtn.onclick = load; + const resetbtn = document.getElementById('resetbtn'); + resetbtn.onclick = reset; + console.log("Loaded!"); +} \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 00000000..3cfbcb36 --- /dev/null +++ b/server.js @@ -0,0 +1,59 @@ +// A simple express server for storing settings data +const express = require('express'); +const app = express(); +const port = 3000; +const bodyParser = require('body-parser'); +const { json } = require('body-parser'); +app.use(express.static('./')); +app.use(bodyParser.urlencoded({extended: true})); + +//simple server for storing settings and defaults +let settings = { volume: 0.5, waveform: "sine", passtype: "lowpass", frequency: 1000 } +const default_settings = { volume: 0.5, waveform: "sine", passtype: "lowpass", frequency: 1000 } + + + +//default get method +app.get('/', (req, res) => { + res.sendFile('index.html', { root: __dirname }) +}) + +//get default settings +app.get("/reset", (req, res) => { + console.log("Loading defaults: " + JSON.stringify(default_settings)); + res.json(default_settings); + console.log("Defaulted!"); + res.end(); +}) + +//get server settings +app.get("/load", (req, res) => { + console.log("Loading old settings: " + JSON.stringify(settings)); + + res.json(settings); + console.log("Loaded!"); + res.end(); +}) + +//post to update server settings +app.post("/save", bodyParser.json(), (req, res) => { + console.log("Before change: " + JSON.stringify(settings)); + console.log("The volume is: " + req.body.volume); + + settings = { + volume: req.body.volume, + waveform: req.body.waveform, + passtype: req.body.passtype, + frequency: req.body.frequency + } + + console.log("These settings saved: " + JSON.stringify(settings)); + console.log("Saved!"); + // res.writeHead(200, "OK", {'Content-Type': 'text/plain' }); + res.end(); +}) + +//listen to port +app.listen(port, () => { + console.log(`Example app listening at http://localhost:${port}`) +}) \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 00000000..8c9fb240 --- /dev/null +++ b/style.css @@ -0,0 +1,169 @@ +@import url('https://fonts.googleapis.com/css?family=Lato'); +*, *::before, *::after { + box-sizing: border-box; +} + +html { + font-size: 20px; + background-color: #2B83B3; +} + +body, html { + font-family: "Lato"; + color: #FFFFFF; + height: 100%; + min-width: 770px; + text-align: center; +} + +.title { + text-align: center; +} + +.modal { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%) scale(0); + transition: 200ms ease-in-out; + border: 1px solid black; + border-radius: 5px; + z-index: 5; + background-color: white; + width: 500px; + max-width: 80%; + color: black; +} + +.modal.active { + transform: translate(-50%, -50%) scale(1); +} + +.modal-header { + padding: 10px 15px; + display: flex; + justify-content: space-between; + align-items: center; + border-bottom: 1px solid black; +} + +.modal-header .modal-title { + font-size: 1.25rem; + font-weight: bold; +} + +.modal-header .close-button { + cursor: pointer; + border: none; + outline: none; + background: none; + font-size: 3rem; + font-weight: bold; + color: red; +} + +.modal-body { + padding: 10px 15px; + user-select: none; +} + +#overlay { + position: fixed; + opacity: 0; + transition: 200ms ease-in-out; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: rgba(0, 0, 0, .5); + pointer-events: none; +} + +#overlay.active { + opacity: 1; + pointer-events: all; +} + +a { + text-decoration: none; + font-weight: bold; + color: #2BCF4C; + justify-content: center; +} + +.piano { + margin: 10px 90px; + min-width: 755px; + display: flex; + justify-content: center; + flex: 1; + min-height: 30vh; +} + +.key { + height: calc(var(--width) * 4); + width: var(--width); +} + +.white { + --width: 100px; + background-color: white; + border: 1px solid #333; + color: black; + text-align: center; + align-items: center; +} + +.white.active { + background-color: #CCC; +} + +.black { + --width: 60px; + background-color: black; + color: white; + margin-left: calc(var(--width) / -2); + margin-right: calc(var(--width) / -2); + z-index: 2; +} + +.black.active { + background-color: #333; +} + +/* This CSS is for styling range inputs which was taken and edited from css-tricks.com */ +input[type=range] { + -webkit-appearance: none; + margin: 18px 0; + width: 15%; +} + +input[type=range]:focus { + outline: none; +} + +input[type=range]::-webkit-slider-runnable-track { + width: 100%; + height: 8.4px; + cursor: pointer; + box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d; + background: #2BCF4C; + border-radius: 1.3px; + border: 0.2px solid #010101; +} + +input[type=range]::-webkit-slider-thumb { + box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d; + border: 1px solid #000000; + height: 30px; + width: 16px; + border-radius: 3px; + background: #ffffff; + cursor: pointer; + -webkit-appearance: none; + margin-top: -14px; +} + +input[type=range]:focus::-webkit-slider-runnable-track { + background: #33F259; +} \ No newline at end of file diff --git a/virtual_piano.PNG b/virtual_piano.PNG new file mode 100644 index 00000000..52be0e99 Binary files /dev/null and b/virtual_piano.PNG differ