-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayhead.pde
132 lines (111 loc) · 2.76 KB
/
playhead.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
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
class Playhead {
float position;
int direction;
int directionOffset; // 1 or -1
int offset; // in integer pixels
float speed; // where 1.0 is same as default
boolean active;
int col;
ArrayList<Integer> playedNotes;
PApplet parent;
Stave stave;
Playhead (Stave s, PApplet p, int c, boolean a) {
this.parent = p;
this.stave = s;
position = 0;
direction = 1;
directionOffset = 1;
offset = 0;
speed = 1.0;
active = a;
col = c;
clearPlayed();
}
void activate() {
active = true;
render();
}
void deactivate() {
active = false;
}
void toggle() {
active = !active;
}
boolean isForwards() {
return directionOffset > 0;
}
boolean isBackwards() {
return directionOffset < 0;
}
void changeOffset(int o, float reference) {
offset = o;
position = reference + o;
if(position > stave.staveWidth) {
position = position % stave.staveWidth;
}
if(position < 0) {
position = stave.staveWidth + position;
}
}
void render() {
strokeWeight(2);
stroke(col);
float x = position + stave.xPadding;
line(x,0,x,height);
}
void renderIntersections() {
for (Note n : stave.notes) {
if(n.intersectedBy(position)) {
noStroke();
fill(col);
int absX = n.x + stave.xPadding;
int absY = n.y + stave.topPadding;
float dia = n.diameter + 8;
ellipse(absX, absY,dia,dia);
}
}
}
void modifyPositionBy(float amt) {
position += (amt * directionOffset * speed);
// notify stave if we've changed direction
if((direction == 1) && (amt < 0)) {
direction = 1 - direction;
}
if((direction == 0) && (amt > 0)) {
direction = 1 - direction;
}
if(position > stave.staveWidth) {
position = position % stave.staveWidth;
clearPlayed();
}
if(position < 0) {
position = stave.staveWidth + position;
clearPlayed();
}
}
void playNotes() {
for (Note n : stave.notes) {
if(n.intersectedBy(position)) {
// println("I should play " + n);
if(playedNotes.indexOf(n.hashCode()) < 0) {
println("Note not in played list");
n.play();
playedNotes.add(n.hashCode());
println(playedNotes.indexOf(n.hashCode()));
} else {
// println("But I won't: Note in played list at index ", playedNotes.indexOf(n.hashCode()));
}
} else {
// if it's in the played list, and we're not intersecting it
// remove it
if(playedNotes.indexOf(n.hashCode()) > -1) {
int i = playedNotes.indexOf(n.hashCode());
playedNotes.remove(i);
}
}
}
}
void clearPlayed() {
playedNotes = new ArrayList<Integer>();
}
}