3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UnrealEngineでWindowsのマスターボリュームを取得する

Posted at

UnrealEngineでWindowsのマスターボリュームを取得してみたので内容をメモしておきます。
Windows Vista以降ではマルチメディア関数(waveXxx 関数など)ではなくてコアオーディオAPI(MMDeviceなど)が使われるようになったみたいなのでMMDeviceを使っています。

確認した環境

  • UnrealEngine 4.27、5.2.0
  • Windows11 22H2

参照サイト

Blueprintノード

以下のように音声入力・出力の音量の取得と、Windowsなどで音量やミュート状態が変更されたときにDelegateで取得できるようにしてみました。

スクリーンショット 2023-05-29 124319.png

C++コード

コードを見てもらえばわかりますが、音量やミュート状態が変更されたときにイベントを受け取る関数を複数呼び出しすると、以前の分が解放されてしまって最後の分しか有効になりません。

MyUtils.h
#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyUtils.generated.h"

class CVolumeNotification;
struct IAudioEndpointVolume;

DECLARE_DYNAMIC_DELEGATE_TwoParams(FMicChangeDelegate, float, Volume, bool, IsMute);

/**
 * 音声入力/出力の種別
 */
UENUM(BlueprintType)
enum class ESoundTypeEnum : uint8
{
	Mic = 0,
	Speaker = 1,
};

/**
 * 音量関係取得のためのUtilityクラス
 */
UCLASS()
class WINSOUNDTEST_API UMyUtils : public UObject
{
	GENERATED_BODY()

public:
    /*
     * 音量取得
     */
	UFUNCTION(BlueprintCallable)
		static float GetSoundVolumeLevel(ESoundTypeEnum Type);

    /*
     * 音量変更時の通知登録
     */
	UFUNCTION(BlueprintCallable)
		static void RegisterSoundChangeNotify(ESoundTypeEnum Type, UPARAM(DisplayName = "Event") FMicChangeDelegate Delegate);

private:
#if PLATFORM_WINDOWS
	static TSharedPtr<CVolumeNotification> MicVolumeNotification;
	static TSharedPtr<IAudioEndpointVolume> PMicAudioEndVol;
	static TSharedPtr<CVolumeNotification> SpeakerVolumeNotification;
	static TSharedPtr<IAudioEndpointVolume> PSpeakerAudioEndVol;
#endif
};
MyUtils.cpp
#include "MyUtils.h"

#if PLATFORM_WINDOWS
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/AllowWindowsPlatformAtomics.h"
#include "Windows/PreWindowsApi.h"

#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <stdio.h>
#include <locale.h>
#include <tchar.h>

#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformAtomics.h"
#include "Windows/HideWindowsPlatformTypes.h"
#endif

#if PLATFORM_WINDOWS
TSharedPtr<CVolumeNotification> UMyUtils::MicVolumeNotification;
TSharedPtr<IAudioEndpointVolume> UMyUtils::PMicAudioEndVol;
TSharedPtr<CVolumeNotification> UMyUtils::SpeakerVolumeNotification;
TSharedPtr<IAudioEndpointVolume> UMyUtils::PSpeakerAudioEndVol;

/**
 * 通知受け取り用のクラス
 */
class CVolumeNotification : public IAudioEndpointVolumeCallback
{
    FMicChangeDelegate _Delegate;
    int32 _cRef;
public:
    CVolumeNotification(FMicChangeDelegate InDelegate) : _Delegate(InDelegate), _cRef(1) {}

    virtual ~CVolumeNotification() {}

    ULONG STDMETHODCALLTYPE AddRef()
    {
        return FPlatformAtomics::InterlockedIncrement(&_cRef);
    }

    ULONG STDMETHODCALLTYPE Release()
    {
        ULONG ulRef = FPlatformAtomics::InterlockedDecrement(&_cRef);
        if (0 == ulRef)
        {
            delete this;
        }
        return ulRef;
    }

    HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID** ppvInterface)
    {
        if (IID_IUnknown == riid)
        {
            AddRef();
            *ppvInterface = (IUnknown*)this;
        }
        else if (__uuidof(IAudioEndpointVolumeCallback) == riid)
        {
            AddRef();
            *ppvInterface = (IAudioEndpointVolumeCallback*)this;
        }
        else
        {
            *ppvInterface = NULL;
            return E_NOINTERFACE;
        }

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify)
    {
        if (!pNotify)
        {
            return E_INVALIDARG;
        }

        _Delegate.ExecuteIfBound(pNotify->fMasterVolume, pNotify->bMuted);
        UE_LOG(LogTemp, Warning, TEXT("Nofity:%f, %d"), pNotify->fMasterVolume, pNotify->bMuted);

        return S_OK;
    }
};
#endif

/*
 * 音量取得
 */
float UMyUtils::GetSoundVolumeLevel(ESoundTypeEnum Type)
{
#if PLATFORM_WINDOWS
    HRESULT hr;

    IMMDeviceEnumerator* pEnum = NULL;
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, IID_PPV_ARGS(&pEnum));
    if (FAILED(hr)) {
        return -1.0f;
    }
    TSharedPtr<IMMDeviceEnumerator> PEnum(pEnum, [](IMMDeviceEnumerator* enumerator) {
        if (enumerator) { enumerator->Release(); }
        });

    IMMDevice* pEndpoint = NULL;
    hr = PEnum->GetDefaultAudioEndpoint(Type == ESoundTypeEnum::Speaker ? eRender : eCapture, eConsole, &pEndpoint);
    if (FAILED(hr))
    {
        return -1.0f;
    }
    TSharedPtr<IMMDevice> PEndPoint(pEndpoint, [](IMMDevice* device) {
        if (device) { device->Release(); }
        });

    IAudioEndpointVolume* pAudioEndVol = NULL;
    hr = PEndPoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pAudioEndVol);
    if (FAILED(hr))
    {
        return -1.0f;
    }
    TSharedPtr<IAudioEndpointVolume> PAudioEndVol
        = TSharedPtr<IAudioEndpointVolume>(pAudioEndVol, [](IAudioEndpointVolume* volume) {
              if (volume) { volume->Release(); }
          });

    float level;
    hr = PAudioEndVol->GetMasterVolumeLevelScalar(&level);
    if (FAILED(hr))
    {
        return -1.0f;
    }
    UE_LOG(LogTemp, Warning, TEXT("VolumeLevel:%f"), level);

    return level;
#else
    return -1.0f;
#endif
}

/*
 * 音量変更時の通知登録
 */
void UMyUtils::RegisterSoundChangeNotify(ESoundTypeEnum Type, FMicChangeDelegate Delegate)
{
#if PLATFORM_WINDOWS
    HRESULT hr;

    TSharedPtr<IAudioEndpointVolume>& PAudioEndVol
        = Type == ESoundTypeEnum::Speaker ? PSpeakerAudioEndVol : PMicAudioEndVol;

    IMMDeviceEnumerator* pEnum = NULL;
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, IID_PPV_ARGS(&pEnum));
    if (FAILED(hr)) {
        return;
    }
    TSharedPtr<IMMDeviceEnumerator> PEnum(pEnum, [](IMMDeviceEnumerator* enumerator) {
        if (enumerator) { enumerator->Release(); }
        });

    IMMDevice* pEndpoint = NULL;
    hr = PEnum->GetDefaultAudioEndpoint(Type == ESoundTypeEnum::Speaker ? eRender : eCapture, eConsole, &pEndpoint);
    if (FAILED(hr))
    {
        return;
    }
    TSharedPtr<IMMDevice> PEndPoint(pEndpoint, [](IMMDevice* device) {
        if (device) { device->Release(); }
        });

    IAudioEndpointVolume* pAudioEndVol = NULL;
    hr = PEndPoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pAudioEndVol);
    if (FAILED(hr))
    {
        return;
    }

    PAudioEndVol = TSharedPtr<IAudioEndpointVolume>(pAudioEndVol, [](IAudioEndpointVolume* volume) {
        if (volume) { volume->Release(); }
    });

    TSharedPtr<CVolumeNotification>& VolumeNotification = 
        Type == ESoundTypeEnum::Speaker ? SpeakerVolumeNotification : MicVolumeNotification;
    VolumeNotification = TSharedPtr<CVolumeNotification>(new CVolumeNotification(Delegate));

    PAudioEndVol->RegisterControlChangeNotify(VolumeNotification.Get());
#endif
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?