.NET 中的精确计时
我刚刚看到这个问题< /a>,其中答案之一表示 System.Diagnostics.Stopwatch 应该仅用于诊断性能,而不是在生产代码中。
在这种情况下,在 .NET 中获得精确计时的最佳方法是什么? 我目前正处于使用 MIDI 输出构建一个非常简单的 MIDI 音序器的早期阶段NAudio 的功能。 我希望能够发送对齐(例如)1/10 秒的 MIDI 消息,并且抖动尽可能小。 这是否可行,或者诸如上下文切换之类的事情会毁掉我的一天吗?
我目前在控制台应用程序中有一些代码,它连续调用 Stopwatch 并计算在 150bpm 生成 1/16 音符流时的抖动。 在这种情况下抖动非常低。 但是,我会将其移至另一个线程,所以我不知道情况是否会保持不变。
I've just seen this question, where one of the answers indicates that System.Diagnostics.Stopwatch should only be used for diagnosing performance and not in production code.
In that case, what would be the best way to get precision timing in .NET? I'm currently in the early stages of building a very simple MIDI sequencer using the MIDI-out functionality of NAudio. I'd like to be able to send MIDI messages out aligned to (say) 1/10s with as little jitter as possible. Is this feasible, or will things like context-switching ruin my day?
I currently have some code in a console app that continuously calls Stopwatch
and calculates the jitter when generating a stream of 1/16th-notes at 150bpm. The jitter is very low in this situation. However, I'll be moving this off to another thread, so I don't know if that will remain the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不介意 P/Invoke,则可以使用 QueryPerformanceCounter:http://www.eggheadcafe。 com/articles/20021111.asp
If you don't mind P/Invoke, you can use QueryPerformanceCounter: http://www.eggheadcafe.com/articles/20021111.asp
您还可以使用本机流媒体 MIDI API。 我不认为它在 NAudio 中,但你可以查看 C# Midi Toolkit 看看该工具包是否支持它。 否则,您有两个关于如何执行本机 MIDI API P/Invoke 的示例,并且可以推出您自己的...
You could also use the native streaming MIDI API. I don't think its in NAudio but you could look at the C# Midi Toolkit to see if that one supports it. Otherwise you have two examples of how to do native MIDI API P/Invoke and can roll your own...
Windows 多媒体计时器声明它允许“以硬件平台可能的最大分辨率(或准确度)安排计时器事件的应用程序。”
它用作示例的应用程序是 MIDI 音序器。
Windows multimedia timer states that it allows "applications to schedule timer events with the greatest resolution (or accuracy) possible for the hardware platform."
The application it uses as an example is a MIDI sequencer.
我是 DryWetMIDI 的作者。 该库具有回放功能,可在 WinMM 高精度计时器的帮助下工作。 计时器包装到 MidiClock 类,以便您可以使用它来驱动您的 MIDI 操作:
OnTick
将使用此代码每 1 毫秒调用一次。I'm the author of DryWetMIDI. The library has playback functionality that works with help of WinMM high-precision timer. Timer is wrapped to MidiClock class so you can use it to drive your MIDI operations:
OnTick
will be called each 1 ms with this code.