Arduino MIDI File Parser
MidiFileParserOut.h
1 #pragma once
2 #include "Midi.h"
3 #include "MidiFileParser.h"
4 #include "MidiStreamOut.h"
5 
6 namespace midi {
7 
15 public:
17  MidiFileParserOut(MidiFileParser &file, MidiStreamOut &out) {
18  p_file = &file;
19  p_out = &out;
20  }
24  bool parse() {
25  // Parse midi
26  auto state = p_file->parseTimed();
27 
28  // Process Result
29  switch (state.status)
30  case MIDI_PARSER_TRACK_MIDI: {
31  switch (state.midi.status) {
32  case MIDI_STATUS_NOTE_OFF:
33  p_out->noteOff(state.midi.param1, state.midi.param2,
34  state.midi.channel);
35  break;
36  case MIDI_STATUS_NOTE_ON:
37  p_out->noteOn(state.midi.param1, state.midi.param2, state.midi.channel);
38  break;
39  case MIDI_STATUS_NOTE_AT:
40  p_out->polyPressure(state.midi.param1, state.midi.channel);
41  break;
42  case MIDI_STATUS_CC:
43  p_out->controlChange(state.midi.param1,
44  state.midi.param2, state.midi.channel);
45  break;
46  case MIDI_STATUS_PGM_CHANGE:
47  p_out->programChange(state.midi.param1, state.midi.channel);
48  break;
49  case MIDI_STATUS_CHANNEL_AT:
50  p_out->channelPressure(state.midi.param1, state.midi.channel);
51  break;
52  case MIDI_STATUS_PITCH_BEND:
53  p_out->pitchBend(state.midi.param1, state.midi.channel);
54  break;
55  default:
56  log("Unknown MIDI Event");
57  break;
58  }
59  active = true;
60  break;
61  case MIDI_PARSER_ERROR:
62  log("Error");
63  case MIDI_PARSER_EOB:
64  // stop processing loop
65  active = false;
66  break;
67  default:
68  break;
69  }
70  return active;
71  }
72 
73 protected:
74  MidiFileParser *p_file = nullptr;
75  MidiStreamOut *p_out = nullptr;
76  bool active = true;
77 
78  void log(const char *msg) {
79 #ifdef ARDUINO
80  Serial.println(msg);
81 #else
82  printf("%s\n", msg);
83 #endif
84  }
85 };
86 
87 } // namespace midi
A simple midi parser based on the following project https://github.com/abique/midi-parser.
Parse the MidiFileParser sending the output to MidiStreamOut. This class requires https://github....
Definition: MidiFileParserOut.h:14
bool parse()
Parses the midi message and forwards it to MidiStreamp_out-> Returns false when we are at the end.
Definition: MidiFileParserOut.h:24
MidiFileParserOut(MidiFileParser &file, MidiStreamOut &out)
Default Constructor.
Definition: MidiFileParserOut.h:17
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 & parseTimed()
Definition: MidiFileParser.h:112