11#pragma once
22
3+ #include < iostream>
4+
35#include < cstdint>
46#include < fstream>
57#include < sstream>
1113namespace RhythmGameUtilities
1214{
1315
14- template <typename T> T ByteSwap (T value, int length = 8 )
16+ template <typename T> inline auto ByteSwap (T value, int length = 8 ) -> T
1517{
1618 return (value >> length) | (value << length);
1719}
1820
1921template <typename T>
20- T ReadChunk (std::istringstream &stream, int length = sizeof (T))
22+ inline auto ReadChunk (std::istringstream &stream, int length = sizeof (T)) -> T
2123{
2224 T chunk{};
2325 stream.read (reinterpret_cast <char *>(&chunk), length);
2426 return chunk;
2527}
2628
27- std::string ReadString (std::istringstream &stream, int length)
29+ inline auto ReadString (std::istringstream &stream, int length) -> std::string
2830{
2931 std::string chunk (length, ' \0 ' );
30- stream.read (& chunk[ 0 ] , length);
32+ stream.read (chunk. data () , length);
3133 return stream.gcount () == length ? chunk : " " ;
3234}
3335
34- uint32_t ReadVarLen (std::istringstream &stream)
36+ inline auto ReadVarLen (std::istringstream &stream) -> uint32_t
3537{
3638 uint32_t value = 0 ;
3739 uint8_t byte = 0 ;
3840 do
3941 {
4042 byte = ReadChunk<uint8_t >(stream);
4143 value = (value << 7 ) | (byte & 0x7F );
42- } while (byte & 0x80 );
44+ } while (( byte & 0x80 ) > 0 );
4345 return value;
4446}
4547
46- auto STATUS_NOTE_EVENT = 0x90 ;
47- auto STATUS_META_EVENT = 0xFF ;
48- auto TYPE_END_OF_TRACK = 0x2F ;
48+ const auto TYPE_END_OF_TRACK = 0x2F ;
49+
50+ const auto SYSTEM_COMMAND = 0xF0 ;
51+ const auto NOTE_OFF_COMMAND = 0x80 ;
52+ const auto NOTE_ON_COMMAND = 0x90 ;
53+ const auto CONTROL_CHANGE_COMMAND = 0xB0 ;
54+ const auto PROGRAM_CHANGE_COMMAND = 0xC0 ;
55+ const auto CHANNEL_PRESSURE_COMMAND = 0xD0 ;
4956
50- std::vector<Note> ReadMidiData (const std::vector<uint8_t > &data)
57+ inline auto ReadMidiData (const std::vector<uint8_t > &data) -> std::vector<Note>
5158{
5259 std::istringstream stream (std::string (data.begin (), data.end ()),
5360 std::ios::binary);
@@ -81,7 +88,7 @@ std::vector<Note> ReadMidiData(const std::vector<uint8_t> &data)
8188
8289 auto status = ReadChunk<uint8_t >(stream);
8390
84- if ((status & 0xF0 ) == STATUS_NOTE_EVENT )
91+ if ((status & SYSTEM_COMMAND ) == NOTE_ON_COMMAND )
8592 {
8693 Note note{.Position = static_cast <int >(absoluteTick),
8794 .HandPosition = ReadChunk<uint8_t >(stream)};
@@ -90,7 +97,7 @@ std::vector<Note> ReadMidiData(const std::vector<uint8_t> &data)
9097
9198 notes.push_back (note);
9299 }
93- else if (status == STATUS_META_EVENT )
100+ else if (( status & SYSTEM_COMMAND) == SYSTEM_COMMAND )
94101 {
95102 auto type = ReadChunk<uint8_t >(stream);
96103
@@ -103,7 +110,8 @@ std::vector<Note> ReadMidiData(const std::vector<uint8_t> &data)
103110 break ;
104111 }
105112 }
106- else if ((status & 0xF0 ) == 0xC0 || (status & 0xF0 ) == 0xD0 )
113+ else if ((status & SYSTEM_COMMAND) == PROGRAM_CHANGE_COMMAND ||
114+ (status & SYSTEM_COMMAND) == CHANNEL_PRESSURE_COMMAND)
107115 {
108116 stream.seekg (1 , std::ios::cur);
109117 }
@@ -117,7 +125,7 @@ std::vector<Note> ReadMidiData(const std::vector<uint8_t> &data)
117125 return notes;
118126}
119127
120- std::vector<Note> ReadMidiFile (const std::string &path)
128+ auto ReadMidiFile (const std::string &path) -> std::vector<Note>
121129{
122130 std::ifstream file (path, std::ios::binary | std::ios::ate);
123131
0 commit comments