js-synthesizer icon indicating copy to clipboard operation
js-synthesizer copied to clipboard

Having trouble Using hook / handle MIDI-related event data

Open ridleytech opened this issue 3 years ago • 1 comments

I attempted using the hookPlayerMIDIEventsByName method but I'm getting this error. How do I go about fixing this?

synth = new JSSynth.AudioWorkletNodeSynthesizer(); synth.init(context.sampleRate); if (!node) { node = synth.createAudioNode(context); node.connect(context.destination); synth.hookPlayerMIDIEventsByName('myHookPlayerEvents', { secondSFont: soundfontbuffer }); }

I added this code to js-synthesizer.worklet.min.js

AudioWorkletGlobalScope.myHookPlayerEvents = function (s, type, event, data) { if (type === 0xc0) { if (event.getProgram() === 0) { s.midiProgramSelect(event.getChannel(), data.secondSFont, 0, 0); return true; } } return false; };

js-synthesizer.min.js:312 Uncaught (in promise) Error: Name not found at js-synthesizer.worklet.min.js:1298 at T (js-synthesizer.worklet.min.js:1199) at js-synthesizer.worklet.min.js:1188

ridleytech avatar Nov 07 '21 18:11 ridleytech

Well, it seems no problem for your code, but I suggest that the function myHookPlayerEvents is placed at the separate script file.

  • foo.js
AudioWorkletGlobalScope.myHookPlayerEvents = function (s, type, event, data) {
  if (type === 0xc0) {
    if (event.getProgram() === 0) {
      s.midiProgramSelect(event.getChannel(), data.secondSFont, 0, 0);
      return true;
    }
  }
  return false;
};
  • main code
await context.audioWorklet.addModule('foo.js'); // add this
await context.audioWorklet.addModule('libfluidsynth-2.2.1.js');
await context.audioWorklet.addModule('js-synthesizer.min.js');
.
.
.

If the problem persists, please use js-synthesizer.worklet.js (non-.min file), set breakpoint around L1901 (the line const fn = (AudioWorkletGlobalScope[name]); in doHookPlayerMIDIEvents method), call synth.hookPlayerMIDIEventsByName (will hit the breakpoint), and dump Object.keys(AudioWorkletGlobalScope). You can verify whether the function myHookPlayerEvents is correctly registered.
(I tried with Google Chrome; I could not debug audio worklet with Firefox.)

P.S. I found the bug around the audio worklet in js-synthesizer and update the package. Please use the version v1.8.4.

jet2jet avatar Nov 09 '21 12:11 jet2jet