3

I use the following code to load and play a MIDI file with JFugue:

import java.io.File;

import org.jfugue.Pattern;
import org.jfugue.Player;

public class PlayMidiFromFile {

    public static void main(final String[] args) {
        try {
            final Player player = new Player();
            final Pattern pattern = player.loadMidi(new File("sample.mid"));
            player.play(pattern);
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

The file was generated with ChordPulse and playback with other programs works fine.

It contains multiple tracks with a different instrument for each, but the Player only uses piano for all and some tracks seem to be missing.

How to fix this? Are there certain MIDI messages, that are not recognized by the parser? Are there any precondition about how the song is using tracks and channels or other known limitations or neccessary initialization steps?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • 1
    Are you sure the Player supports multiple instruments? Not all do. – keshlam Feb 03 '14 at 18:07
  • Well, JFugues internal representation (called `MusicString`) does, like shown in the [examples](http://www.jfugue.org/examples.html). But i'm not sure about the MIDI file parser. – Jens Piegsa Feb 03 '14 at 18:14
  • BTW I'm currently using JFugue 4.0.3. I'll give JFugue 5 Beta a try for that. – Jens Piegsa Feb 03 '14 at 18:17
  • @keshlam is thinking the right stuff - not all MIDI Synthesizers support the full gamut of the MIDI Specification. Without seeing Jens's MIDI file, it's hard to know what the issue is. Fortunately, reading from MIDI files is improved in JFugue 5, as reflected in Jens's answer to his own question! – David Koelle Mar 22 '14 at 02:15

1 Answers1

2

The tuba part is still played by the piano, but, apart from that, the MIDI support obviously has been improved in the beta version 5.

An update of the snippet above (reflecting the API changes):

import java.io.File;

import org.jfugue.midi.MidiFileManager;
import org.jfugue.pattern.Pattern;
import org.jfugue.player.Player;

public class PlayMidiFromFile {

    public static void main(final String[] args) {
        try {
            final Player player = new Player();
            final Pattern pattern = MidiFileManager.loadPatternFromMidi(new File("sample.mid"));
            player.play(pattern);
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • 1
    Yes, improved support for reading existing MIDI files is something I've been working hard towards in writing JFugue 5. Glad you found your answer! – David Koelle Mar 22 '14 at 02:13