jMusic Applets
Java is well known as a language for the internet, in particular it's ability to write small application (applets) that function in a web browser. So it makes sence that jMusic should be able to  add value to that, and indeed it can. However, because of the limited MIDI and audio support in Applets and browsers in general, musical results in Applets are not always optimal. Despite this, the ability to add interactive music to web pages with jMusic is an exciting prospect and one we will explore through this tutorial.

The applet used for this tutorial should appear below.

The yellow area above is the applet frame.
Click here to view the Applet source

Click here to view or download the HTML source

Let's have a closer look at the HTML file.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Audio Applet</title>
    
  <meta http-equiv="content-type"
 content="text/html; charset=ISO-8859-1">
</head>
<body>

<p align="center">
<applet
    archive="./jmusic.jar"
    code="jMusicApplet.class"
    width="100" height="50"
     alt="Your browser does not have Java enabled :(">
</applet><br>

</p>
     
</body>
</html>

The important stuff in the HTML code above is between the <applet> tags. The location of the class file that is the compiled applet is passed as the 'code' argument. Note that in this case the class file is assumed to be in the same location as the html file. Importantly, you need to tell the applet where to access the jMusic classes. The easiest way is to copy the jmusic.jar file and put it in the same folder as the html and class file, then indicate the location as shown as the 'archive' argument. The size (width and height0 can be wahtever is necessary and will define the amount of screen realestate the applet occupies. The string argument for 'alt' is the text that will display inthe browser if java is not availible to or supported by that browser.

Let's have a closer look at the Java file.
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.applet.Applet;

import jm.JMC;
import jm.music.data.*;
import jm.util.*;

Import the classes required for the applet and button clicks, as well as the jMusic pakcages required.

public class jMusicApplet extends Applet implements JMC, ActionListener {
private Button playBtn;
private Score score = new Score();

Applets need to extends the Applet class. We implement the JMc to get all the jmusic constants and implement the ActionListener to notify us of the button clicks.

        public void init() {
setBackground(Color.yellow);
playBtn = new Button("Play");
playBtn.addActionListener(this);
this.add(playBtn);
}

public void start() { }

Applets always run an init method then a start method. So these must be implemented. They can bconsidered as similar in function to the contructor methods in a regular Java class. The Applet provides a graphics context and frame by default, so we simply add the other components we want to this instance.

	public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == playBtn) {
compose();
play();
}
}

When we implement the ActionListener interface we must also include the actionPerformed method, which is called whener the button is clicked. If the playBtn button was clicked then the music is composed and then played.

        private void compose() {
int[] pitchSop = {C5, G4, E4, D4, G4, A4,C4,D4,E4,D4,F4,E4,A4,G4,E4};
double[] rhythmSop = {CROTCHET, C,DC,Q,C,C,C,C,C,C,C,C,M,C,C};
Phrase soprano = new Phrase();
soprano.addNoteList(pitchSop, rhythmSop);
this.score = new Score(new Part (soprano));;
this.score.setTempo(130);
}

This method adds specified notes to the score to play a simple melody. See other introductory  tutorials on jMusic is this does not make sense to you.

       private void play() {
          Play.midi(score);
        }

The play method is simple - perhaps it should even be a method, of well - it calls jMusic's Play class to send the score for playback via Java's JavaSound software synthesizer.

Note: You must have Java 1.3 or higher installed for this feature to work. Some browers run earlier version of Java and so there will be no sound output from them.

        public static void main(String[] args) {
Applet theApplet = new jMusicApplet();
Frame theFrame = new Frame();
theFrame.setSize(100,100);
theFrame.add(theApplet);;
theApplet.init();
theApplet.start();
theFrame.setVisible(true);
}

The class has a main method for testing convenience. This simulates the structure provided by the Applet class which means you can test this class an a nornmal java application. The main methed is NOT used when running as a Applet.


jMusic Tutorial Index


© 2003 Andew R Brown