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();
        }
    }
}
Community
  • 1
  • 1
Icarimosa
  • 11
  • 2
  • Take a look at this answer to this question. It may assist you here: http://stackoverflow.com/a/7425644/5969411 – ManoDestra May 31 '16 at 15:26
  • 1
    Thanks for the answer. (Finally, I used http://stackoverflow.com/questions/16428098/groovy-shell-warning-could-not-open-create-prefs-root-node to fix this problem. But now, the main question is still on) – Icarimosa May 31 '16 at 16:51

0 Answers0