Midi Synthesis in Java

Making a sound

In this section we make an ActionListener for our JButton and put inside ONE 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 number 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 also 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:

  • Change the Instrument number.
  • Change the Midi Channel number.
  • Check the javax.sound.midi Docs

    I hope you have fun, Note: If you like this let me know and I'll learn somemore to teach ya!
  • 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.

    “There's no test like production” - Anon