Java Midi
I'm a musician in my spare time. When I started programming I wanted to make music with code. I managed to produce some notes through the java Synthesizer class. So I thought it would be fun to revisit it after working as a software engineer for almost a year.
I started with the same set of websites, with StackOverflow as hub to more resources. It's amazing to see the paralels between midi and present day async web messaging. Especially if you realize that MIDI was standardized in 1983, :D.
MIDI basically works through a MidiEvents triggering sounds on a MidiSynthesizer. A MidiFile is thus a Sequence of MidiEvents that you can play through a Sequencer on a Synthesizer. This is reflected in the data structure of the relevant classes.
Here is some code to get started:
I started with the same set of websites, with StackOverflow as hub to more resources. It's amazing to see the paralels between midi and present day async web messaging. Especially if you realize that MIDI was standardized in 1983, :D.
MIDI basically works through a MidiEvents triggering sounds on a MidiSynthesizer. A MidiFile is thus a Sequence of MidiEvents that you can play through a Sequencer on a Synthesizer. This is reflected in the data structure of the relevant classes.
Here is some code to get started:
/* Create a new Sythesizer and open it. Most of * the methods you will want to use to expand on this * example can be found in the Java documentation here: * https://docs.oracle.com/javase/7/docs/api/javax/sound/midi/Synthesizer.html */ Synthesizer midiSynth = MidiSystem.getSynthesizer(); List<MidiChannel> channels = Arrays.asList(midiSynth.getChannels()); //16 channels, as full System.out.println("channels: " + channels.size()); // can play 64 notes at the same time System.out.println("max polyphony: " + midiSynth.getMaxPolyphony()); //get and load default instrument and channel lists List<Instrument> instruments = Arrays.asList(midiSynth.getDefaultSoundbank() .getInstruments()); for(Instrument instrument: instruments) { System.out.printf("bank: %d, program: %d, name: %s \n",instrument.getPatch().getBank(), instrument.getPatch().getProgram(), instrument.getName()); } midiSynth.open(); PlayableNote playableNote = new PlayableNote(60, 100, 1000); //load an instrument midiSynth.loadInstrument(instruments.get(0)); //playNoteOnChannel(channels.get(0), playableNote); midiSynth.close(); //TODO feed the sequencer a Midi file and play it on the default synthesizer Anchor anchor = new Anchor(); File file = new File(anchor.getProjectRoot() + "/src/main/resources/pokemon_battle.mid"); System.out.println("got test midi? " + file.isFile()); Sequence sequence = MidiSystem.getSequence(file); Sequencer sequencer = MidiSystem.getSequencer(); sequencer.setSequence(sequence); List<Track> tracks = Arrays.asList(sequence.getTracks()); for (Track track : tracks) { System.out.println("\nTrack: " + track.toString()); for (int i = 0; i < track.size(); i++) { MidiEvent event = track.get(i);
//https://github.com/Uzebox/uzebox/blob/master/tools/JavaTools/src/org/uzebox/tools/converters/midi/MessageInfo.java System.out.println(" tick " + event.getTick() + ", "
+ MessageInfo.toString(event.getMessage())); } } // yep this plays a midi file :D sequencer.open(); sequencer.start(); sleep(sequence.getMicrosecondLength() / 1000); sequencer.stop(); sequencer.close();
Comments
Post a Comment