lichen-community-systems / Flocking

Flocking - Creative audio synthesis for the Web
GNU General Public License v2.0
695 stars 60 forks source link

Flocking's MIDI implementation on Node.js creates MIDI ports overly-eagerly, causing issues with ALSA on Linux #197

Closed colinbdclark closed 5 years ago

colinbdclark commented 7 years ago

The implementation of Flocking's MIDI support in Node.js is excessively eager about creating node-midi MIDIIn and MIDIOut objects. In particular, a node-midi midi.input or midi.output instance is created whenever a MIDIPort object is instantiated.

The issue here is that MIDIPorts are created anew each time a user enumerates the list of MIDI ports connected to a flock.midi.system using the refreshPorts() invoker. However, @simonbates—who is working on a polling feature that regularly checks to see if the list of ports has changed—has discovered that ALSA will crash if too many port instances are created, since node-midi doesn't provide any means for releasing the underlying RtMidi and ALSA resources it creates.

Given this reality on Linux, it seems that Flocking needs to be much lazier about when it actually allocates an underlying MIDIIn or MIDIOut instances—this should be deferred until the MIDIPort is actually opened.

This issue will be addressed in the new flocking-midi library.

simonbates commented 7 years ago

As a workaround, I have created a fork of node-midi that adds a method to the input and output objects to explicitly free the underlying RtMidi objects. I have also made some changes to Flocking to perform this explicit freeing whenever the ports are refreshed.

See also:

colinbdclark commented 5 years ago

Flocking no longer directly supports Node.js, so I'm closing this issue.