Win32 PlaySound: How to control the volume?

11,558

Two possible solutions:

First, if you are targeting Vista and up, you can use the new Windows Audio APIs to adjust the per-application volume. ISimpleAudioVolume, IAudioEndpointVolume, etc...

If that's not suitable, can load the WAV file directly into memory and modify the samples in place. Try this:

Read the WAV file from disk and into a memory buffer and scale the samples back. I'm going to assume that the WAV file in question is 16-bit stereo with uncompressed (PCM) samples. Stereo or mono. If it's not, much of this goes out the window.

I'll leave the reading of the WAV file bytes into memory as an exercise for the reader: But let's start with the following code, where "ReadWavFileIntoMemory" is your own function.

DWORD dwFileSize;
BYTE* pFileBytes;
ReadWavFileIntoMemory(szFilename, &pFileBytes, &dwFileSize);

At this point, an inspection of pFileBytes will look something like the following:

RIFF....WAVEfmt ............data....

This is the WAV file header. "data" is the start of the audio sample chunk.

Seek to the "data" portion, and read the 4 bytes following "data" into a DWORD. This is the size of the "data" chunk that contains the audio samples. The number of samples (assuming PCM 16-bit is this number divided by 2).

// FindDataChunk is your function that parses the WAV file and returns the pointer to the "data" chunk.
BYTE* pDataOffset = FindDataChunk(pBuffer);
DWORD dwNumSampleBytes = *(DWORD*)(pDataOffset + 4);
DWORD dwNumSamples = dwNumSamplesBytes / 2;

Now, we'll create a sample pointer that points to the first real sample in our memory buffer:

SHORT* pSample = (SHORT*)(pDataOffset + 8);

pSample points to the first 16-bit sample in the WAV file. As such, we're ready to scale the audio samples to the appropriate volume level. Let's assume that our volume range is between 0.0 and 1.0. Where 0.0 is complete silence. And 1.0 is the normal full volume. Now we just multiply each sample by the target volume:

float fVolume = 0.5; // half-volume
for (DWORD dwIndex = 0; dwIndex < dwNumSamples; dwIndex++)
{
    SHORT shSample = *pSample;
    shSample = (SHORT)(shSample * fVolume);
    *pSample = shSample;
    pSample++;


    if (((BYTE*)pSample) >= (pFileBytes + dwFileSize - 1))
       break;
}

At this point, you are ready to play your in memory WAV file with PlaySound:

PlaySound((LPCSTR)pFileBytes, NULL, SND_MEMORY);

And that should do it. If you are going to use the SND_ASYNC flag to make the above call non-blocking, then you won't be able to free your memory buffer until it has finished playing. So be careful.

As for the parsing of the WAV file header. I hand-waved my way out of that by declaring a hypothetical function called "FindDataChunk". You should probably invest in writing a proper WAV file header parser rather than just seeking to where you first encounter "data" in the header. For the sake of brevity, I left out the usual error checking. As such, there may be a few security concerns to address with the above code - especially as it relates to traversing the memory buffer and writing into it.

Share:
11,558
David Citron
Author by

David Citron

Programmer, pianist, organist, and composer of eclectic music.

Updated on June 04, 2022

Comments

  • David Citron
    David Citron almost 2 years

    I'm using the Win32 MultiMedia function PlaySound to play a sound from my application.

    I would like to be able to dynamically adjust the volume of the sound that is being played without modifying the system volume level.

    The only suggestions I could find for manipulating the volume of sounds played via PlaySound was to use waveOutSetVolume, however that function sets the system-wide volume level (not what I want).