2015-01-09 37 views
6
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; 
      } 
     } 
    } 

} 

Mã này làm đồ họa ngẫu nhiên cho mỗi sự kiện midi, tôi phải thiết lập ghi chú ở đây, làm thế nào tôi có thể nhập tập tin midi và làm cho nó vẽ đồ họa theo tập tin midi nhập khẩu. Làm thế nào để chuyển đổi tập tin midi thành tin nhắn ngắn?Tập tin Midi vào sự kiện midi

Trả lời

0

Bạn có thể thử JFugue. Đó là mục đích chính thức là đặc biệt liên quan đến thành phần âm nhạc/chơi, nhưng nó có một thư viện đọc MIDI mà không chỉ tập hợp các tin nhắn ngắn nhưng chuyển đổi chúng thành một định dạng dễ đọc hơn nhiều.

Bạn sẽ có thể viết "trình kết xuất" của riêng bạn để đặt các giá trị có liên quan (ghi chú, v.v.) vào danh sách được lập chỉ mục theo thời gian và chỉ phát lại.

3

Tôi đang tham gia này trực tiếp từ các tài liệu:

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. 
} 

Nếu bạn cố gắng để chơi nó trở lại trong thời gian bạn có thể làm một cái gì đó như thế này:

Sequence sequence = MidiSystem.getSequence(...); // either a File or InputStream 
sequencer.setSequence(sequence); 
sequencer.start();