Control Surface  1.1.1
MIDI Control Surface library for Arduino
MIDI_Parser.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 
7 #include <Def/Def.hpp>
8 #include <Settings/SettingsWrapper.hpp>
9 
10 #ifndef ARDUINO
11 #include <vector>
12 #endif
13 
15 
16 const uint8_t NOTE_OFF = 0x80;
17 const uint8_t NOTE_ON = 0x90;
18 const uint8_t KEY_PRESSURE = 0xA0;
19 const uint8_t CC = 0xB0, CONTROL_CHANGE = CC;
20 const uint8_t PROGRAM_CHANGE = 0xC0;
21 const uint8_t CHANNEL_PRESSURE = 0xD0;
22 const uint8_t PITCH_BEND = 0xE0;
23 
24 const uint8_t SysExStart = 0xF0;
25 const uint8_t SysExEnd = 0xF7;
26 
27 const uint8_t TuneRequest = 0xF6;
28 
29 enum MIDI_read_t : uint8_t {
33 
34  /* System Real-Time messages */
37  START_MESSAGE = 0xFA,
39  STOP_MESSAGE = 0xFC,
43 };
44 
45 // -------------------------------------------------------------------------- //
46 
48  uint8_t header;
49  uint8_t data1;
50  uint8_t data2;
51 
52  uint8_t CN = 0;
53 
54  bool operator==(const ChannelMessage &other) const {
55  return this->header == other.header && this->data1 == other.data1 &&
56  this->data2 == other.data2 && this->CN == other.CN;
57  }
58 
59  bool operator!=(const ChannelMessage &other) const {
60  return !(*this == other);
61  }
62 };
63 
64 struct SysExMessage {
65  SysExMessage() : data(nullptr), length(0), CN(0) {}
66  SysExMessage(const uint8_t *data, size_t length, uint8_t CN = 0)
67  : data(data), length(length), CN(CN) {}
68 #ifndef ARDUINO
69  SysExMessage(const std::vector<uint8_t> &vec, uint8_t CN = 0)
70  : data(vec.data()), length(vec.size()), CN(CN) {}
71 #endif
72  const uint8_t *data;
73  uint8_t length;
74  uint8_t CN;
75 };
76 
78  uint8_t message;
79  uint8_t CN;
80 };
81 
82 // -------------------------------------------------------------------------- //
83 
84 class MIDI_Parser {
85  public:
88 #if !IGNORE_SYSEX
89 
90  virtual SysExMessage getSysEx() const = 0;
91 #else
92  SysExMessage getSysEx() const { return {nullptr, 0, 0}; }
93 #endif
94 
95  const uint8_t *getSysExBuffer() const { return getSysEx().data; }
97  size_t getSysExLength() const { return getSysEx().length; }
99  virtual uint8_t getCN() const { return 0; };
100 
101  protected:
103 
104  public:
106  static bool isStatus(uint8_t data);
108  static bool isData(uint8_t data);
109 };
110 
MIDI_Parser::getChannelMessage
ChannelMessage getChannelMessage()
Get the latest MIDI channel message.
Definition: MIDI_Parser.cpp:17
ChannelMessage::operator==
bool operator==(const ChannelMessage &other) const
Definition: MIDI_Parser.hpp:54
CHANNEL_PRESSURE
const uint8_t CHANNEL_PRESSURE
Definition: MIDI_Parser.hpp:21
SysExMessage
Definition: MIDI_Parser.hpp:64
MIDI_Parser::isStatus
static bool isStatus(uint8_t data)
Check if the given byte is a MIDI header byte.
Definition: MIDI_Parser.cpp:11
SysExMessage::data
const uint8_t * data
Definition: MIDI_Parser.hpp:72
CONTROL_CHANGE
const uint8_t CONTROL_CHANGE
Definition: MIDI_Parser.hpp:19
MIDI_Parser::isData
static bool isData(uint8_t data)
Check if the given byte is a MIDI data byte.
Definition: MIDI_Parser.cpp:15
MIDI_read_t
MIDI_read_t
Definition: MIDI_Parser.hpp:29
ChannelMessage::CN
uint8_t CN
Definition: MIDI_Parser.hpp:52
MIDI_Parser
Definition: MIDI_Parser.hpp:84
Def.hpp
CHANNEL_MESSAGE
Definition: MIDI_Parser.hpp:31
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
ChannelMessage::header
uint8_t header
Definition: MIDI_Parser.hpp:48
ChannelMessage::data2
uint8_t data2
Definition: MIDI_Parser.hpp:50
TIMING_CLOCK_MESSAGE
Definition: MIDI_Parser.hpp:35
SysExMessage::SysExMessage
SysExMessage()
Definition: MIDI_Parser.hpp:65
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
START_MESSAGE
Definition: MIDI_Parser.hpp:37
TuneRequest
const uint8_t TuneRequest
Definition: MIDI_Parser.hpp:27
SysExEnd
const uint8_t SysExEnd
Definition: MIDI_Parser.hpp:25
STOP_MESSAGE
Definition: MIDI_Parser.hpp:39
RealTimeMessage
Definition: MIDI_Parser.hpp:77
NO_MESSAGE
Definition: MIDI_Parser.hpp:30
NOTE_ON
const uint8_t NOTE_ON
Definition: MIDI_Parser.hpp:17
PROGRAM_CHANGE
const uint8_t PROGRAM_CHANGE
Definition: MIDI_Parser.hpp:20
MIDI_Parser::midimsg
ChannelMessage midimsg
Definition: MIDI_Parser.hpp:102
PITCH_BEND
const uint8_t PITCH_BEND
Definition: MIDI_Parser.hpp:22
ChannelMessage::data1
uint8_t data1
Definition: MIDI_Parser.hpp:49
SysExMessage::CN
uint8_t CN
Definition: MIDI_Parser.hpp:74
ACTIVE_SENSING_MESSAGE
Definition: MIDI_Parser.hpp:41
UNDEFINED_REALTIME_MESSAGE_1
Definition: MIDI_Parser.hpp:36
NOTE_OFF
const uint8_t NOTE_OFF
Definition: MIDI_Parser.hpp:16
MIDI_Parser::getCN
virtual uint8_t getCN() const
Get the cable number of the latests MIDI message.
Definition: MIDI_Parser.hpp:99
SYSEX_MESSAGE
Definition: MIDI_Parser.hpp:32
CONTINUE_MESSAGE
Definition: MIDI_Parser.hpp:38
MIDI_Parser::getSysExLength
size_t getSysExLength() const
Get the length of the SysEx message.
Definition: MIDI_Parser.hpp:97
ChannelMessage::operator!=
bool operator!=(const ChannelMessage &other) const
Definition: MIDI_Parser.hpp:59
CC
const uint8_t CC
Definition: MIDI_Parser.hpp:19
ChannelMessage
Definition: MIDI_Parser.hpp:47
RealTimeMessage::CN
uint8_t CN
Definition: MIDI_Parser.hpp:79
MIDI_Parser::getSysEx
virtual SysExMessage getSysEx() const =0
Get the latest SysEx message.
SysExMessage::length
uint8_t length
Definition: MIDI_Parser.hpp:73
SysExMessage::SysExMessage
SysExMessage(const uint8_t *data, size_t length, uint8_t CN=0)
Definition: MIDI_Parser.hpp:66
RealTimeMessage::message
uint8_t message
Definition: MIDI_Parser.hpp:78
RESET_MESSAGE
Definition: MIDI_Parser.hpp:42
UNDEFINED_REALTIME_MESSAGE_2
Definition: MIDI_Parser.hpp:40
SysExStart
const uint8_t SysExStart
Definition: MIDI_Parser.hpp:24
MIDI_Parser::getSysExBuffer
const uint8_t * getSysExBuffer() const
Get the pointer to the SysEx data.
Definition: MIDI_Parser.hpp:95
KEY_PRESSURE
const uint8_t KEY_PRESSURE
Definition: MIDI_Parser.hpp:18