File: XAudio2Stream.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,976 kB
  • ctags: 35,666
  • sloc: cpp: 213,139; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (248 lines) | stat: -rw-r--r-- 5,891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <xaudio2.h>
#include "AudioCommon/AudioCommon.h"
#include "AudioCommon/XAudio2Stream.h"
#include "Common/Event.h"
#include "Common/MsgHandler.h"
#include "Common/Logging/Log.h"

#ifndef XAUDIO2_DLL
#error You are building this module against the wrong version of DirectX. You probably need to remove DXSDK_DIR from your include path.
#endif

struct StreamingVoiceContext : public IXAudio2VoiceCallback
{
private:
	CMixer* const m_mixer;
	Common::Event& m_sound_sync_event;
	IXAudio2SourceVoice* m_source_voice;
	std::unique_ptr<BYTE[]> xaudio_buffer;

	void SubmitBuffer(PBYTE buf_data);

public:
	StreamingVoiceContext(IXAudio2 *pXAudio2, CMixer *pMixer, Common::Event& pSyncEvent);

	~StreamingVoiceContext();

	void StreamingVoiceContext::Stop();
	void StreamingVoiceContext::Play();

	STDMETHOD_(void, OnVoiceError) (THIS_ void* pBufferContext, HRESULT Error) {}
	STDMETHOD_(void, OnVoiceProcessingPassStart) (UINT32) {}
	STDMETHOD_(void, OnVoiceProcessingPassEnd) () {}
	STDMETHOD_(void, OnBufferStart) (void*) {}
	STDMETHOD_(void, OnLoopEnd) (void*) {}
	STDMETHOD_(void, OnStreamEnd) () {}

	STDMETHOD_(void, OnBufferEnd) (void* context);
};

const int NUM_BUFFERS = 3;
const int SAMPLES_PER_BUFFER = 96;

const int NUM_CHANNELS = 2;
const int BUFFER_SIZE = SAMPLES_PER_BUFFER * NUM_CHANNELS;
const int BUFFER_SIZE_BYTES = BUFFER_SIZE * sizeof(s16);

void StreamingVoiceContext::SubmitBuffer(PBYTE buf_data)
{
	XAUDIO2_BUFFER buf = {};
	buf.AudioBytes = BUFFER_SIZE_BYTES;
	buf.pContext = buf_data;
	buf.pAudioData = buf_data;

	m_source_voice->SubmitSourceBuffer(&buf);
}

StreamingVoiceContext::StreamingVoiceContext(IXAudio2 *pXAudio2, CMixer *pMixer, Common::Event& pSyncEvent)
	: m_mixer(pMixer)
	, m_sound_sync_event(pSyncEvent)
	, xaudio_buffer(new BYTE[NUM_BUFFERS * BUFFER_SIZE_BYTES]())
{
	WAVEFORMATEXTENSIBLE wfx = {};

	wfx.Format.wFormatTag       = WAVE_FORMAT_EXTENSIBLE;
	wfx.Format.nSamplesPerSec   = m_mixer->GetSampleRate();
	wfx.Format.nChannels        = 2;
	wfx.Format.wBitsPerSample   = 16;
	wfx.Format.nBlockAlign      = wfx.Format.nChannels*wfx.Format.wBitsPerSample / 8;
	wfx.Format.nAvgBytesPerSec  = wfx.Format.nSamplesPerSec * wfx.Format.nBlockAlign;
	wfx.Format.cbSize           = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
	wfx.Samples.wValidBitsPerSample = 16;
	wfx.dwChannelMask           = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
	wfx.SubFormat               = KSDATAFORMAT_SUBTYPE_PCM;

	// create source voice
	HRESULT hr;
	if (FAILED(hr = pXAudio2->CreateSourceVoice(&m_source_voice, &wfx.Format, XAUDIO2_VOICE_NOSRC, 1.0f, this)))
	{
		PanicAlert("XAudio2 CreateSourceVoice failed: %#X", hr);
		return;
	}

	m_source_voice->Start();

	// start buffers with silence
	for (int i = 0; i != NUM_BUFFERS; ++i)
		SubmitBuffer(xaudio_buffer.get() + (i * BUFFER_SIZE_BYTES));
}

