libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
WasapiAudioPlayer.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef _WIN32
4
5#include "AudioPlayer.h"
6#include <wrl/client.h>
7#include <mmdeviceapi.h>
8#include <audioclient.h>
9#include <mutex>
10
11using namespace Microsoft::WRL;
12
13namespace Ship {
23class WasapiAudioPlayer : public AudioPlayer, public IMMNotificationClient {
24 public:
30 }
31
37 int Buffered() override;
38
39 protected:
44 bool DoInit() override;
45
49 void DoClose() override;
50
56 void DoPlay(const uint8_t* buf, size_t len) override;
57
58 // IMMNotificationClient overrides — used to detect audio device changes.
59
61 virtual HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState);
62
64 virtual HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId);
65
67 virtual HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId);
68
75 virtual HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDeviceId);
76
78 virtual HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key);
79
81 virtual ULONG STDMETHODCALLTYPE AddRef();
82
84 virtual ULONG STDMETHODCALLTYPE Release();
85
87 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID** ppvInterface);
88
93 void ThrowIfFailed(HRESULT res);
94
100
101 private:
102 ComPtr<IMMDeviceEnumerator> mDeviceEnumerator;
103 ComPtr<IMMDevice> mDevice;
104 ComPtr<IAudioClient> mClient;
105 ComPtr<IAudioRenderClient> mRenderClient;
106 LONG mRefCount = 1;
107 UINT32 mBufferFrameCount = 0;
108 bool mInitialized = false;
109 bool mStarted = false;
110 int32_t mNumChannels = 2;
111 std::mutex mMutex;
112};
113} // namespace Ship
114#endif
Abstract base class for platform-specific audio output backends.
Definition AudioPlayer.h:32
AudioPlayer implementation backed by the Windows Audio Session API (WASAPI).
Definition WasapiAudioPlayer.h:23
virtual ULONG STDMETHODCALLTYPE AddRef()
Increments the IUnknown reference count.
WasapiAudioPlayer(AudioSettings settings)
Constructs a WasapiAudioPlayer with the given audio settings.
Definition WasapiAudioPlayer.h:29
void DoClose() override
Stops playback, unregisters device notifications, and releases COM objects.
void ThrowIfFailed(HRESULT res)
Throws a Ship::HResultException if res is a failed HRESULT.
virtual HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState)
Called when the state of an audio endpoint device changes.
bool DoInit() override
Opens the WASAPI shared-mode audio client and registers device notifications.
virtual HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDeviceId)
Called when the default audio endpoint for a given data-flow or role changes.
bool SetupStream()
(Re-)opens the WASAPI audio stream on the current default device.
int Buffered() override
Returns the number of frames currently queued in the WASAPI render buffer.
virtual HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId)
Called when a new audio endpoint device is added to the system.
virtual ULONG STDMETHODCALLTYPE Release()
Decrements the IUnknown reference count.
virtual HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId)
Called when an audio endpoint device is removed from the system.
virtual HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key)
Called when a property value of an audio endpoint device changes.
void DoPlay(const uint8_t *buf, size_t len) override
Writes interleaved PCM samples to the WASAPI render buffer.
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface)
Returns the requested interface if supported; otherwise E_NOINTERFACE.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
Configuration parameters shared by all AudioPlayer backends.
Definition AudioPlayer.h:14