Midi transmitter in java... Evan Merz code

288 views
Skip to first unread message

Carlo Ernesto Sarli

unread,
Apr 10, 2017, 6:01:22 AM4/10/17
to beadsproject
Hello everyone,
I'm trying to make my beads synth able to read midi input from my nanoKey 2 from korg. In the book sound synthesis in Java Evan Merz has written code for the korg nanoKey 1  so i thought i was in luck. I put that code in my project but I can't get it to work: every time i try to configure a USB MIDI device it gives me error. It sees my device but it says the transmitter is not available.

javax.sound.midi.MidiUnavailableException: MIDI OUT transmitter not available
at com.sun.media.sound.AbstractMidiDevice.createTransmitter(Unknown Source)
at com.sun.media.sound.AbstractMidiDevice.getTransmitter(Unknown Source)
at source.MidiKeyboard.openDevice(MidiKeyboard.java:110)
at source.MidiKeyboard.configure(MidiKeyboard.java:239)
at source.MidiKeyboardConfigure.main(MidiKeyboardConfigure.java:8)

this is the method in question 

private void openDevice()
{
// Obtain information about all the installed synthesizers.
nkDevice = null;
nkTransmitter = null;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < infos.length; i++)
{
   try
   {
    MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
       MidiDevice.Info d = infos[i];
       
       if( d.getName().contains(deviceName) )
       {
        System.out.println("Opening " + d.getName());
        System.out.println("  max receivers: " + device.getMaxReceivers());
        System.out.println("  max transmitters: " + device.getMaxTransmitters());

        nkDevice = device;
       
        nkTransmitter = device.getTransmitter();
        nkTransmitter.setReceiver(new MidiInputReceiver(device.getDeviceInfo().getName()));
       
        nkDevice.open();
       }
   }
   catch (MidiUnavailableException e)
   {
         // Handle or throw exception...
    System.out.println("Exception occurred while getting MIDI devices!");
    System.out.println(e.getMessage());
    e.printStackTrace();
   }
}
}

I've spent a week messing around with it and trying to figure out why it doesn't work. Online everybody just seem to dismiss this as a java bug,
Is that just simply it?
Any ideas why it does not work and how I can fix it?
If it can't be fixed, any alternatives? something maybe like midibus but not for processing

Thank you very much as always
Carlo

Federico Joselevich Puiggrós

unread,
Apr 10, 2017, 6:48:35 PM4/10/17
to beadsp...@googlegroups.com
I've tested your code without problems. 

I'm testing this with nanoKontrol2, and I always use it on CC mode rather than the default DAW mode. It appears on my MIDI list as "SLIDER/KNOB" (when in DAW mode it appears as "nanoKontrol2"). What I saw is that if I need it to be able to send and/or receive MIDI messages it MUST be changed from the default.

Are you using MacOS or Windows?

--
You received this message because you are subscribed to the Google Groups "beadsproject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beadsproject+unsubscribe@googlegroups.com.
To post to this group, send email to beadsp...@googlegroups.com.
Visit this group at https://groups.google.com/group/beadsproject.
For more options, visit https://groups.google.com/d/optout.



--

Carlo Ernesto Sarli

unread,
Apr 12, 2017, 5:52:28 AM4/12/17
to beadsproject
Hi, thanks for your help,
I'm on windows and it comes out as korg nanokey2 twice but both don't work. CC mode and Daw mode are not available for the nanokey 2 so I don't really know what to do there :).
I tried it on mac and the keyboard comes out with a different name but I haven't been able to test it as it says I don't have permission to save the config file. I tried fixing it but I haven't been able to unfortunately.

Federico Joselevich Puiggrós

unread,
Apr 12, 2017, 7:21:33 AM4/12/17
to beadsp...@googlegroups.com
Btw, did you try to change it to CC mode with Korg Editor?

--
You received this message because you are subscribed to the Google Groups "beadsproject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beadsproject+unsubscribe@googlegroups.com.
To post to this group, send email to beadsp...@googlegroups.com.
Visit this group at https://groups.google.com/group/beadsproject.
For more options, visit https://groups.google.com/d/optout.

Carlo Ernesto Sarli

unread,
Apr 12, 2017, 7:29:42 AM4/12/17
to beadsproject
Sorry for being a tad inept, I have the korg editor but i can't seem to find the option to change the mode of the keyboard
To unsubscribe from this group and stop receiving emails from it, send an email to beadsproject...@googlegroups.com.

To post to this group, send email to beadsp...@googlegroups.com.
Visit this group at https://groups.google.com/group/beadsproject.
For more options, visit https://groups.google.com/d/optout.

Federico Joselevich Puiggrós

unread,
Apr 12, 2017, 7:34:12 AM4/12/17
to beadsp...@googlegroups.com
Sorry, my fault. It doesn't have a way to change it between those two modes as I see, nor in the editor.

Anyway, perhaps you should try to use a virtual midi device such as http://vmpk.sourceforge.net/ to see if your code is working, just to be sure that the problem is not something else.
 


To unsubscribe from this group and stop receiving emails from it, send an email to beadsproject+unsubscribe@googlegroups.com.

