1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-10-01 12:30:00 +02:00
Ryujinx/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs
jduncanator 8275bc3c08 Implement libsoundio as an alternative audio backend (#406)
* Audio: Implement libsoundio as an alternative audio backend

libsoundio will be preferred over OpenAL if it is available on the machine. If neither are available, it will fallback to a dummy audio renderer that outputs no sound.

* Audio: Fix SoundIoRingBuffer documentation

* Audio: Unroll and optimize the audio write callback

Copying one sample at a time is slow, this unrolls the most common audio channel layouts and manually copies the bytes between source and destination. This is over 2x faster than calling CopyBlockUnaligned every sample.

* Audio: Optimize the write callback further

This dramatically reduces the audio buffer copy time. When the sample size is one of handled sample sizes the buffer copy operation is almost 10x faster than CopyBlockAligned.

This works by copying full samples at a time, rather than the individual bytes that make up the sample. This allows for 2x or 4x faster copy operations depending on sample size.

* Audio: Fix typo in Stereo write callback

* Audio: Fix Surround (5.1) audio write callback

* Audio: Update Documentation

* Audio: Use built-in Unsafe.SizeOf<T>()

Built-in `SizeOf<T>()` is 10x faster than our `TypeSize<T>` helper. This also helps reduce code surface area.

* Audio: Keep fixed buffer style consistent

* Audio: Address styling nits

* Audio: More style nits

* Audio: Add additional documentation

* Audio: Move libsoundio bindings internal

As per discussion, moving the libsoundio native bindings into Ryujinx.Audio

* Audio: Bump Target Framework back up to .NET Core 2.1

* Audio: Remove voice mixing optimizations.

Leaves Saturation optimizations in place.
2018-11-15 03:22:50 +01:00

369 lines
No EOL
9.5 KiB
C#

using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace Ryujinx.Audio
{
/// <summary>
/// An audio renderer that uses OpenAL as the audio backend
/// </summary>
public class OpenALAudioOut : IAalOutput, IDisposable
{
private const int MaxTracks = 256;
private const int MaxReleased = 32;
private AudioContext Context;
private class Track : IDisposable
{
public int SourceId { get; private set; }
public int SampleRate { get; private set; }
public ALFormat Format { get; private set; }
private ReleaseCallback Callback;
public PlaybackState State { get; set; }
private ConcurrentDictionary<long, int> Buffers;
private Queue<long> QueuedTagsQueue;
private Queue<long> ReleasedTagsQueue;
private bool Disposed;
public Track(int SampleRate, ALFormat Format, ReleaseCallback Callback)
{
this.SampleRate = SampleRate;
this.Format = Format;
this.Callback = Callback;
State = PlaybackState.Stopped;
SourceId = AL.GenSource();
Buffers = new ConcurrentDictionary<long, int>();
QueuedTagsQueue = new Queue<long>();
ReleasedTagsQueue = new Queue<long>();
}
public bool ContainsBuffer(long Tag)
{
foreach (long QueuedTag in QueuedTagsQueue)
{
if (QueuedTag == Tag)
{
return true;
}
}
return false;
}
public long[] GetReleasedBuffers(int Count)
{
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
ReleasedCount += ReleasedTagsQueue.Count;
if (Count > ReleasedCount)
{
Count = ReleasedCount;
}
List<long> Tags = new List<long>();
while (Count-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
{
Tags.Add(Tag);
}
while (Count-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
{
AL.SourceUnqueueBuffers(SourceId, 1);
Tags.Add(Tag);
}
return Tags.ToArray();
}
public int AppendBuffer(long Tag)
{
if (Disposed)
{
throw new ObjectDisposedException(nameof(Track));
}
int Id = AL.GenBuffer();
Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
{
AL.DeleteBuffer(OldId);
return Id;
});
QueuedTagsQueue.Enqueue(Tag);
return Id;
}
public void CallReleaseCallbackIfNeeded()
{
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
if (ReleasedCount > 0)
{
//If we signal, then we also need to have released buffers available
//to return when GetReleasedBuffers is called.
//If playback needs to be re-started due to all buffers being processed,
//then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue.
while (ReleasedCount-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
{
AL.SourceUnqueueBuffers(SourceId, 1);
ReleasedTagsQueue.Enqueue(Tag);
}
Callback();
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool Disposing)
{
if (Disposing && !Disposed)
{
Disposed = true;
AL.DeleteSource(SourceId);
foreach (int Id in Buffers.Values)
{
AL.DeleteBuffer(Id);
}
}
}
}
private ConcurrentDictionary<int, Track> Tracks;
private Thread AudioPollerThread;
private bool KeepPolling;
public OpenALAudioOut()
{
Context = new AudioContext();
Tracks = new ConcurrentDictionary<int, Track>();
KeepPolling = true;
AudioPollerThread = new Thread(AudioPollerWork);
AudioPollerThread.Start();
}
/// <summary>
/// True if OpenAL is supported on the device.
/// </summary>
public static bool IsSupported
{
get
{
try
{
return AudioContext.AvailableDevices.Count > 0;
}
catch
{
return false;
}
}
}
private void AudioPollerWork()
{
do
{
foreach (Track Td in Tracks.Values)
{
lock (Td)
{
Td.CallReleaseCallbackIfNeeded();
}
}
//If it's not slept it will waste cycles.
Thread.Sleep(10);
}
while (KeepPolling);
foreach (Track Td in Tracks.Values)
{
Td.Dispose();
}
Tracks.Clear();
}
public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
{
Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
for (int Id = 0; Id < MaxTracks; Id++)
{
if (Tracks.TryAdd(Id, Td))
{
return Id;
}
}
return -1;
}
private ALFormat GetALFormat(int Channels)
{
switch (Channels)
{
case 1: return ALFormat.Mono16;
case 2: return ALFormat.Stereo16;
case 6: return ALFormat.Multi51Chn16Ext;
}
throw new ArgumentOutOfRangeException(nameof(Channels));
}
public void CloseTrack(int Track)
{
if (Tracks.TryRemove(Track, out Track Td))
{
lock (Td)
{
Td.Dispose();
}
}
}
public bool ContainsBuffer(int Track, long Tag)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
return Td.ContainsBuffer(Tag);
}
}
return false;
}
public long[] GetReleasedBuffers(int Track, int MaxCount)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
return Td.GetReleasedBuffers(MaxCount);
}
}
return null;
}
public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
int BufferId = Td.AppendBuffer(Tag);
int Size = Buffer.Length * Marshal.SizeOf<T>();
AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
AL.SourceQueueBuffer(Td.SourceId, BufferId);
StartPlaybackIfNeeded(Td);
}
}
}
public void Start(int Track)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
Td.State = PlaybackState.Playing;
StartPlaybackIfNeeded(Td);
}
}
}
private void StartPlaybackIfNeeded(Track Td)
{
AL.GetSource(Td.SourceId, ALGetSourcei.SourceState, out int StateInt);
ALSourceState State = (ALSourceState)StateInt;
if (State != ALSourceState.Playing && Td.State == PlaybackState.Playing)
{
AL.SourcePlay(Td.SourceId);
}
}
public void Stop(int Track)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
Td.State = PlaybackState.Stopped;
AL.SourceStop(Td.SourceId);
}
}
}
public PlaybackState GetState(int Track)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
return Td.State;
}
return PlaybackState.Stopped;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool Disposing)
{
if (Disposing)
{
KeepPolling = false;
}
}
}
}