Skip to content

Commit

Permalink
Sample rate-based sine wave generation
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Nov 27, 2024
1 parent 89680f7 commit e3b9ff8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Cavern.QuickEQ/SignalGeneration/WaveformGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

using Cavern.Filters;
using Cavern.QuickEQ.Equalization;
Expand All @@ -9,16 +10,27 @@ namespace Cavern.QuickEQ.SignalGeneration {
/// Generates various signals.
/// </summary>
public static class WaveformGenerator {
/// <summary>
/// Generates a sine wave signal.
/// </summary>
/// <param name="frequency">The frequency of the sine wave in periods/<paramref name="length"/></param>
/// <param name="length">The length of the generated signal in samples</param>
/// <remarks>No windowing is used, and a click will be heard if the last period doesn't end at
/// the <paramref name="length"/>.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float[] Sine(float frequency, int length) => Sine(frequency, length, length);

/// <summary>
/// Generates a sine wave signal.
/// </summary>
/// <param name="frequency">The frequency of the sine wave in Hertz</param>
/// <param name="length">The length of the generated signal in samples</param>
/// <param name="sampleRate">Samples per second</param>
/// <remarks>No windowing is used, and a click will be heard if the last period doesn't end at
/// the <paramref name="length"/>.</remarks>
public static float[] Sine(float frequency, int length) {
public static float[] Sine(float frequency, int length, int sampleRate) {
float[] result = new float[length];
float mul = 2 * MathF.PI * frequency / length;
float mul = 2 * MathF.PI * frequency / sampleRate;
for (int i = 0; i < length; i++) {
result[i] = MathF.Sin(mul * i);
}
Expand Down

0 comments on commit e3b9ff8

Please sign in to comment.