Created
July 19, 2024 04:39
-
-
Save rweichler/46254a31e1d19d81ca2057d04dc8bc5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PRESET_FOLDER = EQE_PREFIX..'/db/presets' | |
PRESET_DEFAULT = 'CURRENT_BANDS' | |
function preset_file(name) | |
return PRESET_FOLDER..'/'..name..'.lua' | |
end | |
local esc = require 'str_esc' | |
function eqe.save(name, input) | |
input = input or eqe | |
name = string.gsub(name or PRESET_DEFAULT, '%/', '\\') | |
local path = preset_file(name) | |
local buf = {} | |
local function write(s) | |
buf[#buf + 1] = s | |
end | |
write('return {') | |
write(' {') | |
write(' name = "preamp",') | |
write(' gain = '..input.preamp..',') | |
write(' },') | |
for i=1,#input do | |
local filter = input[i] | |
write(' {') | |
write(' name = "'..filter.name..'",') | |
write(' frequency = '..filter.frequency..',') | |
write(' Q = '..filter.Q..',') | |
if filter.gain then | |
write(' gain = '..filter.gain..',') | |
end | |
if filter.channel then | |
write(' channel = '..filter.channel..',') | |
end | |
write(' },') | |
end | |
write('}') | |
if not name and input == eqe and next(eqe.attr) then | |
write(', ') | |
write(esc(eqe.attr)) | |
end | |
IPCD('WRITE_FILE('..esc(path)..','..esc(table.concat(buf))..')') | |
IPCD('UPDATE_PRESET('..esc(name or PRESET_DEFAULT)..','..esc(path)..')') | |
end | |
local function loadLua(input) | |
local f = load(input) | |
if not f then return end | |
local _ = _ENV | |
_ENV = {} | |
local t, attr = f() | |
_ENV = _ | |
local x = {} | |
-- cant inline this because i set _ENV to {} above | |
for k,v in pairs(t) do | |
if v.name == 'preamp' then | |
eqe.preamp = v.gain | |
else | |
if v.name == 'eq' then v.name = 'peak' end | |
x[#x + 1] = v | |
end | |
end | |
if attr then | |
for k,v in pairs(attr) do | |
eqe.attr[k] = v | |
end | |
end | |
return x | |
end | |
local function loadApo(input) | |
local x = nil | |
for s in input:gmatch('(.-)\n') do | |
local f = '(%-?%d+%.?%d*)' | |
local preamp = s:match('Preamp%: '..f..' dB') | |
if preamp then | |
x = x or {} | |
eqe.preamp = assert(tonumber(preamp)) | |
elseif s == '' then | |
-- pass | |
else | |
local supportedKinds = { | |
PK = 'peak', | |
LSC = 'lowshelf', | |
HSC = 'highshelf', | |
} | |
local kind, fc, gain, q = | |
s:match('Filter %d+%: ON (.+) Fc '..f..' Hz Gain '..f..' dB Q '..f) | |
if not kind or not fc or not gain or not q then return end | |
x = x or {} | |
x[#x + 1] = { | |
name = assert(supportedKinds[kind]), | |
gain = assert(tonumber(gain)), | |
Q = assert(tonumber(q)), | |
frequency = assert(tonumber(fc)), | |
} | |
end | |
end | |
return x | |
end | |
function eqe.loadstring(input, dontSave) | |
local x = assert(loadLua(input) or loadApo(input)) | |
eqe.insert(nil, x, true) | |
eqe.update() | |
if not dontSave then | |
eqe.save() | |
end | |
end | |
function eqe.load(name) | |
local f = io.open(preset_file(name or PRESET_DEFAULT), 'r') | |
local input = f:read('*a') | |
f:close() | |
eqe.loadstring(input, not name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment