gpt4 book ai didi

java - MIDI 文件到 MIDI 事件

转载 作者:行者123 更新时间:2023-11-30 08:17:58 28 4
gpt4 key购买 nike

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 注释,我如何才能只导入 midi 文件并使其根据导入的 midi 文件绘制图形。如何将midi文件转成短信?

最佳答案

我直接从文档中获取:

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();

关于java - MIDI 文件到 MIDI 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870826/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com