Deluge Firmware 1.3.0
Build date: 2026.03.02
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 "OSLikeStuff/scheduler_api.h"
23#include "definitions_cxx.hpp"
24#include "io/midi/learned_midi.h"
25#include "playback/playback_handler.h"
26
27class MIDICable;
28class MIDIDrum;
29class MIDIInstrument;
30class MidiFollow;
31class PlaybackHandler;
32
35struct MIDISource {
36 void const* source_{nullptr};
37
38 MIDISource() = default;
39 ~MIDISource() = default;
40
41 MIDISource(MIDICable const* cable) : source_(cable) {};
42 MIDISource(MIDIInstrument const* instrument) : source_(instrument) {};
43 MIDISource(PlaybackHandler const* handler) : source_(handler) {};
44 MIDISource(MidiFollow const* follow) : source_(follow) {};
45 MIDISource(MIDIDrum const* drum) : source_(drum) {};
46 MIDISource(Sound const* sound) : source_(sound) {};
47
48 MIDISource(MIDICable const& cable) : source_(&cable) {};
49 MIDISource(MIDIInstrument const& instrument) : source_(&instrument) {};
50 MIDISource(MIDIDrum const& drum) : source_(&drum) {};
51 MIDISource(Sound const& sound) : source_(&sound) {};
52 MIDISource(MidiFollow const& follow) : source_(&follow) {};
53 MIDISource(PlaybackHandler const& handler) : source_(&handler) {};
54
55 MIDISource(MIDISource const& other) = default;
56 MIDISource(MIDISource&& other) = default;
57
58 MIDISource& operator=(MIDISource const& other) = default;
59 MIDISource& operator=(MIDISource&& other) = default;
60
61 bool operator==(MIDISource const& other) const { return source_ == other.source_; }
62};
63
64class MidiEngine {
65public:
66 MidiEngine();
67
68 void sendNote(MIDISource source, bool on, int32_t note, uint8_t velocity, uint8_t channel, int32_t filter);
69 void sendCC(MIDISource source, int32_t channel, int32_t cc, int32_t value, int32_t filter);
70 bool checkIncomingSerialMidi();
71 static void check_incoming_usb();
72 void checkIncomingUsbMidi();
73
74 void checkIncomingUsbSysex(uint8_t const* message, int32_t ip, int32_t d, int32_t cable);
75
76 void sendMidi(MIDISource source, MIDIMessage message, int32_t filter = kMIDIOutputFilterNoMPE, bool sendUSB = true);
77 void sendClock(MIDISource source, bool sendUSB = true, int32_t howMany = 1);
78 void sendStart(MIDISource source);
79 void sendStop(MIDISource source);
80 void sendPositionPointer(MIDISource source, uint16_t positionPointer);
81 void sendContinue(MIDISource source);
82
83 void flushMIDI();
84 void sendUsbMidi(MIDIMessage message, int32_t filter);
85 void sendSerialMidi(MIDIMessage message);
86
87 void sendPGMChange(MIDISource source, int32_t channel, int32_t pgm, int32_t filter);
88 void sendAllNotesOff(MIDISource source, int32_t channel, int32_t filter);
89 void sendBank(MIDISource source, int32_t channel, int32_t num, int32_t filter);
90 void sendSubBank(MIDISource source, int32_t channel, int32_t num, int32_t filter);
94 void sendPitchBend(MIDISource source, int32_t channel, uint16_t bend, int32_t filter);
95 void sendChannelAftertouch(MIDISource source, int32_t channel, uint8_t value, int32_t filter);
96 void sendPolyphonicAftertouch(MIDISource source, int32_t channel, uint8_t value, uint8_t noteCode, int32_t filter);
97 bool anythingInOutputBuffer();
98 void setupUSBHostReceiveTransfer(int32_t ip, int32_t midiDeviceNum);
99 void flushUSBMIDIOutput();
100
101 // If bit "16" (actually bit 4) is 1, this is a program change. (Wait, still?)
102 LearnedMIDI globalMIDICommands[kNumGlobalMIDICommands];
103
104 bool midiThru;
105 LearnedMIDI midiFollowChannelType[kNumMIDIFollowChannelTypes]; // A, B, C
106 MIDIFollowChannelType midiFollowFeedbackChannelType; // A, B, C, NONE
107 uint8_t midiFollowKitRootNote;
108 bool midiFollowDisplayParam;
109 MIDIFollowFeedbackAutomationMode midiFollowFeedbackAutomation;
110 bool midiFollowFeedbackFilter;
111 MIDITakeoverMode midiTakeover;
112 bool midiSelectKitRow;
113 TaskID routine_task_id;
114
115 // shared buffer for formatting sysex messages.
116 // Not safe for use in interrupts.
117 uint8_t sysex_fmt_buffer[1024];
118
128 void midiMessageReceived(MIDICable& cable, uint8_t statusType, uint8_t channel, uint8_t data1, uint8_t data2,
129 uint32_t* timer = nullptr);
130
136 void midiSysexReceived(MIDICable& cable, uint8_t* data, int32_t len);
137
138private:
139 uint8_t serialMidiInput[3];
140 uint8_t numSerialMidiInput;
141
142 bool currentlyReceivingSysExSerial;
143
144 using EventStackStorage = std::array<MIDISource, 16>;
147 EventStackStorage eventStack_;
149 EventStackStorage::iterator eventStackTop_;
150
151 int32_t getPotentialNumConnectedUSBMIDIDevices(int32_t ip);
152};
153
154uint32_t setupUSBMessage(MIDIMessage message);
155
156extern MidiEngine midiEngine;
157extern bool anythingInUSBOutputBuffer;
158
159extern "C" {
160#endif
161extern uint16_t g_usb_usbmode;
162
163void usbSendCompleteAsHost(int32_t ip); // used when deluge is in host mode
164void usbSendCompleteAsPeripheral(int32_t ip); // used in peripheral mode
165#ifdef __cplusplus
166}
167#endif
A MIDI cable connection. Stores all state specific to a given cable and its contained ports and chann...
Definition midi_device.h:96
Definition midi_drum.h:22
Definition midi_instrument.h:37
Definition midi_follow.h:39
Definition playback_handler.h:54
Definition message.h:30