File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in MIDI music file

 
parthiban rajendran
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic