wavelength 1.0.0
Loading...
Searching...
No Matches
midireader.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5
6#include "mid/midireceiver.h"
7#include "os/binstream.h"
8
18public:
20 struct Midi {
21 unsigned char mStatus;
22 unsigned char mData1;
23 unsigned char mData2;
24 };
25
27 using Compare = bool (*)(const Midi &left, const Midi &right);
28
30 struct ChunkName {
32 static constexpr int kLength = 4;
33
43
44 char mName[kLength];
45 };
46
48 struct VarLen {
56 explicit VarLen(BinStream &stream);
57
67
68 int mValue;
69 };
70
78
89 static bool CompareMidi(const Midi &left, const Midi &right);
90
99 MidiReader(const char *pszFile, MidiReceiver *pReceiver);
100
110 MidiReader(char *pBuffer, int nSize, MidiReceiver *pReceiver);
111
119
126 void ReadAll();
127
135 bool ReadTrack();
136
145 bool ReadSteps(int nSteps);
146
155 bool ReadFor(float fMs);
156
157private:
164 void Step();
165
173 void ReadHeader(BinStream &stream);
174
182 void BeginTrack(BinStream &stream);
183
193 void ReadEvent(BinStream &stream);
194
207 void
208 ReadChannelEvent(int nTick, unsigned char nStatus, unsigned char nData1, BinStream &stream);
209
219 void ReadSystemEvent(int nTick, unsigned char nStatus, BinStream &stream);
220
232 void ReadMeta(int nTick, unsigned char nType, BinStream &stream);
233
244 void AddMidi(int nTick, unsigned char nStatus, unsigned char nData1, unsigned char nData2);
245
252 void Flush();
253
254 std::unique_ptr<BinStream> mStream;
255 MidiReceiver *mReceiver;
256 int mState;
257 short mNumTracks;
258 short mDivision;
259 short mTargetDivision;
260 int mTrackIndex;
261 int mTrackTick;
262 unsigned char mRunningStatus;
263 std::vector<Midi> mPending;
264 int mPendingTick;
265 Compare mCompare;
266};
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
bool ReadFor(float fMs)
Take steps until a time has passed.
MidiReader(char *pBuffer, int nSize, MidiReceiver *pReceiver)
Read a MIDI file from memory.
bool ReadTrack()
Read up to the end of the next track.
bool ReadSteps(int nSteps)
Take a number of steps.
void ReadAll()
Read the whole file.
~MidiReader()
Close the stream.
bool(*)(const Midi &left, const Midi &right) Compare
Ordering of mPending.
Definition midireader.h:27
State
Values of mState.
Definition midireader.h:72
@ kStateHeader
Definition midireader.h:73
@ kStateEvent
Definition midireader.h:75
@ kStateDone
Definition midireader.h:76
@ kStateTrackStart
Definition midireader.h:74
static bool CompareMidi(const Midi &left, const Midi &right)
Order channel events so note offs come first, then controllers, programs, channel pressure,...
MidiReader(const char *pszFile, MidiReceiver *pReceiver)
Open a MIDI file.
Listener a MIDI file reader reports each track and event to.
Definition midireceiver.h:8
The four-character name of a chunk.
Definition midireader.h:30
char mName[kLength]
Definition midireader.h:44
BinStream & Read(BinStream &stream)
Read the name.
static constexpr int kLength
The characters of a name.
Definition midireader.h:32
One channel event waiting in mPending.
Definition midireader.h:20
unsigned char mData2
Definition midireader.h:23
unsigned char mStatus
Definition midireader.h:21
unsigned char mData1
Definition midireader.h:22
int mValue
Definition midireader.h:68
VarLen(BinStream &stream)
Read a quantity.
BinStream & Read(BinStream &stream)
Read the quantity.