-1

I've made a small countdown and every 5 seconds it should make a beep sound but im not sure how to do this without an extra delay. How can i avoid this? I cant find anything that answers this specific question.

#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    while (N > 0)
    {
        Sleep(1000); 
        cout << N << endl;
        if(N%5==0) 
        {
            cout << "beep" << endl;
            Beep(1000, 500);
        }
        N--;
    }

}
  • 1
    Or, since the duration is `500`, change `Sleep(1000)` to `Sleep(500);` to get a total frozen time of `1000`. – 001 Feb 24 '22 at 16:09
  • Why do you not want to add an "extra delay"? Do you want your program to be doing something else in the mean time, so that your program is not blocking during `Sleep`? Also, is it a problem that the function `Beep` also blocks as long as the sound is playing? – Andreas Wenzel Feb 24 '22 at 16:13
  • 1
    `Sleep` is not the right API to use when you need at least (somewhat) accurate timing. Use a [timer](https://learn.microsoft.com/en-us/windows/win32/winmsg/timers) instead. It's also not accurate, but at least it doesn't accumulate inaccuracies. – IInspectable Feb 24 '22 at 16:14
  • If you want to play a more elaborate beep, you can pass a system call to VLC and select an mp3 file. I tried and it works in Linux: system("vlc --no-version -q --play-and-exit mybeep.mp3 &") – ferdymercury Feb 24 '22 at 17:50

2 Answers2

3

You can use MessageBeep() for your purpose. Its syntax is like this:

BOOL MessageBeep(
  [in] UINT uType
);

And its use is something like this:

MessageBeep(uType);

Read up about it on MSDN to find out more about all of the possible arguments.

For your code, you can do something like this:

#include <iostream>
#include <Windows.h>

int main()
{
    int N;
    std::cin >> N;
    while (N > 0)
    {
        Sleep(1000);
        std::cout << N << std::endl;
        if (N % 5 == 0)
        {
            std::cout << "beep" << std::endl;
            MessageBeep(0xFFFFFFFF); // Replace 0xFFFFFFFF with whatever value you want
        }
        N--;
    }
}

Also, consider not using the following line in your code:

using namespace std;

It's considered as bad practice. This is because, for example, if 2 namespaces contain 2 functions with the same name and parameters, and if you write 'using namespace' for both of the namspaces then the compiler won't be able to figure out which function to use, and thus your application won't work.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
The Coding Fox
  • 1,488
  • 1
  • 4
  • 18
  • *"it's considered as bad practice"* - If you cannot explain, **why** others presumably consider this bad practice, you might as well leave this suggestion out. Voicing someone else's opinion without accompanying rationale is just noise. – IInspectable Feb 24 '22 at 16:19
  • Sure but that would make the answer longer. But I've tried to explain it in a short way in my edit. Also, it's not actually the concern of the OP, I just gave it as a good coding practice tip. – The Coding Fox Feb 24 '22 at 16:23
  • 2
    That explanation is wrong. C++ decidedly supports overloading, alongside overload resolution. If the compiler cannot resolve any ambiguities, the program will fail to compile. This is distinctly different from a program failing to work correctly. – IInspectable Feb 24 '22 at 16:30
  • I agree with @IInspectable. Just scrap the explanation. I added a link to answer _why_ instead. – Ted Lyngmo Feb 24 '22 at 16:40
  • 1
    I was just trying to explain in a simple way. BTW thanks @TedLyngmo – The Coding Fox Feb 24 '22 at 16:58
2

On Windows you can play a simple sound asynchronously using the PlaySound Win32 call like the following, where by "asynchronously" we mean without pausing:

PlaySound(
    reinterpret_cast<LPCTSTR>(SND_ALIAS_SYSTEMDEFAULT), 
    hInstance, 
    SND_ASYNC | SND_ALIAS_ID
);

SND_ALIAS_ID means you are not providing a .wav file path or resource in the first parameter but instead are just specifying an ID of a canned sound built into Windows. These canned sounds are all the same chime, not really a beep, on Windows 10, but you could also embed a beep sound into you application as a .wav resource and use that.

To use PlaySound you will need to be linking to Win32 libraries, in particular "Winmm.lib", I believe.

jwezorek
  • 8,592
  • 1
  • 29
  • 46