Skip to content

Instantly share code, notes, and snippets.

@elihugarret
Last active March 23, 2021 05:42
Show Gist options
  • Save elihugarret/fc3de3600c972a64246aa3e3efb96611 to your computer and use it in GitHub Desktop.
Save elihugarret/fc3de3600c972a64246aa3e3efb96611 to your computer and use it in GitHub Desktop.
Pluto.jl notebook for echo MIDI input/output
### A Pluto.jl notebook ###
# v0.11.8
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ afd4a072-d3aa-11ea-3c8f-dda5bce82d86
using Plots
# ╔═╡ 79670ae0-d397-11ea-1f49-999d679323c5
@bind midi html"""
<canvas width=0 height=0></canvas>
<script>
const input = this.document.querySelector('canvas');
let sequence = new Array(16).fill(0);
let output = null;
input.value = sequence;
input.dispatchEvent(new CustomEvent("input"));
function onMIDIMessage(event) {
if (event.data.length > 1) {
sequence.push(event.data[1]);
sequence.shift();
input.value = sequence
input.dispatchEvent(new CustomEvent('input'))
}
if (output && event.data.length > 1) output.send(event.data, event.timestamp);
}
navigator.requestMIDIAccess().then(midiAccess => {
Array.from(midiAccess.inputs)[1][1].onmidimessage = onMIDIMessage
output = midiAccess.outputs.values().next().value
});
</script>
"""
# ╔═╡ c28ebe90-d3a9-11ea-2d4a-8586978579af
midi
# ╔═╡ d3663d30-d3ac-11ea-13c0-3f7d49b4e9ea
plot(midi, leg=false, marker=:o, linetype=:steppre)
# ╔═╡ Cell order:
# ╠═79670ae0-d397-11ea-1f49-999d679323c5
# ╠═c28ebe90-d3a9-11ea-2d4a-8586978579af
# ╠═afd4a072-d3aa-11ea-3c8f-dda5bce82d86
# ╠═d3663d30-d3ac-11ea-13c0-3f7d49b4e9ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment