Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 627229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:30:20+00:00 2026-05-13T19:30:20+00:00

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

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:30:21+00:00Added an answer on May 13, 2026 at 7:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The former is preferrable because if index(es) exist on any… May 14, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer Learn about indexes, and use them properly. Generally speaking*, follow… May 14, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer Would something like this work for you? class Container attr_accessor… May 14, 2026 at 7:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.