Skip to content

Commit

Permalink
Cleaned up code, removed duplicates and double references to same obj…
Browse files Browse the repository at this point in the history
…ects 'n stuff like that
  • Loading branch information
KaleidonKep99 committed Feb 27, 2025
1 parent 8f39e9f commit 283c7dd
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 136 deletions.
37 changes: 0 additions & 37 deletions OmniConverter/Extensions/Audio/AudioEngine.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSCore;
using ManagedBass.Fx;
using System;

namespace OmniConverter
Expand Down Expand Up @@ -34,12 +33,48 @@ enum MIDIEventType
Unknown4 = 0xFD
};

public abstract class MIDIRenderer : ISampleSource
public enum EngineID
{
protected readonly object Lock = new object();
protected long streamLength = 0;
Unknown = -1,
BASS = 0,
XSynth = 1,
FluidSynth = 2,
MAX = FluidSynth
}

public abstract class AudioEngine : IDisposable
{
protected bool _disposed = false;
protected bool _init = false;
protected WaveFormat _waveFormat = new(48000, 32, 2);
protected Settings _cachedSettings;

public AudioEngine(Settings settings, bool defaultInit = true)
{
_cachedSettings = settings;
_waveFormat = new(settings.Synth.SampleRate, 32, 2);
_init = defaultInit;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public Settings GetCachedSettings() => _cachedSettings;
public WaveFormat GetWaveFormat() => _waveFormat;
public bool IsInitialized() => _init;

protected abstract void Dispose(bool disposing);
}

public abstract class AudioRenderer : ISampleSource
{
protected Settings _cachedSettings;
protected readonly object _lock = new object();
protected long _streamLength = 0;

protected double Volume { get; set; }
public string UniqueID { get; protected set; } = IDGenerator.GetID();
public bool CanSeek { get; protected set; } = false;
public WaveFormat WaveFormat { get; protected set; } = new(48000, 32, 2);
Expand All @@ -48,18 +83,24 @@ public abstract class MIDIRenderer : ISampleSource
public ulong ActiveVoices { get; protected set; } = 0;
public float RenderingTime { get; protected set; } = 0.0f;

public MIDIRenderer(WaveFormat waveFormat, double volume, bool defaultInt = true) { WaveFormat = waveFormat; Volume = volume; Initialized = defaultInt; }
public AudioRenderer(AudioEngine audioEngine, bool defaultInt = true)
{
WaveFormat = audioEngine.GetWaveFormat();
_cachedSettings = audioEngine.GetCachedSettings();
Initialized = defaultInt;
}

public abstract void SystemReset();
public abstract bool SendCustomFXEvents(int channel, short reverb, short chorus);
public abstract void SendEvent(byte[] data);
public abstract unsafe int Read(float[] buffer, int offset, long delta, int count);
public abstract void RefreshInfo();
public abstract void SendEndEvent();
public virtual long Position { get { return streamLength; } set { } }
public virtual long Length { get { return streamLength; } }
public virtual long Position { get { return _streamLength; } set { } }
public virtual long Length { get { return _streamLength; } }

public int Read(float[] buffer, int offset, int count) => Read(buffer, offset, 0, count);

public void Dispose()
{
Dispose(true);
Expand Down
46 changes: 20 additions & 26 deletions OmniConverter/Extensions/Audio/Renderers/BASSRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BASSEngine : AudioEngine
private int FlacPlug = 0;
private MidiFontEx[]? _bassArray;

public BASSEngine(CSCore.WaveFormat waveFormat, Settings settings) : base(waveFormat, settings, false)
public BASSEngine(Settings settings) : base(settings, false)
{
/*
-+++------.
Expand Down Expand Up @@ -45,7 +45,7 @@ . .#+ -+-
-++.
*/

if (Bass.Init(Bass.NoSoundDevice, waveFormat.SampleRate, DeviceInitFlags.Default))
if (Bass.Init(Bass.NoSoundDevice, _cachedSettings.WaveFormat.SampleRate, DeviceInitFlags.Default))
{
_bassArray = InitializeSoundFonts();

Expand All @@ -55,17 +55,17 @@ . .#+ -+-
if (tmp != 0)
{
// Subtract 1 because BASS uses -1 for no interpolation, 0 for linear and so on
var interp = ((int)CachedSettings.Synth.Interpolation)
var interp = ((int)_cachedSettings.Synth.Interpolation)
.LimitToRange((int)GlobalSynthSettings.InterpolationType.None,
(int)GlobalSynthSettings.InterpolationType.Max) - 1;

Bass.Configure(Configuration.MidiVoices, CachedSettings.Synth.MaxVoices);
Bass.Configure(Configuration.MidiVoices, _cachedSettings.Synth.MaxVoices);
Bass.Configure(Configuration.SRCQuality, interp);
Bass.Configure(Configuration.SampleSRCQuality, interp);

Bass.StreamFree(tmp);

Initialized = true;
_init = true;

if (FlacPlug == 0)
Debug.PrintToConsole(Debug.LogType.Warning, "BASSFLAC failed to load, this could lead to incorrect opcode handling when using SFZ based SoundFonts using FLAC samples.");
Expand All @@ -90,18 +90,18 @@ protected override void Dispose(bool disposing)
if (FlacPlug != 0)
Bass.PluginFree(FlacPlug);

if (Initialized)
if (_init)
Bass.Free();

Initialized = false;
_init = false;
_disposed = true;
}

private MidiFontEx[]? InitializeSoundFonts()
{
var _bassArray = new List<MidiFontEx>();

foreach (SoundFont sf in CachedSettings.SoundFontsList)
foreach (SoundFont sf in _cachedSettings.SoundFontsList)
{
if (!sf.Enabled)
{
Expand Down Expand Up @@ -187,7 +187,7 @@ private void FreeSoundFontsArray()
public MidiFontEx[]? GetSoundFontsArray() => _bassArray;
}

public class BASSRenderer : MIDIRenderer
public class BASSRenderer : AudioRenderer
{
private readonly BassFlags Flags;
public int Handle { get; private set; } = 0;
Expand All @@ -197,20 +197,23 @@ public class BASSRenderer : MIDIRenderer
private VolumeFxParameters? VolParam = null;
private MidiFontEx[]? SfArray = [];

public BASSRenderer(BASSEngine bass) : base(bass.WaveFormat, bass.CachedSettings.Synth.Volume, false)
public BASSRenderer(BASSEngine bass) : base(bass, false)
{
if (UniqueID == string.Empty)
return;

if (bass == null)
return;

reference = bass;

bool isFloat = WaveFormat.WaveFormatTag == AudioEncoding.IeeeFloat;
Flags = BassFlags.Decode | BassFlags.MidiDecayEnd;
Debug.PrintToConsole(Debug.LogType.Message, $"Stream unique ID: {UniqueID}");

Flags |= (reference.CachedSettings.Synth.Interpolation > GlobalSynthSettings.InterpolationType.Linear) ? BassFlags.SincInterpolation : BassFlags.Default;
Flags |= reference.CachedSettings.BASS.DisableEffects ? BassFlags.MidiNoFx : BassFlags.Default;
Flags |= reference.CachedSettings.BASS.NoteOff1 ? BassFlags.MidiNoteOff1 : BassFlags.Default;
Flags |= (_cachedSettings.Synth.Interpolation > GlobalSynthSettings.InterpolationType.Linear) ? BassFlags.SincInterpolation : BassFlags.Default;
Flags |= _cachedSettings.Synth.DisableEffects ? BassFlags.MidiNoFx : BassFlags.Default;
Flags |= _cachedSettings.BASS.NoteOff1 ? BassFlags.MidiNoteOff1 : BassFlags.Default;
Flags |= isFloat ? BassFlags.Float : BassFlags.Default;

Handle = BassMidi.CreateStream(16, Flags, WaveFormat.SampleRate);
Expand All @@ -223,7 +226,7 @@ public BASSRenderer(BASSEngine bass) : base(bass.WaveFormat, bass.CachedSettings
if (IsError("Unable to set volume FX."))
return;

Bass.ChannelSetAttribute(Handle, ChannelAttribute.MidiKill, Convert.ToDouble(reference.CachedSettings.Synth.KilledNoteFading));
Bass.ChannelSetAttribute(Handle, ChannelAttribute.MidiKill, Convert.ToDouble(_cachedSettings.Synth.KilledNoteFading));

SfArray = reference.GetSoundFontsArray();
if (SfArray != null)
Expand All @@ -237,7 +240,7 @@ public BASSRenderer(BASSEngine bass) : base(bass.WaveFormat, bass.CachedSettings
VolParam = new VolumeFxParameters();

VolParam.fCurrent = 1.0f;
VolParam.fTarget = (float)Volume;
VolParam.fTarget = (float)_cachedSettings.Synth.Volume;
VolParam.fTime = 0.0f;
VolParam.lCurve = 1;
Bass.FXSetParameters(VolHandle, VolParam);
Expand All @@ -260,7 +263,7 @@ public override unsafe int Read(float[] buffer, int offset, long delta, int coun
{
int ret = 0;

lock (Lock)
lock (_lock)
{
fixed (float* buff = buffer)
{
Expand Down Expand Up @@ -305,19 +308,10 @@ public override void SendEvent(byte[] data)
switch ((MIDIEventType)(status & 0xF0))
{
case MIDIEventType.NoteOn:
if (reference.CachedSettings.Event.FilterVelocity && param2 >= reference.CachedSettings.Event.VelocityLow && param2 <= reference.CachedSettings.Event.VelocityHigh)
return;

if (reference.CachedSettings.Event.FilterKey && (param1 < reference.CachedSettings.Event.KeyLow || param1 > reference.CachedSettings.Event.KeyHigh))
return;

eventParams = param2 << 8 | param1;
break;

case MIDIEventType.NoteOff:
if (reference.CachedSettings.Event.FilterKey && (param1 < reference.CachedSettings.Event.KeyLow || param1 > reference.CachedSettings.Event.KeyHigh))
return;

eventParams = param1;
break;

Expand Down Expand Up @@ -388,7 +382,7 @@ protected override void Dispose(bool disposing)

if (disposing)
{
lock (Lock)
lock (_lock)
Bass.StreamFree(Handle);
}

Expand Down
46 changes: 30 additions & 16 deletions OmniConverter/Extensions/Audio/Renderers/FluidSynthRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace OmniConverter
public class FluidSynthEngine : AudioEngine
{
private NFluidsynth.Settings _fluidSynthSettings;
private Settings _cachedSettings;

public readonly object SFLock = new object();

public unsafe FluidSynthEngine(CSCore.WaveFormat waveFormat, Settings settings) : base(waveFormat, settings, false)
public unsafe FluidSynthEngine(Settings settings) : base(settings, false)
{
Debug.PrintToConsole(Debug.LogType.Message, $"Preparing FluidSynth settings...");

Expand All @@ -30,7 +29,7 @@ public unsafe FluidSynthEngine(CSCore.WaveFormat waveFormat, Settings settings)

_cachedSettings = settings;

Initialized = true;
_init = true;

Debug.PrintToConsole(Debug.LogType.Message, $"FluidSynth settings prepared...");

Expand All @@ -42,7 +41,7 @@ protected override void Dispose(bool disposing)
if (_disposed)
return;

if (Initialized)
if (_init)
{
_fluidSynthSettings.Dispose();
}
Expand All @@ -54,7 +53,7 @@ protected override void Dispose(bool disposing)
public Settings GetConverterSettings() => _cachedSettings;
}

public class FluidSynthRenderer : MIDIRenderer
public class FluidSynthRenderer : AudioRenderer
{
public Synth? handle { get; private set; } = null;
private bool noMoreData = false;
Expand All @@ -69,7 +68,7 @@ public class FluidSynthRenderer : MIDIRenderer

private FluidSynthEngine reference;

public FluidSynthRenderer(FluidSynthEngine fluidsynth) : base(fluidsynth.WaveFormat, fluidsynth.CachedSettings.Synth.Volume, false)
public FluidSynthRenderer(FluidSynthEngine fluidsynth) : base(fluidsynth, false)
{
reference = fluidsynth;

Expand All @@ -83,8 +82,31 @@ public FluidSynthRenderer(FluidSynthEngine fluidsynth) : base(fluidsynth.WaveFor

handle = new(reference.GetFluidSynthSettings());
var tmp = reference.GetConverterSettings();
var interp = FluidInterpolation.Linear;

switch (tmp.Synth.Interpolation)
{
case GlobalSynthSettings.InterpolationType.None:
interp = FluidInterpolation.None;
break;

case GlobalSynthSettings.InterpolationType.Point8:
case GlobalSynthSettings.InterpolationType.Point16:
interp = FluidInterpolation.FourthOrder;
break;

case GlobalSynthSettings.InterpolationType.Point32:
case GlobalSynthSettings.InterpolationType.Point64:
interp = FluidInterpolation.SeventhOrder;
break;

case GlobalSynthSettings.InterpolationType.Linear:
default:
break;
}

handle.Gain = (float)tmp.Synth.Volume;
handle.SetInterpolationMethod(-1, interp);

// FluidSynth "thread-safe API" moment
lock (reference.SFLock)
Expand Down Expand Up @@ -147,7 +169,7 @@ public override unsafe int Read(float[] buffer, int offset, long delta, int coun
Array.Clear(bufOutL, 0, bufOutL.Length);
Array.Clear(bufOutR, 0, bufOutR.Length);

lock (Lock)
lock (_lock)
{
fixed (float* buff = buffer)
{
Expand All @@ -163,7 +185,7 @@ public override unsafe int Read(float[] buffer, int offset, long delta, int coun
}
}

streamLength += count;
_streamLength += count;
return count;
}

Expand Down Expand Up @@ -201,11 +223,6 @@ public override void SendEvent(byte[] data)
switch ((MIDIEventType)(status & 0xF0))
{
case MIDIEventType.NoteOn:
if (reference.CachedSettings.Event.FilterVelocity && param2 >= reference.CachedSettings.Event.VelocityLow && param2 <= reference.CachedSettings.Event.VelocityHigh)
return;
if (reference.CachedSettings.Event.FilterKey && (param1 < reference.CachedSettings.Event.KeyLow || param1 > reference.CachedSettings.Event.KeyHigh))
return;

if (param1 == 0)
{
handle.NoteOff(chan, param1);
Expand All @@ -214,9 +231,6 @@ public override void SendEvent(byte[] data)
return;

case MIDIEventType.NoteOff:
if (reference.CachedSettings.Event.FilterKey && (param1 < reference.CachedSettings.Event.KeyLow || param1 > reference.CachedSettings.Event.KeyHigh))
return;

handle.NoteOff(chan, param1);
return;

Expand Down
Loading

0 comments on commit 283c7dd

Please sign in to comment.