diff --git a/hvcc/generators/c2js/template/hv_worklet.js b/hvcc/generators/c2js/template/hv_worklet.js index c043f1a..d63b455 100644 --- a/hvcc/generators/c2js/template/hv_worklet.js +++ b/hvcc/generators/c2js/template/hv_worklet.js @@ -108,11 +108,101 @@ class {{name}}_AudioLibWorklet extends AudioWorkletProcessor { var self = this; // typedef void (HvSendHook_t) (HeavyContextInterface *context, const char *sendName, hv_uint32_t sendHash, const HvMessage *msg); var sendHook = addFunction(function(context, sendName, sendHash, msg) { - // Converts sendhook callback to (sendName, float) message - self.port.postMessage({ - type: 'sendHook', - payload: [UTF8ToString(sendName), _hv_msg_getFloat(msg, 0)] - }); + // Filter out MIDI messages + switch (UTF8ToString(sendName)) { + case "__hv_noteout": + var note = _hv_msg_getFloat(msg, 0); + var velocity = _hv_msg_getFloat(msg, 1); + var channel = _hv_msg_getFloat(msg, 2) % 16; // no pd midi ports + self.port.postMessage({ + type: 'midiOut', + payload:[ + (velocity > 0) ? 144 : 128 | channel, + note, + velocity + ] + }); + break; + case "__hv_ctlout": + var value = _hv_msg_getFloat(msg, 0); + var cc = _hv_msg_getFloat(msg, 1); + var channel = _hv_msg_getFloat(msg, 2) % 16; // no pd midi ports + self.port.postMessage({ + type: 'midiOut', + payload:[ + 176 | channel, + cc, + value + ] + }); + break; + case "__hv_pgmout": + var program = _hv_msg_getFloat(msg, 0); + var channel = _hv_msg_getFloat(msg, 1) % 16; // no pd midi ports + self.port.postMessage({ + type: 'midiOut', + payload:[ + 192 | channel, + program + ] + }); + break; + case "__hv_touchout": + var pressure = _hv_msg_getFloat(msg, 0); + var channel = _hv_msg_getFloat(msg, 1) % 16; // no pd midi ports + self.port.postMessage({ + type: 'midiOut', + payload:[ + 208 | channel, + pressure, + ] + }); + break; + case "__hv_polytouchout": + var value = _hv_msg_getFloat(msg, 0); + var note = _hv_msg_getFloat(msg, 1); + var channel = _hv_msg_getFloat(msg, 2) % 16; // no pd midi ports + self.port.postMessage({ + type: 'midiOut', + payload:[ + 160 | channel, + note, + value + ] + }); + break; + case "__hv_bendout": + var value = _hv_msg_getFloat(msg, 0); + let lsb = value & 0x7F; + let msb = (value >> 7) & 0x7F; + var channel = _hv_msg_getFloat(msg, 2) % 16; // no pd midi ports + self.port.postMessage({ + type: 'midiOut', + payload:[ + 224 | channel, + lsb, + msb + ] + }); + break; + case "__hv_midiout": + var message = [ + _hv_msg_getFloat(msg, 0), + _hv_msg_getFloat(msg, 1), + _hv_msg_getFloat(msg, 2) + ]; + self.port.postMessage({ + type: 'midiOut', + payload: message + }); + break; + default: + // Converts sendhook callback to (sendName, float) message + self.port.postMessage({ + type: 'sendHook', + payload: [UTF8ToString(sendName), _hv_msg_getFloat(msg, 0)] + }); + } }, "viiii" ); diff --git a/hvcc/generators/c2js/template/hv_wrapper.js b/hvcc/generators/c2js/template/hv_wrapper.js index 3b2f85e..95bc841 100644 --- a/hvcc/generators/c2js/template/hv_wrapper.js +++ b/hvcc/generators/c2js/template/hv_wrapper.js @@ -43,6 +43,8 @@ AudioLibLoader.prototype.init = function(options) { options.printHook(event.data.payload); } else if (event.data.type === 'sendHook' && options.sendHook) { options.sendHook(event.data.payload[0], event.data.payload[1]); + } else if (event.data.type === 'midiOut' && options.midiOutHook) { + options.midiOutHook(event.data.payload); } else { console.log('Unhandled message from {{name}}_AudioLibWorklet:', event.data); } @@ -54,7 +56,8 @@ AudioLibLoader.prototype.init = function(options) { sampleRate: this.webAudioContext.sampleRate, blockSize: blockSize, printHook: options.printHook, - sendHook: options.sendHook + sendHook: options.sendHook, + midiOutHook: options.midiOutHook }); this.audiolib = instance; this.webAudioProcessor = this.webAudioContext.createScriptProcessor(blockSize, instance.getNumInputChannels(), Math.max(instance.getNumOutputChannels(), 1)); @@ -155,6 +158,7 @@ var {{name}}_AudioLib = function(options) { this.heavyContext = _hv_{{name}}_new_with_options(this.sampleRate, {{pool_sizes_kb.internal}}, {{pool_sizes_kb.inputQueue}}, {{pool_sizes_kb.outputQueue}}); this.setPrintHook(options.printHook); this.setSendHook(options.sendHook); + this.setMidiOutHook(options.midiOutHook); // allocate temporary buffers (pointer size is 4 bytes in javascript) var lengthInSamples = this.blockSize * this.getNumOutputChannels(); @@ -253,6 +257,24 @@ var tableHashes = { } } +{{name}}_AudioLib.prototype.setMidiOutHook = function(hook) { + if (!this.heavyContext) { + console.error("heavy: Can't set MIDI Out Hook, no Heavy Context instantiated"); + return; + } + + if (hook) { + var midiOutHook = addFunction(function(context, message) { + // Converts MIDI out callback to a MIDI message + var data = new Uint8Array(Module.HEAPU8.buffer, message, 3); + hook(data); + }, + "vii" + ); + _hv_setMidiOutHook(this.heavyContext, midiOutHook); + } +} + {{name}}_AudioLib.prototype.sendEvent = function(name) { if (this.heavyContext) { _hv_sendBangToReceiver(this.heavyContext, eventInHashes[name]); diff --git a/hvcc/generators/c2js/template/index.html b/hvcc/generators/c2js/template/index.html index 06279e7..c1ff521 100644 --- a/hvcc/generators/c2js/template/index.html +++ b/hvcc/generators/c2js/template/index.html @@ -28,6 +28,7 @@