Skip to content

Commit 2a67e0a

Browse files
authored
New parser and new melody
1 parent 7f7cabd commit 2a67e0a

File tree

2 files changed

+163
-146
lines changed

2 files changed

+163
-146
lines changed

FileParser.ino

+155-141
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,155 @@
1-
#define CS_PIN 10
2-
3-
File textFile;
4-
String fileName = "music.txt";
5-
6-
/*****************
7-
# FILE STRUCTURE
8-
//////////////////
9-
1. No. of melodic lines
10-
2. No. of notes in a line
11-
3. BPM of the song
12-
Each next line represents a melodic line, which consists of
13-
notes separated by space. Also, + means holding the previous note
14-
and _ means playing no note. Example:
15-
2
16-
12
17-
120
18-
a2 a2 _ _ c2 + + a2 c2 + _ _
19-
d4 c4 a4 + _ d4 c4 a4 + _ d4 a4
20-
*****************/
21-
22-
void readFromSD() {
23-
24-
//Serial.println("Begin parsing! **********");
25-
26-
if (!SD.begin(CS_PIN)) {
27-
//Serial.println("initialization failed!");
28-
while (true);
29-
}
30-
31-
textFile = SD.open(fileName);
32-
33-
readSongParameters();
34-
initializeArray();
35-
readSong();
36-
37-
//Serial.println("Done parsing! **********");
38-
39-
//Serial.println(songLength);
40-
//Serial.println(songLines);
41-
//Serial.println(noteLength);
42-
for(int m = 0; m < songLines; m++){
43-
for(int n = 0; n < songLength; n++){
44-
//Serial.print(score[m][n]);
45-
//Serial.print(" ");
46-
}
47-
//Serial.print("\n");
48-
}
49-
50-
textFile.close();
51-
}
52-
53-
void readSongParameters() {
54-
for (byte i = 0; i < 3; i++) {
55-
String line = "";
56-
while (textFile.available()) {
57-
char c = textFile.read();
58-
if (c == '\n')
59-
break;
60-
line.concat(c);
61-
}
62-
if (i == 0)
63-
songLines = line.toInt();
64-
else if (i == 1)
65-
songLength = line.toInt();
66-
else
67-
noteLength = floor((double)1000 * (double)60 / (double)line.toInt());
68-
}
69-
}
70-
71-
void initializeArray(){
72-
score = new byte*[songLines];
73-
for (byte i = 0; i < songLines; i++)
74-
score[i] = new byte[songLength];
75-
}
76-
77-
void readSong() {
78-
byte pos = 0;
79-
String token = "";
80-
byte melodicLine = 0;
81-
82-
while (textFile.available()) {
83-
char c = textFile.read();
84-
if (c == '\n') {
85-
melodicLine++;
86-
token = "";
87-
pos = 0;
88-
}
89-
else if (c == ' ' && token != "") {
90-
addToArray(melodicLine, token, pos);
91-
token = "";
92-
pos += 1;
93-
}
94-
else if (c != ' ')
95-
token.concat(c);
96-
}
97-
}
98-
99-
void addToArray(byte melodicLine, String actuaNote, byte pos) {
100-
if (actuaNote.indexOf('+') >= 0)
101-
score[melodicLine][pos] = NOTE_HOLD;
102-
else if (actuaNote.indexOf("_") >= 0)
103-
score[melodicLine][pos] = NOTE_NONE;
104-
else
105-
score[melodicLine][pos] = convertNote(actuaNote);
106-
}
107-
108-
byte convertNote(String note) {
109-
if (note == "e1") return 0;
110-
else if (note == "f1") return 1;
111-
else if (note == "g1") return 2;
112-
else if (note == "a1") return 3;
113-
else if (note == "b1") return 4;
114-
else if (note == "c2") return 5;
115-
else if (note == "d2") return 6;
116-
else if (note == "e2") return 7;
117-
else if (note == "f2") return 8;
118-
else if (note == "g2") return 9;
119-
else if (note == "a2") return 10;
120-
else if (note == "b2") return 11;
121-
else if (note == "c3") return 12;
122-
else if (note == "d3") return 13;
123-
else if (note == "e3") return 14;
124-
else if (note == "f3") return 15;
125-
else if (note == "g3") return 16;
126-
else if (note == "a3") return 17;
127-
else if (note == "b3") return 18;
128-
else if (note == "c4") return 19;
129-
else if (note == "d4") return 20;
130-
else if (note == "e4") return 21;
131-
else if (note == "f4") return 22;
132-
else if (note == "g4") return 23;
133-
else if (note == "a4") return 24;
134-
else if (note == "b4") return 25;
135-
else if (note == "c5") return 26;
136-
else if (note == "d5") return 27;
137-
else if (note == "e5") return 28;
138-
else if (note == "f5") return 29;
139-
else if (note == "g5") return 30;
140-
else if (note == "a5") return 31;
141-
}
1+
#define CS_PIN 10
2+
3+
File textFile;
4+
String fileName = "music.txt";
5+
6+
/*****************
7+
# FILE STRUCTURE
8+
//////////////////
9+
1. No. of melodic lines
10+
2. No. of notes in a line
11+
3. BPM of the song
12+
Each next line represents a melodic line, which consists of
13+
notes separated by space. Also, + means holding the previous note
14+
and _ means playing no note. Example:
15+
2
16+
12
17+
120
18+
a2 a2 _ _ c2 + + a2 c2 + _ _
19+
d4 c4 a4 + _ d4 c4 a4 + _ d4 a4
20+
*****************/
21+
22+
void readFromSD() {
23+
24+
//Serial.println("Begin parsing! **********");
25+
26+
if (!SD.begin(CS_PIN)) {
27+
//Serial.println("initialization failed!");
28+
while (true);
29+
}
30+
31+
textFile = SD.open(fileName);
32+
33+
readSongParameters();
34+
initializeArray();
35+
readSong();
36+
37+
//Serial.println("Done parsing! **********");
38+
39+
//Serial.println(songLength);
40+
//Serial.println(songLines);
41+
//Serial.println(noteLength);
42+
for(int m = 0; m < songLines; m++){
43+
for(int n = 0; n < songLength; n++){
44+
//Serial.print(score[m][n]);
45+
//Serial.print(" ");
46+
}
47+
//Serial.print("\n");
48+
}
49+
50+
textFile.close();
51+
}
52+
53+
void readSongParameters() {
54+
for (byte i = 0; i < 2; i++) {
55+
String line = "";
56+
while (textFile.available()) {
57+
char c = textFile.read();
58+
if (c == '\n')
59+
break;
60+
line.concat(c);
61+
}
62+
if (i == 0)
63+
songLines = line.toInt();
64+
else
65+
noteLength = floor((double)1000 * (double)60 / (double)line.toInt());
66+
}
67+
}
68+
69+
void initializeArray(){
70+
int file_pos = textFile.position();
71+
String token = "";
72+
while (textFile.available()) {
73+
char c = textFile.read();
74+
if (c == '\n')
75+
break;
76+
else if (c == ' ' && token != "") {
77+
token = "";
78+
songLength += 1;
79+
}
80+
else if (c != ' ')
81+
token.concat(c);
82+
}
83+
songLength += 1;
84+
textFile.seek(file_pos);
85+
86+
score = new byte*[songLines];
87+
for (byte i = 0; i < songLines; i++)
88+
score[i] = new byte[songLength];
89+
}
90+
91+
void readSong() {
92+
byte pos = 0;
93+
String token = "";
94+
byte melodicLine = 0;
95+
96+
while (textFile.available()) {
97+
char c = textFile.read();
98+
if (c == '\n') {
99+
melodicLine++;
100+
token = "";
101+
pos = 0;
102+
}
103+
else if (c == ' ' && token != "") {
104+
addToArray(melodicLine, token, pos);
105+
token = "";
106+
pos += 1;
107+
}
108+
else if (c != ' ')
109+
token.concat(c);
110+
}
111+
}
112+
113+
void addToArray(byte melodicLine, String actuaNote, byte pos) {
114+
if (actuaNote.indexOf('+') >= 0)
115+
score[melodicLine][pos] = NOTE_HOLD;
116+
else if (actuaNote.indexOf("_") >= 0)
117+
score[melodicLine][pos] = NOTE_NONE;
118+
else
119+
score[melodicLine][pos] = convertNote(actuaNote);
120+
}
121+
122+
byte convertNote(String note) {
123+
if (note == "e1") return 0;
124+
else if (note == "f1") return 1;
125+
else if (note == "g1") return 2;
126+
else if (note == "a1") return 3;
127+
else if (note == "b1") return 4;
128+
else if (note == "c2") return 5;
129+
else if (note == "d2") return 6;
130+
else if (note == "e2") return 7;
131+
else if (note == "f2") return 8;
132+
else if (note == "g2") return 9;
133+
else if (note == "a2") return 10;
134+
else if (note == "b2") return 11;
135+
else if (note == "c3") return 12;
136+
else if (note == "d3") return 13;
137+
else if (note == "e3") return 14;
138+
else if (note == "f3") return 15;
139+
else if (note == "g3") return 16;
140+
else if (note == "a3") return 17;
141+
else if (note == "b3") return 18;
142+
else if (note == "c4") return 19;
143+
else if (note == "d4") return 20;
144+
else if (note == "e4") return 21;
145+
else if (note == "f4") return 22;
146+
else if (note == "g4") return 23;
147+
else if (note == "a4") return 24;
148+
else if (note == "b4") return 25;
149+
else if (note == "c5") return 26;
150+
else if (note == "d5") return 27;
151+
else if (note == "e5") return 28;
152+
else if (note == "f5") return 29;
153+
else if (note == "g5") return 30;
154+
else if (note == "a5") return 31;
155+
}

