Skip to content
Permalink
Newer
Older
100644 221 lines (189 sloc) 5.05 KB
Apr 12, 2018
1
--- ParamSet class
2
-- @module paramset
3
4
local separator = require 'params/separator'
5
local number = require 'params/number'
6
local option = require 'params/option'
7
local control = require 'params/control'
8
local file = require 'params/file'
9
local taper = require 'params/taper'
10
local trigger = require 'params/trigger'
12
local ParamSet = {
13
tSEPARATOR = 0,
14
tNUMBER = 1,
15
tOPTION = 2,
16
tCONTROL = 3,
17
tFILE = 4,
18
tTAPER = 5,
19
tTRIGGER = 6
20
}
21
22
ParamSet.__index = ParamSet
23
24
--- constructor
25
-- @param name
Apr 12, 2018
26
function ParamSet.new(name)
27
local ps = setmetatable({}, ParamSet)
28
ps.name = name or ""
29
ps.params = {}
30
ps.count = 0
31
ps.lookup = {}
32
return ps
33
end
34
35
--- add separator
36
function ParamSet:add_separator()
37
table.insert(self.params, separator.new())
38
self.count = self.count + 1
39
end
40
May 28, 2018
41
--- add generic parameter
42
function ParamSet:add(args)
43
local param = args.param -- param is mandatory
May 28, 2018
44
table.insert(self.params, param)
45
self.count = self.count + 1
46
self.lookup[param.name] = self.count
47
if args.action then -- action is optional
48
param.action = args.action
49
end
May 28, 2018
50
end
51
52
--- add number
Apr 12, 2018
53
function ParamSet:add_number(name, min, max, default)
54
self:add { param=number.new(name, min, max, default) }
55
end
56
57
--- add option
Apr 12, 2018
58
function ParamSet:add_option(name, options, default)
59
self:add { param=option.new(name, options, default) }
Apr 12, 2018
62
--- add control
63
function ParamSet:add_control(name, controlspec, formatter)
64
self:add { param=control.new(name, controlspec, formatter) }
Apr 9, 2018
66
Apr 20, 2018
67
--- add file
68
function ParamSet:add_file(name, path)
69
self:add { param=file.new(name, path) }
70
end
71
72
--- add taper
73
function ParamSet:add_taper(name, min, max, default, k, units)
74
self:add { param=taper.new(name, min, max, default, k, units) }
75
end
76
77
--- add trigger
78
function ParamSet:add_trigger(name)
79
self:add { param=trigger.new(name) }
80
end
81
82
--- print
Apr 12, 2018
83
function ParamSet:print()
84
print("paramset ["..self.name.."]")
85
for k,v in pairs(self.params) do
86
print(k.." "..v.name.." = "..v:string())
Apr 9, 2018
87
end
88
end
89
Apr 12, 2018
91
function ParamSet:get_name(index)
92
return self.params[index].name
93
end
94
95
--- string
Apr 12, 2018
96
function ParamSet:string(index)
97
if type(index) == "string" then index = self.lookup[index] end
98
return self.params[index]:string()
99
end
100
101
--- set
Apr 12, 2018
102
function ParamSet:set(index, v)
103
if type(index) == "string" then index = self.lookup[index] end
104
self.params[index]:set(v)
105
end
106
107
--- set_raw (for control types only)
108
function ParamSet:set_raw(index, v)
109
if type(index) == "string" then index = self.lookup[index] end
110
self.params[index]:set_raw(v)
111
end
112
Apr 12, 2018
114
function ParamSet:get(index)
115
if type(index) == "string" then index = self.lookup[index] end
116
return self.params[index]:get()
117
end
118
119
--- get_raw (for control types only)
120
function ParamSet:get_raw(index)
121
if type(index) == "string" then index = self.lookup[index] end
122
return self.params[index]:get_raw()
123
end
124
Apr 12, 2018
126
function ParamSet:delta(index, d)
127
if type(index) == "string" then index = self.lookup[index] end
128
self.params[index]:delta(d)
129
end
130
131
--- set action
Apr 12, 2018
132
function ParamSet:set_action(index, func)
133
if type(index) == "string" then index = self.lookup[index] end
134
self.params[index].action = func
135
end
136
137
--- get type
138
function ParamSet:t(index)
139
return self.params[index].t
140
end
141
142
local function quote(s)
143
return '"'..s:gsub('"', '\\"')..'"'
144
end
145
146
local function unquote(s)
147
return s:gsub('^"', ''):gsub('"$', ''):gsub('\\"', '"')
148
end
150
--- write to disk
151
-- @param filename relative to data_dir
152
function ParamSet:write(filename)
Jun 2, 2018
153
-- check for subfolder in filename, create subfolder if it doesn't exist
154
local subfolder, found = string.gsub(filename,"/(.*)","")
155
if found==1 then
156
local fd = io.open(data_dir..subfolder,"r")
157
if fd then
158
io.close(fd)
Jun 2, 2018
159
else
160
print("creating subfolder")
161
os.execute("mkdir "..data_dir..subfolder)
Jun 2, 2018
162
end
163
end
164
-- write file
165
local fd = io.open(data_dir..filename, "w+")
Apr 9, 2018
166
io.output(fd)
167
for k,param in pairs(self.params) do
168
if param.name and param.t ~= self.tTRIGGER then
169
io.write(string.format("%s: %s\n", quote(param.name), param:get()))
170
end
Apr 9, 2018
171
end
172
io.close(fd)
173
end
174
175
--- read from disk
176
-- @param filename relative to data_dir
Apr 12, 2018
177
function ParamSet:read(filename)
178
local fd = io.open(data_dir..filename, "r")
179
if fd then
180
io.close(fd)
181
for line in io.lines(data_dir..filename) do
182
local name, value = string.match(line, "(\".-\")%s*:%s*(.*)")
183
184
if name and value then
185
name = unquote(name)
186
local index = self.lookup[name]
187
188
if index then
189
if tonumber(value) ~= nil then
190
self.params[index]:set(tonumber(value))
191
elseif value == "-inf" then
192
self.params[index]:set(-math.huge)
193
elseif value == "inf" then
194
self.params[index]:set(math.huge)
May 21, 2018
195
elseif value then
196
self.params[index]:set(value)
197
end
198
end
200
201
end
202
else
203
print("paramset: "..filename.." not read.")
204
end
Apr 9, 2018
205
end
206
207
--- bang all params
Apr 12, 2018
208
function ParamSet:bang()
209
for k,v in pairs(self.params) do
Apr 9, 2018
210
v:bang()
211
end
212
end
Apr 9, 2018
213
Apr 12, 2018
215
function ParamSet:clear()
216
self.name = ""
217
self.params = {}
218
self.count = 0
219
end
220
Apr 12, 2018
221
return ParamSet