Audio API
If you are using a third-party plugin for audio in Unity, then you may need to use the Luna Replay Audio API in order to correctly capture and replay the sound for your video.
Luna Replay is able to capture audio bytes when Unity's audio system is used. However, when a third-party plugin is used, we don't have access to these audio bytes.
The API has the following methods.
// Accepts data in Unity UnityEngine.AudioRenderer's format
public static void SetFrameAudioSample( NativeArray<float> sample );
// Accepts data in s16le format
// (see https://trac.ffmpeg.org/wiki/audio%20types for description).
public static void SetFrameAudioBytes( IReadOnlyCollection<byte> s16LeBytes );
Example:
public class AudioTest : MonoBehaviour {
public void Awake() {
StartCoroutine( Coroutine() );
}
private IEnumerator Coroutine() {
while ( true ) {
var sample = GetSample(); // Get audio sample from 3rd party audio engine.
Audio.SetFrameAudioSample( sample );
}
}
}
Integrate Luna Replay with Wwise
To integrate Replay with Wwise audio engine please use the following code:
Show code
using Luna.Replay.Api;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class OfflineWwiseRendering: MonoBehaviour {
private bool isOfflineRendering;
private bool isCurrentlyOfflineRendering;
private ulong outputDeviceId;
protected void Start() {
AkSoundEngine.SetErrorLogger();
#if LUNA_REPLAY
isOfflineRendering = true;
#endif
if (!isOfflineRendering) {
return;
}
outputDeviceId = AkSoundEngine.GetOutputID(AkSoundEngine.AK_INVALID_UNIQUE_ID, 0);
Update();
}
protected void Update() {
if (!isOfflineRendering) {
return;
}
if (!isCurrentlyOfflineRendering) {
isCurrentlyOfflineRendering = true;
#if UNITY_EDITOR
// Ensure that the editor update does not call AkSoundEngine.RenderAudio().
AkSoundEngineController.Instance.DisableEditorLateUpdate();
#endif
AkSoundEngine.ClearCaptureData();
AkSoundEngine.StartDeviceCapture(outputDeviceId);
}
var frameRate = Time.captureFramerate;
var frameTime = frameRate != 0.0 f ? 1.0 f / frameRate : 0.0 f;
AkSoundEngine.SetOfflineRenderingFrameTime(frameTime);
AkSoundEngine.SetOfflineRendering(isOfflineRendering);
var sampleCount = AkSoundEngine.UpdateCaptureSampleCount(outputDeviceId);
if (sampleCount <= 0) {
return;
}
var buffer = new float[sampleCount];
var count = AkSoundEngine.GetCaptureSamples(outputDeviceId, buffer, (uint) buffer.Length);
if (count <= 0) {
return;
}
Audio.SetFrameAudioSample(buffer);
}
}
For more information check out Wwise documentation.