misternopps
Programmer
Hi,
I am trying to run the following code on my mac 0s X 10.4.11, to create and play play midi notes:
import javax.sound.midi.*;
public class Midi1 {
static {
System.out.println("Initialising...");
}
public static void main(String[] args) {
Midi1 mini = new Midi1();
String errorMsg = "Error - Main requires two intigers for input.";
try {
int instrument = 123;
int note = 123;
mini.play(instrument, note);
}
catch(Exception ex){
System.out.println(errorMsg);
}
System.out.println("End of programme");//wrap up
}
public void play(int instrument, int note){
try {
System.out.println("entering play()");
System.out.println("Initialising variables...");
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
MidiEvent event = null;
ShortMessage first = new ShortMessage();
first.setMessage(192, 1, instrument, 0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
ShortMessage a = new ShortMessage();
a.setMessage(144, 1, note, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, note, 100);
MidiEvent noteOff = new MidiEvent(b, 1);
track.add(noteOff);
player.setSequence(seq);
System.out.println("Our control flow has reached open() and start().");
player.open();
player.start();
System.out.println("Now calling open(), start() five times...");
try {
for (int i = 1 ;i < 6; i++)
{
player.open();
player.start();
System.out.print(i + " ");
}
System.out.println("\n");
}
catch(Exception ex)
{
System.out.println("Error Caught while looping. Throwing to play()");
throw(ex);
}
System.out.println("Calling stop() and close() on our midi sequence");
player.stop();
player.close();
System.out.println("Exiting main()...");
}
catch(Exception ex)
{
System.out.println("Error caught in play...");
ex.printStackTrace();
}
}
}
When I run this no error is thrown, but I hear no sound. Is the problem with my own computer, or have I made a mistake in my code?
Help would be very much appreciated.
Thanks!
I am trying to run the following code on my mac 0s X 10.4.11, to create and play play midi notes:
import javax.sound.midi.*;
public class Midi1 {
static {
System.out.println("Initialising...");
}
public static void main(String[] args) {
Midi1 mini = new Midi1();
String errorMsg = "Error - Main requires two intigers for input.";
try {
int instrument = 123;
int note = 123;
mini.play(instrument, note);
}
catch(Exception ex){
System.out.println(errorMsg);
}
System.out.println("End of programme");//wrap up
}
public void play(int instrument, int note){
try {
System.out.println("entering play()");
System.out.println("Initialising variables...");
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
MidiEvent event = null;
ShortMessage first = new ShortMessage();
first.setMessage(192, 1, instrument, 0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
ShortMessage a = new ShortMessage();
a.setMessage(144, 1, note, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, note, 100);
MidiEvent noteOff = new MidiEvent(b, 1);
track.add(noteOff);
player.setSequence(seq);
System.out.println("Our control flow has reached open() and start().");
player.open();
player.start();
System.out.println("Now calling open(), start() five times...");
try {
for (int i = 1 ;i < 6; i++)
{
player.open();
player.start();
System.out.print(i + " ");
}
System.out.println("\n");
}
catch(Exception ex)
{
System.out.println("Error Caught while looping. Throwing to play()");
throw(ex);
}
System.out.println("Calling stop() and close() on our midi sequence");
player.stop();
player.close();
System.out.println("Exiting main()...");
}
catch(Exception ex)
{
System.out.println("Error caught in play...");
ex.printStackTrace();
}
}
}
When I run this no error is thrown, but I hear no sound. Is the problem with my own computer, or have I made a mistake in my code?
Help would be very much appreciated.
Thanks!