GeneratorInstance
A ProcessorInstance that generates audio data.
Read time 3 minutesLast updated 13 hours ago
Definition
- Type: Struct
- Namespace: UnityEngine.Audio
- Assembly: UnityEngine.AudioModule
- Implements: IEquatable<GeneratorInstance>
public struct GeneratorInstance : IEquatable<GeneratorInstance>
Remarks
A GeneratorInstance can be defined through implementing the GeneratorInstance.IControl<TRealtime> and GeneratorInstance.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 _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 _generatorInstance.
The following example implements a sine tone generator and exposes it as a component you can assign to the _generator property.
Examples
[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; }}
Methods
Method | Description |
|---|---|
| Equals | Checks if this instance equals a given object. |
| GetHashCode | Retrieves a hash code based on this instance. |
Operators
Operator | Description |
|---|---|
| operator == | Checks if two instances are equal. |
| ProcessorInstance | Convert this GeneratorInstance to its more general ProcessorInstance representation. |
| operator != | Checks if two instances are not equal. |
Structs
Struct | Description |
|---|---|
| GeneratorInstance.Arguments | Additional arguments passed to the RealtimeContext.Process method. |
| GeneratorInstance.Configuration | The configuration of a specific instance of a GeneratorInstance. |
| GeneratorInstance.CreationParameters | Parameters controlling how a GeneratorInstance is created. |
| GeneratorInstance.Properties | Represents optional or additional metadata about the GeneratorInstance. |
| GeneratorInstance.Result | The result returned from a GeneratorInstance.IRealtime.Process call. |
| GeneratorInstance.Setup | Information on the audio setup of the GeneratorInstance passed back to the instantiator from GeneratorInstance.IControl<TRealtime>.Configure. |
Interfaces
Interface | Description |
|---|---|
| GeneratorInstance.ICapabilities | Describes the runtime behaviour of the GeneratorInstance. These reported values are cached in the beginning and assumed to not change. |
| GeneratorInstance.IControl<TRealtime> | The control interface an implementation of a GeneratorInstance must implement on a struct to be fully formed. |
| GeneratorInstance.IRealtime | The processing interface an implementation of a GeneratorInstance must implement on a struct to be fully formed. |