2020-08-18 03:49:37 +02:00
|
|
|
//
|
2021-01-01 00:10:44 +01:00
|
|
|
// Copyright (c) 2019-2021 Ryujinx
|
2020-08-18 03:49:37 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
using Ryujinx.Audio.Common;
|
2020-08-18 03:49:37 +02:00
|
|
|
using Ryujinx.Audio.Renderer.Common;
|
|
|
|
using Ryujinx.Audio.Renderer.Dsp.State;
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-12-02 00:23:43 +01:00
|
|
|
using Ryujinx.Memory;
|
2020-08-18 03:49:37 +02:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Runtime.Intrinsics;
|
2021-03-02 23:50:46 +01:00
|
|
|
using System.Runtime.Intrinsics.Arm;
|
2020-08-18 03:49:37 +02:00
|
|
|
using System.Runtime.Intrinsics.X86;
|
|
|
|
using static Ryujinx.Audio.Renderer.Parameter.VoiceInParameter;
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Renderer.Dsp
|
|
|
|
{
|
|
|
|
public static class DataSourceHelper
|
|
|
|
{
|
|
|
|
private const int FixedPointPrecision = 15;
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
public struct WaveBufferInformation
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
public uint SourceSampleRate;
|
|
|
|
public float Pitch;
|
|
|
|
public ulong ExtraParameter;
|
|
|
|
public ulong ExtraParameterSize;
|
|
|
|
public int ChannelIndex;
|
|
|
|
public int ChannelCount;
|
2021-07-18 13:05:11 +02:00
|
|
|
public DecodingBehaviour DecodingBehaviour;
|
2020-08-18 03:49:37 +02:00
|
|
|
public SampleRateConversionQuality SrcQuality;
|
2021-07-18 13:05:11 +02:00
|
|
|
public SampleFormat SampleFormat;
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
private static int GetPitchLimitBySrcQuality(SampleRateConversionQuality quality)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
return quality switch
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
SampleRateConversionQuality.Default or SampleRateConversionQuality.Low => 4,
|
|
|
|
SampleRateConversionQuality.High => 8,
|
|
|
|
_ => throw new ArgumentException(quality.ToString()),
|
|
|
|
};
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
public static void ProcessWaveBuffers(IVirtualMemoryManager memoryManager, Span<float> outputBuffer, ref WaveBufferInformation info, Span<WaveBuffer> wavebuffers, ref VoiceUpdateState voiceState, uint targetSampleRate, int sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
const int tempBufferSize = 0x3F00;
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
Span<short> tempBuffer = stackalloc short[tempBufferSize];
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
float sampleRateRatio = (float)info.SourceSampleRate / targetSampleRate * info.Pitch;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
float fraction = voiceState.Fraction;
|
|
|
|
int waveBufferIndex = (int)voiceState.WaveBufferIndex;
|
|
|
|
ulong playedSampleCount = voiceState.PlayedSampleCount;
|
|
|
|
int offset = voiceState.Offset;
|
|
|
|
uint waveBufferConsumed = voiceState.WaveBufferConsumed;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
int pitchMaxLength = GetPitchLimitBySrcQuality(info.SrcQuality);
|
|
|
|
|
|
|
|
int totalNeededSize = (int)MathF.Truncate(fraction + sampleRateRatio * sampleCount);
|
|
|
|
|
|
|
|
if (totalNeededSize + pitchMaxLength <= tempBufferSize && totalNeededSize >= 0)
|
|
|
|
{
|
|
|
|
int sourceSampleCountToProcess = sampleCount;
|
|
|
|
|
|
|
|
int maxSampleCountPerIteration = Math.Min((int)MathF.Truncate((tempBufferSize - fraction) / sampleRateRatio), sampleCount);
|
|
|
|
|
|
|
|
bool isStarving = false;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < sourceSampleCountToProcess)
|
|
|
|
{
|
|
|
|
int tempBufferIndex = 0;
|
|
|
|
|
|
|
|
if (!info.DecodingBehaviour.HasFlag(DecodingBehaviour.SkipPitchAndSampleRateConversion))
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.Pitch.ToSpan().Slice(0, pitchMaxLength).CopyTo(tempBuffer);
|
2020-08-18 03:49:37 +02:00
|
|
|
tempBufferIndex += pitchMaxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sampleCountToProcess = Math.Min(sourceSampleCountToProcess, maxSampleCountPerIteration);
|
|
|
|
|
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
int sampleCountToDecode = (int)MathF.Truncate(fraction + sampleRateRatio * sampleCountToProcess);
|
|
|
|
|
|
|
|
while (y < sampleCountToDecode)
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
if (waveBufferIndex >= Constants.VoiceWaveBufferCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
waveBufferIndex = 0;
|
|
|
|
playedSampleCount = 0;
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
if (!voiceState.IsWaveBufferValid[waveBufferIndex])
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
isStarving = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
ref WaveBuffer waveBuffer = ref wavebuffers[waveBufferIndex];
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
if (offset == 0 && info.SampleFormat == SampleFormat.Adpcm && waveBuffer.Context != 0)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.LoopContext = memoryManager.Read<AdpcmLoopContext>(waveBuffer.Context);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
Span<short> tempSpan = tempBuffer.Slice(tempBufferIndex + y);
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
int decodedSampleCount = -1;
|
|
|
|
|
|
|
|
int targetSampleStartOffset;
|
|
|
|
int targetSampleEndOffset;
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
if (voiceState.LoopCount > 0 && waveBuffer.LoopStartSampleOffset != 0 && waveBuffer.LoopEndSampleOffset != 0 && waveBuffer.LoopStartSampleOffset <= waveBuffer.LoopEndSampleOffset)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
targetSampleStartOffset = (int)waveBuffer.LoopStartSampleOffset;
|
|
|
|
targetSampleEndOffset = (int)waveBuffer.LoopEndSampleOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
targetSampleStartOffset = (int)waveBuffer.StartSampleOffset;
|
|
|
|
targetSampleEndOffset = (int)waveBuffer.EndSampleOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int targetWaveBufferSampleCount = targetSampleEndOffset - targetSampleStartOffset;
|
|
|
|
|
|
|
|
switch (info.SampleFormat)
|
|
|
|
{
|
|
|
|
case SampleFormat.Adpcm:
|
|
|
|
ReadOnlySpan<byte> waveBufferAdpcm = ReadOnlySpan<byte>.Empty;
|
|
|
|
|
|
|
|
if (waveBuffer.Buffer != 0 && waveBuffer.BufferSize != 0)
|
|
|
|
{
|
|
|
|
// TODO: we are possibly copying a lot of unneeded data here, we should only take what we need.
|
|
|
|
waveBufferAdpcm = memoryManager.GetSpan(waveBuffer.Buffer, (int)waveBuffer.BufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadOnlySpan<short> coefficients = MemoryMarshal.Cast<byte, short>(memoryManager.GetSpan(info.ExtraParameter, (int)info.ExtraParameterSize));
|
2021-07-18 13:05:11 +02:00
|
|
|
decodedSampleCount = AdpcmHelper.Decode(tempSpan, waveBufferAdpcm, targetSampleStartOffset, targetSampleEndOffset, offset, sampleCountToDecode - y, coefficients, ref voiceState.LoopContext);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
case SampleFormat.PcmInt16:
|
|
|
|
ReadOnlySpan<short> waveBufferPcm16 = ReadOnlySpan<short>.Empty;
|
|
|
|
|
|
|
|
if (waveBuffer.Buffer != 0 && waveBuffer.BufferSize != 0)
|
|
|
|
{
|
|
|
|
ulong bufferOffset = waveBuffer.Buffer + PcmHelper.GetBufferOffset<short>(targetSampleStartOffset, offset, info.ChannelCount);
|
|
|
|
int bufferSize = PcmHelper.GetBufferSize<short>(targetSampleStartOffset, targetSampleEndOffset, offset, sampleCountToDecode - y) * info.ChannelCount;
|
|
|
|
|
|
|
|
waveBufferPcm16 = MemoryMarshal.Cast<byte, short>(memoryManager.GetSpan(bufferOffset, bufferSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedSampleCount = PcmHelper.Decode(tempSpan, waveBufferPcm16, targetSampleStartOffset, targetSampleEndOffset, info.ChannelIndex, info.ChannelCount);
|
|
|
|
break;
|
|
|
|
case SampleFormat.PcmFloat:
|
|
|
|
ReadOnlySpan<float> waveBufferPcmFloat = ReadOnlySpan<float>.Empty;
|
|
|
|
|
|
|
|
if (waveBuffer.Buffer != 0 && waveBuffer.BufferSize != 0)
|
|
|
|
{
|
|
|
|
ulong bufferOffset = waveBuffer.Buffer + PcmHelper.GetBufferOffset<float>(targetSampleStartOffset, offset, info.ChannelCount);
|
|
|
|
int bufferSize = PcmHelper.GetBufferSize<float>(targetSampleStartOffset, targetSampleEndOffset, offset, sampleCountToDecode - y) * info.ChannelCount;
|
|
|
|
|
|
|
|
waveBufferPcmFloat = MemoryMarshal.Cast<byte, float>(memoryManager.GetSpan(bufferOffset, bufferSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedSampleCount = PcmHelper.Decode(tempSpan, waveBufferPcmFloat, targetSampleStartOffset, targetSampleEndOffset, info.ChannelIndex, info.ChannelCount);
|
|
|
|
break;
|
|
|
|
default:
|
2021-07-18 13:05:11 +02:00
|
|
|
Logger.Error?.Print(LogClass.AudioRenderer, $"Unsupported sample format " + info.SampleFormat);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(decodedSampleCount <= sampleCountToDecode);
|
|
|
|
|
|
|
|
if (decodedSampleCount < 0)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
Logger.Warning?.Print(LogClass.AudioRenderer, "Decoding failed, skipping WaveBuffer");
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.MarkEndOfBufferWaveBufferProcessing(ref waveBuffer, ref waveBufferIndex, ref waveBufferConsumed, ref playedSampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
decodedSampleCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
y += decodedSampleCount;
|
|
|
|
offset += decodedSampleCount;
|
|
|
|
playedSampleCount += (uint)decodedSampleCount;
|
|
|
|
|
|
|
|
if (offset >= targetWaveBufferSampleCount || decodedSampleCount == 0)
|
|
|
|
{
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
if (waveBuffer.Looping)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.LoopCount++;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
if (waveBuffer.LoopCount >= 0)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
if (decodedSampleCount == 0 || voiceState.LoopCount > waveBuffer.LoopCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.MarkEndOfBufferWaveBufferProcessing(ref waveBuffer, ref waveBufferIndex, ref waveBufferConsumed, ref playedSampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decodedSampleCount == 0)
|
|
|
|
{
|
|
|
|
isStarving = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.DecodingBehaviour.HasFlag(DecodingBehaviour.PlayedSampleCountResetWhenLooping))
|
|
|
|
{
|
|
|
|
playedSampleCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.MarkEndOfBufferWaveBufferProcessing(ref waveBuffer, ref waveBufferIndex, ref waveBufferConsumed, ref playedSampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
Span<int> outputSpanInt = MemoryMarshal.Cast<float, int>(outputBuffer.Slice(i));
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
if (info.DecodingBehaviour.HasFlag(DecodingBehaviour.SkipPitchAndSampleRateConversion))
|
|
|
|
{
|
|
|
|
for (int j = 0; j < y; j++)
|
|
|
|
{
|
|
|
|
outputBuffer[j] = tempBuffer[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
Span<short> tempSpan = tempBuffer.Slice(tempBufferIndex + y);
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
tempSpan.Slice(0, sampleCountToDecode - y).Fill(0);
|
|
|
|
|
|
|
|
ToFloat(outputBuffer, outputSpanInt, sampleCountToProcess);
|
|
|
|
|
|
|
|
ResamplerHelper.Resample(outputBuffer, tempBuffer, sampleRateRatio, ref fraction, sampleCountToProcess, info.SrcQuality, y != sourceSampleCountToProcess || info.Pitch != 1.0f);
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
tempBuffer.Slice(sampleCountToDecode, pitchMaxLength).CopyTo(voiceState.Pitch.ToSpan());
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
i += sampleCountToProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(sourceSampleCountToProcess == i || !isStarving);
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
voiceState.WaveBufferConsumed = waveBufferConsumed;
|
|
|
|
voiceState.Offset = offset;
|
|
|
|
voiceState.PlayedSampleCount = playedSampleCount;
|
|
|
|
voiceState.WaveBufferIndex = (uint)waveBufferIndex;
|
|
|
|
voiceState.Fraction = fraction;
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
private static void ToFloatAvx(Span<float> output, ReadOnlySpan<int> input, int sampleCount)
|
|
|
|
{
|
|
|
|
ReadOnlySpan<Vector256<int>> inputVec = MemoryMarshal.Cast<int, Vector256<int>>(input);
|
|
|
|
Span<Vector256<float>> outputVec = MemoryMarshal.Cast<float, Vector256<float>>(output);
|
|
|
|
|
|
|
|
int sisdStart = inputVec.Length * 8;
|
|
|
|
|
|
|
|
for (int i = 0; i < inputVec.Length; i++)
|
|
|
|
{
|
|
|
|
outputVec[i] = Avx.ConvertToVector256Single(inputVec[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = sisdStart; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
private static void ToFloatSse2(Span<float> output, ReadOnlySpan<int> input, int sampleCount)
|
|
|
|
{
|
|
|
|
ReadOnlySpan<Vector128<int>> inputVec = MemoryMarshal.Cast<int, Vector128<int>>(input);
|
|
|
|
Span<Vector128<float>> outputVec = MemoryMarshal.Cast<float, Vector128<float>>(output);
|
|
|
|
|
|
|
|
int sisdStart = inputVec.Length * 4;
|
|
|
|
|
|
|
|
for (int i = 0; i < inputVec.Length; i++)
|
|
|
|
{
|
|
|
|
outputVec[i] = Sse2.ConvertToVector128Single(inputVec[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = sisdStart; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-03-02 23:50:46 +01:00
|
|
|
private static void ToFloatAdvSimd(Span<float> output, ReadOnlySpan<int> input, int sampleCount)
|
|
|
|
{
|
|
|
|
ReadOnlySpan<Vector128<int>> inputVec = MemoryMarshal.Cast<int, Vector128<int>>(input);
|
|
|
|
Span<Vector128<float>> outputVec = MemoryMarshal.Cast<float, Vector128<float>>(output);
|
|
|
|
|
|
|
|
int sisdStart = inputVec.Length * 4;
|
|
|
|
|
|
|
|
for (int i = 0; i < inputVec.Length; i++)
|
|
|
|
{
|
|
|
|
outputVec[i] = AdvSimd.ConvertToSingle(inputVec[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = sisdStart; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 03:49:37 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static void ToFloatSlow(Span<float> output, ReadOnlySpan<int> input, int sampleCount)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
public static void ToFloat(Span<float> output, ReadOnlySpan<int> input, int sampleCount)
|
|
|
|
{
|
|
|
|
if (Avx.IsSupported)
|
|
|
|
{
|
|
|
|
ToFloatAvx(output, input, sampleCount);
|
|
|
|
}
|
|
|
|
else if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
ToFloatSse2(output, input, sampleCount);
|
|
|
|
}
|
2021-03-02 23:50:46 +01:00
|
|
|
else if (AdvSimd.IsSupported)
|
|
|
|
{
|
|
|
|
ToFloatAdvSimd(output, input, sampleCount);
|
|
|
|
}
|
2020-08-18 03:49:37 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ToFloatSlow(output, input, sampleCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
public static void ToIntAvx(Span<int> output, ReadOnlySpan<float> input, int sampleCount)
|
|
|
|
{
|
|
|
|
ReadOnlySpan<Vector256<float>> inputVec = MemoryMarshal.Cast<float, Vector256<float>>(input);
|
|
|
|
Span<Vector256<int>> outputVec = MemoryMarshal.Cast<int, Vector256<int>>(output);
|
|
|
|
|
|
|
|
int sisdStart = inputVec.Length * 8;
|
|
|
|
|
|
|
|
for (int i = 0; i < inputVec.Length; i++)
|
|
|
|
{
|
|
|
|
outputVec[i] = Avx.ConvertToVector256Int32(inputVec[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = sisdStart; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = (int)input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
public static void ToIntSse2(Span<int> output, ReadOnlySpan<float> input, int sampleCount)
|
|
|
|
{
|
|
|
|
ReadOnlySpan<Vector128<float>> inputVec = MemoryMarshal.Cast<float, Vector128<float>>(input);
|
|
|
|
Span<Vector128<int>> outputVec = MemoryMarshal.Cast<int, Vector128<int>>(output);
|
|
|
|
|
|
|
|
int sisdStart = inputVec.Length * 4;
|
|
|
|
|
|
|
|
for (int i = 0; i < inputVec.Length; i++)
|
|
|
|
{
|
2021-03-02 23:50:46 +01:00
|
|
|
outputVec[i] = Sse2.ConvertToVector128Int32(inputVec[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = sisdStart; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = (int)input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2021-03-02 23:50:46 +01:00
|
|
|
public static void ToIntAdvSimd(Span<int> output, ReadOnlySpan<float> input, int sampleCount)
|
|
|
|
{
|
|
|
|
ReadOnlySpan<Vector128<float>> inputVec = MemoryMarshal.Cast<float, Vector128<float>>(input);
|
|
|
|
Span<Vector128<int>> outputVec = MemoryMarshal.Cast<int, Vector128<int>>(output);
|
|
|
|
|
|
|
|
int sisdStart = inputVec.Length * 4;
|
|
|
|
|
|
|
|
for (int i = 0; i < inputVec.Length; i++)
|
|
|
|
{
|
|
|
|
outputVec[i] = AdvSimd.ConvertToInt32RoundToZero(inputVec[i]);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = sisdStart; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = (int)input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static void ToIntSlow(Span<int> output, ReadOnlySpan<float> input, int sampleCount)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
output[i] = (int)input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-08-18 03:49:37 +02:00
|
|
|
public static void ToInt(Span<int> output, ReadOnlySpan<float> input, int sampleCount)
|
|
|
|
{
|
|
|
|
if (Avx.IsSupported)
|
|
|
|
{
|
|
|
|
ToIntAvx(output, input, sampleCount);
|
|
|
|
}
|
|
|
|
else if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
ToIntSse2(output, input, sampleCount);
|
|
|
|
}
|
2021-03-02 23:50:46 +01:00
|
|
|
else if (AdvSimd.IsSupported)
|
|
|
|
{
|
|
|
|
ToIntAdvSimd(output, input, sampleCount);
|
|
|
|
}
|
2020-08-18 03:49:37 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ToIntSlow(output, input, sampleCount);
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 09:12:38 +02:00
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static void RemapLegacyChannelEffectMappingToChannelResourceMapping(bool isSupported, Span<ushort> bufferIndices)
|
|
|
|
{
|
|
|
|
if (!isSupported && bufferIndices.Length == 6)
|
|
|
|
{
|
|
|
|
ushort backLeft = bufferIndices[2];
|
|
|
|
ushort backRight = bufferIndices[3];
|
|
|
|
ushort frontCenter = bufferIndices[4];
|
|
|
|
ushort lowFrequency = bufferIndices[5];
|
|
|
|
|
|
|
|
bufferIndices[2] = frontCenter;
|
|
|
|
bufferIndices[3] = lowFrequency;
|
|
|
|
bufferIndices[4] = backLeft;
|
|
|
|
bufferIndices[5] = backRight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static void RemapChannelResourceMappingToLegacy(bool isSupported, Span<ushort> bufferIndices)
|
|
|
|
{
|
|
|
|
if (isSupported && bufferIndices.Length == 6)
|
|
|
|
{
|
|
|
|
ushort frontCenter = bufferIndices[2];
|
|
|
|
ushort lowFrequency = bufferIndices[3];
|
|
|
|
ushort backLeft = bufferIndices[4];
|
|
|
|
ushort backRight = bufferIndices[5];
|
|
|
|
|
|
|
|
bufferIndices[2] = backLeft;
|
|
|
|
bufferIndices[3] = backRight;
|
|
|
|
bufferIndices[4] = frontCenter;
|
|
|
|
bufferIndices[5] = lowFrequency;
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|