Dynamic Patching

CreateModule(55, 120, 40, 0, 0, 'AutoFader');
CreateConnection('AutoOSC', 'out', 'AutoFader', 'in');

Set of procedures to create dynamic patches: creations of modules, connections, etc.

CreateFileModule, DeleteModule, LoadPatch, ClearPatch, CreateModule when a module named AModuleName already exists, and SetModuleValue, SetModuleArrayValue and TParameter.CopyToModule when the terminal is one of the module's settings, only work when called on the main thread: call them from a callback in cbMainThread mode (CallbackMode(cbMainThread), the default) or from ProcessIDLE. On any other thread they do nothing and trace the script error is not executed in main thread and can't be processed (CreateFileModule, LoadPatch, ClearPatch, CreateModule and TParameter.CopyToModule return FALSE). ProcessThread always runs on its own thread, and Process, which also runs the callbacks in cbImmediate mode, usually runs on the audio thread. The settings concerned are the values of the module's settings panel that are not one of its controls, such as the nb polyphony of a sub-patch; the module's controls, such as the gain and mute of an Audio Mix Volume, although also shown in its settings panel, are written on any thread.

Procedure Description
function SetTargetPatch(AName: string):boolean; Set the target patch where subsequent module operations will take place. If the AName is empty then the current patch containing the script is used as target. Returns TRUE if a patch named AName is found, FALSE otherwise (also when AName is empty).
function CreateFileModule(Left, Top: integer; AFileName, OptionalContent, AModuleName: string):boolean; Create a new module from a file at the specified position and name, optionally providing inline content. If a module already exists with the specified AModuleName it is deleted before the creation of a new one. Only works on the main thread. Returns TRUE if the operation succeed FALSE otherwise.
function CreateModule(numID, Left, Top, QueryIdx1, QueryIdx2: integer; AModuleName: string):boolean; Create a new module by its numeric ID and position within the patch see module's id page. If a module already exists with the specified AModuleName it is deleted before the creation of a new one; this replacement only works on the main thread. Returns TRUE if the operation succeed FALSE otherwise.
procedure DeleteModule(AName: string); Delete a module of the current target patch by its name, on the main thread only
procedure CreateConnection(AModuleSrcName, AModuleSrcOutletName, AModuleTargName, AModuleTargInletName: string); Create a connection between two module terminals (source outlet → target inlet)
procedure DeleteConnection(AModuleSrcName, AModuleSrcOutletName, AModuleTargName, AModuleTargInletName: string); Delete an existing connection between two module terminals
procedure SetModuleValue(AModuleName, AModuleTletName: string; AValue: string); overload; Set a string value on a module terminal (inlet or outlet); a module setting that is not a control is only written on the main thread
procedure SetModuleValue(AModuleName, AModuleTletName: string; AValue: single); overload; Set a float value on a module terminal; a module setting that is not a control is only written on the main thread
procedure SetModuleValue(AModuleName, AModuleTletName: string; AValue: TAlphaColor); overload; Set a color value on a module terminal; a module setting that is not a control is only written on the main thread
procedure SetModuleArrayValue(AModuleName, AModuleTletName: string; AValues: array of single); overload; Set multiple float values on an array-type module terminal; a module setting that is not a control is only written on the main thread
procedure SetModuleArrayValue(AModuleName, AModuleTletName: string; AValues: array of TAlphaColor); overload; Set multiple color values on an array-type module terminal; a module setting that is not a control is only written on the main thread
function GetModuleValue(AModuleName, AModuleTletName: string): TAlphaColor; overload; Get the color value of a module terminal
function GetModuleValue(AModuleName, AModuleTletName: string): single; overload; Get the float value of a module terminal
function GetModuleValue(AModuleName, AModuleTletName: string): string; overload; Get the string value of a module terminal
function GetModulesUserNamesList: string; Return a comma-separated list of the user names, in upper case, of the modules of the current target patch and of its sub-patches
function TParameter.CopyFromModule(AModuleName, AModuleTletName: string): boolean; Copy the value of a module terminal (inlet or outlet) into the parameter. Returns TRUE if the operation succeed FALSE otherwise.
function TParameter.CopyToModule(AModuleName, AModuleTletName: string): boolean; Copy the value of the parameter to a module terminal (inlet or outlet); a module setting that is not a control is only written on the main thread. Returns TRUE if the operation succeed FALSE otherwise.
function LoadPatch(AFileName, ANewPatchName: string):boolean; Loads the current target patch from a file and eventually renames it with a new name. Only works on the main thread, and only when the target patch does not contain the script, so not with the default target (see SetTargetPatch). Returns TRUE if the operation succeed FALSE otherwise.
function ClearPatch(AFileName, ANewPatchName: string):boolean; Clears the current target patch and eventually renames it with a new name (AFileName only appears in error messages). Only works on the main thread, and only when the target patch does not contain the script, so not with the default target (see SetTargetPatch). Returns TRUE if the operation succeed FALSE otherwise.

Procedure Description
function LoadModuleDesign(AModuleName, AFileName: string): boolean; Load a module’s visual design from a specified file and apply it to the given module. Returns TRUE if the operation succeed FALSE otherwise.
function SetModuleControlPosition(AModuleName: string; X, Y: single): boolean; Set the on-screen position of a module’s control interface.Returns TRUE if the operation succeed FALSE otherwise.
function SetModuleControlSize(AModuleName: string; Width, Height: single): boolean; Set the width and height of a module’s control interface. Returns TRUE if the operation succeed FALSE otherwise.
////////////////////////////////////////////////////////////////////////////////
// Dynamic Patch Demo - shows how to create/delete modules on the fly, connect
// them, tweak parameters, and inspect the resulting patch state via scripts.
////////////////////////////////////////////////////////////////////////////////

const PATCH_DEFAULT      = 'MyDynamicPatch';
const FADER_ID           = 55;                  // module ID for a single fader (see Usine module id list)
const FADER_NAME         = 'DemoFader';
const OSC_MODULE_FILE    = 'oscillator.pat';    // file-based module used for the demo source
const OSC_MODULE_NAME    = 'DemoOSC';

var
  BuildBtn     : TParameter;
  RefreshBtn   : TParameter;
  CleanupBtn   : TParameter;
  TargetPatch  : TParameter;
  InfoOutput   : TParameter;

procedure RefreshStatus; forward;

procedure BuildDynamicPatch;
var
  curveValues : array[0..3] of single;
begin
  SetTargetPatch(TargetPatch.asString);         // choose which patch receives the new modules

  CreateModule(FADER_ID, 80, 40, 0, 0, FADER_NAME);
  CreateFileModule(20, 40, OSC_MODULE_FILE, '', OSC_MODULE_NAME);
  CreateConnection(OSC_MODULE_NAME, 'out', FADER_NAME, 'in');

  SetModuleValue(FADER_NAME, 'caption', 'Dynamic Fader'); // string value
  SetModuleValue(FADER_NAME, 'gain', 0.75);               // float value
  SetModuleValue(FADER_NAME, 'led-color', $FF00FF66);     // color value (ARGB)

  curveValues[0] := 0.0;
  curveValues[1] := 0.2;
  curveValues[2] := 0.8;
  curveValues[3] := 1.0;
  SetModuleArrayValue(FADER_NAME, 'curve', curveValues);  // float array example

  RefreshStatus;
  InfoOutput.asString('Dynamic patch built and configured.');
end;

procedure RefreshStatus;
var
  caption     : string;
  gainValue   : single;
  colorValue  : TAlphaColor;
  moduleNames : string;
  report      : string;
begin
  caption     := GetModuleValue(FADER_NAME, 'caption');
  gainValue   := GetModuleValue(FADER_NAME, 'out');
  colorValue  := GetModuleValue(FADER_NAME, 'led-color');
  moduleNames := GetModulesUserNamesList;

  report := 'Caption="' + caption +
            '" gain=' + FloatToStr(gainValue) +
            ' color RGB=(' + IntToStr(colorValue.R) + ',' + IntToStr(colorValue.G) + ',' + IntToStr(colorValue.B) + ')' +
            ' modules=[' + moduleNames + ']';

  InfoOutput.asString(report);
