EffectInstance
A ProcessorInstance that processes audio data.
Read time 3 minutesLast updated 2 days ago
Definition
- Type: Struct
- Namespace: UnityEngine.Audio
- Assembly: UnityEngine.AudioModule
- Implements: IEquatable<EffectInstance>
public struct EffectInstance : IEquatable<EffectInstance>
Remarks
An EffectInstance reads audio from an input buffer, transforms it, and writes the result to an output buffer. You can define the EffectInstance through implementing the EffectInstance.IControl<TProcessor> and EffectInstance.IRealtime interfaces, which defines the control and real-time thread behavior respectively.
To use an effect in a scene, implement IAudioEffect on a MonoBehaviour and add the component to the same GameObject as an AudioSource. The AudioSource auto-discovers IAudioEffect components on its GameObject, and instantiates and manages their EffectInstances for you.
It is also possible to create your own effects through code using ControlContext.AllocateEffect, for example to run an effect nested inside another processor. Interacting with an instantiated effect depends on the ownership of the EffectInstance: If the effect has been created from an AudioSource, you would interact with it through the instance obtained from AudioSource.GetEffectInstance.
Additional Resources: IAudioEffect, ControlContext.AllocateEffect
The following example implements a gain effect and exposes it as a component you can add to the GameObject of an AudioSource.
Examples
[BurstCompile(CompileSynchronously = true)]struct Realtime : EffectInstance.IRealtime{ // Fixed attenuation applied to every sample. private const float k_Gain = 0.5f; // 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 EffectInstance.Result Process( in RealtimeContext context, ChannelBuffer inputBuffer, ChannelBuffer outputBuffer, EffectInstance.Arguments args) { // Scale every input sample and write it to the output buffer. // Always write every output sample: the output buffer starts undefined. for (int frame = 0; frame < inputBuffer.frameCount; frame++) { for (int ch = 0; ch < inputBuffer.channelCount; ch++) outputBuffer[ch, frame] = inputBuffer[ch, frame] * k_Gain; } return default; }}
Methods
Method | Description |
|---|---|
| Configure | Manually reconfigure this EffectInstance with the given AudioConfiguration. |
| Equals | Checks if this instance equals a given object. |
| GetHashCode | Retrieves a hash code based on this instance. |
| Process | Manually process this particular EffectInstance. |
Operators
Operator | Description |
|---|---|
| operator == | Checks if two instances are equal. |
| ProcessorInstance | Convert this EffectInstance to its more general ProcessorInstance representation. |
| operator != | Checks if two instances are not equal. |
Structs
Struct | Description |
|---|---|
| EffectInstance.Arguments | Additional arguments passed to the EffectInstance.IRealtime.Process method. |
| EffectInstance.CreationParameters | Additional parameters and initialization state for creating an EffectInstance. |
| EffectInstance.Result | The result returned from an EffectInstance.IRealtime.Process call. |
| EffectInstance.Setup | Per-instance setup an effect reports back from EffectInstance.IControl<TProcessor>.Configure. |
| EffectInstance.Sidechain | Placeholder for future sidechain wiring and layout information. |
Interfaces
Interface | Description |
|---|---|
| EffectInstance.ICapabilities | Static capabilities an effect declares up front. |
| EffectInstance.IControl<TProcessor> | The control interface an implementation of an EffectInstance must implement on a struct to be fully formed. |
| EffectInstance.IRealtime | The processing interface an implementation of an EffectInstance must implement on a struct to be fully formed. |