-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTone.pde
66 lines (63 loc) · 2.04 KB
/
Tone.pde
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
class Tone {
int channel;
int pitch;
int velocity;
float durationMS;
float startMS;
int signal; // from which MIDI channel the tone was triggered
Tone(int c, int p, int v, float d, int s) {
channel = c;
pitch = p;
velocity = v;
durationMS = d;
startMS = millis();
signal = s;
// println("DEBUG: sending note with pitch "+pitch+" ...");
myBus.sendNoteOn(channel, pitch, velocity);
// append it to the list of active tones unless there is one with same the parameters c,p
boolean is_present = false;
for(int m=0; m<activeTones.length; m++) {
if(activeTones[m].channel == c && activeTones[m].pitch == p) {
activeTones[m].velocity = v;
activeTones[m].durationMS = d;
activeTones[m].startMS = millis();
activeTones[m].signal = s;
is_present = true;
}
}
// println("debug in Tone: adding tone, is_present: "+int(is_present));
if(!is_present) {
Tone[] newActiveTones = new Tone[activeTones.length+1];
for(int m=0; m<activeTones.length; m++) {
newActiveTones[m] = activeTones[m];
}
newActiveTones[activeTones.length] = this;
activeTones = newActiveTones;
}
// println("DEBUG: new number of active tones: "+activeTones.length);
}
void kill() {
myBus.sendNoteOff(this.channel, this.pitch, this.velocity);
}
}
void fadeOutTones() {
Tone ton;
for(int m=0; m<activeTones.length; m++) {
ton = activeTones[m];
if(millis()-ton.startMS > ton.durationMS) {
// remove tone from list and kill it
// println("debug in fadeOutTones: killing tone #"+m+", "+(activeTones.length-1)+" tones remaining active.");
// myBus.sendNoteOff(ton.channel, ton.pitch, ton.velocity);
ton.kill();
Tone[] newActiveTones = new Tone[activeTones.length-1];
int m_added = 0;
for(int mm=0; mm<activeTones.length; mm++) {
if(ton != activeTones[mm]) {
newActiveTones[m_added] = activeTones[mm];
m_added++;
}
}
activeTones = newActiveTones;
}
}
}