Home > Software engineering >  Loading Wav File into Memory for PlaySound
Loading Wav File into Memory for PlaySound

Time:07-13

I am trying to load a .wav file into memory so that I can play it using the PlaySound function.

Currently, I am able to play the sound with this line of code.

PlaySound(TEXT("C:\\DEV\\beep-07a.wav"), NULL, SND_FILENAME);

The problem is I am also displaying graphics when the sound is made, and when the sound plays, the graphics freeze while it is being played. I am assuming that this is due to the fact that the .wav file is being read from the disk each time it is played, and not from memory.

I did some searching and see that I can either load the file directly into my memory buffer or can use a resource file to do this, but I was unable to find any good resources on how to do this, so any help is greatly appreciated!

@NeoKo pointed out that it might be because I need to create a new thread so adding code that I used to do so. Now I'm having trouble figuring out how to get my sound to play using that thread.

#include <iostream>
#include <windows.h>

DWORD WINAPI ThreadFun(LPVOID lpParam)
{
    std::cout << "Thread Running"<<std::endl; 
    return 0; 
}

int main()
{
    HANDLE hThread;
    DWORD ThreadID; 

    hThread = CreateThread(
        NULL,
        0,
        ThreadFun,
        NULL,
        0,
        &ThreadID); 

    if (hThread == NULL)
    {
        std::cout << "Thread Creation FAILED"<<std::endl; 
    }
    std::cout << "Thread Creation SUCCESSS" << std::endl; 
    std::cout << "Thread ID is " << ThreadID << std::endl; 
    CloseHandle(hThread); 
};

CodePudding user response:

If you want to resolve the issue of PlaySound blocking your thread, you can adjust your call like so

PlaySound(..., SND_FILENAME | SND_ASYNC);

From the documentation, including this will change the behaviour such that

The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.

If performance is a concern, you can bundle the sound into a resource file which you can link with your program, and instead load your resource by an ID rather than file path.

CodePudding user response:

There are existing questions for reading a file into a memory buffer, such as:

C read the whole file in buffer
Reading file into buffer
How to read a binary file into a vector of unsigned chars
just to name a few...

Once you have the WAV file data in memory, simply pass a pointer to the WAV data in memory to PlaySound(), specifying the SND_MEMORY flag, eg:

PlaySound((LPCTSTR)data_pointer, NULL, SND_MEMORY);

CodePudding user response:

I'm not sure why, but just using sndPlaySound instead of PlaySound fixed the latency and all of the freezing issues I was having with my graphics without needing to create a new thread or even having to load the file into my memory buffer. This was the line of code I ended up using.

 sndPlaySound(TEXT("C:\\DEV\\beep-07a.wav"), SND_ASYNC | SND_NOSTOP);
  • Related