Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/xaudio2/audio_driver_xaudio2.h
9973 views
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_server.h"
37
38
#include <mmsystem.h>
39
#define WIN32_LEAN_AND_MEAN
40
#include <windows.h>
41
#include <wrl/client.h>
42
#include <xaudio2.h>
43
44
class AudioDriverXAudio2 : public AudioDriver {
45
enum {
46
AUDIO_BUFFERS = 2
47
};
48
49
struct XAudio2DriverVoiceCallback : public IXAudio2VoiceCallback {
50
HANDLE buffer_end_event;
51
XAudio2DriverVoiceCallback() :
52
buffer_end_event(CreateEvent(nullptr, FALSE, FALSE, nullptr)) {}
53
void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) {
54
SetEvent(buffer_end_event);
55
}
56
57
//Unused methods are stubs
58
void STDMETHODCALLTYPE OnStreamEnd() {}
59
void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() {}
60
void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) {}
61
void STDMETHODCALLTYPE OnBufferStart(void *pBufferContext) {}
62
void STDMETHODCALLTYPE OnLoopEnd(void *pBufferContext) {}
63
void STDMETHODCALLTYPE OnVoiceError(void *pBufferContext, HRESULT Error) {}
64
};
65
66
Thread thread;
67
Mutex mutex;
68
69
int32_t *samples_in = nullptr;
70
int16_t *samples_out[AUDIO_BUFFERS];
71
72
static void thread_func(void *p_udata);
73
int buffer_size = 0;
74
75
unsigned int mix_rate = 0;
76
SpeakerMode speaker_mode = SpeakerMode::SPEAKER_MODE_STEREO;
77
78
int channels = 0;
79
80
SafeFlag active;
81
SafeFlag exit_thread;
82
bool pcm_open = false;
83
84
WAVEFORMATEX wave_format = { 0 };
85
Microsoft::WRL::ComPtr<IXAudio2> xaudio;
86
int current_buffer = 0;
87
IXAudio2MasteringVoice *mastering_voice = nullptr;
88
XAUDIO2_BUFFER xaudio_buffer[AUDIO_BUFFERS];
89
IXAudio2SourceVoice *source_voice = nullptr;
90
XAudio2DriverVoiceCallback voice_callback;
91
92
public:
93
virtual const char *get_name() const override {
94
return "XAudio2";
95
}
96
97
virtual Error init() override;
98
virtual void start() override;
99
virtual int get_mix_rate() const override;
100
virtual SpeakerMode get_speaker_mode() const override;
101
virtual float get_latency() override;
102
103
virtual void lock() override;
104
virtual void unlock() override;
105
virtual void finish() override;
106
107
AudioDriverXAudio2();
108
~AudioDriverXAudio2() {}
109
};
110
111