Control Surface  1.2.0
MIDI Control Surface library for Arduino
FortySevenEffects.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <AH/Error/Error.hpp>
5 
7 
9 #include <MIDI.h>
11 
13 
20  private:
21  template <class MidiInterface>
23 
25  template <class MidiInterface>
26  void updateChannelMessage(const MidiInterface &interface) {
27  uint8_t channel = interface.getChannel() - 1;
28  this->midimsg.header = interface.getType() | channel;
29  this->midimsg.data1 = interface.getData1();
30  this->midimsg.data2 = interface.getData2();
31  this->midimsg.CN = 0;
32  }
33 
35  template <class MidiInterface>
36  void updateSysExMessage(const MidiInterface &interface) {
37  this->sysex.data = interface.getSysExArray();
38  this->sysex.length = interface.getSysExArrayLength();
39  this->sysex.CN = 0;
40  }
41 
43  template <class MidiInterface>
44  void updateRealTimeMessage(const MidiInterface &interface) {
45  this->rtmsg.message = interface.getType();
46  this->rtmsg.CN = 0;
47  }
48 
50  SysExMessage sysex = {nullptr, 0, 0};
51 
52  public:
53  SysExMessage getSysExMessage() const override { return sysex; }
54 };
55 
76 template <class MidiInterface>
78  public:
81  midi(std::forward<MidiInterface>(midi)) {}
82 
83  void begin() override { midi.begin(MIDI_CHANNEL_OMNI); }
84 
85  MIDIReadEvent read() override {
86  if (!midi.read()) // Update the MIDI input and check if there's
87  return MIDIReadEvent::NO_MESSAGE; // a new message available
88  auto type = midi.getType();
89  if (midi.isChannelMessage(type)) { // Channel
91  return MIDIReadEvent::CHANNEL_MESSAGE;
92  }
93  if (type == uint8_t(MIDIMessageType::SYSEX_START)) { // SysEx
95  return MIDIReadEvent::SYSEX_MESSAGE;
96  }
97  if (type >= uint8_t(MIDIMessageType::TIMING_CLOCK)) { // Real-Time
100  }
101  return MIDIReadEvent::NO_MESSAGE;
102  }
103 
104  protected:
105  void sendImpl(uint8_t header, uint8_t d1, uint8_t d2, uint8_t cn) override {
106  uint8_t m = header & 0xF0;
107  uint8_t c = header & 0x0F;
108  // channel is zero-based in Control Surface, one-based in MIDI 47 Fx
109  midi.send(static_cast<MIDI_NAMESPACE::MidiType>(m), d1, d2, c + 1);
110  (void)cn;
111  }
112  void sendImpl(uint8_t header, uint8_t d1, uint8_t cn) override {
113  uint8_t m = header & 0xF0;
114  uint8_t c = header & 0x0F;
115  // channel is zero-based in Control Surface, one-based in MIDI 47 Fx
116  midi.send(static_cast<MIDI_NAMESPACE::MidiType>(m), d1, 0, c + 1);
117  // MIDI 47 Fx checks message type to handle 2-byte messages separately
118  (void)cn;
119  }
120  void sendImpl(const uint8_t *data, size_t length, uint8_t cn) {
121  midi.sendSysEx(length, data, true);
122  // true indicates that the array contains the SysEx start and stop bytes
123  (void)cn;
124  }
125  void sendImpl(uint8_t rt, uint8_t cn) override {
126  midi.sendRealTime(static_cast<MIDI_NAMESPACE::MidiType>(rt));
127  (void)cn;
128  }
129 
130  private:
132  MidiInterface midi;
133 };
134 
136 
FortySevenEffectsMIDI_Interface::sendImpl
void sendImpl(uint8_t header, uint8_t d1, uint8_t cn) override
Low-level function for sending a 2-byte MIDI message.
Definition: FortySevenEffects.hpp:112
FortySevenEffectsMIDI_Parser
Wrapper class for the FortySevenEffects MIDI parser.
Definition: FortySevenEffects.hpp:19
FortySevenEffectsMIDI_Interface::sendImpl
void sendImpl(uint8_t header, uint8_t d1, uint8_t d2, uint8_t cn) override
Low-level function for sending a 3-byte MIDI message.
Definition: FortySevenEffects.hpp:105
FortySevenEffectsMIDI_Parser::updateRealTimeMessage
void updateRealTimeMessage(const MidiInterface &interface)
Get the latest real-time message from the given MIDI interface.
Definition: FortySevenEffects.hpp:44
SysExMessage
Definition: MIDI_MessageTypes.hpp:138
FortySevenEffectsMIDI_Interface
Class that wraps the FortySevenEffects MIDI library.
Definition: FortySevenEffects.hpp:77
FortySevenEffectsMIDI_Parser::updateChannelMessage
void updateChannelMessage(const MidiInterface &interface)
Get the latest channel message from the given MIDI interface.
Definition: FortySevenEffects.hpp:26
Error.hpp
Parsing_MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:232
FortySevenEffectsMIDI_Interface::read
MIDIReadEvent read() override
Try reading and parsing a single incoming MIDI message.
Definition: FortySevenEffects.hpp:85
MIDI_Parser
Definition: MIDI_Parser.hpp:22
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
FortySevenEffectsMIDI_Interface::sendImpl
void sendImpl(const uint8_t *data, size_t length, uint8_t cn)
Low-level function for sending a system exclusive MIDI message.
Definition: FortySevenEffects.hpp:120
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
FortySevenEffectsMIDI_Parser::updateSysExMessage
void updateSysExMessage(const MidiInterface &interface)
Get the latest system exclusive message from the given MIDI interface.
Definition: FortySevenEffects.hpp:36
FortySevenEffectsMIDI_Interface::parser
FortySevenEffectsMIDI_Parser parser
Definition: FortySevenEffects.hpp:131
FortySevenEffectsMIDI_Parser::getSysExMessage
SysExMessage getSysExMessage() const override
Get the latest SysEx message.
Definition: FortySevenEffects.hpp:53
FortySevenEffectsMIDI_Interface::midi
MidiInterface midi
Definition: FortySevenEffects.hpp:132
FortySevenEffectsMIDI_Interface::begin
void begin() override
Initialize the MIDI Interface.
Definition: FortySevenEffects.hpp:83
MIDIReadEvent::NO_MESSAGE
@ NO_MESSAGE
No new incoming methods.
FortySevenEffectsMIDI_Interface::sendImpl
void sendImpl(uint8_t rt, uint8_t cn) override
Low-level function for sending a single-byte MIDI message.
Definition: FortySevenEffects.hpp:125
AH_DIAGNOSTIC_EXTERNAL_HEADER
#define AH_DIAGNOSTIC_EXTERNAL_HEADER()
Definition: Warnings.hpp:37
FortySevenEffectsMIDI_Interface::FortySevenEffectsMIDI_Interface
FortySevenEffectsMIDI_Interface(MidiInterface &&midi)
Definition: FortySevenEffects.hpp:79
std
Definition: vector.cpp:5
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
MIDIReadEvent
MIDIReadEvent
Result of the MIDI interface read methods.
Definition: MIDI_Parser.hpp:15
MIDI_Interface.hpp