Skip to content

Commit 98f862d

Browse files
committed
Cleaned up midi parsing.
Syntax updates.
1 parent cf4c474 commit 98f862d

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

include/RhythmGameUtilities/Midi.hpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <iostream>
4+
35
#include <cstdint>
46
#include <fstream>
57
#include <sstream>
@@ -11,43 +13,48 @@
1113
namespace 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

1921
template <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

include/RhythmGameUtilities/MidiInternal.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#pragma once
22

3-
#include <map>
4-
53
#include "Structs/Note.hpp"
64

75
#include "Midi.hpp"
@@ -18,16 +16,16 @@ namespace RhythmGameUtilities
1816
extern "C"
1917
{
2018

21-
PACKAGE_API Note *ReadMidiDataInternal(const uint8_t *data, int dataSize,
22-
int *outSize)
19+
PACKAGE_API auto ReadMidiDataInternal(const uint8_t *data, int dataSize,
20+
int *outSize) -> Note *
2321
{
2422
std::vector<uint8_t> byteVector(data, data + dataSize);
2523

2624
auto internalNotes = ReadMidiData(byteVector);
2725

2826
*outSize = internalNotes.size();
2927

30-
auto notes = (Note *)malloc(internalNotes.size() * sizeof(Note));
28+
auto *notes = (Note *)malloc(internalNotes.size() * sizeof(Note));
3129

3230
for (auto i = 0; i < internalNotes.size(); i += 1)
3331
{
@@ -37,14 +35,14 @@ extern "C"
3735
return notes;
3836
}
3937

40-
PACKAGE_API Note *ReadMidiFileInternal(const std::string &path,
41-
int *outSize)
38+
PACKAGE_API auto ReadMidiFileInternal(const std::string &path, int *outSize)
39+
-> Note *
4240
{
4341
auto internalNotes = ReadMidiFile(path);
4442

4543
*outSize = internalNotes.size();
4644

47-
auto notes = (Note *)malloc(internalNotes.size() * sizeof(Note));
45+
auto *notes = (Note *)malloc(internalNotes.size() * sizeof(Note));
4846

4947
for (auto i = 0; i < internalNotes.size(); i += 1)
5048
{

0 commit comments

Comments
 (0)