开发者

Indefinite system speaker beep

开发者 https://www.devze.com 2022-12-17 04:43 出处:网络
Now, I know that I can play a beep of a set duration with Console.Beep, but what I\'m looking for is a way to indefinitely play a sound of a certain frequency, like a 开发者_如何学编程start and stop f

Now, I know that I can play a beep of a set duration with Console.Beep, but what I'm looking for is a way to indefinitely play a sound of a certain frequency, like a 开发者_如何学编程start and stop function. Any ideas?


If you want to make a beep other than the one offered in the Console namespace you have to talk directly to the speaker. Use the Kernel32.dll to do this like so:

First make sure to include interop:

using System.Runtime.InteropServices;

Then add the extern method for Beep, and use it from your other methods, this one will beep the speaker for as long as you want at the frequency you give it.

[DllImport("Kernel32.dll")]
public static extern bool Beep(Int32 frequency, Int32 duration);
public static void BeepFor(int mSec, int freq)
{
  int Frequency = freq;
  int DurationInMS = mSec;
  Beep(Frequency, DurationInMS);
}


You may be able to use MIDI.

You may also be able to loop a WAV file using the PlaySound API call with SND_LOOP and SND_ASYNC specified.

The concept of playing the same sound indefinitely on a modern PC is somewhat fickle unless you are at the the hardware level where you can write and control an infinite loop or just "turn on" the speaker at a certain frequency. The higher level API's and programming languages aren't going to allow this as, at that level, the OS is forcing you to play nice with other applications.

Edit

It should also be noted that the duration of the Beep call is in milliseconds and it does take an Int32 value. Int32.MaxValue milliseconds is looooong time. Probably more than anyone could stand listening to the beep.


As always in these questions, the first response should be:

What are you really trying to accomplish. Do you really want a beep of indefinite duration, or do you want to play sounds for longer periods?

0

精彩评论

暂无评论...
验证码 换一张
取 消