GetSampleCountForCaptureFrame()
Returns the number of sample frames available since the last time AudioRenderer.Render was called. This count depends on the frame capture rate.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.AudioModule
public static int GetSampleCountForCaptureFrame()
Returns
Type | Description |
|---|---|
| int | Number of sample frames available since the last recorded frame, or 0 if the engine isn't in recording mode. |
Remarks
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.
Examples
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(); }}