2018-03-15 20:06:24 -04:00
|
|
|
using OpenTK.Audio;
|
|
|
|
using OpenTK.Audio.OpenAL;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.OpenAL
|
|
|
|
{
|
|
|
|
public class OpenALAudioOut : IAalOutput
|
|
|
|
{
|
|
|
|
private const int MaxTracks = 256;
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
private const int MaxReleased = 32;
|
|
|
|
|
2018-03-15 20:06:24 -04: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; }
|
|
|
|
|
|
|
|
public PlaybackState State { get; set; }
|
|
|
|
|
|
|
|
private ConcurrentDictionary<long, int> Buffers;
|
|
|
|
|
|
|
|
private Queue<long> QueuedTagsQueue;
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
private Queue<long> ReleasedTagsQueue;
|
|
|
|
|
2018-03-15 20:06:24 -04:00
|
|
|
private bool Disposed;
|
|
|
|
|
|
|
|
public Track(int SampleRate, ALFormat Format)
|
|
|
|
{
|
|
|
|
this.SampleRate = SampleRate;
|
|
|
|
this.Format = Format;
|
|
|
|
|
|
|
|
State = PlaybackState.Stopped;
|
|
|
|
|
|
|
|
SourceId = AL.GenSource();
|
|
|
|
|
|
|
|
Buffers = new ConcurrentDictionary<long, int>();
|
|
|
|
|
|
|
|
QueuedTagsQueue = new Queue<long>();
|
2018-03-15 23:42:44 -04:00
|
|
|
|
|
|
|
ReleasedTagsQueue = new Queue<long>();
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
public bool ContainsBuffer(long Tag)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
SyncQueuedTags();
|
2018-03-15 20:06:24 -04:00
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
foreach (long QueuedTag in QueuedTagsQueue)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
if (QueuedTag == Tag)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 20:06:24 -04:00
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
return false;
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
public long[] GetReleasedBuffers(int MaxCount)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
|
|
|
ClearReleased();
|
|
|
|
|
|
|
|
List<long> Tags = new List<long>();
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
HashSet<long> Unique = new HashSet<long>();
|
|
|
|
|
|
|
|
while (MaxCount-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
if (Unique.Add(Tag))
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
|
|
|
Tags.Add(Tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Tags.ToArray();
|
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
public int AppendBuffer(long Tag)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
if (Disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(nameof(Track));
|
|
|
|
}
|
2018-03-15 20:06:24 -04:00
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
int Id = AL.GenBuffer();
|
2018-03-15 20:06:24 -04:00
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
AL.DeleteBuffer(OldId);
|
|
|
|
|
|
|
|
return Id;
|
|
|
|
});
|
|
|
|
|
|
|
|
QueuedTagsQueue.Enqueue(Tag);
|
|
|
|
|
|
|
|
return Id;
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
public void ClearReleased()
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
|
|
|
SyncQueuedTags();
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
if (ReleasedCount > 0)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
AL.SourceUnqueueBuffers(SourceId, ReleasedCount);
|
2018-03-15 20:06:24 -04: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-15 23:42:44 -04:00
|
|
|
ReleasedTagsQueue.Enqueue(QueuedTagsQueue.Dequeue());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ReleasedTagsQueue.Count > MaxReleased)
|
|
|
|
{
|
|
|
|
ReleasedTagsQueue.Dequeue();
|
2018-03-15 20:06:24 -04: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;
|
|
|
|
|
|
|
|
public OpenALAudioOut()
|
|
|
|
{
|
|
|
|
Context = new AudioContext();
|
|
|
|
|
|
|
|
Tracks = new ConcurrentDictionary<int, Track>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int OpenTrack(int SampleRate, int Channels, out AudioFormat Format)
|
|
|
|
{
|
|
|
|
Format = AudioFormat.PcmInt16;
|
|
|
|
|
|
|
|
Track Td = new Track(SampleRate, GetALFormat(Channels, Format));
|
|
|
|
|
|
|
|
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-15 23:42:44 -04:00
|
|
|
public bool ContainsBuffer(int Track, long Tag)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
return Td.ContainsBuffer(Tag);
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
2018-03-15 23:42:44 -04:00
|
|
|
|
|
|
|
return false;
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
public long[] GetReleasedBuffers(int Track, int MaxCount)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-03-15 23:42:44 -04:00
|
|
|
return Td.GetReleasedBuffers(MaxCount);
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
return null;
|
2018-03-15 20:06:24 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:42:44 -04:00
|
|
|
public void AppendBuffer(int Track, long Tag, byte[] Buffer)
|
2018-03-15 20:06:24 -04:00
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-03-15 23:42:44 -04: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-15 20:06:24 -04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|