0.0.3 • Published 5 years ago
react-native-midi v0.0.3
react-native-midi
React-native module for accessing USB midi controllers (on Android). Plans to include Bluetooth and iOS support eventually.
Getting started
$ npm install react-native-midi --save
Installation
Add USB feature in android/src/main/AndroidManifest:
<manifest>
<uses-feature android:name="android.hardware.usb.host" /> <!-- HERE!-->
<uses-feature android:name="android.software.midi" android:required="true"/> <!-- HERE!-->
<application>
...
</application>
</manifest>Usage
The library is mostly event driven, with a few helper functions for getting the current state of the midi controllers.
Example
import Midi from 'react-native-midi';
// Register for some common events:
Midi.on(Midi.INPUT_DEVICE_ATTACHED, device => {
console.log(`Device "${device.id}" connected`);
});
Midi.on(Midi.INPUT_DEVICE_DETACHED, device => {
console.log(`Device "${device.id}" disconnected`);
});
Midi.on(Midi.NOTE_ON, event => {
console.log(`Device "${event.device}" started note: ${event.note}`);
});
Midi.on(Midi.NOTE_OFF, event => {
console.log(`Device "${event.device}" stopped note: ${event.note}`);
});
// Stop listening to note events
Midi.off(Midi.NOTE_ON);
Midi.off(Midi.NOTE_OFF);
// Getters
const devices = await Midi.getDevices(); // Currently attached devices
const deviceCount = await Midi.getDeviceCount(); // Number of devices currently attached
const device = await Midi.getDevice(123); // Get device with id '123'Functions
| Function | Arguments | Returns | Description |
|---|---|---|---|
| on | Event, callback | Registers listener for Event. Invokes callback until unregistered | |
| off | Event | Unregisters listener | |
| getDevices | Promise\<Device> | Gets connected devices | |
| getDeviceCount | Promise\<Number> | Gets number of connected devices | |
| getDevice | deviceId | Promise\<Device> | Gets connected device by id |
Events
Device Attach/Detach Events
DEVICE_ATTACHEDDEVICE_DETACHEDINPUT_DEVICE_ATTACHEDINPUT_DEVICE_DETACHEDOUTPUT_DEVICE_ATTACHEDOUTPUT_DEVICE_DETACHED
All of these attach/detach events invoke the callback with the following Device object:
{
id, // Id matches the 'device' property of midi events
protocol,
subclass,
name,
productId,
vendorId,
interfaces,
configurations, // >= Android "Lollipop"
serialNumber, // >= Android "Lollipop"
version, // >= Android "M"
deviceAddress, // I/O device only
manufacturerName, // I/O device only
productName // I/O device only
}Example
Midi.on(Midi.INPUT_DEVICE_ATTACHED, device => {
console.log(`Device "${device.name}" by ${device.vendorId} has attached`);
});Midi Events
All midi event listeners invoke the callback with an object containing a device (int) and cable (int) property, in addition to those properties listed below:
MISC_FUNCTION_CODEScodes(byte[])
CABLE_EVENTSevents(byte[])
SYSTEM_COMMON_MESSAGEmessage(string)
SYSTEM_EXCLUSIVEmessage(string)
NOTE_ONchannel(int)note(int)velocity(int)
NOTE_OFFchannel(int)note(int)velocity(int)
POLYPHONIC_AFTERTOUCHchannel(int)note(int)pressure(int)
CONTROL_CHANGEchannel(int)function(int)value(int)
PROGRAM_CHANGEchannel(int)program(int)
CHANNEL_AFTERTOUCHchannel(int)pressure(int)
PITCH_WHEELchannel(int)amount(int)
SINGLE_BYTEbyte(byte)
TIME_CODE_QUARTER_FRAMEtiming(int)
SONG_SELECTsong(int)
SONG_POSITION_POINTERposition(int)
TUNE_REQUESTTIMING_CLOCKSTARTCONTINUESTOPACTIVE_SENSINGRESET
Example
Midi.on(Midi.NOTE_ON, event => {
console.log(`Device "${event.device}" started note: ${event.note}`);
});Contributing
Open to pull requests! I'm looking for someone to take on the iOS support in particular as I'm lacking devices to develop on.
TODO
- iOS support
- Bluetooth support
Document event bodies
