1

Ensure that sound was played after each clicking the mouse.
My idea is to create a thread that runs constantly.

#include <pthread.h>
music ding1("./ding1.wav");
music ding2("./ding2.wav");
void* pmusic(void* a)
{
  while(1)
  {
    DWORD dw=WaitForSingleObject(hmusic, INFINITE) ;
    if(ding1.busy)
    {
      ding2.play();
    }else{
      ding1.play();
    }
    ResetEvent(hmusic);
  }
}

Create a public signal.

HANDLE hmusic=CreateEvent(nullptr,false,false,nullptr);

Play the sound using the playsound function,in music class

class music
{
public:
music(char* path)
{
  //load the wav file to memory
  fs.open(path...);
  ...
  fs.readsome(buf...);
  ...
}
play()
{  
   busy=1;
 
 PlaySoundA(buf,null,SND_MEMORY,SND_ASYNC,SND_NOSTOP,SND_NOWAIT);
  busy=0;
}

char * buf;
int busy;
...
}

WndProc

LRESULT CALLBACK WndProc(hwnd,msg,wparam,lparam)
{
  switch(msg)
  case WM_LBUTTONDOWN:
  {
    SetEvent(hmusic);
    break;
  }
  case WM_LBUTTONUP:
  {
    ResetEvent(hmusic);
    break;
  }
  case WM_CREATE:
  {
    pthread_create(&tid,null,pmusic,null);
    break;
  }
}

It worked on Windows 10 after compiling, BY mingw32 with no problem.
Maybe there is another different way to achieve the above.
Thank you for sharing your wisdom and experience.

anti-gravity
  • 122
  • 6
  • What is the point of creating an event object via `CreateEvent()` if you never wait on the event? You probably meant to use `while (WaitForSingleObject(hmusic, INFINITE) == WAIT_OBJECT_0)` instead of `while(1)` in your thread – Remy Lebeau Jul 20 '21 at 20:16
  • Also, `play()` is calling `PlaySoundA()` (incorrectly) with the `SND_ASYNC` flag, so `PlaySoundA()` will always exit immediately, and thus `busy` will effectively always be `0` even if a sound is actually playing. – Remy Lebeau Jul 20 '21 at 20:20
  • 1
    In any case, if you need fine-grain control over audio playback, `PlaySound()` is not the best way to go. – Remy Lebeau Jul 20 '21 at 20:25
  • @Remy Lebeau,yes i have missed it,as the code was inputed BY manual. – anti-gravity Jul 20 '21 at 21:00
  • Could you please explain the flow of your program? It looks like it starts playing `ding1.wav` on the first `WM_LBUTTONDOWN`, then on the next `WM_LBUTTONDOWN` it will play `ding2.wav` if the `ding1.wav` was still playing or repeat `ding1.wav` if it already finished? – Vlad Feinstein Jul 24 '21 at 00:59
  • @Vlad Feinstein,yes,i'd like to find A way to avoid delay or no sound was play when i click the mouse no matter how fast my figer was pull down.the program is not good enough for the goal,it sometime was delay ,sometime was missed and no sound when i clicking – anti-gravity Jul 24 '21 at 03:08

1 Answers1

0

I suggest to read the doc for PlaySound.

According to that, SND_NOWAIT is not supported. The SND_NOSTOP will prevent you from playing a sound if another one is already playing, that goes against your plan. Also, the flags are combined with the bit-wise OR operator |, not comma:

PlaySoundA(buf, NULL, SND_MEMORY | SND_ASYNC);

As was pointed in the comments, you don't need a thread to play with SND_ASYNC.

Don't type the code into the question; copy/paste the working code from your editor; the one you have won't compile.

Here is what you may use:

LRESULT CALLBACK WndProc(hwnd,msg,wparam,lparam)
{
  switch(msg) {
  case WM_LBUTTONDOWN:
    music();
    break;
  }
....

Where music() is:

void music()
{
  if(ding1.busy)
    ding2.play();
  else
    ding1.play();
}
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27