/VSynth

A ChucK inspired language for drawing pictures

Primary LanguageJavaScript

VSynth

Inspired by the ChucK programming language, this tool allows you to use a similar (but much, much more limited) version of that language to create visual output.

This is a quick/dirty hack language, so don't expect great feedback or ergonomics. It's a toy that I enjoy playing with, and I hope you do too.

Using the tool

You can access a demo from here.

The UI has a canvas area where the output draws, a text area where you can enter code, a run button which will execute your code, and a small console area which will output any errors in your code.

Syntax

The VSynth language consists of two kinds of statements: generator declarations, and wireups.

Generator Declarations

To spawn a generator, you use the following statement:

GeneratorType label;

Or, concretely:

Trig t;

This creates a generator of the specified type, which can be referred to by label. While many times, you'll be wiring together generators via ports, you may wish to control ports directly when creating the generator.

GeneratorType label(portName=defaultValue, otherPortName=defaultValue);

Or, concretely:

Trig t(frequency=0.125);

NB: this language doesn't have datatypes, so whatever you pass into the initalizer will be passed exactly as specified, and it's up to the generator to parse the value. This is important on the Beats generator.

Generator Wireups

Once generators have been declared, they can be wired together via their labels. A wireup statement looks like:

genLabel.portName->otherGenLabel.otherPortName;

Or, for a "hello world" program, which draws a circle:

Trig t(frequency=0.2);
Line l;
t.sin->l.x;
t.cos->l.y;

This connects the sin output of the Trigonometry generator to the x port on the Line generator, and cos to the y.

Generators

There are a number of generators you can use in a drawing. Each generator may have Input ports, and Output ports. When wiring, you should wire Outputs to Inputs (though there's nothing in the language which enforces this, but you probably will get meaningless results).

Any port can be given an initial value using the generator declaration syntax above.

NB: all screen coordinates (any x/y/w/h ports) are mapped so that the screen area ranges from -1..1.

Value

The Value generator outputs just a steady value, useful as a constant. This generator has only one port, but that port is both an input and an output.

Inputs Outputs
  • value: the value to be output (default: null)
  • value: the value to be output

Tick

The Tick generator outputs an incrementing series of values within a range. When it hits the end of the range, it loops back to the beginning.

Inputs Outputs
  • min: floating point value which is the minimum end of the range. (default: -1)
  • max: floating point value which is the maximum edge of the range (default: 1)
  • incr: the step per frame (default: 0.01)
  • tick: the current value of the tick sequence

Trig

The Trigonometry generator outputs the result of trig functions with a specified frequency.

Inputs Outputs
  • frequency: the frequency, in HZ, by which this signal should vary. (default: 10)
  • sin: a sine wave with that frequency
  • cos: a cosine wave with that frequency
  • tan: a tangent wave with that frequency

Math

The Math generator can perform basic arithmetic on its inputs.

Inputs Outputs
  • a: operand a (default: null)
  • b: operand b (default: null)
  • aPlusB: a + b
  • aTimesB: a * b
  • aMinusB: a - b
  • aOverB: a / b
  • aModB: a % b

Gate

The Gate gen allows a value to pass through when a key is pressed. NB, because there are no types, you shouldn't wrap the key expression in quotes.

Gate g(key=page down) is correct, Gate g(key="page down") is wrong.

Inputs Outputs
  • key: The name for a keycode (see keycodes for the lookup table). When this key is held down, the output will be the value of in. (default: q)
  • in: The input signal (default: 1.0)
  • latches: If false, when the key is released, the out value will be zero. If true, when the key is released, the value will hold the last value received from the input while the key was held. (default: false)
  • out: The value of in while the key is held down.

Beat

The Beat generator is a version of a gate which outputs its in value, not based on keystrokes, but based on a beat pattern.

Inputs Outputs
  • pattern: a string. Each character in the string represents one beat. If that character is an x, ^, or *, we output our input value for that beat. Any other character we output 0. This pattern loops indefinitely.
  • in: our input value. (default: 1.0)
  • bpm: the beats-per-minute
  • out: If the current beat in our pattern is x, ^, or *, we output in. Otherwise, we output 0.

Pixel

The Pixel generator colors in pixels each frame, based on its inputs.

NB: the r/g/b values accept valuse in the range from -1..1, but take the absolute value- so -1,-1,-1 would be white, as would 1,1,1

Inputs Outputs
  • x: the x coordinate default: 0)
  • y: the y coordinate (default: 0)
  • r: the red value default: 1)
  • g: the green value default: 1)
  • b: the blue value default: 1)

No ports - draws pixels on the screen

Line

The Line generator works as the pixel generator, but "connects" the dots, by drawing a line from each pixel to the next.

Inputs Outputs
  • x: the x coordinate (default: 0)
  • y: the y coordinate (default: 0)
  • r: the red value (default: 1)
  • g: the green value (default: 1)
  • b: the blue value (default: 1)

No ports - draws lines on the screen

Rotate

The Rotate generator rotates the coordinate system about the origin (the center of the screen).

Inputs Outputs
  • r: the angle, in radians (default: 0)

No ports - rotates the screen

Clear

The Clear generator clears some or all of the screen, by drawing a black rectangle over that area of the screen.

Inputs Outputs
  • x: the x coordinate of the box (default: -3)
  • y: the y coordinate of the box (default: -3)
  • w: the width of the box (default: 4)
  • h: the height of the box (default: 4)
  • trig: When this value is greater than zero, this clears the screen.

No ports - draws pixels on the screen

Log

The Log generator will accept inputs and dump them to the browser console. It can be useful for debugging.

The input ports are all named value0-value9. This has no output ports.