Solved

PC speaker beep

Posted on 2006-06-20
9
1,459 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now