arirusso / unimidi

Realtime MIDI IO for Ruby
Other
255 stars 28 forks source link

Input example pegs CPU #20

Closed trogdoro closed 9 years ago

trogdoro commented 11 years ago

Is it normal for the CPU to go to 99% when input.gets is waiting? That's happening for me on OSX Lion with ruby 1.9.3.

It happens when running the input example code: https://github.com/arirusso/unimidi/blob/master/examples/input.rb

Also, it seems like it misses some events, and gets them on a slight delay.

zensible commented 10 years ago

I had the same issue when using a combination of midi controllers (Yamaha P120 and Behringer FCB1010). I was able to fix it by making a small change to the ffi-coremidi gem:

/your.gems.folder/ffi-coremidi-0.2.0/lib/coremidi/source.rb#gets

    def gets
      until queued_messages?
        sleep(0.0001)  # <-- patch to prevent 100% CPU issue with some midi controllers
      end
      msgs = queued_messages
      @pointer = @buffer.length
      msgs
    end

The issue was that 'queued_messages?' was always returning false, this slight delay seems to fix the problem without introducing any latency.

arirusso commented 10 years ago

You can certainly submit a patch to ffi-coremidi ! If not I'll try and get around to it soon

Thanks

arirusso commented 9 years ago

Wound up adding the patch here: https://github.com/arirusso/ffi-coremidi/commit/05200fbbdf90fdb2065e17270074e0bb37006d8a#diff-d41d8cd98f00b204e9800998ecf8427e