10 enum ProcessingStatus { Inserting, Parsing, EndOfData };
23 tracks[track].push_back(event);
33 process_status = EndOfData;
34 for (
int track = 0; track < tracks.size(); track++) {
35 if (iterators.size() < track + 1) {
36 iterators.push_back(tracks[track].
begin());
38 iterators[track] = tracks[track].begin();
41 if (iterators[0] != tracks[0].
end()) {
42 process_status = Parsing;
49 if (process_status == Inserting) {
54 process_status = EndOfData;
56 uint64_t min_time = 0xFFFFFFFFFFFFFFFF;
58 if (iterators[j] != tracks[j].
end() &&
59 (*iterators[j]).time_ms < min_time) {
61 process_status = Parsing;
65 if (process_status == EndOfData) {
79 result += tracks[j].size();
90 auto &events = tracks[track];
93 return events.back().time_ms;
103 ProcessingStatus
status() {
return process_status; }
109 std::vector<std::list<midi_time_event>> tracks;
110 std::vector<std::list<midi_time_event>::iterator> iterators;
112 ProcessingStatus process_status = Inserting;
114 void addTrack(
int track) {
115 if (track >= tracks.size()) {
116 std::list<midi_time_event> event_list;
117 tracks.push_back(event_list);
133 bool begin(
bool log =
true,
int bufferSize = MIDI_BUFFER_SIZE) {
135 return parser.
begin(log, bufferSize);
140 is_iterators_available =
false;
141 start_time = millis();
142 p_current_state =
nullptr;
149 virtual size_t write(
const uint8_t *data,
size_t len) {
150 int result = parser.write(data, len);
178 parser_state.status = MIDI_PARSER_TRACK_MIDI;
179 parser_state.vtime_ms =
event.time_ms;
180 parser_state.midi.channel =
event.channel;
181 parser_state.midi.status =
event.status;
182 parser_state.midi.param1 =
event.param1;
183 parser_state.midi.param2 =
event.param2;
190 if (p_current_state ==
nullptr) {
198 p_current_state =
nullptr;
208 if ((millis() - start_time) < p_current_state->
time_ms) {
213 parser_state.status = MIDI_PARSER_TRACK_MIDI;
214 parser_state.vtime_ms = p_current_state->
time_ms;
215 parser_state.midi.channel = p_current_state->channel;
216 parser_state.midi.status = p_current_state->status;
217 parser_state.midi.param1 = p_current_state->param1;
218 parser_state.midi.param2 = p_current_state->param2;
219 p_current_state =
nullptr;
224 operator bool() {
return parser || data.
status() == Parsing; }
237 void endWrite() { parser.setState(MIDI_PARSER_EOB); }
257 midi_parser_state eob{MIDI_PARSER_EOB};
258 midi_time_event *p_current_state =
nullptr;
259 uint64_t start_time = 0l;
260 bool is_iterators_available =
false;
262 void parse(
int limit) {
263 while (parser.parser_state.in.available() > limit) {
264 midi_parser_state &state = parser.
parse();
271 int track = state.track.number;
274 event.status = state.midi.status;
275 event.channel = state.midi.channel;
276 event.param1 = state.midi.param1;
277 event.param2 = state.midi.param2;
278 event.time_ms = state.timeInMs() + data.
lastTime(track);
279 data.
add(track, event);
286 if (!is_iterators_available) {
290 is_iterators_available =
true;
A simple midi parser based on the following project https://github.com/abique/midi-parser.
Midi file parser which stores the data in RAM before making them available for parsing....
Definition: MidiFileParserMultiTrack.h:130
size_t size()
Returns the number of available midi events (after writing)
Definition: MidiFileParserMultiTrack.h:230
const char * midi_status_name(int status)
Provides the string description for the midi_status value.
Definition: MidiFileParserMultiTrack.h:240
bool isEmpty()
Returns true if there are no midi events.
Definition: MidiFileParserMultiTrack.h:227
size_t write(uint8_t c) override
Single character write.
Definition: MidiFileParserMultiTrack.h:146
int availableForWrite()
Max number of bytes that we can write.
Definition: MidiFileParserMultiTrack.h:157
int trackCount()
Returns the number of recorded tracks.
Definition: MidiFileParserMultiTrack.h:160
midi_parser_state & parse()
Provide the next midi element.
Definition: MidiFileParserMultiTrack.h:163
midi_time_event & parseTimeEvent()
Provide the next midi element using the iterators.
Definition: MidiFileParserMultiTrack.h:284
const char * midi_file_format_name(int fmt)
Provides the string description for the file format.
Definition: MidiFileParserMultiTrack.h:245
midi_parser_state & parseTimed()
Provide the next midi element considering the times.
Definition: MidiFileParserMultiTrack.h:188
void addEvent(midi_parser_state &state)
Adds an new midi event with the corresponding time.
Definition: MidiFileParserMultiTrack.h:270
void endWrite()
Definition: MidiFileParserMultiTrack.h:237
void end()
Ends the processing: releases all memory.
Definition: MidiFileParserMultiTrack.h:233
const char * midi_meta_name(int type)
Provides the string description for the midi_meta value.
Definition: MidiFileParserMultiTrack.h:250
virtual size_t write(const uint8_t *data, size_t len)
Feed/Provide the midi data to the parser.
Definition: MidiFileParserMultiTrack.h:149
void restart()
Call this method to restart the parsing with the available data.
Definition: MidiFileParserMultiTrack.h:139
bool begin(bool log=true, int bufferSize=MIDI_BUFFER_SIZE)
Initializes & starts the processing.
Definition: MidiFileParserMultiTrack.h:133
Midi File parser. Provide the data via write: You should try to keep the buffer as full as possible w...
Definition: MidiFileParser.h:61
midi_parser_state & parse()
Parse data in order to provide the next midi element.
Definition: MidiFileParser.h:92
int availableForWrite()
Max number of bytes that we can write.
Definition: MidiFileParser.h:89
const char * midi_meta_name(int type)
Provides the string description for the midi_meta value.
Definition: MidiFileParser.h:191
const char * midi_status_name(int status)
Provides the string description for the midi_status value.
Definition: MidiFileParser.h:153
const char * midi_file_format_name(int fmt)
Provides the string description for the file format.
Definition: MidiFileParser.h:176
bool begin(bool log=true, int bufferSize=MIDI_BUFFER_SIZE)
Initializes & starts the processing.
Definition: MidiFileParser.h:64
Definition: MidiFileParser.h:46
Definition: MidiFileParserMultiTrack.h:15
void end()
Releases all data.
Definition: MidiFileParserMultiTrack.h:97
void add(int track, midi_time_event event)
Adds an midi event to the indicated track.
Definition: MidiFileParserMultiTrack.h:18
ProcessingStatus status()
Provides the current processing status.
Definition: MidiFileParserMultiTrack.h:103
size_t size()
Count number for midi events.
Definition: MidiFileParserMultiTrack.h:76
void begin()
Moves the iterators to the first element.
Definition: MidiFileParserMultiTrack.h:31
bool isEmpty()
Returns true if there are no midi events.
Definition: MidiFileParserMultiTrack.h:85
midi_time_event & getNoDataEvent()
Provides the reference to the nodata time event which signals that we provided no data.
Definition: MidiFileParserMultiTrack.h:106
midi_time_event & nextEvent()
Provides the next event.
Definition: MidiFileParserMultiTrack.h:48
int trackCount()
Provides the number of tracks.
Definition: MidiFileParserMultiTrack.h:28
uint64_t lastTime(int track)
Provides the last recorded time for the indicated track.
Definition: MidiFileParserMultiTrack.h:88
MIDI Parser State Information.
Definition: MidiFileParserState.h:119
Definition: MidiFileParserState.h:90
uint64_t time_ms
cummulated time in milliseconds
Definition: MidiFileParserState.h:92