end;

procedure CleanupDynamicPatch;
begin
  DeleteConnection(OSC_MODULE_NAME, 'out', FADER_NAME, 'in');
  DeleteModule(OSC_MODULE_NAME);
  DeleteModule(FADER_NAME);
  InfoOutput.asString('Dynamic modules removed from patch "' + TargetPatch.asString + '".');
end;

procedure Init;
begin
  ModuleColor($FF2E7D32);

  BuildBtn    := CreateParam('build',   ptButton,    pioInput);
  RefreshBtn  := CreateParam('refresh', ptButton,    pioInput);
  CleanupBtn  := CreateParam('cleanup', ptButton,    pioInput);
  TargetPatch := CreateParam('target patch', ptTextField, pioInput);
  InfoOutput  := CreateParam('info', ptTextField, pioOutput);

  TargetPatch.asString(PATCH_DEFAULT);
  InfoOutput.asString('Set the target patch, then click "build" to spawn modules.');
end;

procedure Callback(N: integer);
begin
  if N = BuildBtn then
    BuildDynamicPatch;

  if N = RefreshBtn then
    RefreshStatus;

  if N = CleanupBtn then
    CleanupDynamicPatch;
end;

SetTargetPatch

function SetTargetPatch(AName: string):boolean;

Set the target patch where subsequent module operations will take place. If AName is empty, the current patch containing the script is used as target. Returns TRUE if a patch named AName is found, FALSE otherwise: when AName is empty (the target is then the patch containing the script) and when no patch is found (the target is then unchanged and a warning is traced).

if SetTargetPatch('MyDynamicPatch') then
  CreateModule(338, 20, 40, 0, 0, 'OSC1')   // created in MyDynamicPatch
else
  Trace('MyDynamicPatch not found');
SetTargetPatch('');                         // back to the patch containing the script

CreateFileModule

function CreateFileModule(Left, Top: integer; AFileName, OptionalContent, AModuleName: string):boolean;

Create a new module from a file at the specified position and name, optionally providing inline content. If a module already exists with the specified AModuleName, it is deleted before the creation of a new one. Returns TRUE if the operation succeed FALSE otherwise. Only works when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), it deletes and creates nothing, returns FALSE and traces a script error.

// loads the patch file my-filter.usine-patch as a sub-patch named FILTER1
if not CreateFileModule(20, 200, 'my-filter.usine-patch', '', 'FILTER1') then
  Trace('FILTER1 not created');

CreateModule

function CreateModule(numID, Left, Top, QueryIdx1, QueryIdx2: integer; AModuleName: string): boolean;

Create a new module by its numeric ID and position within the patch. If a module already exists with the specified AModuleName, it is deleted before the creation of a new one. Returns TRUE if the operation succeed FALSE otherwise. Replacing a module only works when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), when a module named AModuleName already exists, it deletes and creates nothing, returns FALSE and traces a script error.

CreateModule(338, 20, 40, 0, 0, 'OSC1');    // Audio Oscillator Simple (no query)
CreateModule(261, 200, 40, 2, 0, 'VOL1');   // Audio Mix Volume, query: 2 audio channels

DeleteModule

procedure DeleteModule(AName: string);

Delete a module of the current target patch (see SetTargetPatch) by its name; the module is also searched in the sub-patches of the target patch. Only works when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), it deletes nothing and traces a script error.

DeleteModule('OSC1');

CreateConnection

procedure CreateConnection(AModuleSrcName, AModuleSrcOutletName, AModuleTargName, AModuleTargInletName: string);

Create a connection between two module terminals (source outlet → target inlet).

