1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
# Information about all variables
variables = ref([])
# Float variables
variables_float = ref([])
# Int variables
variables_int = ref([])
# Bool variables
variables_bool = ref([])
# String variables
variables_string = ref([])
let interactive = ()
let interactive.float = ()
let interactive.int = ()
let interactive.bool = ()
let interactive.string = ()
let interactive.error = error.register("interactive.error")
# @flag hidden
def interactive.list(_)
l = !variables
l = list.map(fun(xv) -> begin
let (x,v) = xv
"#{x} : #{v.type}"
end, l)
string.concat(separator="\n", l)
end
server.register(usage="list", description="List available interactive variables.", namespace="var", "list", interactive.list)
# Description of an interactive variable.
# @flag hidden
def interactive.description(name)
list.assoc(name, !variables).description
end
# Type of an interactive variable.
# @flag hidden
def interactive.type(name)
list.assoc(name, !variables).type
end
# @flag hidden
def interactive.float.ref(name)
list.assoc(name, !variables_float).ref
end
# @flag hidden
def interactive.int.ref(name)
list.assoc(name, !variables_int).ref
end
# @flag hidden
def interactive.bool.ref(name)
list.assoc(name, !variables_bool).ref
end
# @flag hidden
def interactive.string.ref(name)
list.assoc(name, !variables_string).ref
end
# @flag hidden
def interactive.remove(name)
variables := list.assoc.remove.all(name, !variables)
end
# Remove an interactive variable.
# @param name Name of the variable.
# @category Interactive
# @flag hidden
def interactive.float.remove(name)
interactive.remove(name)
variables_float := list.assoc.remove.all(name, !variables_float)
end
# Remove an interactive variable.
# @param name Name of the variable.
# @category Interactive
# @flag hidden
def interactive.int.remove(name)
interactive.remove(name)
variables_int := list.assoc.remove.all(name, !variables_int)
end
# Remove an interactive variable.
# @param name Name of the variable.
# @category Interactive
# @flag hidden
def interactive.bool.remove(name)
interactive.remove(name)
variables_bool := list.assoc.remove.all(name, !variables_bool)
end
# Remove an interactive variable.
# @param name Name of the variable.
# @category Interactive
# @flag hidden
def interactive.string.remove(name)
interactive.remove(name)
variables_string := list.assoc.remove.all(name, !variables_string)
end
# Function called to ensure persistency of data.
let interactive.persistency = ref(fun()->())
# @flag hidden
def interactive.float.set(name, v)
interactive.float.ref(name) := v
p = !interactive.persistency
p()
end
# @flag hidden
def interactive.int.set(name, v)
interactive.int.ref(name) := v
p = !interactive.persistency
p()
end
# @flag hidden
def interactive.bool.set(name, v)
interactive.bool.ref(name) := v
p = !interactive.persistency
p()
end
# @flag hidden
def interactive.string.set(name, v)
interactive.string.ref(name) := v
p = !interactive.persistency
p()
end
%ifdef osc
let stdlib_osc = osc
%endif
# Create an interactive variable.
# @flag hidden
# @param ~name Name of the variable.
# @param ~description Description of the variable.
# @param ~osc OSC address for the variable.
# @param ~type Type of the variable.
def interactive.create(~name, ~description="", ~osc="", ~type)
if list.assoc.mem(name, !variables) then error.raise(interactive.error, "variable already registered") end
variables := (name, { type=type, description=description }) :: !variables
variables := list.sort(fun(n, n') -> if fst(n) < fst(n') then -1 else 1 end, !variables)
%ifdef osc
if osc != "" then
if type == "float" then
stdlib_osc.on_float(osc, interactive.float.set(name))
elsif type == "int" then
stdlib_osc.on_int(osc, interactive.int.set(name))
elsif type == "bool" then
stdlib_osc.on_bool(osc, interactive.bool.set(name))
elsif type == "string" then
stdlib_osc.on_string(osc, interactive.string.set(name))
else
error.raise(error.not_found)
end
end
%else
ignore(osc)
%endif
end
# @flag hidden
def interactive.get(name)
try
t = interactive.type(name)
if t == "float" then
r = interactive.float.ref(name)
string_of_float(decimal_places=3, !r)
elsif t == "int" then
r = interactive.int.ref(name)
string_of(!r)
elsif t == "bool" then
r = interactive.bool.ref(name)
string_of(!r)
elsif t == "string" then
r = interactive.string.ref(name)
!r
else
error.raise(error.not_found)
end
catch _ do
"Variable not found."
end
end
server.register(namespace="var", description="Get the value of a variable.", "get", interactive.get)
# @flag hidden
def interactive.set(arg)
try
arg = r/=/.split(arg)
name = list.nth(arg, 0)
value = list.nth(arg, 1)
t = interactive.type(name)
if t == "float" then
interactive.float.set(name, float_of_string(value))
elsif t == "int" then
interactive.int.set(name, int_of_string(value))
elsif t == "bool" then
interactive.bool.set(name, bool_of_string(value))
elsif t == "string" then
interactive.string.set(name, value)
else
error.raise(error.not_found)
end
"Variable #{name} set."
catch _ do
"Syntax error or variable not found."
end
end
server.register(usage="set <name> = <value>", description="Set the value of a variable.", namespace="var", "set", interactive.set)
# Save the value of all interactive variables in a file.
# @category Interactive
# @param fname Name of the file.
def interactive.save(fname)
let json.stringify data = {
float = list.map(fun (nv) -> begin let (name, v) = nv; (name, !v.ref) end, !variables_float),
int = list.map(fun (nv) -> begin let (name, v) = nv; (name, !v.ref) end, !variables_int),
bool = list.map(fun (nv) -> begin let (name, v) = nv; (name, !v.ref) end, !variables_bool),
string = list.map(fun (nv) -> begin let (name, v) = nv; (name, !v.ref) end, !variables_string)
}
file.write(data=data, fname)
end
# Load the value of interactive variables from a file.
# @category Interactive
# @param fname Name of the file.
def interactive.load(fname)
vars = file.contents(fname)
let json.parse ( vars: {
float : [(string * float)],
int : [(string * int)],
bool : [(string * bool)],
string : [(string * string)]
} ) = vars
list.iter(fun (nv) -> try interactive.float.set (fst(nv), snd(nv)) catch _ do log.important(label="interactive.load", "Variable #{fst(nv)} not found.") end, vars.float )
list.iter(fun (nv) -> try interactive.int.set (fst(nv), snd(nv)) catch _ do log.important(label="interactive.load", "Variable #{fst(nv)} not found.") end, vars.int )
list.iter(fun (nv) -> try interactive.bool.set (fst(nv), snd(nv)) catch _ do log.important(label="interactive.load", "Variable #{fst(nv)} not found.") end, vars.bool )
list.iter(fun (nv) -> try interactive.string.set(fst(nv), snd(nv)) catch _ do log.important(label="interactive.load", "Variable #{fst(nv)} not found.") end, vars.string)
end
# Make the value of interactive variables persistent: they are loaded from the
# given file and stored there whenever updated. This function should be called
# after all interactive variables have been defined (variables not declared yet
# will not be loaded).
# @category Interactive
# @param fname Name of the file.
def interactive.persistent(fname)
if file.exists(fname) then
interactive.load(fname)
else
interactive.save(fname)
end
interactive.persistency := {interactive.save(fname)}
end
# Expose interactive variables through habor http server.
# @category Interaction
# @param ~port Port of the server.
# @param ~uri URI of the server.
def interactive.harbor(~port=8000, ~uri="/interactive")
def webpage(~protocol=_, ~data, ~headers=_, _)
form_data = data
data = ref("")
def add(s) =
data := !data ^ s ^ "\n"
end
title = "Interactive values"
add("<html><head>")
add("<meta charset='utf-8'/>")
add("<title>#{title}</title>")
add("<style>
body {
font-family: sans-serif;
}
h1 {
text-align: center;
}
form {
border-radius: 20px;
display: block;
background-color: whitesmoke;
width: max-content;
margin: 0 auto;
padding: 2ex;
display:grid;
grid-template-columns: max-content max-content;
grid-gap: 5px;
}
label {
text-align: right;
}
input {
width: 300px;
}
</style>")
# TODO: we could send only the updated value instead of sending them all
add("<script>
function send() {
var interactive = document.getElementsByClassName('interactive');
var data = '';
for(var i=0; i<interactive.length; i++){
if (interactive[i].type == 'checkbox') {
if (interactive[i].checked) {
interactive[i].value = 'true'
} else {
interactive[i].value = 'false'
}
}
data = data.concat(interactive[i].name+'='+interactive[i].value)+'&';
}
console.log(data);
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', '#{uri}');
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState === XMLHttpRequest.DONE) {
var status = xmlHttp.status;
if (status === 0 || (status >= 200 && status < 400)) {
//console.log(xmlHttp.responseText);
} else {
console.log('Failed to send values.')
}
}
}
xmlHttp.send(data);
}
</script>")
add("</head><body>")
add("<h1>#{title}</h1>")
def add_var(nv)
let (name, v) = nv
description = interactive.description(name)
description = if description == "" then name else "#{description} (#{name})" end
# add("<div>")
add("<label for=#{name}>#{description}</label>")
common = "id='#{name}' name='#{name}' class='interactive' onchange=\"send()\""
if v.type == "float" then
v = list.assoc(name, !variables_float)
value = http.string_of_float(!v.ref)
if v.min == 0.-infinity or v.max == infinity then
add("<input type='number' #{common} step='#{v.step}' value='#{value}'>")
else
min = http.string_of_float(v.min)
max = http.string_of_float(v.max)
step = http.string_of_float(v.step)
value = http.string_of_float(!v.ref)
unit = if v.unit == "" then "" else " "^v.unit end
add("<div><input type='range' #{common} min='#{min}' max='#{max}' step='#{step}' value='#{value}' oninput='document.getElementById(\"#{name}-value\").innerHTML = this.value+\"#{unit}\"'><text id='#{name}-value' style='inline'>#{value}#{unit}</text></div>")
end
elsif v.type == "int" then
v = list.assoc(name, !variables_int)
value = string_of(!v.ref)
add("<input type='number' #{common} step='1' value='#{value}'>")
elsif v.type == "bool" then
v = list.assoc(name, !variables_bool)
c = (!v.ref) ? "checked" : ""
add("<input type='checkbox' #{common} value='true' #{c}>")
elsif v.type == "string" then
v = list.assoc(name, !variables_string)
add("<input type='text' #{common} value='#{!v.ref}'>")
else
()
end
# add("</div>")
end
add("<form>")
list.iter(add_var, !variables)
# add("<input type='submit' value='Send'>")
add("</form>")
add("</body>")
http.response(data=!data)
end
harbor.http.register(port=port, method="GET", uri, webpage)
def setter(~protocol=_, ~data, ~headers=_, _)
data = url.split_args(data)
def update(nv)
let (name, v) = nv
try
t = interactive.type(name)
if t == "float" then
interactive.float.set(name, float_of_string(v))
elsif t == "int" then
interactive.int.set(name, int_of_string(v))
elsif t == "bool" then
interactive.bool.set(name, bool_of_string(v))
elsif t == "string" then
interactive.string.set(name, v)
end
catch _ do
log.important(label="interactive.harbor", "Could not update variable #{name}.")
end
end
list.iter(update, data)
http.response()
end
harbor.http.register(port=port, method="POST", uri, setter)
log.important(label="interactive.harbor", "Website should be ready at <http://localhost:#{port}#{uri}>.")
end
# Read a float from an interactive input.
# @category Interaction
# @param ~min Minimal value.
# @param ~max Maximal value.
# @param ~step Typical variation of the value.
# @param ~description Description of the variable.
# @param ~unit Unit for the variable.
# @param ~osc OSC address.
# @param name Name of the variable.
# @param v Initial value.
def replaces interactive.float(~min=0.-infinity, ~max=infinity, ~step=0.1, ~description="", ~unit="", ~osc="", name, v)
interactive.create(name=name, description=description, osc=osc, type="float")
r = ref(v)
variables_float := (name, {ref=r, unit=unit, min=min, max=max, step=step }) :: !variables_float
ref.getter(r).{set = fun(x) -> interactive.float.set(name, x), remove = {interactive.float.remove(name)}}
end
# Read an integer from an interactive input.
# @category Interaction
# @param ~description Description of the variable.
# @param ~osc OSC address.
# @param name Name of the variable.
# @param v Initial value.
def replaces interactive.int(~description="", ~osc="", name, v)
interactive.create(name=name, description=description, osc=osc, type="int")
r = ref(v)
variables_int := (name, {ref=r }) :: !variables_int
ref.getter(r).{set = fun(x) -> interactive.int.set(name, x), remove = {interactive.int.remove(name)}}
end
# Read a boolean from an interactive input.
# @category Interaction
# @param ~description Description of the variable.
# @param ~osc OSC address.
# @param name Name of the variable.
# @param v Initial value.
def replaces interactive.bool(~description="", ~osc="", name, v)
interactive.create(name=name, description=description, osc=osc, type="bool")
r = ref(v)
variables_bool := (name, { ref=r }) :: !variables_bool
ref.getter(r).{set = fun(x) -> interactive.bool.set(name, x), remove = {interactive.bool.remove(name)}}
end
# Read a string from an interactive input.
# @category Interaction
# @param ~description Description of the variable.
# @param ~osc OSC address.
# @param name Name of the variable.
# @param v Initial value.
def replaces interactive.string(~description="", ~osc="", name, v)
interactive.create(name=name, description=description, osc=osc, type="string")
r = ref(v)
variables_string := (name, { ref=r }) :: !variables_string
ref.getter(r).{set = fun(x) -> interactive.string.set(name, x), remove = {interactive.string.remove(name)}}
end
# Create a multiband compressor whose parameters are interactive variables.
# @category Interaction
# @param ~id Id of the source. Variable names are prefixed with this.
# @param ~bands Number of bands.
# @param s Source to compress.
def compress.multiband.interactive(~id="compress", ~bands=5, s)
prefix = id^"_"
wet = interactive.float(prefix^"wet", min=0., max=1., 1.)
def band(i)
frequency = 100. + 10. * exp(ln(25000.) * float_of_int(i) / float_of_int(bands))
frequency = interactive.float("#{prefix}frequency#{i}", unit="Hz", min=0., max=20000., step=10., frequency)
attack = interactive.float("#{prefix}attack#{i}", unit="ms", min=0., max=1000., step=10., 100.)
release = interactive.float("#{prefix}release#{i}", unit="ms", min=0., max=1000., step=10., 200.)
threshold = interactive.float("#{prefix}threshold#{i}", unit="dB", min=-20., max=0., step=0.1, -10.)
ratio = interactive.float("#{prefix}ratio#{i}", min=1., max=10., step=0.1, 4.)
gain = interactive.float("#{prefix}gain#{i}", unit="dB", min=0., max=30., step=0.1, 3.)
{frequency=frequency, attack=attack, release=release, threshold=threshold, ratio=ratio, gain=gain}
end
l = list.init(bands, band)
compress.multiband(wet=wet, s, l)
end
|