2018-03-16 01:06:24 +01:00
|
|
|
using OpenTK.Audio;
|
|
|
|
using OpenTK.Audio.OpenAL;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
2018-07-15 04:57:41 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2018-03-19 19:58:46 +01:00
|
|
|
using System.Threading;
|
2018-03-16 01:06:24 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Audio.OpenAL
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
public class OpenALAudioOut : IAalOutput, IDisposable
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
private const int MaxTracks = 256;
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
private const int MaxReleased = 32;
|
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
private AudioContext Context;
|
|
|
|
|
|
|
|
private class Track : IDisposable
|
|
|
|
{
|
|
|
|
public int SourceId { get; private set; }
|
|
|
|
|
|
|
|
public int SampleRate { get; private set; }
|
2018-07-10 03:49:07 +02:00
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
public ALFormat Format { get; private set; }
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
private ReleaseCallback Callback;
|
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
public PlaybackState State { get; set; }
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
private bool ShouldCallReleaseCallback;
|
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
private ConcurrentDictionary<long, int> Buffers;
|
|
|
|
|
|
|
|
private Queue<long> QueuedTagsQueue;
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
private Queue<long> ReleasedTagsQueue;
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
private int LastReleasedCount;
|
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
private bool Disposed;
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
public Track(int SampleRate, ALFormat Format, ReleaseCallback Callback)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
this.SampleRate = SampleRate;
|
|
|
|
this.Format = Format;
|
2018-03-19 19:58:46 +01:00
|
|
|
this.Callback = Callback;
|
2018-03-16 01:06:24 +01:00
|
|
|
|
|
|
|
State = PlaybackState.Stopped;
|
|
|
|
|
|
|
|
SourceId = AL.GenSource();
|
|
|
|
|
|
|
|
Buffers = new ConcurrentDictionary<long, int>();
|
|
|
|
|
|
|
|
QueuedTagsQueue = new Queue<long>();
|
2018-03-16 04:42:44 +01:00
|
|
|
|
|
|
|
ReleasedTagsQueue = new Queue<long>();
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public bool ContainsBuffer(long Tag)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
SyncQueuedTags();
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
foreach (long QueuedTag in QueuedTagsQueue)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
if (QueuedTag == Tag)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
return false;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public long[] GetReleasedBuffers(int MaxCount)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
ClearReleased();
|
|
|
|
|
|
|
|
List<long> Tags = new List<long>();
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
HashSet<long> Unique = new HashSet<long>();
|
|
|
|
|
|
|
|
while (MaxCount-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
if (Unique.Add(Tag))
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
Tags.Add(Tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Tags.ToArray();
|
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public int AppendBuffer(long Tag)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Track));
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
int Id = AL.GenBuffer();
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
AL.DeleteBuffer(OldId);
|
|
|
|
|
|
|
|
return Id;
|
|
|
|
});
|
|
|
|
|
|
|
|
QueuedTagsQueue.Enqueue(Tag);
|
|
|
|
|
|
|
|
return Id;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public void ClearReleased()
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
SyncQueuedTags();
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
CheckReleaseChanges(ReleasedCount);
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
if (ReleasedCount > 0)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
AL.SourceUnqueueBuffers(SourceId, ReleasedCount);
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
public void CallReleaseCallbackIfNeeded()
|
|
|
|
{
|
|
|
|
CheckReleaseChanges();
|
|
|
|
|
|
|
|
if (ShouldCallReleaseCallback)
|
|
|
|
{
|
|
|
|
ShouldCallReleaseCallback = false;
|
|
|
|
|
|
|
|
Callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckReleaseChanges()
|
|
|
|
{
|
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
CheckReleaseChanges(ReleasedCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckReleaseChanges(int NewReleasedCount)
|
|
|
|
{
|
|
|
|
if (LastReleasedCount != NewReleasedCount)
|
|
|
|
{
|
|
|
|
LastReleasedCount = NewReleasedCount;
|
|
|
|
|
|
|
|
ShouldCallReleaseCallback = true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 03:49:07 +02:00
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
private void SyncQueuedTags()
|
|
|
|
{
|
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersQueued, out int QueuedCount);
|
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
QueuedCount -= ReleasedCount;
|
|
|
|
|
|
|
|
while (QueuedTagsQueue.Count > QueuedCount)
|
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
ReleasedTagsQueue.Enqueue(QueuedTagsQueue.Dequeue());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ReleasedTagsQueue.Count > MaxReleased)
|
|
|
|
{
|
|
|
|
ReleasedTagsQueue.Dequeue();
|
2018-03-16 01:06:24 +01: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 19:58:46 +01:00
|
|
|
private Thread AudioPollerThread;
|
|
|
|
|
|
|
|
private bool KeepPolling;
|
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
public OpenALAudioOut()
|
|
|
|
{
|
|
|
|
Context = new AudioContext();
|
|
|
|
|
|
|
|
Tracks = new ConcurrentDictionary<int, Track>();
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
KeepPolling = true;
|
|
|
|
|
|
|
|
AudioPollerThread = new Thread(AudioPollerWork);
|
|
|
|
|
|
|
|
AudioPollerThread.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AudioPollerWork()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
foreach (Track Td in Tracks.Values)
|
|
|
|
{
|
|
|
|
Td.CallReleaseCallbackIfNeeded();
|
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
//If it's not slept it will waste cycles.
|
2018-08-01 05:48:49 +02:00
|
|
|
Thread.Sleep(10);
|
2018-03-19 19:58:46 +01:00
|
|
|
}
|
|
|
|
while (KeepPolling);
|
2018-08-17 01:47:36 +02:00
|
|
|
|
|
|
|
foreach (Track Td in Tracks.Values)
|
|
|
|
{
|
|
|
|
Td.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tracks.Clear();
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-07-15 04:57:41 +02:00
|
|
|
public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-07-15 04:57:41 +02:00
|
|
|
Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
|
2018-03-16 01:06:24 +01:00
|
|
|
|
|
|
|
for (int Id = 0; Id < MaxTracks; Id++)
|
|
|
|
{
|
|
|
|
if (Tracks.TryAdd(Id, Td))
|
|
|
|
{
|
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-15 04:57:41 +02:00
|
|
|
private ALFormat GetALFormat(int Channels)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-07-15 04:57:41 +02:00
|
|
|
switch (Channels)
|
2018-07-10 03:49:07 +02:00
|
|
|
{
|
2018-07-15 04:57:41 +02:00
|
|
|
case 1: return ALFormat.Mono16;
|
|
|
|
case 2: return ALFormat.Stereo16;
|
|
|
|
case 6: return ALFormat.Multi51Chn16Ext;
|
2018-07-10 03:49:07 +02:00
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-07-15 04:57:41 +02:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(Channels));
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CloseTrack(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryRemove(Track, out Track Td))
|
|
|
|
{
|
|
|
|
Td.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public bool ContainsBuffer(int Track, long Tag)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
return Td.ContainsBuffer(Tag);
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
2018-07-10 03:49:07 +02:00
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
return false;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public long[] GetReleasedBuffers(int Track, int MaxCount)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
return Td.GetReleasedBuffers(MaxCount);
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
2018-07-10 03:49:07 +02:00
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
return null;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-07-15 04:57:41 +02:00
|
|
|
public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
int BufferId = Td.AppendBuffer(Tag);
|
|
|
|
|
2018-07-15 04:57:41 +02:00
|
|
|
int Size = Buffer.Length * Marshal.SizeOf<T>();
|
|
|
|
|
|
|
|
AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
|
2018-03-16 04:42:44 +01:00
|
|
|
|
|
|
|
AL.SourceQueueBuffer(Td.SourceId, BufferId);
|
|
|
|
|
|
|
|
StartPlaybackIfNeeded(Td);
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track 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)
|
|
|
|
{
|
|
|
|
Td.ClearReleased();
|
|
|
|
|
|
|
|
AL.SourcePlay(Td.SourceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track 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;
|
|
|
|
}
|
2018-08-17 01:47:36 +02:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
|
|
|
KeepPolling = false;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|