WIP: OpenAL audio system.
This commit is contained in:
59
Voile.OpenAL/OpenALSystem.cs
Normal file
59
Voile.OpenAL/OpenALSystem.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Voile.Audio;
|
||||
using Silk.NET.OpenAL;
|
||||
|
||||
namespace Voile.OpenAL;
|
||||
|
||||
public class OpenALSystem : AudioSystem
|
||||
{
|
||||
public OpenALSystem()
|
||||
{
|
||||
_al = AL.GetApi();
|
||||
_alc = ALContext.GetApi();
|
||||
|
||||
Init();
|
||||
}
|
||||
|
||||
public override void PlaySound(Sound sound, float volume)
|
||||
{
|
||||
var buffer = CreateAlBuffer(sound.Buffer, sound.SampleRate, sound.Channel);
|
||||
var source = CreateAlSource(buffer);
|
||||
_al.SourcePlay(source);
|
||||
}
|
||||
|
||||
public override void Update(double deltaTime)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private unsafe void Init()
|
||||
{
|
||||
_device = _alc.OpenDevice("");
|
||||
_context = _alc.CreateContext(_device, null);
|
||||
_alc.MakeContextCurrent(_context);
|
||||
}
|
||||
|
||||
private uint CreateAlBuffer(ReadOnlyMemory<short> data, int sampleRate, SoundChannel channels)
|
||||
{
|
||||
var buffer = _al.GenBuffer();
|
||||
var format = channels == SoundChannel.Mono ? BufferFormat.Mono16 : BufferFormat.Stereo16;
|
||||
|
||||
unsafe
|
||||
{
|
||||
_al.BufferData(buffer, format, data.Pin().Pointer, data.Length, sampleRate);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private uint CreateAlSource(uint buffer)
|
||||
{
|
||||
var source = _al.GenSource();
|
||||
_al.SetSourceProperty(source, SourceInteger.Buffer, buffer);
|
||||
return source;
|
||||
}
|
||||
|
||||
private unsafe Device* _device;
|
||||
private ALContext _alc;
|
||||
private unsafe Context* _context;
|
||||
private AL _al;
|
||||
}
|
||||
17
Voile.OpenAL/Voile.OpenAL.csproj
Normal file
17
Voile.OpenAL/Voile.OpenAL.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../Voile/Voile.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Silk.NET.OpenAL" Version="2.21.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user