Skip to content

Commit

Permalink
Constant filling
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Sep 30, 2018
1 parent 0e0bfed commit fef4fbe
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
4 changes: 2 additions & 2 deletions CavernUnity DLL/AudioSource3D_Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static void WriteOutputApproxCP(float[] Samples, float[] Target, int Ch
/// <summary>Output samples to a multichannel array with constant power.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteOutputCP(float[] Samples, float[] Target, int ChannelLength, float Gain, int Channel, int Channels) {
WriteOutput(Samples, Target, ChannelLength, Mathf.Sin(Gain * CavernUtilities.HalfPi), Channel, Channels);
WriteOutput(Samples, Target, ChannelLength, Mathf.Sin(Mathf.PI / 2 * Gain), Channel, Channels);
}

/// <summary>Output samples to a multichannel array, while trying to fix standing waves.</summary>
Expand Down Expand Up @@ -62,7 +62,7 @@ internal static void WriteFixedOutputApproxCP(float[] Samples, float[] Target, i
/// <summary>Output samples to a multichannel array with constant power, while trying to fix standing waves.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteFixedOutputCP(float[] Samples, float[] Target, int ChannelLength, float Gain, int Channel, int Channels) {
WriteFixedOutput(Samples, Target, ChannelLength, Mathf.Sin(Gain * CavernUtilities.HalfPi), Channel, Channels);
WriteFixedOutput(Samples, Target, ChannelLength, Mathf.Sin(Mathf.PI / 2 * Gain), Channel, Channels);
}
}
}
4 changes: 2 additions & 2 deletions CavernUnity DLL/CavernizeRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void OnAudioFilterRead(float[] data, int channels) {
Height = Mathf.LerpUnclamped(Height, MaxHeight, SmoothFactor);
// Output
float UpperMix = (MaxHeight - BottomSpeakerHeight) / (TopSpeakerHeight - BottomSpeakerHeight),
LowerMix = Mathf.Sin((1f - UpperMix) * CavernUtilities.HalfPi);
UpperMix = Mathf.Sin(UpperMix * CavernUtilities.HalfPi);
LowerMix = Mathf.Sin(Mathf.PI / 2 * (1f - UpperMix));
UpperMix = Mathf.Sin(Mathf.PI / 2 * UpperMix);
int OutputPos = (int)Divert % channels - channels;
Array.Clear(data, 0, Samples);
for (int Sample = 0; Sample < UpdateRate; ++Sample) // Base channel
Expand Down
10 changes: 4 additions & 6 deletions CavernUnity DLL/QuickEQ/Measurements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Cavern.QuickEQ {
/// <summary>Tools for measuring frequency response.</summary>
public static class Measurements {
internal const float Pix2 = Mathf.PI * 2f, NegPix2 = -Pix2;

/// <summary>Fast Fourier transform a real signal.</summary>
public static Complex[] FFT(float[] Samples) {
int Length = Samples.Length;
Expand All @@ -25,7 +23,7 @@ public static Complex[] FFT(Complex[] Samples) {
Odd[Sample] = Samples[Pair++];
}
Complex[] EvenFFT = FFT(Even), OddFFT = FFT(Odd);
float Step = NegPix2 / Length;
float Step = -2 * Mathf.PI / Length;
for (int i = 0; i < HalfLength; ++i)
OddFFT[i].Rotate(Step * i);
for (int i = 0; i < HalfLength; ++i) {
Expand All @@ -46,7 +44,7 @@ static Complex[] ProcessIFFT(Complex[] Samples) {
Odd[Sample] = Samples[Pair++];
}
Complex[] EvenFFT = ProcessIFFT(Even), OddFFT = ProcessIFFT(Odd);
float Step = Pix2 / Length;
float Step = 2 * Mathf.PI / Length;
for (int i = 0; i < HalfLength; ++i)
OddFFT[i].Rotate(Step * i);
for (int i = 0; i < HalfLength; ++i) {
Expand Down Expand Up @@ -108,7 +106,7 @@ public static float[] LinearSweep(float StartFreq, float EndFreq, int Samples, i
float Chirpyness = (EndFreq - StartFreq) / (2 * Samples / (float)SampleRate);
for (int Sample = 0; Sample < Samples; ++Sample) {
float Position = Sample / (float)SampleRate;
Output[Sample] = Mathf.Sin(Pix2 * (StartFreq * Position + Chirpyness * Position * Position));
Output[Sample] = Mathf.Sin(2 * Mathf.PI * (StartFreq * Position + Chirpyness * Position * Position));
}
return Output;
}
Expand All @@ -126,7 +124,7 @@ public static float[] LinearSweepFreqs(float StartFreq, float EndFreq, int Sampl
public static float[] ExponentialSweep(float StartFreq, float EndFreq, int Samples, int SampleRate) {
float[] Output = new float[Samples];
float Chirpyness = Mathf.Pow(EndFreq / StartFreq, SampleRate / (float)Samples),
LogChirpyness = Mathf.Log(Chirpyness), SinConst = Pix2 * StartFreq;
LogChirpyness = Mathf.Log(Chirpyness), SinConst = 2 * Mathf.PI * StartFreq;
for (int Sample = 0; Sample < Samples; ++Sample)
Output[Sample] =
Mathf.Sin(SinConst * (Mathf.Pow(Chirpyness, Sample / (float)SampleRate) - 1) / LogChirpyness);
Expand Down
6 changes: 3 additions & 3 deletions CavernUnity DLL/QuickEQ/Utils/Windowing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void ApplyWindow(float[] Samples, Window Function) {
/// <param name="End">End of the window in samples</param>
public static void ApplyWindow(float[] Samples, Window Left, Window Right, int Start, int Splitter, int End) {
int LeftSpan = Splitter - Start, RightSpan = End - Splitter, EndMirror = Splitter - (End - Splitter), PosSplitter = Math.Max(Splitter, 0);
float LeftSpanDiv = Measurements.Pix2 / (LeftSpan * 2), RightSpanDiv = Measurements.Pix2 / (RightSpan * 2);
float LeftSpanDiv = 2 * Mathf.PI / (LeftSpan * 2), RightSpanDiv = 2 * Mathf.PI / (RightSpan * 2);
if (Left != Window.Disabled) {
WindowFunction LeftFunc = GetWindowFunction(Left);
for (int Sample = 0; Sample < Start; ++Sample)
Expand Down Expand Up @@ -74,7 +74,7 @@ public static void ApplyWindow(Complex[] Samples, Window Function) {
/// <param name="End">End of the window in samples</param>
public static void ApplyWindow(Complex[] Samples, Window Left, Window Right, int Start, int Splitter, int End) {
int LeftSpan = Splitter - Start, RightSpan = End - Splitter, EndMirror = Splitter - (End - Splitter), PosSplitter = Math.Max(Splitter, 0);
float LeftSpanDiv = Measurements.Pix2 / (LeftSpan * 2), RightSpanDiv = Measurements.Pix2 / (RightSpan * 2);
float LeftSpanDiv = 2 * Mathf.PI / (LeftSpan * 2), RightSpanDiv = 2 * Mathf.PI / (RightSpan * 2);
if (Left != Window.Disabled) {
WindowFunction LeftFunc = GetWindowFunction(Left);
for (int Sample = 0; Sample < Start; ++Sample)
Expand Down Expand Up @@ -129,7 +129,7 @@ static float TukeyWindow(float x) {
if (x < FlatLeft)
return (Mathf.Cos(x * Positioner - Mathf.PI) + 1) * .5f;
else if (x > FlatRight)
return (Mathf.Cos((Measurements.Pix2 - x) * Positioner - Mathf.PI) + 1) * .5f;
return (Mathf.Cos((2 * Mathf.PI - x) * Positioner - Mathf.PI) + 1) * .5f;
else
return 1;
}
Expand Down
2 changes: 0 additions & 2 deletions CavernUnity DLL/Utilities/CavernUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public static string Info {
}
}

/// <summary>pi / 2</summary>
internal const float HalfPi = Mathf.PI * .5f;
/// <summary>sqrt(2) / 2</summary>
internal const float Sqrt2p2 = .7071067811f;
/// <summary>sqrt(2) / -2</summary>
Expand Down

0 comments on commit fef4fbe

Please sign in to comment.