2018-07-15 04:57:41 +02:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using Ryujinx.Audio.Adpcm;
|
|
|
|
using System;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Aud.AudioRenderer
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
|
|
|
class VoiceContext
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
private bool Acquired;
|
|
|
|
private bool BufferReload;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private int ResamplerFracPart;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private int BufferIndex;
|
|
|
|
private int Offset;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
|
|
|
public int SampleRate;
|
|
|
|
public int ChannelsCount;
|
|
|
|
|
|
|
|
public float Volume;
|
|
|
|
|
|
|
|
public PlayState PlayState;
|
|
|
|
|
|
|
|
public SampleFormat SampleFormat;
|
|
|
|
|
|
|
|
public AdpcmDecoderContext AdpcmCtx;
|
|
|
|
|
|
|
|
public WaveBuffer[] WaveBuffers;
|
|
|
|
|
|
|
|
public VoiceOut OutStatus;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private int[] Samples;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool Playing => Acquired && PlayState == PlayState.Playing;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
|
|
|
public VoiceContext()
|
|
|
|
{
|
|
|
|
WaveBuffers = new WaveBuffer[4];
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void SetAcquireState(bool NewState)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Acquired && !NewState)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
|
|
|
//Release.
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Acquired = NewState;
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Reset()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
BufferReload = true;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
BufferIndex = 0;
|
|
|
|
Offset = 0;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
|
|
|
OutStatus.PlayedSamplesCount = 0;
|
|
|
|
OutStatus.PlayedWaveBuffersCount = 0;
|
|
|
|
OutStatus.VoiceDropsCount = 0;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public int[] GetBufferData(MemoryManager Memory, int MaxSamples, out int SamplesCount)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
|
|
|
if (!Playing)
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
SamplesCount = 0;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (BufferReload)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
BufferReload = false;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
UpdateBuffer(Memory);
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
WaveBuffer Wb = WaveBuffers[BufferIndex];
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int MaxSize = Samples.Length - Offset;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int Size = MaxSamples * AudioConsts.HostChannelsCount;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Size > MaxSize)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Size = MaxSize;
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int[] Output = new int[Size];
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Array.Copy(Samples, Offset, Output, 0, Size);
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
SamplesCount = Size / AudioConsts.HostChannelsCount;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
OutStatus.PlayedSamplesCount += SamplesCount;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Offset += Size;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Offset == Samples.Length)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Offset = 0;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Wb.Looping == 0)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
SetBufferIndex((BufferIndex + 1) & 3);
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
OutStatus.PlayedWaveBuffersCount++;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Wb.LastBuffer != 0)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
|
|
|
PlayState = PlayState.Paused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Output;
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private void UpdateBuffer(MemoryManager Memory)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
|
|
|
//TODO: Implement conversion for formats other
|
|
|
|
//than interleaved stereo (2 channels).
|
|
|
|
//As of now, it assumes that HostChannelsCount == 2.
|
2018-12-05 01:52:39 +01:00
|
|
|
WaveBuffer Wb = WaveBuffers[BufferIndex];
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Wb.Position == 0)
|
2018-10-21 00:07:52 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Samples = new int[0];
|
2018-10-21 00:07:52 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 04:57:41 +02:00
|
|
|
if (SampleFormat == SampleFormat.PcmInt16)
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int SamplesCount = (int)(Wb.Size / (sizeof(short) * ChannelsCount));
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Samples = new int[SamplesCount * AudioConsts.HostChannelsCount];
|
2018-07-15 04:57:41 +02:00
|
|
|
|
|
|
|
if (ChannelsCount == 1)
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
for (int Index = 0; Index < SamplesCount; Index++)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
short Sample = Memory.ReadInt16(Wb.Position + Index * 2);
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Samples[Index * 2 + 0] = Sample;
|
|
|
|
Samples[Index * 2 + 1] = Sample;
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
for (int Index = 0; Index < SamplesCount * 2; Index++)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Samples[Index] = Memory.ReadInt16(Wb.Position + Index * 2);
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (SampleFormat == SampleFormat.Adpcm)
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
byte[] Buffer = Memory.ReadBytes(Wb.Position, Wb.Size);
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Samples = AdpcmDecoder.Decode(Buffer, AdpcmCtx);
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SampleRate != AudioConsts.HostSampleRate)
|
|
|
|
{
|
|
|
|
//TODO: We should keep the frames being discarded (see the 4 below)
|
|
|
|
//on a buffer and include it on the next samples buffer, to allow
|
|
|
|
//the resampler to do seamless interpolation between wave buffers.
|
2018-12-05 01:52:39 +01:00
|
|
|
int SamplesCount = Samples.Length / AudioConsts.HostChannelsCount;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
SamplesCount = Math.Max(SamplesCount - 4, 0);
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Samples = Resampler.Resample2Ch(
|
|
|
|
Samples,
|
2018-07-15 04:57:41 +02:00
|
|
|
SampleRate,
|
|
|
|
AudioConsts.HostSampleRate,
|
2018-12-05 01:52:39 +01:00
|
|
|
SamplesCount,
|
|
|
|
ref ResamplerFracPart);
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void SetBufferIndex(int Index)
|
2018-07-15 04:57:41 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
BufferIndex = Index & 3;
|
2018-07-15 04:57:41 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
BufferReload = true;
|
2018-07-15 04:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|