JFugue: Is there a way to get the current note that a player() is on?

181 views Asked by At

I am making a virtual piano project and I have some transcribed sample songs. I would like to know if I could get the current note that the player is on, so that it could be displayed visually on a piano.

Edit: I'm still learning Java, so sorry in advance if I need some more explanation than usual.

2

There are 2 answers

2
sorifiend On

You can create a ParserListener to listen for any musical event that any parser is parsing. I have adjusted one of the examples to print out the note position in an octave. You can modify this to find out exactly which note is pressed:

public class ParserDemo {
    public static void main(String[] args) throws InvalidMidiDataException, IOException {
        MidiParser parser = new MidiParser(); // Remember, you can use any Parser!
        MyParserListener listener = new MyParserListener();
        parser.addParserListener(listener);
        parser.parse(MidiSystem.getSequence(new File(PUT A MIDI FILE HERE)));
    }
}

//Extend the ParserListenerAdapter and override the onNoteParsed event to find the current note
class MyParserListener extends ParserListenerAdapter {    
    @Override
    public void onNoteParsed(Note note) {
        //A "C" note is in the 0th position of an octave
        System.out.println("Note pushed at position " + note.getPositionInOctave());
    }
}

Source: http://www.jfugue.org/examples.html

2
David Koelle On

When JFugue is playing music, it is using javax.sound.midi.Sequencer to playback a MIDI Sequence. That means you can listen to the MIDI events themselves using a Receiver on the same MidiDevice, and since MIDI is a system-wide resource on your computer, you can even do this outside of Java.