Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some backported stuff from my Touhou 8 decomp work #358

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Backport MidiOutput::Ntohl
MidiOutput::ParseFile is temporarily non-matching again with this commit.
  • Loading branch information
KSSBrawl committed Jan 8, 2025
commit 668cc41f9497ef028539bdb2b39ac52a946a34d0
20 changes: 3 additions & 17 deletions src/MidiOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,30 +222,17 @@ void MidiOutput::ClearTracks()
this->numTracks = 0;
}

inline u32 InlineNtohl(u32 val, u8 *tmp)
{
tmp[0] = ((u8 *)&val)[3];
tmp[1] = ((u8 *)&val)[2];
tmp[2] = ((u8 *)&val)[1];
tmp[3] = ((u8 *)&val)[0];

return *(const u32 *)tmp;
}

#pragma var_order(trackIdx, currentCursor, currentCursorTrack, fileData, hdrLength, hdrRaw, trackLength, \
endOfHeaderPointer, tmpHdrLength, trackArraySize, tmpTrackLength, trackLengthRaw)
endOfHeaderPointer, trackArraySize)
ZunResult MidiOutput::ParseFile(i32 fileIdx)
{
u8 trackLengthRaw[4];
u8 hdrRaw[8];
u32 trackLength;
u8 *currentCursor, *currentCursorTrack, *endOfHeaderPointer;
i32 trackIdx;
u8 *fileData;
u32 hdrLength;
size_t trackArraySize;
u8 tmpHdrLength[4];
u8 tmpTrackLength[4];

this->ClearTracks();
currentCursor = this->midiFileData[fileIdx];
Expand All @@ -262,7 +249,7 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx)

// Get a pointer to the end of the header chunk
currentCursor += sizeof(hdrRaw);
hdrLength = InlineNtohl(*(u32 *)(&hdrRaw[4]), tmpHdrLength);
hdrLength = MidiOutput::Ntohl(*(u32 *)(hdrRaw + 4));

endOfHeaderPointer = currentCursor;
currentCursor += hdrLength;
Expand Down Expand Up @@ -293,8 +280,7 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx)
// Read a track (MTrk) chunk.
//
// First, read the length of the chunk
memcpy(trackLengthRaw, currentCursorTrack + 4, 4);
trackLength = InlineNtohl(*(u32 *)trackLengthRaw, tmpTrackLength);
trackLength = MidiOutput::Ntohl(*(u32 *)(currentCursorTrack + 4));
this->tracks[trackIdx].trackLength = trackLength;
this->tracks[trackIdx].trackData = (u8 *)malloc(trackLength);
this->tracks[trackIdx].trackPlaying = 1;
Expand Down
12 changes: 12 additions & 0 deletions src/MidiOutput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ struct MidiOutput : MidiTimer
static u16 Ntohs(u16 val);
static u32 SkipVariableLength(u8 **curTrackDataCursor);

static u32 Ntohl(u32 val)
{
u8 tmp[4];

tmp[0] = ((u8 *)&val)[3];
tmp[1] = ((u8 *)&val)[2];
tmp[2] = ((u8 *)&val)[1];
tmp[3] = ((u8 *)&val)[0];

return *(const u32 *)tmp;
}

MIDIHDR *midiHeaders[32];
i32 midiHeadersCursor;
u8 *midiFileData[32];
Expand Down