美文网首页
Processing接收MIDI消息

Processing接收MIDI消息

作者: StormerX | 来源:发表于2020-03-29 22:49 被阅读0次

    直接上代码了

    import themidibus.*; //Import the library
    import javax.sound.midi.MidiMessage; 
    
    MidiBus myBus; // The MidiBus 
     
    void setup() {
      size(400, 400);
      background(0);
    
      MidiBus.list();  
     
      myBus = new MidiBus(this, 0, -1);  
    }
    
    
    void draw() { 
    }
    
    
    void midiMessage(MidiMessage message) { // You can also use midiMessage(MidiMessage message, long timestamp, String bus_name)
      // Receive a MidiMessage
      // MidiMessage is an abstract class, the actual passed object will be either javax.sound.midi.MetaMessage, javax.sound.midi.ShortMessage, javax.sound.midi.SysexMessage.
      // Check it out here http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/midi/package-summary.html
      println();
      println("MidiMessage Data:");
      println("--------");
      println("Status Byte/MIDI Command:"+message.getStatus());
      for (int i = 1;i < message.getMessage().length;i++) { 
        println("Param "+(i+1)+": "+(int)(message.getMessage()[i] & 0xFF));
      }
    }
    
    
    void noteOn(Note note) {  
      // Receive a noteOn
      println();
      println("Note On:");
      println("--------");
      println("Channel:"+note.channel());
      println("Pitch:"+note.pitch());
      println("Velocity:"+note.velocity());
    }
    
    void noteOff(Note note) {
      // Receive a noteOff
      println();
      println("Note Off:");
      println("--------");
      println("Channel:"+note.channel());
      println("Pitch:"+note.pitch());
      println("Velocity:"+note.velocity());
    }
    
    void controllerChange(ControlChange change) {
      // Receive a controllerChange
      println();
      println("Controller Change:");
      println("--------");
      println("Channel:"+change.channel());
      println("Number:"+change.number());
      println("Value:"+change.value());
    }
    
    void rawMidi(byte[] data) { // You can also use rawMidi(byte[] data, String bus_name)
      // Receive some raw data
      // data[0] will be the status byte
      // data[1] and data[2] will contain the parameter of the message (e.g. pitch and volume for noteOn noteOff)
      println();
      println("Raw Midi Data:");
      println("--------");
      println("Status Byte/MIDI Command:"+(int)(data[0] & 0xFF));
      // N.B. In some cases (noteOn, noteOff, controllerChange, etc) the first half of the status byte is the command and the second half if the channel
      // In these cases (data[0] & 0xF0) gives you the command and (data[0] & 0x0F) gives you the channel
      for (int i = 1;i < data.length;i++) {
        println("Param "+(i+1)+": "+(int)(data[i] & 0xFF));
      }
    }
    
    void delay(int time) {
      int current = millis();
      while (millis () < current+time) Thread.yield();
    }
    
    Live里把MIDI消息发送到IAC_BUS1 Processing里选取MIDI IN的port [0], 即IAC_BUS1,
    执行代码后,接收到的MIDI消息被显示在Console窗口。

    相关文章

      网友评论

          本文标题:Processing接收MIDI消息

          本文链接:https://www.haomeiwen.com/subject/sbppuhtx.html