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;
}
}
}
}
这段代码为每个midi事件制作随机图形,我必须在这里设置midi Notes,如何导入midi文件并根据导入的midi文件绘制图形。如何将midi文件转换为短消息?
答案 0 :(得分:3)
我直接从文档中接受这一点:
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.
}
如果您试图及时播放,可以执行以下操作:
Sequence sequence = MidiSystem.getSequence(...); // either a File or InputStream
sequencer.setSequence(sequence);
sequencer.start();
答案 1 :(得分:0)
您可以尝试JFugue。它的官方目的与音乐创作/播放有关,但它确实有一个MIDI阅读库,不仅可以收集短信,还可以将它们转换成更易读的格式。
您应该能够编写自己的“渲染器”,将相关值(注释等)放入按时间索引的列表中,然后重新播放。