Fix instruments for engine changes, fixes program change bug. (#7365)
This commit is contained in:
committed by
GitHub
parent
a4d55235cc
commit
6e73e94cc6
@@ -1,11 +1,6 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Content.Shared.Instruments;
|
using Content.Shared.Instruments;
|
||||||
using Robust.Client.Audio.Midi;
|
using Robust.Client.Audio.Midi;
|
||||||
using Robust.Shared.Audio.Midi;
|
using Robust.Shared.Audio.Midi;
|
||||||
using Robust.Shared.GameObjects;
|
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
|
||||||
using Robust.Shared.ViewVariables;
|
|
||||||
|
|
||||||
namespace Content.Client.Instruments;
|
namespace Content.Client.Instruments;
|
||||||
|
|
||||||
@@ -14,21 +9,26 @@ public sealed class InstrumentComponent : SharedInstrumentComponent
|
|||||||
{
|
{
|
||||||
public event Action? OnMidiPlaybackEnded;
|
public event Action? OnMidiPlaybackEnded;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
public IMidiRenderer? Renderer;
|
public IMidiRenderer? Renderer;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
public uint SequenceDelay;
|
public uint SequenceDelay;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
public uint SequenceStartTick;
|
public uint SequenceStartTick;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
public TimeSpan LastMeasured = TimeSpan.MinValue;
|
public TimeSpan LastMeasured = TimeSpan.MinValue;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
public int SentWithinASec;
|
public int SentWithinASec;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A queue of MidiEvents to be sent to the server.
|
/// A queue of MidiEvents to be sent to the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public readonly List<MidiEvent> MidiEventBuffer = new();
|
public readonly List<RobustMidiEvent> MidiEventBuffer = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether a midi song will loop or not.
|
/// Whether a midi song will loop or not.
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.Metrics;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Content.Shared.CCVar;
|
using Content.Shared.CCVar;
|
||||||
using Content.Shared.Instruments;
|
using Content.Shared.Instruments;
|
||||||
@@ -9,372 +6,367 @@ using JetBrains.Annotations;
|
|||||||
using Robust.Client.Audio.Midi;
|
using Robust.Client.Audio.Midi;
|
||||||
using Robust.Shared.Audio.Midi;
|
using Robust.Shared.Audio.Midi;
|
||||||
using Robust.Shared.Configuration;
|
using Robust.Shared.Configuration;
|
||||||
using Robust.Shared.GameObjects;
|
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Network;
|
using Robust.Shared.Network;
|
||||||
using Robust.Shared.Timing;
|
using Robust.Shared.Timing;
|
||||||
using SharpFont;
|
|
||||||
|
|
||||||
namespace Content.Client.Instruments
|
namespace Content.Client.Instruments;
|
||||||
|
|
||||||
|
[UsedImplicitly]
|
||||||
|
public sealed class InstrumentSystem : SharedInstrumentSystem
|
||||||
{
|
{
|
||||||
[UsedImplicitly]
|
[Dependency] private readonly IClientNetManager _netManager = default!;
|
||||||
public sealed class InstrumentSystem : SharedInstrumentSystem
|
[Dependency] private readonly IMidiManager _midiManager = default!;
|
||||||
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||||
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||||
|
|
||||||
|
public readonly TimeSpan OneSecAgo = TimeSpan.FromSeconds(-1);
|
||||||
|
public int MaxMidiEventsPerBatch { get; private set; }
|
||||||
|
public int MaxMidiEventsPerSecond { get; private set; }
|
||||||
|
|
||||||
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IClientNetManager _netManager = default!;
|
base.Initialize();
|
||||||
[Dependency] private readonly IMidiManager _midiManager = default!;
|
|
||||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
||||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
|
||||||
|
|
||||||
public readonly TimeSpan OneSecAgo = TimeSpan.FromSeconds(-1);
|
UpdatesOutsidePrediction = true;
|
||||||
|
|
||||||
public readonly Comparer<MidiEvent> SortMidiEventTick
|
_cfg.OnValueChanged(CCVars.MaxMidiEventsPerBatch, OnMaxMidiEventsPerBatchChanged, true);
|
||||||
= Comparer<MidiEvent>.Create((x, y)
|
_cfg.OnValueChanged(CCVars.MaxMidiEventsPerSecond, OnMaxMidiEventsPerSecondChanged, true);
|
||||||
=> x.Tick.CompareTo(y.Tick));
|
|
||||||
|
|
||||||
public int MaxMidiEventsPerBatch { get; private set; }
|
SubscribeNetworkEvent<InstrumentMidiEventEvent>(OnMidiEventRx);
|
||||||
public int MaxMidiEventsPerSecond { get; private set; }
|
SubscribeNetworkEvent<InstrumentStartMidiEvent>(OnMidiStart);
|
||||||
|
SubscribeNetworkEvent<InstrumentStopMidiEvent>(OnMidiStop);
|
||||||
|
|
||||||
public override void Initialize()
|
SubscribeLocalEvent<InstrumentComponent, ComponentShutdown>(OnShutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Shutdown()
|
||||||
|
{
|
||||||
|
base.Shutdown();
|
||||||
|
|
||||||
|
_cfg.UnsubValueChanged(CCVars.MaxMidiEventsPerBatch, OnMaxMidiEventsPerBatchChanged);
|
||||||
|
_cfg.UnsubValueChanged(CCVars.MaxMidiEventsPerSecond, OnMaxMidiEventsPerSecondChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnShutdown(EntityUid uid, InstrumentComponent component, ComponentShutdown args)
|
||||||
|
{
|
||||||
|
EndRenderer(uid, false, component);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref component))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (component is not InstrumentComponent instrument || instrument.IsRendererAlive)
|
||||||
|
return;
|
||||||
|
|
||||||
|
instrument.SequenceDelay = 0;
|
||||||
|
instrument.SequenceStartTick = 0;
|
||||||
|
_midiManager.OcclusionCollisionMask = (int) CollisionGroup.Impassable;
|
||||||
|
instrument.Renderer = _midiManager.GetNewRenderer();
|
||||||
|
|
||||||
|
if (instrument.Renderer != null)
|
||||||
{
|
{
|
||||||
base.Initialize();
|
instrument.Renderer.SendMidiEvent(RobustMidiEvent.SystemReset(instrument.Renderer.SequencerTick));
|
||||||
|
UpdateRenderer(uid, instrument);
|
||||||
UpdatesOutsidePrediction = true;
|
instrument.Renderer.OnMidiPlayerFinished += () =>
|
||||||
|
|
||||||
_cfg.OnValueChanged(CCVars.MaxMidiEventsPerBatch, OnMaxMidiEventsPerBatchChanged, true);
|
|
||||||
_cfg.OnValueChanged(CCVars.MaxMidiEventsPerSecond, OnMaxMidiEventsPerSecondChanged, true);
|
|
||||||
|
|
||||||
SubscribeNetworkEvent<InstrumentMidiEventEvent>(OnMidiEventRx);
|
|
||||||
SubscribeNetworkEvent<InstrumentStartMidiEvent>(OnMidiStart);
|
|
||||||
SubscribeNetworkEvent<InstrumentStopMidiEvent>(OnMidiStop);
|
|
||||||
|
|
||||||
SubscribeLocalEvent<InstrumentComponent, ComponentShutdown>(OnShutdown);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Shutdown()
|
|
||||||
{
|
|
||||||
base.Shutdown();
|
|
||||||
|
|
||||||
_cfg.UnsubValueChanged(CCVars.MaxMidiEventsPerBatch, OnMaxMidiEventsPerBatchChanged);
|
|
||||||
_cfg.UnsubValueChanged(CCVars.MaxMidiEventsPerSecond, OnMaxMidiEventsPerSecondChanged);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnShutdown(EntityUid uid, InstrumentComponent component, ComponentShutdown args)
|
|
||||||
{
|
|
||||||
EndRenderer(uid, false, component);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void SetupRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
|
|
||||||
{
|
|
||||||
if (!Resolve(uid, ref component))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (component is not InstrumentComponent instrument || instrument.IsRendererAlive)
|
|
||||||
return;
|
|
||||||
|
|
||||||
instrument.SequenceDelay = 0;
|
|
||||||
instrument.SequenceStartTick = 0;
|
|
||||||
_midiManager.OcclusionCollisionMask = (int) CollisionGroup.Impassable;
|
|
||||||
instrument.Renderer = _midiManager.GetNewRenderer();
|
|
||||||
|
|
||||||
if (instrument.Renderer != null)
|
|
||||||
{
|
{
|
||||||
UpdateRenderer(uid, instrument);
|
instrument.PlaybackEndedInvoke();
|
||||||
instrument.Renderer.OnMidiPlayerFinished += () =>
|
EndRenderer(uid, fromStateChange, instrument);
|
||||||
{
|
};
|
||||||
instrument.PlaybackEndedInvoke();
|
|
||||||
EndRenderer(uid, fromStateChange, instrument);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fromStateChange)
|
|
||||||
{
|
|
||||||
RaiseNetworkEvent(new InstrumentStartMidiEvent(uid));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateRenderer(EntityUid uid, InstrumentComponent? instrument = null)
|
if (!fromStateChange)
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref instrument) || instrument.Renderer == null)
|
RaiseNetworkEvent(new InstrumentStartMidiEvent(uid));
|
||||||
return;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
instrument.Renderer.MidiBank = instrument.InstrumentBank;
|
public void UpdateRenderer(EntityUid uid, InstrumentComponent? instrument = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref instrument) || instrument.Renderer == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
instrument.Renderer.TrackingEntity = instrument.Owner;
|
||||||
|
instrument.Renderer.DisablePercussionChannel = !instrument.AllowPercussion;
|
||||||
|
instrument.Renderer.DisableProgramChangeEvent = !instrument.AllowProgramChange;
|
||||||
|
|
||||||
|
if (!instrument.AllowProgramChange)
|
||||||
|
{
|
||||||
instrument.Renderer.MidiProgram = instrument.InstrumentProgram;
|
instrument.Renderer.MidiProgram = instrument.InstrumentProgram;
|
||||||
instrument.Renderer.TrackingEntity = instrument.Owner;
|
instrument.Renderer.MidiBank = instrument.InstrumentBank;
|
||||||
instrument.Renderer.DisablePercussionChannel = !instrument.AllowPercussion;
|
|
||||||
instrument.Renderer.DisableProgramChangeEvent = !instrument.AllowProgramChange;
|
|
||||||
instrument.Renderer.LoopMidi = instrument.LoopMidi;
|
|
||||||
instrument.DirtyRenderer = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void EndRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
|
instrument.Renderer.LoopMidi = instrument.LoopMidi;
|
||||||
|
instrument.DirtyRenderer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void EndRenderer(EntityUid uid, bool fromStateChange, SharedInstrumentComponent? component = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref component, false))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (component is not InstrumentComponent instrument)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (instrument.IsInputOpen)
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref component, false))
|
CloseInput(uid, fromStateChange, instrument);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (component is not InstrumentComponent instrument)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (instrument.IsInputOpen)
|
|
||||||
{
|
|
||||||
CloseInput(uid, fromStateChange, instrument);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (instrument.IsMidiOpen)
|
|
||||||
{
|
|
||||||
CloseMidi(uid, fromStateChange, instrument);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
instrument.Renderer?.StopAllNotes();
|
|
||||||
|
|
||||||
var renderer = instrument.Renderer;
|
|
||||||
|
|
||||||
// We dispose of the synth two seconds from now to allow the last notes to stop from playing.
|
|
||||||
// Don't use timers bound to the entity in case it is getting deleted.
|
|
||||||
Timer.Spawn(2000, () => { renderer?.Dispose(); });
|
|
||||||
instrument.Renderer = null;
|
|
||||||
instrument.MidiEventBuffer.Clear();
|
|
||||||
|
|
||||||
if (!fromStateChange && _netManager.IsConnected)
|
|
||||||
{
|
|
||||||
RaiseNetworkEvent(new InstrumentStopMidiEvent(uid));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPlayerTick(EntityUid uid, int playerTick, InstrumentComponent? instrument = null)
|
if (instrument.IsMidiOpen)
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref instrument))
|
CloseMidi(uid, fromStateChange, instrument);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (instrument.Renderer == null || instrument.Renderer.Status != MidiRendererStatus.File)
|
|
||||||
return;
|
|
||||||
|
|
||||||
instrument.MidiEventBuffer.Clear();
|
|
||||||
|
|
||||||
instrument.Renderer.PlayerTick = playerTick;
|
|
||||||
var tick = instrument.Renderer.SequencerTick;
|
|
||||||
|
|
||||||
// We add a "all notes off" message.
|
|
||||||
for (byte i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
instrument.MidiEventBuffer.Add(new MidiEvent()
|
|
||||||
{
|
|
||||||
Tick = tick, Type = 176,
|
|
||||||
Control = 123, Velocity = 0, Channel = i,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now we add a Reset All Controllers message.
|
|
||||||
instrument.MidiEventBuffer.Add(new MidiEvent()
|
|
||||||
{
|
|
||||||
Tick = tick, Type = 176,
|
|
||||||
Control = 121, Value = 0,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool OpenInput(EntityUid uid, InstrumentComponent? instrument = null)
|
instrument.Renderer?.StopAllNotes();
|
||||||
|
|
||||||
|
var renderer = instrument.Renderer;
|
||||||
|
|
||||||
|
// We dispose of the synth two seconds from now to allow the last notes to stop from playing.
|
||||||
|
// Don't use timers bound to the entity in case it is getting deleted.
|
||||||
|
Timer.Spawn(2000, () => { renderer?.Dispose(); });
|
||||||
|
instrument.Renderer = null;
|
||||||
|
instrument.MidiEventBuffer.Clear();
|
||||||
|
|
||||||
|
if (!fromStateChange && _netManager.IsConnected)
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref instrument, false))
|
RaiseNetworkEvent(new InstrumentStopMidiEvent(uid));
|
||||||
return false;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SetupRenderer(uid, false, instrument);
|
public void SetPlayerTick(EntityUid uid, int playerTick, InstrumentComponent? instrument = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref instrument))
|
||||||
|
return;
|
||||||
|
|
||||||
if (instrument.Renderer != null && instrument.Renderer.OpenInput())
|
if (instrument.Renderer is not { Status: MidiRendererStatus.File })
|
||||||
{
|
return;
|
||||||
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
instrument.MidiEventBuffer.Clear();
|
||||||
|
|
||||||
|
var tick = instrument.Renderer.SequencerTick-1;
|
||||||
|
|
||||||
|
instrument.MidiEventBuffer.Add(RobustMidiEvent.SystemReset(tick));
|
||||||
|
|
||||||
|
// We add a "all notes off" message.
|
||||||
|
for (byte i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
//instrument.MidiEventBuffer.Add(RobustMidiEvent.AllNotesOff(i, tick));
|
||||||
|
}
|
||||||
|
|
||||||
|
instrument.Renderer.PlayerTick = playerTick;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool OpenInput(EntityUid uid, InstrumentComponent? instrument = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref instrument, false))
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
public bool OpenMidi(EntityUid uid, ReadOnlySpan<byte> data, InstrumentComponent? instrument = null)
|
SetupRenderer(uid, false, instrument);
|
||||||
|
|
||||||
|
if (instrument.Renderer != null && instrument.Renderer.OpenInput())
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref instrument))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
SetupRenderer(uid, false, instrument);
|
|
||||||
|
|
||||||
if (instrument.Renderer == null || !instrument.Renderer.OpenMidi(data))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
|
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CloseInput(EntityUid uid, bool fromStateChange, InstrumentComponent? instrument = null)
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool OpenMidi(EntityUid uid, ReadOnlySpan<byte> data, InstrumentComponent? instrument = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref instrument))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
SetupRenderer(uid, false, instrument);
|
||||||
|
|
||||||
|
if (instrument.Renderer == null || !instrument.Renderer.OpenMidi(data))
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref instrument))
|
return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
if (instrument.Renderer == null || !instrument.Renderer.CloseInput())
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
EndRenderer(uid, fromStateChange, instrument);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CloseMidi(EntityUid uid, bool fromStateChange, InstrumentComponent? instrument = null)
|
instrument.MidiEventBuffer.Clear();
|
||||||
|
|
||||||
|
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CloseInput(EntityUid uid, bool fromStateChange, InstrumentComponent? instrument = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref instrument))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (instrument.Renderer == null || !instrument.Renderer.CloseInput())
|
||||||
{
|
{
|
||||||
if (!Resolve(uid, ref instrument))
|
return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
if (instrument.Renderer == null || !instrument.Renderer.CloseMidi())
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
EndRenderer(uid, fromStateChange, instrument);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMaxMidiEventsPerSecondChanged(int obj)
|
EndRenderer(uid, fromStateChange, instrument);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CloseMidi(EntityUid uid, bool fromStateChange, InstrumentComponent? instrument = null)
|
||||||
|
{
|
||||||
|
if (!Resolve(uid, ref instrument))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (instrument.Renderer == null || !instrument.Renderer.CloseMidi())
|
||||||
{
|
{
|
||||||
MaxMidiEventsPerSecond = obj;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMaxMidiEventsPerBatchChanged(int obj)
|
EndRenderer(uid, fromStateChange, instrument);
|
||||||
{
|
return true;
|
||||||
MaxMidiEventsPerBatch = obj;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void OnMidiEventRx(InstrumentMidiEventEvent midiEv)
|
private void OnMaxMidiEventsPerSecondChanged(int obj)
|
||||||
{
|
{
|
||||||
var uid = midiEv.Uid;
|
MaxMidiEventsPerSecond = obj;
|
||||||
|
}
|
||||||
|
|
||||||
if (!EntityManager.TryGetComponent(uid, out InstrumentComponent? instrument))
|
private void OnMaxMidiEventsPerBatchChanged(int obj)
|
||||||
|
{
|
||||||
|
MaxMidiEventsPerBatch = obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMidiEventRx(InstrumentMidiEventEvent midiEv)
|
||||||
|
{
|
||||||
|
var uid = midiEv.Uid;
|
||||||
|
|
||||||
|
if (!TryComp(uid, out InstrumentComponent? instrument))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var renderer = instrument.Renderer;
|
||||||
|
|
||||||
|
if (renderer != null)
|
||||||
|
{
|
||||||
|
// If we're the ones sending the MidiEvents, we ignore this message.
|
||||||
|
if (instrument.IsInputOpen || instrument.IsMidiOpen)
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
var renderer = instrument.Renderer;
|
else
|
||||||
|
{
|
||||||
if (renderer != null)
|
// if we haven't started or finished some sequence
|
||||||
|
if (instrument.SequenceStartTick == 0)
|
||||||
{
|
{
|
||||||
// If we're the ones sending the MidiEvents, we ignore this message.
|
// we may have arrived late
|
||||||
if (instrument.IsInputOpen || instrument.IsMidiOpen)
|
SetupRenderer(uid, true, instrument);
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// if we haven't started or finished some sequence
|
|
||||||
if (instrument.SequenceStartTick == 0)
|
|
||||||
{
|
|
||||||
// we may have arrived late
|
|
||||||
SetupRenderer(uid, true, instrument);
|
|
||||||
}
|
|
||||||
|
|
||||||
// might be our own notes after we already finished playing
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instrument.SequenceStartTick <= 0)
|
// might be our own notes after we already finished playing
|
||||||
{
|
return;
|
||||||
instrument.SequenceStartTick = midiEv.MidiEvent.Min(x => x.Tick) - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sqrtLag = MathF.Sqrt(_netManager.ServerChannel!.Ping / 1000f);
|
|
||||||
var delay = (uint) (renderer.SequencerTimeScale * (.2 + sqrtLag));
|
|
||||||
var delta = delay - instrument.SequenceStartTick;
|
|
||||||
|
|
||||||
instrument.SequenceDelay = Math.Max(instrument.SequenceDelay, delta);
|
|
||||||
|
|
||||||
var currentTick = renderer.SequencerTick;
|
|
||||||
|
|
||||||
// ReSharper disable once ForCanBeConvertedToForeach
|
|
||||||
for (var i = 0; i < midiEv.MidiEvent.Length; i++)
|
|
||||||
{
|
|
||||||
var ev = midiEv.MidiEvent[i];
|
|
||||||
var scheduled = ev.Tick + instrument.SequenceDelay;
|
|
||||||
|
|
||||||
if (scheduled <= currentTick)
|
|
||||||
{
|
|
||||||
instrument.SequenceDelay += currentTick - ev.Tick;
|
|
||||||
scheduled = ev.Tick + instrument.SequenceDelay;
|
|
||||||
}
|
|
||||||
|
|
||||||
instrument.Renderer?.ScheduleMidiEvent(ev, scheduled, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMidiStart(InstrumentStartMidiEvent ev)
|
if (instrument.SequenceStartTick <= 0)
|
||||||
{
|
{
|
||||||
SetupRenderer(ev.Uid, true);
|
instrument.SequenceStartTick = midiEv.MidiEvent.Min(x => x.Tick) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMidiStop(InstrumentStopMidiEvent ev)
|
var sqrtLag = MathF.Sqrt(_netManager.ServerChannel!.Ping / 1000f);
|
||||||
|
var delay = (uint) (renderer.SequencerTimeScale * (.2 + sqrtLag));
|
||||||
|
var delta = delay - instrument.SequenceStartTick;
|
||||||
|
|
||||||
|
instrument.SequenceDelay = Math.Max(instrument.SequenceDelay, delta);
|
||||||
|
|
||||||
|
var currentTick = renderer.SequencerTick;
|
||||||
|
|
||||||
|
// ReSharper disable once ForCanBeConvertedToForeach
|
||||||
|
for (uint i = 0; i < midiEv.MidiEvent.Length; i++)
|
||||||
{
|
{
|
||||||
EndRenderer(ev.Uid, true);
|
var ev = midiEv.MidiEvent[i];
|
||||||
|
|
||||||
|
var scheduled = ev.Tick + instrument.SequenceDelay;
|
||||||
|
|
||||||
|
if (scheduled < currentTick)
|
||||||
|
{
|
||||||
|
instrument.SequenceDelay += currentTick - ev.Tick;
|
||||||
|
scheduled = ev.Tick + instrument.SequenceDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The order of events with the same timestamp is undefined in Fluidsynth's sequencer...
|
||||||
|
// Therefore we add the event index to the scheduled time to ensure every event has an unique timestamp.
|
||||||
|
instrument.Renderer?.ScheduleMidiEvent(ev, scheduled+i, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMidiStart(InstrumentStartMidiEvent ev)
|
||||||
|
{
|
||||||
|
SetupRenderer(ev.Uid, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMidiStop(InstrumentStopMidiEvent ev)
|
||||||
|
{
|
||||||
|
EndRenderer(ev.Uid, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(float frameTime)
|
||||||
|
{
|
||||||
|
base.Update(frameTime);
|
||||||
|
|
||||||
|
if (!_gameTiming.IsFirstTimePredicted)
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Update(float frameTime)
|
foreach (var instrument in EntityManager.EntityQuery<InstrumentComponent>(true))
|
||||||
{
|
{
|
||||||
base.Update(frameTime);
|
if (instrument.DirtyRenderer && instrument.Renderer != null)
|
||||||
|
UpdateRenderer(instrument.Owner, instrument);
|
||||||
|
|
||||||
if (!_gameTiming.IsFirstTimePredicted)
|
if (!instrument.IsMidiOpen && !instrument.IsInputOpen)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var now = _gameTiming.RealTime;
|
||||||
|
var oneSecAGo = now.Add(OneSecAgo);
|
||||||
|
|
||||||
|
if (instrument.LastMeasured <= oneSecAGo)
|
||||||
{
|
{
|
||||||
return;
|
instrument.LastMeasured = now;
|
||||||
|
instrument.SentWithinASec = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var instrument in EntityManager.EntityQuery<InstrumentComponent>(true))
|
if (instrument.MidiEventBuffer.Count == 0) continue;
|
||||||
|
|
||||||
|
var max = instrument.RespectMidiLimits ?
|
||||||
|
Math.Min(MaxMidiEventsPerBatch, MaxMidiEventsPerSecond - instrument.SentWithinASec)
|
||||||
|
: instrument.MidiEventBuffer.Count;
|
||||||
|
|
||||||
|
if (max <= 0)
|
||||||
{
|
{
|
||||||
if (instrument.DirtyRenderer && instrument.Renderer != null)
|
// hit event/sec limit, have to lag the batch or drop events
|
||||||
UpdateRenderer(instrument.Owner, instrument);
|
continue;
|
||||||
|
|
||||||
if (!instrument.IsMidiOpen && !instrument.IsInputOpen)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var now = _gameTiming.RealTime;
|
|
||||||
var oneSecAGo = now.Add(OneSecAgo);
|
|
||||||
|
|
||||||
if (instrument.LastMeasured <= oneSecAGo)
|
|
||||||
{
|
|
||||||
instrument.LastMeasured = now;
|
|
||||||
instrument.SentWithinASec = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (instrument.MidiEventBuffer.Count == 0) continue;
|
|
||||||
|
|
||||||
var max = instrument.RespectMidiLimits ?
|
|
||||||
Math.Min(MaxMidiEventsPerBatch, MaxMidiEventsPerSecond - instrument.SentWithinASec)
|
|
||||||
: instrument.MidiEventBuffer.Count;
|
|
||||||
|
|
||||||
if (max <= 0)
|
|
||||||
{
|
|
||||||
// hit event/sec limit, have to lag the batch or drop events
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// fix cross-fade events generating retroactive events
|
|
||||||
// also handle any significant backlog of events after midi finished
|
|
||||||
|
|
||||||
instrument.MidiEventBuffer.Sort(SortMidiEventTick);
|
|
||||||
var bufferTicks = instrument.IsRendererAlive && instrument.Renderer!.Status != MidiRendererStatus.None
|
|
||||||
? instrument.Renderer.SequencerTimeScale * .2f
|
|
||||||
: 0;
|
|
||||||
var bufferedTick = instrument.IsRendererAlive
|
|
||||||
? instrument.Renderer!.SequencerTick - bufferTicks
|
|
||||||
: int.MaxValue;
|
|
||||||
|
|
||||||
var events = instrument.MidiEventBuffer
|
|
||||||
.TakeWhile(x => x.Tick < bufferedTick)
|
|
||||||
.Take(max)
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
var eventCount = events.Length;
|
|
||||||
|
|
||||||
if (eventCount == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
RaiseNetworkEvent(new InstrumentMidiEventEvent(instrument.Owner, events));
|
|
||||||
|
|
||||||
instrument.SentWithinASec += eventCount;
|
|
||||||
|
|
||||||
instrument.MidiEventBuffer.RemoveRange(0, eventCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fix cross-fade events generating retroactive events
|
||||||
|
// also handle any significant backlog of events after midi finished
|
||||||
|
|
||||||
|
var bufferTicks = instrument.IsRendererAlive && instrument.Renderer!.Status != MidiRendererStatus.None
|
||||||
|
? instrument.Renderer.SequencerTimeScale * .2f
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
var bufferedTick = instrument.IsRendererAlive
|
||||||
|
? instrument.Renderer!.SequencerTick - bufferTicks
|
||||||
|
: int.MaxValue;
|
||||||
|
|
||||||
|
// TODO: Remove LINQ brain-rot.
|
||||||
|
var events = instrument.MidiEventBuffer
|
||||||
|
.TakeWhile(x => x.Tick < bufferedTick)
|
||||||
|
.Take(max)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
var eventCount = events.Length;
|
||||||
|
|
||||||
|
if (eventCount == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
RaiseNetworkEvent(new InstrumentMidiEventEvent(instrument.Owner, events));
|
||||||
|
|
||||||
|
instrument.SentWithinASec += eventCount;
|
||||||
|
|
||||||
|
instrument.MidiEventBuffer.RemoveRange(0, eventCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ public sealed class InstrumentComponent : SharedInstrumentComponent
|
|||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public int MidiEventCount = 0;
|
public int MidiEventCount = 0;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
|
public uint LastSequencerTick = 0;
|
||||||
|
|
||||||
// TODO Instruments: Make this ECS
|
// TODO Instruments: Make this ECS
|
||||||
public IPlayerSession? InstrumentPlayer =>
|
public IPlayerSession? InstrumentPlayer =>
|
||||||
_entMan.GetComponentOrNull<ActivatableUIComponent>(Owner)?.CurrentSingleUser
|
_entMan.GetComponentOrNull<ActivatableUIComponent>(Owner)?.CurrentSingleUser
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
|
|||||||
{
|
{
|
||||||
if (args.UiKey is not InstrumentUiKey)
|
if (args.UiKey is not InstrumentUiKey)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
EnsureComp<ActiveInstrumentComponent>(uid);
|
EnsureComp<ActiveInstrumentComponent>(uid);
|
||||||
Clean(uid, component);
|
Clean(uid, component);
|
||||||
}
|
}
|
||||||
@@ -110,7 +110,7 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
|
|||||||
{
|
{
|
||||||
var uid = msg.Uid;
|
var uid = msg.Uid;
|
||||||
|
|
||||||
if (!EntityManager.TryGetComponent(uid, out InstrumentComponent? instrument))
|
if (!TryComp(uid, out InstrumentComponent? instrument))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!instrument.Playing
|
if (!instrument.Playing
|
||||||
@@ -121,7 +121,20 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
|
|||||||
|
|
||||||
var send = true;
|
var send = true;
|
||||||
|
|
||||||
var minTick = msg.MidiEvent.Min(x => x.Tick);
|
var minTick = uint.MaxValue;
|
||||||
|
var maxTick = uint.MinValue;
|
||||||
|
|
||||||
|
for (var i = 0; i < msg.MidiEvent.Length; i++)
|
||||||
|
{
|
||||||
|
var tick = msg.MidiEvent[i].Tick;
|
||||||
|
|
||||||
|
if (tick < minTick)
|
||||||
|
minTick = tick;
|
||||||
|
|
||||||
|
if (tick > maxTick)
|
||||||
|
maxTick = tick;
|
||||||
|
}
|
||||||
|
|
||||||
if (instrument.LastSequencerTick > minTick)
|
if (instrument.LastSequencerTick > minTick)
|
||||||
{
|
{
|
||||||
instrument.LaggedBatches++;
|
instrument.LaggedBatches++;
|
||||||
@@ -158,7 +171,6 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
|
|||||||
RaiseNetworkEvent(msg);
|
RaiseNetworkEvent(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxTick = msg.MidiEvent.Max(x => x.Tick);
|
|
||||||
instrument.LastSequencerTick = Math.Max(maxTick, minTick);
|
instrument.LastSequencerTick = Math.Max(maxTick, minTick);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ public abstract class SharedInstrumentComponent : Component
|
|||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public bool Playing { get; set; }
|
public bool Playing { get; set; }
|
||||||
|
|
||||||
[ViewVariables]
|
|
||||||
public uint LastSequencerTick { get; set; }
|
|
||||||
|
|
||||||
[DataField("program"), ViewVariables(VVAccess.ReadWrite)]
|
[DataField("program"), ViewVariables(VVAccess.ReadWrite)]
|
||||||
public byte InstrumentProgram { get; set; }
|
public byte InstrumentProgram { get; set; }
|
||||||
|
|
||||||
@@ -73,9 +70,9 @@ public sealed class InstrumentStartMidiEvent : EntityEventArgs
|
|||||||
public sealed class InstrumentMidiEventEvent : EntityEventArgs
|
public sealed class InstrumentMidiEventEvent : EntityEventArgs
|
||||||
{
|
{
|
||||||
public EntityUid Uid { get; }
|
public EntityUid Uid { get; }
|
||||||
public MidiEvent[] MidiEvent { get; }
|
public RobustMidiEvent[] MidiEvent { get; }
|
||||||
|
|
||||||
public InstrumentMidiEventEvent(EntityUid uid, MidiEvent[] midiEvent)
|
public InstrumentMidiEventEvent(EntityUid uid, RobustMidiEvent[] midiEvent)
|
||||||
{
|
{
|
||||||
Uid = uid;
|
Uid = uid;
|
||||||
MidiEvent = midiEvent;
|
MidiEvent = midiEvent;
|
||||||
@@ -92,7 +89,7 @@ public sealed class InstrumentState : ComponentState
|
|||||||
public bool AllowProgramChange { get; }
|
public bool AllowProgramChange { get; }
|
||||||
public bool RespectMidiLimits { get; }
|
public bool RespectMidiLimits { get; }
|
||||||
|
|
||||||
public InstrumentState(bool playing, byte instrumentProgram, byte instrumentBank, bool allowPercussion, bool allowProgramChange, bool respectMidiLimits, uint sequencerTick = 0)
|
public InstrumentState(bool playing, byte instrumentProgram, byte instrumentBank, bool allowPercussion, bool allowProgramChange, bool respectMidiLimits)
|
||||||
{
|
{
|
||||||
Playing = playing;
|
Playing = playing;
|
||||||
InstrumentProgram = instrumentProgram;
|
InstrumentProgram = instrumentProgram;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public abstract class SharedInstrumentSystem : EntitySystem
|
|||||||
{
|
{
|
||||||
args.State =
|
args.State =
|
||||||
new InstrumentState(instrument.Playing, instrument.InstrumentProgram, instrument.InstrumentBank,
|
new InstrumentState(instrument.Playing, instrument.InstrumentProgram, instrument.InstrumentBank,
|
||||||
instrument.AllowPercussion, instrument.AllowProgramChange, instrument.RespectMidiLimits, instrument.LastSequencerTick);
|
instrument.AllowPercussion, instrument.AllowProgramChange, instrument.RespectMidiLimits);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnHandleState(EntityUid uid, SharedInstrumentComponent instrument, ref ComponentHandleState args)
|
private void OnHandleState(EntityUid uid, SharedInstrumentComponent instrument, ref ComponentHandleState args)
|
||||||
|
|||||||
Submodule RobustToolbox updated: 4bb695121f...7094c29b2e
@@ -283,6 +283,7 @@
|
|||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thermite/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thermite/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thermo/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thermo/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thonk/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thonk/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=threadsafe/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=tickrate/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=tickrate/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trasen/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trasen/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=unanchor/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=unanchor/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
|||||||
Reference in New Issue
Block a user