-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestReceiver.java
More file actions
60 lines (53 loc) · 1.84 KB
/
TestReceiver.java
File metadata and controls
60 lines (53 loc) · 1.84 KB
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
import javax.sound.midi.*;
import java.util.*;
/**
* Utility class to support <tt>PianoTester.java</tt>
* that allows the tester to query different properties
* of the keys in the virtual Piano keyboard.
*/
public class TestReceiver implements Receiver {
private Map<Integer, Boolean> _keyIsOnMap = new HashMap<>();
private Map<Integer, Integer> _keyCountOnMap = new HashMap<>();
private Map<Integer, Integer> _keyCountOffMap = new HashMap<>();
/**
* Returns whether the key of the specified pitch is currently on.
* @param pitch the pitch to query
* @return whether the key is on
*/
public boolean isKeyOn (int pitch) {
return _keyIsOnMap.containsKey(pitch) ? _keyIsOnMap.get(pitch) : false;
}
/**
* Returns the count of how many times the key with the specified pitch has been turned on.
* @param pitch the pitch to query
* @return the count.
*/
public int getKeyOnCount (int pitch) {
return _keyCountOnMap.containsKey(pitch) ? _keyCountOnMap.get(pitch) : 0;
}
/**
* Returns the count of how many times the key with the specified pitch has been turned off.
* @param pitch the pitch to query
* @return the count.
*/
public int getKeyOffCount (int pitch) {
return _keyCountOffMap.containsKey(pitch) ? _keyCountOffMap.get(pitch) : 0;
}
public TestReceiver () {
}
@Override
public void close () {
}
@Override
public void send (MidiMessage midiMessage, long timeStamp) {
ShortMessage message = (ShortMessage) midiMessage;
int pitch = message.getData1();
if (message.getCommand() == ShortMessage.NOTE_ON) {
_keyIsOnMap.put(message.getData1(), true);
_keyCountOnMap.put(pitch, _keyCountOnMap.getOrDefault(pitch, 0) + 1);
} else if (message.getCommand() == ShortMessage.NOTE_OFF) {
_keyIsOnMap.put(message.getData1(), false);
_keyCountOffMap.put(pitch, _keyCountOffMap.getOrDefault(pitch, 0) + 1);
}
}
}