Library tutorials & articles
Midi Synthesis in Java
- Introduction
- The Synthesizer Object
- The Swing Window
- Making a sound
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!
Related articles
Related discussion
-
Java Based Mobile Development Tool Now Supports Wireless PDA Access To SQL Server Databases
by mobiforms (0 replies)
-
Bin Packing
by sc609 (18 replies)
-
The Beauty of Closures
by bdicroce (0 replies)
-
What technology / Framework would you propose
by James Crowley (2 replies)
-
vb.net application inside a chrome-like frame
by BKRoberts (3 replies)
Related podcasts
-
Java Posse #217 - Newscast for Nov 14th 2008
Newscast for November 14th 2008 Fully formatted shownotes can always be found at http://javaposse.com Java Posse Roundup 2009 dates set, March 10th to 13th in Crested Butte, CO with alternative JVM language day on the 9th. http://www.artima.com/weblogs/viewpost.jsp?thread=242122 What Happened...
Related jobs
-
Content Management Consultant
in NETHERLANDS (€50K-€90K per annum) -
Content Manager/ Java Developer
in AMSTERDAM (€50K-€90K per annum) -
Java Developer
in AMSTERDAM (€50K-€90K per annum) -
Java RUP Designer
in AMSTERDAM (€50K-€90K per annum)
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
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.
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) {}
}
}
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
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
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
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
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
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
This thread is for discussions of Midi Synthesis in Java.