This is the main class of the entire program. Without this class the synth couldn’t function because this is where we initialize the MIDI device and instruments that make up the synthesizer. This is were most of the calls to the Java Sound API are made. There are a couple other places, but without this class those calls would be meaningless. So lets walk through the code below. The most important part of the MidiSetup class is this little snippet of code.
// initialize the midi synthesizer
MidiSetup(){
try {
synth = MidiSystem.getSynthesizer();
synth.open();
mc = synth.getChannels();
instr = synth.getDefaultSoundbank().getInstruments();
synth.loadInstrument(instr[0]);
} catch (MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above method creates the Java synthesizer midi object. The snippet is pretty straight forward. Create the synthesizer, then open the synth. Once opened make a call to get the available midi channels. This call returns an array of available channels. According to the Java documentation the MIDI 1.0 specification allows for up to 16 channels. Once the array of channels is obtained, the synth makes a call to get the default sound bank that is part of the Java Sound API, for more information follow this link.
private Synthesizer synth;
private MidiChannel[] mc;
We can see that there is a private synthesizer variable that is protected in this class, and a lot of the methods in the class are just getter and setter methods for both the private Synthesizer and MidiChannel variables shown above. All the previous methods we talked about rely on these two variables. Below is the full MidiSetup class code.
import javax.sound.midi.*;
import javax.sound.midi.MidiDevice.Info;
/**
* @author Anthony Harrell
* @author Santa Clara University
* @version 3/2015
*/
public class MidiSetup {
private Synthesizer synth;
private MidiChannel[] mc;
Instrument[] instr;
// initialize the midi synthesizer
MidiSetup(){
try {
synth = MidiSystem.getSynthesizer();
synth.open();
mc = synth.getChannels();
instr = synth.getDefaultSoundbank().getInstruments();
synth.loadInstrument(instr[0]);
} catch (MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// load requested instrument in the synthesizer
// unload previous instrument from the synthesizer
public void setInstrument(int nInstr){
int p = mc[1].getProgram();
synth.unloadInstrument(instr[p]);
synth.loadInstrument(instr[nInstr]);
mc[1].programChange(nInstr);
mc[0].programChange(nInstr);
}
// return currently active midi channel
public MidiChannel[] getMidiChannel(){
return this.mc;
}
// returns current synthesizer loaded
public Synthesizer getSynth(){
return this.synth;
}
// returns current instrument loaded
public Instrument[] getInstrument(){
return this.instr;
}
// create array of instrument names for user selection
public String[] getInstrNames(){
String[] instrArray = new String[this.instr.length];
for(int i=0; i < this.instr.length; i++){
instrArray[i] = this.instr[i].getName();
}
return instrArray;
}
/* helper function to get midi information
* only for debugging purposes
* should remove if ever released */
public void getMidiInfo(){
Info[] info = MidiSystem.getMidiDeviceInfo();
try {
for(int i=0; i < info.length; i++){
System.out.println(MidiSystem.getMidiDevice(info[i]));
//System.out.println(info[i]);
}
} catch (MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}