Unity
Unity_real time _ audio thread : audioBuffer
잉_민
2023. 11. 6. 14:50
728x90
반응형
두 개의 오디오가 실시간으로 들어오는 상황.
둘이 같은 smaplerate로 들어와야 한다.
메인에서는 EEG의 alpha 값이 들어와서 담아지고 있는 상황.
Unity 에서 지원하는 audio thread를 사용할 수 있다.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnAudioFilterRead.html
Unity - Scripting API: MonoBehaviour.OnAudioFilterRead(float[], int)
The filter is inserted in the same order as the MonoBehaviour script is shown in the Inspector. OnAudioFilterRead is called every time a chunk of audio is sent to the filter (this happens frequently, every ~20ms depending on the sample rate and platform).
docs.unity3d.com
// AudioDataReceiver.cs
using UnityEngine;
using System.Collections.Generic;
public class AudioDataReceiver : MonoBehaviour
{
public List<float> audioBuffer = new List<float>();
int sampleRate = 44100;
// 이 메소드는 오디오 소스가 오디오 데이터를 필터링할 때마다 호출됩니다.
void OnAudioFilterRead(float[] data, int channels)
{
// 오디오 데이터를 버퍼에 추가합니다.
for (int i = 0; i < data.Length; i += channels)
{
audioBuffer.Add(data[i]);
if (channels == 2 && i + 1 < data.Length) // 스테레오 오디오를 위한 처리
{
audioBuffer.Add(data[i + 1]);
}
}
// 버퍼가 너무 커지지 않도록 관리합니다.
if (audioBuffer.Count > 2 * sampleRate) // 예를 들어, 2초 분량 이상의 데이터는 필요하지 않다고 가정
{
audioBuffer.RemoveRange(0, audioBuffer.Count - 2 * sampleRate);
}
}
}
audio source 컴포넌트가 audio clip을 필터링할때마다 호출되기 때문에
실시간으로 오디오를 버퍼에 담기 좋다.
728x90
반응형