Solved

PC speaker beep

Posted on 2006-06-20
9
1,467 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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