1 /*************************************************************************/
2 /*  audio_driver_xaudio2.h                                               */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifndef AUDIO_DRIVER_XAUDIO2_H
32 #define AUDIO_DRIVER_XAUDIO2_H
33 
34 #include "core/os/mutex.h"
35 #include "core/os/thread.h"
36 #include "servers/audio_server.h"
37 
38 #include <mmsystem.h>
39 #include <windows.h>
40 #include <wrl/client.h>
41 #include <xaudio2.h>
42 
43 class AudioDriverXAudio2 : public AudioDriver {
44 
45 	enum {
46 		AUDIO_BUFFERS = 2
47 	};
48 
49 	struct XAudio2DriverVoiceCallback : public IXAudio2VoiceCallback {
50 
51 		HANDLE buffer_end_event;
XAudio2DriverVoiceCallbackXAudio2DriverVoiceCallback52 		XAudio2DriverVoiceCallback() :
53 				buffer_end_event(CreateEvent(NULL, FALSE, FALSE, NULL)) {}
OnBufferEndXAudio2DriverVoiceCallback54 		void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) {
55 			SetEvent(buffer_end_event);
56 		}
57 
58 		//Unused methods are stubs
OnStreamEndXAudio2DriverVoiceCallback59 		void STDMETHODCALLTYPE OnStreamEnd() {}
OnVoiceProcessingPassEndXAudio2DriverVoiceCallback60 		void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() {}
OnVoiceProcessingPassStartXAudio2DriverVoiceCallback61 		void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) {}
OnBufferStartXAudio2DriverVoiceCallback62 		void STDMETHODCALLTYPE OnBufferStart(void *pBufferContext) {}
OnLoopEndXAudio2DriverVoiceCallback63 		void STDMETHODCALLTYPE OnLoopEnd(void *pBufferContext) {}
OnVoiceErrorXAudio2DriverVoiceCallback64 		void STDMETHODCALLTYPE OnVoiceError(void *pBufferContext, HRESULT Error) {}
65 	};
66 
67 	Thread *thread;
68 	Mutex *mutex;
69 
70 	int32_t *samples_in;
71 	int16_t *samples_out[AUDIO_BUFFERS];
72 
73 	static void thread_func(void *p_udata);
74 	int buffer_size;
75 
76 	unsigned int mix_rate;
77 	SpeakerMode speaker_mode;
78 
79 	int channels;
80 
81 	bool active;
82 	bool thread_exited;
83 	mutable bool exit_thread;
84 	bool pcm_open;
85 
86 	WAVEFORMATEX wave_format;
87 	Microsoft::WRL::ComPtr<IXAudio2> xaudio;
88 	int current_buffer;
89 	IXAudio2MasteringVoice *mastering_voice;
90 	XAUDIO2_BUFFER xaudio_buffer[AUDIO_BUFFERS];
91 	IXAudio2SourceVoice *source_voice;
92 	XAudio2DriverVoiceCallback voice_callback;
93 
94 public:
95 	const char *get_name() const;
96 
97 	virtual Error init();
98 	virtual void start();
99 	virtual int get_mix_rate() const;
100 	virtual SpeakerMode get_speaker_mode() const;
101 	virtual float get_latency();
102 	virtual void lock();
103 	virtual void unlock();
104 	virtual void finish();
105 
106 	AudioDriverXAudio2();
107 	~AudioDriverXAudio2();
108 };
109 
110 #endif
111