Scala console MIDI hands-on

As Scala console, Interactive Interpreter or REPL was one of my selling points for programming MIDI with Scala, I show what can be done with it. I do not show how to install Scala or how to access console.

First things first: Import Midi libraries to save typing later on.

scala> import javax.sound.midi._
import javax.sound.midi._

Enumerate devices attached.

scala> MidiSystem.getMidiDeviceInfo
res0: Array[javax.sound.midi.MidiDevice.Info] = Array(Bus 1, SCS.3d, SCS.3d, FastTrack Pro, Bus 1, SCS.3d, SCS.3d, FastTrack Pro, Real Time Sequencer, Java Sound Synthesizer)

We are interested in two Stanton SCS.3d DaScratch controllers attached directly to USB ports, so let’s filter them using res0 from previous evaluation.

scala> res0.filter(_.getName == "SCS.3d")
res1: Array[javax.sound.midi.MidiDevice.Info] = Array(SCS.3d, SCS.3d, SCS.3d, SCS.3d)

Now there are four MidiDevice infos: Two inputs and two outputs – one for each physical device.

Let’s map those information blocks to actual devices.

scala> res1.map(MidiSystem.getMidiDevice)
res2: Array[javax.sound.midi.MidiDevice] = Array(com.sun.media.sound.MidiInDevice@503f0b70, com.sun.media.sound.MidiInDevice@5b080f38, com.sun.media.sound.MidiOutDevice@6e1f5438, com.sun.media.sound.MidiOutDevice@4ad26103)

Let’s filter out the inputs and outputs in two separate lists. Again we use result from previous evaluation, but now we are storing the lists to named variables:

scala> val ins = res2.filter(_.getMaxTransmitters != 0)  
ins: Array[javax.sound.midi.MidiDevice] = Array(com.sun.media.sound.MidiInDevice@503f0b70, com.sun.media.sound.MidiInDevice@5b080f38)

scala> val outs = res2.filter(_.getMaxReceivers != 0)
outs: Array[javax.sound.midi.MidiDevice] = Array(com.sun.media.sound.MidiOutDevice@6e1f5438, com.sun.media.sound.MidiOutDevice@4ad26103)

Get the first output device and open it.

scala> val out = outs.head
out: javax.sound.midi.MidiDevice = com.sun.media.sound.MidiOutDevice@6e1f5438

scala> out.open

Send note on message for key number 0×72 on channel 1 with velocity 1 to light a deck indicator on DaScratch.

scala> val m = new ShortMessage()
m: javax.sound.midi.ShortMessage = javax.sound.midi.ShortMessage@7f7ef0a7

scala> m.setMessage(0x90, 0x72, 0x01)

scala> out.getReceiver.send(m, -1)

That’s it. Using Scala REPL is very fast way to check how the MIDI works with JVM.