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
36
43 int Buffered() override;
44
45 protected:
50 bool DoInit() override;
51
55 void DoClose() override;
56
62 void DoPlay(const uint8_t* buf, size_t len) override;
63
64 // IMMNotificationClient overrides — used to detect audio device changes.
65
67 virtual HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState);
68
70 virtual HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId);
71
73 virtual HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId);
74
81 virtual HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDeviceId);
82
84 virtual HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key);
85
87 virtual ULONG STDMETHODCALLTYPE AddRef();
88
90 virtual ULONG STDMETHODCALLTYPE Release();
91
93 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID** ppvInterface);
94
99 void ThrowIfFailed(HRESULT res);
100
106
107 private:
108 ComPtr<IMMDeviceEnumerator> mDeviceEnumerator;
109 ComPtr<IMMDevice> mDevice;
110 ComPtr<IAudioClient> mClient;
111 ComPtr<IAudioRenderClient> mRenderClient;
112 LONG mRefCount = 1;
113 UINT32 mBufferFrameCount = 0;
114 bool mInitialized = false;
115 bool mStarted = false;
116 int32_t mNumChannels = 2;
117 std::mutex mMutex;
118};
119} // namespace Ship
120#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 and releases the stream's 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.
~WasapiAudioPlayer()
Closes the stream and unregisters the endpoint notification callback.
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