From 2868c02c16c8f429c12409ef8e856e652a3f3e54 Mon Sep 17 00:00:00 2001 From: "ALIENJACK\\alien" Date: Fri, 5 Jul 2024 11:24:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9F=B3=E9=A2=91=E8=A7=A3=E5=86=B3(=E7=BC=BA?= =?UTF-8?q?=E5=B0=91=E8=BF=BD=E8=B5=B6=E6=9C=BA=E5=88=B6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/MyNes.Core/IAudioProvider.cs | 2 + .../Assets/MyNes.Core/IVideoProvider.cs | 2 + .../Assets/Scene/EmuTest.unity | 17 +- .../Assets/Script/Emu/AudioProvider.cs | 48 +++-- .../Assets/Script/Emu/DefaultAudioOutput.cs | 189 +++++++++--------- .../Assets/Script/Emu/UguiVideoProvider.cs | 2 +- .../Assets/Script/Manager/AppEmu.cs | 14 +- .../Assets/Script/NesCoreProxy.cs | 1 + .../AxibugEmuOnline.Client.sln | 12 +- 9 files changed, 152 insertions(+), 135 deletions(-) diff --git a/AxibugEmuOnline.Client/Assets/MyNes.Core/IAudioProvider.cs b/AxibugEmuOnline.Client/Assets/MyNes.Core/IAudioProvider.cs index 6388e3b..547b742 100644 --- a/AxibugEmuOnline.Client/Assets/MyNes.Core/IAudioProvider.cs +++ b/AxibugEmuOnline.Client/Assets/MyNes.Core/IAudioProvider.cs @@ -25,5 +25,7 @@ namespace MyNes.Core void SignalToggle(bool started); void SetVolume(int Vol); + + void Update(); } } diff --git a/AxibugEmuOnline.Client/Assets/MyNes.Core/IVideoProvider.cs b/AxibugEmuOnline.Client/Assets/MyNes.Core/IVideoProvider.cs index b89152c..a830e74 100644 --- a/AxibugEmuOnline.Client/Assets/MyNes.Core/IVideoProvider.cs +++ b/AxibugEmuOnline.Client/Assets/MyNes.Core/IVideoProvider.cs @@ -37,5 +37,7 @@ namespace MyNes.Core void ToggleFPS(bool show_fps); void ApplyFilter(); + + void Update(); } } diff --git a/AxibugEmuOnline.Client/Assets/Scene/EmuTest.unity b/AxibugEmuOnline.Client/Assets/Scene/EmuTest.unity index 378b7f0..e44b63c 100644 --- a/AxibugEmuOnline.Client/Assets/Scene/EmuTest.unity +++ b/AxibugEmuOnline.Client/Assets/Scene/EmuTest.unity @@ -167,8 +167,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ac8cd27a180bf3e489b2ca27c821bffe, type: 3} m_Name: m_EditorClassIdentifier: + AS: {fileID: 1379369699} DrawImage: {fileID: 730321751} - DO: {fileID: 1379369700} + DO: {fileID: 0} Fps: {fileID: 1680039028} --- !u!1 &708549044 GameObject: @@ -610,7 +611,6 @@ GameObject: m_Component: - component: {fileID: 1379369698} - component: {fileID: 1379369699} - - component: {fileID: 1379369700} m_Layer: 5 m_Name: Audio m_TagString: Untagged @@ -733,19 +733,6 @@ AudioSource: m_PreInfinity: 2 m_PostInfinity: 2 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 GameObject: m_ObjectHideFlags: 0 diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/AudioProvider.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/AudioProvider.cs index 9024a83..8a0af09 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Emu/AudioProvider.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/AudioProvider.cs @@ -2,10 +2,12 @@ using MyNes.Core; using System; using System.Collections; using System.Collections.Generic; +using System.IO; +using System.IO.Pipes; using System.Linq; +using System.Threading; using UnityEditor.PackageManager.UI; using UnityEngine; -using UnityEngine.Profiling; namespace AxibugEmuOnline.Client { @@ -20,25 +22,47 @@ namespace AxibugEmuOnline.Client public bool AllowFrequencyChange => true; private bool m_isPlaying; + private AudioSource m_as; private int samples_added; - private Queue<(float[], int)> queues = new Queue<(float[], int)>(); + + private Queue _buffer = new Queue(); + 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) { - NesCoreProxy.Instance.DO.Play(buffer.Take(samples_a).SelectMany(s => toBytes(s)).ToArray()); - } - - public byte[] toBytes(short value) - { - byte[] temp = new byte[2]; - //temp[0] = (byte)(value >> 8); - temp[0] = temp[1] = (byte)((uint)value & 0xFFu); - - return temp; + lock (_buffer) + { + foreach (var a in buffer.Take(samples_a).ToArray()) + { + var floatData = (float)a / 124; + _buffer.Enqueue(floatData); + } + } } public void TogglePause(bool paused) diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/DefaultAudioOutput.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/DefaultAudioOutput.cs index 6174481..96f145a 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Emu/DefaultAudioOutput.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/DefaultAudioOutput.cs @@ -65,120 +65,119 @@ public class DefaultAudioOutput : MonoBehaviour public void Play(byte[] data) { _pipeStream.Write(data, 0, data.Length); + } +} + +public class PipeStream : Stream +{ + private readonly Queue _buffer = new Queue(); + private long _maxBufferLength = 8192; + + public long MaxBufferLength + { + get { return _maxBufferLength; } + set { _maxBufferLength = value; } } - private class PipeStream : Stream + public new void Dispose() { - private readonly Queue _buffer = new Queue(); - private long _maxBufferLength = 8192; + _buffer.Clear(); + } - public long MaxBufferLength + public override void Flush() + { + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotImplementedException(); + } + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + public override int Read(byte[] buffer, int offset, int count) + { + if (offset != 0) + throw new NotImplementedException("Offsets with value of non-zero are not supported"); + if (buffer == null) + throw new ArgumentException("Buffer is null"); + if (offset + count > buffer.Length) + throw new ArgumentException("The sum of offset and count is greater than the buffer length. "); + if (offset < 0 || count < 0) + throw new ArgumentOutOfRangeException("offset", "offset or count is negative."); + + if (count == 0) + return 0; + + int readLength = 0; + + lock (_buffer) { - get { return _maxBufferLength; } - set { _maxBufferLength = value; } - } - - public new void Dispose() - { - _buffer.Clear(); - } - - public override void Flush() - { - } - - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotImplementedException(); - } - - public override void SetLength(long value) - { - throw new NotImplementedException(); - } - - public override int Read(byte[] buffer, int offset, int count) - { - if (offset != 0) - throw new NotImplementedException("Offsets with value of non-zero are not supported"); - if (buffer == null) - throw new ArgumentException("Buffer is null"); - if (offset + count > buffer.Length) - throw new ArgumentException("The sum of offset and count is greater than the buffer length. "); - if (offset < 0 || count < 0) - throw new ArgumentOutOfRangeException("offset", "offset or count is negative."); - - if (count == 0) - return 0; - - int readLength = 0; - - lock (_buffer) + // fill the read buffer + for (; readLength < count && Length > 0; readLength++) { - // fill the read buffer - for (; readLength < count && Length > 0; readLength++) - { - buffer[readLength] = _buffer.Dequeue(); - } + buffer[readLength] = _buffer.Dequeue(); } - - return readLength; } - private bool ReadAvailable(int count) - { - return (Length >= count); - } + return readLength; + } - public override void Write(byte[] buffer, int offset, int count) + private bool ReadAvailable(int count) + { + return (Length >= count); + } + + public override void Write(byte[] buffer, int offset, int count) + { + if (buffer == null) + throw new ArgumentException("Buffer is null"); + if (offset + count > buffer.Length) + throw new ArgumentException("The sum of offset and count is greater than the buffer length. "); + if (offset < 0 || count < 0) + throw new ArgumentOutOfRangeException("offset", "offset or count is negative."); + if (count == 0) + return; + + lock (_buffer) { - if (buffer == null) - throw new ArgumentException("Buffer is null"); - if (offset + count > buffer.Length) - throw new ArgumentException("The sum of offset and count is greater than the buffer length. "); - if (offset < 0 || count < 0) - throw new ArgumentOutOfRangeException("offset", "offset or count is negative."); - if (count == 0) + while (Length >= _maxBufferLength) return; - lock (_buffer) + // queue up the buffer data + foreach (byte b in buffer) { - while (Length >= _maxBufferLength) - return; - - // queue up the buffer data - foreach (byte b in buffer) - { - _buffer.Enqueue(b); - } + _buffer.Enqueue(b); } } + } - public override bool CanRead - { - get { return true; } - } + public override bool CanRead + { + get { return true; } + } - public override bool CanSeek - { - get { return false; } - } + public override bool CanSeek + { + get { return false; } + } - public override bool CanWrite - { - get { return true; } - } + public override bool CanWrite + { + get { return true; } + } - public override long Length - { - get { return _buffer.Count; } - } + public override long Length + { + get { return _buffer.Count; } + } - public override long Position - { - get { return 0; } - set { throw new NotImplementedException(); } - } - } - + public override long Position + { + get { return 0; } + set { throw new NotImplementedException(); } + } } \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/UguiVideoProvider.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/UguiVideoProvider.cs index 1b33e3d..b8a1aff 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Emu/UguiVideoProvider.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/UguiVideoProvider.cs @@ -34,7 +34,7 @@ namespace AxibugEmuOnline.Client return color; } - public void Draw() + public void Update() { var colors = m_texRawBuffer.Select(w => GetColor((uint)w)).ToArray(); m_rawBufferWarper.SetPixels(colors); diff --git a/AxibugEmuOnline.Client/Assets/Script/Manager/AppEmu.cs b/AxibugEmuOnline.Client/Assets/Script/Manager/AppEmu.cs index 21c8606..420186c 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Manager/AppEmu.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Manager/AppEmu.cs @@ -1,4 +1,5 @@ -using MyNes.Core; +using MyNes; +using MyNes.Core; using System.IO; using UnityEngine; @@ -6,15 +7,15 @@ namespace AxibugEmuOnline.Client.Manager { public class AppEmu : IFileManager { - public UguiVideoProvider UguiVideo { get; private set; } - public AudioProvider Audio { get; private set; } + public IVideoProvider UguiVideo { get; private set; } + public IAudioProvider Audio { get; private set; } public void Init() { MyNesMain.Initialize(this); NesEmu.LoadGame("E:/rzg4.nes", out var successed, true); - UguiVideo = MyNesMain.VideoProvider as UguiVideoProvider; - Audio = MyNesMain.AudioProvider as AudioProvider; + UguiVideo = MyNesMain.VideoProvider; + Audio = MyNesMain.AudioProvider; var fps_nes_missle = 1.0 / 59.0; NesEmu.SetFramePeriod(ref fps_nes_missle); @@ -22,7 +23,8 @@ namespace AxibugEmuOnline.Client.Manager public void Update() { - UguiVideo.Draw(); + UguiVideo.Update(); + Audio.Update(); double t = Time.deltaTime; NesEmu.SetFramePeriod(ref t); diff --git a/AxibugEmuOnline.Client/Assets/Script/NesCoreProxy.cs b/AxibugEmuOnline.Client/Assets/Script/NesCoreProxy.cs index 0692edb..f70bb9f 100644 --- a/AxibugEmuOnline.Client/Assets/Script/NesCoreProxy.cs +++ b/AxibugEmuOnline.Client/Assets/Script/NesCoreProxy.cs @@ -10,6 +10,7 @@ namespace AxibugEmuOnline.Client { public static NesCoreProxy Instance { get; private set; } + public AudioSource AS; public RawImage DrawImage; public DefaultAudioOutput DO; public Text Fps; diff --git a/AxibugEmuOnline.Client/AxibugEmuOnline.Client.sln b/AxibugEmuOnline.Client/AxibugEmuOnline.Client.sln index f2b666d..579c232 100644 --- a/AxibugEmuOnline.Client/AxibugEmuOnline.Client.sln +++ b/AxibugEmuOnline.Client/AxibugEmuOnline.Client.sln @@ -63,6 +63,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Services.Core.Network EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Performance.Profile-Analyzer.Editor", "Unity.Performance.Profile-Analyzer.Editor.csproj", "{F57AB79F-532A-EBC2-871B-64C9FD27FEA9}" 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}" EndProject 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 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Settings.Editor", "Unity.Settings.Editor.csproj", "{541572F4-E051-F4F8-9B39-87BFA550E13A}" 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}" EndProject 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}.Release|Any CPU.ActiveCfg = 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.Build.0 = Debug|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}.Release|Any CPU.ActiveCfg = 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.Build.0 = Debug|Any CPU {A5C76622-FE9B-BD41-A430-5208CCE95FDA}.Release|Any CPU.ActiveCfg = Release|Any CPU