WASD rotate the keyboard. ARROW KEYS play notes and chords. SLIDERS adjust attack and decay values. BUTTONS shift oscillator type and chord pattern, and play/pause Bach's C Major Prelude.
A fork of Synth Keyboard by Richard Bourne
xxxxxxxxxx
let flip = 0;
let keys = [];
let note = -1;
let tune;
let lastStamp = 0;
let interval = 140;
let ang, avel;
let root = 24;
let paused = true;
let ctype = 0;
let chords = [ [4,7,12], [3,7,12],[3,6,12],[4,7,11],[3,7,10],[4,7,10],[2,7,12],[4,8,12] ];
let cTypes = ['Major','Minor','Diminished','Major Seventh','Minor Seventh','Dominant Seventh','Suspended','Augmented'];
let otype = 0;
let oTypes = ['Triangle', 'Square', 'Sine', 'Sawtooth'];
let pLight = -300;
let sliderA, sliderD, sliderS, sliderR;
let buttonO, buttonC, buttonS;
function preload(){
tune=loadStrings('CPrelude.txt');
}
function setup() {
createCanvas(1112, 834, WEBGL);
ang = createVector(0,0,0);
avel = createVector(1.2,0,0);
makeInputs();
makeKeys();
startOscillators('triangle');
}
function makeKeys(){
let shapes=[0,1,2,0,1,1,2]
let id=36;
for(i=0; i<35; i++){
let x = (-17.5*30)+i*30;
let posw = createVector(x,0,-100);
let posb = createVector(x+15,-9,-100);
let shape = shapes[i%shapes.length];
keys.push(new Key(posw, shape, id));
if(shape==0 || shape==1){
id++;
keys.push(new Key(posb, 3, id));
}
id++;
}
}
function startOscillators(otype){
for(let k of keys){
k.start(otype);
}
}
function draw() {
orbitControl();
if (flip == 1){
scale(1, -1);
pLight = 600;
}
setScene();
for(let k of keys){
if(k.counter<50) k.move();
k.show();
}
if(!paused) autoPlay();
if(paused) checkArrows();
checkWASD();
}
function setScene(){
background(20);
ambientLight(120);
pointLight(80,80,80,0,pLight,0);
rotateX(ang.x);
rotateY(ang.y);
rotateZ(ang.z);
ang.add(avel);
avel.mult(0.9);
}
function autoPlay(){
if (millis() - lastStamp > interval) {
lastStamp = millis();
keys[tune[note]-24].sound();
note+=1;
if(tune.length-note < 24) interval*=1.05;
}
if(note==tune.length) {
sliderD.value(2);
keys[12].sound();
keys[12+chords[0][0]].sound();
keys[12+chords[0][1]].sound();
keys[12+chords[0][2]].sound();
keys[24+chords[0][0]].sound();
keys[24+chords[0][1]].sound();
keys[24+chords[0][2]].sound();
paused = true;
note = -1;
interval = 120;
}
}
function saver(){
save('pix.jpg');
}
function keyPressed(){
if(keyCode==DOWN_ARROW && paused){
if(root>47) root=47;
keys[root].sound();
keys[root+chords[ctype][0]].sound();
keys[root+chords[ctype][1]].sound();
keys[root+chords[ctype][2]].sound();
}
if(keyCode==UP_ARROW && paused){
keys[root].sound();
}
}
function checkWASD(){
if(keyIsDown(68)) avel.y-=0.01;
if(keyIsDown(65)) avel.y+=0.01;
if(keyIsDown(83)) avel.x-=0.01;
if(keyIsDown(87)) avel.x+=0.01;
}
function checkArrows(){
if(keyIsDown(RIGHT_ARROW) && root<59 && frameCount%3==0){
root+=1;
keys[root].sound();
}
if(keyIsDown(LEFT_ARROW) && root>0 && frameCount%3==0){
root-=1;
keys[root].sound();
}
}