Skip to content

Instantly share code, notes, and snippets.

@x2mirko
Created July 8, 2018 11:07
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save x2mirko/8e35b6825d04e3d925817f288aab16c4 to your computer and use it in GitHub Desktop.
Minimal MPE example for Norns
Engine_MinimalMPE : CroneEngine {
var synths;
*new { arg context, doneCallback;
^super.new(context, doneCallback);
}
alloc {
synths = Array.newClear(8);
SynthDef("mpeSynth", {
arg out, freq = 110.0, gate = 1.0, amp = 0.0;
var sig;
var env = Env.adsr(0.02, 0.1);
sig = SinOsc.ar(freq, mul: amp * 0.8 * EnvGen.kr(env, gate, doneAction: Done.freeSelf));
Out.ar(out, sig!2);
}).add;
this.addCommand("noteOn", "if", { arg msg;
var chan = msg[1];
if (synths[chan] != nil) {
synths[chan].set(\gate, 0);
};
synths[chan] = Synth("mpeSynth", [\out, context.out_b, \freq, msg[2], \gate, 1], target:context.xg);
});
this.addCommand("noteOff", "i", { arg msg;
var chan = msg[1];
if (synths[chan] != nil) {
synths[chan].set(\gate, 0);
};
});
this.addCommand("freq", "if", { arg msg;
var chan = msg[1];
if (synths[chan] != nil) {
synths[chan].set(\freq, msg[2]);
};
});
this.addCommand("amp", "if", { arg msg;
var chan = msg[1];
if (synths[chan] != nil) {
synths[chan].set(\amp, msg[2]);
};
});
}
}
engine.name = "MinimalMPE"
local notes = {0,0,0,0,0,0,0,0}
function init()
end
local function getHz(note)
return 13.75*2^(note/12)
end
local function note_on(channel, note, vel)
notes[channel] = note
engine.noteOn(channel, getHz(note))
end
local function note_off(channel, note, vel)
engine.noteOff(channel)
end
local function getChannel(status)
return bit32.band(15, status)
end
local function getEventType(status)
return bit32.band(240, status)
end
local function midi_event(data)
local channel = getChannel(data[1])
local eventType = getEventType(data[1])
if eventType == 144 then
if data[3] == 0 then
note_off(channel, data[2])
else
note_on(channel, data[2], data[3])
end
elseif eventType == 128 then
note_off(channel, data[2])
elseif eventType == 176 then
--cc(channel, data1, data2)
elseif eventType == 224 then
local bend = 24.0 * ((data[3] * 127.0 + data[2] - 8192.0)/8192.0);
engine.freq(channel, getHz(notes[channel] + bend))
elseif eventType == 208 then
engine.amp(channel, 0.3 * (data[2]/127.0))
end
end
midi.add = function(dev)
print('mpeTest: midi device added', dev.id, dev.name)
dev.event = midi_event
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment