Cameron Neil
Cameron Neil

Reputation: 11

Finding the current volume level of the speaker output in Windows

EDITED: I'm trying to write a sort of audio visualizer and I need to get the current volume of the sound being sent to the speakers. I need the actual volume of the sound, not just the master volume of the device. I've been trying to do it using this but I'm unsure how to implement it. Having called the IAudioCaptureClient::GetBuffer() method, I don't know how to use the resulting data packet to find what I need. The audio is 2 channel, 32 bits per sample. Any help would be greatly appreciated.

Upvotes: 1

Views: 2296

Answers (2)

Helmut Obertanner
Helmut Obertanner

Reputation: 86

not sure if you found it for yourself. I would start here with the peakmeter info:

https://learn.microsoft.com/en-us/windows/win32/coreaudio/peak-meters

Upvotes: 0

Strive Sun
Strive Sun

Reputation: 6289

Starting with Windows 7, the existing APIs have been improved and new APIs have been added to support new scenarios. The stream and session management APIs have been improved so that the application can now enumerate and get extended control over the audio session. By using the new APIs, the application can implement a custom stream attenuation experience. New device-related APIs provide access to the driver properties of the endpoint devices.

Refer: Core Audio APIs

Here is a simple demo for reference:

#include <Windows.h>
#include <stdio.h>
#include <mmeapi.h>
#include <mmdeviceapi.h> 
#include <endpointvolume.h>
#include <audioclient.h>

#pragma comment(lib,"Winmm.lib")

bool GetVolumeLevel()
{
    HRESULT hr;
    IMMDeviceEnumerator* pDeviceEnumerator = 0;
    IMMDevice* pDevice = 0;
    IAudioEndpointVolume* pAudioEndpointVolume = 0;

    try {
        hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pDeviceEnumerator);
        if (FAILED(hr)) throw "CoCreateInstance";
        hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
        if (FAILED(hr)) throw "GetDefaultAudioEndpoint";
        hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pAudioEndpointVolume);
        if (FAILED(hr)) throw "pDevice->Active";

        float fVolume;

        hr = pAudioEndpointVolume->GetMasterVolumeLevelScalar(&fVolume);
        printf("%lf", fVolume);
        if (FAILED(hr)) throw "SetMasterVolumeLevelScalar";

        pAudioEndpointVolume->Release();
        pDevice->Release();
        pDeviceEnumerator->Release();
            return true;
    }
    catch (...) {
        if (pAudioEndpointVolume) pAudioEndpointVolume->Release();
        if (pDevice) pDevice->Release();
        if (pDeviceEnumerator) pDeviceEnumerator->Release();
        throw;
    }
    return false;
}

int main()
{
    CoInitialize(0);
    try {
        GetVolumeLevel();

    }
    catch (...) {
        //err handle..
    }
    CoUninitialize();

    getchar();
    return 0;
}

It will return to the volume level of the current speaker.

You can focus on this api: IAudioEndpointVolume::GetMasterVolumeLevelScal

The GetMasterVolumeLevelScalar method gets the master volume level of the audio stream that enters or leaves the audio endpoint device. The volume level is expressed as a normalized, audio-tapered value in the range from 0.0 to 1.0.

Upvotes: 1

Related Questions