aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Output/out_wave/out_wave.cpp
blob: a29715f26ccfd88482fc6a7bdbf6a3509903a5f9 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "out_wave.h"
#include "api.h"
#include "resource.h"
#include "waveout.h"
#include "../winamp/wa_ipc.h"
#include "../nu/AutoWide.h"

#ifdef HAVE_SSRC
#include "ssrc\ssrc.h"
static Resampler_base * pSSRC;
#endif

static bool gapless_stop;
static WaveOut * pWO;
static __int64 total_written;
static int pos_delta;
static UINT canwrite_hack;

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

void _init();

//cfg_prebuf now in ms !
UINT cfg_dev = 0, cfg_buf_ms = 2000, cfg_prebuf = 200, cfg_trackhack = 200;
bool cfg_volume = 1, cfg_altvol = 0, cfg_resetvol = 0;
static int fmt_sr, fmt_bps, fmt_nch;

static int volume = 255, pan = 0;


#ifdef HAVE_SSRC
UINT cfg_dither = 1, cfg_resample_freq = 48000, cfg_resample_bps = 1;
bool cfg_fast = 1;
UINT cfg_pdf = 1;
UINT bps_tab[3] = {8, 16, 24};
static bool finished, use_finish;

static UINT resample_freq;
static UINT resample_bps;

static void do_ssrc_create()
{
	pSSRC = SSRC_create(fmt_sr, resample_freq, fmt_bps, resample_bps, fmt_nch, cfg_dither, cfg_pdf, cfg_fast, 0);
	finished = 0;
	use_finish = cfg_trackhack == 0 ? 1 : 0;
}
#endif

void do_cfg(bool s);

static CRITICAL_SECTION sync; //various funky time consuming stuff going on, better protect ourselves with a critical section here, resampler doesnt have its own one
#define SYNC_IN EnterCriticalSection(&sync);
#define SYNC_OUT LeaveCriticalSection(&sync);


void Config(HWND);

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);
}

void About(HWND hwndParent)
{
	wchar_t message[1024] = {0}, text[1024] = {0};
	WASABI_API_LNGSTRINGW_BUF(IDS_NULLSOFT_WAVEOUT_OLD,text,1024);
	wsprintfW(message, WASABI_API_LNGSTRINGW(IDS_ABOUT_TEXT),
			  mod.description, __DATE__);
	DoAboutMessageBox(hwndParent,text,message);
}

static void Init()
{
	if (!IsWindow(mod.hMainWindow))
		return;

	// loader so that we can get the localisation service api for use
	WASABI_API_SVC = (api_service*)SendMessage(mod.hMainWindow, WM_WA_IPC, 0, IPC_GET_API_SERVICE);
	if (WASABI_API_SVC == (api_service*)1) WASABI_API_SVC = NULL;
	if (!WASABI_API_SVC || WASABI_API_SVC == (api_service *)1)
		return;

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

	sf = WASABI_API_SVC->service_getServiceByGuid(applicationApiServiceGuid);
	if (sf) WASABI_API_APP = reinterpret_cast<api_application*>(sf->getInterface());

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

	static wchar_t szDescription[256];
	swprintf(szDescription,256,WASABI_API_LNGSTRINGW(IDS_NULLSOFT_WAVEOUT),OUT_WAVE_VER);
	mod.description = (char*)szDescription;
}

static int inited;

static void Quit()
{
	if (inited)
	{
		if (pWO)
		{
			delete pWO;
			pWO = 0;
		}
#ifdef HAVE_SSRC
		if (pSSRC)
		{
			delete pSSRC;
			pSSRC = 0;
		}
#endif
		do_cfg(1);
		inited = 0;
	}
}

static void reset_stuff()
{
	canwrite_hack = 0;
	pos_delta = 0;
	total_written = 0;
	gapless_stop = 0;
}


void _init()
{
	if (!inited)
	{
		inited = 1;
		do_cfg(0);
		if (cfg_dev > waveOutGetNumDevs()) cfg_dev = 0;
	}
}

static void _write(char * data, int size);

