Midi Synthesis in Java

The Synthesizer Object

The Synthesizer Object is a public interface which means that it can't be instanced with the new keyword. I put the code in the main method so you can copy/paste it into what ever file/class name ya want. To get the Synthesizer and open it, you have to use a few lines of code like this:

public static void main(String[] args) {
    Synthesizer synth = MidiSystem.getSynthesizer();
    synth.open();


MidiSystem is one of those "Don't need to instance it" objects, and here I use MidiSystem to get a fully working (almost) synthesizer. After we get the Synthesizer we need to open, why? I dunno, but you have to or it won't work!

Next, we get an array of MidiChannels which is what we'll actually use later to make a sound.

final MidiChannel[] mc = syn.getChannels();

I declare mc as final because we need to be able to access it without problems in the JButton's ActionListener later. So far this is straight forward and I think it doesn't get much harder than a bunch of method calls with several objects and interfaces.

Now, we want an array of Instruments to choose from. To do this we have to get a Soundbank object, I just use the default one with this line o' code:

Instrument[] instr = syn.getDefaultSoundbank().getInstruments();

Instruments are used to choose what instrument the sound is played with.

syn.loadInstrument(instr[90]);

Here I load into our Synthesizer, an arbitrary instrument from the Instrument array that we just made.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold