Drawing and visualisations

IMPORTANT: This functionality is still in beta.

Spektar 2.1 introduces rudimentary ability to draw custom visualisations directly from components. These visualisations are made available in the VST Designer and can be exported and rendered into VSTGUI.

Here is an example scripted component with drawing:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component name="sample" description="sample component" package="dsp.script">
  <inputs>
    <input name="a" label="a" description="An input port"/>
    <input name="b"/>
  </inputs>
  <controls>
    <control displayMode="POT" label="Amp" max="1.0" min="0.0" name="amp" />
   </controls>
  <outputs>
    <output label="a+b" name="out"/>
  </outputs>
  <data>
	float $max;
  </data>
  <init>
  	$max = 0;
	$amp = 1;
  </init>
  <exec>
	$out = $amp * #math:sin(6.28 * ( $a + $b )); 
	$max = #base:max( $out, $max );
  </exec>
  <draw>
   // we have w, h and context variables injected for us
   // and can use all variables defined in data section above
   // (in our case, we can use the max variable)
   
   // let's get a nice violet
   int $cleft = #draw:makeColorPalSInt( $context, "Violet", 3);

   // and a pretty sky blue 
   int $cright = #draw:makeColorPalSInt( $context, "Sky", 2);

   // arc for rounded rect @15%
   int $arc = (int) ( 0.15 * #base:min( w, h) );

   // fill with gradient 	
	#draw:gradientFillRoundRect(context, 
			0, 0, $w, $h, $arc, // rectangle x, y and dimensions + arc
			0, 0, cleft, // gradient start point and colour
			w, h, cright); // gradient end point and colour

  </draw>
  <end>
  </end>
  
</component>

A fairly elaborate example is the Visualiser component - note the component documentation has the full source script of the component so can be used as an example.

Visualisation data types

Visualisation scripts can use same data types available in Scripting and same rules apply as for functions. Additional data types needed for drawing have also been added - color, image and gcontext.

See definition of all data types in Scripting

Special functions for drawing

Following special functions are available in scripts. All drawing functions below must be prefixed with #draw: (as if they belong to draw tribe), for example:

color blue = #draw:makeColor(0xff0000ff);

Note that since 2.1, there are additional special functions for drawing visualisations.

Function Parameters Description Example
color makeColor(int argb) argb - 32 bit ARGB (8888) encoded color Return a color struct initialised from supplied color value. color $blue = #draw:makeColor(0xff0000ff);
image makeImage(gcontext ctx, int w, int h) ctx - graphics context, w - image width, h - image height Create a new off-screen image. Can be used to directly draw to image framebuffer (image.data). Call #commitImage once done and before drawing this image to the screen (gcontext) image $off = #draw:makeImage( $context, 100, 33);
void makeFreeImage(image img) img - the image to release Release image resources. #draw:makeFreeImage( $off);

For brevity, below functions are not tabulated but simply listed.

void drawRect(gcontext ctx, float x, float y, float w, float h, int color);
void fillRect(gcontext ctx, float x, float y, float w, float h, int color);
void drawLine(gcontext ctx, float x1, float y1, float x2, float y2, float thickness, int color); 

void drawRoundRect(gcontext ctx, float x, float y, float w, float h, float arc, int thecolor);
void fillRoundRect(gcontext ctx, float x, float y, float w, float h, float arc, int thecolor);
void gradientFillRoundRect(gcontext ctx, float x, float y, float w, float h, float arc, 
			float gx1, float gy1, int color1, float gx2, float gy2, int color2);

// text drawing
void drawText(gcontext ctx, float x, float y, [stringliteral], float fontSize, int color);
int textWidth(gcontext ctx, [stringliteral], float fontSize, int color);

// images
void drawImage(gcontext ctx, float x, float y, image img);
// write image pixels from data variable to platform image
void commitImage(gcontext ctx, image image); 

// get ARGB, 32 bit 8888 representation of color c
int makeColorInt(color c);

// built in palettes 5 palettes of 8 colors each
// palette names are Violet, Sky, Rosy, Leafy, Fire, Gray
color makeColorPalS(gcontext ctx, [stringliteral], int thecolor);
color makeColorPal(gcontext ctx, int palette, int thecolor);
int makeColorPalSInt(gcontext ctx, [stringliteral], int thecolor);
int makeColorPalInt(gcontext ctx, int palette, int thecolor);