VCV Rack API v2
Loading...
Searching...
No Matches
midi.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <set>
4
5#include <jansson.h>
6
7#include <common.hpp>
8#include <context.hpp>
9
10
11namespace rack {
13namespace midi {
14
15
16struct Message {
18 std::vector<uint8_t> bytes;
24 int64_t frame = -1;
25
26 Message() : bytes(3) {}
27
28 int getSize() const {
29 return bytes.size();
30 }
31 void setSize(int size) {
32 bytes.resize(size);
33 }
34
35 uint8_t getChannel() const {
36 if (bytes.size() < 1)
37 return 0;
38 return bytes[0] & 0xf;
39 }
40 void setChannel(uint8_t channel) {
41 if (bytes.size() < 1)
42 return;
43 bytes[0] = (bytes[0] & 0xf0) | (channel & 0xf);
44 }
45
46 uint8_t getStatus() const {
47 if (bytes.size() < 1)
48 return 0;
49 return bytes[0] >> 4;
50 }
51 void setStatus(uint8_t status) {
52 if (bytes.size() < 1)
53 return;
54 bytes[0] = (bytes[0] & 0xf) | (status << 4);
55 }
56
57 uint8_t getNote() const {
58 if (bytes.size() < 2)
59 return 0;
60 return bytes[1];
61 }
62 void setNote(uint8_t note) {
63 if (bytes.size() < 2)
64 return;
65 bytes[1] = note & 0x7f;
66 }
67
68 uint8_t getValue() const {
69 if (bytes.size() < 3)
70 return 0;
71 return bytes[2];
72 }
73 void setValue(uint8_t value) {
74 if (bytes.size() < 3)
75 return;
76 bytes[2] = value & 0x7f;
77 }
78
79 std::string toString() const;
80
81 int64_t getFrame() const {
82 return frame;
83 }
84
85 void setFrame(int64_t frame) {
86 this->frame = frame;
87 }
88};
89
91// Driver
93
94struct InputDevice;
95struct Input;
96struct OutputDevice;
97struct Output;
98
101struct Driver {
102 virtual ~Driver() {}
104 virtual std::string getName() {
105 return "";
106 }
108 virtual std::vector<int> getInputDeviceIds() {
109 return {};
110 }
113 return -1;
114 }
116 virtual std::string getInputDeviceName(int deviceId) {
117 return "";
118 }
122 virtual InputDevice* subscribeInput(int deviceId, Input* input) {
123 return NULL;
124 }
128 virtual void unsubscribeInput(int deviceId, Input* input) {}
129
130 // The following behave identically as the above methods except for outputs.
131
132 virtual std::vector<int> getOutputDeviceIds() {
133 return {};
134 }
136 return -1;
137 }
138 virtual std::string getOutputDeviceName(int deviceId) {
139 return "";
140 }
141 virtual OutputDevice* subscribeOutput(int deviceId, Output* output) {
142 return NULL;
143 }
144 virtual void unsubscribeOutput(int deviceId, Output* output) {}
145};
146
148// Device
150
157struct Device {
158 virtual ~Device() {}
159 virtual std::string getName() {
160 return "";
161 }
162};
163
165 std::set<Input*> subscribed;
167 void subscribe(Input* input);
169 void unsubscribe(Input* input);
171 void onMessage(const Message& message);
172};
173
175 std::set<Output*> subscribed;
177 void subscribe(Output* output);
179 void unsubscribe(Output* output);
181 virtual void sendMessage(const Message& message) {}
182};
183
185// Port
187
195struct Port {
202 int channel = -1;
203
204 // private
205 int driverId = -1;
206 int deviceId = -1;
208 Driver* driver = NULL;
209 Device* device = NULL;
211
213 virtual ~Port();
214
218
220 virtual std::vector<int> getDeviceIds() = 0;
221 virtual int getDefaultDeviceId() = 0;
223 virtual void setDeviceId(int deviceId) = 0;
224 virtual std::string getDeviceName(int deviceId) = 0;
225
226 virtual std::vector<int> getChannels() = 0;
229 std::string getChannelName(int channel);
230
231 json_t* toJson();
232 void fromJson(json_t* rootJ);
233};
234
235
236struct Input : Port {
239
242 void reset();
243
244 std::vector<int> getDeviceIds() override;
245 int getDefaultDeviceId() override;
246 void setDeviceId(int deviceId) override;
247 std::string getDeviceName(int deviceId) override;
248
249 std::vector<int> getChannels() override;
250
251 virtual void onMessage(const Message& message) {}
252};
253
254
258 struct Internal;
259 Internal* internal;
260
263 void onMessage(const Message& message) override;
267 bool tryPop(Message* messageOut, int64_t maxFrame);
268 size_t size();
269};
270
271
272struct Output : Port {
275
278 void reset();
279
280 std::vector<int> getDeviceIds() override;
281 int getDefaultDeviceId() override;
282 void setDeviceId(int deviceId) override;
283 std::string getDeviceName(int deviceId) override;
284
285 std::vector<int> getChannels() override;
286
287 void sendMessage(const Message& message);
288};
289
290
294void addDriver(int driverId, Driver* driver);
295std::vector<int> getDriverIds();
296Driver* getDriver(int driverId);
297
298
299} // namespace midi
300} // namespace rack
#define PRIVATE
Attribute for private functions not intended to be called by plugins.
Definition common.hpp:32
Driver * getDriver(int driverId)
PRIVATE void init()
std::vector< int > getDriverIds()
PRIVATE void destroy()
void addDriver(int driverId, Driver *driver)
Registers a new MIDI driver.
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Rack instance state.
Definition context.hpp:45
Definition Port.hpp:207
Definition Port.hpp:205
A single MIDI device of a driver API.
Definition midi.hpp:157
virtual ~Device()
Definition midi.hpp:158
virtual std::string getName()
Definition midi.hpp:159
Wraps a MIDI driver API containing any number of MIDI devices.
Definition midi.hpp:101
virtual int getDefaultOutputDeviceId()
Definition midi.hpp:135
virtual std::string getInputDeviceName(int deviceId)
Returns the name of an input device without obtaining it.
Definition midi.hpp:116
virtual void unsubscribeOutput(int deviceId, Output *output)
Definition midi.hpp:144
virtual void unsubscribeInput(int deviceId, Input *input)
Removes the give port as a reference holder of a device.
Definition midi.hpp:128
virtual std::string getName()
Returns the name of the driver.
Definition midi.hpp:104
virtual ~Driver()
Definition midi.hpp:102
virtual std::string getOutputDeviceName(int deviceId)
Definition midi.hpp:138
virtual std::vector< int > getOutputDeviceIds()
Definition midi.hpp:132
virtual InputDevice * subscribeInput(int deviceId, Input *input)
Adds the given port as a reference holder of a device and returns the it.
Definition midi.hpp:122
virtual int getDefaultInputDeviceId()
Returns the default device to use when the driver is selected, or -1 for none.
Definition midi.hpp:112
virtual std::vector< int > getInputDeviceIds()
Returns a list of all input device IDs that can be subscribed to.
Definition midi.hpp:108
virtual OutputDevice * subscribeOutput(int deviceId, Output *output)
Definition midi.hpp:141
Definition midi.hpp:164
std::set< Input * > subscribed
Definition midi.hpp:165
void subscribe(Input *input)
Not public.
void onMessage(const Message &message)
Called when a MIDI message is received from the device.
void unsubscribe(Input *input)
Not public.
An Input port that stores incoming MIDI messages and releases them when ready according to their fram...
Definition midi.hpp:257
bool tryPop(Message *messageOut, int64_t maxFrame)
Pops and returns the next message (by setting messageOut) if its frame timestamp is maxFrame or earli...
Internal * internal
Definition midi.hpp:259
void onMessage(const Message &message) override
Definition midi.hpp:236
InputDevice * inputDevice
Not owned.
Definition midi.hpp:238
void setDeviceId(int deviceId) override
int getDefaultDeviceId() override
std::vector< int > getDeviceIds() override
std::vector< int > getChannels() override
virtual void onMessage(const Message &message)
Definition midi.hpp:251
std::string getDeviceName(int deviceId) override
Definition midi.hpp:16
void setSize(int size)
Definition midi.hpp:31
void setFrame(int64_t frame)
Definition midi.hpp:85
std::string toString() const
void setValue(uint8_t value)
Definition midi.hpp:73
void setStatus(uint8_t status)
Definition midi.hpp:51
std::vector< uint8_t > bytes
Initialized to 3 empty bytes.
Definition midi.hpp:18
Message()
Definition midi.hpp:26
uint8_t getNote() const
Definition midi.hpp:57
void setNote(uint8_t note)
Definition midi.hpp:62
void setChannel(uint8_t channel)
Definition midi.hpp:40
int getSize() const
Definition midi.hpp:28
uint8_t getChannel() const
Definition midi.hpp:35
int64_t getFrame() const
Definition midi.hpp:81
uint8_t getStatus() const
Definition midi.hpp:46
int64_t frame
The Engine frame timestamp of the Message.
Definition midi.hpp:24
uint8_t getValue() const
Definition midi.hpp:68
Definition midi.hpp:174
void unsubscribe(Output *output)
Not public.
virtual void sendMessage(const Message &message)
Sends a MIDI message to the device.
Definition midi.hpp:181
std::set< Output * > subscribed
Definition midi.hpp:175
void subscribe(Output *output)
Not public.
Definition midi.hpp:272
void setDeviceId(int deviceId) override
OutputDevice * outputDevice
Not owned.
Definition midi.hpp:274
void sendMessage(const Message &message)
std::vector< int > getDeviceIds() override
std::vector< int > getChannels() override
std::string getDeviceName(int deviceId) override
int getDefaultDeviceId() override
A handle to a Device, typically owned by modules to have shared access to a single Device.
Definition midi.hpp:195
std::string getChannelName(int channel)
void setChannel(int channel)
virtual std::string getDeviceName(int deviceId)=0
json_t * toJson()
int deviceId
Definition midi.hpp:206
virtual std::vector< int > getChannels()=0
void fromJson(json_t *rootJ)
Driver * getDriver()
virtual std::vector< int > getDeviceIds()=0
Driver * driver
Not owned.
Definition midi.hpp:208
virtual void setDeviceId(int deviceId)=0
int driverId
Definition midi.hpp:205
int channel
For MIDI output, the channel to automatically set outbound messages.
Definition midi.hpp:202
void setDriverId(int driverId)
Device * getDevice()
virtual int getDefaultDeviceId()=0
Context * context
Definition midi.hpp:210
Device * device
Definition midi.hpp:209