This is a continuation of the previous article (https://qiita.com/absol_abcX/items/e9c230b808e522154ae5).
Implement the MIDI synthesizer selection function to be used in the previous MIDI file playback sample. Below is the actual execution screen.
[0] Gervill
[1] LoopBe Internal MIDI
[2] Microsoft MIDI Mapper
[3] CoolSoft MIDIMapper
[4] Microsoft GS Wavetable Synth
[5] VirtualMIDISynth #1
[6] LoopBe Internal MIDI
[7] Real Time Sequencer
Enter the number of the MIDI device to use>> 4
[Microsoft GS Wavetable Synth]Is open...
MIDI file path>> sample.mid
Playing...
Enter an appropriate character string to finish.
>>a
Stop
By the way, The 0th device called "Gervill" is a MIDI synthesizer provided by the Java library. By default, this device is specified.
The "Microsoft GS Wavetable Synth" used this time is a synthesizer that comes standard with Windows.
SamplePlayer.java
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class SamplePlayer {
public static void main(String[] args) {
//Hardware to play MIDI data/An instance of a software device.
Sequencer sequencer = null;
Scanner scanner = new Scanner(System.in);
try {
//Get a Sequencer instance.
sequencer = MidiSystem.getSequencer(false);
//Open the device and acquire resources.
sequencer.open();
//Get a list of available MIDI devices.
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i=0; i<infos.length; i++) {
String line = String.format("[%s] %s", i, infos[i].getName());
System.out.println(line);
}
System.out.println();
int index = 0;
while (true) {
//Get the index of the MIDI device to use from the console input.
System.out.print("Enter the number of the MIDI device to use>> ");
String str = scanner.next();
try {
index = Integer.parseInt(str);
}
catch (NumberFormatException nfe) {
continue;
}
break;
}
//Request a MIDI device with device information that represents the device of interest to the system.
MidiDevice device = MidiSystem.getMidiDevice(infos[index]);
System.out.println("[" + device.getDeviceInfo().getName() + "]Is open...");
//Open a MIDI device.
if (device.isOpen() == false) {
device.open();
}
//Connect the MIDI OUT of the sequencer to the MIDI IN of the MIDI device.
Receiver receiver = device.getReceiver();
sequencer.getTransmitter().setReceiver(receiver);
}
catch (MidiUnavailableException e) {
e.printStackTrace();
}
System.out.println();
//Get the MIDI file path from the console input.
System.out.print("MIDI file path>> ");
String path = scanner.next();
try {
//MIDI data from MIDI files(Sequence object)Get.
File file = new File(path);
Sequence sequence = MidiSystem.getSequence(file);
//Set the acquired MIDI data in the sequencer.
sequencer.setSequence(sequence);
}
catch (InvalidMidiDataException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//Sequencer playback
sequencer.start();
System.out.println("Playing...");
//Waiting for key input
System.out.println("Enter an appropriate character string to finish.");
System.out.print(">>");
scanner.next();
scanner.close();
//Sequencer stop
sequencer.stop();
System.out.println("Stop");
//Close the sequencer and release the resources you were using.
sequencer.close();
}
}
Gets an object array of all available MIDI device information.
//Get a list of available MIDI devices.
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
You can get the corresponding device by calling getMidiDevice using the obtained device information object. You can operate it by calling open on the acquired device.
//Request a MIDI device with device information that represents the device of interest to the system.
MidiDevice device = MidiSystem.getMidiDevice(infos[index]);
//Open a MIDI device.
if (device.isOpen() == false) {
device.open();
}
There are two interfaces provided by the MIDI device class:
Interface name | Overview | Commonly used |
---|---|---|
Transmitter | Send MIDI data. | Sequencer, MIDI input device, etc. |
Receiver | Receive MIDI data. | Synthesizer / MIDI output device |
This time, connect the MIDI receiver port (Receiver) of the specified MIDI device to the MIDI transmit port (Transmitter) of the sequencer.
//Connect the MIDI OUT of the sequencer to the MIDI IN of the MIDI device.
Receiver receiver = device.getReceiver();
sequencer.getTransmitter().setReceiver(receiver);
Then, the configuration image will look like this.
This completes the device connection.
Recommended Posts