ArtOfCode
ArtOfCode

Reputation: 5712

A way to concatenate Console.Beep sounds

I've been playing around with some C# and specifically making sounds... because it's fun. So I've got it all working but there's something bugging me to do with Console.Beep(): it doesn't directly concatenate sounds. For example, running the code below will result in a series of 250-millisecond bursts of sound - but instead of all being run together and sounding as if they are one, they become disjointed, with a ~50ms pause in between each sound.

for(int i = 0; i < 11; i++)
{
    Console.Beep(980, 250);
}

So the question is, is there any programmatic way to make the system run the sounds together? I have to say I don't really expect there to be but I figured it was worth an ask, since many other resources seem to just accept the fact that it doesn't.

Upvotes: 4

Views: 917

Answers (1)

KugBuBu
KugBuBu

Reputation: 630

You can't, that method uses kernel's functions. I will prove:

[SecuritySafeCritical]
[HostProtection(SecurityAction.LinkDemand, UI = true)]
public static void Beep(int frequency, int duration)
{
    if (frequency < 37 || frequency > 32767)
    {
        throw new ArgumentOutOfRangeException("frequency", frequency, Environment.GetResourceString("ArgumentOutOfRange_BeepFrequency", new object[]
        {
            37,
            32767
        }));
    }
    if (duration <= 0)
    {
        throw new ArgumentOutOfRangeException("duration", duration, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
    }
    Win32Native.Beep(frequency, duration);
}

This is the Console.Beep's code, it uses Win32Native.Beep to actually perform beep (Aside from the checks above it), and that method leads to:

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool Beep(int frequency, int duration);

A function that imported from kernel.

Unless you hard-code and modify your kernel, you can't. (Which I am sure you don't want to)

I can give you an alternative: http://naudio.codeplex.com/, you can instead control your sound by using this tool and giving it stream. (You can create stream that don't use file as source)

Upvotes: 4

Related Questions