Sunday, September 18, 2011

More MicroMIDI Tricks

MicroMIDI has a few less obvious features that may be useful once you have the basics down.

Sans block

In addition to how I worked in the other examples, MicroMIDI can be used outside of the context of a Ruby block. This allows you to integrate it as a more conventional Ruby library.

Here is the first example from this post redone without a block
@o = UniMIDI::Output.use(:first)

midi = MIDI::IO.new(@o)

midi.note("C")
midi.off 

midi.cc(5, 120)

midi.play("C3", 0.5)
Note that you can also re-open the IO object in a block later using the edit method
midi.edit do 
  play "G3", 1
end
Performance Shortcuts

There are also performance shortcuts that generate messages based on past messages or common conventions.
  • off - this generates a note-off message based on the last note-on message created
  • quiet! - sends note-off messages to all MIDI channels on all outputs
  • repeat - generates a copy of the last message created

Super Sticky Mode

Again in this post, I explained how MicroMIDI uses sticky values. There is also a super sticky mode that allows you to change the values inline with each message you create. Here's an example:
MIDI.using(@o) do

  super_sticky

  channel 1

  note "C4"
  off

  octave 5
  velocity 60

  note "E", :channel => 2
  off

  note "C3"
  off

end

When this program is run, these messages are sent to @o:

* note C4 (channel 1, vel 100)
* note-off C4 (channel 1, vel 100)
* note E5 (channel 2, vel 60)
* note-off E5 (channel 2, vel 60)
* note C3 (channel 2, vel 60)
* note-off C3 (channel 2, vel 60)

As you can see, when I sent :channel => 2 to the second note command, the MIDI channel remained for the commands that followed rather than just being transient for that command.

Message Cache

MicroMIDI keeps a timestamped log of the messages you create. You can access this log using the cache command, like this:
M do
  note "C4"
  cc 5, 120
  play "C2", 3
  
  puts cache
end
This code gives you this output:
{ :message=>#<MIDIMessage::NoteOn:0x007fbb6092baf8 
    @const=#<MIDIMessage::Constant:0x007fbb60930eb8 @key="C4", @value=60>, 
    @status=[9, 0], 
    @data=[60, 100], 
    @channel=0, 
    @note=60, 
    @velocity=100, 
    @name="C4", 
    @verbose_name="Note On: C4">, 
  :timestamp=>2.513885498046875 
}

{ :message=>#<MIDIMessage::ControlChange:0x007fbb60924e10 
    @const=#<MIDIMessage::Constant:0x007fbb6093df00 @key="Portamento Time", @value=5>, 
    @status=[11, 0], 
    @data=[5, 120], 
    @channel=0, @index=5, 
    @value=120, 
    @name="Portamento Time", 
    @verbose_name="Control Change: Portamento Time">, 
  :timestamp=>2.7201175689697266 }

{ :message=>#<MIDIMessage::NoteOn:0x007fbb60921558 
    @const=#<MIDIMessage::Constant:0x007fbb60931f48 @key="C2", @value=36>, 
    @status=[9, 0], 
    @data=[36, 100], 
    @channel=0, 
    @note=36, 
    @velocity=100, 
    @name="C2", 
    @verbose_name="Note On: C2">, 
  :timestamp=>2.961874008178711 
}

{ :message=>#<MIDIMessage::NoteOff:0x007fbb60917c38 
    @const=#<MIDIMessage::Constant:0x007fbb60931f48 @key="C2", @value=36>, 
    @status=[8, 0], 
    @data=[36, 100], 
    @channel=0, 
    @note=36, 
    @velocity=100, 
    @name="C2", 
    @verbose_name="Note Off: C2">, 
  :timestamp=>3003.7591457366943 
}


http://github.com/arirusso/micromidi

No comments:

Post a Comment