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,28 +6,20 @@ 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]
|
|
||||||
public sealed class InstrumentSystem : SharedInstrumentSystem
|
|
||||||
{
|
|
||||||
[Dependency] private readonly IClientNetManager _netManager = default!;
|
[Dependency] private readonly IClientNetManager _netManager = default!;
|
||||||
[Dependency] private readonly IMidiManager _midiManager = default!;
|
[Dependency] private readonly IMidiManager _midiManager = default!;
|
||||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||||
|
|
||||||
public readonly TimeSpan OneSecAgo = TimeSpan.FromSeconds(-1);
|
public readonly TimeSpan OneSecAgo = TimeSpan.FromSeconds(-1);
|
||||||
|
|
||||||
public readonly Comparer<MidiEvent> SortMidiEventTick
|
|
||||||
= Comparer<MidiEvent>.Create((x, y)
|
|
||||||
=> x.Tick.CompareTo(y.Tick));
|
|
||||||
|
|
||||||
public int MaxMidiEventsPerBatch { get; private set; }
|
public int MaxMidiEventsPerBatch { get; private set; }
|
||||||
public int MaxMidiEventsPerSecond { get; private set; }
|
public int MaxMidiEventsPerSecond { get; private set; }
|
||||||
|
|
||||||
@@ -78,6 +67,7 @@ namespace Content.Client.Instruments
|
|||||||
|
|
||||||
if (instrument.Renderer != null)
|
if (instrument.Renderer != null)
|
||||||
{
|
{
|
||||||
|
instrument.Renderer.SendMidiEvent(RobustMidiEvent.SystemReset(instrument.Renderer.SequencerTick));
|
||||||
UpdateRenderer(uid, instrument);
|
UpdateRenderer(uid, instrument);
|
||||||
instrument.Renderer.OnMidiPlayerFinished += () =>
|
instrument.Renderer.OnMidiPlayerFinished += () =>
|
||||||
{
|
{
|
||||||
@@ -97,11 +87,16 @@ namespace Content.Client.Instruments
|
|||||||
if (!Resolve(uid, ref instrument) || instrument.Renderer == null)
|
if (!Resolve(uid, ref instrument) || instrument.Renderer == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
instrument.Renderer.MidiBank = instrument.InstrumentBank;
|
|
||||||
instrument.Renderer.MidiProgram = instrument.InstrumentProgram;
|
|
||||||
instrument.Renderer.TrackingEntity = instrument.Owner;
|
instrument.Renderer.TrackingEntity = instrument.Owner;
|
||||||
instrument.Renderer.DisablePercussionChannel = !instrument.AllowPercussion;
|
instrument.Renderer.DisablePercussionChannel = !instrument.AllowPercussion;
|
||||||
instrument.Renderer.DisableProgramChangeEvent = !instrument.AllowProgramChange;
|
instrument.Renderer.DisableProgramChangeEvent = !instrument.AllowProgramChange;
|
||||||
|
|
||||||
|
if (!instrument.AllowProgramChange)
|
||||||
|
{
|
||||||
|
instrument.Renderer.MidiProgram = instrument.InstrumentProgram;
|
||||||
|
instrument.Renderer.MidiBank = instrument.InstrumentBank;
|
||||||
|
}
|
||||||
|
|
||||||
instrument.Renderer.LoopMidi = instrument.LoopMidi;
|
instrument.Renderer.LoopMidi = instrument.LoopMidi;
|
||||||
instrument.DirtyRenderer = false;
|
instrument.DirtyRenderer = false;
|
||||||
}
|
}
|
||||||
@@ -147,30 +142,22 @@ namespace Content.Client.Instruments
|
|||||||
if (!Resolve(uid, ref instrument))
|
if (!Resolve(uid, ref instrument))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (instrument.Renderer == null || instrument.Renderer.Status != MidiRendererStatus.File)
|
if (instrument.Renderer is not { Status: MidiRendererStatus.File })
|
||||||
return;
|
return;
|
||||||
|
|
||||||
instrument.MidiEventBuffer.Clear();
|
instrument.MidiEventBuffer.Clear();
|
||||||
|
|
||||||
instrument.Renderer.PlayerTick = playerTick;
|
var tick = instrument.Renderer.SequencerTick-1;
|
||||||
var tick = instrument.Renderer.SequencerTick;
|
|
||||||
|
instrument.MidiEventBuffer.Add(RobustMidiEvent.SystemReset(tick));
|
||||||
|
|
||||||
// We add a "all notes off" message.
|
// We add a "all notes off" message.
|
||||||
for (byte i = 0; i < 16; i++)
|
for (byte i = 0; i < 16; i++)
|
||||||
{
|
{
|
||||||
instrument.MidiEventBuffer.Add(new MidiEvent()
|
//instrument.MidiEventBuffer.Add(RobustMidiEvent.AllNotesOff(i, tick));
|
||||||
{
|
|
||||||
Tick = tick, Type = 176,
|
|
||||||
Control = 123, Velocity = 0, Channel = i,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we add a Reset All Controllers message.
|
instrument.Renderer.PlayerTick = playerTick;
|
||||||
instrument.MidiEventBuffer.Add(new MidiEvent()
|
|
||||||
{
|
|
||||||
Tick = tick, Type = 176,
|
|
||||||
Control = 121, Value = 0,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool OpenInput(EntityUid uid, InstrumentComponent? instrument = null)
|
public bool OpenInput(EntityUid uid, InstrumentComponent? instrument = null)
|
||||||
@@ -201,6 +188,8 @@ namespace Content.Client.Instruments
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
instrument.MidiEventBuffer.Clear();
|
||||||
|
|
||||||
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
|
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -247,7 +236,7 @@ namespace Content.Client.Instruments
|
|||||||
{
|
{
|
||||||
var uid = midiEv.Uid;
|
var uid = midiEv.Uid;
|
||||||
|
|
||||||
if (!EntityManager.TryGetComponent(uid, out InstrumentComponent? instrument))
|
if (!TryComp(uid, out InstrumentComponent? instrument))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var renderer = instrument.Renderer;
|
var renderer = instrument.Renderer;
|
||||||
@@ -285,18 +274,21 @@ namespace Content.Client.Instruments
|
|||||||
var currentTick = renderer.SequencerTick;
|
var currentTick = renderer.SequencerTick;
|
||||||
|
|
||||||
// ReSharper disable once ForCanBeConvertedToForeach
|
// ReSharper disable once ForCanBeConvertedToForeach
|
||||||
for (var i = 0; i < midiEv.MidiEvent.Length; i++)
|
for (uint i = 0; i < midiEv.MidiEvent.Length; i++)
|
||||||
{
|
{
|
||||||
var ev = midiEv.MidiEvent[i];
|
var ev = midiEv.MidiEvent[i];
|
||||||
|
|
||||||
var scheduled = ev.Tick + instrument.SequenceDelay;
|
var scheduled = ev.Tick + instrument.SequenceDelay;
|
||||||
|
|
||||||
if (scheduled <= currentTick)
|
if (scheduled < currentTick)
|
||||||
{
|
{
|
||||||
instrument.SequenceDelay += currentTick - ev.Tick;
|
instrument.SequenceDelay += currentTick - ev.Tick;
|
||||||
scheduled = ev.Tick + instrument.SequenceDelay;
|
scheduled = ev.Tick + instrument.SequenceDelay;
|
||||||
}
|
}
|
||||||
|
|
||||||
instrument.Renderer?.ScheduleMidiEvent(ev, scheduled, true);
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -351,14 +343,15 @@ namespace Content.Client.Instruments
|
|||||||
// fix cross-fade events generating retroactive events
|
// fix cross-fade events generating retroactive events
|
||||||
// also handle any significant backlog of events after midi finished
|
// also handle any significant backlog of events after midi finished
|
||||||
|
|
||||||
instrument.MidiEventBuffer.Sort(SortMidiEventTick);
|
|
||||||
var bufferTicks = instrument.IsRendererAlive && instrument.Renderer!.Status != MidiRendererStatus.None
|
var bufferTicks = instrument.IsRendererAlive && instrument.Renderer!.Status != MidiRendererStatus.None
|
||||||
? instrument.Renderer.SequencerTimeScale * .2f
|
? instrument.Renderer.SequencerTimeScale * .2f
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
var bufferedTick = instrument.IsRendererAlive
|
var bufferedTick = instrument.IsRendererAlive
|
||||||
? instrument.Renderer!.SequencerTick - bufferTicks
|
? instrument.Renderer!.SequencerTick - bufferTicks
|
||||||
: int.MaxValue;
|
: int.MaxValue;
|
||||||
|
|
||||||
|
// TODO: Remove LINQ brain-rot.
|
||||||
var events = instrument.MidiEventBuffer
|
var events = instrument.MidiEventBuffer
|
||||||
.TakeWhile(x => x.Tick < bufferedTick)
|
.TakeWhile(x => x.Tick < bufferedTick)
|
||||||
.Take(max)
|
.Take(max)
|
||||||
@@ -376,5 +369,4 @@ namespace Content.Client.Instruments
|
|||||||
instrument.MidiEventBuffer.RemoveRange(0, 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
|
||||||
|
|||||||
@@ -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