开发者

Generating a Sine Sweep in C#

开发者 https://www.devze.com 2023-01-26 19:07 出处:网络
I want to generate a sine sweep in C# where I am able to define the start frequency, end frequency, and the duration of the sweep. I\'ve looked at sound libraries such as DirectSo开发者_Go百科und and

I want to generate a sine sweep in C# where I am able to define the start frequency, end frequency, and the duration of the sweep. I've looked at sound libraries such as DirectSo开发者_Go百科und and ASIO that play a buffer. But I haven't been able to figure out how to control the duration of the sweep when the duration of the sweep is long enough to fill more than one buffer due to the buffer size limitation. Any samples or guides would be extremely helpful.


If you are satisfied with an running program without writing it yourself take a look at The Audio Test File Generator.

This small windows EXE is able to generate a linear sine sweep with a given start and end frequency.


If you want to write it by your own, you have to fill the buffer using:

sin(2*pi * f * n/sample_rate)

where

f is the current sine frequency (you want to sweep) in Hz
n is the sample index of the buffer
sample_rate is the sample rate in Hz

An example with f=10Hz.


ulrichb has already stated all necessary information but recently I had to build a sine sweep generator in .Net with C#. It looked cool to me, I'll leave the code here, maybe it will be useful for others.

numberofSamples: Buffer size.

sweepDuration: Time takes to go from low frequency to high frequency.

lowFreq: Start frequency

highFreq: End Frequency

deltaTime: 1 / sampling rate (time taken to take 1 sample)

        float sweepCurrentTime = 0.0f;
        float sweepFrequencyFactor = 0.0f;
        float sweepCurrentCyclePosition = 0.0f;
        float sweepFrequency = 0.0f;

public void generateSineSweep(float[] buffer, int numberOfSamples, int sampleRate, int sweepDuration, float lowFreq, float highFreq)
{
           float deltaTime = 1.0f / sampleRate;
           for (int i = 0; i < numberOfSamples; i++)
                {
                    sweepFrequency = lowFreq + ((highFreq - lowFreq) * sweepFrequencyFactor);

                    sweepCurrentCyclePosition += sweepFrequency / sampleRate;

                    buffer[i] = Convert.ToSingle(0.25f * Math.Sin(sweepCurrentCyclePosition * 2 * Math.PI));

                    if (sweepCurrentTime > sweepDuration)
                    {
                        sweepCurrentTime -= sweepDuration;
                        sweepCurrentTime += deltaTime;
                        sweepFrequencyFactor = 0.0f;
                    }
                    else
                    {
                        sweepCurrentTime += deltaTime;
                        sweepFrequencyFactor = sweepCurrentTime / sweepDuration; 
                    }

                }
}

The function progresses from low frequency to high frequency increasing the frequency by a certain amount after each sample.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号