Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Midi file to midi event

import javax.sound.midi.*;
import javax.swing.*;
import java.awt.*;

/**
 * Created by Jonik on 09.01.2015.
 */
public class MiniMusicPlayer1 {
    static JFrame f = new JFrame("My first video clip");
    static MyDrawPanel ml;
    int t = 2;

    public static void main(String[] args) {
        MiniMusicPlayer1 mini = new MiniMusicPlayer1();
        mini.go();
    }
    public void setUpGui(){
        ml = new MyDrawPanel();
        f.setContentPane(ml);
        f.setBounds(30,30,300,300);
        f.setVisible(true);
    }
    public void go(){
        setUpGui();

        try {
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();
            sequencer.addControllerEventListener(ml, new int[] {127});
            Sequence seq = new Sequence(Sequence.PPQ, 4);
            Track track = seq.createTrack();

            int r =0;
            for (int i = 0; i < 60; i+=4){

                r = (int)((Math.random() * 50) +1);
                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));
            }

            sequencer.setSequence(seq);
            sequencer.start();
            sequencer.setTempoInBPM(120);
        } catch (Exception ex){ ex.printStackTrace();}
    }
    public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick){
        MidiEvent event = null;
        try {
            ShortMessage a = new ShortMessage();
            a.setMessage(comd, chan, one, two);
            event = new MidiEvent(a, tick);
        } catch (Exception ex) {}
        return event;
    }
    public class MyDrawPanel extends JPanel implements ControllerEventListener {
        boolean msg = false;
        public void controlChange(ShortMessage event){
            msg = true;
            repaint();
        }
        public void paintComponent(Graphics g){
            if(msg){

                Graphics2D g2 = (Graphics2D) g;

                int r = (int) (Math.random() * 250);
                int gr = (int) (Math.random() * 250);
                int b = (int) (Math.random() * 250);

                g.setColor(new Color(r,gr,b));

                int ht =(int) ((Math.random()* 120)+ 10);
                int width =(int) ((Math.random()* 120)+ 10);
                int x =(int) ((Math.random()* 40)+ 10);
                int y =(int) ((Math.random()* 40)+ 10);

                if (t%2== 0)
                {
                    g.fillOval(x,y,ht, width);

                }
                else
                {
                    g.fillRect(x,y,ht, width);

                }
                t++;
                msg = false;
            }
        }
    }

}

This code makes random graphics for each midi event, i have to set up midi Notes here, how i can just import midi file and make it draw graphics according to imported midi file. How to convert midi file to short messages?

like image 459
Jonik Avatar asked Jan 24 '26 00:01

Jonik


1 Answers

I'm taking this straight from the docs:

Sequence sequence = MidiSystem.getSequence(...); // either a File or InputStream
Track track0 = sequence.getTracks[0];
for (int i = 0 ; i < track0.size() ; i++)
{
    MidiEvent event = track0.get(i);
    // do your processing here.
}

If your trying to play it back in time you could do something like this:

Sequence sequence = MidiSystem.getSequence(...); // either a File or InputStream
sequencer.setSequence(sequence);
sequencer.start();
like image 91
jaket Avatar answered Jan 26 '26 15:01

jaket



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!