在 C# 中生成正弦扫描

发布于 2024-10-03 07:07:23 字数 146 浏览 10 评论 0原文

我想在 C# 中生成正弦扫描,我可以在其中定义起始频率、结束频率和扫描持续时间。我研究过播放缓冲区的声音库,例如 DirectSound 和 ASIO。但由于缓冲区大小限制,当扫描持续时间足够长以填充多个缓冲区时,我无法弄清楚如何控制扫描持续时间。任何示例或指南都会非常有帮助。

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 DirectSound 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2024-10-10 07:07:23

如果您对正在运行的程序感到满意,而无需自己编写程序,请查看音频测试文件生成器

这个小窗口 EXE 能够生成具有给定开始和结束频率的线性正弦扫描。


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

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

其中

f 是当前正弦频率(您想要扫描),以 Hz 为单位
n 是缓冲区的样本索引
sample_rate 是以 Hz 为单位的采样率

f=10Hz 的示例

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.

请持续率性 2024-10-10 07:07:23

ulrichb 已经说明了所有必要的信息,但最近我不得不用 C# 在 .Net 中构建一个正弦扫描发生器。
对我来说看起来很酷,我将把代码留在这里,也许对其他人有用。

numberofSamples:缓冲区大小。

scanDuration:从低频到高频所需的时间。

lowFreq:起始频率

highFreq:结束频率

deltaTime:1 / 采样率(获取 1 个样本所需的时间)

        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; 
                    }

                }
}

该函数从低频进展到高频,在每次采样后将频率增加一定量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文