80 lines
2.0 KiB
C#
80 lines
2.0 KiB
C#
|
using OptimeGBA;
|
|||
|
using System;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
|
|||
|
public class AudioProvider : MonoBehaviour
|
|||
|
{
|
|||
|
[SerializeField]
|
|||
|
private AudioSource m_as;
|
|||
|
private RingBuffer<float> _buffer;
|
|||
|
private TimeSpan lastElapsed;
|
|||
|
public double audioFPS { get; private set; }
|
|||
|
float lastData = 0;
|
|||
|
|
|||
|
public void Awake()
|
|||
|
{
|
|||
|
//var dummy = AudioClip.Create("dummy", 1,2, AudioSettings.outputSampleRate, false);
|
|||
|
AudioClip clip = AudioClip.Create("blank", GbaAudio.SampleRate * 2, 2, GbaAudio.SampleRate, true);
|
|||
|
_buffer = new RingBuffer<float>(GbaAudio.SampleRate * 2);
|
|||
|
m_as.clip = clip;
|
|||
|
m_as.playOnAwake = true;
|
|||
|
//m_as.loop = true;
|
|||
|
m_as.spatialBlend = 1;
|
|||
|
}
|
|||
|
public void Initialize()
|
|||
|
{
|
|||
|
if (!m_as.isPlaying)
|
|||
|
{
|
|||
|
m_as.Play();
|
|||
|
}
|
|||
|
}
|
|||
|
private void OnAudioFilterRead(float[] data, int channels)
|
|||
|
{
|
|||
|
int step = channels;
|
|||
|
step = 1;
|
|||
|
for (int i = 0; i < data.Length; i += step)
|
|||
|
{
|
|||
|
if (_buffer.TryRead(out float rawData))
|
|||
|
{
|
|||
|
data[i] = rawData;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//int step = channels;
|
|||
|
//step = 1;
|
|||
|
//for (int i = 0; i < data.Length; i += step)
|
|||
|
//{
|
|||
|
// float rawFloat = lastData;
|
|||
|
// if (_buffer.TryRead(out float rawData))
|
|||
|
// {
|
|||
|
// rawFloat = rawData;
|
|||
|
// }
|
|||
|
|
|||
|
// data[i] = rawFloat;
|
|||
|
// for (int fill = 1; fill < step; fill++)
|
|||
|
// data[i + fill] = rawFloat;
|
|||
|
// lastData = rawFloat;
|
|||
|
//}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public void AudioReady(float[] data)
|
|||
|
{
|
|||
|
if (!Emulator.instance.EnableAudio) return;
|
|||
|
|
|||
|
var current = Emulator.sw.Elapsed;
|
|||
|
var delta = current - lastElapsed;
|
|||
|
lastElapsed = current;
|
|||
|
audioFPS = 1d / delta.TotalSeconds;
|
|||
|
|
|||
|
for (int i = 0; i < data.Length; i++)
|
|||
|
{
|
|||
|
_buffer.Write(data[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|