Compare commits
No commits in common. "c54a7a7bd1811f8fd45042188d16cf8ff2ae0d87" and "f9ce5620447a152b406de8b96a45bf52d8f9db02" have entirely different histories.
c54a7a7bd1
...
f9ce562044
@ -30,6 +30,7 @@ struct Header {
|
||||
std::string name;
|
||||
int ppq;
|
||||
float bpm;
|
||||
double tempo;
|
||||
};
|
||||
|
||||
struct Song {
|
||||
@ -37,6 +38,13 @@ struct Song {
|
||||
std::vector<Track> tracks;
|
||||
};
|
||||
|
||||
int ticksToMilliseconds(int ticks, double tempo, int ppq) {
|
||||
double microseconds = (ticks * tempo) / ppq;
|
||||
|
||||
// Convert to milliseconds with rounding
|
||||
return static_cast<int>(microseconds / 1000.0 + 0.5);
|
||||
}
|
||||
|
||||
Song parseSong(const rapidjson::Document& doc) {
|
||||
assert(doc.IsObject());
|
||||
|
||||
@ -51,6 +59,7 @@ Song parseSong(const rapidjson::Document& doc) {
|
||||
if (h.HasMember("tempos") && h["tempos"].IsArray()) {
|
||||
const auto& tempos = h["tempos"].GetArray();
|
||||
header.bpm = tempos[0]["bpm"].GetFloat();
|
||||
header.tempo = 60000000.0 / header.bpm;
|
||||
}
|
||||
song.header = header;
|
||||
}
|
||||
@ -76,9 +85,9 @@ Song parseSong(const rapidjson::Document& doc) {
|
||||
note.name = n["name"].GetString();
|
||||
note.midi = n["midi"].GetInt();
|
||||
note.duration_ticks = n["durationTicks"].GetInt();
|
||||
note.duration_ms = n["duration"].GetFloat() * 1000;
|
||||
note.duration_ms = ticksToMilliseconds(note.duration_ticks, song.header.tempo, song.header.ppq);
|
||||
note.ticks = n["ticks"].GetInt();
|
||||
note.position_ms = n["time"].GetFloat() * 1000;
|
||||
note.position_ms = ticksToMilliseconds(note.ticks, song.header.tempo, song.header.ppq);
|
||||
track.notes.push_back(note);
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ int main(int argc, char** argv)
|
||||
Song& song = song_manager->load_song("mary_had_a_lil_lamb", "assets/songs/json/mary.json");
|
||||
printf("Song name: %s\n", song.header.name.c_str());
|
||||
printf("Song bpm: %f\n", song.header.bpm);
|
||||
printf("Song tempo: %f\n", song.header.tempo);
|
||||
printf("First note duration: %d\n", song.tracks[0].notes[0].duration_ms);
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user