Skip to content

Commit

Permalink
wasm midi_out implemented, no test
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinissance committed Oct 4, 2024
1 parent fa7eca5 commit c133b13
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 9 deletions.
100 changes: 95 additions & 5 deletions hvcc/generators/c2js/template/hv_worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
Expand Down
24 changes: 23 additions & 1 deletion hvcc/generators/c2js/template/hv_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]);
Expand Down
19 changes: 16 additions & 3 deletions hvcc/generators/c2js/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script type="text/javascript">
var heavyModule = null;
var loader = null;
midioutPort = null;

window.onload = function() {
{{name}}_Module().then(loadedModule => {
Expand All @@ -51,6 +52,8 @@
printHook: onPrint,
// optional: provide a callback handler for [s {sendName} @hv_param] messages
sendHook: onFloatMessage,
// optional: provide a callback handler for midiout messages
midiOutHook: onMidiOutMessage,
// optional: pass an existing web audio context, otherwise a new one
// will be constructed.
webAudioContext: null
Expand Down Expand Up @@ -125,12 +128,11 @@

if (midiOutputs.length > 0) {
midiOutputsSelect.selectedIndex = 0;
// TODO: Behavior for the first selected MIDI output here
midioutPort = midiOutputs[0];
}

midiOutputsSelect.onchange = function() {
var selectedOutput = midiOutputs[midiOutputsSelect.value];
// You can add behavior for selected MIDI output here
midioutPort = midiOutputs[midiOutputsSelect.value];
};
{%- endif %}
}
Expand All @@ -147,6 +149,17 @@
}
}
{%- endif %}



function onMidiOutMessage(message) {
if (midioutPort !== null) {
midioutPort.send(message);
}
else {
console.error("No MIDI output port available.");
}
}

function onFloatMessage(sendName, floatValue) {

Expand Down

0 comments on commit c133b13

Please sign in to comment.