C# 可以生成复杂的音调吗?

发布于 2024-08-08 20:57:19 字数 111 浏览 8 评论 0原文

我需要创建包含许多不同频率的音调的声音。在 C# 中有什么办法可以做到这一点吗?

到目前为止,我见过的唯一音调生成方法涉及 console.beep,它可以工作,但仅适用于纯音调(单一频率)。

I need to create a sound containing tones of many different frequencies. Is there any way to do this in C#?

The only tone generating methods I've seen so far involve console.beep, which works, but only for pure tones (single frequencies).

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

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

发布评论

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

评论(6

辞慾 2024-08-15 20:57:19

Audiere 库使这一切变得非常容易。下面是一个几乎完整的 C# 程序,用于为“1”按钮生成 DTMF 音调:

AudioDevice device = new AudioDevice();
OutputStream tone1a = device.CreateTone(697);  // part A of DTMF for "1" button
OutputStream tone1b = device.CreateTone(1209); // part B
tone1a.Volume = 0.25f;
tone1b.Volume = 0.25f;
tone1a.Play();
tone1b.Play();
Thread.Sleep(2000);
// when tone1a stops, you can easily tell that the tone was indeed DTMF
tone1a.Stop();  

要在 C# 中使用 Audiere,最简单的启动和运行方法是使用 Harald Fielker 的 C# 绑定(他声称它适用于 Mono VS;我可以确认它可以在完整版本的 VS2005 和使用单独的 Express 2008 版本的 C# 和 VC++ 中运行。您需要下载 Win32 Audiere DLL、lib 和标头(全部位于相同的 zip),并且您需要使用 VC++ 和 C# 从源代码构建 C# 绑定。

使用 Audiere 的好处之一是调用是非阻塞的。您不必等到 tone1a 停止播放才开始 tone1b,这显然是播放复杂音调的必要条件。我不知道您可以使用多少个同时输出流有任何硬性上限,所以它可能是您的硬件/操作系统支持的。顺便说一句,Audiere 还可以播放某些音频文件(MP3、WAV、AIFF、MOD、S3M、XM、IT 本身;Ogg Vorbis、Flac、Speex 以及外部库),而不仅仅是纯粹生成的音调。

一个可能的缺点是,当您开始或停止单个音调时,会听到轻微的“咔哒”声;如果您将一种音调添加到已播放的音调中,则不会引起注意。我发现的最简单的解决方法是在打开或关闭音调时分别缓慢增大或减小音调的音量。您可能需要调整斜坡速度才能使其听起来“恰到好处”。

请注意,Audiere 已获得 LGPL 许可,并且该绑定没有附加任何许可证。如果您想在商业产品中使用 Harald 的绑定,您必须咨询您的法律团队或尝试联系 Harald;或者您可以自己制作绑定并避免麻烦。


@Tom: Since there is no specific license attached to Harald's library, I'm not sure what implications would come of hosting it; however, I believe I can at least give you fine detail on exactly how my libaudieresharpglue project is set up.

使用 Visual C++ Express 2008,打开 bindings/csharp/libaudieresharpglue/vc8.0/libaudieresharpglue.sln。 VC++ 会自动将解决方案转换为 VS9 解决方案。

在另一个文件夹中,您应该有来自 Sourceforge 的 Audiere 包。在 VC++ 项目属性下,转到“配置属性”>“ C/C++>一般情况,并确保“其他包含目录”中有 path/to/audiere-1.9.4-win32/include。然后,在同一窗口中,转到“链接器”>“链接器”。常规并确保“其他库目录”中有 /path/to/audiere-1.9.4-win32/lib。然后,您应该能够在 vc8.0/Release 文件夹中构建项目(最好在发布模式下)并输出 libaudieresharpglue.dll

接下来,打开 Visual C# Express 2008。打开 bindings\csharp\test\vc8.0\AudiereCSharpTest.sln 并让它转换解决方案。该项目应该可以正常构建,但运行时会出现错误。没关系;在您的 csharp/test/vc8.0/bin/Release 文件夹中,您需要添加 VC++ 解决方案中的 libaudieresharpglue.dll 和 Sourceforge 包中的 audiere.dll。

现在,您应该能够构建并运行 AudiereCSharpTest。请注意,默认情况下,AudiereTest.cs 顶部的 #define stream_test 不会被注释掉,并且它将引用不在您的硬盘驱动器上的文件。您只需注释掉 #define 并取消注释 noise_testsquare_test 即可。

那应该涵盖它;如果我错过了任何细节,希望它们足够小,您可以自己完成:)

The Audiere library makes this extremely easy to do. Here's a nearly complete C# program to generate the DTMF tone for the "1" button:

