# GeneratorInstance

> A ProcessorInstance that generates audio data.

## Definition

* **Type:** Struct
* **Namespace:** [UnityEngine.Audio](/engine/6000.7/script-reference/unityengine/audio.md)
* **Assembly:** UnityEngine.AudioModule
* **Implements:** [IEquatable\<GeneratorInstance>](https://learn.microsoft.com/dotnet/api/system.iequatable-1)

```csharp
public struct GeneratorInstance : IEquatable<GeneratorInstance>
```

## Remarks

A [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md) can be defined through implementing the [GeneratorInstance.IControl\<TRealtime>](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/icontrol1.md) and [GeneratorInstance.IRealtime](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/irealtime.md) interfaces, which defines the control and real-time thread behaviour respectively.

To use a generator in a scene, for example with an [AudioSource](/engine/6000.7/script-reference/unityengine/audiosource.md), it must have an associated [IAudioGenerator](/engine/6000.7/script-reference/unityengine/audio/iaudiogenerator.md) that will instantiate it. In the Unity Audio system they are used with an [AudioSource](/engine/6000.7/script-reference/unityengine/audiosource.md) by setting the [\_generator](/engine/6000.7/script-reference/unityengine/audiosource/generator.md) property to the associated [IAudioGenerator](/engine/6000.7/script-reference/unityengine/audio/iaudiogenerator.md).

It is also possible to create your own generators through code using a [ControlContext](/engine/6000.7/script-reference/unityengine/audio/controlcontext.md). Interacting with an instantiated generator depends on the ownership of the [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md): If the generator has been created from an [AudioSource](/engine/6000.7/script-reference/unityengine/audiosource.md), you would interact with it through the instance obtained from [\_generatorInstance](/engine/6000.7/script-reference/unityengine/audiosource/generatorinstance.md).

The following example implements a sine tone generator and exposes it as a component you can assign to the [\_generator](/engine/6000.7/script-reference/unityengine/audiosource/generator.md) property.

## Examples

```csharp
[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](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/equals.md)           | Checks if this instance equals a given object. |
| [GetHashCode](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/gethashcode.md) | Retrieves a hash code based on this instance.  |

## Operators

| Operator                                                                                                | Description                                                                                                                                                                                                                              |
| ------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [operator ==](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/op-equality.md)       | Checks if two instances are equal.                                                                                                                                                                                                       |
| [ProcessorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/op-implicit.md) | Convert this [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md) to its more general [ProcessorInstance](/engine/6000.7/script-reference/unityengine/audio/processorinstance.md) representation. |
| [operator !=](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/op-inequality.md)     | Checks if two instances are not equal.                                                                                                                                                                                                   |

## Structs

| Struct                                                                                                                            | Description                                                                                                                                                                                                              |
| --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [GeneratorInstance.Arguments](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/arguments.md)                   | Additional arguments passed to the [RealtimeContext.Process](/engine/6000.7/script-reference/unityengine/audio/realtimecontext/process.md) method.                                                                       |
| [GeneratorInstance.Configuration](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/configuration.md)           | The configuration of a specific instance of a [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md).                                                                               |
| [GeneratorInstance.CreationParameters](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/creationparameters.md) | Parameters controlling how a [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md) is created.                                                                                     |
| [GeneratorInstance.Properties](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/properties.md)                 | Represents optional or additional metadata about the [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md).                                                                        |
| [GeneratorInstance.Result](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/result.md)                         | The result returned from a GeneratorInstance.IRealtime.Process call.                                                                                                                                                     |
| [GeneratorInstance.Setup](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/setup.md)                           | Information on the audio setup of the [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md) passed back to the instantiator from GeneratorInstance.IControl\<TRealtime>.Configure. |

## Interfaces

| Interface                                                                                                                  | Description                                                                                                                                                                                                      |
| -------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [GeneratorInstance.ICapabilities](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/icapabilities.md)    | Describes the runtime behaviour of the [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md). These reported values are cached in the beginning and assumed to not change. |
| [GeneratorInstance.IControl\<TRealtime>](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/icontrol1.md) | The control interface an implementation of a [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md) must implement on a struct to be fully formed.                          |
| [GeneratorInstance.IRealtime](/engine/6000.7/script-reference/unityengine/audio/generatorinstance/irealtime.md)            | The processing interface an implementation of a [GeneratorInstance](/engine/6000.7/script-reference/unityengine/audio/generatorinstance.md) must implement on a struct to be fully formed.                       |
