Solved

PC speaker beep

Posted on 2006-06-20
9
1,449 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
Comment Utility
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
Comment Utility
>>>> 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
Comment Utility
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
Comment Utility
(Preferably one that disregards the beep requests if they start to come faster than they are dispatched.)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>>> 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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

763 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

9 Experts available now in Live!

Get 1:1 Help Now