A ProcessorInstance that generates audio data.
A GeneratorInstance can be defined through implementing the IControl<T0> and
IRealtime interfaces, which defines the control and real-time thread behaviour respectively.
To use a generator in a scene, for example with an AudioSource, it must have an associated
IAudioGenerator that will instantiate it.
In the Unity Audio system they are used with an AudioSource by setting the AudioSource.generator
property to the associated IAudioGenerator.
It is also possible to create your own generators through code using a ControlContext.
Interacting with an instantiated generator depends on the ownership of the GeneratorInstance:
If the generator has been created from an AudioSource, you would interact with it
through the instance obtained from AudioSource.generatorInstance.
The following example implements a sine tone generator and exposes it as a component you can
assign to the <see cref="AudioSource._generator" /> property.
[BurstCompile(CompileSynchronously = true)]
struct Realtime : GeneratorInstance.IRealtime
{
private const float k_TwoPi = 2.0f * Mathf.PI;
// Normalized phase accumulator in [0, 1).
private float phase;
// Precomputed phase step per sample (cycles per sample).
internal float phaseIncrement;
// Capabilities must match those reported from IAudioGenerator and IRealtime.
public bool isFinite => false;
public bool isRealtime => false;
public DiscreteTime? length => null;
// Called when the real-time side of the graph updates (e.g., new control data available).
// Keep this method allocation-free and exception-free.
public void Update(UpdatedDataContext context, Pipe pipe) { }
public GeneratorInstance.Result Process(
in RealtimeContext context, Pipe pipe,
ChannelBuffer buffer,
GeneratorInstance.Arguments args)
{
// Compute a mono sine and copy it to all channels.
for (int frame = 0; frame < buffer.frameCount; frame++)
{
float s = Mathf.Sin(phase * k_TwoPi);
for (int ch = 0; ch < buffer.channelCount; ch++)
buffer[ch, frame] = s;
// Advance and wrap the phase into [0, 1).
phase += phaseIncrement;
if (phase >= 1.0f) phase -= 1.0f;
}
// Return the number of frames written.
return buffer.frameCount;
}
}
| Method | Description |
|---|---|
| Equals | Checks if this instance equals another. |
| GetHashCode | Retrieves a hash code based on this instance. |
| Operator | Description |
|---|---|
| operator != | Checks if two instances are not equal. |
| operator == | Checks if two instances are equal. |
| ProcessorInstance | Convert this GeneratorInstance to its more general ProcessorInstance representation. |