1

I am trying to create a program that can play notes to an MIDI Keyboard. I couldn't get the whole program to work, but am now trying to create a smaller proof of concept program to try and work it out.

For this I am trying to play a .mid file to the keyboard (A Casio CTK-3000), MidiPlay can play to the keyboard. The problem is that when I try to play to the keyboard in my program, it just plays nothing. My code is below:

public class midiOut {

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

        while(true) {
            //List Midi Devices
            MidiDevice.Info[] midiInfo = MidiSystem.getMidiDeviceInfo();
            int i = 0;
            for (MidiDevice.Info info : midiInfo) {
                System.out.println(i + ": " +info.getName());
                i+=1;
            }
            //Select Device
            Scanner in = new Scanner(System.in);
            int input = in.nextInt();

            if(input == -1)
            {
                break;
            }
            //Get Device and Open it
            MidiDevice selectedDevice = MidiSystem.getMidiDevice(midiInfo[input]);
            System.out.println(selectedDevice.getDeviceInfo().getName());
            selectedDevice.open();
            //Get Sequencer and Receiver, load the File and start playing
            Receiver receiver = selectedDevice.getReceiver();
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.getTransmitter().setReceiver(receiver);
            sequencer.open();
            sequencer.setSequence(MidiSystem.getSequence(new File("C:\\Users\\Harris Mirza\\Downloads\\PennyLane.mid")));
            sequencer.start();

            selectedDevice.close();

        }
    }
}
Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
Harris Mirza
  • 76
  • 1
  • 14

1 Answers1

1

Realised my mistake, I was closing the device too early, added a check before closing, amended code here:

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class midiOut {

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

        while(true) {
            //List Midi Devices
            MidiDevice.Info[] midiInfo = MidiSystem.getMidiDeviceInfo();
            int i = 0;
            for (MidiDevice.Info info : midiInfo) {
                System.out.println(i + ": " +info.getName());
                i+=1;
            }
            //Select Device
            Scanner in = new Scanner(System.in);
            int input = in.nextInt();

            if(input == -1)
            {
                break;
            }
            //Get Device and Open it
            MidiDevice selectedDevice = MidiSystem.getMidiDevice(midiInfo[input]);
            System.out.println(selectedDevice.getDeviceInfo().getName());
            selectedDevice.open();
            //Get Sequencer and Receiver, load the File and start playing
            Receiver receiver = selectedDevice.getReceiver();
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.getTransmitter().setReceiver(receiver);
            sequencer.open();
            sequencer.setSequence(MidiSystem.getSequence(new File("C:\\Users\\Harris Mirza\\Downloads\\Eight_bar_blues.mid")));
            sequencer.start();

            sequencer.addMetaEventListener(new MetaEventListener() {
                @Override
                public void meta(MetaMessage meta) {
                    if(meta.getType() == 47)
                    {
                        sequencer.close();
                    }
                }
            });

            while (sequencer.isOpen()){}
        }
    }
}
Harris Mirza
  • 76
  • 1
  • 14
  • 1
    Strangely when I do this it also plays through the PC Speakers. – Harris Mirza Jan 12 '16 at 13:42
  • I confirm it here.On my end it does not play at all. My keyboard is PSR-A3000 and it connected via USB-MIDI to iMac Mojave. I guess the core midi driver of Apple is not working properly. Any ideas? – chikitin Mar 20 '19 at 04:15