1

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.

jwang22
  • 19
  • 4
  • What have you tried so far? Did you check any of the examples on the jfugue website or any tutorials? – sorifiend Apr 27 '21 at 23:41
  • I've tried reading up on parser but nothing seems to be what I'm looking for. – jwang22 Apr 27 '21 at 23:52
  • 1
    JFugue knows about notes as it is *parsing* the MusicString, but once the notes are *playing*, that's all handled by javax.sound.midi.Sequencer playing a Sequence. – David Koelle Apr 28 '21 at 18:11

2 Answers2

1

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

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • Note that you could also use `note.getToneString() `, see other methods you can use to get details about a note here: http://www.jfugue.org/doc/org/jfugue/theory/Note.html – sorifiend Apr 27 '21 at 23:56
  • Thanks, I'll try this solution out! – jwang22 Apr 28 '21 at 19:37
1

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.

David Koelle
  • 20,726
  • 23
  • 93
  • 130
  • I've been doing some research but so far I'm having trouble understanding some of the code. How would I use a receiver to get the MIDI events from the RealtimePlayer I am using? Thanks. – jwang22 Apr 28 '21 at 20:14
  • See this post, which has an answer with a code sample. In particular, note the send() method. Within that message, you can pick apart the MIDI event to find out whether it's a Note On, Note Off (and in those cases, which notes), or other event. https://stackoverflow.com/questions/51013510/receive-midi-input-with-javax-sound-midi – David Koelle May 05 '21 at 03:00