CreateConnection('OSC1', 'out', 'VOL1', 'in 1');   // outlet 'out' of OSC1 to inlet 'in 1' of VOL1
CreateConnection('OSC1', 'out', 'VOL1', 'in 2');

DeleteConnection

procedure DeleteConnection(AModuleSrcName, AModuleSrcOutletName, AModuleTargName, AModuleTargInletName: string);

Delete an existing connection between two module terminals.

DeleteConnection('OSC1', 'out', 'VOL1', 'in 2');

SetModuleValue (string)

procedure SetModuleValue(AModuleName, AModuleTletName: string; AValue: string); overload;

Set a string value on a module terminal (inlet or outlet). When AModuleTletName is one of the module's settings (a value of its settings panel that is not one of its controls, such as the nb polyphony of a sub-patch), it is only written when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), nothing is written and a script error is traced.

SetModuleValue('FADER1', 'caption', 'cutoff');   // FADER1: a Fader Horizontal module (ID 3)

SetModuleValue (single)

procedure SetModuleValue(AModuleName, AModuleTletName: string; AValue: single); overload;

Set a float value on a module terminal. When AModuleTletName is one of the module's settings (a value of its settings panel that is not one of its controls, such as the nb polyphony of a sub-patch), it is only written when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), nothing is written and a script error is traced.

SetModuleValue('VOL1', 'gain', -6.0);          // gain in dB
SetModuleValue('OSC1', 'frequency', 220.0);

SetModuleValue (color)

procedure SetModuleValue(AModuleName, AModuleTletName: string; AValue: TAlphaColor); overload;

Set a color value on a module terminal. When AModuleTletName is one of the module's settings (a value of its settings panel that is not one of its controls, such as the nb polyphony of a sub-patch), it is only written when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), nothing is written and a script error is traced.

procedure ColorFader;
var col : TAlphaColor;
begin
  col := $FFFF8000;                          // opaque orange ($AARRGGBB)
  SetModuleValue('FADER1', 'color', col);
end;

SetModuleArrayValue (float array)

procedure SetModuleArrayValue(AModuleName, AModuleTletName: string; AValues: array of single); overload;

Set multiple float values on an array-type module terminal. When AModuleTletName is one of the module's settings (a value of its settings panel that is not one of its controls, such as the nb polyphony of a sub-patch), it is only written when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), nothing is written and a script error is traced.

procedure SendRamp;
var ramp : array[0..3] of single;
begin
  ramp[0] := 0.0;
  ramp[1] := 0.25;
  ramp[2] := 0.5;
  ramp[3] := 1.0;
  SetModuleArrayValue('ARRAY1', 'array in', ramp);   // ARRAY1: an Array Display module (ID 260)
end;

SetModuleArrayValue (color array)

procedure SetModuleArrayValue(AModuleName, AModuleTletName: string; AValues: array of TAlphaColor); overload;

Set multiple color values on an array-type module terminal. When AModuleTletName is one of the module's settings (a value of its settings panel that is not one of its controls, such as the nb polyphony of a sub-patch), it is only written when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), nothing is written and a script error is traced.

procedure SendPalette;
var palette : array[0..2] of TAlphaColor;
begin
  palette[0] := $FFFF0000;   // red
  palette[1] := $FF00FF00;   // green
  palette[2] := $FF0000FF;   // blue
  SetModuleArrayValue('COLORS1', 'color array', palette);   // COLORS1: a Color Array Display module (ID 585)
end;

GetModuleValue (color)

function GetModuleValue(AModuleName, AModuleTletName: string): TAlphaColor; overload;

Get the color value of a module terminal.

procedure ShowFaderColor;
var col : TAlphaColor;
begin
  col := GetModuleValue('FADER1', 'color');
  Trace('red component: ' + IntToStr(col.R));
end;

GetModuleValue (float)

function GetModuleValue(AModuleName, AModuleTletName: string): single; overload;

Get the float value of a module terminal.

procedure ShowGain;
var gain : single;
begin
  gain := GetModuleValue('VOL1', 'gain');
  Trace('VOL1 gain (dB): ', gain);
