rsc3/doc-schelp/HelpSource/Classes/SynthDesc.scrbl

263 lines
9.2 KiB
Racket

#lang scribble/manual
@(require (for-label racket))
@title{SynthDesc}
Description of a synth definition@section{categories}
Server>Nodes
@section{related}
Classes/SynthDef, Classes/Synth
@section{description}
Contains a description of a link::Classes/SynthDef::, including its name, control names and default values.
Information is also stored on its outputs and inputs and whether it has a gate control.
SynthDescs are needed by the event stream system, so when using link::Classes/Pbind::, the instruments' default parameters are derived from the SynthDesc.
@section{subsection}
Creation
SynthDescs are created by link::Classes/SynthDescLib::, by reading a compiled synth def file.
@racketblock[
SynthDescLib.global.read("synthdefs/default.scsyndef");
SynthDescLib.global.synthDescs.at(\default)
SynthDescLib.global.at(\default) // shortcut, same as line above
::
]
@racketblock[aSynthDef.store:: and ]
@racketblock[aSynthDef.add:: also create a synthDesc in the global library.
]
@section{definitionlist}
## .store || saves a synthdef file on disk (like .load);
## .add || creates the synthDesc wholly in memory and sends the synthdef to registered servers.
::
@racketblock[
(
SynthDef("test", { arg out, freq, xFade;
XOut.ar(out, xFade, SinOsc.ar(freq))
}).store
);
::
Browse the properties of SynthDescs:
]
@racketblock[
SynthDescLib.global.browse;
::
]
@section{section}
SynthDescs and SynthDef metadata
Metadata associated with a link::Classes/SynthDef:: consists of an link::Classes/Event:: (a syntactically shorter form of an identity dictionary) containing information about the SynthDef that is useful to the client, and which cannot be inferred from the binary .scsyndef stream.
For example, by listing argument names and link::Classes/ControlSpec::s under the 'specs' key in the event, the client can use the specs to build a link::Classes/GUI:: allowing control over all the SynthDef's inputs, with sensible range mappings. (The "window" button in the SynthDescLib browser does exactly this -- any ControlSpecs listed in the metadata will be used for the corresponding synth input's slider.)
Currently only the 'specs' key is reserved. Other keys may be used as needed for specific applications. As the use of SynthDef metadata evolves, other keys may be standardized.
@section{subsection}
Creation and access
Metadata are specified when creating a link::Classes/SynthDef::. If the SynthDef is .store'd (or .add'd) into a SynthDescLib, the metadata become part of the SynthDesc as well. Thereafter, the metadata can be accessed through SynthDescLib:
@racketblock[
SynthDescLib.global[\synthDefName].metadata
::
]
@section{subsection}
Persistence and metadata plug-ins
Storing a SynthDef into the library with .store persists the SynthDef on disk. Metadata may also be persisted at the same time by using the appropriate metadata plug-in class. The plug-in is responsible for writing a separate metadata file into the synthdefs directory, and reading the file back at the same time that a SynthDesc is created for a .scsyndef file using SynthDesc.read or SynthDescLib.global.read.
The currently available plug-ins are:
@section{definitionlist}
## AbstractMDPlugin || A dummy plug-in, which writes no metadata. , so that users who are not interested in metadata will not find extra files in the synthdefs directory.
## TextArchiveMDPlugin || Writes the metadata as a SuperCollider text archive -- metadata.writeArchive(path). This is the default. Metadata are written only if the SynthDef contains metadata.
::
Other plug-ins may be written at a later date, to support sharing metadata with applications in other languages using formats like JSON (JavaScript Object Notation) or XML.
You may specify a global default metadata plug-in as follows:
@racketblock[
SynthDesc.mdPlugin = ... plug-in class name ...;
::
Metadata are not written when using ]
@racketblock[SynthDef().load(server)::. This is because SynthDesc exists to describe a SynthDef to the client, whereas SynthDef is really just an abstraction to create a link::Classes/UGen:: graph for the server. Metadata are also not written for ]
@racketblock[SynthDef().add::, because in the normal case, nothing is persisted to disk. (If the SynthDef is very large and a disk file is required, then the metadata will persist along with the .scsyndef file.)
]
@section{subsection}
Automatic population
You may write a function to populate entries into the metadata automatically, based on the SynthDesc object. This function executes when reading a SynthDesc from disk, when using .add, and before writing a metadata file (in .store).
@racketblock[
SynthDesc.populateMetadataFunc = { |synthdesc|
... do work here ...
};
::
]
@section{subsection}
Synchronization
Whenever a .scsyndef file is written, any existing metadata files will be deleted so that a new .scsyndef file will not exist on disk with out-of-date metadata.
@section{subsection}
Reading
When reading a SynthDesc, metadata files will be checked and one will be read, regardless of format. (Even if the default SynthDesc.mdPlugin is different from the file format on disk, the disk file will be read using the appropriate plug-in anyway.)
There should be only one metadata file at a time. However, in the case of conflicts, the default SynthDesc.mdPlugin will be preferred. The file extension identifies the format.
@section{subsection}
Metadata Examples
@racketblock[
s.boot;
d = SynthDef(\mdDemo, { |out, freq, cutoff, volume, gate = 1|
var sig = LPF.ar(Saw.ar(freq, volume), cutoff),
env = EnvGen.kr(Env.adsr, gate, doneAction: Done.freeSelf);
Out.ar(out, (sig * env) ! 2)
}).add;
SynthDescLib.global[\mdDemo].makeWindow;
// Note in the resulting window that Freq has a slider, but Cutoff and Volume do not.
// This is because there are no global specs for the argument names 'cutoff' and 'volume'.
// Same SynthDef, but adding metadata
// \freq and \amp exist in the global ControlSpec collection -- Spec.specs
// They are converted to real ControlSpecs using .asSpec
d = SynthDef(\mdDemo, { |out, freq, cutoff, volume, gate = 1|
var sig = LPF.ar(Saw.ar(freq, volume), cutoff),
env = EnvGen.kr(Env.adsr, gate, doneAction: Done.freeSelf);
Out.ar(out, (sig * env) ! 2)
}, metadata: (specs: (cutoff: \freq, volume: \amp))).add;
SynthDescLib.global[\mdDemo].makeWindow;
// Now cutoff has a slider for frequency and volume has amplitude scaling
// Store the SynthDef along with metadata
d.store(mdPlugin: TextArchiveMDPlugin);
"ls %mdDemo.*".format(SynthDef.synthDefDir.escapeChar($ )).unixCmd;
// In addition to .scsyndef, there's also .txarcmeta - "text archive metadata"
// Load a fresh SynthDesc from disk for it
// The SynthDesc.read interface is a bit weird - e will be a dictionary holding the SynthDesc
e = SynthDesc.read(SynthDef.synthDefDir ++ "mdDemo.scsyndef");
// Metadata have been successfully read from disk!
// You could even do the above after recompiling and the MD would be there
e[\mdDemo].metadata
e[\mdDemo].makeWindow;
::
]
@section{classmethods}
@section{private}
initClass
@section{method}
read
Adds all synthDescs in a path to a dict. You should not use this method or *readFile to read SynthDescs into a SynthDescLib. Use link::Classes/SynthDescLib#read:: or link::Classes/SynthDescLib#readStream:: instead.
@section{instancemethods}
@section{method}
name
@section{returns}
the name of the SynthDef
@section{method}
controls
@section{returns}
an array of instances of link::Classes/ControlName::, each of which
have the following fields: name, index, rate, defaultValue
@section{discussion}
@racketblock[
SynthDescLib.global.at(\default).controlNames.postln;
::
]
@section{method}
controlDict
An link::Classes/IdentityDictionary:: of the link::Classes/ControlName::'s, indexed by name.
This can be used for fast lookup of control index by name, for example to set a specific element of a multichannel control.
@section{method}
controlNames
@section{returns}
an array of Strings with the names of controls
@section{method}
outputs
@section{returns}
an array of link::Classes/IODesc:: that describes the available outputs.
@section{method}
inputs
@section{returns}
an array of link::Classes/IODesc:: that describes the available inputs.
@section{method}
hasGate
is true if the Synthdef has a gate input
@section{method}
canFreeSynth
is true if the link::Classes/Synth:: can free itself (via some means, usually a doneAction)
@section{discussion}
This can be used to decide if to remove a Synth directly via free-message.
@racketblock[
SynthDescLib.global.at(\default).canFreeSynth;
::
]
@section{method}
outputData
Returns an array of events with information about any UGens that write to a bus (such as link::Classes/Out:: etc.). This includes the rate and number of channels of the UGen. If its first input is a control, also the corresponding control name is provided.
@racketblock[
a = SynthDef(\x, { |out, freq = 440| Out.ar(out, SinOsc.ar(freq)) }).add;
a.desc.outputData;
a = SynthDef(\x, { |out, freq = 440| Out.ar(out + 7, SinOsc.ar(freq)) }).add; // no controlName in this case
a.desc.outputData;
::
]
@section{method}
msgFunc
the function which is used to create an array of arguments for
playing a synth def in patterns
@section{discussion}
@racketblock[
SynthDescLib.global.synthDescs.at(\default).msgFunc.postcs;
::
]