Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

midi

Status
Not open for further replies.

misternopps

Programmer
May 22, 2011
1
NZ
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!
 
Hi misternopps,

Midi is a strange beast. It's more about controlling messages to sound devices than the sound devices themselves. I suggest you read a little more about the Java Midi classes and how they relate to each other, to help understand what I will show below.

First you need a synthesizer object to receive messages from your sequencer. This synthesizer also needs to have some instruments loaded to actually play the sounds.

The following code snippit should help

System.out.println("entering play()");
System.out.println("Initialising variables...");
Sequencer player = MidiSystem.getSequencer();
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
Transmitter playerTx = player.getTransmitter();
Receiver synthRx = synth.getReceiver();
playerTx.setReceiver(synthRx);
Soundbank sb = synth.getDefaultSoundbank();

Instrument[] instr = sb.getInstruments();
System.out.println("We have "+instr.length+" instruments available");
synth.loadInstrument(instr[instrument]);

You also need to wait a little after starting the sequencer, otherwise it will not have time to actually do anything before you stop it. A quick hack would be the following:

player.open();
player.start();
try { Thread.sleep(2000); }
catch(InterruptedException e) {}

Finally, instrument 123 did not make much noise on my machine (OSX Darwin Kernel Version 11.0.1), but 23 did. Try playing with the numbers, or use the Soundbank.getInstruments() method to show which ones are available.

Cheers,
Scott



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top