拙网论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 260|回复: 0

NAudio Sine Generator with Portamento

[复制链接]

949

主题

1001

帖子

3736

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3736
发表于 2018-11-27 16:57:23 | 显示全部楼层 |阅读模式
https://markheath.net/post/naudio-sine-portamento


NAudio Sine Generator with Portamento


May 13. 2016

Here’s a code sample I’ve been meaning to share on my blog for years. NAudio already has a built-in SignalGenerator class which can generate sine waves as well as various other waveforms. But what if you want to implement “portamento” to glide smoothly between frequencies?
One simple way to do this is to make use of a “wavetable”. Basically we store one cycle of a sine wave in a memory buffer, and then to work out what the next sample to be played is, we move forward a certain number of slots in that buffer and read out the value. If we go past the end of the buffer we simply start again.
If we call the current position in the waveform the “phase” and the number of steps in the wavetable we must move forward the “phase step”, then when the target frequency is changed, instead of immediately recalculating a new “phase step”, we slowly change the current phase step until it reaches the desired phase step.
It’s quite a simple technique, and the great thing is that it works for any waveform, so you could quite easily do the same with square or sawtooth waveforms.
Here’s a really basic implementation of this, which allows you to customise the portamento time. I meant for this setting to be in seconds, but I think I’ve got it slightly wrong as when you set it to 1.0 it seems to take longer than a second to reach the target frequency.

I’ve packaged this up into a very simple WPF application available on GitHub for you to try out. One word of warning, sine waves can be ear piercing so start off with the volume set very low before trying this out.
Please note, my wavetable code is extremely rudimentary. If you want to go into the science behind doing this properly, make sure to check out Nigel Redmon’s excellent wavetable series at Ear Level Engineering. This involves creating multiple wavetables to cover different frequency ranges and using linear interpolation (I just truncate the phase to the nearest array entry).

Want to get up to speed with the the fundamentals principles of digital audio and how to got about writing audio applications with NAudio? Be sure to check out my Pluralsight courses, Digital Audio Fundamentals, and Audio Programming with NAudio.



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|抱朴守拙BBS

GMT+8, 2025-5-26 03:45 , Processed in 0.197251 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表