Skip to content

Commit a250458

Browse files
committed
Added song struct and parse utilities.
1 parent 537c06d commit a250458

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

includes/RhythmGameUtilities/Parsers.hpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#pragma once
22

3+
#include <algorithm>
4+
#include <map>
35
#include <regex>
46
#include <string>
57
#include <vector>
68

9+
#include "Enums/Difficulty.h"
10+
#include "Enums/NamedSection.h"
11+
#include "Enums/TypeCode.h"
12+
13+
#include "Structs/Note.h"
14+
715
#include "Utilities.hpp"
816

917
#ifdef _WIN32
@@ -91,4 +99,96 @@ std::vector<ChartSection> ParseSectionsFromChart(const char *contents)
9199
return sections;
92100
}
93101

102+
std::map<int, int>
103+
ParseTimeSignaturesFromChartSection(std::vector<ChartSection> sections)
104+
{
105+
auto timeSignatures = std::map<int, int>();
106+
107+
for (auto &section : sections)
108+
{
109+
if (section.name == ToString(NamedSection::SyncTrack))
110+
{
111+
for (auto &line : section.lines)
112+
{
113+
if (line.second.front() ==
114+
ToString(TypeCode::TimeSignatureMarker))
115+
{
116+
timeSignatures.insert(
117+
{std::stoi(line.first), std::stoi(line.second.at(1))});
118+
}
119+
}
120+
}
121+
}
122+
123+
return timeSignatures;
124+
}
125+
126+
std::map<int, int> ParseBpmFromChartSection(std::vector<ChartSection> sections)
127+
{
128+
auto bpm = std::map<int, int>();
129+
130+
for (auto &section : sections)
131+
{
132+
if (section.name == ToString(NamedSection::SyncTrack))
133+
{
134+
for (auto &line : section.lines)
135+
{
136+
if (line.second.front() == ToString(TypeCode::BPM_Marker))
137+
{
138+
bpm.insert(
139+
{std::stoi(line.first), std::stoi(line.second.at(1))});
140+
}
141+
}
142+
}
143+
}
144+
145+
return bpm;
146+
}
147+
148+
std::vector<Note> ParseNotesFromChartSection(std::vector<ChartSection> sections,
149+
Difficulty difficulty)
150+
{
151+
auto notes = std::vector<Note>();
152+
153+
for (auto &section : sections)
154+
{
155+
if (section.name == ToString(difficulty) + "Single")
156+
{
157+
for (auto &line : section.lines)
158+
{
159+
if (line.second.front() == ToString(TypeCode::NoteMarker))
160+
{
161+
notes.push_back({std::stoi(line.first),
162+
std::stoi(line.second.at(1)),
163+
std::stoi(line.second.at(2))});
164+
}
165+
}
166+
}
167+
}
168+
169+
return notes;
170+
}
171+
172+
std::map<int, std::string>
173+
ParseLyricsFromChartSection(std::vector<ChartSection> sections)
174+
{
175+
auto lyrics = std::map<int, std::string>();
176+
177+
for (auto &section : sections)
178+
{
179+
if (section.name == ToString(NamedSection::Events))
180+
{
181+
for (auto &line : section.lines)
182+
{
183+
if (line.second.back().rfind("lyric", 0) == 0)
184+
{
185+
lyrics.insert({std::stoi(line.first), line.second.at(1)});
186+
}
187+
}
188+
}
189+
}
190+
191+
return lyrics;
192+
}
193+
94194
} // namespace RhythmGameUtilities
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include <map>
4+
#include <string>
5+
6+
#include "RhythmGameUtilities/Enums/Difficulty.h"
7+
#include "RhythmGameUtilities/Structs/BeatBar.h"
8+
#include "RhythmGameUtilities/Structs/Note.h"
9+
10+
namespace RhythmGameUtilities
11+
{
12+
13+
struct Song
14+
{
15+
16+
// Title of the song.
17+
std::string Name;
18+
19+
// Artist(s) or band(s) behind the song.
20+
std::string Artist;
21+
22+
// Title of the album the song is featured in.
23+
std::string Album;
24+
25+
// Genre of the song.
26+
std::string Genre;
27+
28+
// Year of the song’s release.<br />Typically preceded by a comma and space,
29+
// for example `, 2002`, to make importing into GHTCP quicker.
30+
std::string Year;
31+
32+
// Community member who charted the song.
33+
std::string Charter;
34+
35+
// (Required) Number of positional ticks between each 1/4th note in the
36+
// chart.
37+
int Resolution;
38+
39+
// Estimated difficulty of the song.
40+
int Difficulty;
41+
42+
// Start time of the audio, in seconds.<br />A higher value makes the audio
43+
// start sooner.
44+
double Offset;
45+
46+
// Time of the song, in seconds, where the song preview should start.
47+
double PreviewStart;
48+
49+
// Time of the song, in seconds, where the song preview should end.
50+
double PreviewEnd;
51+
52+
// The main audio stream.<br />When other audio stems are present, this is
53+
// background audio not in the other tracks and/or instruments not charted.
54+
std::string MusicStream;
55+
56+
std::map<int, std::string> Lyrics;
57+
58+
std::map<DifficultyType, std::vector<Note>> Difficulties;
59+
60+
std::map<int, int> BPM;
61+
62+
std::map<int, std::vector<int>> TimeSignatures;
63+
64+
std::vector<BeatBar> BeatBars;
65+
};
66+
67+
} // namespace RhythmGameUtilities

tests/RhythmGameUtilities/Parsers.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,59 @@ void testParseValuesFromChartSections()
104104
std::cout << ".";
105105
}
106106

107+
void testParseTimeSignaturesFromChartSection()
108+
{
109+
auto sections = ParseSectionsFromChart(contents);
110+
111+
auto lines = ParseTimeSignaturesFromChartSection(sections);
112+
113+
assert(lines.size() == 4);
114+
115+
std::cout << ".";
116+
}
117+
118+
void testParseBpmFromChartSection()
119+
{
120+
auto sections = ParseSectionsFromChart(contents);
121+
122+
auto lines = ParseBpmFromChartSection(sections);
123+
124+
assert(lines.size() == 7);
125+
126+
std::cout << ".";
127+
}
128+
129+
void testParseNotesFromChartSection()
130+
{
131+
auto sections = ParseSectionsFromChart(contents);
132+
133+
auto lines = ParseNotesFromChartSection(sections, Difficulty::Expert);
134+
135+
assert(lines.size() == 8);
136+
137+
std::cout << ".";
138+
}
139+
140+
void testParseLyricsFromChartSection()
141+
{
142+
auto sections = ParseSectionsFromChart(contents);
143+
144+
auto lines = ParseLyricsFromChartSection(sections);
145+
146+
assert(lines.size() == 12);
147+
148+
std::cout << ".";
149+
}
150+
107151
int main()
108152
{
109153
testParseSectionsFromChart();
110154
testParseValuesFromChartSections();
111155

156+
testParseTimeSignaturesFromChartSection();
157+
testParseBpmFromChartSection();
158+
testParseNotesFromChartSection();
159+
testParseLyricsFromChartSection();
160+
112161
return 0;
113162
}

0 commit comments

Comments
 (0)