JScore's raw text file importer

Introduction

JScore is JMSL's music notation package. JMSL can generate notes and send them to JScore directly. But JScore can import music that was generated by other languages, too. A simple text file that specifies musical content can be parsed by a utility class presented here. This text file could be generated by a C program, a Perl script, a CGI program on the web, or typed in by hand.
 

Usage

To import a textfile into JScore, use the class com.softsynth.jmsl.score.util.RawJScoreFileImporter, passing it the input filename and the output filename.

Example:

java -classpath %CLASSES%\jmsl.jar;%CLASSES%\jscore.jar;%CLASSES%\jsynclasses.jar com.softsynth.jmsl.score.util.RawJScoreFileImporter inputfile.txt outputscore.xml


You can also load the file from a URL and create a JScore directly in an applet (see examples below).

The class responsible for parsing the textfile is called RawJScoreFileParser, which we will use for our examples at the end of this document.

Two ways of working

You may either create a file that adds measures and notes in a free flow style or by specifying measure numbers.

1) In a freeflow style, you specify zero or more MEASURE tags. Specify -1 as the measure number of all NOTE tags. These NOTE's are simply poured into the score in the order they are encountered in the textfile. As notes fill measures, more measures are added.

2) When specifying measure numbers, MEASURE tags and NOTE tags are passed measure numbers. NOTE's will be added to their specified measures. Gaps in the score will be filled in with empty measures.

You may use one method for measures and the other method for notes, but it is not recommended that you mix methods for the same tag.

Comparing both methods

We jump right into showing you examples here, and explain details of tags and parameters later. Notice that measures are numbered beginning with 0, so for example the fourth measure is 3.

EXAMPLE 1, if a score began in 4/4 time and changed to 5/4 time in measure 3,

In both methods above, as notes are added and overflow measure 3, more measures of 5/4 will be added.

EXAMPLE 2, if a score had one whole note in measure 0 and one whole note in measure 2,

TWO COMPLETE EXAMPLES COMPARING METHODS

Example of a freeflow input file that inputs the first 8 notes of a C major scale using quarter notes:
TICKSPERQUARTER 240
MAXVEL 127
STAFFS 1
MEASURE 4 4
NOTE -1 0 240 60 100 0 0 0
NOTE -1 0 240 62 100 0 0 0
NOTE -1 0 240 64 100 0 0 0
NOTE -1 0 240 65 100 0 0 0
NOTE -1 0 240 67 100 0 0 0
NOTE -1 0 240 69 100 0 0 0
NOTE -1 0 240 71 100 0 0 0
NOTE -1 0 240 72 100 0 0 0
Example of the same musical content as above, specifying measure numbers:
TICKSPERQUARTER 240
MAXVEL 127
STAFFS 1
MEASURE 0 4 4
NOTE 0 0 240 60 100 0 0 0
NOTE 0 0 240 62 100 0 0 0
NOTE 0 0 240 64 100 0 0 0
NOTE 0 0 240 65 100 0 0 0
NOTE 1 0 240 67 100 0 0 0
NOTE 1 0 240 69 100 0 0 0
NOTE 1 0 240 71 100 0 0 0
NOTE 1 0 240 72 100 0 0 0

Tags and their parameters

Here we specify tags and their parameters.
 

Examples

RawJScoreFileParser can read from a disk file or from a URL. We use the latter feature to demonstrate some examples (see applet source below).
 

Applet source

package jmslexamples;
import com.softsynth.jmsl.*;
import com.softsynth.jmsl.score.*;
import com.softsynth.jmsl.score.util.*;
import java.net.*;
import java.awt.*;
import java.io.*;

/** Read a URL that points to a raw JScore input text file. Print it to a TextArea, parse it, 
    and open the score in a ScoreFrame
	@author Nick Didkovsky, (c) 2000 Nick Didkovsky, all rights reserved */
public class RawJScoreImportDemo extends java.applet.Applet {

	ScoreFrame scoreFrame;
	TextArea textArea;

	public void init() {
	}

	public void start() {
		try {
			JMSL.setIsApplet(true);
			JMSL.clock = new DefaultMusicClock();
			JMSL.clock.setAdvance(0.1);
			JMSL.scheduler = new EventScheduler();
			JMSL.scheduler.start();
			String urlName = getParameter("URL");
			System.out.println(urlName);
			URL url = new URL(getCodeBase(), urlName);
			System.out.println(url + "");

			add(textArea = new TextArea(40, 40));
			readScoreToTextArea(url);

			// parse the textfile and build a score
			RawJScoreFileParser parser = new RawJScoreFileParser(url);
			RawJScoreFileImporter importer = new RawJScoreFileImporter();
			parser.addRawJScoreFileListener(importer);
			parser.parse();
			parser.close();

			// display it
			scoreFrame = new ScoreFrame();
			scoreFrame.addScore(importer.getScore(800, 600));
			scoreFrame.setSize(850, 600);
			scoreFrame.setVisible(true);

		}
		catch (Exception e) {
			System.out.println("ERR: " + e);
		}
	}

	public void stop() {

		JMSL.closeMusicDevices();
		JMSL.scheduler.stop();
		removeAll();
		if (scoreFrame != null) {
			scoreFrame.setVisible(false);
			scoreFrame.dispose();
			Score.deleteCanvas();
		}
	}
	/** textarea.append() left deprecated to work in all browsers */
	void readScoreToTextArea(URL url) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		textArea.setText("");
		String line = in.readLine();
		while (line != null) {
			textArea.appendText(line + "\n");
			line = in.readLine();
		}
		in.close();
	}

}

More JMSL Demos