C#/WPF and Internal Speaker

W

Wonko the Sane

Hello,

This is for a WPF (3.5) application.

We have a requirement to produce sounds on an internal speaker (in the case
that no external sound device is available).

I know of Console.Beep(frequency, duration), but I wondered if there is a
better, more controllable way. Basically, the user will be toggling a
(hardware) switch on and off. When it is on, we need to play a tone, and
when it's off, we need to stop playing the tone.

We can use a series of short Beeps, but there will be a gap between each
beep, and the beep will need to run its entire duration (in other words, it
won't stop immediately when the switch is turned off).

Thanks,
WtS
 
L

Linda Liu[MSFT]

Hi Wonko,

Firstly, there's a Win32 API function "Beep" which generates simple tones
on the speaker. The function is synchronous; it performs an alertable wait
and does not return control to its caller until the sound finishes.

The definition of this function is as follows:
BOOL Beep(
DWORD dwFreq,
DWORD dwDuration
);

It's similar to the Console.Beep(int, int) method overload. So it seems
that the Console.Beep(int, int) method overload is the wrapper method of
the Beep function.

Secondly, we could use the command "net stop beep" to silence the tone.
When the Console.Beep(int, int) method is being executed and we execute the
command "net stop beep" during the beep, the command window outputs the
following text:
The Beep service is stopping...

Only after the Console.Beep finishes the execution, the command window
outputs the following text:
The Beep service was stopped successfully.

As we can see, the beep service cannot be stopped when the tone is being
used. Only after the tone is free, the beep service can be stopped. So this
is a hardware design and this behavior is by design. And IMO, there's no
better and more controllable method or function than the Console.Beep
method since the Control.Beep is already the wrapper of the Beep function.

As for your question, I suggest that you use a series of short beeps by
calling the Console.Beep(int, int). For example:
for (int i = 0 ; i < 10; i++)
{
Console.Beep(80, 500);
}

Thus, the entire beep will last for 5 seconds. But if you silence the tone
during the beep, the beep will be stopped more promptly.

Hope I make myself clear.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wonko the Sane

Hi Linda,

Thanks for the feedback. That is the preliminary design of that part of my
app - I was just wondering if there was a "better" way, or a more .NET way
that I wasn't aware of.

Thanks again for the help,
WtS
 
B

Ben Voigt [C++ MVP]

Wonko said:
Hi Linda,

Thanks for the feedback. That is the preliminary design of that part
of my app - I was just wondering if there was a "better" way, or a
more .NET way that I wasn't aware of.

The "better" way is to install a sound driver for the PC speaker, then use
any streaming audio API like DirectSound.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top