music.txt

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
2
2-
8
3-
200
4-
c3 e3 g3 b3 c3 e3 g3 b3
5-
f3 a3 c4 d3 f3 a3 c4 d3
1+
6
2+
120
3+
e4 + + + + + d4 + f4 + + + + e4 d4 c4 d4 e4 e4 + + + d4 + c4 + + c4 c4 + + + e4 + + d4 d4 + + + e4 + + d4 d4 + + e4 c4 d4 + b3 c4 d4 + b3 c4 b3 a3 + e4 d4 + c4 b3 c4 c4 + c4 + + + b3 c4 + c4 + d4 c4 b3 b3 + + + c4 + + +
4+
c4 + + + + + a3 + c4 + + + + g3 f3 e3 _ _ c4 + + + + + a3 + + a3 a3 + + + c4 + + b3 b3 + + + b3 + + + + + b3 + a3 + + + a3 + + + a3 _ _ _ c4 b3 + a3 g3 + g3 + + + + + _ _ _ a3 + _ _ _ g3 + + + g3 + + +
5+
g3 + + + + + f3 + a3 + + + + _ _ _ f3 g3 g3 + + + g3 + e3 + + e3 e3 + + + g3 + + g3 g3 + + + g3 + + + + + g3 + e3 + + + f3 + + + f3 + + + g3 g3 + f3 d3 + e3 + + + + + f3 + + f3 + f3 e3 d3 d3 + + + e3 + + +
6+
c3 + + + c3 + + + f2 + + + f2 + + + d2 + g2 + + + g2 + a2 + + + a2 + + + g2 + + + g2 + + + e2 + + + e2 + + + a2 + + + a2 + + + f2 + + + f2 g2 g2 f2 g2 + + + g2 + + + f2 + + + + + + + g2 + + + e2 + + +
7+
g2 + + + g2 + + + c2 + + + c2 + + + _ _ _ _ _ _ _ _ e2 + + + e2 + + + _ _ _ _ d2 + + + b1 + + + b1 + + + e2 + + + f2 + + + c2 + + + c2 _ d2 c2 d2 + + + _ _ _ _ c2 + + + + + + + d2 + + + c2 + + +
8+
c2 + + + c2 + + + f1 + + + f1 + + + g1 + c2 + + + b1 + a1 + + + a1 + + + c2 + + + g1 + + + e1 + + + e1 + + + a1 + + + c2 + + + f1 + + + f1 c2 g1 f1 g1 + + + c2 + + + f1 + + + + + + g1 g1 + + + g1 + + +

0 commit comments

Comments
 (0)