I've just coded the litter music player. Each time we give it 2 ints: "Instrument" and "Node", It'll play the node of that instrument. Very simple. I tried that but even if I changed the "instrument" (changed the int in input), only the piano sound note plays.. The code is below:
package edu.swing.beatbox;
import java.util.Scanner;
import javax.sound.midi.*;
public class MiniMusic {
public void play(int instrument, int note) {
try {
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
MidiEvent event = null;
ShortMessage first = new ShortMessage();
first.setMessage(192, 1, instrument, 0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
ShortMessage a = new ShortMessage();
a.setMessage(144, 1, note, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
a.setMessage(128, 1, note, 100);
MidiEvent noteOff = new MidiEvent(b, 3);
track.add(noteOff);
player.setSequence(seq);
player.start();
} catch (Exception exc) {
exc.printStackTrace();
}
}
public static void main(String[] args) {
int instrument, note;
MiniMusic mini = new MiniMusic();
while (true) {
Scanner inp = new Scanner(System.in);
System.out.print("Instrument (0 --> 127) : "); instrument = inp.nextInt();
System.out.print("Note (0 --> 127) : "); note = inp.nextInt();
mini.play(instrument, note);
}
}
}