/**
* karlmidi.java
*
* Original prototype class which creates one-note MIDI file.
* The pitch, patch, and file name can be set as parameters.
* Note: everything that this "proof of concept" class does
* has since been encapsulated in other classes in this package.
*
* This module is part of Karl Brown's MIDI programming project.
*
*/
package MidiApps;
import java.io.*;
import java.util.*;
import javax.sound.midi.*;
/**
*
* @author Karl Brown
* @version 1.0
* last updated 2/16/2003
*/
public class karlmidi {
static tracer tr;
static int note;
static int patch;
static String name;
public static void setName(String nameIn) {
name = new String(nameIn);
}
public static void setNote(int noteIn) {
note = noteIn;
}
public static void setPatch(int patchIn) {
patch = patchIn;
}
public static void setTracer(tracer trIn) {
tr = trIn;
}
/**
*
* creates a one-note MIDI sequence
* and writes to a standard MIDI file
*/
public static void compose() {
try
{
Sequence s = new Sequence(0,96);
Track t = s.createTrack();
ShortMessage mm = new ShortMessage();
SysexMessage sm = new SysexMessage();
MetaMessage mt = new MetaMessage();
MidiEvent me;
String TrackName;
// General MIDI sysex;
byte[] b = {(byte)0xF0, 0x7E, 0x7F, 0x09, 0x01, (byte)0xF7};
sm = new SysexMessage();
sm.setMessage(b, 6);
me = new MidiEvent(sm,(long)0);
t.add(me);
// set tempo (meta event);
mt = new MetaMessage();
byte[] bt = {0x02, (byte)0xA1, 0x20};
mt.setMessage(0x51 ,bt, 3);
me = new MidiEvent(mt,(long)0);
t.add(me);
// set track name (meta event);
mt = new MetaMessage();
TrackName = new String("One-note MIDI track");
mt.setMessage(0x03 ,TrackName.getBytes(), TrackName.length());
me = new MidiEvent(mt,(long)0);
t.add(me);
// set omni on;
mm = new ShortMessage();
mm.setMessage(0xB0, 0x7D,0x00);
me = new MidiEvent(mm,(long)0);
t.add(me);
// set poly on;
mm = new ShortMessage();
mm.setMessage(0xB0, 0x7F,0x00);
me = new MidiEvent(mm,(long)0);
t.add(me);
// set instrument channel 1;
mm = new ShortMessage();
mm.setMessage(0xC0, (byte)patch, 0x00);
me = new MidiEvent(mm,(long)0);
t.add(me);
// note on;
mm = new ShortMessage();
mm.setMessage(0x90,(byte)note,0x60);
me = new MidiEvent(mm,(long)0);
t.add(me);
// note off;
mm = new ShortMessage();
mm.setMessage(0x80,(byte)note,0x60);
me = new MidiEvent(mm,(long)480);
t.add(me);
// set end of track (meta event) channel 1;
mt = new MetaMessage();
byte[] bet = {};
mt.setMessage(0x2F,bet,0);
me = new MidiEvent(mt, (long)480);
t.add(me);
// String dirname = "C:\\javamidi\\MidiApps\\midifiles"; //test
// String dirname = "C:\\jakarta-tomcat-4.0.4\\webapps\\ROOT\\midifiles"; //test
String dirname = "/home/virtual/site94/fst/var/www/html/midifiles"; //prod
File dir = new File(dirname);
String dirpath = dir.getAbsolutePath();
Calendar rightNow = Calendar.getInstance();
int year = (rightNow.get(rightNow.YEAR) % 100);
String yr;
if (year < 10) {
yr = "0" + String.valueOf(year);
}
else {
yr = String.valueOf(year);
}
int month = (rightNow.get(rightNow.MONTH) + 1);
String mo;
if (year < 10) {
mo = "0" + String.valueOf(month);
}
else {
mo = String.valueOf(month);
}
int day = (rightNow.get(rightNow.DAY_OF_MONTH));
String dy;
if (day < 10) {
dy = "0" + String.valueOf(day);
}
else {
dy = String.valueOf(day);
}
int hour = (rightNow.get(rightNow.HOUR_OF_DAY));
String hr;
if (hour < 10) {
hr = "0" + String.valueOf(hour);
}
else {
hr = String.valueOf(hour);
}
int minute = (rightNow.get(rightNow.MINUTE));
String mn;
if (minute < 10) {
mn = "0" + String.valueOf(minute);
}
else {
mn = String.valueOf(minute);
}
int second = (rightNow.get(rightNow.SECOND));
String sc;
if (second < 10) {
sc = "0" + String.valueOf(second);
}
else {
sc = String.valueOf(second);
}
String ts = yr + mo + dy + hr + mn + sc;
String fname = name + ts + ".mid"; //prod
tr.trace("filename is " + fname + ".
");
String pathname = dirpath + "/" + fname;
File f = new File(pathname);//
String path = f.getAbsolutePath();
MidiSystem.write(s,1,f);
tr.trace("
Here is your MIDI file:
" + fname + "
"); } //try catch(Exception e) { tr.trace("karlmidi::compose Exception caught:"); tr.trace(e.toString()); } //catch } /** * * main() function allows this class to run as a standalone unit * in batch mode */ public static void main(String argv[]) { tracer trc = new tracer(); trc.setPrintStream(System.out); setTracer(trc); tr.trace("karlmidi tracing "); tr.trace("karlmidi main begin "); setName("karl"); setNote((byte)(60)); setPatch((byte)(0)); compose(); tr.trace("karlmidi main ends "); } }