Giter VIP home page Giter VIP logo

midi-eye's Introduction

MIDI EYE

MIDI input event listener for Ruby

Install

gem install midi-eye

or using Bundler, add this to your Gemfile

gem "midi-eye"

Usage

require 'midi-eye'

The following is an example that takes any note messages received from a unimidi input, transposes them up one octave and then sends them to an output

First, pick some MIDI IO ports

@input = UniMIDI::Input.gets
@output = UniMIDI::Output.gets

Then create a listener for the input port

transpose = MIDIEye::Listener.new(@input)

You can bind an event to the listener using Listener#listen_for

The listener will try to positively match the parameters you pass in to the properties of the messages it receives.

In this example, we specify that the listener listens for note on/off messages, which are identifiable by their class.

transpose.listen_for(:class => [MIDIMessage::NoteOn, MIDIMessage::NoteOff]) do |event|

  # raise the note value by an octave
  event[:message].note += 12

  # send the altered note message to the output you chose earlier
  @output.puts(event[:message])

end

There is also the option of leaving out the parameters altogether and including using conditional if/unless/case/etc statements in the callback.

You can bind as many events to a listener as you wish by repeatedly calling Listener#listen_for

Once all the events are bound, start the listener

transpose.run

A listener can also be run in a background thread by passing in :background => true.

transpose.run(:background => true)

transpose.join # join the background thread later

Documentation

Author

License

Apache 2.0, See the file LICENSE

Copyright (c) 2011-2017 Ari Russo

midi-eye's People

Contributors

arirusso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

epictetus

midi-eye's Issues

Overflow(?) Thread Hang Issue

Consider the following script, adapted from the main example in the doc:

require "midi-eye"
#puts UniMIDI::Input.gets
@input = UniMIDI::Input.use(2)
midi_listener = MIDIEye::Listener.new(@input)
midi_listener.listen_for(:class => [MIDIMessage::NoteOn, MIDIMessage::NoteOff]) {|event| puts event[:message]}
midi_listener.run

When playing a midi controller very quickly (arguably unnecessarily so), the interpreter hangs without failure or exception, requiring a cmd interrupt. This may just be a limitation of Ruby threads, but I'm positing the potential issue nevertheless.

Potential MIDIMessage content/logging error

Consider the following short script (adapted from the example given in the doc)

require "midi-eye"

@input = UniMIDI::Input.use(1)
midi_listener = MIDIEye::Listener.new(@input)
midi_listener.listen_for {|event| puts event[:message]}
midi_listener.run

When running, tied to a device (Alesis V49) I know to be in position 1, to save testing time, I repeatedly hit 1 key. This is the resulting output:

#<MIDIMessage::NoteOn:0x000000000337c078>
#<MIDIMessage::NoteOff:0x0000000003445338>
#<MIDIMessage::NoteOff:0x0000000003454ef0>
#<MIDIMessage::NoteOff:0x00000000034642d8>
#<MIDIMessage::NoteOff:0x000000000344e618>
#<MIDIMessage::NoteOff:0x000000000342c1a8>
#<MIDIMessage::NoteOff:0x00000000033f65f8>
#<MIDIMessage::NoteOff:0x00000000033a7e30>
#<MIDIMessage::NoteOff:0x000000000337e260>
#<MIDIMessage::NoteOff:0x000000000335da10>
#<MIDIMessage::NoteOff:0x000000000332dea0>
#<MIDIMessage::NoteOff:0x0000000003112e40>
#<MIDIMessage::NoteOff:0x00000000030d4a00>
#<MIDIMessage::NoteOff:0x0000000002f8de30>
#<MIDIMessage::NoteOff:0x000000000345efb8>
... 

I would expect both a NoteOn and a NoteOff for each key pressed.
If i start pressing keys "simultaneously" i get mixed messages, some NoteOn, others NoteOff, but once I return to pushing one key repeatedly, the behavior resurfaces.

Another device, a Novation Launchpad Pro, has the opposite issue, where one note is pressed repeatedly, NoteOn messages are the only messages shown.

I have confirmed both of my controllers are functioning on their respective ports with a program called MIDI-OX:

Alesis V49

TIMESTAMP IN PORT STATUS DATA1 DATA2 CHAN NOTE EVENT
 000008F5   5  --     92    37    13    3  G  3 Note On
 0000095D   5  --     82    37    00    3  G  3 Note Off
 00000FB7   5  --     92    37    15    3  G  3 Note On
 0000102B   5  --     82    37    00    3  G  3 Note Off
 0000117C   5  --     92    37    15    3  G  3 Note On
 000011E1   5  --     82    37    00    3  G  3 Note Off
 00001323   5  --     92    37    15    3  G  3 Note On
 0000138A   5  --     82    37    00    3  G  3 Note Off
 000014B0   5  --     92    37    14    3  G  3 Note On
 00001517   5  --     82    37    00    3  G  3 Note Off
 00001647   5  --     92    37    14    3  G  3 Note On
 000016AE   5  --     82    37    00    3  G  3 Note Off

Novation Launchpad Pro

TIMESTAMP IN PORT STATUS DATA1 DATA2 CHAN NOTE EVENT
 000004FC   3  --     98    45    4A    9  A  4 Note On
 00000578   3  --     98    45    00    9  A  4 Note Off
 0000071F   3  --     98    45    3B    9  A  4 Note On
 000007CF   3  --     98    45    00    9  A  4 Note Off
 000008D2   3  --     98    45    4A    9  A  4 Note On
 00000991   3  --     98    45    00    9  A  4 Note Off
 00000A6B   3  --     98    45    3B    9  A  4 Note On
 00000B21   3  --     98    45    00    9  A  4 Note Off
 00000BFA   3  --     98    45    4D    9  A  4 Note On
 00000C8F   3  --     98    45    00    9  A  4 Note Off

Cannot `run` due to nil device buffer

Code:

def find_mpk(io)
  io.all.find { |device| device.name.downcase.strip.include?("mpk mini") }
end

input = find_mpk(UniMIDI::Input)

midi_listener = MIDIEye::Listener.new(input)
midi_listener.listen_for(:class => [MIDIMessage::NoteOn, MIDIMessage::NoteOff]) {|event| puts event[:message]}
midi_listener.run

Error:

Traceback (most recent call last):
        7: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:137:in `block in listen'
        6: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:126:in `listen_loop'
        5: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:126:in `loop'
        4: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:127:in `block in listen_loop'
        3: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:106:in `poll'
        2: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:106:in `each'
        1: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/listener.rb:107:in `block in poll'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/midi-eye-0.3.10/lib/midi-eye/source.rb:24:in `poll': undefined method `length' for nil:NilClass (NoMethodError)

Source:

messages = @device.buffer.slice(@pointer, @device.buffer.length - @pointer)

Note that changing the input to input = UniMIDI::Input.first allows the code to work just fine,.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.