forked from Ableton/link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioPlatform_Wasapi.cpp
331 lines (287 loc) · 9.18 KB
/
AudioPlatform_Wasapi.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* Copyright 2016, Ableton AG, Berlin. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you would like to incorporate Link into a proprietary software application,
* please contact <link-devs@ableton.com>.
*/
#include "AudioPlatform_Wasapi.hpp"
#include <Comdef.h>
#include <chrono>
// WARNING: This file provides an audio driver for Windows using WASAPI. This driver is
// considered experimental and has problems with low-latency playback. Please consider
// using the ASIO driver instead.
namespace ableton
{
namespace linkaudio
{
// GUID identifiers used to when looking up COM enumerators and devices
static const IID kMMDeviceEnumeratorId = __uuidof(MMDeviceEnumerator);
static const IID kIMMDeviceEnumeratorId = __uuidof(IMMDeviceEnumerator);
static const IID kAudioClientId = __uuidof(IAudioClient);
static const IID kAudioRenderClientId = __uuidof(IAudioRenderClient);
// Controls how large the driver's ring buffer will be, expressed in terms of
// 100-nanosecond units. This value also influences the overall driver latency.
static const REFERENCE_TIME kBufferDuration = 1000000;
// How long to block the runloop while waiting for an event callback.
static const DWORD kWaitTimeoutInMs = 2000;
void fatalError(HRESULT result, LPCTSTR context)
{
if (result > 0)
{
_com_error error(result);
LPCTSTR errorMessage = error.ErrorMessage();
std::cerr << context << ": " << errorMessage << std::endl;
}
else
{
std::cerr << context << std::endl;
}
std::terminate();
}
DWORD renderAudioRunloop(LPVOID lpParam)
{
AudioPlatform* platform = static_cast<AudioPlatform*>(lpParam);
return platform->audioRunloop();
}
AudioPlatform::AudioPlatform(Link& link)
: mEngine(link)
, mDevice(nullptr)
, mAudioClient(nullptr)
, mRenderClient(nullptr)
, mStreamFormat(nullptr)
, mEventHandle(nullptr)
, mAudioThreadHandle(nullptr)
, mIsRunning(false)
, mSampleTime(0)
{
initialize();
mEngine.setBufferSize(bufferSize());
mEngine.setSampleRate(mStreamFormat->nSamplesPerSec);
start();
}
AudioPlatform::~AudioPlatform()
{
// WARNING: Here be dragons!
// The WASAPI driver is not thread-safe, and crashes may occur when shutting down due
// to these fields being concurrently accessed in the audio thread. Introducing a mutex
// in the audio thread is not an appropriate solution to fix this race condition; a more
// robust solution needs to be considered instead.
if (mDevice != nullptr)
{
mDevice->Release();
}
if (mAudioClient != nullptr)
{
mAudioClient->Release();
}
if (mRenderClient != nullptr)
{
mRenderClient->Release();
}
CoTaskMemFree(mStreamFormat);
}
UINT32 AudioPlatform::bufferSize()
{
UINT32 bufferSize;
HRESULT result = mAudioClient->GetBufferSize(&bufferSize);
if (FAILED(result))
{
fatalError(result, "Could not get buffer size");
return 0; // not reached
}
return bufferSize;
}
void AudioPlatform::initialize()
{
HRESULT result = CoInitialize(nullptr);
if (FAILED(result))
{
fatalError(result, "Could not initialize COM library");
}
IMMDeviceEnumerator* enumerator = nullptr;
result = CoCreateInstance(kMMDeviceEnumeratorId, nullptr, CLSCTX_ALL,
kIMMDeviceEnumeratorId, (void**)&enumerator);
if (FAILED(result))
{
fatalError(result, "Could not create device instance");
}
result = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &(mDevice));
if (FAILED(result))
{
fatalError(result, "Could not get default audio endpoint");
}
else
{
enumerator->Release();
enumerator = nullptr;
}
result =
mDevice->Activate(kAudioClientId, CLSCTX_ALL, nullptr, (void**)&(mAudioClient));
if (FAILED(result))
{
fatalError(result, "Could not activate audio device");
}
result = mAudioClient->GetMixFormat(&(mStreamFormat));
if (FAILED(result))
{
fatalError(result, "Could not get mix format");
}
if (mStreamFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
{
WAVEFORMATEXTENSIBLE* streamFormatEx =
reinterpret_cast<WAVEFORMATEXTENSIBLE*>(mStreamFormat);
if (streamFormatEx->SubFormat != KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
{
fatalError(0, "Sorry, only IEEE floating point streams are supported");
}
}
else
{
fatalError(0, "Sorry, only extensible wave streams are supported");
}
result = mAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK, kBufferDuration, 0, mStreamFormat, nullptr);
if (FAILED(result))
{
fatalError(result, "Could not initialize audio device");
}
mEventHandle = CreateEvent(nullptr, false, false, nullptr);
if (mEventHandle == nullptr)
{
fatalError(result, "Could not create event handle");
}
result = mAudioClient->GetService(kAudioRenderClientId, (void**)&(mRenderClient));
if (FAILED(result))
{
fatalError(result, "Could not get audio render service");
}
mIsRunning = true;
LPTHREAD_START_ROUTINE threadEntryPoint =
reinterpret_cast<LPTHREAD_START_ROUTINE>(renderAudioRunloop);
mAudioThreadHandle = CreateThread(nullptr, 0, threadEntryPoint, this, 0, nullptr);
if (mAudioThreadHandle == nullptr)
{
fatalError(GetLastError(), "Could not create audio thread");
}
}
void AudioPlatform::start()
{
UINT32 bufSize = bufferSize();
BYTE* buffer;
HRESULT result = mRenderClient->GetBuffer(bufSize, &buffer);
if (FAILED(result))
{
fatalError(result, "Could not get render client buffer (in start audio engine)");
}
result = mRenderClient->ReleaseBuffer(bufSize, 0);
if (FAILED(result))
{
fatalError(result, "Could not release buffer");
}
result = mAudioClient->SetEventHandle(mEventHandle);
if (FAILED(result))
{
fatalError(result, "Could not set event handle to audio client");
}
REFERENCE_TIME latency;
result = mAudioClient->GetStreamLatency(&latency);
if (FAILED(result))
{
fatalError(result, "Could not get stream latency");
}
result = mAudioClient->Start();
if (FAILED(result))
{
fatalError(result, "Could not start audio client");
}
}
DWORD AudioPlatform::audioRunloop()
{
while (mIsRunning)
{
DWORD wait = WaitForSingleObject(mEventHandle, kWaitTimeoutInMs);
if (wait != WAIT_OBJECT_0)
{
mIsRunning = false;
mAudioClient->Stop();
return wait;
}
// Get the amount of padding, which basically is the amount of data in the driver's
// ring buffer that is filled with unread data. Thus, subtracting this amount from
// the buffer size gives the effective buffer size, which is the amount of frames
// that can be safely written to the driver.
UINT32 paddingFrames;
HRESULT result = mAudioClient->GetCurrentPadding(&paddingFrames);
if (FAILED(result))
{
fatalError(result, "Could not get number of padding frames");
}
const UINT32 numSamples = bufferSize() - paddingFrames;
BYTE* buffer;
result = mRenderClient->GetBuffer(numSamples, &buffer);
if (FAILED(result))
{
fatalError(result, "Could not get render client buffer (in callback)");
}
const double sampleRate = static_cast<double>(mStreamFormat->nSamplesPerSec);
using namespace std::chrono;
const auto bufferDuration =
duration_cast<microseconds>(duration<double>{numSamples / sampleRate});
const auto hostTime = mHostTimeFilter.sampleTimeToHostTime(mSampleTime);
mSampleTime += numSamples;
const auto bufferBeginAtOutput = hostTime + mEngine.mOutputLatency;
mEngine.audioCallback(bufferBeginAtOutput, numSamples);
float* floatBuffer = reinterpret_cast<float*>(buffer);
for (WORD i = 0; i < numSamples; ++i)
{
if (i >= mEngine.mBuffer.size())
{
break;
}
for (WORD j = 0; j < mStreamFormat->nChannels; ++j)
{
floatBuffer[j + (i * mStreamFormat->nChannels)] =
static_cast<float>(mEngine.mBuffer[i]);
}
}
// Write the buffer to the audio driver and subsequently free the buffer memory
result = mRenderClient->ReleaseBuffer(numSamples, 0);
if (FAILED(result))
{
fatalError(result, "Error rendering data");
}
} // end of runloop
mIsRunning = false;
return 0;
}
// void fillBuffer(MetronomeSynth& metronome,
// const UINT32 startFrame,
// const UINT32 numSamples,
// const UINT32 numChannels,
// BYTE* buffer)
//{
// float* floatBuffer = reinterpret_cast<float*>(buffer);
// UINT32 frame = startFrame;
// while (frame < numSamples * numChannels)
// {
// const float sample = static_cast<float>(metronome.getSample());
// for (UINT32 channel = 0; channel < numChannels; ++channel)
// {
// floatBuffer[frame++] = sample;
// }
// }
//}
} // namespace linkaudio
} // namespace ableton