AudioDevice device = new AudioDevice();
OutputStream tone1a = device.CreateTone(697);  // part A of DTMF for "1" button
OutputStream tone1b = device.CreateTone(1209); // part B
tone1a.Volume = 0.25f;
tone1b.Volume = 0.25f;
tone1a.Play();
tone1b.Play();
Thread.Sleep(2000);
// when tone1a stops, you can easily tell that the tone was indeed DTMF
tone1a.Stop();  

To use Audiere in C#, the easiest way to get up and running is to use Harald Fielker's C# binding (which he claims works on Mono and VS; I can confirm it works in the both full version of VS2005 and using the separate Express 2008 versions of C# and VC++). You'll need to download the Win32 Audiere DLL, lib, and header (which are all in the same zip) and you'll need to build the C# binding from source using both VC++ and C#.

One of the nice benefits of using Audiere is that the calls are non-blocking. You don't have to wait for tone1a to stop playing before you start tone1b, which is clearly a necessity for playing complex tones. I am not aware of any hard upper limits on how many simultaneous output streams you can use, so it's probably whatever your hardware/OS supports. By the way, Audiere can also play certain audio files (MP3, WAV, AIFF, MOD, S3M, XM, IT by itself; Ogg Vorbis, Flac, Speex with external libraries), not just pure generated tones.

One possible downside is that there is a slightly audible "click" as you start or stop an individual tone; it's not noticeable if you add one tone to an already playing tone. The easiest workaround I've found for that is to slowly ramp the tone's volume up or down when you're turning the tone on or off, respectively. You might have to play around with the ramp speed to get it to sound "just right".

Note that Audiere is LGPL-licensed, and the binding has no license attached to it. You'll have to consult your legal team or try to get a hold of Harald if you want to use his binding in a commercial product; or you could just make your own binding and avoid the hassle.


@Tom: Since there is no specific license attached to Harald's library, I'm not sure what implications would come of hosting it; however, I believe I can at least give you fine detail on exactly how my libaudieresharpglue project is set up.

Using Visual C++ Express 2008, open up bindings/csharp/libaudieresharpglue/vc8.0/libaudieresharpglue.sln. VC++ will automatically convert the solution to a VS9 solution.

In another folder, you should have the Audiere package from Sourceforge. Under your VC++ project properties, go to Configuration Properties > C/C++ > General, and make sure you have path/to/audiere-1.9.4-win32/include in your "Additional Include Directories." Then, in that same window, go to Linker > General and make sure you have /path/to/audiere-1.9.4-win32/lib in your "Additional Library Directories." Then, you should be able to build the project (preferably in Release mode) and this output libaudieresharpglue.dll in your vc8.0/Release folder.

Next, open up Visual C# Express 2008. Open up bindings\csharp\test\vc8.0\AudiereCSharpTest.sln and let it convert the solution. The project should build fine, but then you will get an error when you run it. That's fine; in your csharp/test/vc8.0/bin/Release folder, you need to add both libaudieresharpglue.dll from the VC++ solution and audiere.dll from the package from Sourceforge.

Now, you should be able to build and run AudiereCSharpTest. Note that by default, #define stream_test is not commented out at the top of AudiereTest.cs, and that will reference a file that is not on your hard drive. You can simply comment out that #define and uncomment noise_test or square_test.

That should cover it; if I missed any details, hopefully they are small enough to get by on your own :)

冧九 2024-08-15 20:57:19

您随时可以尝试 DirectSound...

You can always try DirectSound...

清君侧 2024-08-15 20:57:19

我一直在研究NAudio,希望创建一个在播放背景音乐时模拟反馈的程序。有一篇关于生成 特定频率的正弦波,我怀疑这可以适应你正在寻找的东西。

I have been looking at NAudio with the view to create a program that emulates feedback whilst playing a backing track. There is a blog post about generating sine waves at specific frequencies, I suspect that this could be adapted to do what you are looking for.

心欲静而疯不止 2024-08-15 20:57:19

是的,这是可能的。

这是相关教程的链接。但当然这也使用 Console.Beep

Yes it is possible.

Here is a link to a tutorial on this. but of course this also uses Console.Beep

画尸师 2024-08-15 20:57:19

MSDN 文档没有明确说明 Console.Beep 是否是异步的。如果是这样,您可能可以快速连续地拨打所需数量的电话,而没有人会知道。当然,您想要使用需要频率和持续时间的版本。

The MSDN documentation doesn't make it clear if Console.Beep is asynchronous or not. If it is, you can probably fire off as many calls as you need in quick succession and nobody will be the wiser. You'd want to use the version that takes a frequency and a duration, of course.

jJeQQOZ5 2024-08-15 20:57:19

本质上,您必须实现自己的软件合成器或找到第三方库。请参阅:http://en.wikipedia.org/wiki/Demo_(computer_programming)#音乐

Essentially, you have to implement your own software synthesizer or find a 3rd party library. See: http://en.wikipedia.org/wiki/Demo_(computer_programming)#Music

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