音频解决(缺少追赶机制)

This commit is contained in:
ALIENJACK\alien 2024-07-05 11:24:59 +08:00
parent 28e0071191
commit 2868c02c16
9 changed files with 152 additions and 135 deletions

View File

@ -25,5 +25,7 @@ namespace MyNes.Core
void SignalToggle(bool started); void SignalToggle(bool started);
void SetVolume(int Vol); void SetVolume(int Vol);
void Update();
} }
} }

View File

@ -37,5 +37,7 @@ namespace MyNes.Core
void ToggleFPS(bool show_fps); void ToggleFPS(bool show_fps);
void ApplyFilter(); void ApplyFilter();
void Update();
} }
} }

View File

@ -167,8 +167,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ac8cd27a180bf3e489b2ca27c821bffe, type: 3} m_Script: {fileID: 11500000, guid: ac8cd27a180bf3e489b2ca27c821bffe, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
AS: {fileID: 1379369699}
DrawImage: {fileID: 730321751} DrawImage: {fileID: 730321751}
DO: {fileID: 1379369700} DO: {fileID: 0}
Fps: {fileID: 1680039028} Fps: {fileID: 1680039028}
--- !u!1 &708549044 --- !u!1 &708549044
GameObject: GameObject:
@ -610,7 +611,6 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 1379369698} - component: {fileID: 1379369698}
- component: {fileID: 1379369699} - component: {fileID: 1379369699}
- component: {fileID: 1379369700}
m_Layer: 5 m_Layer: 5
m_Name: Audio m_Name: Audio
m_TagString: Untagged m_TagString: Untagged
@ -733,19 +733,6 @@ AudioSource:
m_PreInfinity: 2 m_PreInfinity: 2
m_PostInfinity: 2 m_PostInfinity: 2
m_RotationOrder: 4 m_RotationOrder: 4
--- !u!114 &1379369700
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1379369697}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f25db9f5a7339c34f94e6e978be38c82, type: 3}
m_Name:
m_EditorClassIdentifier:
Gain: 0.05
--- !u!1 &1680039027 --- !u!1 &1680039027
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -2,10 +2,12 @@ using MyNes.Core;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq; using System.Linq;
using System.Threading;
using UnityEditor.PackageManager.UI; using UnityEditor.PackageManager.UI;
using UnityEngine; using UnityEngine;
using UnityEngine.Profiling;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
@ -20,25 +22,47 @@ namespace AxibugEmuOnline.Client
public bool AllowFrequencyChange => true; public bool AllowFrequencyChange => true;
private bool m_isPlaying; private bool m_isPlaying;
private AudioSource m_as;
private int samples_added; private int samples_added;
private Queue<(float[], int)> queues = new Queue<(float[], int)>();
private Queue<float> _buffer = new Queue<float>();
public void Initialize() public void Initialize()
{ {
m_as = NesCoreProxy.Instance.AS;
m_as.clip = AudioClip.Create("nes wav", 48000 * 2, 1, 48000, true, OnAudioFilterRead);
m_as.loop = true;
m_as.playOnAwake = false;
m_as.spatialBlend = 0f;
m_as.Play();
}
public void Update() { }
private void OnAudioFilterRead(float[] data)
{
lock (_buffer)
{
for (int i = 0; i < data.Length; i++)
{
data[i] = _buffer.Count > 0 ? _buffer.Dequeue() : 0;
}
}
} }
public void SubmitSamples(ref short[] buffer, ref int samples_a) public void SubmitSamples(ref short[] buffer, ref int samples_a)
{ {
NesCoreProxy.Instance.DO.Play(buffer.Take(samples_a).SelectMany(s => toBytes(s)).ToArray()); lock (_buffer)
}
public byte[] toBytes(short value)
{ {
byte[] temp = new byte[2]; foreach (var a in buffer.Take(samples_a).ToArray())
//temp[0] = (byte)(value >> 8); {
temp[0] = temp[1] = (byte)((uint)value & 0xFFu); var floatData = (float)a / 124;
_buffer.Enqueue(floatData);
return temp; }
}
} }
public void TogglePause(bool paused) public void TogglePause(bool paused)

View File

@ -66,9 +66,10 @@ public class DefaultAudioOutput : MonoBehaviour
{ {
_pipeStream.Write(data, 0, data.Length); _pipeStream.Write(data, 0, data.Length);
} }
}
private class PipeStream : Stream public class PipeStream : Stream
{ {
private readonly Queue<byte> _buffer = new Queue<byte>(); private readonly Queue<byte> _buffer = new Queue<byte>();
private long _maxBufferLength = 8192; private long _maxBufferLength = 8192;
@ -179,6 +180,4 @@ public class DefaultAudioOutput : MonoBehaviour
get { return 0; } get { return 0; }
set { throw new NotImplementedException(); } set { throw new NotImplementedException(); }
} }
}
} }

View File

