This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problem in MIDI music file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in MIDI music file " Watch "problem in MIDI music file " New topic
Author

problem in MIDI music file

parthiban rajendran
Greenhorn

Joined: Oct 05, 2005
Posts: 8
hello friends,
i created a program which is expected to play a MIDI song, continuously by continuous event handling. everytime the track is finished, the sequencer object will fire a event, which will be handled by a method called meta(). the problem is the event is fired so fast that the song is not being played at all from second time. the execution of the following program may explain better.

import javax.sound.midi.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


class Orchestra implements MetaEventListener
{

Sequencer player;// CD player
Sequence seq;// CD
Track track;// track in CD


int[] instruments = {35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63};




public void buildTrackandStart()
{

try
{

//create the CD player and switch it on
player = MidiSystem.getSequencer();
player.open();


//registering for event handling
player.addMetaEventListener(this);



//create the CD
seq = new Sequence(Sequence.PPQ,4);



//create the track 'in the CD'
track = seq.createTrack();



//-------- SONG CREATION ----------------------
for(int i=0; i < 16;i++)
{
track.add(makeEvent(144,9,instruments[i],100,i));
track.add(makeEvent(128,9,instruments[i],100,i+2));
}
//-------- end of SONG CREATION ---------------



//making an event explicitly
track.add(makeEvent(192,9,1,0,15));


//put the CD in the CD player
player.setSequence(seq);


//push the play button in the CD player
player.start();





}
catch(Exception ex)
{
ex.printStackTrace();
}

}

public MidiEvent makeEvent(int command,int channel,int note,int velocity,int beat_timing)
{
MidiEvent event = null;
try
{
ShortMessage a = new ShortMessage();
a.setMessage(command,channel,note,velocity);
event = new MidiEvent(a,beat_timing);
}
catch(Exception ex)
{

}
return event;
}


public void meta(MetaMessage message)
{
System.out.println("hello");
player.start()
}

}


public class testMusicVer42a
{
public static void main(String args[])
{

Orchestra troop1 = new Orchestra();
troop1.buildTrackandStart();


}


}

when it runs, it is supposed to play the sequence continuously but it is not. what is the problem in this code? please help me friends. kindly reply at the earliest.
[ October 17, 2005: Message edited by: parthiban rajendran ]

i love 'drinking' java
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It looks like there are many MetaMessage types. When messages come in try displaying the message type. Remove the start() call for now to reduce confusion. I haven't seen a description of what the types mean anywhere, but you may be able to figure out one of the last few is when the sequencer ends.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem in MIDI music file
 
Similar Threads
Java sounds not working!
Head First Java Chapter 11, example on page 346
Head First Java - Chapter 11 - MiniMusicCmdLine Error
MidiSystem.getSequencer (locks up)
midi player when used from commandline does not work