Skip to content
0.2.0-dev Download ZIP
114 lines ·4.3 KB Raw
1/**************************************************************************/
2/* audio_driver_xaudio2.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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#pragma once
32
33#include "core/os/mutex.h"
34#include "core/os/thread.h"
35#include "core/templates/safe_refcount.h"
36#include "servers/audio/audio_driver.h"
37
38#include <windows.h>
39
40#include <wrl/client.h>
41#include <xaudio2.h>
42
43class AudioDriverXAudio2 : public AudioDriver {
44 enum {
45 AUDIO_BUFFERS = 2
46 };
47
48 struct XAudio2DriverVoiceCallback : public IXAudio2VoiceCallback {
49 HANDLE buffer_end_event;
50
51 XAudio2DriverVoiceCallback() :
52 buffer_end_event(CreateEvent(nullptr, FALSE, FALSE, nullptr)) {}
53
54 virtual ~XAudio2DriverVoiceCallback() {
55 CloseHandle(buffer_end_event);
56 }
57
58 void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) {
59 SetEvent(buffer_end_event);
60 }
61
62 //Unused methods are stubs
63 void STDMETHODCALLTYPE OnStreamEnd() {}
64 void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() {}
65 void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) {}
66 void STDMETHODCALLTYPE OnBufferStart(void *pBufferContext) {}
67 void STDMETHODCALLTYPE OnLoopEnd(void *pBufferContext) {}
68 void STDMETHODCALLTYPE OnVoiceError(void *pBufferContext, HRESULT Error) {}
69 };
70
71 Thread thread;
72 Mutex mutex;
73
74 int32_t *samples_in = nullptr;
75 int16_t *samples_out[AUDIO_BUFFERS];
76
77 static void thread_func(void *p_udata);
78 int buffer_size = 0;
79
80 unsigned int mix_rate = 0;
81 SpeakerMode speaker_mode = SpeakerMode::SPEAKER_MODE_STEREO;
82
83 int channels = 0;
84
85 SafeFlag active;
86 SafeFlag exit_thread;
87 bool pcm_open = false;
88
89 WAVEFORMATEX wave_format = {};
90 Microsoft::WRL::ComPtr<IXAudio2> xaudio;
91 int current_buffer = 0;
92 IXAudio2MasteringVoice *mastering_voice = nullptr;
93 XAUDIO2_BUFFER xaudio_buffer[AUDIO_BUFFERS];
94 IXAudio2SourceVoice *source_voice = nullptr;
95 XAudio2DriverVoiceCallback voice_callback;
96
97public:
98 virtual const char *get_name() const override {
99 return "XAudio2";
100 }
101
102 virtual Error init() override;
103 virtual void start() override;
104 virtual int get_mix_rate() const override;
105 virtual SpeakerMode get_speaker_mode() const override;
106 virtual float get_latency() override;
107
108 virtual void lock() override;
109 virtual void unlock() override;
110 virtual void finish() override;
111
112 AudioDriverXAudio2();
113 ~AudioDriverXAudio2() {}
114};