Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PC speaker beep

Posted on 2006-06-20
9
Medium Priority
?
1,494 Views
Last Modified: 2012-06-27
Does anybody know if there is any asynchronous equivilant to Beep in Visual C++?
0
Comment
Question by:krispin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:Mikeh926
ID: 16940512
You could call MessageBeep(-1) if you want a simple asynchrous beep from the sound card/PC speaker, although you have no control over the frequency or duration. PlaySound() can also be asynchronous but this requires a wav file and a working sound card.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16941004
>>>> although you have no control over the frequency or duration.

You may use Beep() function:

BOOL Beep(
  DWORD dwFreq,      // sound frequency, in hertz
  DWORD dwDuration   // sound duration, in milliseconds
);

where I once achieved to play a little melody ;.)

Regards, Alex

0
 
LVL 7

Expert Comment

by:aib_42
ID: 16954587
I've seen no asynchronous alternative, but I've always thought I could create a custom thread for the job.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:aib_42
ID: 16954592
(Preferably one that disregards the beep requests if they start to come faster than they are dispatched.)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16958491
>>>> is any asynchronous equivilant to Beep in Visual C++?

If calling Beep in a timer proc it runs asynchronously.

MFC:

  void CMyDialog::OnBtnClickedStartBeep()
  {
         SetTimer(99, 10);  // ID=99, every 10ms
  }

  void CMyDialog::OnTimer(int event)
  {
       if (event == 99)
       {
            Beep(.., ...);
       }
  }

Regards, Alex
0
 

Author Comment

by:krispin
ID: 17000729
>>If calling Beep in a timer proc it runs asynchronously.

If called in a timer proc, it will execute asynchronously to the rest of the application, but does the timer proc stop and wait for the beep function to finish before continuing?
0
 

Author Comment

by:krispin
ID: 17002801
I tested this out myself, the timer proc will hold until the Beep function completes:
For example, if I have:
--------------------------------------------------------------------------------------------------
case WM_TIMER:
    Beep(400, 1000);
    MessageBox(NULL, "Sound Complete", "", MB_OK);
break;

--------------------------------------------------------------------------------------------------
The function won't continue until Beep returns.
I need something that can execute completely asynchronously. There is a function for doing it under Borlands C++. It is quite similar to Beep. Using the code would look as follows

--------------------------------------------------------------------------------------------------
case WM_TIMER:
    sound(400);
    MessageBox(NULL, "Sound Complete", "", MB_OK);
    nosound();
break;

--------------------------------------------------------------------------------------------------

The function takes a single parameter; the frequency, and executes asynchronously. The program can continue as normal, but the sound will play continuously until the nosound function is called. I guess I'm looking for something similar to this. Unfortunately I can't use this because I am stuck with using Visual Studio .NET

I tried executing Beep in a thread containing something like:
while(sound)
{
    Beep(400, 30);
}

But the problem with this was that there was a delay from when the sound should begin and when it does (possibly to do with gaining access to the speaker resource in windows?), and then while "sound" is true, there is a noticable gap between the beeps, where I would prefer a minimal gap, or none at all.

MessageBeep(-1) as suggested by  Mikeh926 can be used as a stop-gap measure, but in the long term I really need to be able to control the frequency. The software also won't have access to a sound card, so that avenue is unfortunately closed off to me.

Thanks for all the suggestions so far and I appreciate any further insights anyone my have.
Krispin
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 600 total points
ID: 17003028
>>>> I need something that can execute completely asynchronously

The WM_TIMER proc is done in the message loop of the GUI so it isn't very precise and it waits for the beep to complete (or more precise, it waits for the call to Beep to return).

You can turn the last off by beeping in a thread.
   ...
case WM_TIMER:
   
    _beginthread(BeepProc, 0, (400 << 16) + 1000);
break;
   ...

void BeepProc(void* param)
{
    unsigned int bparam = (unsigned int)param;
    int bp1 = bparam>>16;
    int bp2 = bparam & 0xffff;
    Beep(bp1, bp2);
}

Here the thread beep would run asynchronously with no delay beside of thread start.

The advantage is that you get a controlled frequency by the timer. Its not very precise but better than an infinite loop in a thread.

But, you will never get an optimum cause Windows isn't a real-time OS. That means thread invokation, timer frequency, message handling are subjects of randomness. If you start an application from desktop beeping will slow down as well as when you are moving rapidly with tne mouse.

And of course you may not use MessageBox or Debug versions cause you'll get irregular time behavior then.

Regards, Alex
0
 
LVL 7

Expert Comment

by:aib_42
ID: 17017440
Use itsmeandnobodyelse's exact code, but don't put it in a timer loop. Just use "_beginthread(BeepProc, 0, (400 << 16) + 1000);" as an async call to Beep.

#define BeepAsync(f,t) _beginthread(BeepProc, 0, ((f << 16) & t))
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question