Permalink
Newer
100644
221 lines (189 sloc)
5.05 KB
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'
12
local ParamSet = {
13
tSEPARATOR = 0,
14
tNUMBER = 1,
15
tOPTION = 2,
16
tCONTROL = 3,
17
tFILE = 4,
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
42
function ParamSet:add(args)
43
local param = args.param -- param is mandatory
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
54
self:add { param=number.new(name, min, max, default) }
59
self:add { param=option.new(name, options, default) }
62
--- add control
63
function ParamSet:add_control(name, controlspec, formatter)
64
self:add { param=control.new(name, controlspec, formatter) }
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) }
77
--- add trigger
78
function ParamSet:add_trigger(name)
79
self:add { param=trigger.new(name) }
80
end
81
84
print("paramset ["..self.name.."]")
85
for k,v in pairs(self.params) do
86
print(k.." "..v.name.." = "..v:string())
92
return self.params[index].name
93
end
94
95
--- string
97
if type(index) == "string" then index = self.lookup[index] end
98
return self.params[index]:string()
99
end
100
101
--- set
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
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
127
if type(index) == "string" then index = self.lookup[index] end
128
self.params[index]:delta(d)
129
end
130
131
--- set action
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
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
165
local fd = io.open(data_dir..filename, "w+")
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
178
local fd = io.open(data_dir..filename, "r")
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)
196
self.params[index]:set(value)
197
end
198
end
200
201
end
202
else
203
print("paramset: "..filename.." not read.")
204
end
216
self.name = ""
217
self.params = {}
218
self.count = 0
219
end
220