2018-03-16 00:06:24 +00:00
|
|
|
using OpenTK.Audio;
|
|
|
|
using OpenTK.Audio.OpenAL;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
2018-07-15 02:57:41 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2018-03-19 18:58:46 +00:00
|
|
|
using System.Threading;
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-11-15 02:22:50 +00:00
|
|
|
namespace Ryujinx.Audio
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-11-15 02:22:50 +00:00
|
|
|
/// <summary>
|
|
|
|
/// An audio renderer that uses OpenAL as the audio backend
|
|
|
|
/// </summary>
|
2018-08-16 23:47:36 +00:00
|
|
|
public class OpenALAudioOut : IAalOutput, IDisposable
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
|
|
|
private const int MaxTracks = 256;
|
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
private const int MaxReleased = 32;
|
|
|
|
|
2018-03-16 00:06:24 +00:00
|
|
|
private AudioContext Context;
|
|
|
|
|
|
|
|
private class Track : IDisposable
|
|
|
|
{
|
|
|
|
public int SourceId { get; private set; }
|
|
|
|
|
|
|
|
public int SampleRate { get; private set; }
|
2018-07-10 01:49:07 +00:00
|
|
|
|
2018-03-16 00:06:24 +00:00
|
|
|
public ALFormat Format { get; private set; }
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
private ReleaseCallback Callback;
|
|
|
|
|
2018-03-16 00:06:24 +00:00
|
|
|
public PlaybackState State { get; set; }
|
|
|
|
|
|
|
|
private ConcurrentDictionary<long, int> Buffers;
|
|
|
|
|
|
|
|
private Queue<long> QueuedTagsQueue;
|
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
private Queue<long> ReleasedTagsQueue;
|
|
|
|
|
2018-03-16 00:06:24 +00:00
|
|
|
private bool Disposed;
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
public Track(int SampleRate, ALFormat Format, ReleaseCallback Callback)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
|
|
|
this.SampleRate = SampleRate;
|
|
|
|
this.Format = Format;
|
2018-03-19 18:58:46 +00:00
|
|
|
this.Callback = Callback;
|
2018-03-16 00:06:24 +00:00
|
|
|
|
|
|
|
State = PlaybackState.Stopped;
|
|
|
|
|
|
|
|
SourceId = AL.GenSource();
|
|
|
|
|
|
|
|
Buffers = new ConcurrentDictionary<long, int>();
|
|
|
|
|
|
|
|
QueuedTagsQueue = new Queue<long>();
|
2018-03-16 03:42:44 +00:00
|
|
|
|
|
|
|
ReleasedTagsQueue = new Queue<long>();
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
public bool ContainsBuffer(long Tag)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-03-16 03:42:44 +00:00
|
|
|
foreach (long QueuedTag in QueuedTagsQueue)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-03-16 03:42:44 +00:00
|
|
|
if (QueuedTag == Tag)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
return false;
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
public long[] GetReleasedBuffers(int Count)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
ReleasedCount += ReleasedTagsQueue.Count;
|
|
|
|
|
|
|
|
if (Count > ReleasedCount)
|
|
|
|
{
|
|
|
|
Count = ReleasedCount;
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
|
|
|
|
List<long> Tags = new List<long>();
|
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
while (Count-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
|
|
|
|
{
|
|
|
|
Tags.Add(Tag);
|
|
|
|
}
|
2018-03-16 03:42:44 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
while (Count-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
AL.SourceUnqueueBuffers(SourceId, 1);
|
|
|
|
|
|
|
|
Tags.Add(Tag);
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Tags.ToArray();
|
|
|
|
}
|
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
public int AppendBuffer(long Tag)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-03-16 03:42:44 +00:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Track));
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
int Id = AL.GenBuffer();
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-03-16 03:42:44 +00:00
|
|
|
AL.DeleteBuffer(OldId);
|
|
|
|
|
|
|
|
return Id;
|
|
|
|
});
|
|
|
|
|
|
|
|
QueuedTagsQueue.Enqueue(Tag);
|
|
|
|
|
|
|
|
return Id;
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
public void CallReleaseCallbackIfNeeded()
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-03-16 03:42:44 +00:00
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
if (ReleasedCount > 0)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
//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);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
ReleasedTagsQueue.Enqueue(Tag);
|
|
|
|
}
|
2018-03-19 18:58:46 +00:00
|
|
|
|
|
|
|
Callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 00:06:24 +00:00
|
|
|
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;
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
private Thread AudioPollerThread;
|
|
|
|
|
|
|
|
private bool KeepPolling;
|
|
|
|
|
2018-03-16 00:06:24 +00:00
|
|
|
public OpenALAudioOut()
|
|
|
|
{
|
|
|
|
Context = new AudioContext();
|
|
|
|
|
|
|
|
Tracks = new ConcurrentDictionary<int, Track>();
|
2018-03-19 18:58:46 +00:00
|
|
|
|
|
|
|
KeepPolling = true;
|
|
|
|
|
|
|
|
AudioPollerThread = new Thread(AudioPollerWork);
|
|
|
|
|
|
|
|
AudioPollerThread.Start();
|
|
|
|
}
|
|
|
|
|
2018-11-15 02:22:50 +00:00
|
|
|
/// <summary>
|
|
|
|
/// True if OpenAL is supported on the device.
|
|
|
|
/// </summary>
|
|
|
|
public static bool IsSupported
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return AudioContext.AvailableDevices.Count > 0;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
private void AudioPollerWork()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
foreach (Track Td in Tracks.Values)
|
|
|
|
{
|
2018-10-12 21:47:53 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.CallReleaseCallbackIfNeeded();
|
|
|
|
}
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
//If it's not slept it will waste cycles.
|
2018-08-01 03:48:49 +00:00
|
|
|
Thread.Sleep(10);
|
2018-03-19 18:58:46 +00:00
|
|
|
}
|
|
|
|
while (KeepPolling);
|
2018-08-16 23:47:36 +00:00
|
|
|
|
|
|
|
foreach (Track Td in Tracks.Values)
|
|
|
|
{
|
|
|
|
Td.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tracks.Clear();
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 02:57:41 +00:00
|
|
|
public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-07-15 02:57:41 +00:00
|
|
|
Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
|
2018-03-16 00:06:24 +00:00
|
|
|
|
|
|
|
for (int Id = 0; Id < MaxTracks; Id++)
|
|
|
|
{
|
|
|
|
if (Tracks.TryAdd(Id, Td))
|
|
|
|
{
|
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-15 02:57:41 +00:00
|
|
|
private ALFormat GetALFormat(int Channels)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-07-15 02:57:41 +00:00
|
|
|
switch (Channels)
|
2018-07-10 01:49:07 +00:00
|
|
|
{
|
2018-07-15 02:57:41 +00:00
|
|
|
case 1: return ALFormat.Mono16;
|
|
|
|
case 2: return ALFormat.Stereo16;
|
|
|
|
case 6: return ALFormat.Multi51Chn16Ext;
|
2018-07-10 01:49:07 +00:00
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-07-15 02:57:41 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(Channels));
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CloseTrack(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryRemove(Track, out Track Td))
|
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.Dispose();
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
public bool ContainsBuffer(int Track, long Tag)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
return Td.ContainsBuffer(Tag);
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
2018-07-10 01:49:07 +00:00
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
return false;
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
public long[] GetReleasedBuffers(int Track, int MaxCount)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
return Td.GetReleasedBuffers(MaxCount);
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
2018-07-10 01:49:07 +00:00
|
|
|
|
2018-03-16 03:42:44 +00:00
|
|
|
return null;
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 02:57:41 +00:00
|
|
|
public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
int BufferId = Td.AppendBuffer(Tag);
|
2018-03-16 03:42:44 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
int Size = Buffer.Length * Marshal.SizeOf<T>();
|
2018-07-15 02:57:41 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
|
2018-03-16 03:42:44 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
AL.SourceQueueBuffer(Td.SourceId, BufferId);
|
2018-03-16 03:42:44 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
StartPlaybackIfNeeded(Td);
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.State = PlaybackState.Playing;
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
StartPlaybackIfNeeded(Td);
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2018-09-18 03:12:47 +00:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.State = PlaybackState.Stopped;
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-09-18 03:12:47 +00:00
|
|
|
AL.SourceStop(Td.SourceId);
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlaybackState GetState(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
|
|
|
return Td.State;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PlaybackState.Stopped;
|
|
|
|
}
|
2018-08-16 23:47:36 +00:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
|
|
|
KeepPolling = false;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
}
|