Win32 PlaySound: How to control the volume?
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开发者_Go百科).
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.
Adding as an answer to implementation of @selbie clean solution.
#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
void ReadWavFileIntoMemory(string fname, BYTE** pb, DWORD *fsize){
ifstream f(fname, ios::binary);
f.seekg(0, ios::end);
int lim = f.tellg();
*fsize = lim;
*pb = new BYTE[lim];
f.seekg(0, ios::beg);
f.read((char *)*pb, lim);
f.close();
}
int main(){
DWORD dwFileSize;
BYTE* pFileBytes;
ReadWavFileIntoMemory("D:\\OpenAL 1.1 SDK\\samples\\media\\fiveptone.wav", &pFileBytes, &dwFileSize);
BYTE* pDataOffset = (pFileBytes + 40);
cout << "Length: " << dwFileSize << endl;
float fVolume = 0.02;
__int16 * p = (__int16 *)(pDataOffset + 8);
cout << sizeof(*p) << endl;
for (int i = 80 / sizeof(*p); i < dwFileSize / sizeof(*p); i++){
p[i] = (float)p[i] * fVolume;
}
cout << "PlaySound" << endl;
PlaySound((LPCSTR)pFileBytes, NULL, SND_MEMORY);
return 0;
}
The problem I faced with the proper sized datatype (here I used __int16) for manipulation in wav file.
The wav file I used comes with OpenAL media files "fiveptone.wav".
Please feel free to modify because the offset I searched from documentations of wav files and some trial and error.
Another example (this one in Rust)
#![allow(uncommon_codepoints)]
#![feature(const_float_bits_conv)]
winapi = {version = "0.3", features = ["mmeapi", "playsoundapi"]}
use core::convert::TryInto;
use core::ptr::{addr_of, read_unaligned, null_mut};
use gstuff::re::Re;
use std::path::Path;
use winapi::shared::mmreg::WAVEFORMATEX;
/// synchronously play a `wav` file at a `vol`ume
fn play_vol (wav: &dyn AsRef<Path>, vol: f32) -> Re<()> {
let mut wavᵇ = gstuff::slurp (wav);
// http://soundfile.sapp.org/doc/WaveFormat/
// https://docs.rs/winapi/latest/winapi/shared/mmreg/struct.WAVEFORMATEX.html
let header: *const WAVEFORMATEX = &wavᵇ[20] as *const u8 as *const WAVEFORMATEX;
let bits_per_sample = unsafe {read_unaligned (addr_of! ((*header).wBitsPerSample))};
if bits_per_sample != 32 {fail! ([=bits_per_sample])}
if &wavᵇ[36..40] != b"data" {fail! ("unexpected Subchunk2ID")}
let subchunk2size = u32::from_le_bytes ((&wavᵇ[40..44]) .try_into()?) as usize;
if subchunk2size != wavᵇ.len() - 44 {fail! ([=subchunk2size] " vs " [=wavᵇ.len()])}
let samples = subchunk2size / 4;
for sx in 0 .. samples {
let sampleᵇ = &mut wavᵇ[44 + sx * 4 .. 44 + sx * 4 + 4];
let mut sample = f32::from_le_bytes (sampleᵇ.try_into()?);
sample *= vol;
let sb: [u8; 4] = sample.to_le_bytes();
sampleᵇ[0] = sb[0]; sampleᵇ[1] = sb[1]; sampleᵇ[2] = sb[2]; sampleᵇ[3] = sb[3]}
// https://learn.microsoft.com/en-us/previous-versions/dd743680(v=vs.85)
let flags = winapi::um::playsoundapi::SND_MEMORY;
unsafe {winapi::um::playsoundapi::PlaySoundA (
wavᵇ.as_ptr() as *const i8,
null_mut(),
flags)};
Re::Ok(())}
精彩评论