-
Notifications
You must be signed in to change notification settings - Fork 18
/
Piano.java
207 lines (172 loc) · 6.6 KB
/
Piano.java
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
package com.nicobrailo.pianoli;
import android.util.Log;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* Backing model / state of our virtual piano keyboard.
*
* <p>
* Handles geometry, coordinate conversion and current state.
* </p>
* <p>
* If you wish to do anything in response to key-presses/releases, implement a {@link PianoListener},
* and register yourself via {@link #addListener(PianoListener)}.
* You'll then be notified of each event.
* </p>
*
* @see PianoCanvas
* @see PianoListener
*/
public class Piano {
/**
* Floor limit, if screensize dictates less than this amount of keys, start shrinking key width,
* so we always display at least an octave.
*
* @see #KEY_PREFERRED_WIDTH
*/
public static final int MIN_NUMBER_OF_KEYS = 7;
/** Preferred width (device independent pixels) of a key, but see also {@link #MIN_NUMBER_OF_KEYS} */
public static final int KEY_PREFERRED_WIDTH = 220;
private final int keys_width;
private final int keys_flat_width;
private final int keys_height;
private final int keys_flats_height;
private final int keys_count;
/** state tracker: which keys are <em>currently</em> pressed */
private final boolean[] key_pressed;
private final List<PianoListener> listeners;
/**
* Construct a partially initialised (geometry only) Piano model.
*
* @param screen_size_x the long dimension of the screen (keys are side-by-side along this axis)
* @param screen_size_y the short dimension of the screen.
*/
Piano(int screen_size_x, int screen_size_y) {
keys_height = screen_size_y;
keys_flats_height = (int) (screen_size_y * Key.FLAT_HEIGHT_RATIO);
keys_width = Math.min(screen_size_x / MIN_NUMBER_OF_KEYS, KEY_PREFERRED_WIDTH);
keys_flat_width = (int) (keys_width * Key.FLAT_WIDTH_RATIO);
// Round up for possible half-key display
final int big_keys = 1 + (screen_size_x / keys_width);
// Count flats too
// *2: Because ALL big keys get a matching flat-key, though for some its 0x0 pixels.
// +1: not sure about this... The *2 already ensures a (partial) flat-key on the (partial) big-key.
keys_count = (big_keys * 2) + 1;
key_pressed = new boolean[keys_count]; // new array defaults to all false;
listeners = new ArrayList<>();
}
int get_keys_flat_width() {
return keys_flat_width;
}
int get_keys_width() {
return keys_width;
}
int get_keys_count() {
return keys_count;
}
void resetState() {
Arrays.fill(key_pressed, false);
}
boolean is_key_pressed(int key_idx) {
if (isOutOfRange(key_idx)) {
Log.d("PianOli::Piano", "This shouldn't happen: isKeyPressed out of range, key" + key_idx);
return false;
}
return key_pressed[key_idx];
}
/**
* Switch key <code>keyIdx</code> to DOWN state, notifying all {@link PianoListener}s.
*
* @see PianoListener#onKeyDown(int)
*/
public void doKeyDown(int keyIdx) {
if (isOutOfRange(keyIdx)) {
Log.d("PianOli::Piano", "This shouldn't happen: Key-Down out of range, key" + keyIdx);
return;
}
Log.d("PianOli::Piano", "Key " + keyIdx + " is now DOWN");
key_pressed[keyIdx] = true;
for (PianoListener l : listeners) {
l.onKeyDown(keyIdx);
}
}
/**
* Switch key <code>keyIdx</code> to UP state, notifying all {@link PianoListener}s.
*
* @see PianoListener#onKeyUp(int)
*/
public void doKeyUp(int keyIdx) {
if (isOutOfRange(keyIdx)) {
Log.d("PianOli::Piano", "This shouldn't happen: Key-Up out of range, key" + keyIdx);
return;
}
Log.d("PianOli::Piano", "Key " + keyIdx + " is now UP");
key_pressed[keyIdx] = false;
for (PianoListener l : listeners) {
l.onKeyUp(keyIdx);
}
}
private boolean isOutOfRange(int key_idx) {
return key_idx < 0 || key_idx >= key_pressed.length;
}
/**
* @param l new Listener to be notified
* @return Per the {@link java.util.Collection#add(Object)} contract, <code>true</code> if the listener list changed as a result of this add,
* <code>false</code> if it was already subscribed.
*/
public boolean addListener(@NonNull PianoListener l) {
Objects.requireNonNull(l, "Listeners must not be null to avoid NullPointerExceptions on notify");
if (!listeners.contains(l)) { // don't double-add listeners, to avoid double-triggers
return listeners.add(l);
}
return false;
}
/**
* @param l Listener to unsubscribe.
* @return Per the {@link java.util.Collection#remove(Object)} contract, <code>true</code> if the listener was removed,
* <code>false</code> if it wasn't found.
*/
public boolean removeListener(PianoListener l) {
return listeners.remove(l);
}
int pos_to_key_idx(float pos_x, float pos_y) {
final int big_key_idx = 2 * ((int) pos_x / keys_width);
if (pos_y > keys_flats_height) return big_key_idx;
// Check if press is inside rect of flat key
Key flat = getAreaForSmallKey(big_key_idx + 1);
if (flat.contains(pos_x, pos_y)) return big_key_idx + 1;
if (big_key_idx > 0) {
Key prev_flat = getAreaForSmallKey(big_key_idx - 1);
if (prev_flat.contains(pos_x, pos_y)) return big_key_idx - 1;
}
// If not in the current or previous flat, it must be a hit in the big key
return big_key_idx;
}
@NonNull
Key getAreaForKey(int keyIdx) {
if ((keyIdx & 1) == 0) { // even positions are the full, big keys
return getAreaForBigKey(keyIdx);
} else {
return getAreaForSmallKey(keyIdx); // odd positions are the small/black/flat keys.
}
}
@NonNull
private Key getAreaForBigKey(int keyIdx) {
int x_i = keyIdx / 2 * keys_width;
return new Key(x_i, x_i + keys_width, 0, keys_height);
}
@NonNull
private Key getAreaForSmallKey(int keyIdx) {
final int octaveIdx = (keyIdx / 2) % 7;
if (octaveIdx == 2 || octaveIdx == 6) {
// Keys without flat get a null-area
return Key.CANT_TOUCH_THIS;
}
final int offset = keys_width - (keys_flat_width / 2);
int x_i = (keyIdx / 2) * keys_width + offset;
return new Key(x_i, x_i + keys_flat_width, 0, keys_flats_height);
}
}