create
Creates a new instrument definition, instrument instance, or opcode object.
Syntax
Initialization
code -- String containing Csound language code for an instrument.
instr -- compiled instrument definition.
opc -- opcode definition, supplied directly or looked up by name with opcoderef
overload -- (optional, defaults to 0) opcode overload (version) (see opcodeinfo)
len -- opcode object array length.
All forms run at initialization. The string form takes an instrument body without instr or endin, compiles it and returns an InstrDef. createinstr is another name for this form.
create(definition) allocates an Instr instance without scheduling it. Use init to initialize it, then perf to run it from the calling instrument. For an instance that Csound performs automatically, use play or schedule.
The Opcode form creates one object from the chosen overload. The array form creates a one-dimensional Opcode[] with len independent objects. Use a nonnegative integer for len. Creating an opcode object does not run its initialization or performance code. Use run, or separate init and perf calls, to execute it.
Arrange delete in the owning instrument for cleanup. The object guide explains lifetimes and gives examples of each form.
Instrument definition numbers
Each call to create(code) compiles a new definition in a free instrument-number slot. Assigning the result to the same InstrDef variable changes which definition the variable refers to; it does not replace or remove the previous definition. The variable's name does not name the compiled instrument. Use nstrnum to get the number of the definition it currently holds.
Removing an unused definition can free its number, but a later create call does not guarantee reuse of that number. Keep references to definitions that still need cleanup. delete runs when the containing instrument ends; ensure all notes using the definition have finished first.
If you need to replace an instrument while keeping its number, use compilestr with a complete instr/endin definition using the existing number or instrument name. Do not rely on repeated create calls preserving instrument performance order, for example when one instrument writes an audio bus that another reads.
Examples
This example assigns two definitions to the same variable. Their numbers differ, and a saved reference still refers to the first definition. Both definitions are removed when instrument 1 ends. It uses create-instrdef-numbers.csd and produces no audio.
Here is an example of the create opcode. It uses the file create.csd.
See also
Instrument definitions, instances and opcode objects, delete, init, perf
Credits
Author: Victor Lazzarini
Maynooth University
Ireland
Csound 7, 2024