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; } } }
Method | Description |
---|---|
Configure | Manually configures a particular instance of a Generator. |
Process | Manually process a particular instance of a Generator. |
Update | Manually Update a Generator. |
Operator | Description |
---|---|
Processor | Manually Process a Generator. |