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-03-19 19:58:46 +01:00
|
|
|
using System.Threading;
|
2018-03-16 01:06:24 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Audio.OpenAL
|
|
|
|
{
|
|
|
|
public class OpenALAudioOut : IAalOutput
|
|
|
|
{
|
|
|
|
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; }
|
|
|
|
|
|
|
|
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-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();
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Yield();
|
|
|
|
}
|
|
|
|
while (KeepPolling);
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
public int OpenTrack(
|
|
|
|
int SampleRate,
|
|
|
|
int Channels,
|
|
|
|
ReleaseCallback Callback,
|
|
|
|
out AudioFormat Format)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
|
|
|
Format = AudioFormat.PcmInt16;
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
Track Td = new Track(SampleRate, GetALFormat(Channels, Format), 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ALFormat GetALFormat(int Channels, AudioFormat Format)
|
|
|
|
{
|
|
|
|
if (Channels < 1 || Channels > 2)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(Channels));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Channels == 1)
|
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
|
|
|
case AudioFormat.PcmInt8: return ALFormat.Mono8;
|
|
|
|
case AudioFormat.PcmInt16: return ALFormat.Mono16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* if (Channels == 2) */
|
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
|
|
|
case AudioFormat.PcmInt8: return ALFormat.Stereo8;
|
|
|
|
case AudioFormat.PcmInt16: return ALFormat.Stereo16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-03-16 04:42:44 +01:00
|
|
|
return null;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
public void AppendBuffer(int Track, long Tag, byte[] Buffer)
|
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);
|
|
|
|
|
|
|
|
AL.BufferData(BufferId, Td.Format, Buffer, Buffer.Length, Td.SampleRate);
|
|
|
|
|
|
|
|
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-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|