tags:

views:

106

answers:

3

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?

+3  A: 

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);
}
Tj Kellie
And this differs from Console.Beep... how? This is basically what Console.Beep calls.
Bevin
It is the *exact* same thing.
Hans Passant
Yes, therefore not what I'm looking for.
Bevin
Well that wasn't a great idea. I just copied your code over to try it out. I didn't get a beep the first time, so I turned my sound all the way up -- still nothing. Then put it in a loop of 1000. ...Needless to say, I just had a heart attack.
George
A: 

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.

A: 

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?

Bob K