-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileParser.ino
163 lines (145 loc) · 3.99 KB
/
FileParser.ino
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
#define CS_PIN 10
File textFile;
String fileName = "music.txt";
/*****************
# FILE STRUCTURE
//////////////////
1. No. of melodic lines
2. No. of notes in a line
3. BPM of the song
Each next line represents a melodic line, which consists of
notes separated by space. Also, + means holding the previous note
and _ means playing no note. Example:
2
12
120
a2 a2 _ _ c2 + + a2 c2 + _ _
d4 c4 a4 + _ d4 c4 a4 + _ d4 a4
*****************/
void readFromSD() {
//Serial.println("Begin parsing! **********");
if (!SD.begin(CS_PIN)) {
//Serial.println("initialization failed!");
while (true);
}
textFile = SD.open(fileName);
readSongParameters();
initializeArray();
readSong();
//Serial.println("Done parsing! **********");
//Serial.println(songLength);
//Serial.println(songLines);
//Serial.println(noteLength);
for(int m = 0; m < songLines; m++){
for(int n = 0; n < songLength; n++){
//Serial.print(score[m][n]);
//Serial.print(" ");
}
//Serial.print("\n");
}
textFile.close();
}
void readSongParameters() {
String line = "";
while (textFile.available()) {;
char c = textFile.read();
if (c == '\n')
break;
line.concat(c);
}
noteLength = floor((double)1000 * (double)60 / (double)line.toInt());
inferLineSize();
inferLineCount();
}
void inferLineSize(){
unsigned long file_pos = textFile.position();
String token = "";
while (textFile.available()) {
char c = textFile.read();
if (c == '\n')
break;
else if (c == ' ' && token != "") {
token = "";
songLength += 1;
}
else if (c != ' ')
token.concat(c);
}
textFile.seek(file_pos - 1);
}
void inferLineCount(){
unsigned long file_pos = textFile.position();
while (textFile.available()) {
char c = textFile.read();
if (c == '\n')
songLines++;
}
textFile.seek(file_pos+1);
}
void initializeArray(){
score = new byte*[songLines];
for (byte i = 0; i < songLines; i++)
score[i] = new byte[songLength];
}
void readSong() {
byte pos = 0;
String token = "";
byte melodicLine = 0;
while (textFile.available()) {
char c = textFile.read();
if (c == '\n') {
melodicLine++;
token = "";
pos = 0;
}
else if (c == ' ' && token != "") {
addToArray(melodicLine, token, pos);
token = "";
pos += 1;
}
else if (c != ' ')
token.concat(c);
}
}
void addToArray(byte melodicLine, String actuaNote, byte pos) {
if (actuaNote.indexOf('+') >= 0)
score[melodicLine][pos] = NOTE_HOLD;
else if (actuaNote.indexOf("_") >= 0)
score[melodicLine][pos] = NOTE_NONE;
else
score[melodicLine][pos] = convertNote(actuaNote);
}
byte convertNote(String note) {
if (note == "e1") return 0;
else if (note == "f1") return 1;
else if (note == "g1") return 2;
else if (note == "a1") return 3;
else if (note == "b1") return 4;
else if (note == "c2") return 5;
else if (note == "d2") return 6;
else if (note == "e2") return 7;
else if (note == "f2") return 8;
else if (note == "g2") return 9;
else if (note == "a2") return 10;
else if (note == "b2") return 11;
else if (note == "c3") return 12;
else if (note == "d3") return 13;
else if (note == "e3") return 14;
else if (note == "f3") return 15;
else if (note == "g3") return 16;
else if (note == "a3") return 17;
else if (note == "b3") return 18;
else if (note == "c4") return 19;
else if (note == "d4") return 20;
else if (note == "e4") return 21;
else if (note == "f4") return 22;
else if (note == "g4") return 23;
else if (note == "a4") return 24;
else if (note == "b4") return 25;
else if (note == "c5") return 26;
else if (note == "d5") return 27;
else if (note == "e5") return 28;
else if (note == "f5") return 29;
else if (note == "g5") return 30;
else if (note == "a5") return 31;
}