Overte C++ Documentation
Midi.h
1 //
2 // Midi.h
3 // libraries/midi/src
4 //
5 // Created by Burt Sloane
6 // Modified by Bruce Brown
7 // Copyright 2015 High Fidelity, Inc.
8 // Copyright 2021 Vircadia contributors.
9 //
10 // Distributed under the Apache License, Version 2.0.
11 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12 //
13 
14 #ifndef hifi_Midi_h
15 #define hifi_Midi_h
16 
17 #include <QtCore/QObject>
18 #include <QAbstractNativeEventFilter>
19 #include <DependencyManager.h>
20 
21 #include <vector>
22 #include <string>
23 
24 /*@jsdoc
25  * The <code>Midi</code> API provides the ability to connect Interface with musical instruments and other external or virtual
26  * devices via the MIDI protocol. For further information and examples, see the tutorial:
27  * <a href="https://docs.overte.org/script/midi-tutorial.html">Use MIDI to Control Your Environment</a>.
28  *
29  * <p><strong>Note:</strong> Only works on Windows.</p>
30  *
31  * @namespace Midi
32  *
33  * @hifi-interface
34  * @hifi-client-entity
35  * @hifi-avatar
36  */
37 
38 class Midi : public QObject, public Dependency {
39  Q_OBJECT
40  SINGLETON_DEPENDENCY
41 
42 public:
43  void midiReceived(int device, int raw, int channel, int status, int type, int note, int velocity, int bend, int program); // relay a note to Javascript
44  void midiHardwareChange(); // relay hardware change to Javascript
45  void sendRawMessage(int device, int raw); // relay midi message to MIDI outputs
46  void sendNote(int status, int note, int velocity); // relay a note to MIDI outputs
47  void sendMessage(int device, int channel, int type, int note, int velocity); // relay a message to MIDI outputs
48  static void USBchanged();
49 
50 private:
51  static std::vector<QString> midiInExclude;
52  static std::vector<QString> midiOutExclude;
53 
54 private:
55  void MidiSetup();
56  void MidiCleanup();
57 
58 signals:
59 
60  /*@jsdoc
61  * Triggered when a connected device sends an output.
62  * @function Midi.midiNote
63  * @param {Midi.MidiMessage} message - The MIDI message.
64  * @returns {Signal}
65  * @deprecated This signal is deprecated and will be removed. Use {@link Midi.midiMessage|midiMessage} instead.
66  */
67  void midiNote(QVariantMap eventData);
68 
69  /*@jsdoc
70  * Triggered when a connected device sends an output.
71  * @function Midi.midiMessage
72  * @param {Midi.MidiMessage} message - The MIDI message.
73  * @returns {Signal}
74  */
75  void midiMessage(QVariantMap eventData);
76 
77  /*@jsdoc
78  * Triggered when the system detects there was a reset such as when a device is plugged in or unplugged.
79  * @function Midi.midiReset
80  * @returns {Signal}
81  */
82  void midiReset();
83 
84 public slots:
85 
86  /*@jsdoc
87  * Sends a raw MIDI packet to a particular device.
88  * @function Midi.sendRawDword
89  * @param {number} device - Integer device number.
90  * @param {Midi.RawMidiMessage} raw - Raw MIDI message.
91  */
92  Q_INVOKABLE void sendRawDword(int device, int raw);
93 
94  /*@jsdoc
95  * Sends a MIDI message to a particular device.
96  * @function Midi.sendMidiMessage
97  * @param {number} device - Integer device number.
98  * @param {number} channel - Integer channel number.
99  * @param {Midi.MidiStatus} type - Integer status value.
100  * @param {number} note - Note number.
101  * @param {number} velocity - Note velocity. (<code>0</code> means "note off".)
102  * @comment The "type" parameter has that name to match up with {@link Midi.MidiMessage}.
103  */
104  Q_INVOKABLE void sendMidiMessage(int device, int channel, int type, int note, int velocity);
105 
106  /*@jsdoc
107  * Plays a note on all connected devices.
108  * @function Midi.playMidiNote
109  * @param {MidiStatus} status - Note status.
110  * @param {number} note - Note number.
111  * @param {number} velocity - Note velocity. (<code>0</code> means "note off".)
112  */
113  Q_INVOKABLE void playMidiNote(int status, int note, int velocity);
114 
115  /*@jsdoc
116  * Turns off all notes on all connected MIDI devices.
117  * @function Midi.allNotesOff
118  */
119  Q_INVOKABLE void allNotesOff();
120 
121  /*@jsdoc
122  * Cleans up and rediscovers attached MIDI devices.
123  * @function Midi.resetDevices
124  */
125  Q_INVOKABLE void resetDevices();
126 
127  /*@jsdoc
128  * Gets a list of MIDI input or output devices.
129  * @function Midi.listMidiDevices
130  * @param {boolean} output - <code>true</code> to list output devices, <code>false</code> to list input devices.
131  * @returns {string[]}
132  */
133  Q_INVOKABLE QStringList listMidiDevices(bool output);
134 
135  /*@jsdoc
136  * Blocks a MIDI device's input or output.
137  * @function Midi.blockMidiDevice
138  * @param {string} name - The name of the MIDI device to block.
139  * @param {boolean} output - <code>true</code> to block the device's output, <code>false</code> to block its input.
140  */
141  Q_INVOKABLE void blockMidiDevice(QString name, bool output);
142 
143  /*@jsdoc
144  * Unblocks a MIDI device's input or output.
145  * @function Midi.unblockMidiDevice
146  * @param {string} name- The name of the MIDI device to unblock.
147  * @param {boolean} output - <code>true</code> to unblock the device's output, <code>false</code> to unblock its input.
148  */
149  Q_INVOKABLE void unblockMidiDevice(QString name, bool output);
150 
151  /*@jsdoc
152  * Enables or disables repeating all incoming notes to all outputs. (Default is disabled.)
153  * @function Midi.thruModeEnable
154  * @param {boolean} enable - <code>true</code> to enable repeating all incoming notes to all output, <code>false</code> to
155  * disable.
156  */
157  Q_INVOKABLE void thruModeEnable(bool enable);
158 
159 
160  /*@jsdoc
161  * Enables or disables broadcasts to all unblocked devices.
162  * @function Midi.broadcastEnable
163  * @param {boolean} enable - <code>true</code> to have "send" functions broadcast to all devices, <code>false</code> to
164  * have them send to specific output devices.
165  */
166  Q_INVOKABLE void broadcastEnable(bool enable);
167 
168 
170 
171  /*@jsdoc
172  * Enables or disables note off events.
173  * @function Midi.typeNoteOffEnable
174  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
175  */
176  Q_INVOKABLE void typeNoteOffEnable(bool enable);
177 
178  /*@jsdoc
179  * Enables or disables note on events.
180  * @function Midi.typeNoteOnEnable
181  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
182  */
183  Q_INVOKABLE void typeNoteOnEnable(bool enable);
184 
185  /*@jsdoc
186  * Enables or disables poly key pressure events.
187  * @function Midi.typePolyKeyPressureEnable
188  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
189  */
190  Q_INVOKABLE void typePolyKeyPressureEnable(bool enable);
191 
192  /*@jsdoc
193  * Enables or disables control change events.
194  * @function Midi.typeControlChangeEnable
195  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
196  */
197  Q_INVOKABLE void typeControlChangeEnable(bool enable);
198 
199  /*@jsdoc
200  * Enables or disables program change events.
201  * @function Midi.typeProgramChangeEnable
202  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
203  */
204  Q_INVOKABLE void typeProgramChangeEnable(bool enable);
205 
206  /*@jsdoc
207  * Enables or disables channel pressure events.
208  * @function Midi.typeChanPressureEnable
209  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
210  */
211  Q_INVOKABLE void typeChanPressureEnable(bool enable);
212 
213  /*@jsdoc
214  * Enables or disables pitch bend events.
215  * @function Midi.typePitchBendEnable
216  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
217  */
218  Q_INVOKABLE void typePitchBendEnable(bool enable);
219 
220  /*@jsdoc
221  * Enables or disables system message events.
222  * @function Midi.typeSystemMessageEnable
223  * @param {boolean} enable - <code>true</code> to enable, <code>false</code> to disable.
224  */
225  Q_INVOKABLE void typeSystemMessageEnable(bool enable);
226 
227 
228 public:
229  Midi();
230  virtual ~Midi();
231 };
232 
233 #endif // hifi_Midi_h