Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Render(NativeArray<float>)

Writes the audio rendered since the previous call into the supplied buffer.
Read time 1 minuteLast updated 5 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.AudioModule
public static bool Render(NativeArray<float> buffer)

Parameters

The buffer to write the sample data to. Its length must be the value that AudioRenderer.GetSampleCountForCaptureFrame returns, multiplied by the channel count of the current speaker mode.

Returns

Type

Description

boolReturns
true
if the recording succeeded and
false
if the engine isn't in recording mode.

Remarks

Call this method once per frame while the engine is in recording mode. Unity writes interleaved samples for every output channel into the buffer, so the buffer length must match the sample frame count that AudioRenderer.GetSampleCountForCaptureFrame reports, multiplied by the channel count of the current speaker mode.
An oversized buffer takes audio from ahead of the current frame, so the recording drifts out of sync with the visuals.
Call this method on every frame, including the frames where AudioRenderer.GetSampleCountForCaptureFrame returns 0, otherwise the capture stops producing samples. Pass a zero-length buffer on those frames. Calling this method outside recording mode logs an error and returns false.

Examples

using Unity.Collections;using UnityEngine;public class RenderExample : MonoBehaviour{ void Update() { // Assumes AudioRenderer.Start has already been called. int sampleFrames = AudioRenderer.GetSampleCountForCaptureFrame(); AudioConfiguration config = AudioSettings.GetConfiguration(); AudioSpeakerMode speakerMode = config.speakerMode; int channels = speakerMode.ChannelCount(); int length = sampleFrames * channels; NativeArray<float> buffer = new NativeArray<float>( length, Allocator.Temp); // A zero-length buffer is expected on frames that produce // fewer samples than one DSP buffer. if (AudioRenderer.Render(buffer) && sampleFrames > 0) Debug.Log("Rendered " + sampleFrames + " sample frames."); buffer.Dispose(); }}