From 31b0b64db7fa8ffdee5124776480b40f96d26b3b Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 14 Feb 2021 07:46:50 +0700 Subject: [PATCH] migrate truths --- truths/truths.lua | 111 ++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 78 deletions(-) diff --git a/truths/truths.lua b/truths/truths.lua index 47f20ad..cf1130e 100644 --- a/truths/truths.lua +++ b/truths/truths.lua @@ -1,100 +1,56 @@ -- truths local BGUtil = include('bitgraves/common/bgutil') +local BGMidi = include('bitgraves/common/bgmidi') local Hexagon = include('bitgraves/common/hexagon') engine.name = 'Truths' mid = nil local indexOffset = 2 +local MPD218 function init() - audio:rev_off() -- no system reverb - audio:pitch_off() -- no system pitch analysis - audio:monitor_mono() -- expect only channel 1 input + BGUtil.configureSystemStuff() - params:add_control("bend", "bend", controlspec.new(0, 1, 'lin', 0, 0, '')) - params:set_action("bend", function(x) - engine.bend(util.linlin(0, 1, 0, -2, x)) - end) - - params:add_control("carrierSource", "carrierSource", controlspec.new(0, 1, 'lin', 0, 0, '')) - params:set_action("carrierSource", function(x) - engine.carrierInAmp(util.linlin(0, 1, 1, 0, x)) - engine.carrierNoiseAmp(util.linlin(0, 1, 0.2, 1, x)) - end) - - params:add_control("modulatorSource", "modulatorSource", controlspec.new(0, 1, 'lin', 0, 0, '')) - params:set_action("modulatorSource", function(x) - engine.modSource(x) - end) - - params:add_control("sustain", "sustain", controlspec.new(0, 1, 'lin', 0, 0, '')) - params:set_action("sustain", function(x) - engine.sustain(x) - end) + BGUtil.addEngineControlParam(params, { id = "amp" }) + BGUtil.addEngineControlParam(params, { + id = "bend", + max = 2, + action = function(x) + engine.bend(x * -1) -- max < 1 fails for some reason + end, + }) + BGUtil.addEngineControlParam(params, { id = "modSource" }) + BGUtil.addEngineControlParam(params, { id = "sustain" }) + BGUtil.addEngineControlParam(params, { + id = "carrierSource", + action = function(x) + engine.carrierInAmp(util.linlin(0, 1, 1, 0, x)) + engine.carrierNoiseAmp(util.linlin(0, 1, 0.2, 1, x)) + end, + }) - params:add_control("amp", "amp", controlspec.new(0, 1, 'lin', 0, 0, '')) - params:set_action("amp", function(x) - engine.amp(x) - end) - params:add_control("monitor", "monitor", controlspec.new(0, 1, 'lin', 0, 0, '')) params:set_action("monitor", function(x) audio.level_monitor(x) end) + MPD218 = BGMidi.newInputMappingMPD218({ + [3] = 'bend', + [9] = 'carrierSource', + [12] = 'modSource', + [13] = 'sustain', + [14] = 'monitor', + [15] = 'amp', + -- [16] = 'padOffset', + }) + mid = midi.connect() mid.event = midiEvent redraw() end -function enc(nEnc, delta) - -end - --- mapping from Akai MPD218 knobs to param handlers -local ccAkaiMapping = { - [3] = 'bend', - [9] = 'carrierSource', - [12] = 'modulatorSource', - [13] = 'sustain', - [14] = 'monitor', - [15] = 'amp', - [16] = 'padOffset', -} - -local ccHandlers = { - ['bend'] = function(val) - params:set('bend', val) - return 'bend ' .. val - end, - ['carrierSource'] = function(val) - params:set('carrierSource', val) - return 'carrier noise ' .. val - end, - ['modulatorSource'] = function(val) - params:set('modulatorSource', val) - return 'in mod ' .. val - end, - ['sustain'] = function(val) - params:set('sustain', val) - return 'sustain ' .. val - end, - ['monitor'] = function(val) - params:set('monitor', val) - return 'monitor ' .. val - end, - ['amp'] = function(val) - params:set('amp', val) - return 'amp ' .. val - end, - ['padOffset'] = function(val) - indexOffset = math.floor(util.linlin(0, 1, 0, 12, val)) - return 'pad offset ' .. indexOffset - end, -} - function midiEvent(data) -- tab.print(midi.to_msg(data)) local d = midi.to_msg(data) @@ -105,14 +61,13 @@ function midiEvent(data) local index = d.note - 36 engine.noteOff(index + indexOffset) elseif d.type == 'cc' then - local handler = ccAkaiMapping[d.cc] - if handler ~= nil and ccHandlers[handler] ~= nil then - local msg = ccHandlers[handler](d.val / 127) + local handled, msg = BGMidi.handleCCMPD218(MPD218, params, d.cc, d.val) + if handled then redraw(msg) end end end function redraw(msg) - Hexagon:draw(msg, ccAkaiMapping) + Hexagon:drawFancy(MPD218, msg) end