Module: Ruby2Faust::Live

Defined in:
lib/ruby2faust/live.rb

Overview

Live reload support: graph diffing, compilation, crossfade.

Class Method Summary collapse

Class Method Details

.changed?(old_graph, new_graph) ⇒ Boolean

Check if two graphs have different structure

Parameters:

  • old_graph (DSP, Node)

    Previous graph

  • new_graph (DSP, Node)

    New graph

Returns:

  • (Boolean)

    True if structures differ



16
17
18
19
20
# File 'lib/ruby2faust/live.rb', line 16

def changed?(old_graph, new_graph)
  old_node = old_graph.is_a?(DSP) ? old_graph.node : old_graph
  new_node = new_graph.is_a?(DSP) ? new_graph.node : new_graph
  !old_node.same_structure?(new_node)
end

.compile(graph, output:, imports: Emitter::DEFAULT_IMPORTS) ⇒ String

Compile a DSP graph to a Faust file

Parameters:

  • graph (DSP)

    The DSP graph to compile

  • output (String)

    Output file path (.dsp)

  • imports (Array<String>) (defaults to: Emitter::DEFAULT_IMPORTS)

    Libraries to import

Returns:

  • (String)

    Path to the output file



28
29
30
31
32
# File 'lib/ruby2faust/live.rb', line 28

def compile(graph, output:, imports: Emitter::DEFAULT_IMPORTS)
  code = Emitter.program(graph, imports: imports)
  File.write(output, code)
  output
end

.crossfade_dsp(old_process, new_process, duration: 0.05) ⇒ String

Generate crossfade DSP code for smooth transitions Creates a Faust program that crossfades between old and new DSP

Parameters:

  • old_process (String)

    Faust expression for old process

  • new_process (String)

    Faust expression for new process

  • duration (Float) (defaults to: 0.05)

    Crossfade duration in seconds (default 0.05)

Returns:

  • (String)

    Faust source with crossfade



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby2faust/live.rb', line 41

def crossfade_dsp(old_process, new_process, duration: 0.05)
  "    import(\"stdfaust.lib\");\n\n    // Crossfade envelope\n    xfade = hslider(\"xfade\", 0, 0, 1, 0.001) : si.smoo;\n\n    // Old and new processes\n    old = \#{old_process};\n    new = \#{new_process};\n\n    // Crossfade: (1-x)*old + x*new\n    process = old * (1 - xfade), new * xfade :> _;\n  FAUST\nend\n"

.faust_compile(dsp_file, target: :cpp, output_dir: nil) ⇒ Boolean

Run the Faust compiler on a .dsp file Requires faust to be in PATH

Parameters:

  • dsp_file (String)

    Path to .dsp file

  • target (Symbol) (defaults to: :cpp)

    Compilation target (:wasm, :cpp, :llvm)

  • output_dir (String) (defaults to: nil)

    Output directory (default: same as input)

Returns:

  • (Boolean)

    True if compilation succeeded



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby2faust/live.rb', line 64

def faust_compile(dsp_file, target: :cpp, output_dir: nil)
  output_dir ||= File.dirname(dsp_file)
  basename = File.basename(dsp_file, ".dsp")

  cmd = case target
        when :wasm
          "faust2wasm #{dsp_file} -o #{output_dir}/#{basename}.wasm"
        when :cpp
          "faust -a minimal.cpp #{dsp_file} -o #{output_dir}/#{basename}.cpp"
        when :llvm
          "faust -lang llvm #{dsp_file} -o #{output_dir}/#{basename}.ll"
        else
          raise ArgumentError, "Unknown target: #{target}"
        end

  system(cmd)
end