2023-12-04 14:17:13 +01:00
|
|
|
using OpenTK.Audio.OpenAL;
|
2021-02-26 01:11:56 +01:00
|
|
|
using Ryujinx.Audio.Backends.Common;
|
|
|
|
using Ryujinx.Audio.Common;
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Backends.OpenAL
|
|
|
|
{
|
|
|
|
class OpenALHardwareDeviceSession : HardwareDeviceSessionOutputBase
|
|
|
|
{
|
2023-06-25 01:29:40 +02:00
|
|
|
private readonly OpenALHardwareDeviceDriver _driver;
|
|
|
|
private readonly int _sourceId;
|
|
|
|
private readonly ALFormat _targetFormat;
|
2021-02-26 01:11:56 +01:00
|
|
|
private bool _isActive;
|
2023-06-25 01:29:40 +02:00
|
|
|
private readonly Queue<OpenALAudioBuffer> _queuedBuffers;
|
2021-02-26 01:11:56 +01:00
|
|
|
private ulong _playedSampleCount;
|
2024-02-22 20:58:33 +01:00
|
|
|
private float _volume;
|
2021-02-26 01:11:56 +01:00
|
|
|
|
2023-06-15 02:34:55 +02:00
|
|
|
private readonly object _lock = new();
|
2021-02-26 01:11:56 +01:00
|
|
|
|
2024-02-22 20:58:33 +01:00
|
|
|
public OpenALHardwareDeviceSession(OpenALHardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
|
2021-02-26 01:11:56 +01:00
|
|
|
{
|
|
|
|
_driver = driver;
|
|
|
|
_queuedBuffers = new Queue<OpenALAudioBuffer>();
|
|
|
|
_sourceId = AL.GenSource();
|
|
|
|
_targetFormat = GetALFormat();
|
|
|
|
_isActive = false;
|
|
|
|
_playedSampleCount = 0;
|
2024-02-22 20:58:33 +01:00
|
|
|
SetVolume(1f);
|
2021-02-26 01:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private ALFormat GetALFormat()
|
|
|
|
{
|
2023-06-25 01:29:40 +02:00
|
|
|
return RequestedSampleFormat switch
|
2021-02-26 01:11:56 +01:00
|
|
|
{
|
2023-06-25 01:29:40 +02:00
|
|
|
SampleFormat.PcmInt16 => RequestedChannelCount switch
|
|
|
|
{
|
|
|
|
1 => ALFormat.Mono16,
|
|
|
|
2 => ALFormat.Stereo16,
|
|
|
|
6 => ALFormat.Multi51Chn16Ext,
|
|
|
|
_ => throw new NotImplementedException($"Unsupported channel config {RequestedChannelCount}"),
|
|
|
|
},
|
|
|
|
_ => throw new NotImplementedException($"Unsupported sample format {RequestedSampleFormat}"),
|
|
|
|
};
|
2021-02-26 01:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void PrepareToClose() { }
|
|
|
|
|
|
|
|
private void StartIfNotPlaying()
|
|
|
|
{
|
|
|
|
AL.GetSource(_sourceId, ALGetSourcei.SourceState, out int stateInt);
|
|
|
|
|
|
|
|
ALSourceState State = (ALSourceState)stateInt;
|
|
|
|
|
|
|
|
if (State != ALSourceState.Playing)
|
|
|
|
{
|
|
|
|
AL.SourcePlay(_sourceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void QueueBuffer(AudioBuffer buffer)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2023-06-25 01:29:40 +02:00
|
|
|
OpenALAudioBuffer driverBuffer = new()
|
2021-02-26 01:11:56 +01:00
|
|
|
{
|
|
|
|
DriverIdentifier = buffer.DataPointer,
|
2021-04-14 12:28:43 +02:00
|
|
|
BufferId = AL.GenBuffer(),
|
2023-07-24 18:35:04 +02:00
|
|
|
SampleCount = GetSampleCount(buffer),
|
2021-02-26 01:11:56 +01:00
|
|
|
};
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
AL.BufferData(driverBuffer.BufferId, _targetFormat, buffer.Data, (int)RequestedSampleRate);
|
2021-02-26 01:11:56 +01:00
|
|
|
|
|
|
|
_queuedBuffers.Enqueue(driverBuffer);
|
|
|
|
|
|
|
|
AL.SourceQueueBuffer(_sourceId, driverBuffer.BufferId);
|
|
|
|
|
|
|
|
if (_isActive)
|
|
|
|
{
|
|
|
|
StartIfNotPlaying();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetVolume(float volume)
|
|
|
|
{
|
2024-02-22 20:58:33 +01:00
|
|
|
_volume = volume;
|
|
|
|
|
|
|
|
UpdateMasterVolume(_driver.Volume);
|
2021-02-26 01:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override float GetVolume()
|
|
|
|
{
|
2024-02-22 20:58:33 +01:00
|
|
|
return _volume;
|
|
|
|
}
|
2021-02-26 01:11:56 +01:00
|
|
|
|
2024-02-22 20:58:33 +01:00
|
|
|
public void UpdateMasterVolume(float newVolume)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
AL.Source(_sourceId, ALSourcef.Gain, newVolume * _volume);
|
|
|
|
}
|
2021-02-26 01:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Start()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
_isActive = true;
|
|
|
|
|
|
|
|
StartIfNotPlaying();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Stop()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
SetVolume(0.0f);
|
|
|
|
|
|
|
|
AL.SourceStop(_sourceId);
|
|
|
|
|
|
|
|
_isActive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
public override void UnregisterBuffer(AudioBuffer buffer) { }
|
2021-02-26 01:11:56 +01:00
|
|
|
|
|
|
|
public override bool WasBufferFullyConsumed(AudioBuffer buffer)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
if (!_queuedBuffers.TryPeek(out OpenALAudioBuffer driverBuffer))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return driverBuffer.DriverIdentifier != buffer.DataPointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ulong GetPlayedSampleCount()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return _playedSampleCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Update()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
if (_isActive)
|
|
|
|
{
|
|
|
|
AL.GetSource(_sourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
|
|
|
|
|
|
|
|
if (releasedCount > 0)
|
|
|
|
{
|
|
|
|
int[] bufferIds = new int[releasedCount];
|
|
|
|
|
|
|
|
AL.SourceUnqueueBuffers(_sourceId, releasedCount, bufferIds);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (_queuedBuffers.TryPeek(out OpenALAudioBuffer buffer) && i < bufferIds.Length)
|
|
|
|
{
|
|
|
|
if (buffer.BufferId == bufferIds[i])
|
|
|
|
{
|
|
|
|
_playedSampleCount += buffer.SampleCount;
|
|
|
|
|
|
|
|
_queuedBuffers.TryDequeue(out _);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:48:35 +01:00
|
|
|
Debug.Assert(i == bufferIds.Length, "Unknown buffer ids found!");
|
2021-02-26 01:11:56 +01:00
|
|
|
|
|
|
|
AL.DeleteBuffers(bufferIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
return releasedCount > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
2021-08-04 20:28:33 +02:00
|
|
|
if (disposing && _driver.Unregister(this))
|
2021-02-26 01:11:56 +01:00
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
PrepareToClose();
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
AL.DeleteSource(_sourceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|