From 58b1f03a9ad3b0a3c7cae2e8fbf7b5e054cb243d Mon Sep 17 00:00:00 2001 From: Stevo John Date: Wed, 14 Aug 2024 23:31:43 +0100 Subject: [PATCH] WIP --- .../BassAudioEngine.cs} | 8 +-- .../Modules/Audio/Engines/IZenAudioEngine.cs | 6 +++ .../Modules/Audio/Engines/PortAudioEngine.cs | 49 +++++++++++++++++++ src/Zen.System/Modules/AyAudio.cs | 3 +- src/Zen.System/Zen.System.csproj | 1 + 5 files changed, 62 insertions(+), 5 deletions(-) rename src/Zen.System/Modules/Audio/{AudioEngine.cs => Engines/BassAudioEngine.cs} (91%) create mode 100644 src/Zen.System/Modules/Audio/Engines/IZenAudioEngine.cs create mode 100644 src/Zen.System/Modules/Audio/Engines/PortAudioEngine.cs diff --git a/src/Zen.System/Modules/Audio/AudioEngine.cs b/src/Zen.System/Modules/Audio/Engines/BassAudioEngine.cs similarity index 91% rename from src/Zen.System/Modules/Audio/AudioEngine.cs rename to src/Zen.System/Modules/Audio/Engines/BassAudioEngine.cs index 9197e775..01718623 100644 --- a/src/Zen.System/Modules/Audio/AudioEngine.cs +++ b/src/Zen.System/Modules/Audio/Engines/BassAudioEngine.cs @@ -2,9 +2,9 @@ using Un4seen.Bass; using Zen.Common.Infrastructure; -namespace Zen.System.Modules.Audio; +namespace Zen.System.Modules.Audio.Engines; -public class AudioEngine : IDisposable +public class BassAudioEngine : IZenAudioEngine { private static readonly AutoResetEvent ResetEvent = new(true); @@ -14,7 +14,7 @@ public class AudioEngine : IDisposable private bool _first = true; - public AudioEngine() + public BassAudioEngine() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && ! File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libbass.so"))) { @@ -40,7 +40,7 @@ public AudioEngine() } catch (Exception exception) { - Logger.LogException(nameof(AudioEngine), exception); + Logger.LogException(nameof(BassAudioEngine), exception); } } diff --git a/src/Zen.System/Modules/Audio/Engines/IZenAudioEngine.cs b/src/Zen.System/Modules/Audio/Engines/IZenAudioEngine.cs new file mode 100644 index 00000000..e59ee5f9 --- /dev/null +++ b/src/Zen.System/Modules/Audio/Engines/IZenAudioEngine.cs @@ -0,0 +1,6 @@ +namespace Zen.System.Modules.Audio.Engines; + +public interface IZenAudioEngine : IDisposable +{ + void Send(float[] data); +} \ No newline at end of file diff --git a/src/Zen.System/Modules/Audio/Engines/PortAudioEngine.cs b/src/Zen.System/Modules/Audio/Engines/PortAudioEngine.cs new file mode 100644 index 00000000..a5c8b04e --- /dev/null +++ b/src/Zen.System/Modules/Audio/Engines/PortAudioEngine.cs @@ -0,0 +1,49 @@ +using System.Runtime.InteropServices; +using Bufdio; +using Bufdio.Engines; +using Zen.Common.Infrastructure; + +namespace Zen.System.Modules.Audio.Engines; + +public class PortAudioEngine : IZenAudioEngine +{ + private readonly IAudioEngine? _engine; + + public PortAudioEngine() + { + try + { + if (Environment.OSVersion.Platform == PlatformID.Unix) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + BufdioLib.InitializePortAudio(); + } + else + { + BufdioLib.InitializePortAudio(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Libraries", "libportaudio.dylib")); + } + } + else + { + BufdioLib.InitializePortAudio(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Libraries", "libportaudio")); + } + + _engine = new Bufdio.Engines.PortAudioEngine(new AudioEngineOptions(1, Constants.SampleRate)); + } + catch (Exception exception) + { + Logger.LogException(nameof(PortAudioEngine), exception); + } + } + + public void Send(float[] data) + { + _engine?.Send(data); + } + + public void Dispose() + { + _engine?.Dispose(); + } +} \ No newline at end of file diff --git a/src/Zen.System/Modules/AyAudio.cs b/src/Zen.System/Modules/AyAudio.cs index 99c3f622..b9449310 100644 --- a/src/Zen.System/Modules/AyAudio.cs +++ b/src/Zen.System/Modules/AyAudio.cs @@ -1,5 +1,6 @@ using Zen.Common.Infrastructure; using Zen.System.Modules.Audio; +using Zen.System.Modules.Audio.Engines; namespace Zen.System.Modules; @@ -21,7 +22,7 @@ public class AyAudio : IDisposable private float[] _buffer; - private readonly AudioEngine _engine = new(); + private readonly IZenAudioEngine _engine = new PortAudioEngine(); private readonly CancellationTokenSource _cancellationTokenSource; diff --git a/src/Zen.System/Zen.System.csproj b/src/Zen.System/Zen.System.csproj index a4a0c466..9a314ce2 100644 --- a/src/Zen.System/Zen.System.csproj +++ b/src/Zen.System/Zen.System.csproj @@ -17,6 +17,7 @@ +