Skip to content

Commit

Permalink
* Converted increment and multiply and modulo into add and modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
MineCake147E committed Mar 11, 2020
1 parent 8071cd0 commit 2ed171b
Show file tree
Hide file tree
Showing 12 changed files with 1,011 additions and 868 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace MonoAudio.Conversion.Resampling.Sample
{
public sealed partial class SplineResampler
{
internal abstract class InternalResampler
{
private protected abstract void Resample(Span<float> buffer, ref Span<float> srcBuffer);
}
}
}
708 changes: 289 additions & 419 deletions MonoAudio/Conversion/Resampling/Sample/SplineResampler.Resample.cs

Large diffs are not rendered by default.

482 changes: 199 additions & 283 deletions MonoAudio/Conversion/Resampling/Sample/SplineResampler.Resample.tt

Large diffs are not rendered by default.

67 changes: 43 additions & 24 deletions MonoAudio/Conversion/Resampling/Sample/SplineResampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public sealed partial class SplineResampler : ResamplerBase
/// </summary>
private Vector4[] preCalculatedCatmullRomCoefficents;

private int conversionGradient = 0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int AdvanceConversionGradient(ref int conversionGradient)
{
unchecked
{
conversionGradient += RateDiv;
if (conversionGradient >= RateMul)
{
conversionGradient = (int)RateMulDivisor.DivRem((uint)conversionGradient, out var posDiff);
return (int)posDiff;
}
else
{
return 0;
}
}
}

[Obsolete("", true)]
private bool IsCatmullRomOptimized { get; }

Expand Down Expand Up @@ -59,14 +79,7 @@ public SplineResampler(IReadableAudioSource<float, SampleFormat> source, int des
for (int i = 0; i < preCalculatedCatmullRomCoefficents.Length; i++)
{
var x = i * RateMulInverse;
var xP2 = x * x;
var xP3 = xP2 * x;
preCalculatedCatmullRomCoefficents[i] =
new Vector4(
(-xP3 + (2 * xP2) - x) * 0.5f,
((3 * xP3) - (5 * xP2) + 2) * 0.5f,
(-(3 * xP3) + (4 * xP2) + x) * 0.5f,
(xP3 - xP2) * 0.5f);
preCalculatedCatmullRomCoefficents[i] = CalculateCatmullRomCoeffs(x);
}
}
else if (RateMul < 1024)
Expand All @@ -78,14 +91,7 @@ public SplineResampler(IReadableAudioSource<float, SampleFormat> source, int des
for (int i = 0; i < preCalculatedCatmullRomCoefficents.Length; i++)
{
var x = i * RateMulInverse;
var xP2 = x * x;
var xP3 = xP2 * x;
preCalculatedCatmullRomCoefficents[i] =
new Vector4(
(-xP3 + (2 * xP2) - x) * 0.5f,
((3 * xP3) - (5 * xP2) + 2) * 0.5f,
(-(3 * xP3) + (4 * xP2) + x) * 0.5f,
(xP3 - xP2) * 0.5f);
preCalculatedCatmullRomCoefficents[i] = CalculateCatmullRomCoeffs(x);
}
}
else
Expand All @@ -95,20 +101,25 @@ public SplineResampler(IReadableAudioSource<float, SampleFormat> source, int des
for (int i = 0; i < preCalculatedCatmullRomCoefficents.Length; i++)
{
var x = i * RateMulInverse;
var xP2 = x * x;
var xP3 = xP2 * x;
preCalculatedCatmullRomCoefficents[i] =
new Vector4(
(-xP3 + (2 * xP2) - x) * 0.5f,
((3 * xP3) - (5 * xP2) + 2) * 0.5f,
(-(3 * xP3) + (4 * xP2) + x) * 0.5f,
(xP3 - xP2) * 0.5f);
preCalculatedCatmullRomCoefficents[i] = CalculateCatmullRomCoeffs(x);
}
}
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector4 CalculateCatmullRomCoeffs(float x)
{
var xP2 = x * x;
var xP3 = xP2 * x;
return new Vector4(
(-xP3 + (2 * xP2) - x) * 0.5f,
((3 * xP3) - (5 * xP2) + 2) * 0.5f,
(-(3 * xP3) + (4 * xP2) + x) * 0.5f,
(xP3 - xP2) * 0.5f);
}

/// <summary>
/// Reads the audio to the specified buffer.
/// </summary>
Expand Down Expand Up @@ -178,6 +189,14 @@ private void ExpandBuffer(int internalBufferLengthRequired)
a.CopyTo(bufferWrapper.Buffer);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Abs(int value)
{
//TODO: get outside
int mask = value >> 31;
return (uint)((value + mask) ^ mask);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
Expand Down
94 changes: 67 additions & 27 deletions MonoAudio/MonoAudio.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2ed171b

Please sign in to comment.