|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #pragma once
- #include <vector>
- #include <queue>
- #include <set>
-
- #include <jansson.h>
-
- #include <common.hpp>
- #include <context.hpp>
-
-
- namespace rack {
-
-
- /** MIDI driver
- */
- namespace midi {
-
-
- struct Message {
- /** Initialized to 3 empty bytes. */
- std::vector<uint8_t> bytes;
- /** Timestamp of MIDI message in nanoseconds. Negative if not set. */
- int64_t timestamp = -1;
-
- Message() : bytes(3) {}
-
- int getSize() const {
- return bytes.size();
- }
- void setSize(int size) {
- bytes.resize(size);
- }
-
- uint8_t getChannel() const {
- if (bytes.size() < 1)
- return 0;
- return bytes[0] & 0xf;
- }
- void setChannel(uint8_t channel) {
- if (bytes.size() < 1)
- return;
- bytes[0] = (bytes[0] & 0xf0) | (channel & 0xf);
- }
-
- uint8_t getStatus() const {
- if (bytes.size() < 1)
- return 0;
- return bytes[0] >> 4;
- }
- void setStatus(uint8_t status) {
- if (bytes.size() < 1)
- return;
- bytes[0] = (bytes[0] & 0xf) | (status << 4);
- }
-
- uint8_t getNote() const {
- if (bytes.size() < 2)
- return 0;
- return bytes[1];
- }
- void setNote(uint8_t note) {
- if (bytes.size() < 2)
- return;
- bytes[1] = note & 0x7f;
- }
-
- uint8_t getValue() const {
- if (bytes.size() < 3)
- return 0;
- return bytes[2];
- }
- void setValue(uint8_t value) {
- if (bytes.size() < 3)
- return;
- bytes[2] = value & 0x7f;
- }
- };
-
- ////////////////////
- // Driver
- ////////////////////
-
- struct InputDevice;
- struct Input;
- struct OutputDevice;
- struct Output;
-
- struct Driver {
- virtual ~Driver() {}
- virtual std::string getName() {
- return "";
- }
- virtual std::vector<int> getInputDeviceIds() {
- return {};
- }
- virtual std::string getInputDeviceName(int deviceId) {
- return "";
- }
- virtual InputDevice* subscribeInput(int deviceId, Input* input) {
- return NULL;
- }
- virtual void unsubscribeInput(int deviceId, Input* input) {}
-
- virtual std::vector<int> getOutputDeviceIds() {
- return {};
- }
- virtual std::string getOutputDeviceName(int deviceId) {
- return "";
- }
- virtual OutputDevice* subscribeOutput(int deviceId, Output* output) {
- return NULL;
- }
- virtual void unsubscribeOutput(int deviceId, Output* output) {}
- };
-
- ////////////////////
- // Device
- ////////////////////
-
- struct Device {
- virtual ~Device() {}
- virtual std::string getName() {
- return "";
- }
- };
-
- struct InputDevice : Device {
- std::set<Input*> subscribed;
- void subscribe(Input* input);
- void unsubscribe(Input* input);
- void onMessage(const Message &message);
- };
-
- struct OutputDevice : Device {
- std::set<Output*> subscribed;
- void subscribe(Output* input);
- void unsubscribe(Output* input);
- virtual void sendMessage(const Message &message) {}
- };
-
- ////////////////////
- // Port
- ////////////////////
-
- struct Port {
- /* For MIDI output, the channel to output messages.
- For MIDI input, the channel to filter.
- Set to -1 to allow all MIDI channels (for input).
- Zero indexed.
- */
- int channel = -1;
-
- // private
- int driverId = -1;
- int deviceId = -1;
- /** Not owned */
- Driver* driver = NULL;
- Device* device = NULL;
- Context* context;
-
- Port();
- virtual ~Port();
-
- Driver* getDriver() {
- return driver;
- }
- int getDriverId() {
- return driverId;
- }
- void setDriverId(int driverId);
-
- Device* getDevice() {
- return device;
- }
- virtual std::vector<int> getDeviceIds() = 0;
- int getDeviceId() {
- return deviceId;
- }
- virtual void setDeviceId(int deviceId) = 0;
- virtual std::string getDeviceName(int deviceId) = 0;
-
- virtual std::vector<int> getChannels() = 0;
- int getChannel() {
- return channel;
- }
- void setChannel(int channel);
- std::string getChannelName(int channel);
-
- json_t* toJson();
- void fromJson(json_t* rootJ);
- };
-
-
- struct Input : Port {
- /** Not owned */
- InputDevice* inputDevice = NULL;
-
- Input();
- ~Input();
- void reset();
-
- std::vector<int> getDeviceIds() override {
- if (driver)
- return driver->getInputDeviceIds();
- return {};
- }
- void setDeviceId(int deviceId) override;
- std::string getDeviceName(int deviceId) override {
- if (driver)
- return driver->getInputDeviceName(deviceId);
- return "";
- }
-
- std::vector<int> getChannels() override;
-
- virtual void onMessage(const Message &message) {}
- };
-
-
- struct InputQueue : Input {
- int queueMaxSize = 8192;
- std::queue<Message> queue;
- void onMessage(const Message &message) override;
- };
-
-
- struct Output : Port {
- /** Not owned */
- OutputDevice* outputDevice = NULL;
-
- Output();
- ~Output();
- void reset();
-
- std::vector<int> getDeviceIds() override {
- if (driver)
- return driver->getOutputDeviceIds();
- return {};
- }
- void setDeviceId(int deviceId) override;
- std::string getDeviceName(int deviceId) override {
- if (driver)
- return driver->getInputDeviceName(deviceId);
- return "";
- }
-
- std::vector<int> getChannels() override;
-
- void sendMessage(const Message &message);
- };
-
-
- void init();
- void destroy();
- /** Registers a new MIDI driver. Takes pointer ownership. */
- void addDriver(int driverId, Driver* driver);
- std::vector<int> getDriverIds();
- Driver* getDriver(int driverId);
-
-
- } // namespace midi
- } // namespace rack
|