How can I stream the input from a MIDI piano in Java?

1

I would like to stream data I can get from a MIDI piano. I used some code from another MIDI related question here, but I can't seem to be able to get it working, as I get this message :

mai 31, 2016 4:20:16 PM java.util.prefs.WindowsPreferences

WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

The track I use to store what is supposed to be read also only contains an ImmutableEndOfTrack.

In the end, the goal would be to get a stream of the notes played rather than storing them, as a listener would.

import javax.sound.midi.*;

public class Main {

    private static final String DEVICE_NAME = "Medeli e-Drum  ";    // MIDI piano

    public static void main(String args[]) {
        MidiDevice md = null;
        Sequencer seqr = null;
        Transmitter trans = null;
        Receiver rec = null;
        Sequence seq = null;
        Track currTrack = null;

        try {
            seq = new Sequence(Sequence.PPQ, 24);

            currTrack = seq.createTrack();

            seqr = MidiSystem.getSequencer();
            seqr.setSequence(seq);
            seqr.setTickPosition(0);
            seqr.recordEnable(currTrack, -1);

            // get MIDI device by DEVICE_NAME
            for(MidiDevice.Info md_i : MidiSystem.getMidiDeviceInfo())
                if(DEVICE_NAME.equals(md_i.getName()))
                    md = MidiSystem.getMidiDevice(md_i);

            if(!md.isOpen())
                md.open();
            seqr.open();

            seqr.startRecording();
            Thread t = new Thread(new Runnable() { // Just to have the time to play on the piano
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
            t.join();
            seqr.stopRecording();

            for(int i = 0 ; i < currTrack.size() ; i++)
                System.out.println(currTrack.get(i).getMessage());
        } catch (InvalidMidiDataException e) {
            e.printStackTrace();
        } catch (MidiUnavailableException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            md.close();
            seqr.close();
        }
    }
}
java
midi
asked on Stack Overflow May 31, 2016 by Icarimosa

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0