Fix instruments for engine changes, fixes program change bug. (#7365)

This commit is contained in:
Vera Aguilera Puerto
2022-04-08 16:22:05 +02:00
committed by GitHub
parent a4d55235cc
commit 6e73e94cc6
8 changed files with 327 additions and 322 deletions

View File

@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using Content.Shared.Instruments;
using Robust.Client.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;
@@ -14,21 +9,26 @@ public sealed class InstrumentComponent : SharedInstrumentComponent
{
public event Action? OnMidiPlaybackEnded;
[ViewVariables]
public IMidiRenderer? Renderer;
[ViewVariables]
public uint SequenceDelay;
[ViewVariables]
public uint SequenceStartTick;
[ViewVariables]
public TimeSpan LastMeasured = TimeSpan.MinValue;
[ViewVariables]
public int SentWithinASec;
/// <summary>
/// A queue of MidiEvents to be sent to the server.
/// </summary>
[ViewVariables]
public readonly List<MidiEvent> MidiEventBuffer = new();
public readonly List<RobustMidiEvent> MidiEventBuffer = new();
/// <summary>
/// Whether a midi song will loop or not.

View File

@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using Content.Shared.CCVar;
using Content.Shared.Instruments;
@@ -9,28 +6,20 @@ using JetBrains.Annotations;
using Robust.Client.Audio.Midi;
using Robust.Shared.Audio.Midi;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Network;
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 IMidiManager _midiManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
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 MaxMidiEventsPerSecond { get; private set; }
@@ -78,6 +67,7 @@ namespace Content.Client.Instruments
if (instrument.Renderer != null)
{
instrument.Renderer.SendMidiEvent(RobustMidiEvent.SystemReset(instrument.Renderer.SequencerTick));
UpdateRenderer(uid, instrument);
instrument.Renderer.OnMidiPlayerFinished += () =>
{
@@ -97,11 +87,16 @@ namespace Content.Client.Instruments
if (!Resolve(uid, ref instrument) || instrument.Renderer == null)
return;
instrument.Renderer.MidiBank = instrument.InstrumentBank;
instrument.Renderer.MidiProgram = instrument.InstrumentProgram;
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.MidiBank = instrument.InstrumentBank;
}
instrument.Renderer.LoopMidi = instrument.LoopMidi;
instrument.DirtyRenderer = false;
}
@@ -147,30 +142,22 @@ namespace Content.Client.Instruments
if (!Resolve(uid, ref instrument))
return;
if (instrument.Renderer == null || instrument.Renderer.Status != MidiRendererStatus.File)
if (instrument.Renderer is not { Status: MidiRendererStatus.File })
return;
instrument.MidiEventBuffer.Clear();
instrument.Renderer.PlayerTick = playerTick;
var tick = instrument.Renderer.SequencerTick;
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(new MidiEvent()
{
Tick = tick, Type = 176,
Control = 123, Velocity = 0, Channel = i,
});
//instrument.MidiEventBuffer.Add(RobustMidiEvent.AllNotesOff(i, tick));
}
// Now we add a Reset All Controllers message.
instrument.MidiEventBuffer.Add(new MidiEvent()
{
Tick = tick, Type = 176,
Control = 121, Value = 0,
});
instrument.Renderer.PlayerTick = playerTick;
}
public bool OpenInput(EntityUid uid, InstrumentComponent? instrument = null)
@@ -201,6 +188,8 @@ namespace Content.Client.Instruments
return false;
}
instrument.MidiEventBuffer.Clear();
instrument.Renderer.OnMidiEvent += instrument.MidiEventBuffer.Add;
return true;
}
@@ -247,7 +236,7 @@ namespace Content.Client.Instruments
{
var uid = midiEv.Uid;
if (!EntityManager.TryGetComponent(uid, out InstrumentComponent? instrument))
if (!TryComp(uid, out InstrumentComponent? instrument))
return;
var renderer = instrument.Renderer;
@@ -285,18 +274,21 @@ namespace Content.Client.Instruments
var currentTick = renderer.SequencerTick;
// 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 scheduled = ev.Tick + instrument.SequenceDelay;
if (scheduled <= currentTick)
if (scheduled < currentTick)
{
instrument.SequenceDelay += currentTick - ev.Tick;
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
// 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;
// TODO: Remove LINQ brain-rot.
var events = instrument.MidiEventBuffer
.TakeWhile(x => x.Tick < bufferedTick)
.Take(max)
@@ -376,5 +369,4 @@ namespace Content.Client.Instruments
instrument.MidiEventBuffer.RemoveRange(0, eventCount);
}
}
}
}

View File

@@ -25,6 +25,9 @@ public sealed class InstrumentComponent : SharedInstrumentComponent
[ViewVariables]
public int MidiEventCount = 0;
[ViewVariables]
public uint LastSequencerTick = 0;
// TODO Instruments: Make this ECS
public IPlayerSession? InstrumentPlayer =>
_entMan.GetComponentOrNull<ActivatableUIComponent>(Owner)?.CurrentSingleUser

View File

@@ -110,7 +110,7 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
{
var uid = msg.Uid;
if (!EntityManager.TryGetComponent(uid, out InstrumentComponent? instrument))
if (!TryComp(uid, out InstrumentComponent? instrument))
return;
if (!instrument.Playing
@@ -121,7 +121,20 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
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)
{
instrument.LaggedBatches++;
@@ -158,7 +171,6 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
RaiseNetworkEvent(msg);
}
var maxTick = msg.MidiEvent.Max(x => x.Tick);
instrument.LastSequencerTick = Math.Max(maxTick, minTick);
}

View File

@@ -15,9 +15,6 @@ public abstract class SharedInstrumentComponent : Component
[ViewVariables]
public bool Playing { get; set; }
[ViewVariables]
public uint LastSequencerTick { get; set; }
[DataField("program"), ViewVariables(VVAccess.ReadWrite)]
public byte InstrumentProgram { get; set; }
@@ -73,9 +70,9 @@ public sealed class InstrumentStartMidiEvent : EntityEventArgs
public sealed class InstrumentMidiEventEvent : EntityEventArgs
{
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;
MidiEvent = midiEvent;
@@ -92,7 +89,7 @@ public sealed class InstrumentState : ComponentState
public bool AllowProgramChange { 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;
InstrumentProgram = instrumentProgram;

View File

@@ -23,7 +23,7 @@ public abstract class SharedInstrumentSystem : EntitySystem
{
args.State =
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)

View File

@@ -283,6 +283,7 @@
<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/=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/=Trasen/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unanchor/@EntryIndexedValue">True</s:Boolean>