diff --git a/globals.js b/globals.js index df9475b..40748fb 100644 --- a/globals.js +++ b/globals.js @@ -39,110 +39,109 @@ let notePressedCount = 0; let notePressedCountHistory = []; WebMidi.enable(function (err) { //check if WebMidi.js is enabled - if (err) { - console.log("WebMidi could not be enabled.", err); - } else { - console.log("WebMidi enabled!"); - } + if (err) { + console.log("WebMidi could not be enabled.", err); + } else { + console.log("WebMidi enabled!"); + } - //name our visible MIDI input and output ports - console.log("---"); - console.log("Inputs Ports: "); - for (i = 0; i < WebMidi.inputs.length; i++) { - console.log(i + ": " + WebMidi.inputs[i].name); - } + //name our visible MIDI input and output ports + console.log("---"); + console.log("Inputs Ports: "); + for (i = 0; i < WebMidi.inputs.length; i++) { + console.log(i + ": " + WebMidi.inputs[i].name); + } - console.log("---"); - console.log("Output Ports: "); - for (i = 0; i < WebMidi.outputs.length; i++) { - console.log(i + ": " + WebMidi.outputs[i].name); - } - midiSelectSlider = select("#slider"); - midiSelectSlider.attribute("max", WebMidi.inputs.length - 1); - midiSelectSlider.input(inputChanged); - midiIn = WebMidi.inputs[midiSelectSlider.value()] - inputChanged(); + console.log("---"); + console.log("Output Ports: "); + for (i = 0; i < WebMidi.outputs.length; i++) { + console.log(i + ": " + WebMidi.outputs[i].name); + } + midiSelectSlider = select("#slider"); + midiSelectSlider.attribute("max", WebMidi.inputs.length - 1); + midiSelectSlider.input(inputChanged); + midiIn = WebMidi.inputs[midiSelectSlider.value()] + inputChanged(); }); function inputChanged() { - isKeyOn.fill(0); - controllerChange(64, 0); - controllerChange(67, 0); + isKeyOn.fill(0); + controllerChange(64, 0); + controllerChange(67, 0); - midiIn.removeListener(); - midiIn = WebMidi.inputs[midiSelectSlider.value()]; - midiIn.addListener('noteon', "all", function (e) { - console.log("Received 'noteon' message (" + e.note.number + ", " + e.velocity + ")."); - noteOn(e.note.number, e.velocity); - }); - midiIn.addListener('noteoff', "all", function (e) { - console.log("Received 'noteoff' message (" + e.note.number + ", " + e.velocity + ")."); - noteOff(e.note.number, e.velocity); - }) - midiIn.addListener('controlchange', 'all', function(e) { - console.log("Received control change message:", e.controller.number, e.value); - controllerChange(e.controller.number, e.value) - }); - console.log(midiIn.name); - select("#device").html(midiIn.name); + midiIn.removeListener(); + midiIn = WebMidi.inputs[midiSelectSlider.value()]; + midiIn.addListener('noteon', "all", function (e) { + console.log("Received 'noteon' message (" + e.note.number + ", " + e.velocity + ")."); + noteOn(e.note.number, e.velocity); + }); + midiIn.addListener('noteoff', "all", function (e) { + console.log("Received 'noteoff' message (" + e.note.number + ", " + e.velocity + ")."); + noteOff(e.note.number, e.velocity); + }) + midiIn.addListener('controlchange', 'all', function(e) { + console.log("Received control change message:", e.controller.number, e.value); + controllerChange(e.controller.number, e.value) + }); + console.log(midiIn.name); + select("#device").html(midiIn.name); }; function noteOn(pitch, velocity) { - totalNotesPlayed++; - notesThisFrame++; - totalIntensityScore += velocity; - - // piano visualizer - isKeyOn[pitch] = velocity; - if (nowPedaling) { - isPedaled[pitch] = velocity; - } + totalNotesPlayed++; + notesThisFrame++; + totalIntensityScore += velocity; + + // piano visualizer + isKeyOn[pitch] = velocity; + if (nowPedaling) { + isPedaled[pitch] = velocity; + } } -function noteOff(pitch, velocity) { - isKeyOn[pitch] = 0; +function noteOff(pitch, _velocity) { + isKeyOn[pitch] = 0; } function controllerChange(number, value) { - // Receive a controllerChange - if (number == 64) { - cc64now = value; + // Receive a controllerChange + if (number == 64) { + cc64now = value; - if (value >= 64) { - nowPedaling = true; - for (let i = 0; i < 128; i++) { - // copy key on to pedal - isPedaled[i] = isKeyOn[i]; - } - } else if (value < 64) { - nowPedaling = false; - for (let i = 0; i < 128; i++) { - // reset isPedaled - isPedaled[i] = 0; - } - } + if (value >= 64) { + nowPedaling = true; + for (let i = 0; i < 128; i++) { + // copy key on to pedal + isPedaled[i] = isKeyOn[i]; + } + } else if (value < 64) { + nowPedaling = false; + for (let i = 0; i < 128; i++) { + // reset isPedaled + isPedaled[i] = 0; + } } + } - if (number == 67) { - cc67now = value; - - } + if (number == 67) { + cc67now = value; + } } function toggleRainbowMode(cb) { - rainbowMode = cb.checked; - if (rainbowMode) - select('#colorpicker').attribute('disabled', true) - else - select('#colorpicker').removeAttribute('disabled') + rainbowMode = cb.checked; + if (rainbowMode) + select('#colorpicker').attribute('disabled', true) + else + select('#colorpicker').removeAttribute('disabled') } function toggleVelocityMode(cb) { - velocityMode = cb.checked; + velocityMode = cb.checked; } function changeColor() { - keyOnColor = color(select('#colorpicker').value()); - let darkenedColor = keyOnColor.levels.map(x => floor(x * .7)); - pedaledColor = color(`rgb(${darkenedColor[0]}, ${darkenedColor[1]}, ${darkenedColor[2]})`); -} \ No newline at end of file + keyOnColor = color(select('#colorpicker').value()); + let darkenedColor = keyOnColor.levels.map(x => floor(x * .7)); + pedaledColor = color(`rgb(${darkenedColor[0]}, ${darkenedColor[1]}, ${darkenedColor[2]})`); +} diff --git a/piano-visualizer.js b/piano-visualizer.js index 36d38ed..192b2a6 100644 --- a/piano-visualizer.js +++ b/piano-visualizer.js @@ -1,12 +1,11 @@ function setup() { createCanvas(1098, 118).parent('piano-visualizer'); colorMode(HSB, 360, 100, 100, 100); - keyOnColor = color(326, 100, 100, 100); // <---- 編輯這裡換「按下時」的顏色![HSB Color Mode] + keyOnColor = color(326, 100, 100, 100); // <---- 編輯這裡換「按下時」的顏色![HSB Color Mode] pedaledColor = color(326, 100, 70, 100); // <---- 編輯這裡換「踏板踩住」的顏色![HSB Color Mode] smooth(2); frameRate(60); initKeys(); - } function draw() { @@ -14,9 +13,6 @@ function draw() { pushHistories(); drawWhiteKeys(); drawBlackKeys(); - // drawPedalLines(); - // drawNotes(); - drawTexts(); } @@ -93,12 +89,6 @@ function drawBlackKeys() { if (isBlack[i % 12] > 0) { // it's a black key if (velocityMode) { - // let m = max(isKeyOn[i], isPedaled[i]) * .9 + .1; - // if ((isKeyOn[i] || isPedaled[i]) && !rainbowMode) { - // let darkenedColor = keyOnColor.levels.map(x => floor(x * m)); - // fill(`rgb(${darkenedColor[0]}, ${darkenedColor[1]}, ${darkenedColor[2]})`); // keypressed - // } else if ((isKeyOn[i] || isPedaled[i]) && rainbowMode) { - // fill(map(i, 21, 108, 0, 1080) % 360, 100, 100 * m, 100); // rainbowMode let m = max(isKeyOn[i], isPedaled[i]) * .9 + .1; if ((isKeyOn[i] || isPedaled[i]) && !rainbowMode) { let whitenedColor = keyOnColor.levels.map(x => floor(x * m + 255 * (1 - m))); @@ -179,8 +169,6 @@ function pushHistories() { notesThisFrame = 0; legatoHistory.push(isKeyOn.reduce((accumulator, currentValue) => accumulator + !!currentValue, 0)); legatoHistory.shift(); - - } function convertNumberToBars(number) { @@ -236,7 +224,6 @@ function getPressedKeys(returnString = true) { } else { return pressedKeys; } - } function truncateString(str, maxLength = 40) { @@ -280,7 +267,7 @@ function mouseClicked() { } if (mouseX > 441 && mouseX < 841) { - flatNames = !flatNames; // toggle flat + flatNames = !flatNames; // toggle flat } } console.log(mouseX, mouseY);