Created
December 2, 2017 16:27
-
-
Save mgostIH/d452e6c7b2037a1aa7aa859677d79ec2 to your computer and use it in GitHub Desktop.
Organya file reading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Organya.hpp" | |
#include <fstream> | |
#include <iomanip> | |
Org Load_Organya(const char * filename); | |
int main() { | |
Org org = Load_Organya("test.org"); | |
if (org) { | |
std::cout << org.header<<'\n'; | |
for (const auto & c : org.tracks) std::cout << c << '\n'; | |
} | |
else { | |
std::cout << "Error of some sort.\n"; | |
std::cout << org.header; | |
} | |
system("PAUSE>NUL"); | |
} | |
Org Load_Organya(const char * filename) { | |
Org org; | |
std::ifstream file(filename, std::ios::binary); | |
if (!file) return org; | |
file >> org.header; | |
if (!org) return org; //Check if header magic is correct | |
for (auto & track : org.tracks) file >> track; //Read all tracks | |
for (auto & track : org.tracks) { | |
for (int i = 0; i < track.note_count; i++) { | |
OrgNote note; | |
file >> note; | |
track.notes.push_back(note); //Adds each note to the track's vector | |
} | |
} | |
return org; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef int int32; typedef unsigned int uint32; | |
typedef short int16; typedef unsigned short uint16; | |
typedef char int8; typedef unsigned char uint8; | |
#include <array> | |
#include <vector> | |
#include <algorithm> | |
#include <iterator> | |
#include <iostream> | |
//18 total bytes, not sizeof struct | |
struct OrgHeader { | |
std::array<char, 6> magic; | |
uint16 tempo; | |
uint8 beats, steps; | |
uint32 loop_start, loop_end; | |
}; | |
//8 total bytes | |
struct OrgNote { | |
uint32 position; | |
uint8 key, length, volume, pan; | |
}; | |
//6 total bytes, not sizeof struct | |
struct OrgTrack { | |
uint16 freq; | |
uint8 instrument, pi; | |
uint16 note_count; | |
std::vector<OrgNote> notes; | |
}; | |
struct Org { | |
OrgHeader header; | |
std::array<OrgTrack,16> tracks; | |
explicit operator bool() const { //Allows for if(Org) | |
return std::equal(header.magic.begin(), header.magic.end(), "Org-02"); | |
} | |
}; | |
//OrgHeader input | |
std::istream& operator>>(std::istream& stream, OrgHeader& lhs) { | |
auto it = std::istreambuf_iterator<char>(stream); | |
std::copy_n(it, lhs.magic.size(), lhs.magic.begin()); //Writing on array | |
it++; | |
stream.read((char*)&lhs.tempo, sizeof(lhs.tempo)); | |
stream.read((char*)&lhs.beats, sizeof(lhs.beats)); | |
stream.read((char*)&lhs.steps, sizeof(lhs.steps)); | |
stream.read((char*)&lhs.loop_start, sizeof(lhs.loop_start)); | |
stream.read((char*)&lhs.loop_end, sizeof(lhs.loop_end)); | |
return stream; | |
} | |
//OrgHeader output | |
std::ostream& operator<<(std::ostream& stream, const OrgHeader& lhs) { | |
stream << "Magic: "; | |
for (const auto & i : lhs.magic) stream << i; | |
stream << "\nTempo: " << lhs.tempo << "\nBeats: " << +lhs.beats << "\nSteps: " << +lhs.steps << | |
"\nLoops from measure: " << (lhs.loop_start / (lhs.beats * lhs.steps)) << "\nTo measure: " << (lhs.loop_end / (lhs.beats * lhs.steps)) << '\n'; | |
return stream; | |
} | |
//OrgNote input | |
std::istream& operator>>(std::istream& stream, OrgNote& lhs) { | |
auto it = std::istreambuf_iterator<char>(stream); | |
stream.read((char*)&lhs.position, sizeof(lhs.position)); | |
stream.read((char*)&lhs.key, sizeof(lhs.key)); | |
stream.read((char*)&lhs.length, sizeof(lhs.length)); | |
stream.read((char*)&lhs.volume, sizeof(lhs.volume)); | |
stream.read((char*)&lhs.pan, sizeof(lhs.pan)); | |
return stream; | |
} | |
//OrgNote output | |
std::ostream& operator<<(std::ostream& stream, const OrgNote& lhs) { | |
stream << "Position: " << lhs.position << "\nKey: " << +lhs.key << "\nLength: " << +lhs.length << | |
"\nVolume: " << +lhs.volume << "\nPan: " << +lhs.pan << '\n'; | |
return stream; | |
} | |
//OrgTrack input | |
std::istream& operator>>(std::istream& stream, OrgTrack& lhs) { | |
auto it = std::istreambuf_iterator<char>(stream); | |
stream.read((char*)&lhs.freq, sizeof(lhs.freq)); | |
stream.read((char*)&lhs.instrument, sizeof(lhs.instrument)); | |
stream.read((char*)&lhs.pi, sizeof(lhs.pi)); | |
stream.read((char*)&lhs.note_count, sizeof(lhs.note_count)); | |
return stream; | |
} | |
//OrgTrack output | |
std::ostream& operator<<(std::ostream& stream, const OrgTrack& lhs) { | |
stream << "Frequency: " << lhs.freq << "\nInstrument: " << +lhs.instrument << "\nPi: " << +lhs.pi << | |
"\nNote Count: " << +lhs.note_count <<'\n'; | |
return stream; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment