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; }
|
|
|
|
|
|
|
|
private ConcurrentDictionary<long, int> Buffers;
|
|
|
|
|
|
|
|
private Queue<long> QueuedTagsQueue;
|
|
|
|
|
2018-03-16 04:42:44 +01:00
|
|
|
private Queue<long> ReleasedTagsQueue;
|
|
|
|
|
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
|
|
|
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-09-18 05:12:47 +02:00
|
|
|
public long[] GetReleasedBuffers(int Count)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-09-18 05:12:47 +02:00
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
ReleasedCount += ReleasedTagsQueue.Count;
|
|
|
|
|
|
|
|
if (Count > ReleasedCount)
|
|
|
|
{
|
|
|
|
Count = ReleasedCount;
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
|
|
|
|
List<long> Tags = new List<long>();
|
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
while (Count-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
|
|
|
|
{
|
|
|
|
Tags.Add(Tag);
|
|
|
|
}
|
2018-03-16 04:42:44 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
while (Count-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-09-18 05:12:47 +02:00
|
|
|
AL.SourceUnqueueBuffers(SourceId, 1);
|
|
|
|
|
|
|
|
Tags.Add(Tag);
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-18 05:12:47 +02:00
|
|
|
public void CallReleaseCallbackIfNeeded()
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-03-16 04:42:44 +01:00
|
|
|
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
|
|
|
|
|
|
|
|
if (ReleasedCount > 0)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-09-18 05:12:47 +02: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 19:58:46 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
ReleasedTagsQueue.Enqueue(Tag);
|
|
|
|
}
|
2018-03-19 19:58:46 +01:00
|
|
|
|
|
|
|
Callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-10-12 23:47:53 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.CallReleaseCallbackIfNeeded();
|
|
|
|
}
|
2018-03-19 19:58:46 +01:00
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
{
|
2018-09-18 05:12:47 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.Dispose();
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-18 05:12:47 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
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-09-18 05:12:47 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
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-09-18 05:12:47 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
int BufferId = Td.AppendBuffer(Tag);
|
2018-03-16 04:42:44 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
int Size = Buffer.Length * Marshal.SizeOf<T>();
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
|
2018-03-16 04:42:44 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
AL.SourceQueueBuffer(Td.SourceId, BufferId);
|
2018-03-16 04:42:44 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
StartPlaybackIfNeeded(Td);
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(int Track)
|
|
|
|
{
|
|
|
|
if (Tracks.TryGetValue(Track, out Track Td))
|
|
|
|
{
|
2018-09-18 05:12:47 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.State = PlaybackState.Playing;
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
StartPlaybackIfNeeded(Td);
|
|
|
|
}
|
2018-03-16 01:06:24 +01: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 05:12:47 +02:00
|
|
|
lock (Td)
|
|
|
|
{
|
|
|
|
Td.State = PlaybackState.Stopped;
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-09-18 05:12:47 +02:00
|
|
|
AL.SourceStop(Td.SourceId);
|
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|