forked from dbry/WavPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_wv.c
2317 lines (1795 loc) · 68 KB
/
in_wv.c
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////
// **** WAVPACK **** //
// Hybrid Lossless Wavefile Compressor //
// Copyright (c) 2000 - 2024 David Bryant. //
// All Rights Reserved. //
// Distributed under the BSD Software License (see license.txt) //
////////////////////////////////////////////////////////////////////////////
// in_wv_c.c
// .WV input plug-in for WavPack
#include <windows.h>
#include <fcntl.h>
#include <stdio.h>
#include <mmreg.h>
#include <msacm.h>
#include <math.h>
#include <sys/stat.h>
#include <shlobj.h>
#include <io.h>
#include "in2.h"
#include "wavpack.h"
#include "resource.h"
#include "wasabi/wasabi.h"
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
#ifdef __MINGW32__ // mingw32's _ftelli64() and _fseeki64() are defective
#define _ftelli64 ftello64
#define _fseeki64 fseeko64
#endif
static float calculate_gain (WavpackContext *wpc, int *pSoftClip);
static void *decimation_init (int num_channels, int ratio);
static int decimation_run (void *context, int32_t *samples, int num_samples);
static void decimation_reset (void *context);
static void *decimation_destroy (void *context);
#define PLUGIN_VERSION "2.8.0.3"
//#define DEBUG_CONSOLE
#define UNICODE_METADATA
// use CRT. Good. Useful. Portable.
BOOL WINAPI DllMain (HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
// post this to the main window at end of file (after playback as stopped)
#define WM_WA_MPEG_EOF WM_USER+2
#define MAX_NCH 8
static struct wpcnxt {
WavpackContext *wpc; // WavPack context provided by library
float play_gain; // playback gain (for replaygain support)
int soft_clipping; // soft clipping active for playback
int output_bits; // 16, 24, or 32 bits / sample
int32_t sample_buffer[576*MAX_NCH*4]; // sample buffer
float error [MAX_NCH]; // error term for noise shaping
char lastfn[MAX_PATH]; // filename stored for comparisons only
wchar_t w_lastfn[MAX_PATH];// w_filename stored for comparisons only
FILE *wv_id, *wvc_id; // file pointer when we use reader callbacks
void *decimation_cnxt; // context for 4x decimation (DSD playback)
} curr, edit, info;
In_Module mod; // the output module (declared near the bottom of this file)
int decode_pos_ms; // current decoding position, in milliseconds
int paused; // are we paused?
int seek_needed; // if != -1, it is the point that the decode thread should seek to, in ms.
#define ALLOW_WVC 0x1
#define REPLAYGAIN_TRACK 0x2
#define REPLAYGAIN_ALBUM 0x4
#define SOFTEN_CLIPPING 0x8
#define PREVENT_CLIPPING 0x10
#define ALWAYS_16BIT 0x20 // new flags added for version 2.5
#define ALLOW_MULTICHANNEL 0x40
#define REPLAYGAIN_24BIT 0x80
int config_bits; // all configuration goes here
int killDecodeThread=0; // the kill switch for the decode thread
HANDLE thread_handle=INVALID_HANDLE_VALUE; // the handle to the decode thread
DWORD WINAPI __stdcall DecodeThread(void *b); // the decode thread procedure
HMODULE hResources; // module handle for resources to use
static BOOL CALLBACK WavPackDlgProc (HWND, UINT, WPARAM, LPARAM);
// static const GUID InWvLangGuid =
// { 0x6de2e465, 0x690e, 0x4df1, { 0xb6, 0xe2, 0x2a, 0x9b, 0x33, 0xed, 0x3d, 0xbb } };
static const char *InWvLangGuid = "{6DE2E465-690E-4df1-B6E2-2A9B33ED3DBB}";
static void configure_resources (void)
{
char CheckGuid [64];
if (!hResources) {
hResources = GetModuleHandle ("in_wv.lng");
if (hResources)
if (!LoadString (hResources, IDS_GUID, CheckGuid, sizeof (CheckGuid)) ||
_stricmp (InWvLangGuid, CheckGuid))
hResources = NULL;
}
if (!hResources)
hResources = GetModuleHandle ("in_wv.dll");
}
typedef HRESULT (WINAPI *getfolderpath_t)(HWND,int,HANDLE,DWORD,LPSTR); /* SHGetFolderPathA */
typedef BOOL (WINAPI *getspecialfolderpath_t)(HWND,LPSTR,int,BOOL); /* SHGetSpecialFolderPathA */
static int get_app_path (char *app_path)
{
static char file_path [MAX_PATH], tried, result;
HINSTANCE hinstLib;
getfolderpath_t getfolderpath;
if (tried) {
if (result)
strcpy (app_path, file_path);
return result;
}
tried = TRUE;
hinstLib = LoadLibrary ("shell32.dll");
if (hinstLib) {
getfolderpath = (getfolderpath_t) GetProcAddress (hinstLib, "SHGetFolderPathA");
if (getfolderpath && SUCCEEDED (getfolderpath (NULL, CSIDL_APPDATA | 0x8000, NULL, 0, file_path)))
result = TRUE;
if (!result) {
getspecialfolderpath_t getspecialfolderpath;
getspecialfolderpath = (getspecialfolderpath_t) GetProcAddress (hinstLib, "SHGetSpecialFolderPathA");
if (getspecialfolderpath && getspecialfolderpath (NULL, file_path, CSIDL_APPDATA, TRUE) != 0)
result = TRUE;
}
FreeLibrary (hinstLib);
}
if (!result) {
hinstLib = LoadLibrary ("shfolder.dll");
if (hinstLib) {
getfolderpath = (getfolderpath_t) GetProcAddress (hinstLib, "SHGetFolderPathA");
if (getfolderpath && SUCCEEDED (getfolderpath (NULL, CSIDL_APPDATA | 0x8000, NULL, 0, file_path)))
result = TRUE;
FreeLibrary (hinstLib);
}
}
if (result)
strcpy (app_path, file_path);
return result;
}
void debug_write (char *str);
void config (HWND hwndParent)
{
char dllname [512];
int temp_config;
HANDLE confile;
DWORD result;
if (!hResources)
configure_resources ();
temp_config = (int) DialogBoxParam (hResources, "WinAmp", hwndParent, (DLGPROC) WavPackDlgProc, config_bits);
if (temp_config == config_bits || (temp_config & 0xffffff00) ||
(temp_config & 6) == 6 || (temp_config & 0x18) == 0x18)
return;
config_bits = temp_config;
if (get_app_path (dllname)) {
strcat (dllname, "\\WavPack\\in_wv.dat");
confile = CreateFile (dllname, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (confile == INVALID_HANDLE_VALUE) {
get_app_path (dllname);
strcat (dllname, "\\WavPack");
if (CreateDirectory (dllname, NULL)) {
strcat (dllname, "\\in_wv.dat");
confile = CreateFile (dllname, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
}
if (confile == INVALID_HANDLE_VALUE)
return;
WriteFile (confile, &config_bits, sizeof (config_bits), &result, NULL);
CloseHandle (confile);
}
}
BOOL CALLBACK WavPackDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
char tempstr [128];
static int local_config;
switch (message) {
case WM_INITDIALOG:
local_config = (int) lParam;
CheckDlgButton (hDlg, IDC_USEWVC, local_config & ALLOW_WVC);
CheckDlgButton (hDlg, IDC_ALWAYS_16BIT, local_config & ALWAYS_16BIT);
CheckDlgButton (hDlg, IDC_MULTICHANNEL, local_config & ALLOW_MULTICHANNEL);
if (LoadString (hResources, IDS_DISABLED, tempstr, sizeof (tempstr)))
SendDlgItemMessage (hDlg, IDC_REPLAYGAIN, CB_ADDSTRING, 0, (LPARAM) tempstr);
if (LoadString (hResources, IDS_USE_TRACK, tempstr, sizeof (tempstr)))
SendDlgItemMessage (hDlg, IDC_REPLAYGAIN, CB_ADDSTRING, 0, (LPARAM) tempstr);
if (LoadString (hResources, IDS_USE_ALBUM, tempstr, sizeof (tempstr)))
SendDlgItemMessage (hDlg, IDC_REPLAYGAIN, CB_ADDSTRING, 0, (LPARAM) tempstr);
SendDlgItemMessage (hDlg, IDC_REPLAYGAIN, CB_SETCURSEL, (local_config >> 1) & 3, 0);
if (LoadString (hResources, IDS_JUST_CLIP, tempstr, sizeof (tempstr)))
SendDlgItemMessage (hDlg, IDC_CLIPPING, CB_ADDSTRING, 0, (LPARAM) tempstr);
if (LoadString (hResources, IDS_SOFT_CLIP, tempstr, sizeof (tempstr)))
SendDlgItemMessage (hDlg, IDC_CLIPPING, CB_ADDSTRING, 0, (LPARAM) tempstr);
if (LoadString (hResources, IDS_PREVENT_CLIP, tempstr, sizeof (tempstr)))
SendDlgItemMessage (hDlg, IDC_CLIPPING, CB_ADDSTRING, 0, (LPARAM) tempstr);
SendDlgItemMessage (hDlg, IDC_CLIPPING, CB_SETCURSEL, (local_config >> 3) & 3, 0);
CheckDlgButton (hDlg, IDC_24BIT_RG, local_config & REPLAYGAIN_24BIT);
if (!(local_config & (REPLAYGAIN_TRACK | REPLAYGAIN_ALBUM))) {
EnableWindow (GetDlgItem (hDlg, IDC_CLIPPING_TEXT), FALSE);
EnableWindow (GetDlgItem (hDlg, IDC_CLIPPING), FALSE);
EnableWindow (GetDlgItem (hDlg, IDC_24BIT_RG), FALSE);
}
SetFocus (GetDlgItem (hDlg, IDC_USEWVC));
return FALSE;
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDC_REPLAYGAIN:
if (SendDlgItemMessage (hDlg, IDC_REPLAYGAIN, CB_GETCURSEL, 0, 0)) {
EnableWindow (GetDlgItem (hDlg, IDC_CLIPPING_TEXT), TRUE);
EnableWindow (GetDlgItem (hDlg, IDC_CLIPPING), TRUE);
EnableWindow (GetDlgItem (hDlg, IDC_24BIT_RG), TRUE);
}
else {
EnableWindow (GetDlgItem (hDlg, IDC_CLIPPING_TEXT), FALSE);
EnableWindow (GetDlgItem (hDlg, IDC_CLIPPING), FALSE);
EnableWindow (GetDlgItem (hDlg, IDC_24BIT_RG), FALSE);
}
break;
case IDOK:
local_config = 0;
if (IsDlgButtonChecked (hDlg, IDC_USEWVC))
local_config |= ALLOW_WVC;
if (IsDlgButtonChecked (hDlg, IDC_ALWAYS_16BIT))
local_config |= ALWAYS_16BIT;
if (IsDlgButtonChecked (hDlg, IDC_MULTICHANNEL))
local_config |= ALLOW_MULTICHANNEL;
local_config |= SendDlgItemMessage (hDlg, IDC_REPLAYGAIN, CB_GETCURSEL, 0, 0) << 1;
local_config |= SendDlgItemMessage (hDlg, IDC_CLIPPING, CB_GETCURSEL, 0, 0) << 3;
if (IsDlgButtonChecked (hDlg, IDC_24BIT_RG))
local_config |= REPLAYGAIN_24BIT;
case IDCANCEL:
EndDialog (hDlg, local_config);
return TRUE;
}
break;
}
return FALSE;
}
void about (HWND hwndParent)
{
char about_title [128], about_string [256], about_format [256];
if (!hResources)
configure_resources ();
if (!LoadString (hResources, IDS_ABOUT, about_title, sizeof (about_title)) ||
!LoadString (hResources, IDS_FORMAT, about_format, sizeof (about_format)))
return;
sprintf (about_string, about_format, PLUGIN_VERSION, 2017);
MessageBox (hwndParent, about_string, about_title, MB_OK);
}
void init() { /* any one-time initialization goes here (configuration reading, etc) */
char dllname [512];
HMODULE module;
HANDLE confile;
DWORD result;
Wasabi_Init ();
config_bits = ALLOW_WVC | ALLOW_MULTICHANNEL; // reasonable config defaults
// first, try to read config from the application-specific data folder
if (get_app_path (dllname)) {
strcat (dllname, "\\WavPack\\in_wv.dat");
confile = CreateFile (dllname, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (confile != INVALID_HANDLE_VALUE) {
if (ReadFile (confile, &config_bits, sizeof (config_bits), &result, NULL) &&
result == sizeof (config_bits)) {
CloseHandle (confile);
return;
}
CloseHandle (confile);
}
}
// if nothing there, try our old location (next to the in_wv.dll plugin)...
// we no longer write here, but trying to read here should avoid configuration loss on upgrade
module = GetModuleHandle ("in_wv.dll");
if (module && GetModuleFileName (module, dllname, sizeof (dllname))) {
dllname [strlen (dllname) - 2] = 'a';
dllname [strlen (dllname) - 1] = 't';
confile = CreateFile (dllname, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (confile != INVALID_HANDLE_VALUE) {
if (ReadFile (confile, &config_bits, sizeof (config_bits), &result, NULL) &&
result == sizeof (config_bits)) {
CloseHandle (confile);
return;
}
CloseHandle (confile);
}
}
}
#ifdef DEBUG_CONSOLE
HANDLE debug_console=INVALID_HANDLE_VALUE; // debug console
void debug_write (char *str)
{
static int cant_debug;
if (cant_debug)
return;
if (debug_console == INVALID_HANDLE_VALUE) {
AllocConsole ();
#if 1
debug_console = GetStdHandle (STD_OUTPUT_HANDLE);
#else
debug_console = CreateConsoleScreenBuffer (GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
#endif
if (debug_console == INVALID_HANDLE_VALUE) {
MessageBox(NULL, "Can't get a console handle", "WavPack",MB_OK);
cant_debug = 1;
return;
}
else if (!SetConsoleActiveScreenBuffer (debug_console)) {
MessageBox(NULL, "Can't activate console buffer", "WavPack",MB_OK);
cant_debug = 1;
return;
}
}
WriteConsole (debug_console, str, strlen (str), NULL, NULL);
}
#endif
void quit() { /* one-time deinit, such as memory freeing */
#ifdef DEBUG_CONSOLE
if (debug_console != INVALID_HANDLE_VALUE) {
FreeConsole ();
if (debug_console != GetStdHandle (STD_OUTPUT_HANDLE))
CloseHandle (debug_console);
debug_console = INVALID_HANDLE_VALUE;
}
#endif
Wasabi_Quit ();
}
int isourfile(char *fn)
{
return 0;
}
// used for detecting URL streams.. unused here. strncmp(fn,"http://",7) to detect HTTP streams, etc
int play (char *fn)
{
int num_chans, sample_rate;
char error [128];
int maxlatency;
DWORD thread_id;
int open_flags;
#ifdef DEBUG_CONSOLE
sprintf (error, "play (%s)\n", fn);
debug_write (error);
#endif
open_flags = OPEN_TAGS | OPEN_NORMALIZE | OPEN_DSD_AS_PCM;
if (config_bits & ALLOW_WVC)
open_flags |= OPEN_WVC;
if (!(config_bits & ALLOW_MULTICHANNEL))
open_flags |= OPEN_2CH_MAX;
curr.wpc = WavpackOpenFileInput (fn, error, open_flags, 0);
if (!curr.wpc) // error opening file, just return error
return -1;
num_chans = WavpackGetReducedChannels (curr.wpc);
curr.output_bits = WavpackGetBitsPerSample (curr.wpc) > 16 ? 24 : 16;
if (config_bits & ALWAYS_16BIT)
curr.output_bits = 16;
else if ((config_bits & (REPLAYGAIN_TRACK | REPLAYGAIN_ALBUM)) &&
(config_bits & REPLAYGAIN_24BIT))
curr.output_bits = 24;
if (num_chans > MAX_NCH) { // don't allow too many channels!
WavpackCloseFile (curr.wpc);
return -1;
}
curr.play_gain = calculate_gain (curr.wpc, &curr.soft_clipping);
strcpy (curr.lastfn, fn);
paused = 0;
decode_pos_ms = 0;
seek_needed = -1;
sample_rate = WavpackGetSampleRate (curr.wpc);
if (sample_rate >= 256000) {
curr.decimation_cnxt = decimation_init (num_chans, 4);
sample_rate /= 4;
}
maxlatency = mod.outMod->Open (sample_rate, num_chans, curr.output_bits, -1, -1);
if (maxlatency < 0) { // error opening device
curr.decimation_cnxt = decimation_destroy (curr.decimation_cnxt);
curr.wpc = WavpackCloseFile (curr.wpc);
return -1;
}
// dividing by 1000 for the first parameter of setinfo makes it
// display 'H'... for hundred.. i.e. 14H Kbps.
mod.SetInfo (0, (sample_rate + 500) / 1000, num_chans, 1);
// initialize vis stuff
mod.SAVSAInit (maxlatency, sample_rate);
mod.VSASetInfo (sample_rate, num_chans);
mod.outMod->SetVolume (-666); // set the output plug-ins default volume
killDecodeThread=0;
thread_handle = (HANDLE) CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) DecodeThread,
(void *) &killDecodeThread, 0, &thread_id);
if (SetThreadPriority (thread_handle, THREAD_PRIORITY_HIGHEST) == 0) {
curr.decimation_cnxt = decimation_destroy (curr.decimation_cnxt);
curr.wpc = WavpackCloseFile (curr.wpc);
return -1;
}
return 0;
}
void pause ()
{
#ifdef DEBUG_CONSOLE
debug_write ("pause ()\n");
#endif
paused = 1;
mod.outMod->Pause (1);
}
void unpause ()
{
#ifdef DEBUG_CONSOLE
debug_write ("unpause ()\n");
#endif
paused = 0;
mod.outMod->Pause (0);
}
int ispaused ()
{
return paused;
}
void stop()
{
#ifdef DEBUG_CONSOLE
debug_write ("stop ()\n");
#endif
if (thread_handle != INVALID_HANDLE_VALUE) {
killDecodeThread = 1;
if (WaitForSingleObject (thread_handle, INFINITE) == WAIT_TIMEOUT) {
MessageBox(mod.hMainWindow,"error asking thread to die!\n", "error killing decode thread", 0);
TerminateThread(thread_handle,0);
}
CloseHandle (thread_handle);
thread_handle = INVALID_HANDLE_VALUE;
}
if (curr.wpc)
curr.wpc = WavpackCloseFile (curr.wpc);
curr.decimation_cnxt = decimation_destroy (curr.decimation_cnxt);
mod.outMod->Close ();
mod.SAVSADeInit ();
}
int getlength()
{
return (int)(WavpackGetNumSamples64 (curr.wpc) * 1000.0 / WavpackGetSampleRate (curr.wpc));
}
int getoutputtime()
{
if (seek_needed == -1)
return decode_pos_ms + (mod.outMod->GetOutputTime () - mod.outMod->GetWrittenTime ());
else
return seek_needed;
}
void setoutputtime (int time_in_ms)
{
#ifdef DEBUG_CONSOLE
char str [40];
sprintf (str, "setoutputtime (%d)\n", time_in_ms);
debug_write (str);
#endif
seek_needed = time_in_ms;
}
void setvolume (int volume)
{
mod.outMod->SetVolume (volume);
}
void setpan (int pan)
{
mod.outMod->SetPan(pan);
}
static void generate_format_string (WavpackContext *wpc, char *string, int maxlen, int wide);
static int UTF8ToWideChar (const unsigned char *pUTF8, unsigned short *pWide);
static int WideCharToUTF8 (const unsigned short *Wide, unsigned char *pUTF8, int len);
static void AnsiToUTF8 (char *string, int len);
static void UTF8ToAnsi (char *string, int len);
int infoDlg (char *fn, HWND hwnd)
{
char string [2048];
unsigned short w_string [2048];
WavpackContext *wpc;
int open_flags;
open_flags = OPEN_TAGS | OPEN_NORMALIZE | OPEN_DSD_AS_PCM;
if (config_bits & ALLOW_WVC)
open_flags |= OPEN_WVC;
if (!(config_bits & ALLOW_MULTICHANNEL))
open_flags |= OPEN_2CH_MAX;
wpc = WavpackOpenFileInput (fn, string, open_flags, 0);
if (wpc) {
int mode = WavpackGetMode (wpc);
generate_format_string (wpc, string, sizeof (string), 1);
if (mode & MODE_VALID_TAG) {
char value [128];
if (config_bits & (REPLAYGAIN_TRACK | REPLAYGAIN_ALBUM)) {
int local_clipping;
float local_gain;
local_gain = calculate_gain (wpc, &local_clipping);
if (local_gain != 1.0)
sprintf (string + strlen (string), "Gain: %+.2f dB %s\n",
log10 (local_gain) * 20.0, local_clipping ? "(w/soft clipping)" : "");
}
if (WavpackGetTagItem (wpc, "title", value, sizeof (value))) {
if (!(mode & MODE_APETAG))
AnsiToUTF8 (value, sizeof (value));
sprintf (string + strlen (string), "\nTitle: %s", value);
}
if (WavpackGetTagItem (wpc, "artist", value, sizeof (value))) {
if (!(mode & MODE_APETAG))
AnsiToUTF8 (value, sizeof (value));
sprintf (string + strlen (string), "\nArtist: %s", value);
}
if (WavpackGetTagItem (wpc, "album", value, sizeof (value))) {
if (!(mode & MODE_APETAG))
AnsiToUTF8 (value, sizeof (value));
sprintf (string + strlen (string), "\nAlbum: %s", value);
}
if (WavpackGetTagItem (wpc, "genre", value, sizeof (value))) {
if (!(mode & MODE_APETAG))
AnsiToUTF8 (value, sizeof (value));
sprintf (string + strlen (string), "\nGenre: %s", value);
}
if (WavpackGetTagItem (wpc, "comment", value, sizeof (value))) {
if (!(mode & MODE_APETAG))
AnsiToUTF8 (value, sizeof (value));
sprintf (string + strlen (string), "\nComment: %s", value);
}
if (WavpackGetTagItem (wpc, "year", value, sizeof (value)))
sprintf (string + strlen (string), "\nYear: %s", value);
if (WavpackGetTagItem (wpc, "track", value, sizeof (value)))
sprintf (string + strlen (string), "\nTrack: %s", value);
strcat (string, "\n");
}
UTF8ToWideChar (string, w_string);
MessageBoxW (hwnd, w_string, L"WavPack File Info Box", MB_OK);
wpc = WavpackCloseFile (wpc);
}
else
MessageBox (hwnd, string, "WavPack Decoder", MB_OK);
return 0;
}
void getfileinfo (char *filename, char *title, int *length_in_ms)
{
if (!filename || !*filename) { // currently playing file
if (length_in_ms)
*length_in_ms = getlength ();
if (title) {
if (WavpackGetTagItem (curr.wpc, "title", NULL, 0)) {
char art [128], ttl [128];
WavpackGetTagItem (curr.wpc, "title", ttl, sizeof (ttl));
if (WavpackGetMode (curr.wpc) & MODE_APETAG)
UTF8ToAnsi (ttl, sizeof (ttl));
if (WavpackGetTagItem (curr.wpc, "artist", art, sizeof (art))) {
if (WavpackGetMode (curr.wpc) & MODE_APETAG)
UTF8ToAnsi (art, sizeof (art));
sprintf (title, "%s - %s", art, ttl);
}
else
strcpy (title, ttl);
}
else {
char *p = curr.lastfn + strlen (curr.lastfn);
while (*p != '\\' && p >= curr.lastfn)
p--;
strcpy(title,++p);
}
}
}
else { // some other file
WavpackContext *wpc;
char error [128];
int open_flags;
if (length_in_ms)
*length_in_ms = -1000;
if (title)
*title = 0;
open_flags = OPEN_TAGS | OPEN_NORMALIZE | OPEN_DSD_AS_PCM;
if (config_bits & ALLOW_WVC)
open_flags |= OPEN_WVC;
if (!(config_bits & ALLOW_MULTICHANNEL))
open_flags |= OPEN_2CH_MAX;
wpc = WavpackOpenFileInput (filename, error, open_flags, 0);
if (wpc) {
if (length_in_ms)
*length_in_ms = (int)(WavpackGetNumSamples64 (wpc) * 1000.0 / WavpackGetSampleRate (wpc));
if (title && WavpackGetTagItem (wpc, "title", NULL, 0)) {
char art [128], ttl [128];
WavpackGetTagItem (wpc, "title", ttl, sizeof (ttl));
if (WavpackGetMode (wpc) & MODE_APETAG)
UTF8ToAnsi (ttl, sizeof (ttl));
if (WavpackGetTagItem (wpc, "artist", art, sizeof (art))) {
if (WavpackGetMode (wpc) & MODE_APETAG)
UTF8ToAnsi (art, sizeof (art));
sprintf (title, "%s - %s", art, ttl);
}
else
strcpy (title, ttl);
}
wpc = WavpackCloseFile (wpc);
}
if (title && !*title) {
char *p = filename + strlen (filename);
while (*p != '\\' && p >= filename) p--;
strcpy(title,++p);
}
}
}
void eq_set (int on, char data [10], int preamp)
{
// most plug-ins can't even do an EQ anyhow.. I'm working on writing
// a generic PCM EQ, but it looks like it'll be a little too CPU
// consuming to be useful :)
}
static int read_samples (struct wpcnxt *cnxt, int num_samples);
DWORD WINAPI __stdcall DecodeThread (void *b)
{
int num_chans, sample_rate;
int done = 0;
memset (curr.error, 0, sizeof (curr.error));
num_chans = WavpackGetReducedChannels (curr.wpc);
sample_rate = WavpackGetSampleRate (curr.wpc);
while (!*((int *)b) ) {
if (seek_needed != -1) {
int seek_position = seek_needed;
seek_needed = -1;
if (seek_position > getlength () - 1000 && getlength () > 1000)
seek_position = getlength () - 1000; // don't seek to last second
mod.outMod->Flush (decode_pos_ms = seek_position);
decimation_reset (curr.decimation_cnxt);
if (WavpackSeekSample64 (curr.wpc, (int64_t)(sample_rate / 1000.0 * seek_position))) {
decode_pos_ms = (int)(WavpackGetSampleIndex64 (curr.wpc) * 1000.0 / sample_rate);
mod.outMod->Flush (decode_pos_ms);
continue;
}
else
done = 1;
}
if (done) {
mod.outMod->CanWrite ();
if (!mod.outMod->IsPlaying ()) {
PostMessage (mod.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
return 0;
}
Sleep (10);
}
else if (mod.outMod->CanWrite() >= ((576 * num_chans * (curr.output_bits / 8)) << (mod.dsp_isactive () ? 1 : 0))) {
int tsamples = read_samples (&curr, 576) * num_chans;
int tbytes = tsamples * (curr.output_bits/8);
if (tsamples) {
mod.SAAddPCMData ((char *) curr.sample_buffer, num_chans, curr.output_bits, decode_pos_ms);
mod.VSAAddPCMData ((char *) curr.sample_buffer, num_chans, curr.output_bits, decode_pos_ms);
decode_pos_ms = (int)(WavpackGetSampleIndex64 (curr.wpc) * 1000.0 / sample_rate);
if (mod.dsp_isactive())
tbytes = mod.dsp_dosamples ((short *) curr.sample_buffer,
tsamples / num_chans, curr.output_bits, num_chans, sample_rate) * (num_chans * (curr.output_bits/8));
mod.outMod->Write ((char *) curr.sample_buffer, tbytes);
}
else
done = 1;
}
else {
mod.SetInfo ((int) ((WavpackGetInstantBitrate (curr.wpc) + 500.0) / 1000.0), -1, -1, 1);
Sleep(20);
}
}
return 0;
}
/********* These functions provide the "transcoding" mode of winamp. *********/
__declspec (dllexport) intptr_t winampGetExtendedRead_open (
const char *fn, int *size, int *bps, int *nch, int *srate)
{
struct wpcnxt *cnxt = (struct wpcnxt *) malloc (sizeof (struct wpcnxt));
int num_chans, sample_rate, open_flags;
int64_t actual_size;
char error [128];
#ifdef DEBUG_CONSOLE
sprintf (error, "Read_open (%s)\n", fn);
debug_write (error);
#endif
if (!cnxt)
return 0;
memset (cnxt, 0, sizeof (struct wpcnxt));
open_flags = OPEN_NORMALIZE | OPEN_WVC | OPEN_DSD_AS_PCM;
if (!(config_bits & ALLOW_MULTICHANNEL) || *nch == 2)
open_flags |= OPEN_2CH_MAX;
if (config_bits & (REPLAYGAIN_TRACK | REPLAYGAIN_ALBUM))
open_flags |= OPEN_TAGS;
cnxt->wpc = WavpackOpenFileInput (fn, error, open_flags, 0);
if (!cnxt->wpc) { // error opening file, just return error
free (cnxt);
return 0;
}
num_chans = WavpackGetReducedChannels (cnxt->wpc);
sample_rate = WavpackGetSampleRate (cnxt->wpc);
if (num_chans > MAX_NCH) {
WavpackCloseFile (cnxt->wpc);
free (cnxt);
return 0;
}
if (*bps != 16 && *bps != 24 && *bps != 32) {
cnxt->output_bits = WavpackGetBitsPerSample (cnxt->wpc) > 16 ? 24 : 16;
if (config_bits & ALWAYS_16BIT)
cnxt->output_bits = 16;
else if ((config_bits & (REPLAYGAIN_TRACK | REPLAYGAIN_ALBUM)) &&
(config_bits & REPLAYGAIN_24BIT))
cnxt->output_bits = 24;
}
else
cnxt->output_bits = *bps;
if (sample_rate >= 256000) {
cnxt->decimation_cnxt = decimation_init (num_chans, 4);
sample_rate /= 4;
}
*nch = num_chans;
*srate = sample_rate;
*bps = cnxt->output_bits;
actual_size = WavpackGetNumSamples64 (cnxt->wpc) * (*bps / 8) * (*nch);
if (cnxt->decimation_cnxt)
actual_size /= 4;
if (actual_size <= 2147483647)
*size = (int) actual_size;
else
*size = -1;
cnxt->play_gain = calculate_gain (cnxt->wpc, &cnxt->soft_clipping);
#ifdef DEBUG_CONSOLE
sprintf (error, "Read_open success! nch=%d, srate=%d, bps=%d, size=%d\n",
*nch, *srate, *bps, *size);
debug_write (error);
#endif
return (intptr_t) cnxt;
}
__declspec (dllexport) intptr_t winampGetExtendedRead_getData (
intptr_t handle, char *dest, int len, int *killswitch)
{
struct wpcnxt *cnxt = (struct wpcnxt *) handle;
int num_chans = WavpackGetReducedChannels (cnxt->wpc);
int bytes_per_sample = num_chans * cnxt->output_bits / 8;
int used = 0;
#ifdef DEBUG_CONSOLE
char error [128];
#endif
while (used < len && !*killswitch) {
int nsamples = (len - used) / bytes_per_sample, tsamples;
if (!nsamples)
break;
else if (nsamples > 576)
nsamples = 576;
tsamples = read_samples (cnxt, nsamples) * num_chans;
if (tsamples) {
int tbytes = tsamples * (cnxt->output_bits/8);
memcpy (dest + used, cnxt->sample_buffer, tbytes);
used += tbytes;
}
else
break;
}
#ifdef DEBUG_CONSOLE
sprintf (error, "Read_getData (%d), actually read %d\n", len, used);
debug_write (error);
#endif
return used;
}
__declspec (dllexport) int winampGetExtendedRead_setTime (intptr_t handle, int millisecs)
{
struct wpcnxt *cnxt = (struct wpcnxt *) handle;