forked from nicolasbrailo/PianOli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleSongMelody.java
125 lines (91 loc) · 3.26 KB
/
SingleSongMelody.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
package com.nicobrailo.pianoli;
import java.util.Locale;
public class SingleSongMelody implements Melody {
public static final SingleSongMelody im_a_little_teapot = SingleSongMelody.fromString(
"C D E F G C2 " +
// I’m a little teapot
"A C2 G " +
// Short and stout
"F F F E E " +
// Here is my handle
"D D D C " +
// Here is my spout
"C D E F G C2 " +
// When I get all steamed up
"A C2 G " +
// Hear me shout
"C2 A G G " +
// “Tip me over
"F E D C "
// And pour me out!”
);
public static final SingleSongMelody twinkle_twinkle_little_star = SingleSongMelody.fromString(
"C C G G A A G " +
// Twinkle, twinkle, little star
"F F E E D D C " +
// How I wonder what you are!
"G G F F E E D " +
// Up above the world so high,
"G G F F E E D " +
// Like a diamond in the sky...
"C C G G A A G " +
// Twinkle, twinkle, little star
"F F E E D D C"
// How I wonder what you are!
);
public static final SingleSongMelody insy_winsy_spider = SingleSongMelody.fromString(
"G1 C2 C2 C2 D2 E2 E2 " +
// "Insy-winsy spider...
"E2 D2 C2 D2 E2 C2 " +
// "... climbed up the water spout.
"E2 E2 F2 G2 " +
// Down came the rain...
"G2 F2 E2 F2 G2 E2 " +
// ... and washed the spider out.
"C2 C2 D2 E2 " +
// Out came the sun...
"E2 D2 C2 D2 E2 C2 " +
// ... and dried up all the rain.
"G1 G1 C2 C2 C2 D2 E2 E2 " +
// Insy-winsy spider...
"E2 D2 C2 D2 E2 C2"
// ... climbed up the spout again.
);
public static final SingleSongMelody[] all = new SingleSongMelody[] {
twinkle_twinkle_little_star,
insy_winsy_spider
};
/**
* A somewhat-robust string to melody parser.
* Allows melodies to be specified as a string of notes, where the notes are: "A", "B1", "C#1", "G2", etc.
*
* Notes are separated by whitespace.
*
* Notes in the first octave can leave off the octave designation and it will be automatically
* appended (i.e. "C" will become "C1"). This makes it simpler to write songs that fall within a single octave.
*/
static SingleSongMelody fromString(String melody) {
String[] notes = melody.trim().toUpperCase(Locale.ENGLISH).split("\\s+");
for (int i = 0; i < notes.length; i ++) {
if (!notes[i].matches(".*\\d$")) {
notes[i] = notes[i] + "1";
}
}
return new SingleSongMelody(notes);
}
private int melody_idx = 0;
private final String[] notes;
SingleSongMelody(String[] notes) {
this.notes = notes;
}
@Override
public String nextNote() {
String nextNote = notes[melody_idx];
melody_idx = (melody_idx + 1) % notes.length;
return nextNote;
}
@Override
public boolean hasNextNote() {
return melody_idx < notes.length - 1;
}
}