cwilso / WebMIDIAPIShim

Polyfill using the Jazz NPAPI MIDI plugin to implement the Web MIDI API on Mac and Windows.
380 stars 53 forks source link

Requiring this package on node uses 2 cpus #80

Open rektide opened 6 years ago

rektide commented 6 years ago

Hello. If I require('web-midi-api-shim'); navigator.requestMIDIAccess() from Node.js (say a repl, or small script) my Node process starts using two CPUs at >90%. Context-switches go from ~500/s to 36k/s.

This makes the library unsuitable for use on a laptop or battery powered system, and I am unsure but somewhat under the impression that it degrades my other web browsing activity on this computer.

rektide commented 6 years ago

I pulled some power consumption numbers from powertop and dstat, going from a fresh startup, to plugging in the Korg NanoKontrol2 I use, to running midisnoop listening via alsa while i tap ~2 buttons/s, to turning that off & starting web-midi-api-shim, running requetMIDIAccess() & nothing else, & again tapping ~2 buttons/s.

Stage Power (W) User cpu sys cpu Wakeups Context Switches
Idle 3.55 0% 0% 40 28
Device attached 4.31 0% 0% 55 50
midisnoop 4.55 5% 0% 415 340
web-midi-api-shim 10.4 21% 7% 560 36,000
rektide commented 6 years ago

I haven't tried using it in any real capacity what-so-ever, but https://github.com/abudaan/WebMIDIAPIShim/tree/upgrade-2018 branch shows very tame cpu usage. I'll see about reproducing above conditions to test how much better, but it looks really close to midisnoop. But I haven't done any real work with that branch yet!

@abudaan - can you report in on the status of that branch? what are the major work areas remaining? anything I can assist with?

jazz-soft commented 6 years ago

Is that issue from WebMIDIAPIShim or from the underlying https://www.npmjs.com/package/jazz-midi ?

rektide commented 6 years ago

@jazz-soft I'm not super sure how to test. I imported jazz-midi in a repl, and ran midi.MidiOutOpen(0) and midi.MidiInOpen(0) and my cpu consumption & power is fine. There doesn't appear to be any kind of event-based api in jazz-midi to, for example, see button presses. That implies I'd have to write some kind of loop to sample the device?

jazz-soft commented 6 years ago

You can set the callback function in jazz-midi: http://jazz-soft.net/doc/Jazz-Plugin/MidiInOpen.html But it may be more convenient to use via the JZZ library: https://github.com/jazz-soft/JZZ

rektide commented 6 years ago

@jazz-soft In @abudaan's update-2018 branch, he's started switching WebMIDIAPIShim to use jzz. :) I'm just starting to test whether the basic functionality I need works on that branch.

jazz-soft commented 6 years ago

I believe that was an experimental branch.

abudaan commented 6 years ago

@jazz-soft @rektide Yes, Sema is right, update-2018 is still wip; currently it is a quick-and-dirty implementation of the latest Jazz plugin but it has all functionality so you can use it.

I will start working on this branch again early April and it will be backwards compatible so you should be able to update the WebMIDIAPIShim by then without breaking your project's code.

jazz-soft commented 6 years ago

@abudaan FYI: I'm planning to make JZZ expose Web MIDI API in the next release. If you are interested, we can discuss it by email.

jazz-soft commented 6 years ago

@rektide & @abudaan, please have a look at https://github.com/jazz-soft/JZZ - it now enables Web MIDI API

abudaan commented 6 years ago

Great work! I have replaced the WebMIDIAPIShim in both heartbeatjs and qambi and it works like a charm! I think we can now deprecate the WebMIDIAPIShim?