end;

GetModuleValue (string)

function GetModuleValue(AModuleName, AModuleTletName: string): string; overload;

Get the string value of a module terminal.

procedure ShowFaderCaption;
var txt : string;
begin
  txt := GetModuleValue('FADER1', 'caption');
  Trace('FADER1 caption: ' + txt);
end;

GetModulesUserNamesList

function GetModulesUserNamesList: string;

Return a comma-separated list of the user names, in upper case, of the modules of the current target patch (see SetTargetPatch) and of its sub-patches.

procedure ListModules;
var names : TStringList;
    i     : integer;
begin
  names.Create;
  names.SetCommaText(GetModulesUserNamesList);
  for i := 0 to names.Count - 1 do
    Trace(names.GetStrings(i));                 // OSC1, VOL1, ...
  names.Free;
end;

TParameter.CopyFromModule

function TParameter.CopyFromModule(AModuleName, AModuleTletName: string): boolean;

Copy the value of a module terminal (inlet or outlet) into the parameter. Returns TRUE if the operation succeed FALSE otherwise.

// freq: a ptDataField parameter created in Init
if not freq.CopyFromModule('OSC1', 'frequency') then
  Trace('OSC1 frequency not found');

TParameter.CopyToModule

function TParameter.CopyToModule(AModuleName, AModuleTletName: string): boolean;

Copy the value of the parameter to a module terminal (inlet or outlet). Returns TRUE if the operation succeed FALSE otherwise. When AModuleTletName is one of the module's settings (a value of its settings panel that is not one of its controls, such as the nb polyphony of a sub-patch), it is only written when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), nothing is written, it returns FALSE and a script error is traced.

// freq: a ptDataFader parameter created in Init
freq.CopyToModule('OSC1', 'frequency');

LoadPatch

function LoadPatch(AFileName, ANewPatchName: string):boolean;

Loads the current target patch from a file and eventually renames it with a new name. Returns TRUE if the operation succeed FALSE otherwise. Only works when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), it loads nothing, returns FALSE and traces a script error. When the target patch, or one of its sub-patches, contains the script, which is always the case of the default target (the patch containing the script), it loads nothing, returns FALSE and traces a warning: call SetTargetPatch first with the name of a patch that does not contain the script.

// from a callback in cbMainThread mode or from ProcessIDLE
if SetTargetPatch('MyDynamicPatch') then
  LoadPatch('my-preset.usine-patch', 'MyDynamicPatch');   // keeps the name for the next SetTargetPatch

ClearPatch

function ClearPatch(AFileName, ANewPatchName: string):boolean;

Clears the current target patch and eventually renames it with a new name; AFileName only appears in error messages. Returns TRUE if the operation succeed FALSE otherwise. Only works when called on the main thread: on another thread (always in ProcessThread, usually in Process and in callbacks in cbImmediate mode), it clears nothing, returns FALSE and traces a script error. When the target patch, or one of its sub-patches, contains the script, which is always the case of the default target (the patch containing the script), it clears nothing, returns FALSE and traces a warning: call SetTargetPatch first with the name of a patch that does not contain the script.

// from a callback in cbMainThread mode or from ProcessIDLE
if SetTargetPatch('MyDynamicPatch') then
  ClearPatch('', 'MyDynamicPatch');

function LoadModuleDesign

function LoadModuleDesign(AModuleName, AFileName: string): boolean;

Load a module’s visual design from a specified file and apply it to the given module. Returns TRUE if the operation succeed FALSE otherwise.

function SetModuleControlPosition

function SetModuleControlPosition(AModuleName: string; X, Y: single): boolean;

Set the on-screen position of a module’s control interface.Returns TRUE if the operation succeed FALSE otherwise.

function SetModuleControlSize

function SetModuleControlSize(AModuleName: string; Width, Height: single): boolean;

Set the width and height of a module’s control interface. Returns TRUE if the operation succeed FALSE otherwise.

more about scripts

version 7.0.250921

Edit All Pages