package MiniMusicPlayer;
import javax.sound.midi.ControllerEventListener;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;
import javax.swing.JFrame;
public class Player implements ControllerEventListener
{
static JFrame f = new JFrame("My First Music Video"); //Activate a new JFrame with a title
static MyDrawPanel ml; //Make a new drawing panel of the class that we created
public static void main(String[] args)
{
Player mini = new Player(); //Create a new Player class
mini.Go(); //Activate Go method from within the new player object
}
public void setUpGui()
{
ml = new MyDrawPanel(); //Set a new DrawPanel to the one we staticly made
f.setContentPane(ml) ; //Set the JFrame to the new MyDrawPanel object
f.setBounds(30,30,300,300); //Set the size of the draw panel
f.setVisible(true); //Set it to be visible
}
public void Go()
{
setUpGui(); //Call our setUpGui method
try
{
Sequencer sequencer = MidiSystem.getSequencer(); //Create a new midi system sequencer
sequencer.open(); //Open up the sequencer object we created
sequencer.addControllerEventListener(ml, new int[] {127});
//Add a control event listener using our ml object
Sequence seq = new Sequence(Sequence.PPQ, 4); //Create a new sequence with a type and res
Track track = seq.createTrack(); //Set the track to our new track on seq
int r = 0;
for (int i = 5; i < 61; i += 4)
{
r = (int) ((Math.random() * 50) + 1);
//Picking up the beats and adding our events to the track
//Helps to add our rectangles along with the beat itself
track.add(makeEvent(144, 1, r, 100,i));
track.add(makeEvent(176, 1, 127, 0, i));
track.add(makeEvent(128, 1, r, 100, i + 2));
}
//Set the sequencer's sequence to the Sequence object we created
sequencer.setSequence(seq);
//Set the tempo to beats per minute
sequencer.setTempoInBPM(220);
//Start our sequencer
sequencer.start();
}
catch (Exception ex) //Catch any exceptions and print the stack trace
{
ex.printStackTrace();
}
}
@Override
public void controlChange(ShortMessage event)
{
System.out.println("la"); //Each time we got an event we print this
}
public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick)
{
MidiEvent event = null; //Create a midi event and set it to null
try
{
ShortMessage a = new ShortMessage(); //Create a new short message
a.setMessage(comd, chan, one, two); //Set the message according to what's passed in
event = new MidiEvent(a, tick); //Set the MidiEvent to contain a tick as well as our
//short message
}
catch(Exception e)
{
}
return event;
}
}