-------------------------------------------------------------------------------- --! @title ChordMemory --! @brief Chord Memory With Setups Saved to and Recalled From Files --! @category AssetLoading --! --! A chord memory: switch Learn on, play the chord, switch Learn off, and every --! single key then plays that chord from its own root. The whole setup - the --! widget values and the learned chord - can be written to a file and read back --! with saveState and loadState, so users can keep a library of chord sets --! outside their presets. --! --! The learned chord is not a widget value, so it reaches the file through --! onSave and comes back through onLoad, exactly as it does when the preset is --! saved. loadState only reads the file: the values are applied on the next --! audio block, which calls onLoad and the widgets' changed callbacks, so the --! script refreshes its display from there rather than from the task callback. --! --! @demonstrates saveState, loadState, browseForFile, onSave, onLoad, AsyncTask -------------------------------------------------------------------------------- local learn = OnOffButton("Learn", false) learn.persistent = false -- a recalled setup should never come back in learn mode local spread = Knob{"Spread", 0, -24, 24, true} -- shifts the whole chord, in semitones local saveButton = Button("Save Setup") local loadButton = Button("Load Setup") local display = Label("chord") display.width = 360 local status = Label("status") status.width = 360 -- Intervals from the lowest learned note, e.g. {0, 4, 7}. A plain array, kept -- apart from any string keys so it survives the JSON round trip unchanged. local chord = {0} local learned = {} -- absolute notes captured while learning local NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"} local function showChord() local names = {} for i, interval in ipairs(chord) do names[i] = "+" .. interval end display.text = "Chord: " .. table.concat(names, " ") end local function buildChord() if #learned == 0 then return end -- nothing played: keep the previous chord table.sort(learned) chord = {} for i, note in ipairs(learned) do chord[i] = note - learned[1] end showChord() end learn.changed = function(self) if self.value then learned = {} else buildChord() end end function onNote(e) if learn.value then table.insert(learned, e.note) status.text = "Learning: lowest note " .. NOTE_NAMES[e.note % 12 + 1] postEvent(e) -- let the user hear what is being learned return end for _, interval in ipairs(chord) do local note = e.note + interval + spread.value if note >= 0 and note <= 127 then -- -1 ties each chord note to the key that triggered it playNote(note, e.velocity, -1) end end end function onSave() return {chord = chord} end function onLoad(data) -- Synchronous and possibly off the audio thread: only restore state here. if type(data) == "table" and type(data.chord) == "table" and #data.chord > 0 then chord = data.chord showChord() end end saveButton.changed = function() browseForFile("save", "Save chord setup", "ChordSetup.state", "*.state", function(task) if not task.success then return end -- dialog cancelled -- saveState does not resolve relative paths; browseForFile gives an absolute one. saveState(task.result, function(t) status.text = t.success and "Saved" or "Could not write the file" end) end) end loadButton.changed = function() browseForFile("open", "Load chord setup", "", "*.state", function(task) if not task.success then return end loadState(task.result, function(t) -- The file was read; onLoad refreshes the chord display itself. status.text = t.success and "Loaded" or "Not a chord setup file" end) end) end showChord() makePerformanceView()