-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjects.js
209 lines (156 loc) · 4.21 KB
/
objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const MIDI_START_NOTE = 21;
const NUM_STEPS = 32;
const NUM_NOTES = 88;
let ht = 0;
// Class for the disc
class Disc {
constructor(x, y, r, randomAngleX, randomAngleY) {
this.x = x;
this.y = y;
this.r = r;
this.randomAngleX = randomAngleX;
this.randomAngleY = randomAngleY;
this. brightness = 30;
this.isActive = false;
this.releasedInside = false;
this.notes = [];
this.volume = 0;
this.instrument = 'Piano';
// Random angles
}
activate() {
this.brightness = 255;
this.isActive = true;
}
deactivate() {
this.brightness = 30;
this.isActive = false;
this.notes = [];
this.releasedInside = false;
}
show() {
fill(255, 236, 204, this.brightness);
ellipse(this.x, this.y, this.r);
fill(0);
ellipse(this.x, this.y, this.r / 4);
}
// Function to draw the melodies, taken from:
// https://medium.com/@torinblankensmith/melody-mixer-using-deeplearn-js-to-mix-melodies-in-the-browser-8ad5b42b4d0b
drawNotes(notes, speed) {
push();
angleMode(DEGREES); // Change angle from radians to degrees
let angle = 0;
translate(this.x, this.y);
this.notes = notes;
let radius = this.r / 2;
var cellWidth = radius / NUM_STEPS;
var cellHeigth = radius / NUM_NOTES;
stroke(135, 0, 88);
strokeWeight(2);
let randomAngleX = this.randomAngleX;
let randomAngleY = this.randomAngleY;
this.notes.forEach(function(note) {
let steps = 360 / notes.length;
let rx = (radius + radius / 3) * cos(angle - speed / randomAngleX);
let ry = - radius / 2 + (cellHeigth * (note.pitch - MIDI_START_NOTE)) + (radius + radius / 3) * sin(angle - speed / randomAngleY);
let rw = (cellWidth * (note.quantizedEndStep - note.quantizedStartStep));
let rh = cellHeigth;
rect(rx, ry, rw, rh);
angle += steps;
});
pop();
}
// Check if an element is in the circle
contains(px, py) {
// Calculate distance between the center of the circle and other point
let d = dist(px, py, this.x, this.y)
if (d < this.r / 2) {
return true;
} else {
return false;
}
}
// Check if an element is in the inner circle
containsInner(px, py) {
// Calculate distance between the center of the circle and other point
let d = dist(px, py, this.x, this.y)
if (d < this.r / 8) {
return true;
} else {
return false;
}
}
}
// Melody tiles and its melodies
class Tile {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.dragging = false; // Is the object being dragged?
this.notes = [];
this.brightness = 100;
this.strokeWeight = 2;
// Offsets to drag the object
this.offsetX = 0;
this.offsetY = 0;
}
// When hover mouse
changeStyle(brightness, strokeWeight, newW, newH) {
this.brightness = brightness;
this.strokeWeight = strokeWeight;
this.w = newW;
this.h = newH;
}
update(px, py) {
if (this.dragging) {
this.x = px + this.offsetX;
this.y = py + this.offsetY;
}
}
show() {
// Stroke for the melody tiles
stroke(135, 0, 88);
strokeWeight(this.strokeWeight);
fill(50, this.brightness);
rect(this.x, this.y, this.w, this.h);
}
// Function to draw the melodies, taken from:
// https://medium.com/@torinblankensmith/melody-mixer-using-deeplearn-js-to-mix-melodies-in-the-browser-8ad5b42b4d0b
drawNotes(notes) {
push();
translate(this.x, this.y);
var cellWidth = this.w / NUM_STEPS;
var cellHeigth = this.h / NUM_NOTES;
this.notes = notes;
ht = this.h;
this.notes.forEach(function(note) {
var emptyNoteSpacer = 5;
rect(emptyNoteSpacer + cellWidth * note.quantizedStartStep, ht - cellHeigth * (note.pitch - MIDI_START_NOTE),
cellWidth * (note.quantizedEndStep - note.quantizedStartStep) - emptyNoteSpacer, cellHeigth);
});
pop();
}
// Check if mouse is over tile
contains(px, py) {
if ((px > this.x) && (px < this.x + this.w) && (py > this.y) && (py < this.y + this.h)) {
return true;
} else {
return false;
}
}
// Check if mouse is pressed and over the tile
pressed(px, py) {
if ((px > this.x) && (px < this.x + this.w) && (py > this.y) && (py < this.y + this.h)) {
this.dragging = true;
this.offsetX = this.x - px;
this.offsetY = this.y - py;
}
}
notPressed() {
this.dragging = false;
this.offsetX = 0;
this.offsetY = 0;
}
}