Java's Midi package is javax.sound.midi, unlike some of the other media packages like the Java3D one, javax.media.j3d, it isn't under the javax.media namespace. This tutorial is specific to Midi Synthesis, so if you just want to know how to play a WAV File, this ain't for you. If in fact you just want to play a file, goto Java's Site and look for the info, it is there. Anyway, Java makes it rather easy compared to some other languages to do Midi Synthesis. The code to make a simple sound when a Swing JButton is pressed is about oh, 12-15 lines. That's about the size of an equally simple Java3D program. Also, most of those lines just set-up the synthesizer and midi channels and instruments. But enough blabbing, let's see some code!
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: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.public static void main(String[] args) { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open();
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.The Swing Window
In this section we construct a simple Swing JFrame with a single JButton on it. Simple stuff so far, eh?
Now, for the Swing window, I'm just going to give you the 8 line chunk of code that creates a JFrame and puts a JButton inside. This tutorial will NOT explain how to make JFC/Swing GUI applications, I'm only 15 and I know Swing fairly well, I'm sure you can too. Here it is:JFrame frame = new JFrame("Sound1"); JPanel pane = new JPanel(); JButton button1 = new JButton("Click me!"); frame.getContentPane().add(pane); pane.add(button1); frame.pack(); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.show();
That makes the window and shows it. That also is the end of this section.Making a Sound
In this section we make an ActionListener for our JButton and put inside ONE (1) line of code to make a sound. Here's the start of the JButton's ActionListener as usual this is pretty straight forward:
button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
Now, ladies and gentleman the code you've all been waiting for:mc[5].noteOn(60,600);
That makes the sound in an arbitrary MidiChannel that I picked (THE STANDARD AMOUNT OF MIDI CHANNELS IS 16 SO YA KNOW!) I don't know the difference of the Midi Channels. To make the sound we just turn on a note with the noteOn method. The 60 is the note number (middle C) and 600 is how hard we hit the piano key or plucked the instrument's string (you get the idea). THERE IS A noteOff METHOD BUT AS FAR AS I CAN TELL THE SOUND DIES OFF ALL BY ITSELF!!! Here's some finish up code:}}); // END OF THE ACTION LISTENER } // END OF THE MAIN METHOD
Isn't that neet? Here's some thing to try:I hope you have fun, NOTE: If you like this let me know and I'll learn somemore to teach ya!
Change the Instrument number. Change the Midi Channel number. Check the javax.sound.midi Docs