int Open(int sr, int nch, int bps, int bufferlenms, int prebufferms)
{
	_init();
	SYNC_IN;

	if (pWO)	//"gapless" track change (or someone forgot to close output)
	{
		pWO->SetCloseOnStop(0);	//turn off self-destruct on out-of-PCM-data
		if (!pWO->IsClosed())	//has it run out of PCM data or not ? if yes, we can only delete and create new one
		{
			if (sr != fmt_sr || nch != fmt_nch || bps != fmt_bps) //tough shit, new pcm format
			{ //wait-then-close, dont cut previous track
#ifdef HAVE_SSRC
				if (!pSSRC && !finished)
				{
					use_finish = 1;
					_write(0, 0);
				}
#endif
				while (pWO->GetLatency() > 0) Sleep(1);
			}
			else
			{ //successful gapless track change. yay.
				reset_stuff();
#ifdef HAVE_SSRC
				if (pSSRC)
				{
					if (finished)
					{
						delete pSSRC;
						do_ssrc_create();
					}
					else use_finish = cfg_trackhack == 0 ? 1 : 0;
				}
#endif
				int r = pWO->GetMaxLatency();
				SYNC_OUT;
				return r;
			}
		}
#ifdef HAVE_SSRC
		if (pSSRC)
		{
			delete pSSRC;
			pSSRC = 0;
		}
#endif
		delete pWO;
	}

	WaveOutConfig * cfg = new WaveOutConfig; //avoid crazy crt references with creating cfg on stack, keep TINY_DLL config happy
	cfg->SetBuffer(cfg_buf_ms, cfg_prebuf);
	cfg->SetDevice(cfg_dev);
	cfg->SetVolumeSetup(cfg_volume, cfg_altvol, cfg_resetvol);

	fmt_sr = sr;
	fmt_nch = nch;
	fmt_bps = bps;

#ifdef HAVE_SSRC
	resample_freq = cfg_resample_freq;
	if (resample_freq < 6000) resample_freq = 6000;
	else if (resample_freq > 192000) resample_freq = 192000;
	resample_bps = bps_tab[cfg_resample_bps];

	if (fmt_sr == (int)resample_freq && fmt_bps == (int)resample_bps)
	{
		cfg->SetPCM(fmt_sr, fmt_nch, fmt_bps);
	}
	else
	{
		do_ssrc_create();
	}


	if (!pSSRC) cfg->SetPCM(sr, nch, bps);
	else cfg->SetPCM(resample_freq, nch, resample_bps);

#else//!HAVE_SSRC
	cfg->SetPCM(sr, nch, bps);
#endif

	pWO = WaveOut::Create(cfg);
	if (!pWO)
	{
		const WCHAR *error = cfg->GetError();
		if (error)
		{
			WCHAR err[128] = {0}, temp[128] = {0};
			swprintf(err,128,WASABI_API_LNGSTRINGW(IDS_ERROR),WASABI_API_LNGSTRINGW_BUF(IDS_NULLSOFT_WAVEOUT_OLD,temp,128));
			MessageBoxW(mod.hMainWindow, error, err, MB_ICONERROR);
		}
	}
	else
	{
		reset_stuff();
	}

	delete cfg;
	if (pWO)
	{
		int r = pWO->GetMaxLatency();
		SYNC_OUT;
		return r;
	}
	else
	{
#ifdef HAVE_SSRC
		if (pSSRC)
		{
			delete pSSRC;
			pSSRC = 0;
		}
#endif
		SYNC_OUT;
		return -1;
	}
}

#ifdef HAVE_SSRC
static UINT ssrc_extra_latency;

static void _write(char * data, int size)
{
	if (pWO)
	{
		if (pSSRC)
		{
			if (!finished)
			{
				UINT nsiz;
				if (data > 0) pSSRC->Write(data, (UINT)size);
				else if (use_finish)
				{
					finished = 1;
					pSSRC->Finish();
				}
				data = (char*)pSSRC->GetBuffer(&nsiz);
				UINT nsiz1 = nsiz;
				while (nsiz) //ugly
				{
					int wr = pWO->WriteData(data, nsiz);
					if (wr > 0)
					{
						data += wr;
						nsiz -= wr;
						if (!nsiz) break;
					}
					ssrc_extra_latency = MulDiv(nsiz, 1000, resample_freq * fmt_nch * (resample_bps >> 3));
					SYNC_OUT;
					Sleep(1); //shouldnt happen anymore since canwrite works correctly
					SYNC_IN;
				}
				pSSRC->Read(nsiz1);
				ssrc_extra_latency = 0;
			}
		}
		else
		{
			pWO->WriteData(data, size);
		}
		total_written += size / ((fmt_bps >> 3) * fmt_nch);
	}
}
#endif

int Write(char *data, int size)
{
	SYNC_IN;
	gapless_stop = 0;
	canwrite_hack = 0;
	// decrypt, if necessary

#ifdef HAVE_SSRC
	_write(data, size);
#else
	if (pWO)
	{
		pWO->WriteData(data, size);
		total_written += size / ((fmt_bps >> 3) * fmt_nch);
	}
#endif
	SYNC_OUT;
	return 0;
}

void Close()
{
	SYNC_IN;
	if (pWO)
	{
		if (gapless_stop) //end-of-song stop, dont close yet, use gapless hacks
		{
			pWO->SetCloseOnStop(1);	//will self-destruct when out of PCM data to play, has no more than 200ms in buffer
		}
		else	//regular stop (user action)
		{
			delete pWO;
			pWO = 0;
#ifdef HAVE_SSRC
			if (pSSRC)
			{
				delete pSSRC;
				pSSRC = 0;
			}
#endif

		}
	}
	SYNC_OUT;
}

