Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

AudioRenderer.GetSampleCountForCaptureFrame

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

Declaration

public static int GetSampleCountForCaptureFrame();

Returns

int Number of sample frames available since the last recorded frame, or 0 if the engine isn't in recording mode.

Description

Returns the number of sample frames available since the last time AudioRenderer.Render was called. This count depends on the frame capture rate.

A sample frame holds one sample for each output channel. Multiply this count by the channel count of the current speaker mode to get the length of the buffer that AudioRenderer.Render fills.

The count follows Time.captureDeltaTime when that property is non-zero, and Time.deltaTime otherwise.

Call AudioRenderer.Render on every frame, including the frames that report 0. Unity discards the audio for any frame you skip, and only AudioRenderer.Render carries the leftover samples forward to the next frame.

using Unity.Collections;
using UnityEngine;

public class SampleCountExample : 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();

NativeArray<float> buffer = new NativeArray<float>( sampleFrames * channels, Allocator.Temp);

// Render runs even when the count is zero, so that the next // frame reports the samples accumulated so far. AudioRenderer.Render(buffer); buffer.Dispose(); } }