Windows 中低级音频的最佳 API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2010400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best API for low-level audio in Windows?
提问by steveha
I'm working on an audio application, written in C. I need to provide live audio playback under Windows. I need to decide which audio API to use. I'm planning to use the basic waveOut API, but I wanted to check to see what the community here recommends.
我正在开发一个用 C 编写的音频应用程序。我需要在 Windows 下提供实时音频播放。我需要决定使用哪个音频 API。我打算使用基本的 waveOut API,但我想查看这里的社区推荐什么。
I want code that will Just Work on any recent version of Windows, with no need to install libraries; and I want minimal latency.
我想要的代码可以在任何最新版本的 Windows 上运行,无需安装库;我想要最小的延迟。
I don't need or want any "effects", I just need to faithfully play whatever wave samples the application generates.
我不需要也不想要任何“效果”,我只需要忠实地播放应用程序生成的任何波形样本。
My understanding is that most of the professional audio applications on Windows use ASIO, which gives excellent low latency, but I don't want ASIO because I want my code to Just Work and most people don't have ASIO pre-installed on their computers. (At a later date I may go back and also add ASIO as an option, but I'm going for the most general solution first.)
我的理解是 Windows 上的大多数专业音频应用程序都使用 ASIO,这提供了极好的低延迟,但我不想要 ASIO,因为我希望我的代码能够正常工作,而且大多数人的计算机上没有预装 ASIO . (稍后我可能会返回并添加 ASIO 作为一个选项,但我首先要寻找最通用的解决方案。)
Is there anything out there that would be better than waveOut for my purposes, or is that the best choice?
就我的目的而言,有没有比 waveOut 更好的东西,或者那是最好的选择?
回答by John Knoeller
It depends on what you are trying to do. The basic waveOut audio API is better for streaming audio. It lets you queue up several buffers and have them automatically played in succession. But if audio is playing and you want to change it, or add something to it, that's relatively hard.
这取决于您要尝试做什么。基本的 waveOut 音频 API 更适合流式传输音频。它允许您将多个缓冲区排队并让它们自动连续播放。但是,如果正在播放音频并且您想对其进行更改或添加一些内容,那就相对困难了。
DirectX audio is better for event based audio. You can have several things playing at the same time without having to do the mixing yourself. You can add or remove little pieces of audio easily - like playing a sound when the user pulls the trigger on his gun. But streaming (i.e. playing 1 buffer after another) is harder.
DirectX 音频更适合基于事件的音频。您可以同时播放多种内容,而无需自己进行混音。您可以轻松添加或删除小段音频 - 就像在用户扣动枪上的扳机时播放声音一样。但是流式传输(即一个接一个地播放缓冲区)更难。
waveOut is designed to facilitate playing audio that is constant, like a .mp3 file. DirectX is designed for audio that is intermittent, like feedback in a game.
waveOut 旨在促进播放恒定的音频,如 .mp3 文件。DirectX 专为间歇性音频而设计,例如游戏中的反馈。
ASIO is like the worst of waveOut and DirectX in terms of difficulty of programming. And it's not that stable. Applications typically can't share the audio device. But it gives you the lowest latency access to that audio hardware.
ASIO在编程难度上堪比waveOut和DirectX。而且它不是那么稳定。应用程序通常无法共享音频设备。但它为您提供了对该音频硬件的最低延迟访问。
ASIO also gives you a way to synchronize playback on multiple devices.
ASIO 还为您提供了一种在多个设备上同步播放的方法。
If you don't need to be able to changewhat is going to be played right before it is played, and you don't need to synchronize multiple devices then you don't need ASIO.
如果您不需要能够在播放前立即更改将要播放的内容,并且不需要同步多个设备,那么您就不需要 ASIO。
回答by steveha
At the time I asked this question, I wrote streaming code using the waveOut and waveIn APIs. Since then, I have discovered a useful library:
在我问这个问题时,我使用 waveOut 和 waveIn API 编写了流代码。从那时起,我发现了一个有用的库:
PortAudio http://www.portaudio.com/
PortAudio http://www.portaudio.com/
PortAudio is free software with a commercial-friendly license. If you write your code to call PortAudio it should be able to work with waveOut devices but also with ASIO devices under Windows; it can be then recompiled for Linux and should work with ALSA devices; and it can then be recompiled for the Mac and should work with CoreAudio devices. I haven't tested the Mac part but my project is working great with Windows and Linux.
PortAudio 是具有商业友好许可证的免费软件。如果您编写代码来调用 PortAudio,它应该能够与 waveOut 设备以及 Windows 下的 ASIO 设备一起使用;然后可以为 Linux 重新编译它,并且应该可以与 ALSA 设备一起使用;然后可以为 Mac 重新编译它,并且应该可以与 CoreAudio 设备一起使用。我还没有测试 Mac 部分,但我的项目在 Windows 和 Linux 上运行良好。
回答by Mark Heath
回答by Nathan Osman
Having written a DirectSound streaming application myself, I certainly recommend it for low-latency and ease of use. Also, it enables you to set a higher quality format for playback on legacy editions of Windows.
我自己编写了一个 DirectSound 流应用程序,我当然推荐它以实现低延迟和易用性。此外,它还使您能够为在旧版 Windows 上播放设置更高质量的格式。