Solved

PC speaker beep

Posted on 2006-06-20
9
1,482 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 150 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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