Library tutorials & articles

Midi Synthesis in Java

Introduction

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!

Comments

  1. 07 Apr 2006 at 22:20

    I am writing a simple program; the user will select instrument (from a drop-down menu), pitch and delay for the tone, and press the Play button. I understand playing sounds. But i don't understand, which of the 16 channels should I select. When calling the Synthesiser.loadInstrument(Instrument i), method, I don't tell the Synthesiser to which channel should it load the specified Instrument. So I don't know, where the Instrument is loaded.

  2. 18 Aug 2004 at 13:51

    Below is the complete code to play a scale.



    import java.util.;
    import javax.sound.midi.
    ;


    class SoundTest
    {
       public static void main(String[] args) {
           try {
               Synthesizer synth = MidiSystem.getSynthesizer();
               synth.open();
           
               final MidiChannel[] mc    = synth.getChannels();
               Instrument[]        instr = synth.getDefaultSoundbank().getInstruments();
               synth.loadInstrument(instr[76]);  // Bottle Blow
               for (int i = 0; i < 120; ++i) {
                   try {
                       Thread.sleep(100);
                   } catch (InterruptedException e) {}
                   mc[4].noteOn(i,200);
               }
           } catch (MidiUnavailableException e) {}
       }
    }


  3. 28 Jan 2004 at 00:17

    Hi


    I have successfully synthesized note using midi/java. now I want to save a particular sequence as a midi file and also retrieve the sequence when needed... How do I do that????


    Thanking You


    Turtle

  4. 09 Sep 2002 at 11:52

    Thanks, I might just do that. I found your article on a search for Java MIDI example code so I haven't been through the site yet. I do c# and ASP primarily but I really love Java (and I'm Sun Certified ), so I may well try to explain the things I've learnt doing this project here. I always quite fancied tutorials and teaching, and, as you have no doubt already found, information on the Java MIDI subject is pretty thin on the ground. It'll be nice to do something to change that when I finally get some free time! I shall have to check out how to submit tutes. Watch this space
    L

  5. 08 Sep 2002 at 20:32

    I may or may not try Sequencing, it depends if I find a tutorial on it.


    The reason I wrote this tutorial on Synthesizing is because I wanted to do it but was unable to find a tutorial. I figured out all that's in this document from reading the online java documentation of package java.sound.midi .


    So there probably are plenty of errors in the article, including some silly mistakes and typos which'll be fixed when I have some time on my hands.


    I wrote this basically so that the typical guy can pick it up make his speakers something a little more than useless hardware in a Java program and can do more than Toolkit.getDefaultToolkit().beep().


    writing programing articles is one of my favorite things to do next to programing itself.


    You seem like a knowledgable Java guy. You should help get some Java content on DeveloperFusion as there is currently a lack. I have an upcoming
    Swing tutorial, but that and this one I believe are the only Java articles.


    Any thoughts,


    - Mike H



    - "I need a life... No make that a better one..."
    - "I hate writing, and yet I write programing articles.. wierd..."
    - Me


  6. 08 Sep 2002 at 16:56

    I'm actually in the middle of coding a Java MIDI app on a contract, and to be honest, I haven't used the noteOn and noteOff methods, I have been building Sequences using ShortMessages and MetaMessages. It's a bit more involved and requires you to know a bit more about MIDI, but as long as you put JMF2.1 or above into your classpath, its pretty good, and I've had no problem with timing yet, which was always going to be tricky in Java's loosely scheduled environment. You will definitely notice the difference without JMF 2.1. But it's pretty rewarding when it works!


    have fun.
    Leon

  7. 08 Sep 2002 at 10:57

    From what I had read of the Java MIDI package documentation,
    I guess I didn't understand that noteOff is as important as it is.


    There are about 3 noteOff methods that I've found so far:


    allNotesOff()      // turns off all the notes
    noteOff(int notenumber)    // turns off the specific note.
    noteOff(int notenumber, int velocity)   // what is velocity for when you're
    // turning the note off?


    wow, thanks for all that info
    I haven't read the MIDI spec.


    This tutorial, I think I mean't it to be about
    of how to generate simple tones, not playing
    music.


    Sound is always the hardest thing for me to do on any platform.
    So far the only 2 platforms I can do simple sound effects with are:
    16bit DOS assembly
    Java


    thanks again for all that info,
    maybe I'll go further with MIDI.


    - Mike H

  8. 05 Sep 2002 at 15:20

    You need noteOff messages to close the note, otherwise it is strictly analogous to holding down the piano key after the note has rung out.


    MIDI has no concept of the sound having finished, that is a case for the synthesizer to deal with. A clever synthesizer may send the noteOff for you but you cannot rely on that. Try using a Sweep Pad or Seashore sound to appreciate the need for noteOff. MIDI itself does not know why these are different to the standard piano on patch 1. Synthesizers send noteOff messages when you take your finger off the key. The response to this will depend on the ADSR envelope of the sound. Also, the General MIDI specification requires drum notes to have a length of 1/32nd note (I think), and you will need noteOffs to achieve this.


    You will find when writing a more complex piece that if you do not use noteOff messages to close the noteOn you have opened, you will fill the synthesizer and no more noteOn's will sound until you send some noteOffs.


    If you are planning to program for MIDI, it will be worth your while investigating the MIDI 1.0 Specification. You will find out useful things like the fact that Velocity (how hard you strike the note) can only range from 0 to 127, and that Midi messages are organised in unsigned bytes. Java uses signed bytes so you will also need to compensate for this.


    hope this helps

  9. 01 Jan 1999 at 00:00

    This thread is for discussions of Midi Synthesis in Java.

Leave a comment

Sign in or Join us (it's free).

Michael H

Related podcasts

  • Java Posse #213 - Newscast for Oct 23rd 2008

    Newscast for Oct 23rd 2008 Fully formatted shownotes can always be found at http://javaposse.com The Android project has been released as open source, beating the rumored launch date for the source code by several months http://source.android.com/ And, Gizmodo and ZDNet both offer in-depth ...

Events coming up

  • May 19

    Google I/O 2010

    San Francisco, United States

    Google's largest developer event returns to San Francisco in 2010. Google I/O brings together thousands of developers for two days of highly technical content, focused on pushing the boundaries of web applications through open web technologies and Google developer products like App Engine, Google Web Toolkit, Android, Chrome, APIs, and more. Early registration for Google I/O will open in January 2010.

We'd love to hear what you think! Submit ideas or give us feedback