let midiStatus = 0, midiKey = 0, midiPressure = 0;
let pad = new Array(8).fill(0);
let messageType = 0, controlNumber = 0, controlValue = 0;
let pitchWheel = 64, modWheel = 0;
let knob = new Array(8).fill(0);
let fader = new Array(4).fill(0);
let fftPad1, fftPad2, fftPad3, fftPad4, fftPad5, fftPad6, fftPad7, fftPad8;
loop1 = loadSound("01_Loop1.wav");
loop2 = loadSound("02_Loop2.wav");
kick = loadSound("03_Kick.wav");
snare = loadSound("04_Snare.wav");
claps = loadSound("05_Claps.wav");
hihat = loadSound("06_HiHat.wav");
bass = loadSound("07_Bass1.wav");
synth = loadSound("09_Synth1.wav");
font = loadFont("Digital.ttf");
createCanvas(windowWidth, windowHeight);
WebMidi.enable().then(onEnabled).catch(err => console.error("WebMidi could not be enabled:", err));
bSize = windowWidth / 12;
bOff = (width - (bSize*8)) / 9;
ampMain = new p5.Amplitude();
levelMain = ampMain.getLevel();
let circleSize = map(levelMain, 0, 1, 300, 800);
circle(width/2, height/2.5, circleSize)
padVisuals(bSize, bOff, bStroke, pad, knob);
function padVisuals(bSize, bOff, bStroke, padArray, knobArray){
let wavePad1 = fftPad1.waveform();
let wavePad2 = fftPad2.waveform();
let wavePad3 = fftPad3.waveform();
let wavePad4 = fftPad4.waveform();
let wavePad5 = fftPad5.waveform();
let wavePad6 = fftPad6.waveform();
let wavePad7 = fftPad7.waveform();
let wavePad8 = fftPad8.waveform();
let waveForms = [wavePad1,wavePad2,wavePad3,wavePad4,wavePad5,wavePad6,wavePad7,wavePad8];
let knob_r = map(knob[i], 0, 127, 0, 255);
let sqrX = bOff+(bSize*i)+bOff*i
let sqrY = height-bSize-(bStroke*1.5)
square(sqrX, sqrY, bSize, 10);
for(let j=0; j<height-(bSize+(bStroke*1.5)); j++){
let index = floor(map(j, 0, height-(bSize+(bStroke*1.5)), 0, waveForms[i].length));
let waveX = waveForms[i][index] * 150 + sqrX + bSize/2;
strokeWeight(bStroke*0.25);
rect(width/2, height-bSize-(bStroke*1.5)-(height/12), bSize, bSize/3, 10);
textAlign(CENTER, CENTER);
text(floor(map(controlValue, 0, 127, 0, 100)), width/2, height-bSize-(bStroke*1.5)-(height/12))
function padCheck(midiStatus, midiKey){
pad[midiKey-44] = 1 - pad[midiKey-44];
console.log("Pad toggled to:" + pad[midiKey-44]);
padSound(pad, loop1, loop2, kick, snare, claps, hihat, bass, synth);
function padSound(pad, ...sounds) {
for (let i = 0; i < pad.length; i++) {
if (pad[i] === 1 && sounds[i]) {
if (!sounds[i].isPlaying()) {
if (sounds[i] && sounds[i].isPlaying()) {
loop1.setVolume(map(knob[0], 0, 127, 0.05, 1));
loop2.setVolume(map(knob[1], 0, 127, 0.05, 1));
kick.setVolume(map(knob[2], 0, 127, 0.05, 1));
snare.setVolume(map(knob[3], 0, 127, 0.05, 1));
claps.setVolume(map(knob[4], 0, 127, 0.05, 1));
hihat.setVolume(map(knob[5], 0, 127, 0.05, 1));
bass.setVolume(map(knob[6], 0, 127, 0.05, 1));
synth.setVolume(map(knob[7], 0, 127, 0.05, 1));