まぁ、なんとなくMIDIデータ作って再生、保存するプログラムを作りたいんですよ。例外処理とかとりあえず考えずになんとなく作ってみたわけですよ。これから頑張る
import javax.sound.midi.Track; import javax.sound.midi.Receiver; import javax.sound.midi.Sequence; import javax.sound.midi.Sequencer; import javax.sound.midi.MidiEvent; import javax.sound.midi.MidiSystem; import javax.sound.midi.MidiMessage; import javax.sound.midi.ShortMessage; import java.io.IOException; import javax.sound.midi.InvalidMidiDataException; import javax.sound.midi.MidiUnavailableException; public class MidiTest{ public static void main(String[] args) throws IOException, InvalidMidiDataException, MidiUnavailableException, InterruptedException{ Sequence sequence = new Sequence(Sequence.PPQ, 24); Track track = sequence.createTrack(); MidiTest.addProgramChange(track, 49); MidiTest.addNoteOn(track, 60, 127, 0); MidiTest.addNoteOff(track, 60, 127, 24); Sequencer sequencer = MidiTest.startSequence(sequence); Thread.sleep(1000); sequencer.close(); } private static Sequencer startSequence(Sequence sequence) throws InvalidMidiDataException, MidiUnavailableException{ Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); sequencer.start(); return sequencer; } private static void addNoteOn(Track track, int pitch, int velocity, int start) throws InvalidMidiDataException{ track.add(new MidiEvent(MidiTest.createNoteOn(pitch, velocity), start)); } private static void addNoteOff(Track track, int pitch, int velocity, int start) throws InvalidMidiDataException{ track.add(new MidiEvent(MidiTest.createNoteOff(pitch, velocity), start)); } private static void addProgramChange(Track track, int program) throws InvalidMidiDataException{ track.add(new MidiEvent(MidiTest.createProgramChange(program), 0)); } private static MidiMessage createNoteOn(int pitch, int velocity) throws InvalidMidiDataException{ ShortMessage message = new ShortMessage(); message.setMessage(ShortMessage.NOTE_ON, pitch, velocity); return message; } private static MidiMessage createNoteOff(int pitch, int velocity) throws InvalidMidiDataException{ ShortMessage message = new ShortMessage(); message.setMessage(ShortMessage.NOTE_OFF, pitch, velocity); return message; } private static MidiMessage createProgramChange(int program) throws InvalidMidiDataException{ ShortMessage message = new ShortMessage(); message.setMessage(ShortMessage.PROGRAM_CHANGE, program, 0); return message; } }