int CanWrite()
{
	int r;
	SYNC_IN;
	if (pWO)
	{
#ifdef HAVE_SSRC
		if (pSSRC)
		{
			r = MulDiv(pWO->CanWrite() - (resample_bps >> 3) * fmt_nch, fmt_bps * fmt_sr, resample_freq * resample_bps) - pSSRC->GetDataInInbuf();
			if (r < 0) r = 0;
		}
		else
#endif
			r = pWO->CanWrite();

		if (++canwrite_hack > 2) pWO->ForcePlay(); //avoid constant-small-canwrite-while-still-prebuffering snafu
	}
	else r = 0;
	SYNC_OUT;
	return r;
}

int IsPlaying()
{ //this is called only when decoding is done unless some input plugin dev is really nuts about making useless calls
	SYNC_IN;
	if (pWO)
	{
#ifdef HAVE_SSRC
		_write(0, 0);
#endif
		pWO->ForcePlay(); //evil short files: make sure that output has started
		if ((UINT)pWO->GetLatency() > cfg_trackhack) //cfg_trackhack used to be 200ms constant
		{	//just for the case some input plugin dev is actually nuts about making useless calls or user presses stop/prev/next when decoding is finished, we don't activate gapless_stop here
			gapless_stop = 0;
			SYNC_OUT;
			return 1;
		}
		else
		{	//ok so looks like we're really near the end-of-track, time to do gapless track switch mumbo-jumbo
			gapless_stop = 1;
			SYNC_OUT;
			return 0; //hack: make the input plugin think that we're done with current track
		}
	}
	else
	{
		SYNC_OUT;
		return 0;
	}
}

int Pause(int new_state)
{
	int rv;
	SYNC_IN;
	if (pWO)
	{
		rv = pWO->IsPaused();
		pWO->Pause(new_state);
	}
	else rv = 0;
	SYNC_OUT;
	return rv;
}


void Flush(int pos)
{
	SYNC_IN;
	if (pWO) pWO->Flush();
#ifdef HAVE_SSRC
	if (pSSRC)
	{
		delete pSSRC;
		do_ssrc_create();
	}
#endif
	reset_stuff();
	pos_delta = pos;
	SYNC_OUT;
}

void SetVolume(int v)
{
	SYNC_IN;
	if (v != -666)
	{
		volume = v;
	}
	if (pWO) pWO->SetVolume(volume);
	SYNC_OUT;
}

void SetPan(int p)
{
	SYNC_IN;
	pan = p;
	if (pWO) pWO->SetPan(pan);
	SYNC_OUT;
}

int get_written_time() //this ignores high 32bits of total_written
{
	return MulDiv((int)total_written, 1000, fmt_sr);
}

int GetWrittenTime()
{
	int r;
	SYNC_IN;
	r = pWO ? pos_delta + get_written_time() : 0;
	SYNC_OUT;
	return r;
}

int GetOutputTime()
{
	int r;
	SYNC_IN;
	r = pWO ? (pos_delta + get_written_time()) - pWO->GetLatency() : 0;
#ifdef HAVE_SSRC
	if (pSSRC)
		r -= ssrc_extra_latency ? ssrc_extra_latency : pSSRC->GetLatency();
#endif
	SYNC_OUT;
	return r;
}

static int Validate(int dummy1, int dummy2, short key, char dummy4);
static int NewWrite(int len, char *buf);

Out_Module mod =
{
	OUT_VER_U,
#ifdef HAVE_SSRC
    NAME" SSRC",
#else
    0,
#endif
    1471482036, //could put different one for SSRC config but i'm too lazy and this shit doesnt seem used anymore anyway
    0, 0,
    Config,
    About,

    Init,
	Quit,
    Open,

    Close,

    Write,

    CanWrite,

    IsPlaying,

    Pause,

    SetVolume,
    SetPan,

    Flush,

    GetOutputTime,
    GetWrittenTime,
};

HMODULE thisMod=0;
Out_Module *(*waveGetter)(HINSTANCE) = 0;
HMODULE inWMDLL = 0;
BOOL APIENTRY DllMain(HANDLE hMod, DWORD r, void*)
{
	if (r == DLL_PROCESS_ATTACH)
	{
		thisMod=(HMODULE)hMod;
		DisableThreadLibraryCalls((HMODULE)hMod);
		InitializeCriticalSection(&sync);
	}
	else if (r == DLL_PROCESS_DETACH)
	{
		DeleteCriticalSection(&sync);

		if (inWMDLL)
		{
			FreeLibrary(inWMDLL); // potentially unsafe, we'll see ...
			inWMDLL = 0;
		}
	}
	return TRUE;
}

extern "C"
{
	__declspec( dllexport ) Out_Module * winampGetOutModule()
	{
		inWMDLL = GetModuleHandleW(L"in_wm.dll");
		if (inWMDLL)
		{
			waveGetter = (Out_Module * (*)(HINSTANCE))GetProcAddress(inWMDLL, "GetWave");
			if (waveGetter)
				return waveGetter(thisMod);
		}

		return &mod;
	}
}

bool get_waveout_state(char * z)
{
	if (pWO) return pWO->PrintState(z);
	else return 0;
}