Deluge Firmware 1.3.0
Build date: 2025.04.16
Loading...
Searching...
No Matches
midi_engine.h
1/*
2 * Copyright © 2014-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#ifdef __cplusplus
21
22#include "definitions_cxx.hpp"
23#include "io/midi/learned_midi.h"
24#include "playback/playback_handler.h"
25
26class MIDICable;
27class MIDIDrum;
28class MIDIInstrument;
29class MidiFollow;
30class PlaybackHandler;
31
34struct MIDISource {
35 void const* source_{nullptr};
36
37 MIDISource() = default;
38 ~MIDISource() = default;
39
40 MIDISource(MIDICable const* cable) : source_(cable) {};
41 MIDISource(MIDIInstrument const* instrument) : source_(instrument) {};
42 MIDISource(PlaybackHandler const* handler) : source_(handler) {};
43 MIDISource(MidiFollow const* follow) : source_(follow) {};
44 MIDISource(MIDIDrum const* drum) : source_(drum) {};
45 MIDISource(Sound const* sound) : source_(sound) {};
46
47 MIDISource(MIDICable const& cable) : source_(&cable) {};
48 MIDISource(MIDIInstrument const& instrument) : source_(&instrument) {};
49 MIDISource(MIDIDrum const& drum) : source_(&drum) {};
50 MIDISource(Sound const& sound) : source_(&sound) {};
51 MIDISource(MidiFollow const& follow) : source_(&follow) {};
52 MIDISource(PlaybackHandler const& handler) : source_(&handler) {};
53
54 MIDISource(MIDISource const& other) = default;
55 MIDISource(MIDISource&& other) = default;
56
57 MIDISource& operator=(MIDISource const& other) = default;
58 MIDISource& operator=(MIDISource&& other) = default;
59
60 bool operator==(MIDISource const& other) const { return source_ == other.source_; }
61};
62
63class MidiEngine {
64public:
65 MidiEngine();
66
67 void sendNote(MIDISource source, bool on, int32_t note, uint8_t velocity, uint8_t channel, int32_t filter);
68 void sendCC(MIDISource source, int32_t channel, int32_t cc, int32_t value, int32_t filter);
69 bool checkIncomingSerialMidi();
70 void checkIncomingUsbMidi();
71
72 void checkIncomingUsbSysex(uint8_t const* message, int32_t ip, int32_t d, int32_t cable);
73
74 void sendMidi(MIDISource source, MIDIMessage message, int32_t filter = kMIDIOutputFilterNoMPE, bool sendUSB = true);
75 void sendClock(MIDISource source, bool sendUSB = true, int32_t howMany = 1);
76 void sendStart(MIDISource source);
77 void sendStop(MIDISource source);
78 void sendPositionPointer(MIDISource source, uint16_t positionPointer);
79 void sendContinue(MIDISource source);
80
81 void flushMIDI();
82 void sendUsbMidi(MIDIMessage message, int32_t filter);
83 void sendSerialMidi(MIDIMessage message);
84
85 void sendPGMChange(MIDISource source, int32_t channel, int32_t pgm, int32_t filter);
86 void sendAllNotesOff(MIDISource source, int32_t channel, int32_t filter);
87 void sendBank(MIDISource source, int32_t channel, int32_t num, int32_t filter);
88 void sendSubBank(MIDISource source, int32_t channel, int32_t num, int32_t filter);
92 void sendPitchBend(MIDISource source, int32_t channel, uint16_t bend, int32_t filter);
93 void sendChannelAftertouch(MIDISource source, int32_t channel, uint8_t value, int32_t filter);
94 void sendPolyphonicAftertouch(MIDISource source, int32_t channel, uint8_t value, uint8_t noteCode, int32_t filter);
95 bool anythingInOutputBuffer();
96 void setupUSBHostReceiveTransfer(int32_t ip, int32_t midiDeviceNum);
97 void flushUSBMIDIOutput();
98
99 // If bit "16" (actually bit 4) is 1, this is a program change. (Wait, still?)
100 LearnedMIDI globalMIDICommands[kNumGlobalMIDICommands];
101
102 bool midiThru;
103 LearnedMIDI midiFollowChannelType[kNumMIDIFollowChannelTypes]; // A, B, C
104 MIDIFollowChannelType midiFollowFeedbackChannelType; // A, B, C, NONE
105 uint8_t midiFollowKitRootNote;
106 bool midiFollowDisplayParam;
107 MIDIFollowFeedbackAutomationMode midiFollowFeedbackAutomation;
108 bool midiFollowFeedbackFilter;
109 MIDITakeoverMode midiTakeover;
110 bool midiSelectKitRow;
111
112 // shared buffer for formatting sysex messages.
113 // Not safe for use in interrupts.
114 uint8_t sysex_fmt_buffer[1024];
115
125 void midiMessageReceived(MIDICable& cable, uint8_t statusType, uint8_t channel, uint8_t data1, uint8_t data2,
126 uint32_t* timer = nullptr);
127
133 void midiSysexReceived(MIDICable& cable, uint8_t* data, int32_t len);
134
135private:
136 uint8_t serialMidiInput[3];
137 uint8_t numSerialMidiInput;
138
139 bool currentlyReceivingSysExSerial;
140
141 using EventStackStorage = std::array<MIDISource, 16>;
144 EventStackStorage eventStack_;
146 EventStackStorage::iterator eventStackTop_;
147
148 int32_t getPotentialNumConnectedUSBMIDIDevices(int32_t ip);
149};
150
151uint32_t setupUSBMessage(MIDIMessage message);
152
153extern MidiEngine midiEngine;
154extern bool anythingInUSBOutputBuffer;
155
156extern "C" {
157#endif
158extern uint16_t g_usb_usbmode;
159
160void usbSendCompleteAsHost(int32_t ip); // used when deluge is in host mode
161void usbSendCompleteAsPeripheral(int32_t ip); // used in peripheral mode
162#ifdef __cplusplus
163}
164#endif
A MIDI cable connection. Stores all state specific to a given cable and its contained ports and chann...
Definition midi_device.h:94
Definition midi_drum.h:22
Definition midi_instrument.h:37
Definition midi_follow.h:39
Definition playback_handler.h:54
Definition message.h:30