[jmsl] Playing Through An External Midi Device

jmsl at music.columbia.edu jmsl at music.columbia.edu
Tue Apr 14 22:01:35 EDT 2009


Run this and then select the device from the console. It'll play a short 
note.
device.getMicrosecondPosition();
This method returns -1 if the device ignores time stamps. Otherwise, it 
returns the device's current notion of time, which you as the sender can use 
as an offset when determining the time stamps for messages you subsequently 
send. For example, if you want to send a message with a time stamp for five 
milliseconds in the future, you can get the device's current position in 
microseconds, add 5000 microseconds, and use that as the time stamp. Keep in 
mind that the MidiDevice's notion of time always places time zero at the 
time the device was opened.


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import javax.sound.midi.InvalidMidiDataException;

import javax.sound.midi.MidiDevice;

import javax.sound.midi.MidiSystem;

import javax.sound.midi.MidiUnavailableException;

import javax.sound.midi.ShortMessage;


public class Test {

public static void main(String args[]) throws InvalidMidiDataException, 
MidiUnavailableException, IOException {


MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();

for (int i = 0; i < infos.length; i++) {

System.out.println(i+": "+infos[i]);

}


BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Which device?");

int n = 0;

try {

n = Integer.parseInt(bf.readLine());

} catch (NumberFormatException e) {}


MidiDevice device = null;

try {

device = MidiSystem.getMidiDevice(infos[n]);

} catch (MidiUnavailableException e) {}

if (!(device.isOpen())) {

try {

device.open();

} catch (MidiUnavailableException e) {

// Handle or throw exception...

}

}


ShortMessage msg = new ShortMessage();

msg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);

long timeStamp = -1;

javax.sound.midi.Receiver rcvr = device.getReceiver();

rcvr.send(msg, timeStamp);

System.exit(0);

}


}



----- Original Message ----- 
From: <jmsl at music.columbia.edu>
To: <jmsl at music.columbia.edu>
Sent: Tuesday, April 14, 2009 11:50 AM
Subject: Re: [jmsl] Playing Through An External Midi Device


> Hi Chi
>
> Please help me out here, I don't have a lot of resources currently to work 
> on this.  If you can do the following in a few lines of pure Java (no JMSL 
> at all), then I will implement it in JMSL's JavaSound support
>
> If someone already knows how to do this, please share it here.
>
> You already know how to enumerate through midi device info.
>
> You can create a MidiDevice using minfo like this:
> MidiDevice mdev = MidiSystem.getMidiDevice(infos[i]);
>
> Your challenge is to open the mdev you want, get the transmitter or 
> receiver or whatever it is one needs to do to send a midi message out to 
> it, and send it a simple MidiMessage like a noteon.
> I've tried it but cannot get a receiver or a transmitter for these midi 
> devices, I just get nulls or exceptions.  Something's missing and I don't 
> know what it is.
>
> My code follows...
> MidiDevice.Info[] minfo= MidiSystem.getMidiDeviceInfo();
> for (int i = 0; i < minfo.length; i++) {
>                MidiDevice mdev = MidiSystem.getMidiDevice(minfo[i]);
>                String info = minfo[i].getDescription() + ", " +
>                minfo[i].getName() + ", " +
>                minfo[i].getVendor() + ", " +
>                minfo[i].getVersion();
>                System.out.println("\nDevice " + i + " = " + minfo[i] + " 
> ... " + mdev.toString());
>                System.out.println("CLASS: " + mdev.getClass());
>                mdev.open();
>                try {
>                    System.out.println("transmitter for this: " + 
> mdev.getTransmitter());
>                    System.out.println("receiver for this : " + 
> mdev.getReceiver());
>                } catch (MidiUnavailableException e) {
>                    System.out.println("can't get transmitter or receiver 
> for this");
>                }               mdev.close();
> }
>
> Thanks
> Nick
>
> jmsl at music.columbia.edu wrote:
>> How can I hookup JavaSound ports to JMSL? If I do this I can see all the 
>> external midi devices I have on my computer.
>>
>> MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
>> for (int i = 0; i < infos.length; i++) {
>>    System.out.println(infos[i]);
>> }
>>
>> /*
>> Result:
>> USB Audio Device
>> Microsoft MIDI Mapper
>> USB Audio Device
>> SB X-Fi Synth A [BC00]
>> SB X-Fi Synth B [BC00]
>> Microsoft GS Wavetable SW Synth
>> Real Time Sequencer
>> Java Sound Synthesizer
>> */
>>
>> If I do this, I get an error.
>> JMSL.midi = MidiIO_JavaSound.instance();
>> String[] deviceNames = JMSL.midi.getOutputDeviceNames();
>> System.out.println("There are " + deviceNames.length + "midi devices.");
>> for (int i=0; i<deviceNames.length; i++) {
>> System.out.println(deviceNames[i]);
>> }
>>
>> /*
>> Result:
>> There are 1midi devices.
>> Unimplemented
>> */
>>
>> _______________________________________________
>> jmsl mailing list
>> jmsl at music.columbia.edu
>> http://music.columbia.edu/mailman/listinfo/jmsl
> _______________________________________________
> jmsl mailing list
> jmsl at music.columbia.edu
> http://music.columbia.edu/mailman/listinfo/jmsl
> 



More information about the jmsl mailing list