Skip to content

Instantly share code, notes, and snippets.

@markwheeler
Created July 19, 2019 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwheeler/fbf0d7e62ce15e1d7110bc7e58f4022f to your computer and use it in GitHub Desktop.
Save markwheeler/fbf0d7e62ce15e1d7110bc7e58f4022f to your computer and use it in GitHub Desktop.
Poly synth lua template
-- Poly Template
--
-- v1.0.2 Mark Eats
--
local MusicUtil = require "musicutil"
engine.name = "PolyTemplate"
local midi_in_device
local function note_on(note_num, vel)
engine.noteOn(note_num, MusicUtil.note_num_to_freq(note_num), vel)
end
local function note_off(note_num)
engine.noteOff(note_num)
end
local function note_off_all()
engine.noteOffAll()
end
local function note_kill_all()
engine.noteKillAll()
end
local function set_key_pressure(note_num, pressure)
engine.pressure(note_num, pressure)
end
local function set_channel_pressure(pressure)
engine.pressureAll(pressure)
end
local function set_timbre(note_num, timbre)
engine.timbre(note_num, timbre)
end
local function set_timbre_all(timbre)
engine.timbreAll(timbre)
end
local function set_pitch_bend(bend_st)
engine.pitchBendAll(MusicUtil.interval_to_ratio(bend_st))
end
-- Encoder input
function enc(n, delta)
end
-- Key input
function key(n, z)
if z == 1 then
if n == 2 then
note_off_all()
elseif n == 3 then
note_kill_all()
end
end
end
-- MIDI input
local function midi_event(data)
local msg = midi.to_msg(data)
local channel_param = params:get("midi_channel")
if channel_param == 1 or (channel_param > 1 and msg.ch == channel_param - 1) then
-- Note off
if msg.type == "note_off" then
note_off(msg.note)
-- Note on
elseif msg.type == "note_on" then
note_on(msg.note, msg.vel / 127)
-- Key pressure
elseif msg.type == "key_pressure" then
set_key_pressure(msg.note, msg.val / 127)
-- Channel pressure
elseif msg.type == "channel_pressure" then
set_channel_pressure(msg.val / 127)
-- Pitch bend
elseif msg.type == "pitchbend" then
local bend_st = (util.round(msg.val / 2)) / 8192 * 2 -1 -- Convert to -1 to 1
set_pitch_bend(bend_st * 2) -- 2 Semitones of bend
end
end
end
function init()
midi_in_device = midi.connect(1)
midi_in_device.event = midi_event
-- Add params
params:add{type = "number", id = "midi_device", name = "MIDI Device", min = 1, max = 4, default = 1, action = function(value)
midi_in_device.event = nil
midi_in_device = midi.connect(value)
midi_in_device.event = midi_event
end}
local channels = {"All"}
for i = 1, 16 do table.insert(channels, i) end
params:add{type = "option", id = "midi_channel", name = "MIDI Channel", options = channels}
redraw()
end
function redraw()
screen.clear()
screen.aa(1)
screen.level(15)
screen.move(10, 15)
screen.text(engine.name)
screen.update()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment