2024-07-04 21:06:41 +08:00
|
|
|
using MyNes.Core;
|
|
|
|
using System.Collections.Generic;
|
2024-07-05 11:24:59 +08:00
|
|
|
using System.Threading;
|
2024-07-04 21:06:41 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace AxibugEmuOnline.Client
|
|
|
|
{
|
2024-07-10 15:02:14 +08:00
|
|
|
public class AudioProvider : MonoBehaviour, IAudioProvider
|
2024-07-04 21:06:41 +08:00
|
|
|
{
|
|
|
|
public string Name => nameof(AudioProvider);
|
|
|
|
|
|
|
|
public string ID => Name.GetHashCode().ToString();
|
|
|
|
|
|
|
|
public bool AllowBufferChange => true;
|
|
|
|
|
|
|
|
public bool AllowFrequencyChange => true;
|
|
|
|
|
|
|
|
private bool m_isPlaying;
|
2024-07-10 15:02:14 +08:00
|
|
|
|
|
|
|
[SerializeField]
|
2024-07-05 11:24:59 +08:00
|
|
|
private AudioSource m_as;
|
2024-07-04 21:06:41 +08:00
|
|
|
|
2024-07-05 11:24:59 +08:00
|
|
|
|
2024-07-10 15:02:14 +08:00
|
|
|
private Queue<short> _buffer = new Queue<short>();
|
2024-07-05 11:24:59 +08:00
|
|
|
|
2024-07-04 21:06:41 +08:00
|
|
|
public void Initialize()
|
|
|
|
{
|
2024-07-10 15:02:14 +08:00
|
|
|
var dummy = AudioClip.Create("dummy", 1, 1, AudioSettings.outputSampleRate, false);
|
2024-07-05 11:24:59 +08:00
|
|
|
|
2024-07-10 15:02:14 +08:00
|
|
|
dummy.SetData(new float[] { 1 }, 0);
|
|
|
|
m_as.clip = dummy; //just to let unity play the audiosource
|
|
|
|
m_as.loop = true;
|
|
|
|
m_as.spatialBlend = 1;
|
2024-07-05 11:24:59 +08:00
|
|
|
m_as.Play();
|
2024-07-04 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2024-07-10 15:02:14 +08:00
|
|
|
void OnAudioFilterRead(float[] data, int channels)
|
2024-07-04 21:06:41 +08:00
|
|
|
{
|
2024-07-10 15:02:14 +08:00
|
|
|
while (_buffer.Count >= data.Length / 2)
|
2024-07-05 11:24:59 +08:00
|
|
|
{
|
2024-07-10 15:02:14 +08:00
|
|
|
//Thread.Sleep(10);
|
|
|
|
break;
|
2024-07-05 11:24:59 +08:00
|
|
|
}
|
2024-07-10 15:02:14 +08:00
|
|
|
|
|
|
|
int step = channels;
|
|
|
|
for (int i = 0; i < data.Length; i += step)
|
|
|
|
{
|
|
|
|
var rawData = _buffer.Count > 0 ? _buffer.Dequeue() : 0;
|
|
|
|
var rawFloat = rawData / 124f;
|
|
|
|
data[i] = rawFloat;
|
|
|
|
for (int fill = 1; fill < step; fill++)
|
|
|
|
data[i + fill] = rawFloat;
|
|
|
|
}
|
|
|
|
|
2024-07-04 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2024-07-10 15:02:14 +08:00
|
|
|
int EmuAudioTimeSample = 0;
|
2024-07-05 11:24:59 +08:00
|
|
|
public void SubmitSamples(ref short[] buffer, ref int samples_a)
|
2024-07-04 21:06:41 +08:00
|
|
|
{
|
2024-07-10 15:02:14 +08:00
|
|
|
EmuAudioTimeSample += samples_a;
|
|
|
|
for (int i = 0; i < samples_a; i++)
|
2024-07-05 11:24:59 +08:00
|
|
|
{
|
2024-07-10 15:02:14 +08:00
|
|
|
_buffer.Enqueue(buffer[i]);
|
2024-07-05 11:24:59 +08:00
|
|
|
}
|
2024-07-10 15:02:14 +08:00
|
|
|
|
2024-07-04 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void TogglePause(bool paused)
|
|
|
|
{
|
|
|
|
m_isPlaying = !paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void GetIsPlaying(out bool playing)
|
|
|
|
{
|
|
|
|
playing = m_isPlaying;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ShutDown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SignalToggle(bool started)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetVolume(int Vol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|