Java Sound API #3 - MIDI player

Java Sound API supports not only digital audio but also MIDI. Here is a simple MIDI player with the default Sequencer and Synthesizer.

% cat MidiPlayer3.java

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Synthesizer;
import java.io.File;

public class MidiPlayer3 {

  public static void main(String[] args) throws Exception {

    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();

    Synthesizer synthesizer = MidiSystem.getSynthesizer();
    synthesizer.open();

    sequencer.setSequence(MidiSystem.getSequence(new File(args[0])));
    sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());

    sequencer.start();
  }
}

% javac MidiPlayer3.java
% java MidiPlayer3 Kashmir.mid

But, the soundbank bundled with JDK is minimal, so I recommend to get the deluxe one from SDN.

% unzip soundbank-deluxe.gm.zip
# cp soundbank-deluxe.gm ${JAVA_HOME}/jre/lib/audio
Comments:

Post a Comment:
Comments are closed for this entry.
About

kashmir

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today