Version: Unity 6.3 Beta (6000.3)
LanguageEnglish
  • C#

Generator

struct in UnityEngine.Audio

/

Implemented in:UnityEngine.AudioModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

A Processor that generates audio data.

A generator is defined through implementing the Generator<T0> and IProcessor interfaces, which defines the control and processing thread behaviour respectively.

To use a generator in a scene, for example, with an AudioSource it must have an associated IGeneratorDefinition that will instantiate it. In the Unity Audio system they are used with an AudioSource through setting the AudioSource.resource property to the associated IGeneratorDefinition.

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 generator. If the generator has been set on an AudioSource you would interact with it through the handle obtained with AudioSource.generatorHandle.

using Unity.Burst;
using Unity.IntegerTime;
using UnityEngine;
using UnityEngine.Audio;

[BurstCompile(CompileSynchronously = true)] public struct SineProcessor : Generator.IProcessor { private const float k_Tau = Mathf.PI * 2; private float m_Frequency; private float m_Phase;

public static Generator Allocate(ControlContext context, float frequency) { return context.AllocateGenerator(new SineProcessor(frequency), new Control()); }

public bool isFinite => false; public bool isRealtime => true; public DiscreteTime? length => null;

private Generator.Setup m_Setup;

SineProcessor(float initialFrequency) { m_Frequency = initialFrequency; m_Phase = 0.0f; m_Setup = new Generator.Setup(); }

public void DataAvailable(DataAvailableContext context) { foreach (var data in context.GetAvailableData()) { if (data.TryGetData(out FrequencyData freq)) { m_Frequency = freq.Value; } } }

public void ReturnData(ReturnDataContext context) { }

public Generator.Result Process(in ProcessingContext ctx, ChannelBuffer buffer, Generator.Arguments args) { for (var frame = 0; frame < buffer.frameCount; frame++) { for (var channel = 0; channel < buffer.channelCount; channel++) buffer[channel, frame] = Mathf.Sin(m_Phase * k_Tau);

m_Phase += m_Frequency / m_Setup.samplingRate;

if (m_Phase > 1.0f) m_Phase -= 1.0f; }

return buffer.frameCount; }

struct Control : Generator.IControl<SineProcessor> { public void Configure(ControlContext context, ref SineProcessor generator, in DSPConfiguration config, out Generator.Setup setup, ref Generator.Properties p) { generator.m_Setup = new Generator.Setup(channelCount: 1, samplingRate: config.sampleRate); setup = generator.m_Setup; }

public void Dispose(ControlContext context, ref SineProcessor processor) { }

public void Update(ControlContext context, AvailableData availableData) { }

public MessageStatus OnMessage(ControlContext context, Message message) { return MessageStatus.Unhandled; } }

public struct FrequencyData { public readonly float Value; public FrequencyData(float value) { Value = value; } } }

Public Methods

Method Description
ConfigureManually configures a particular instance of a Generator.
ProcessManually process a particular instance of a Generator.
UpdateManually Update a Generator.

Operators

Operator Description
ProcessorManually Process a Generator.