@ -34,7 +34,7 @@ namespace AxibugEmuOnline.Client
return color; return color;
} }
public void Draw() public void Update()
{ {
var colors = m_texRawBuffer.Select(w => GetColor((uint)w)).ToArray(); var colors = m_texRawBuffer.Select(w => GetColor((uint)w)).ToArray();
m_rawBufferWarper.SetPixels(colors); m_rawBufferWarper.SetPixels(colors);

View File

@ -1,4 +1,5 @@
using MyNes.Core; using MyNes;
using MyNes.Core;
using System.IO; using System.IO;
using UnityEngine; using UnityEngine;
@ -6,15 +7,15 @@ namespace AxibugEmuOnline.Client.Manager
{ {
public class AppEmu : IFileManager public class AppEmu : IFileManager
{ {
public UguiVideoProvider UguiVideo { get; private set; } public IVideoProvider UguiVideo { get; private set; }
public AudioProvider Audio { get; private set; } public IAudioProvider Audio { get; private set; }
public void Init() public void Init()
{ {
MyNesMain.Initialize(this); MyNesMain.Initialize(this);
NesEmu.LoadGame("E:/rzg4.nes", out var successed, true); NesEmu.LoadGame("E:/rzg4.nes", out var successed, true);
UguiVideo = MyNesMain.VideoProvider as UguiVideoProvider; UguiVideo = MyNesMain.VideoProvider;
Audio = MyNesMain.AudioProvider as AudioProvider; Audio = MyNesMain.AudioProvider;
var fps_nes_missle = 1.0 / 59.0; var fps_nes_missle = 1.0 / 59.0;
NesEmu.SetFramePeriod(ref fps_nes_missle); NesEmu.SetFramePeriod(ref fps_nes_missle);
@ -22,7 +23,8 @@ namespace AxibugEmuOnline.Client.Manager
public void Update() public void Update()
{ {
UguiVideo.Draw(); UguiVideo.Update();
Audio.Update();
double t = Time.deltaTime; double t = Time.deltaTime;
NesEmu.SetFramePeriod(ref t); NesEmu.SetFramePeriod(ref t);

View File

@ -10,6 +10,7 @@ namespace AxibugEmuOnline.Client
{ {
public static NesCoreProxy Instance { get; private set; } public static NesCoreProxy Instance { get; private set; }
public AudioSource AS;
public RawImage DrawImage; public RawImage DrawImage;
public DefaultAudioOutput DO; public DefaultAudioOutput DO;
public Text Fps; public Text Fps;

View File

@ -63,6 +63,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Network
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Performance.Profile-Analyzer.Editor", "Unity.Performance.Profile-Analyzer.Editor.csproj", "{F57AB79F-532A-EBC2-871B-64C9FD27FEA9}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Performance.Profile-Analyzer.Editor", "Unity.Performance.Profile-Analyzer.Editor.csproj", "{F57AB79F-532A-EBC2-871B-64C9FD27FEA9}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AxibugEmuOnline.Client", "AxibugEmuOnline.Client.csproj", "{59C211A4-B928-7505-AF2E-6C604DAB3622}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Scheduler", "Unity.Services.Core.Scheduler.csproj", "{2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Scheduler", "Unity.Services.Core.Scheduler.csproj", "{2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.VSCode.Editor", "Unity.VSCode.Editor.csproj", "{5A8F066D-8FFA-AFC1-1E8E-0DE9AEAE676F}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.VSCode.Editor", "Unity.VSCode.Editor.csproj", "{5A8F066D-8FFA-AFC1-1E8E-0DE9AEAE676F}"
@ -71,8 +73,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.TestTools.CodeCoverag
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Settings.Editor", "Unity.Settings.Editor.csproj", "{541572F4-E051-F4F8-9B39-87BFA550E13A}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Settings.Editor", "Unity.Settings.Editor.csproj", "{541572F4-E051-F4F8-9B39-87BFA550E13A}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AxibugEmuOnline.Client", "AxibugEmuOnline.Client.csproj", "{59C211A4-B928-7505-AF2E-6C604DAB3622}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Registration", "Unity.Services.Core.Registration.csproj", "{A5C76622-FE9B-BD41-A430-5208CCE95FDA}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Registration", "Unity.Services.Core.Registration.csproj", "{A5C76622-FE9B-BD41-A430-5208CCE95FDA}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Telemetry", "Unity.Services.Core.Telemetry.csproj", "{7F93B805-8A3C-D81B-FEE7-A4CE542AB2D2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Telemetry", "Unity.Services.Core.Telemetry.csproj", "{7F93B805-8A3C-D81B-FEE7-A4CE542AB2D2}"
@ -219,6 +219,10 @@ Global
{F57AB79F-532A-EBC2-871B-64C9FD27FEA9}.Debug|Any CPU.Build.0 = Debug|Any CPU {F57AB79F-532A-EBC2-871B-64C9FD27FEA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F57AB79F-532A-EBC2-871B-64C9FD27FEA9}.Release|Any CPU.ActiveCfg = Release|Any CPU {F57AB79F-532A-EBC2-871B-64C9FD27FEA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F57AB79F-532A-EBC2-871B-64C9FD27FEA9}.Release|Any CPU.Build.0 = Release|Any CPU {F57AB79F-532A-EBC2-871B-64C9FD27FEA9}.Release|Any CPU.Build.0 = Release|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Release|Any CPU.Build.0 = Release|Any CPU
{2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}.Debug|Any CPU.Build.0 = Debug|Any CPU {2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}.Release|Any CPU.ActiveCfg = Release|Any CPU {2598AAB1-DA3E-A2AE-B060-D1BAF7BBE4BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -235,10 +239,6 @@ Global
{541572F4-E051-F4F8-9B39-87BFA550E13A}.Debug|Any CPU.Build.0 = Debug|Any CPU {541572F4-E051-F4F8-9B39-87BFA550E13A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{541572F4-E051-F4F8-9B39-87BFA550E13A}.Release|Any CPU.ActiveCfg = Release|Any CPU {541572F4-E051-F4F8-9B39-87BFA550E13A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{541572F4-E051-F4F8-9B39-87BFA550E13A}.Release|Any CPU.Build.0 = Release|Any CPU {541572F4-E051-F4F8-9B39-87BFA550E13A}.Release|Any CPU.Build.0 = Release|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59C211A4-B928-7505-AF2E-6C604DAB3622}.Release|Any CPU.Build.0 = Release|Any CPU
{A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Debug|Any CPU.Build.0 = Debug|Any CPU {A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Release|Any CPU.ActiveCfg = Release|Any CPU {A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Release|Any CPU.ActiveCfg = Release|Any CPU