Class: Vizcore::DSL::LayerBuilder
- Inherits:
-
Object
- Object
- Vizcore::DSL::LayerBuilder
- Includes:
- ColorHelpers, LayoutHelpers
- Defined in:
- lib/vizcore/dsl/layer_builder.rb
Overview
Builder for one render layer in a scene.
Defined Under Namespace
Classes: ShapeReference
Constant Summary collapse
- NO_ARGUMENT =
Object.new.freeze
- SHAPE_SCHEMA_VERSION =
2- MAPPING_SOURCE_KINDS =
%i[ amplitude peak frequency_band frequency_band_peak fft_spectrum onset kick snare hihat beat beat_confidence beat_pulse beat_count bpm beat_phase beat_2 beat_4 beat_8 beat_triplet triplet bar_phase bar_count phrase_count bpm_confidence spectral_centroid spectral_rolloff spectral_flatness spectral_flux zero_crossing_rate global lfo adsr envelope ].freeze
- PATH_DEFAULT_DETAIL =
32- PATH_MIN_DETAIL =
4- PATH_MAX_DETAIL =
128- PATH_DEFAULT_MAX_SEGMENTS =
4096- SHAPE_TARGET_ALIASES =
{ "translate_x" => "transform.translate.x", "translate_y" => "transform.translate.y", "rotate" => "transform.rotate", "rotation" => "transform.rotate", "scale" => "transform.scale", "scale_x" => "transform.scale.x", "scale_y" => "transform.scale.y", "origin_x" => "transform.origin.x", "origin_y" => "transform.origin.y" }.freeze
- SHAPE_STYLE_KEYS =
Vizcore::Shape::STYLE_KEYS
- SHAPE_TRANSFORM_KEYS =
%i[translate rotate rotation scale origin].freeze
- STRICT_PARAM_ALLOWLIST =
%i[ custom_shape_controls custom_shapes glsl_source group origin rotate scale translate ].freeze
Instance Method Summary collapse
-
#adsr(source = :kick, attack: 0.02, decay: 0.08, sustain: 0.7, release: 0.16, threshold: 0.0, peak: 1.0) ⇒ Hash
Source descriptor for an ADSR envelope.
- #align(value) ⇒ Symbol
-
#amplitude ⇒ Hash
Source descriptor for overall amplitude.
- #arc_to(rx, ry, rotation, large_arc, sweep, x, y) ⇒ Object
-
#bar_count(value = NO_ARGUMENT) ⇒ Hash
Source descriptor for completed 4-beat bars.
-
#bar_phase ⇒ Hash
Source descriptor for 0.0..1.0 phase within the current 4-beat bar.
-
#bass ⇒ Hash
Source descriptor for the low/bass frequency band.
-
#bass_peak ⇒ Hash
Source descriptor for the held low/bass peak.
-
#beat ⇒ Hash
Source descriptor for beat trigger.
-
#beat? ⇒ Hash
Source descriptor for beat trigger.
-
#beat_2 ⇒ Hash
Source descriptor for half-beat subdivision pulses.
-
#beat_4 ⇒ Hash
Source descriptor for quarter-beat subdivision pulses.
-
#beat_8 ⇒ Hash
Source descriptor for eighth-beat subdivision pulses.
-
#beat_confidence ⇒ Hash
Source descriptor for beat detector confidence.
-
#beat_count ⇒ Hash
Source descriptor for beat counter.
-
#beat_phase ⇒ Hash
Source descriptor for 0.0..1.0 phase within the current beat.
-
#beat_pulse ⇒ Hash
Source descriptor for beat pulse decay value.
-
#beat_triplet ⇒ Hash
Source descriptor for triplet subdivision pulses.
-
#bezier(id = nil, from:, to:, control: nil, c1: nil, c2: nil, **options) { ... } ⇒ Hash
Declare a quadratic or cubic bezier curve.
- #blend(value) ⇒ Symbol
-
#bpm ⇒ Hash
Source descriptor for estimated BPM.
-
#bpm_confidence ⇒ Hash
Source descriptor for tempo estimator confidence.
-
#circle(id = nil, **options) { ... } ⇒ Hash
Declare a 2D circle/ring primitive for a shape layer.
- #close ⇒ Object
- #content(value) ⇒ String
- #count(value) ⇒ Integer
- #cubic_to(c1x, c1y, c2x, c2y, x, y) ⇒ Object
-
#custom_shape(renderer, **options) { ... } ⇒ Array<Hash>
Expand a registered Ruby custom shape into normal shape primitives.
-
#draw { ... } ⇒ Array<Hash>
Group shape primitives in a block for readability.
-
#envelope(source = :kick, **options) ⇒ Hash
Alias for ADSR envelope mapping source.
-
#evaluate { ... } ⇒ Vizcore::DSL::LayerBuilder
Evaluate a layer block.
-
#fft_spectrum ⇒ Hash
Source descriptor for FFT spectrum array.
- #file(path) ⇒ String
- #fill(value) ⇒ String
- #font(value) ⇒ String
- #font_size(value) ⇒ Integer
-
#frequency_band(name) ⇒ Hash
Source descriptor for a frequency band.
-
#frequency_band_peak(name) ⇒ Hash
Source descriptor for a held frequency-band peak.
-
#global(name) ⇒ Hash
Source descriptor for mutable runtime globals.
- #glsl(path) ⇒ String
-
#group(_id = nil, **attrs) { ... } ⇒ Array<Hash>
Apply shared style and transform to shape primitives declared in the block.
-
#high ⇒ Hash
Source descriptor for the high frequency band.
-
#high_peak ⇒ Hash
Source descriptor for the held high peak.
-
#hihat(value = NO_ARGUMENT) ⇒ Hash
Source descriptor for high-band percussive confidence.
- #horizontal_to(x) ⇒ Object
-
#initialize(name:, styles: {}, defaults: {}, mapping_presets: {}, strict: false) ⇒ LayerBuilder
constructor
A new instance of LayerBuilder.
-
#kick(value = NO_ARGUMENT) ⇒ Hash
Source descriptor for low-band percussive confidence.
- #letter_spacing(value) ⇒ Float
-
#lfo(wave = :sine, rate: 1.0, phase: 0.0) ⇒ Hash
Source descriptor for a time-based low-frequency oscillator.
-
#line(id = nil, **options) { ... } ⇒ Hash
Declare a 2D line primitive for a shape layer.
- #line_to(x, y) ⇒ Object
-
#low ⇒ Hash
Source descriptor for the low/bass frequency band.
-
#low_peak ⇒ Hash
Source descriptor for the held low/bass peak.
-
#map(definition = nil, **options, &block) ⇒ void
Map analysis source(s) to layer parameter target(s).
-
#method_missing(method_name, *args, &block) ⇒ Object
private
Stores dynamic one-argument setters into ‘params`.
-
#mid ⇒ Hash
Source descriptor for the mid frequency band.
-
#mid_peak ⇒ Hash
Source descriptor for the held mid peak.
- #move_to(x, y) ⇒ Object
-
#onset(band = nil) ⇒ Hash
Source descriptor for positive audio feature changes.
-
#opacity(value) ⇒ Float, Hash
Set layer or shape opacity.
-
#origin(*args, x: nil, y: nil) ⇒ Hash
Set a shape/layer transform origin.
-
#palette(*colors) ⇒ Array<String>
Store an ordered color palette for this layer.
-
#param(name, default: nil, range: nil, min: nil, max: nil, step: nil) ⇒ Hash
Declare numeric metadata for a shader/layer parameter.
-
#path(id = nil, **options) { ... } ⇒ Hash
Declare a path primitive using SVG-like path commands.
-
#peak ⇒ Hash
Source descriptor for absolute sample peak level.
-
#phrase_count ⇒ Hash
Source descriptor for completed 8-bar phrases.
-
#polygon(id = nil, **options) { ... } ⇒ Hash
Declare a closed polygon primitive for a shape layer.
-
#polyline(id = nil, **options) { ... } ⇒ Hash
Declare an open polyline primitive for a shape layer.
-
#post(name) ⇒ Symbol
Append one post effect to this layer’s post effect chain.
- #quad_to(cx, cy, x, y) ⇒ Object
-
#react_to(source_value) { ... } ⇒ void
High-level mapping DSL for describing audio reactions inside a layer.
-
#rect(id = nil, **options) { ... } ⇒ Hash
Declare a 2D rectangle primitive for a shape layer.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#rotate(value) ⇒ Float, Hash
Set a shape/layer rotation transform in degrees.
-
#scale(value = NO_ARGUMENT, x: nil, y: nil) ⇒ Float, Hash
Set a shape/layer scale transform.
- #shader(value, reload: nil) ⇒ Symbol
- #shadow(color: nil, blur: nil) ⇒ Hash
-
#shape(id) ⇒ ShapeReference
Return a reference object for mapping to a named shape.
-
#snare(value = NO_ARGUMENT) ⇒ Hash
Source descriptor for mid-band percussive confidence.
- #source(value, **options) ⇒ Symbol, Hash
-
#spectral_centroid ⇒ Hash
Source descriptor for spectral centroid in Hz.
-
#spectral_flatness ⇒ Hash
Source descriptor for spectral flatness.
-
#spectral_flux ⇒ Hash
Source descriptor for positive spectrum delta.
-
#spectral_rolloff ⇒ Hash
Source descriptor for spectral rolloff in Hz.
-
#star(id = nil, **options) { ... } ⇒ Hash
Declare a star polygon primitive for a shape layer.
- #stroke(value = NO_ARGUMENT, width: nil, color: nil) ⇒ Hash
-
#sub ⇒ Hash
Source descriptor for the sub-bass frequency band.
-
#sub_peak ⇒ Hash
Source descriptor for the held sub-bass peak.
-
#to_h ⇒ Hash
Serialized layer payload.
-
#translate(*args, x: nil, y: nil) ⇒ Hash
Set a shape/layer translation transform.
-
#treble ⇒ Hash
Source descriptor for the high/treble frequency band.
-
#treble_peak ⇒ Hash
Source descriptor for the held high/treble peak.
-
#triplet ⇒ Hash
Source descriptor for triplet subdivision pulses.
- #type(value) ⇒ Symbol
-
#use_mapping(name) ⇒ void
Apply a named mapping preset to this layer.
-
#use_style(name) ⇒ Hash
Apply a named style by merging its params into this layer.
- #vertical_to(y) ⇒ Object
-
#zero_crossing_rate ⇒ Hash
Source descriptor for time-domain zero crossing rate.
Methods included from LayoutHelpers
#circle_pack, #grid, #radial, #scatter, #spiral
Methods included from ColorHelpers
Constructor Details
#initialize(name:, styles: {}, defaults: {}, mapping_presets: {}, strict: false) ⇒ LayerBuilder
Returns a new instance of LayerBuilder.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 66 def initialize(name:, styles: {}, defaults: {}, mapping_presets: {}, strict: false) @name = name.to_sym @styles = styles @mapping_presets = mapping_presets @strict = !!strict @type = nil @shader = nil @glsl = nil @params = deep_dup(defaults) @param_schema = {} @mappings = [] @shape_index_by_id = {} @shape_group_stack = [{}] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Stores dynamic one-argument setters into ‘params`.
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 989 def method_missing(method_name, *args, &block) if @current_shape && block.nil? && args.length == 1 @current_shape[method_name.to_sym] = args.first return args.first end if @current_custom_shape && block.nil? && args.length == 1 @current_custom_shape[:params][method_name.to_sym] = args.first return args.first end if in_shape_group? && block.nil? && args.length == 1 current_shape_group[method_name.to_sym] = args.first return args.first end if block.nil? && args.length == 1 @params[method_name.to_sym] = args.first return args.first end super end |
Instance Method Details
#adsr(source = :kick, attack: 0.02, decay: 0.08, sustain: 0.7, release: 0.16, threshold: 0.0, peak: 1.0) ⇒ Hash
Returns source descriptor for an ADSR envelope.
949 950 951 952 953 954 955 956 957 958 959 960 961 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 949 def adsr(source = :kick, attack: 0.02, decay: 0.08, sustain: 0.7, release: 0.16, threshold: 0.0, peak: 1.0) source_value = source.nil? ? { kind: :kick } : normalize_source(source) mapping_source( :adsr, source: source_value, attack: normalize_non_negative_float(attack, :attack), decay: normalize_non_negative_float(decay, :decay), sustain: clamp(normalize_float(sustain, :sustain), 0.0, 1.0), release: normalize_non_negative_float(release, :release), threshold: normalize_float(threshold, :threshold), peak: normalize_float(peak, :peak) ) end |
#align(value) ⇒ Symbol
313 314 315 316 317 318 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 313 def align(value) alignment = value.to_sym raise ArgumentError, "unsupported text align: #{value.inspect}" unless %i[left center right].include?(alignment) @params[:align] = alignment end |
#amplitude ⇒ Hash
Returns source descriptor for overall amplitude.
705 706 707 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 705 def amplitude mapping_source(:amplitude) end |
#arc_to(rx, ry, rotation, large_arc, sweep, x, y) ⇒ Object
574 575 576 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 574 def arc_to(rx, ry, rotation, large_arc, sweep, x, y) append_path_command("A", rx, ry, rotation, large_arc, sweep, x, y) end |
#bar_count(value = NO_ARGUMENT) ⇒ Hash
Returns source descriptor for completed 4-beat bars.
879 880 881 882 883 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 879 def (value = NO_ARGUMENT) return @params[:bar_count] = Integer(value) unless value.equal?(NO_ARGUMENT) mapping_source(:bar_count) end |
#bar_phase ⇒ Hash
Returns source descriptor for 0.0..1.0 phase within the current 4-beat bar.
874 875 876 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 874 def mapping_source(:bar_phase) end |
#bass ⇒ Hash
Returns source descriptor for the low/bass frequency band.
746 747 748 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 746 def bass frequency_band(:low) end |
#bass_peak ⇒ Hash
Returns source descriptor for the held low/bass peak.
751 752 753 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 751 def bass_peak frequency_band_peak(:low) end |
#beat ⇒ Hash
Returns source descriptor for beat trigger.
824 825 826 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 824 def beat beat? end |
#beat? ⇒ Hash
Returns source descriptor for beat trigger.
819 820 821 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 819 def beat? mapping_source(:beat) end |
#beat_2 ⇒ Hash
Returns source descriptor for half-beat subdivision pulses.
849 850 851 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 849 def beat_2 mapping_source(:beat_2) end |
#beat_4 ⇒ Hash
Returns source descriptor for quarter-beat subdivision pulses.
854 855 856 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 854 def beat_4 mapping_source(:beat_4) end |
#beat_8 ⇒ Hash
Returns source descriptor for eighth-beat subdivision pulses.
859 860 861 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 859 def beat_8 mapping_source(:beat_8) end |
#beat_confidence ⇒ Hash
Returns source descriptor for beat detector confidence.
829 830 831 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 829 def beat_confidence mapping_source(:beat_confidence) end |
#beat_count ⇒ Hash
Returns source descriptor for beat counter.
839 840 841 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 839 def beat_count mapping_source(:beat_count) end |
#beat_phase ⇒ Hash
Returns source descriptor for 0.0..1.0 phase within the current beat.
844 845 846 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 844 def beat_phase mapping_source(:beat_phase) end |
#beat_pulse ⇒ Hash
Returns source descriptor for beat pulse decay value.
834 835 836 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 834 def beat_pulse mapping_source(:beat_pulse) end |
#beat_triplet ⇒ Hash
Returns source descriptor for triplet subdivision pulses.
864 865 866 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 864 def beat_triplet mapping_source(:beat_triplet) end |
#bezier(id = nil, from:, to:, control: nil, c1: nil, c2: nil, **options) { ... } ⇒ Hash
Declare a quadratic or cubic bezier curve. The serialized primitive is a path.
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 196 def bezier(id = nil, from:, to:, control: nil, c1: nil, c2: nil, **, &block) commands = [["M", *point_values(from)]] if control commands << ["Q", *point_values(control), *point_values(to)] elsif c1 && c2 commands << ["C", *point_values(c1), *point_values(c2), *point_values(to)] else raise ArgumentError, "bezier requires either :control or both :c1 and :c2" end build_shape(:path, (id, ).merge(commands: commands), schema_version: true, &block) end |
#blend(value) ⇒ Symbol
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 389 def blend(value) if @current_shape @current_shape[:blend] = value.to_sym mark_shape_schema_version! return @current_shape end if @current_custom_shape current_custom_shape_style[:blend] = value.to_sym return @current_custom_shape end if in_shape_group? current_shape_group[:blend] = value.to_sym return current_shape_group end @params[:blend] = value.to_sym end |
#bpm ⇒ Hash
Returns source descriptor for estimated BPM.
891 892 893 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 891 def bpm mapping_source(:bpm) end |
#bpm_confidence ⇒ Hash
Returns source descriptor for tempo estimator confidence.
896 897 898 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 896 def bpm_confidence mapping_source(:bpm_confidence) end |
#circle(id = nil, **options) { ... } ⇒ Hash
Declare a 2D circle/ring primitive for a shape layer.
130 131 132 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 130 def circle(id = nil, **, &block) build_shape(:circle, (id, ), &block) end |
#close ⇒ Object
578 579 580 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 578 def close append_path_command("Z") end |
#content(value) ⇒ String
295 296 297 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 295 def content(value) @params[:content] = value.to_s end |
#count(value) ⇒ Integer
289 290 291 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 289 def count(value) @params[:count] = Integer(value) end |
#cubic_to(c1x, c1y, c2x, c2y, x, y) ⇒ Object
562 563 564 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 562 def cubic_to(c1x, c1y, c2x, c2y, x, y) append_path_command("C", c1x, c1y, c2x, c2y, x, y) end |
#custom_shape(renderer, **options) { ... } ⇒ Array<Hash>
Expand a registered Ruby custom shape into normal shape primitives.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 225 def custom_shape(renderer, **, &block) mark_shape_schema_version! shape_id = .delete(:id) dynamic = .delete(:dynamic) static = .delete(:static) raise ArgumentError, "custom_shape cannot be both static and dynamic" if dynamic && static dynamic = true if static == false return append_dynamic_custom_shape(renderer, , shape_id: shape_id, &block) if dynamic primitives = (renderer, , shape_id: shape_id, cache: !!static) raise ArgumentError, "custom_shape produced no primitives" if primitives.empty? raise ArgumentError, "custom_shape id can only be assigned when one primitive is produced" if shape_id && primitives.length > 1 @type ||= :shape @params[:shapes] ||= [] primitives.map do |primitive| primitive[:id] ||= shape_id.to_sym if shape_id (primitive, &block) end end |
#draw { ... } ⇒ Array<Hash>
Group shape primitives in a block for readability.
272 273 274 275 276 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 272 def draw(&block) @type ||= :shape instance_eval(&block) if block @params[:shapes] || [] end |
#envelope(source = :kick, **options) ⇒ Hash
Alias for ADSR envelope mapping source.
968 969 970 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 968 def envelope(source = :kick, **) adsr(source, **) end |
#evaluate { ... } ⇒ Vizcore::DSL::LayerBuilder
Evaluate a layer block.
88 89 90 91 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 88 def evaluate(&block) instance_eval(&block) if block self end |
#fft_spectrum ⇒ Hash
Returns source descriptor for FFT spectrum array.
786 787 788 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 786 def fft_spectrum mapping_source(:fft_spectrum) end |
#file(path) ⇒ String
121 122 123 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 121 def file(path) @params[:file] = path.to_s end |
#fill(value) ⇒ String
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 328 def fill(value) if @current_shape @current_shape[:fill] = value.to_s mark_shape_schema_version! return @current_shape end if @current_custom_shape current_custom_shape_style[:fill] = value.to_s return @current_custom_shape end if in_shape_group? current_shape_group[:fill] = value.to_s return current_shape_group end @params[:color] = value.to_s end |
#font(value) ⇒ String
322 323 324 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 322 def font(value) @params[:font] = value.to_s end |
#font_size(value) ⇒ Integer
301 302 303 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 301 def font_size(value) @params[:font_size] = Integer(value) end |
#frequency_band(name) ⇒ Hash
Returns source descriptor for a frequency band.
716 717 718 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 716 def frequency_band(name) mapping_source(:frequency_band, band: name.to_sym) end |
#frequency_band_peak(name) ⇒ Hash
Returns source descriptor for a held frequency-band peak.
721 722 723 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 721 def frequency_band_peak(name) mapping_source(:frequency_band_peak, band: name.to_sym) end |
#global(name) ⇒ Hash
Returns source descriptor for mutable runtime globals.
927 928 929 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 927 def global(name) mapping_source(:global, name: name.to_sym) end |
#glsl(path) ⇒ String
114 115 116 117 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 114 def glsl(path) @glsl = path.to_s @type ||= :shader end |
#group(_id = nil, **attrs) { ... } ⇒ Array<Hash>
Apply shared style and transform to shape primitives declared in the block.
Group attributes are flattened into child primitives so the frontend only needs to render regular shape primitives.
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 256 def group(_id = nil, **attrs, &block) raise ArgumentError, "group requires a block" unless block mark_shape_schema_version! @type ||= :shape @shape_group_stack << merge_shape_group(current_shape_group, normalize_shape_group(attrs)) instance_eval(&block) @params[:shapes] || [] ensure @shape_group_stack.pop if @shape_group_stack.length > 1 end |
#high ⇒ Hash
Returns source descriptor for the high frequency band.
766 767 768 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 766 def high frequency_band(:high) end |
#high_peak ⇒ Hash
Returns source descriptor for the held high peak.
771 772 773 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 771 def high_peak frequency_band_peak(:high) end |
#hihat(value = NO_ARGUMENT) ⇒ Hash
Returns source descriptor for high-band percussive confidence.
812 813 814 815 816 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 812 def hihat(value = NO_ARGUMENT) return @params[:hihat] = value unless value.equal?(NO_ARGUMENT) mapping_source(:hihat) end |
#horizontal_to(x) ⇒ Object
566 567 568 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 566 def horizontal_to(x) append_path_command("H", x) end |
#kick(value = NO_ARGUMENT) ⇒ Hash
Returns source descriptor for low-band percussive confidence.
798 799 800 801 802 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 798 def kick(value = NO_ARGUMENT) return @params[:kick] = value unless value.equal?(NO_ARGUMENT) mapping_source(:kick) end |
#letter_spacing(value) ⇒ Float
307 308 309 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 307 def letter_spacing(value) @params[:letter_spacing] = normalize_non_negative_param_number(value, :letter_spacing) end |
#lfo(wave = :sine, rate: 1.0, phase: 0.0) ⇒ Hash
Returns source descriptor for a time-based low-frequency oscillator.
935 936 937 938 939 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 935 def lfo(wave = :sine, rate: 1.0, phase: 0.0) mapping_source(:lfo, wave: wave.to_sym, rate: Float(rate), phase: Float(phase)) rescue ArgumentError, TypeError raise ArgumentError, "lfo rate and phase must be numeric" end |
#line(id = nil, **options) { ... } ⇒ Hash
Declare a 2D line primitive for a shape layer.
139 140 141 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 139 def line(id = nil, **, &block) build_shape(:line, (id, ), &block) end |
#line_to(x, y) ⇒ Object
554 555 556 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 554 def line_to(x, y) append_path_command("L", x, y) end |
#low ⇒ Hash
Returns source descriptor for the low/bass frequency band.
736 737 738 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 736 def low frequency_band(:low) end |
#low_peak ⇒ Hash
Returns source descriptor for the held low/bass peak.
741 742 743 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 741 def low_peak frequency_band_peak(:low) end |
#map(definition = nil, **options, &block) ⇒ void
This method returns an undefined value.
Map analysis source(s) to layer parameter target(s).
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 644 def map(definition = nil, **, &block) definition, = normalize_custom_shape_mapping(definition, ) if @custom_shape_target_prefix definition, = normalize_shape_mapping(definition, ) if @shape_target_prefix if .key?(:to) = .dup to = .delete(:to) = evaluate_transform_block(, &block) if block @mappings << build_mapping( source: normalize_source(definition), target: to, transform: normalize_transform(**) ) return end mapping = definition.nil? ? : Hash(definition) raise ArgumentError, "map requires at least one mapping pair" if mapping.empty? raise ArgumentError, "map block syntax supports one mapping pair" if block && mapping.length != 1 mapping.each do |source, target| target_name, transform = normalize_target(target) transform = normalize_transform(**evaluate_transform_block(transform, &block)) if block @mappings << build_mapping(source: normalize_source(source), target: target_name, transform: transform) end end |
#mid ⇒ Hash
Returns source descriptor for the mid frequency band.
756 757 758 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 756 def mid frequency_band(:mid) end |
#mid_peak ⇒ Hash
Returns source descriptor for the held mid peak.
761 762 763 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 761 def mid_peak frequency_band_peak(:mid) end |
#move_to(x, y) ⇒ Object
550 551 552 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 550 def move_to(x, y) append_path_command("M", x, y) end |
#onset(band = nil) ⇒ Hash
Returns source descriptor for positive audio feature changes.
792 793 794 795 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 792 def onset(band = nil) = band.nil? ? {} : { band: band.to_sym } mapping_source(:onset, **) end |
#opacity(value) ⇒ Float, Hash
Set layer or shape opacity.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 413 def opacity(value) if @current_shape @current_shape[:opacity] = normalize_param_number(value, :opacity) mark_shape_schema_version! return @current_shape end if @current_custom_shape current_custom_shape_style[:opacity] = normalize_param_number(value, :opacity) return @current_custom_shape end if in_shape_group? current_shape_group[:opacity] = current_shape_group.key?(:opacity) ? normalize_param_number(current_shape_group[:opacity], :opacity) * normalize_param_number(value, :opacity) : normalize_param_number(value, :opacity) return current_shape_group end @params[:opacity] = normalize_param_number(value, :opacity) end |
#origin(*args, x: nil, y: nil) ⇒ Hash
Set a shape/layer transform origin.
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 515 def origin(*args, x: nil, y: nil) values = normalize_xy_args(args, x: x, y: y, name: :origin) if @current_shape current_shape_transform[:origin] = values return @current_shape end if @current_custom_shape current_custom_shape_transform[:origin] = values return @current_custom_shape end if in_shape_group? current_shape_group_transform[:origin] = values return current_shape_group end @params[:origin] = values end |
#palette(*colors) ⇒ Array<String>
Store an ordered color palette for this layer.
587 588 589 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 587 def palette(*colors) @params[:palette] = normalize_palette(colors) end |
#param(name, default: nil, range: nil, min: nil, max: nil, step: nil) ⇒ Hash
Declare numeric metadata for a shader/layer parameter.
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 622 def param(name, default: nil, range: nil, min: nil, max: nil, step: nil) key = normalize_param_name(name) range_min, range_max = normalize_range(range, context: "param") min = range_min if min.nil? max = range_max if max.nil? = { name: key } [:default] = normalize_param_number(default, :default) unless default.nil? [:min] = normalize_param_number(min, :min) unless min.nil? [:max] = normalize_param_number(max, :max) unless max.nil? [:step] = normalize_param_number(step, :step) unless step.nil? validate_param_range!() @params[key] = [:default] if .key?(:default) @param_schema[key] = end |
#path(id = nil, **options) { ... } ⇒ Hash
Declare a path primitive using SVG-like path commands.
179 180 181 182 183 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 179 def path(id = nil, **, &block) shape = (id, ) shape[:commands] ||= [] build_shape(:path, shape, schema_version: true, &block) end |
#peak ⇒ Hash
Returns source descriptor for absolute sample peak level.
710 711 712 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 710 def peak mapping_source(:peak) end |
#phrase_count ⇒ Hash
Returns source descriptor for completed 8-bar phrases.
886 887 888 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 886 def phrase_count mapping_source(:phrase_count) end |
#polygon(id = nil, **options) { ... } ⇒ Hash
Declare a closed polygon primitive for a shape layer.
159 160 161 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 159 def polygon(id = nil, **, &block) build_shape(:polygon, (id, ), schema_version: true, &block) end |
#polyline(id = nil, **options) { ... } ⇒ Hash
Declare an open polyline primitive for a shape layer.
169 170 171 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 169 def polyline(id = nil, **, &block) build_shape(:polyline, (id, ).merge(closed: false), schema_version: true, &block) end |
#post(name) ⇒ Symbol
Append one post effect to this layer’s post effect chain.
595 596 597 598 599 600 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 595 def post(name) raise ArgumentError, "post expects a symbol or string" unless name @params[:post_effects] ||= [] @params[:post_effects] << name.to_sym end |
#quad_to(cx, cy, x, y) ⇒ Object
558 559 560 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 558 def quad_to(cx, cy, x, y) append_path_command("Q", cx, cy, x, y) end |
#react_to(source_value) { ... } ⇒ void
This method returns an undefined value.
High-level mapping DSL for describing audio reactions inside a layer.
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 688 def react_to(source_value, &block) raise ArgumentError, "react_to requires a block" unless block source_descriptor = normalize_source(source_value) reaction = ReactionBuilder.new( mapping_factory: lambda do |target, | build_mapping( source: source_descriptor, target: target, transform: normalize_transform(**) ) end ) @mappings.concat(reaction.evaluate(&block)) end |
#rect(id = nil, **options) { ... } ⇒ Hash
Declare a 2D rectangle primitive for a shape layer.
149 150 151 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 149 def rect(id = nil, **, &block) build_shape(:rect, (id, ), schema_version: true, &block) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
1013 1014 1015 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 1013 def respond_to_missing?(method_name, include_private = false) !!@current_custom_shape || @params.key?(method_name.to_sym) || super end |
#rotate(value) ⇒ Float, Hash
Set a shape/layer rotation transform in degrees.
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 463 def rotate(value) rotation = normalize_param_number(value, :rotate) if @current_shape current_shape_transform[:rotate] = rotation return @current_shape end if @current_custom_shape current_custom_shape_transform[:rotate] = normalize_param_number(current_custom_shape_transform[:rotate] || 0, :rotate) + rotation return @current_custom_shape end if in_shape_group? current_shape_group_transform[:rotate] = normalize_param_number(current_shape_group_transform[:rotate] || 0, :rotate) + rotation return current_shape_group end @params[:rotate] = rotation end |
#scale(value = NO_ARGUMENT, x: nil, y: nil) ⇒ Float, Hash
Set a shape/layer scale transform.
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 489 def scale(value = NO_ARGUMENT, x: nil, y: nil) scale_value = normalize_scale_args(value, x: x, y: y) if @current_shape current_shape_transform[:scale] = scale_value return @current_shape end if @current_custom_shape current_custom_shape_transform[:scale] = multiply_shape_scale(current_custom_shape_transform[:scale], scale_value) return @current_custom_shape end if in_shape_group? current_shape_group_transform[:scale] = multiply_shape_scale(current_shape_group_transform[:scale], scale_value) return current_shape_group end @params[:scale] = scale_value end |
#shader(value, reload: nil) ⇒ Symbol
102 103 104 105 106 107 108 109 110 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 102 def shader(value, reload: nil) if shader_path?(value) @glsl = value.to_s @params[:shader_reload] = !!reload unless reload.nil? else @shader = value.to_sym end @type ||= :shader end |
#shadow(color: nil, blur: nil) ⇒ Hash
381 382 383 384 385 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 381 def shadow(color: nil, blur: nil) @params[:shadow_color] = color.to_s unless color.nil? @params[:shadow_blur] = normalize_non_negative_param_number(blur, :shadow_blur) unless blur.nil? @params end |
#shape(id) ⇒ ShapeReference
Return a reference object for mapping to a named shape.
539 540 541 542 543 544 545 546 547 548 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 539 def shape(id) key = id.to_sym index = @shape_index_by_id.fetch(key) do = shape_id_suggestions(key) = "unknown shape id: #{key.inspect}" = "#{}. Did you mean: #{}" unless .empty? raise ArgumentError, end ShapeReference.new("shapes.#{index}") end |
#snare(value = NO_ARGUMENT) ⇒ Hash
Returns source descriptor for mid-band percussive confidence.
805 806 807 808 809 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 805 def snare(value = NO_ARGUMENT) return @params[:snare] = value unless value.equal?(NO_ARGUMENT) mapping_source(:snare) end |
#source(value, **options) ⇒ Symbol, Hash
280 281 282 283 284 285 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 280 def source(value, **) source_name = value.to_sym return mapping_source(source_name, **) if .any? || MAPPING_SOURCE_KINDS.include?(source_name) @params[:source] = source_name end |
#spectral_centroid ⇒ Hash
Returns source descriptor for spectral centroid in Hz.
901 902 903 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 901 def spectral_centroid mapping_source(:spectral_centroid) end |
#spectral_flatness ⇒ Hash
Returns source descriptor for spectral flatness.
911 912 913 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 911 def spectral_flatness mapping_source(:spectral_flatness) end |
#spectral_flux ⇒ Hash
Returns source descriptor for positive spectrum delta.
916 917 918 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 916 def spectral_flux mapping_source(:spectral_flux) end |
#spectral_rolloff ⇒ Hash
Returns source descriptor for spectral rolloff in Hz.
906 907 908 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 906 def spectral_rolloff mapping_source(:spectral_rolloff) end |
#star(id = nil, **options) { ... } ⇒ Hash
Declare a star polygon primitive for a shape layer.
215 216 217 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 215 def star(id = nil, **, &block) build_shape(:star, (id, ), schema_version: true, &block) end |
#stroke(value = NO_ARGUMENT, width: nil, color: nil) ⇒ Hash
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 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 351 def stroke(value = NO_ARGUMENT, width: nil, color: nil) if @current_shape @current_shape[:stroke] = normalize_non_negative_param_number(value, :stroke) unless value.equal?(NO_ARGUMENT) @current_shape[:stroke_width] = normalize_non_negative_param_number(width, :stroke_width) unless width.nil? @current_shape[:stroke_color] = color.to_s unless color.nil? return @current_shape end if @current_custom_shape current_custom_shape_style[:stroke] = normalize_non_negative_param_number(value, :stroke) unless value.equal?(NO_ARGUMENT) current_custom_shape_style[:stroke_width] = normalize_non_negative_param_number(width, :stroke_width) unless width.nil? current_custom_shape_style[:stroke_color] = color.to_s unless color.nil? return @current_custom_shape end if in_shape_group? current_shape_group[:stroke] = normalize_non_negative_param_number(value, :stroke) unless value.equal?(NO_ARGUMENT) current_shape_group[:stroke_width] = normalize_non_negative_param_number(width, :stroke_width) unless width.nil? current_shape_group[:stroke_color] = color.to_s unless color.nil? return current_shape_group end @params[:stroke_width] = normalize_non_negative_param_number(width, :stroke_width) unless width.nil? @params[:stroke_color] = color.to_s unless color.nil? @params end |
#sub ⇒ Hash
Returns source descriptor for the sub-bass frequency band.
726 727 728 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 726 def sub frequency_band(:sub) end |
#sub_peak ⇒ Hash
Returns source descriptor for the held sub-bass peak.
731 732 733 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 731 def sub_peak frequency_band_peak(:sub) end |
#to_h ⇒ Hash
Returns serialized layer payload.
973 974 975 976 977 978 979 980 981 982 983 984 985 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 973 def to_h validate_strict_params! if @strict layer = { name: @name, type: resolved_type, params: @params.dup } layer[:shader] = @shader if @shader layer[:glsl] = @glsl if @glsl layer[:param_schema] = @param_schema.values.map(&:dup) unless @param_schema.empty? layer[:mappings] = @mappings.map { |mapping| mapping.dup } unless @mappings.empty? layer end |
#translate(*args, x: nil, y: nil) ⇒ Hash
Set a shape/layer translation transform.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 439 def translate(*args, x: nil, y: nil) values = normalize_xy_args(args, x: x, y: y, name: :translate) if @current_shape current_shape_transform[:translate] = values return @current_shape end if @current_custom_shape current_custom_shape_transform[:translate] = add_shape_xy(current_custom_shape_transform[:translate], values) return @current_custom_shape end if in_shape_group? current_shape_group_transform[:translate] = add_shape_xy(current_shape_group_transform[:translate], values) return current_shape_group end @params[:translate] = values end |
#treble ⇒ Hash
Returns source descriptor for the high/treble frequency band.
776 777 778 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 776 def treble frequency_band(:high) end |
#treble_peak ⇒ Hash
Returns source descriptor for the held high/treble peak.
781 782 783 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 781 def treble_peak frequency_band_peak(:high) end |
#triplet ⇒ Hash
Returns source descriptor for triplet subdivision pulses.
869 870 871 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 869 def triplet mapping_source(:beat_triplet) end |
#type(value) ⇒ Symbol
95 96 97 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 95 def type(value) @type = value.to_sym end |
#use_mapping(name) ⇒ void
This method returns an undefined value.
Apply a named mapping preset to this layer.
676 677 678 679 680 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 676 def use_mapping(name) preset_name = name.to_sym preset = @mapping_presets.fetch(preset_name) { raise ArgumentError, "unknown mapping preset: #{preset_name}" } preset.each { |mapping| @mappings << deep_dup(mapping) } end |
#use_style(name) ⇒ Hash
Apply a named style by merging its params into this layer.
607 608 609 610 611 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 607 def use_style(name) style_name = name.to_sym style_params = @styles.fetch(style_name) { raise ArgumentError, "unknown style: #{style_name}" } @params.merge!(deep_dup(style_params)) end |
#vertical_to(y) ⇒ Object
570 571 572 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 570 def vertical_to(y) append_path_command("V", y) end |
#zero_crossing_rate ⇒ Hash
Returns source descriptor for time-domain zero crossing rate.
921 922 923 |
# File 'lib/vizcore/dsl/layer_builder.rb', line 921 def zero_crossing_rate mapping_source(:zero_crossing_rate) end |