1

I apologize if this is a duplicate, but nothing I have found here seems to work. I am working on a game for Desktop and Android in Java using libGDX. I added MIDI support by using an interface in my core module that is implemented in the platform-specific modules. Here is that interface:

public interface Midi {
    void setLooping(boolean looping);
    void pause();
    void stop();
    void setPosition(int pos);
    void dispose();
    long getPosition();
    boolean isLooping();
    void setVolume(float volume);
    void onPause();
    void onResume();
}

And my implementations in my Desktop and Android projects load and play the midi files perfectly. In the Android version, I can even set the volume easily. However I cannot figure out how to implement the volume on desktop. I want to be able to set the volume of the track to a float between 0 and 1 such that 0 is completely quiet, and 1 is the loudest. Here is my current implementation of the midi player on Desktop:

public class MidiPlayer implements Midi {

    /**
     * The sequencer to play the midi track.
     */
    private Sequencer sequencer;
    /**
     * Whether or not the track is looping.
     */
    private boolean looping;

    public MidiPlayer(FileHandle fileHandle) {
        try {
            sequencer = MidiSystem.getSequencer();
            sequencer.open();
            sequencer.setSequence(fileHandle.read());
            sequencer.setMicrosecondPosition(0);
            sequencer.setLoopStartPoint(0);
            sequencer.setLoopEndPoint(-1);
            looping = false;
        } catch (MidiUnavailableException | InvalidMidiDataException | IOException e) {
            Gdx.app.error(getClass().getName(), e.getMessage(), e);
        }
    }

    @Override
    public void play() {
        sequencer.start();
    }

    @Override
    public void setLooping(boolean looping) {
        this.looping = looping;
        if(!looping) {
            sequencer.setLoopCount(0);
        } else {
            sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
        }

    }

    @Override
    public void pause() {
        sequencer.stop();
    }

    @Override
    public void stop() {
        sequencer.stop();
        sequencer.setMicrosecondPosition(0);
    }

    @Override
    public void setPosition(int pos) {
        sequencer.setMicrosecondPosition(pos);
    }

    @Override
    public void dispose() {
        sequencer.close();
    }

    @Override
    public long getPosition() {
        return sequencer.getMicrosecondPosition();
    }

    @Override
    public boolean isLooping() {
        return this.looping;
    }

    @Override
    public void setVolume(float volume) {
        //I have tried several things here and none have worked
    }

    @Override
    public void onPause() {
        //ANDROID ONLY
    }

    @Override
    public void onResume() {
        //ANDROID ONLY
    }
}

I have tried many different things, I have tried the following solutions: http://www.java2s.com/Code/Java/Development-Class/SettingtheVolumeofPlayingMidiAudio.htm

What's the method to control volume in an MIDI sequencer?

How to control the MIDI channel's volume

And none have worked.

JoseRivas1998
  • 433
  • 1
  • 6
  • 14
  • How do you handle the volume on Android? What synthesizer are you using on the desktop? – CL. Oct 01 '17 at 06:46
  • @CL.As for desktop, I am assuming that I am just using the system synthesizer as described in `sequencer = MidiSystem.getSequencer();` while on Android, I am using the built-in `MediaPlayer` class which can easily set the volume with `mediaPlayer.setVolume(volume, volume);` – JoseRivas1998 Oct 01 '17 at 16:57

1 Answers1

0

Sending your own Channel Volume messages is not very useful if the MIDI file already has them.

You could try to send Master Volume messages, if that synthesizer supports them.

The Java synthesizer does not have a volume controller separate from its MIDI input. If MIDI messages do not work, you have to go directly to the audio device (and there is no good way to find out which one the synthesizer uses).

CL.
  • 173,858
  • 17
  • 217
  • 259