fixed audio
This commit is contained in:
parent
d30a4e1d9c
commit
113ea59fe9
@ -44,9 +44,9 @@ public class Essgeeinit : MonoBehaviour
|
|||||||
uegResources = new UEGResources();
|
uegResources = new UEGResources();
|
||||||
uegLog = new UEGLog();
|
uegLog = new UEGLog();
|
||||||
InitAll(uegResources, Application.persistentDataPath);
|
InitAll(uegResources, Application.persistentDataPath);
|
||||||
//LoadAndRunCartridge("G:/psjapa.sms");
|
LoadAndRunCartridge("G:/psjapa.sms");
|
||||||
//LoadAndRunCartridge("G:/Ninja_Gaiden_(UE)_type_A_[!].sms");
|
//LoadAndRunCartridge("G:/Ninja_Gaiden_(UE)_type_A_[!].sms");
|
||||||
LoadAndRunCartridge("G:/SML2.gb");
|
//LoadAndRunCartridge("G:/SML2.gb");
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDisable()
|
void OnDisable()
|
||||||
|
@ -9,7 +9,7 @@ public class UEGSoundPlayer : MonoBehaviour//, ISoundPlayer
|
|||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private AudioSource m_as;
|
private AudioSource m_as;
|
||||||
private RingBuffer<float> _buffer = new RingBuffer<float>(44100*2);
|
private RingBuffer<float> _buffer = new RingBuffer<float>(44100 * 2);
|
||||||
private TimeSpan lastElapsed;
|
private TimeSpan lastElapsed;
|
||||||
public double audioFPS { get; private set; }
|
public double audioFPS { get; private set; }
|
||||||
public bool IsRecording { get; private set; }
|
public bool IsRecording { get; private set; }
|
||||||
@ -20,37 +20,45 @@ public class UEGSoundPlayer : MonoBehaviour//, ISoundPlayer
|
|||||||
private int writePos = 0;
|
private int writePos = 0;
|
||||||
private float[] buffer;
|
private float[] buffer;
|
||||||
|
|
||||||
void Start()
|
void Awake()
|
||||||
{
|
{
|
||||||
|
// 获取当前音频配置
|
||||||
|
AudioConfiguration config = AudioSettings.GetConfiguration();
|
||||||
|
|
||||||
|
// 设置目标音频配置
|
||||||
|
config.sampleRate = 44100; // 采样率为 44100Hz
|
||||||
|
config.numRealVoices = 32; // 设置最大音频源数量(可选)
|
||||||
|
config.numVirtualVoices = 512; // 设置虚拟音频源数量(可选)
|
||||||
|
config.dspBufferSize = 1024; // 设置 DSP 缓冲区大小(可选)
|
||||||
|
config.speakerMode = AudioSpeakerMode.Stereo; // 设置为立体声(2 声道)
|
||||||
|
|
||||||
|
// 应用新的音频配置
|
||||||
|
if (AudioSettings.Reset(config))
|
||||||
|
{
|
||||||
|
Debug.Log("Audio settings updated successfully.");
|
||||||
|
Debug.Log("Sample Rate: " + config.sampleRate + "Hz");
|
||||||
|
Debug.Log("Speaker Mode: " + config.speakerMode);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("Failed to update audio settings.");
|
||||||
|
}
|
||||||
|
|
||||||
GetComponent<AudioSource>().PlayOneShot(audioClip);
|
GetComponent<AudioSource>().PlayOneShot(audioClip);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Queue<float> sampleQueue = new Queue<float>();
|
private Queue<float> sampleQueue = new Queue<float>();
|
||||||
|
|
||||||
// 外部填充数据
|
|
||||||
public void AddData(float[] data)
|
|
||||||
{
|
|
||||||
lock (sampleQueue)
|
|
||||||
{
|
|
||||||
foreach (var sample in data)
|
|
||||||
{
|
|
||||||
sampleQueue.Enqueue(sample);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unity ÒôƵÏ̻߳ص÷
|
// Unity ÒôƵÏ̻߳ص÷
|
||||||
void OnAudioFilterRead(float[] data, int channels)
|
void OnAudioFilterRead(float[] data, int channels)
|
||||||
{
|
{
|
||||||
lock (sampleQueue)
|
for (int i = 0; i < data.Length; i++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < data.Length; i++)
|
if (_buffer.TryRead(out float rawData))
|
||||||
{
|
data[i] = rawData;
|
||||||
if (sampleQueue.Count > 0)
|
else
|
||||||
data[i] = sampleQueue.Dequeue();
|
data[i] = 0; // 无数据时静音
|
||||||
else
|
|
||||||
data[i] = 0; // 无数据时静音
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,30 +86,12 @@ public class UEGSoundPlayer : MonoBehaviour//, ISoundPlayer
|
|||||||
lastElapsed = current;
|
lastElapsed = current;
|
||||||
audioFPS = 1d / delta.TotalSeconds;
|
audioFPS = 1d / delta.TotalSeconds;
|
||||||
|
|
||||||
//for (int i = 0; i < samples_a; i += 1)
|
for (int i = 0; i < samples_a; i += 1)
|
||||||
//{
|
|
||||||
// //_buffer.Write(((i % 2 == 0)?-1:1) * buffer[i] / 32767.0f);
|
|
||||||
// _buffer.Write(buffer[i] / 32767.0f);
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
AddData(ConvertShortToFloat(buffer, samples_a));
|
|
||||||
|
|
||||||
if (IsRecording)
|
|
||||||
{
|
{
|
||||||
dataChunk.AddSampleData(buffer, samples_a);
|
_buffer.Write(buffer[i] / 32767.0f);
|
||||||
waveHeader.FileLength += (uint)samples_a;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe float[] ConvertShortToFloat(short* input,int length)
|
|
||||||
{
|
|
||||||
float[] output = new float[length];
|
|
||||||
for (int i = 0; i < length; i++)
|
|
||||||
{
|
|
||||||
output[i] = input[i] / 32768.0f;
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
public void BufferWirte(int Off, byte[] Data)
|
public void BufferWirte(int Off, byte[] Data)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -312,7 +302,7 @@ public class UEGSoundPlayer : MonoBehaviour//, ISoundPlayer
|
|||||||
|
|
||||||
ChunkSize += (uint)(stereoBuffer.Length * 2);
|
ChunkSize += (uint)(stereoBuffer.Length * 2);
|
||||||
}
|
}
|
||||||
public unsafe void AddSampleData(short* stereoBuffer,int lenght)
|
public unsafe void AddSampleData(short* stereoBuffer, int lenght)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < lenght; i++)
|
for (int i = 0; i < lenght; i++)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user