优化音频读取方式

This commit is contained in:
ALIENJACK\alien 2024-08-06 16:03:17 +08:00
parent 66f0cc5bd7
commit aad4c1775a
6 changed files with 41 additions and 26 deletions

View File

@ -323,6 +323,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4232056521131536012, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: RomName
value: tortoise4.nes
objectReference: {fileID: 0}
- target: {fileID: 4232056521131536013, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3} - target: {fileID: 4232056521131536013, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: m_Name propertyPath: m_Name
value: NesEmulator value: NesEmulator

View File

@ -1,6 +1,4 @@
using System; using UnityEngine;
using System.Diagnostics;
using UnityEngine;
using VirtualNes.Core; using VirtualNes.Core;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
@ -8,12 +6,13 @@ namespace AxibugEmuOnline.Client
public class AudioProvider : MonoBehaviour public class AudioProvider : MonoBehaviour
{ {
public NesEmulator NesEmu;
[SerializeField] [SerializeField]
private AudioSource m_as; private AudioSource m_as;
private SoundBuffer _buffer = new SoundBuffer(4096); private SoundBuffer _buffer = new SoundBuffer(4096);
public void Start()
public void Initialize()
{ {
var dummy = AudioClip.Create("dummy", 1, 1, AudioSettings.outputSampleRate, false); var dummy = AudioClip.Create("dummy", 1, 1, AudioSettings.outputSampleRate, false);
@ -28,7 +27,9 @@ namespace AxibugEmuOnline.Client
{ {
int step = channels; int step = channels;
var bufferCount = _buffer.Available(); if (NesEmu == null || NesEmu.NesCore == null) return;
ProcessSound(NesEmu.NesCore, (uint)(data.Length / channels));
for (int i = 0; i < data.Length; i += step) for (int i = 0; i < data.Length; i += step)
{ {
@ -43,9 +44,9 @@ namespace AxibugEmuOnline.Client
} }
} }
public void ProcessSound(NES nes) void ProcessSound(NES nes, uint feedCount)
{ {
nes.apu.Process(_buffer, (uint)(Supporter.Config.sound.nRate * Time.deltaTime)); nes.apu.Process(_buffer, feedCount);
} }
} }

View File

@ -9,15 +9,24 @@ namespace AxibugEmuOnline.Client
{ {
public class NesEmulator : MonoBehaviour public class NesEmulator : MonoBehaviour
{ {
private NES m_nesIns; public NES NesCore { get; private set; }
public VideoProvider VideoProvider; public VideoProvider VideoProvider;
public AudioProvider AudioProvider; public AudioProvider AudioProvider;
#if UNITY_EDITOR
public string RomName;
#endif
private void Start() private void Start()
{ {
Application.targetFrameRate = 60; Application.targetFrameRate = 60;
StartGame("ff1.nes"); VideoProvider.NesEmu = this;
AudioProvider.NesEmu = this;
#if UNITY_EDITOR
StartGame(RomName);
#endif
} }
public void StartGame(string romName) public void StartGame(string romName)
@ -29,32 +38,30 @@ namespace AxibugEmuOnline.Client
try try
{ {
m_nesIns = new NES(romName); NesCore = new NES(romName);
} }
catch (Exception ex) catch (Exception ex)
{ {
m_nesIns = null; NesCore = null;
Debug.LogError(ex); Debug.LogError(ex);
} }
} }
public void StopGame() public void StopGame()
{ {
m_nesIns?.Dispose(); NesCore?.Dispose();
m_nesIns = null; NesCore = null;
} }
private void Update() private void Update()
{ {
if (m_nesIns != null) if (NesCore != null)
{ {
m_nesIns.EmulateFrame(true); NesCore.EmulateFrame(true);
var screenBuffer = m_nesIns.ppu.GetScreenPtr(); var screenBuffer = NesCore.ppu.GetScreenPtr();
var lineColorMode = m_nesIns.ppu.GetLineColorMode(); var lineColorMode = NesCore.ppu.GetLineColorMode();
VideoProvider.SetDrawData(screenBuffer, lineColorMode, 256, 240); VideoProvider.SetDrawData(screenBuffer, lineColorMode, 256, 240);
AudioProvider.ProcessSound(m_nesIns);
} }
} }

View File

@ -10,7 +10,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 4785916497946256520} - component: {fileID: 4785916497946256520}
- component: {fileID: 9003897287163669553} - component: {fileID: 9003897287163669553}
- component: {fileID: 7558824780418593440} - component: {fileID: 8726979175317618791}
m_Layer: 0 m_Layer: 0
m_Name: AudioProvider m_Name: AudioProvider
m_TagString: Untagged m_TagString: Untagged
@ -44,8 +44,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a6a09b6a4cf4c2d4f994a13fd7e89d6f, type: 3} m_Script: {fileID: 11500000, guid: a6a09b6a4cf4c2d4f994a13fd7e89d6f, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
m_as: {fileID: 7558824780418593440} m_as: {fileID: 8726979175317618791}
--- !u!82 &7558824780418593440 --- !u!82 &8726979175317618791
AudioSource: AudioSource:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
@ -158,7 +158,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!224 &1038087993597378172 --- !u!224 &1038087993597378172
RectTransform: RectTransform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -407,6 +407,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
VideoProvider: {fileID: 4232056520112715744} VideoProvider: {fileID: 4232056520112715744}
AudioProvider: {fileID: 9003897287163669553} AudioProvider: {fileID: 9003897287163669553}
RomName:
--- !u!1 &4232056521759880276 --- !u!1 &4232056521759880276
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -495,7 +496,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &393435831810118449 --- !u!4 &393435831810118449
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -7,6 +7,8 @@ namespace AxibugEmuOnline.Client
{ {
public class VideoProvider : MonoBehaviour public class VideoProvider : MonoBehaviour
{ {
public NesEmulator NesEmu;
public RawImage Image; public RawImage Image;
private UInt32[] wrapTexBuffer; private UInt32[] wrapTexBuffer;

View File

@ -82,7 +82,7 @@ PlayerSettings:
androidFullscreenMode: 1 androidFullscreenMode: 1
defaultIsNativeResolution: 1 defaultIsNativeResolution: 1
macRetinaSupport: 1 macRetinaSupport: 1
runInBackground: 0 runInBackground: 1
captureSingleScreen: 0 captureSingleScreen: 0
muteOtherAudioSources: 0 muteOtherAudioSources: 0
Prepare IOS For Recording: 0 Prepare IOS For Recording: 0