n音讯算法发挥正弦波,其频率可被实时平滑地改变 [英] NAudio Algorithm to play a sinewave whose frequency can be changed smoothly in real time

查看:266
本文介绍了n音讯算法发挥正弦波,其频率可被实时平滑地改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经实现这个的博客文章成果有限。

So far, I have implemented the algorithm found on this blog post with limited success.

我的程序的概念是初始化正弦波,然后根据屏幕上的鼠标的位置更改频率 - 移动鼠标和正弦波变得更高,反之亦然(基本上是使用一个特雷门式仪表鼠标)。

The concept of my program is to initialise the sinewave, then change the frequency according to the position of the mouse on screen - move the mouse up and the sine wave gets higher and vice versa (essentially a theremin-type instrument using the mouse).

与我到目前为止所实现的问题是,当正弦波的频率更新,还有一个咔嗒声,这不是提供平滑的频率扫描,使它听起来像有离散频率水平。
我一直在寻找高和低的n音讯论坛和这里,但它似乎并不像有其他人试图做使用n音讯这种东西,或与此有关的任何其他音源 - 所有执行类似的计划同样使用的设备,如Kinect的使用虚拟MIDI布线和现有的软件模块,但我想实现相同的概念,而不依赖外部软件包。

The problem with what I've implemented so far, is that when the frequency of the sine wave is updated, there is an audible click, which instead of providing the smooth frequency sweep, makes it sound like there are discrete frequency levels. I have been searching high and low on NAudio forums and on here, but it doesn't seem like there's anyone else trying to do this kind of thing using NAudio, or for that matter any other sound module - all of the similar programs that perform similarly using equipment like the Kinect use virtual midi cabling and an existing software module, but I would like to implement the same concept without relying on external software packages.

我已经发布我的code有关这一问题的n音讯论坛的这里,正如你所看到的,我通过如下MarkHeath对这里的建议,试图找到一个解决我的问题。

I have posted the section of my code pertaining to this issue on NAudio's forum here and as you can see, I'm following through MarkHeath's recommendation on here to attempt to find a solution to my problem.

推荐答案

您需要避免输出波形(这是你听到的点击次数)的不连续性。要做到这一点,最简单的方法是使用一个基于LUT的波形发生器 - 这适用于任何周期性波形(即,不只是单纯的正弦波)。通常使用一个固定的点相位累加器,其被增量由对应于电流的输出频率的增量值的每个新样品。您可以安全地修改三角洲但是你喜欢,波形仍然会持续。

You need to avoid discontinuities in the output waveform (these are the clicks you are hearing). The easiest way to do this is with a LUT-based waveform generator - this works for any periodic waveform (i.e. not just pure sine waves). Typically you use a fixed point phase accumulator, which is incremented for each new sample by a delta value which corresponds to the current output frequency. You can safely modify delta however you like and the waveform will still be continuous.

伪code(一个输出采样):

Pseudo code (for one output sample):

const int LUT_SIZE;
const int LUT[LUT_SIZE];  // waveform lookup table (typically 2^N, N >= 8)
Fixed index; // current index into LUT (integer + fraction)
Fixed delta; // delta controls output frequency

output_value = LUT[(int)index];
    // NB: for higher quality use the fractional part of index to interpolate
    //     between LUT[(int)index] and LUT[(int)index + 1], rather than just
    //     truncating the index and using LUT[(int)index] only. Depends on both
    //     LUT_SIZE and required output quality.
index = (index + delta) % LUT_SIZE;



注:计算三角洲对于给定的输出频率˚F和采样率 FS


Note: to calculate delta for a given output frequency f and a sample rate Fs:

delta = FloatToFixed(LUT_SIZE * f / Fs);

这篇关于n音讯算法发挥正弦波,其频率可被实时平滑地改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