/usr/bin/faust2sc is in faust 0.9.95~repack1-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | #!/usr/bin/env ruby
# ======================================================================
# faust2sc - Generate language modules from Faust XML.
# Copyright (C) 2005-2008 Stefan Kersten
# ======================================================================
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
# ======================================================================
# TODO:
# rexml is dog slow, maybe use libxml?
require 'getoptlong'
require 'rexml/document'
PROGRAM = File.basename($0)
PROGRAM_VERSION = "1.1.0"
class Array
def flatten1
res = []
self.each { |l|
res += l
}
res
end
end
module REXML
class Element
def to_i
self.text.to_i
end
def to_f
self.text.to_f
end
end
end
class String
def encapitalize
self[0..0].upcase + self[1..-1]
end
def decapitalize(greedy=false)
unless greedy
self[0..0].downcase + self[1..-1]
else
res = self.clone
(0..res.size-1).each { |i|
c = res[i]
if 65 <= c && c <= 90
res[i] = c + 32
else
break
end
}
res
end
end
end
def print_error(str)
$stderr.print("#{PROGRAM}[ERROR] #{str}")
end
def print_info(str)
$stderr.print("#{PROGRAM}[INFO] #{str}")
end
module Faust
class Widget
attr_reader :type, :id, :label, :init, :min, :max, :step
def initialize(node)
@type = node.attributes["type"]
@id = node.attributes["id"]
if (node.elements["label"].text)
@label = node.elements["label"].text
else
print_info("No label for widget ID #{id} - calling it widget#{id}\n")
@label = "widget#{id}"
end
if (@label =~ /^[0-9]/)
plabel = @label
@label = @type + "_" + @label
print_info("Widget label `#{plabel}' prefixed to become `#{@label}'\n")
end
dict = node.elements
@init = dict["init"].to_f
@min = dict["min"].to_f
@max = dict["max"].to_f
@step = dict["step"].to_f
if (@type == "button")
@max = 1 # no metadata other than name for buttons
@step = 1
end
end
end
class UI
attr_reader :active_widgets, :passive_widgets
def initialize(node)
@active_widgets = node.get_elements("//activewidgets/widget").collect { |x| Widget.new(x) }
@passive_widgets = node.get_elements("//passivewidgets/widget").collect { |x| Widget.new(x) }
end
end
class Plugin
attr_reader :path, :name, :author, :copyright, :license, :inputs, :outputs, :ui
def initialize(path, node)
@path = path
%w(name author copyright license).each { |name|
instance_variable_set("@#{name}", node.elements["/faust/#{name}"].text)
}
%w(inputs outputs).each { |name|
instance_variable_set("@#{name}", node.elements["/faust/#{name}"].text.to_i)
}
@ui = UI.new(node.elements["/faust/ui"])
end
def total_inputs
inputs + ui.active_widgets.size
end
def Plugin::from_file(path)
self.new(path, REXML::Document.new(File.open(path) { |io| io.read }))
end
end
class Generator
attr_reader :plugins, :options
def initialize(plugins, options)
@plugins = plugins
@options = options
end
def lang
"unknown"
end
def generate(io)
generate_header(io)
generate_body(io)
generate_footer(io)
end
def generate_header(io)
end
def generate_footer(io)
end
def generate_body(io)
plugins.each_with_index { |plugin,i|
if plugin
begin
print_info("Generating #{lang} code for #{plugin.name} ...\n")
generate_plugin(io, plugin)
if i < (plugins.size - 1)
io.print("\n")
end
rescue
print_error("#{$!}\n")
$!.backtrace.each { |l| print_error(l + "\n") }
print_error("Omitting #{plugin.path}\n")
end
end
}
io.print("\n")
end
def generate_plugin(io, plugin)
raise "#{self.class}::generate_plugin() missing!"
end
end
module Language
IDENTIFIER_REGEXP = /^[a-z][a-zA-Z0-9_]*[a-zA-Z0-9]?$/
def make_identifier(name)
# gentle identifier massage
# remove quotes
name = name.sub(/^"([^"]*)"/, "\\1")
# replace invalid chars with underscores
name = name.downcase.gsub(/[^a-zA-Z0-9_]/, "_")
# reduce multiple underscores to one
name = name.gsub(/__+/, "_")
# remove leading/terminating underscores
name = name.sub(/(^_|_$)/, "")
# move leading digits to the end
name = name.sub(/^([0-9]+)_/, "") + ("_#{$1}" if $1).to_s
# if digit(s) only, prepend alpha prefix
if (name[0..0] =~ /^[0-9]/)
pname = name
name = "w" + name
print_info("Widget label `#{pname}' prefixed to become `#{name}'\n")
end
unless name =~ IDENTIFIER_REGEXP
raise "invalid identifier: \"#{name}\""
end
name
end
def make_unique(list)
# bad, bad, bad
list = list.clone
res = []
ids = {}
while hd = list.shift
if ids.has_key?(hd)
ids[hd] = id = ids[hd] + 1
else
if list.include?(hd)
ids[hd] = id = 0
end
end
res << (id ? "#{hd}_#{id}" : hd)
end
res
end
module_function :make_identifier, :make_unique
end
module Haskell
INDENT = " " * 4
def mk_function_names(plugin)
fname = plugin.name.decapitalize(true)
lname = fname + "'"
[lname, fname]
end
module_function :mk_function_names
class PluginGenerator
attr_reader :plugin
def initialize(plugin)
@plugin = plugin
end
def generate(io)
lname, fname = Haskell.mk_function_names(plugin)
gen_curry_func(io, lname, fname)
io << "\n"
gen_list_func(io, lname)
end
def gen_ugen(io, name, rate, inputs, num_outputs)
io << "UGen.mkUGen #{rate} \"#{name}\" #{inputs} (replicate #{num_outputs} #{rate}) (UGen.Special 0) Nothing"
io << "\n"
end
def gen_curry_func(io, lname, fname)
args = mk_args(plugin.total_inputs)
decs = args.collect { "UGen" } + ["UGen"] # add result type
io << "#{fname} :: #{decs.join(" -> ")}\n"
io << "#{fname} #{args.join(" ")} = #{lname} [#{args.join(',')}]\n"
end
def gen_list_func(io, fname)
io << "#{fname} :: [UGen] -> UGen\n"
io << "#{fname} args = "
gen_ugen(io, plugin.name, "UGen.AR", "args", plugin.outputs)
end
protected
def mk_args(n, x="x")
(1..n).collect { |i| "#{x}#{i}" }
end
end # PluginGenerator
class Generator < Faust::Generator
def initialize(plugins, options)
super(plugins, options)
@module = options["prefix"]
end
def lang
"haskell"
end
def generate_header(io)
gen_module(io, plugins.collect { |p| Haskell.mk_function_names(p) }.flatten1)
io << "\n"
gen_imports(io)
io << "\n"
end
def generate_plugin(io, plugin)
PluginGenerator.new(plugin).generate(io)
end
def gen_module(io, exports)
#m = @module.empty? ? "" : @module + "."
#io << "module #{m}#{plugin.name.encapitalize} (\n"
io << "module #{@module.empty? ? "Main" : @module} (\n"
io << exports.collect { |x| (INDENT * 1) + x }.join(",\n") << "\n"
io << ") where\n"
end
def gen_imports(io)
io << "import Sound.SC3.UGen (UGen)\n"
io << "import qualified Sound.SC3.UGen as UGen\n"
end
end
end
module SC3
include Language
CLASS_REGEXP = /^[A-Z][a-zA-Z0-9_]*[a-zA-Z0-9]?$/
def path_to_unitname(path)
name = File.basename(path)
if ext_index = name.index(".")
name = name[0..ext_index-1]
end
name
end
def make_class_name(unit_name, prefix)
# The SuperCollider UGen class name generated here must match
# that generated in the Faust architecture file supercollider.cpp
if prefix == ""
prefix = "Faust"
end
class_name = unit_name
class_name = class_name.sub(/^#{prefix}/i, '')
class_name = prefix + '_' + class_name
cna = class_name.split(/[^0-9a-z]/i)
cna.each_index { |i| cna[i] = cna[i].encapitalize } # SC name convention
class_name = cna.join
print_info(unit_name + " -> " + class_name + "\n")
if class_name.length > 31 # maximum SC Node ID length is 31 (hashed)
print_info("Class name `#{class_name}' truncated to 1st 31 chars\n")
class_name = class_name[0,30]
end
unless class_name =~ CLASS_REGEXP
raise "invalid class name: \"#{class_name}\""
end
class_name
end
module_function :path_to_unitname, :make_class_name
class Faust::Widget
def sc3_identifier
l = self.label
l = l.gsub(/\([^\)]*\)/, '')
l = Language.make_identifier(l)
if l.length > 30 # maximum SC ParamSpec length is 31 (hashed)
lt = l[0,29] # leave a final char for make_unique
print_info("Label `#{l}' for #{self.type} #{self.id} truncated to " +
"`#{lt}' (30 chars)\n")
l = lt
end
l
end
def sc3_arg_string
"#{self.sc3_identifier}(#{self.init})"
end
def sc3_default
"(#{self.init})"
end
def sc3_default2
"=(#{self.init})" # Parens make unary minus work in SC
end
end
class PluginGenerator
attr_reader :unit_name, :class_name
def initialize(plugin, options)
@plugin = plugin
@options = options
@unit_name = plugin.name || SC3::path_to_unitname(plugin.path)
@class_name = SC3::make_class_name(@unit_name, options["prefix"])
end
def inputs
@plugin.inputs
end
def outputs
@plugin.outputs
end
def superclass_name
@plugin.outputs > 1 ? "MultiOutUGen" : "UGen"
end
def input_names
(1..self.inputs).collect { |i| "in#{i}" }
end
def control_names
Language::make_unique(@plugin.ui.active_widgets.collect { |x| x.sc3_identifier })
end
def decl_args # "name(default)"
cnames = self.control_names
cdefaults = @plugin.ui.active_widgets.collect { |x| x.sc3_default }
args = self.input_names + cnames.zip(cdefaults).collect { |ary| ary[0] + ary[1] }
args.empty? ? "" : " | " + args.join(", ") + " |"
end
def decl_args2 # "name=default"
cnames = self.control_names
cdefaults = @plugin.ui.active_widgets.collect { |x| x.sc3_default2 }
args = self.input_names + cnames.zip(cdefaults).collect { |ary| ary[0] + ary[1] }
args.empty? ? "" : args.join(", ") + ";"
end
def decl_args3 # args for SynthDef
cnames = self.control_names
cdefaults = @plugin.ui.active_widgets.collect { |x| x.sc3_default }
args = self.input_names.collect { |x| "\\#{x}.ar(0)" } \
+ cnames.zip(cdefaults).collect { |x| "\\#{x[0]}.kr#{x[1]}" }
args.empty? ? "" : args.join(", ")
end
def decl_buses # declare input buses for Synth
args = self.input_names.collect { |x| \
" #{x}Bus = Bus.audio(s,1);\n"};
end
def decl_args4 # args for Synth
cnames = self.control_names
cdefaults = @plugin.ui.active_widgets.collect { |x| x.sc3_default }
args = self.input_names.collect { |x| "#{x}:#{x}Bus.asMap"} \
+ cnames.collect { |x| "#{x}:#{x}Var" }
args.empty? ? "" : "\n\t[\t" + args.join(",\n\t\t") + "\n\t]"
end
def decl_metadata
cnames = self.control_names
cnames.collect! {|x| " \\"+x+":"}
warp = 0
cmeta = @plugin.ui.active_widgets.collect \
{ |x| "[#{x.min}, #{x.max}, #{warp}, #{x.step}, #{x.init}].asSpec" }
# \\name1: [minval, maxval, warp, step, default, units].asSpec,
# Note: could append x.units as well
args = cnames.zip(cmeta).collect { |ary| ary[0] + ary[1] }
args.empty? ? "" : args.join(",\n ")
end
def new_args(rate)
["'%s'" % rate] + self.input_names + self.control_names
end
def validate
args = self.input_names + self.control_names
unless args.uniq == args
raise "argument list not unique"
end
self
end
def generate(io)
self.validate
generate_decl(io)
generate_body(io)
end
def generate_decl(io)
io.print("#{@class_name} : #{self.superclass_name}\n")
print_info("UGen: #{@class_name}\n")
end
def generate_body(io)
io.print("{\n")
body = <<EOF
*ar {#{self.decl_args}
^this.multiNew(#{self.new_args(:audio).join(", ")})
}
*kr {#{self.decl_args}
^this.multiNew(#{self.new_args(:control).join(", ")})
}
EOF
if self.inputs > 0
body = body + <<EOF
checkInputs {
if (rate == 'audio', {
#{self.inputs}.do({|i|
if (inputs.at(i).rate != 'audio', {
^(" input at index " + i + "(" + inputs.at(i) +
") is not audio rate");
});
});
});
^this.checkValidInputs
}
EOF
end
io.print(body)
generate_outputs(io)
if @options["synthdef"]
generate_synthdef(io)
end
io.print("\n name { ^\"#{@class_name}\" }\n")
io.print("}\n")
end
def generate_outputs(io)
if self.outputs > 1 # add initOutputs to overridden init:
io.print <<EOF
init { | ... theInputs |
inputs = theInputs
^this.initOutputs(#{self.outputs}, rate)
}
EOF
end
end
def generate_synthdef(io)
s = @class_name
sdname = s[0,1].downcase + s[1 .. s.length]
print_info("SynthDef: \\#{sdname}\n")
# if \arg.ar(0) form not used, add after out=0: ,#{self.decl_args2}
io.print <<EOF
/*
SynthDef - Typical usage in SuperCollider:
EOF
print_info("SynthDef: have #{self.input_names.size} inputs\n")
if self.input_names.size>0
io.print "#{decl_buses}\n";
end
io.print <<EOF
~synth = Synth(\\#{sdname}, #{self.decl_args4});
*/
*initClass {
StartUp.add {
SynthDesc.mdPlugin = TextArchiveMDPlugin;
// When SynthDef.writeOnce writes metadata:
// SynthDef.writeOnce(\\#{sdname},
SynthDef(\\#{sdname},
{ |out=0|
Out.ar(out,
#{@class_name}.ar(
#{self.decl_args3}
)
)
}, metadata: (specs:(
//\\controlName:[min, max, warp, step, default, units].asSpec,
#{self.decl_metadata}
) ) ).store } }
// When SynthDef.writeOnce writes metadata:
//) ) ) } }
EOF
end
end
class Generator < Faust::Generator
def lang
"sclang"
end
def generate_plugin(io, plugin)
PluginGenerator.new(plugin, options).generate(io)
end
end
end # module SC3
end # module Faust
def usage
$stdout.print <<EOF
Usage: #{File.basename($0)} [OPTION]... INPUT_FILE...
Generate a language module file from FAUST generated XML.
Currently supported languages are Haskell and SuperCollider.
Options:
-h, --help Display this help
-l, --lang Set output language (haskell, sclang)
-o, --output Set output file name
-p, --prefix Set class or module prefix
-s, --synthdef Emit SynthDef including metadata (sclang)
-V, --version Display version information
EOF
end
opts = GetoptLong.new(
[ "--help", "-h", GetoptLong::NO_ARGUMENT ],
[ "--lang", "-l", GetoptLong::REQUIRED_ARGUMENT ],
[ "--output", "-o", GetoptLong::REQUIRED_ARGUMENT ],
[ "--prefix", "-p", GetoptLong::REQUIRED_ARGUMENT ],
[ "--synthdef", "-s", GetoptLong::NO_ARGUMENT ],
[ "--version", "-V", GetoptLong::NO_ARGUMENT ]
)
LANG_MAP = {
"sclang" => Faust::SC3::Generator,
"haskell" => Faust::Haskell::Generator
}
lang = "sclang"
generator = nil
output_file = nil
options = {
"prefix" => "",
"synthdef" => false
}
opts.each { | opt, arg |
case opt
when "--help"
usage
exit(0)
when "--lang"
lang = arg
when "--output"
output_file = arg
when "--prefix"
options["prefix"] = arg
options["module"] = arg
when "--synthdef"
options["synthdef"] = true
when "--version"
puts "#{PROGRAM} #{PROGRAM_VERSION}"
exit(0)
end
}
if LANG_MAP.key?(lang)
generator = LANG_MAP[lang]
else
print_error("unknown output language #{lang}\n")
exit(1)
end
if output_file
output = File.open(output_file, "w")
else
output = $stdout
end
plugins = ARGV.collect { |file|
begin
print_info("Parsing #{file} ...\n")
Faust::Plugin.from_file(file)
rescue
print_error("#{$!}\n")
print_error("Omitting #{file}\n")
end
}
begin
generator.new(plugins, options).generate(output)
ensure
output.close unless output === $stdout
end
# EOF
|