To post to this group, send email to beadsp...@googlegroups.com.
Visit this group at https://groups.google.com/group/beadsproject.
For more options, visit https://groups.google.com/d/optout.

Carlo Ernesto Sarli

unread,
Apr 12, 2017, 7:50:23 AM4/12/17
to beadsproject
I'm trying the virtual keyboard but the code doesn't seem to see it. I tried messing aroumd with the midi connections but no luck


On Monday, 10 April 2017 11:01:22 UTC+1, Carlo Ernesto Sarli wrote:

Carlo Ernesto Sarli

unread,
Apr 12, 2017, 9:50:04 AM4/12/17
to beadsproject
Btw have you managed to run the midi example?


On Monday, 10 April 2017 11:01:22 UTC+1, Carlo Ernesto Sarli wrote:

Federico Joselevich Puiggrós

unread,
Apr 12, 2017, 9:58:55 AM4/12/17
to beadsp...@googlegroups.com
Yes, here you have the files I created. As you'll see I just made little modifications to the code you've sent. 

Here is the full code:

package cc.ludic;

import javafx.application.Application;
import javafx.stage.Stage;

import javax.sound.midi.*;

/**
* Created by federico on 10/4/17.
*/
public class MidiExample extends Application {
private MidiDevice nkDevice;
private Transmitter nkTransmitter;
@Override
public void start(Stage primaryStage) throws Exception {
openDevice();
    }

private void openDevice()
{
// Obtain information about all the installed synthesizers.
nkDevice = null;
nkTransmitter = null;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < infos.length; i++)
{
try
{
                System.out.println(infos[i].toString());
String deviceName = "SLIDER";
                MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
MidiDevice.Info d = infos[i];

if( d.getName().contains(deviceName) )
{
System.out.println("Opening " + d.getName());
System.out.println(" max receivers: " + device.getMaxReceivers());
System.out.println(" max transmitters: " + device.getMaxTransmitters());

nkDevice = device;

nkTransmitter = device.getTransmitter();
nkTransmitter.setReceiver(new MidiInputReceiver(device.getDeviceInfo().getName()));

nkDevice.open();
}
}
catch (MidiUnavailableException e)
{
// Handle or throw exception...
System.out.println("Exception occurred while getting MIDI devices!");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
}

class MidiInputReceiver implements Receiver {
public MidiInputReceiver(String name) {
System.out.println(name);
}

@Override
public void send(MidiMessage message, long timeStamp) {

}

@Override
public void close() {

}
}
And here is the output:

Gervill
Midi Bus 1
Bus IAC 2
SLIDER/KNOB
Opening SLIDER/KNOB
  max receivers: 0
  max transmitters: -1
SLIDER/KNOB
Midi Bus 1
Bus IAC 2
CTRL
Real Time Sequencer


--
You received this message because you are subscribed to the Google Groups "beadsproject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beadsproject+unsubscribe@googlegroups.com.
To post to this group, send email to beadsp...@googlegroups.com.
Visit this group at https://groups.google.com/group/beadsproject.
For more options, visit https://groups.google.com/d/optout.

Carlo Ernesto Sarli

unread,
Apr 12, 2017, 10:06:00 AM4/12/17
to beadsproject
Thank you, I really appreciate the help, but this is so frustrating... even on mac I get keyboard by korg which works but it is the midi in so I can'y use it to play or CTRL by korg which is the out and does not work... I keep getting midi out transmitter error

On Monday, 10 April 2017 11:01:22 UTC+1, Carlo Ernesto Sarli wrote:

Piotr Amenda

unread,
Jan 8, 2018, 6:28:17 PM1/8/18
to beadsproject
Hello,

I know it's been a while since you've asked this questions but I've run into the very same problem recently and I think I found the solution. I'll post it here just in case someone else struggles with this issue.

As you've mentioned the nanoKEY2 gets listed twice: once as a transmitter and once as a receiver. When in the for loop the first occurence of nanoKEY2 gets checked a transmitter gets detected correctly, however the exception gets thrown upon checking of the second (receiver) occurence. I believe the original configuration code that you've posted is a bit clunky and it tries to get the transmitter from device's instance even when there aren't any available, hence the exception. And since the device gets listed twice with the same name the code always checks both occurences as it identifies the device only by name. In conclusion - transmitter does get detected on the first occurence of nanoKEY2 so the thrown exception is a bit misguiding.

I also found another issue with MIDI examples in the book "Sound Synthesis in Java" (I believe that this is where you got this code from). The original example with MIDI keyboard doesn't work as presented in the book, however surprisingly it has nothing to do with the discussed exception. To fix it you just have to change the hardcoded device name in class "MidiKeyboard.java" from "nanoKEY 1 KEYBOARD" to "nanoKEY2":
[CODE]
public class MidiKeyboard
{
    private String deviceName = "nanoKEY 1 KEYBOARD"; //change to nanoKEY2
[/CODE]

The original example code doesn't set properly the device name stored in config file that gets created after you run the "MidiKeyboardConfigure.java" and it always uses this hardcoded value instead.

Regards,
Piotr
Reply all
Reply to author
Forward
0 new messages