StreamingVoiceContext::~StreamingVoiceContext()
{
	if (m_source_voice)
	{
		m_source_voice->Stop();
		m_source_voice->DestroyVoice();
	}
}

void StreamingVoiceContext::Stop()
{
	if (m_source_voice)
		m_source_voice->Stop();
}

void StreamingVoiceContext::Play()
{
	if (m_source_voice)
		m_source_voice->Start();
}

void StreamingVoiceContext::OnBufferEnd(void* context)
{
	//  buffer end callback; gets SAMPLES_PER_BUFFER samples for a new buffer

	if (!m_source_voice || !context)
		return;

	//m_sound_sync_event->Wait(); // sync
	//m_sound_sync_event->Spin(); // or tight sync

	m_mixer->Mix(static_cast<short*>(context), SAMPLES_PER_BUFFER);
	SubmitBuffer(static_cast<BYTE*>(context));
}

HMODULE XAudio2::m_xaudio2_dll = nullptr;
typedef decltype(&XAudio2Create) XAudio2Create_t;
void *XAudio2::PXAudio2Create = nullptr;

bool XAudio2::InitLibrary()
{
	if (m_xaudio2_dll)
	{
		return true;
	}

	m_xaudio2_dll = ::LoadLibrary(XAUDIO2_DLL);
	if (!m_xaudio2_dll)
	{
		return false;
	}

	if (!PXAudio2Create)
	{
		PXAudio2Create = (XAudio2Create_t)::GetProcAddress(m_xaudio2_dll, "XAudio2Create");
		if (!PXAudio2Create)
		{
			::FreeLibrary(m_xaudio2_dll);
			m_xaudio2_dll = nullptr;
			return false;
		}
	}

	return true;
}

XAudio2::XAudio2()
	: m_mastering_voice(nullptr)
	, m_volume(1.0f)
	, m_cleanup_com(SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
{
}

XAudio2::~XAudio2()
{
	Stop();
	if (m_cleanup_com)
		CoUninitialize();
}

bool XAudio2::Start()
{
	HRESULT hr;

	// callback doesn't seem to run on a specific CPU anyways
	IXAudio2* xaudptr;
	if (FAILED(hr = ((XAudio2Create_t)PXAudio2Create)(&xaudptr, 0, XAUDIO2_DEFAULT_PROCESSOR)))
	{
		PanicAlert("XAudio2 init failed: %#X", hr);
		Stop();
		return false;
	}
	m_xaudio2 = std::unique_ptr<IXAudio2, Releaser>(xaudptr);

	// XAudio2 master voice
	// XAUDIO2_DEFAULT_CHANNELS instead of 2 for expansion?
	if (FAILED(hr = m_xaudio2->CreateMasteringVoice(&m_mastering_voice, 2, m_mixer->GetSampleRate())))
	{
		PanicAlert("XAudio2 master voice creation failed: %#X", hr);
		Stop();
		return false;
	}

	// Volume
	m_mastering_voice->SetVolume(m_volume);

	m_voice_context = std::unique_ptr<StreamingVoiceContext>
		(new StreamingVoiceContext(m_xaudio2.get(), m_mixer.get(), m_sound_sync_event));

	return true;
}

void XAudio2::SetVolume(int volume)
{
	//linear 1- .01
	m_volume = (float)volume / 100.f;

	if (m_mastering_voice)
		m_mastering_voice->SetVolume(m_volume);
}

void XAudio2::Clear(bool mute)
{
	m_muted = mute;

	if (m_voice_context)
	{
		if (m_muted)
			m_voice_context->Stop();
		else
			m_voice_context->Play();
	}
}

void XAudio2::Stop()
{
	//m_sound_sync_event.Set();

	m_voice_context.reset();

	if (m_mastering_voice)
	{
		m_mastering_voice->DestroyVoice();
		m_mastering_voice = nullptr;
	}

	m_xaudio2.reset(); // release interface

	if (m_xaudio2_dll)
	{
		::FreeLibrary(m_xaudio2_dll);
		m_xaudio2_dll = nullptr;
		PXAudio2Create = nullptr;
	}
}