Skip to content

Commit fec77b0

Browse files
committed
improved sketch
1 parent 1815846 commit fec77b0

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

examples/synthpresets/sketch.js

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
1-
var monoSynth;
21
var polySynth;
3-
var note;
4-
var octave = 0;
2+
var octave;
53

64
var keysDown={};
5+
var colors = {};
6+
var keys = {};
7+
var notes = {};
78

9+
var description;
810
function setup() {
9-
// monoSynth = new p5.MonoSynth();
10-
// monoSynth.loadPreset('punchyBass');
1111

12-
polySynth = new p5.PolySynth(p5.MonoSynth, 4);
12+
polySynth = new p5.PolySynth(p5.MonoSynth, 8);
13+
stroke(0);
14+
for(var i = 0; i<17; i++) {
15+
if(i!==11 && i!==15){
16+
colors[i] = [color(255),false];
17+
}
18+
}
19+
20+
keys = {'A':60, 'W':61, 'S':62, 'E':63, 'D':64, 'F':65, 'T':66,'G':67,
21+
'Y':68, 'H':69, 'U':70, 'J':71, 'K':72, 'O':73, 'L':74};
22+
notes = {60:0, 62:1 , 64:2 , 65:3 , 67:4 , 69:5 , 71:6 , 72:7 , 74:8,
23+
61: 9, 63:10, 66:12, 68:13, 70:14, 73:16};
24+
octave = 0;
25+
26+
description = createP('p5.PolySynth is a handler class for monophonic extensions '+
27+
'of the p5.AudioVoice class. Use the computer keyboard to play notes on '+
28+
'the piano roll. Use UP_ARROW and DOWN_ARROW to change octave');
29+
}
1330

31+
function draw() {
32+
background(255);
33+
createCanvas(800,600);
34+
35+
//draw white keys
36+
for(var i = 0; i<9; i++) {
37+
fill(colors[i][0]);
38+
rect(50*i,0,50,230);
39+
}
40+
//draw black keys
41+
for(var i = 9; i<17; i++) {
42+
if(i!==11 && i!==15){
43+
fill(colors[i][0]);
44+
rect(50*(i - 8) - 12.5, 0, 25, 150);
45+
}
46+
}
47+
1448
}
1549

1650
function keyPressed() {
@@ -20,9 +54,11 @@ function keyPressed() {
2054
} else if (keyCode === DOWN_ARROW) {
2155
octave -=12;
2256
}
23-
else {
24-
polySynth.noteAttack(keyToMidi() + octave);
25-
57+
else if (keyToMidi() && keysDown[key] !== true){
58+
polySynth.noteAttack(keyToMidi() + octave);
59+
var index = notes[keyToMidi()];
60+
colors[index][1] = !colors[index][1];
61+
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
2662
if (keysDown[key] === undefined) {
2763
keysDown[key] = true;
2864
}
@@ -33,39 +69,15 @@ function keyReleased() {
3369
Object.keys(keysDown).forEach(function(key) {
3470
if(!keyIsDown(key)){
3571
polySynth.noteRelease(keyToMidi(key) + octave);
72+
var index = notes[keyToMidi(key)];
73+
colors[index][1] = !colors[index][1];
74+
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
3675
delete keysDown[key];
3776
}
3877
});
3978
}
4079

41-
4280
function keyToMidi(keyboard) {
4381
var thisKey = typeof keyboard ==='undefined' ? key : keyboard
44-
switch (thisKey) {
45-
case 'A':
46-
return 60;
47-
48-
case 'S':
49-
return 62;
50-
51-
case 'D':
52-
return 64;
53-
54-
case 'F':
55-
return 65;
56-
57-
case 'G':
58-
return 67;
59-
60-
case 'H':
61-
return 69;
62-
63-
case 'J':
64-
return 71;
65-
66-
case 'K':
67-
return 72;
68-
69-
}
70-
82+
return keys[thisKey];
7183
}

src/monosynth.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ define(function (require) {
1717
* @class p5.MonoSynth
1818
* @constructor
1919
*
20+
*
2021
**/
2122

2223
p5.MonoSynth = function () {

0 commit comments

Comments
 (0)