aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Output/out_wasapi/main.cpp
blob: 8878cedb9ce0041f6bf58cff01aca6c6a080f47e (plain) (blame)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "../Winamp/OUT.H"
#include "api.h"
#include "resource.h"
#include <Mmdeviceapi.h>
#include <Audioclient.h>
#include <Audiosessiontypes.h>
#include "../winamp/wa_ipc.h"
#include <api/service/waServiceFactory.h>
#include <strsafe.h>

#define WASAPI_PLUGIN_VERSION L"0.3"

constexpr auto VolumeLevelMultiplier = 255;
static wchar_t plugin_name[256];

// wasabi based services for localisation support
api_service *WASABI_API_SVC = 0;
api_language *WASABI_API_LNG = 0;
HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;

static const UINT32 REFTIMES_PER_SEC = 10000000;
static const UINT32 REFTIMES_PER_MILLISEC = 10000;

// TODO(benski) is there some library that has this
static const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
static const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
static const IID IID_IAudioClient = __uuidof(IAudioClient);
static const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
static const IID IID_IAudioClock = __uuidof(IAudioClock);

static bool InitializedCOM;
extern Out_Module plugin;

static IAudioClient *client=0;
static IAudioRenderClient *render_client=0;
static IAudioClock *clock=0;
static ISimpleAudioVolume *audio_volume = 0;
static IChannelAudioVolume *channel_volume = 0;

static UINT32 bufferFrameCount;
static WORD bytes_per_frame;
static UINT64 frequency=0;
static UINT32 sample_rate;
static double start_time_ms = 0;
static bool paused=false;
static float start_volume = 1.0;
static float start_pan = 0;
WAVEFORMATEXTENSIBLE WaveFormatForParameters(int samplerate, int numchannels, int bitspersamp);
static void SetVolume(int volume);
static void SetPan(int pan);
static CRITICAL_SECTION ThreadSync;


static void Config(HWND hwndParent)
{
}

static int DoAboutMessageBox(HWND parent, wchar_t* title, wchar_t* message)
{
	MSGBOXPARAMSW msgbx = {sizeof(MSGBOXPARAMSW),0};
	msgbx.lpszText = message;
	msgbx.lpszCaption = title;
	msgbx.lpszIcon = MAKEINTRESOURCEW(102);
	msgbx.hInstance = GetModuleHandle(0);
	msgbx.dwStyle = MB_USERICON;
	msgbx.hwndOwner = parent;
	return MessageBoxIndirectW(&msgbx);
}

static void About(HWND hwndParent)
{
	wchar_t message[1024], text[1024] =L"";
	WASABI_API_LNGSTRINGW_BUF(IDS_NULLSOFT_WASAPI_OLD,text,1024);
	StringCchPrintfW(message, 1024, WASABI_API_LNGSTRINGW(IDS_ABOUT_TEXT),
					 plugin.description, TEXT(__DATE__));
	DoAboutMessageBox(hwndParent,text,message);
}

static void Init()
{

	/*
	HRESULT hr;
	hr=CoInitializeEx(0, COINIT_MULTITHREADED);
	if (SUCCEEDED(hr)) {
	InitializedCOM = true;
	} else {
	InitializedCOM = false;
	}
	*/
	// loader so that we can get the localisation service api for use
	WASABI_API_SVC = (api_service*)SendMessage(plugin.hMainWindow, WM_WA_IPC, 0, IPC_GET_API_SERVICE);
	if (WASABI_API_SVC == (api_service*)1) WASABI_API_SVC = NULL;

	waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(languageApiGUID);
	if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());

	// need to have this initialised before we try to do anything with localisation features
	WASABI_API_START_LANG(plugin.hDllInstance,OutWasapiLangGUID);

	StringCbPrintfW(plugin_name,sizeof(plugin_name),WASABI_API_LNGSTRINGW(IDS_NULLSOFT_WASAPI), WASAPI_PLUGIN_VERSION);
	plugin.description = (char *)plugin_name;
}

static void Quit()
{
	/*
	if (InitializedCOM) {
	CoUninitialize();
	}*/
}

