From 53698a7fa42823c7c1e921eb169dfaa268440cc7 Mon Sep 17 00:00:00 2001 From: Apollo Nargang Date: Thu, 10 Oct 2024 16:57:03 -0400 Subject: [PATCH] Add basic MIDI functionality Adds a MIDIDevice class that wraps a reciever device and a transmitter device, and adds MIDI functionality to the TestControlMIDI. --- .../buttonbox/controls/TestControlMIDI.java | 19 +++++++++- .../buttonbox/midi/MIDIDevice.java | 35 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/midi/MIDIDevice.java diff --git a/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/controls/TestControlMIDI.java b/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/controls/TestControlMIDI.java index 2297144..ba08879 100644 --- a/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/controls/TestControlMIDI.java +++ b/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/controls/TestControlMIDI.java @@ -1,22 +1,39 @@ package io.github.roboblazers7617.buttonbox.controls; +import javax.sound.midi.ShortMessage; +import javax.sound.midi.InvalidMidiDataException; + +import io.github.roboblazers7617.buttonbox.midi.MIDIDevice; + /** * A test {@link io.github.roboblazers7617.buttonbox.Control} that reads the data published by a {@link io.github.roboblazers7617.buttonbox.controls.TestControl} and outputs it as MIDI messages. */ public class TestControlMIDI extends TestControl { + private final MIDIDevice midiDevice; + /** * Creates a new TestControlMIDI. * * @param id * The ID string for the TestControlMIDI to use. + * @param midiDevice + * The MIDI device to send messages to. * @see io.github.roboblazers7617.buttonbox.Control */ - public TestControlMIDI(String id) { + public TestControlMIDI(String id, MIDIDevice midiDevice) { super(id); + this.midiDevice = midiDevice; } @Override public void updateHardware() { System.out.println(getValue()); + try { + ShortMessage message = new ShortMessage(); + message.setMessage(ShortMessage.CONTROL_CHANGE, 0, 0, (int) (getValue() % 127)); + midiDevice.send(message); + } catch (InvalidMidiDataException ex) { + System.err.println("Invalid MIDI data!"); + } } } diff --git a/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/midi/MIDIDevice.java b/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/midi/MIDIDevice.java new file mode 100644 index 0000000..520864f --- /dev/null +++ b/buttonbox-bridge-midi/src/main/java/io/github/roboblazers7617/buttonbox/midi/MIDIDevice.java @@ -0,0 +1,35 @@ +package io.github.roboblazers7617.buttonbox.midi; + +import javax.sound.midi.MidiDevice; +import javax.sound.midi.MidiUnavailableException; +import javax.sound.midi.Receiver; +import javax.sound.midi.ShortMessage; +import javax.sound.midi.Transmitter; + +public class MIDIDevice { + private final MidiDevice rxDevice; + private final MidiDevice txDevice; + private final Receiver receiver; + private final Transmitter transmitter; + + /** + * Wrapper for {@link MidiDevice} + * + * @param rxDevice + * {@link MidiDevice} to use for receiving messages + * @param txDevice + * {@link MidiDevice} to use for transmitting messages + * @implNote + * Provided devices must already be opened. + */ + public MIDIDevice(MidiDevice rxDevice, MidiDevice txDevice) throws MidiUnavailableException { + this.rxDevice = rxDevice; + this.txDevice = txDevice; + this.receiver = rxDevice.getReceiver(); + this.transmitter = txDevice.getTransmitter(); + } + + public void send(ShortMessage message) { + receiver.send(message, -1); + } +}