Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehjohn committed Aug 14, 2024
1 parent 32a52f0 commit 58b1f03
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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")))
{
Expand All @@ -40,7 +40,7 @@ public AudioEngine()
}
catch (Exception exception)
{
Logger.LogException(nameof(AudioEngine), exception);
Logger.LogException(nameof(BassAudioEngine), exception);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Zen.System/Modules/Audio/Engines/IZenAudioEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Zen.System.Modules.Audio.Engines;

public interface IZenAudioEngine : IDisposable
{
void Send(float[] data);
}
49 changes: 49 additions & 0 deletions src/Zen.System/Modules/Audio/Engines/PortAudioEngine.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 2 additions & 1 deletion src/Zen.System/Modules/AyAudio.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Zen.Common.Infrastructure;
using Zen.System.Modules.Audio;
using Zen.System.Modules.Audio.Engines;

namespace Zen.System.Modules;

Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/Zen.System/Zen.System.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Bufdio" Version="0.1.0" />
<PackageReference Include="radio42.Bass.Net.core" Version="2.4.17.5" />
</ItemGroup>

Expand Down

0 comments on commit 58b1f03

Please sign in to comment.