static int Open(int samplerate, int numchannels, int bitspersamp, int bufferlenms, int prebufferms)
{
	
	CoInitialize(0);
	IMMDeviceEnumerator *enumerator=0;
	IMMDevice *device=0;
	sample_rate = samplerate;
	HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator);
	hr = enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &device);

	hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&client);
	if (FAILED(hr)) {
		wchar_t temp[1234];
		wsprintf(temp, L"device->Activate: %x", hr);
		::MessageBox(NULL, temp, L"error", MB_OK);
		return -1;
	}

	WAVEFORMATEXTENSIBLE wave_format = WaveFormatForParameters(samplerate, numchannels, bitspersamp);
	bytes_per_frame = wave_format.Format.nBlockAlign;

	hr = client->Initialize(
		AUDCLNT_SHAREMODE_SHARED,
		0x80000000,
		1 * REFTIMES_PER_SEC,
		0,
		(WAVEFORMATEX *)&wave_format,
		NULL);
	if (FAILED(hr)) {
		wchar_t temp[1234];
		wsprintf(temp, L"client->Initialize: %x", hr);
		::MessageBox(NULL, temp, L"error", MB_OK);
		return -1;
	}

	// Get the actual size of the allocated buffer.
	hr = client->GetBufferSize(&bufferFrameCount);
	if (FAILED(hr)) {
		wchar_t temp[1234];
		wsprintf(temp, L"client->GetBufferSize: %x", hr);
		::MessageBox(NULL, temp, L"error", MB_OK);
		return -1;
	}
	hr = client->GetService(
		IID_IAudioRenderClient,
		(void**)&render_client);
	if (FAILED(hr)) {
		wchar_t temp[1234];
		wsprintf(temp, L"client->GetService(IID_IAudioRenderClient): %x", hr);
	::MessageBox(NULL, temp, L"error", MB_OK);
	return -1;
	}
	hr = client->GetService(
		IID_IAudioClock,
		(void**)&clock);
	if (FAILED(hr)) {
		wchar_t temp[1234];
		wsprintf(temp, L"client->GetService(IID_IAudioClock): %x", hr);
		::MessageBox(NULL, temp, L"error", MB_OK);
		return -1;
	}
	hr = clock->GetFrequency(&frequency);

	hr = client->GetService(__uuidof(ISimpleAudioVolume), reinterpret_cast<void **>( & audio_volume));

	hr = client->GetService(__uuidof(IChannelAudioVolume), (void **)&channel_volume);

	start_time_ms = 0;
	paused=false;
	client->Start();

	// Start volume is in range 0.0 to 1.0, should be converted
	SetVolume((int)(start_volume * VolumeLevelMultiplier));
	
	SetPan((int)start_pan);
	
	return 1000;
}

static void Close()
{
	
	if (client) {
		client->Stop();
		client->Release();
		client=0;
	}

	if (render_client) {
		render_client->Release();
		render_client=0;
	}

	if (clock) {
		clock->Release();
		clock=0;
	}

	if (audio_volume) {
		audio_volume->Release();
		audio_volume=0;
	}

	if (channel_volume) {
		channel_volume->Release();
		channel_volume=0;
	}
	
}

static int CanWrite()
{

	if (client) {
		UINT32 numFramesPadding;
		HRESULT hr = client->GetCurrentPadding(&numFramesPadding);
		return (bufferFrameCount - numFramesPadding) * bytes_per_frame;
	}
	else {
		return 0;
	}
}

static int Write(char* buf, int len)
{
	
	if (!render_client) 
	{
		return -1;
	}
	else 
	{
		int LenghtToWrite = CanWrite();
		if (LenghtToWrite > 0 && LenghtToWrite >= len)
		{
			BYTE* data;
			render_client->GetBuffer(len / bytes_per_frame, &data);
			memcpy(data, buf, len);
			render_client->ReleaseBuffer(len / bytes_per_frame, 0);
		}
	}

	return 0;
	
}



static int IsPlaying()
{
	return CanWrite() == 0;
}

static int Pause(int pause)
{
	
	
	int old_paused = paused?1:0;
	if (client) {
		if (pause) {
			client->Stop();
			paused=true;
		} else {
			client->Start();
			paused=false;
		}
	}
	
	return old_paused;
}

static void SetVolume(int volume)
{
	
	
	float fVolume = (float)volume / (float)VolumeLevelMultiplier;
	if (volume >= 0) {
		start_volume = fVolume;
		if (audio_volume) {
			audio_volume->SetMasterVolume(fVolume, 0);
		} 
	}
	
}

static void SetPan(int pan)
{
	
	
	float fPan = (float)pan/128.0f;
	if (channel_volume) {
		start_pan = fPan;
		if (fPan < 0) { 
			channel_volume->SetChannelVolume(0, 1.0f, NULL);
			channel_volume->SetChannelVolume(1, 1.0f-fPan, NULL);
		} else if (fPan > 0) {
			channel_volume->SetChannelVolume(1, 1.0f, NULL);
			channel_volume->SetChannelVolume(0, 1.0f-fPan, NULL);
		}
	}
	
}

static void Flush(int t)
{
	
	
	if (client) {
		client->Stop();
		client->Reset();
		start_time_ms = t;
		client->Start();
	}
	
}

static double GetOutputTimeAsDouble()
{
	
	
	if (clock) {
		UINT64 position;
		HRESULT hr = clock->GetPosition(&position, NULL);
		double output_time = (double)position * 1000.0 / (double)frequency;
		
		return output_time + start_time_ms;
	} else {
		
		return 0;
	}
}

static int GetOutputTime()
{
	return (int)GetOutputTimeAsDouble();
}

static int GetWrittenTime()
{
	double time_in_buffer = (1000.0 * (double)CanWrite()) / ((double)bytes_per_frame * (double)sample_rate);
	return (int)(GetOutputTimeAsDouble() + time_in_buffer);
}

Out_Module plugin = {
	OUT_VER_U,
	0,
	70,
	NULL,
	NULL,
	Config,
	About,
	Init,
	Quit,
	Open,
	Close,
	Write,
	CanWrite,
	IsPlaying,
	Pause,
	SetVolume,
	SetPan,
	Flush,
	GetOutputTime,
	GetWrittenTime,
};


extern "C" {

	__declspec(dllexport) int __cdecl winampUninstallPlugin(HINSTANCE hDllInst, HWND hwndDlg, int param)
	{
		return OUT_PLUGIN_UNINSTALL_REBOOT;
	}

	__declspec(dllexport) Out_Module * __cdecl winampGetOutModule(){ return &plugin; }

	__declspec(dllexport) void __cdecl winampGetOutModeChange(int mode)
	{ 
	}

}