INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

midi

midi

(OP)
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!

RE: midi

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



 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close