forked from ValleyBell/in_vgm-libvgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_vgm.cpp
1063 lines (881 loc) · 28.2 KB
/
in_vgm.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
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
// Winamp VGM input plug-in
// ------------------------
// Written by Valley Bell, 2020
//
// Made using the Example .RAW plug-in by Justin Frankel and
// small parts (mainly dialogues) of the old in_vgm 0.35 by Maxim.
// #defines used by the Visual C++ project file:
// UNICODE_INPUT_PLUGIN (Unicode build only)
#include <vector>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <locale.h> // for setlocale
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
extern "C"
{
#include "Winamp/in2.h"
#include "Winamp/wa_ipc.h"
#include <zlib.h> // for info in About message
#include <stdtype.h>
#include "ini_func.h"
} // end extern "C"
#include <emu/SoundEmu.h> // for SndEmu_GetDevName()
#include <utils/DataLoader.h>
#include <utils/FileLoader.h>
#include <player/playerbase.hpp>
#include <player/vgmplayer.hpp>
#include <player/s98player.hpp>
#include <player/droplayer.hpp>
#include <player/gymplayer.hpp>
#include <player/playera.hpp>
#include "utils.hpp"
#include "FileInfoStorage.hpp"
#include "TagFormatter.hpp"
#include "playcfg.hpp"
#include "in_vgm.h"
#ifndef WM_WA_MPEG_EOF
// post this to the main window at end of file (after playback as stopped)
#define WM_WA_MPEG_EOF WM_USER+2
#endif
#ifdef UNICODE_INPUT_PLUGIN
typedef std::wstring in2string;
#else
typedef std::string in2string;
#endif
#define DLL_EXPORT __declspec(dllexport)
#define NUM_CHN 2
#define BIT_PER_SEC pluginCfg.genOpts.smplBits
#define SMPL_BYTES (NUM_CHN * BIT_PER_SEC / 8)
#define RENDER_SAMPLES 576 // visualizations look best with 576 samples
#define BLOCK_SIZE (RENDER_SAMPLES * SMPL_BYTES)
#define BLOCK_SIZE_MAX (RENDER_SAMPLES * 4) // assume up to 32-bit samples
// Function Prototypes from dlg_cfg.c
int ConfigDlg_Show(HINSTANCE hDllInst, HWND hWndParent);
void Dialogue_TrackChange(void);
// Function Protorypes from dlg_fileinfo.c
void InitFileInfoDialog(FileInfoStorage* fis);
void DeinitFileInfoDialog(void);
void FileInfoDlg_LoadFile(const char* fileName);
void FileInfoDlg_LoadFile(const wchar_t* fileName);
int FileInfoDlg_Show(HINSTANCE hDllInst, HWND hWndParent);
//void QueueInfoReload(void);
// Function Protorypes from extFileInfo.cpp
void InitExtFileInfo(FileInfoStorage* scan, CPCONV* cpcU8w, CPCONV* cpcU8a);
void DeinitExtFileInfo(void);
// Function Prototypes
//extern "C" BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved);
void Config(HWND hWndParent); // Note: also used by dlg_fileinfo.cpp
static void About(HWND hWndParent);
static void GenerateFileExtList(void);
static void Init(void);
static void Deinit(void);
static int IsOurFile(const in_char* fn);
static inline UINT32 MulDivRound(UINT64 Number, UINT64 Numerator, UINT64 Denominator);
static inline UINT32 MSec2Samples(UINT32 val, const PlayerA& player);
static void PreparePlayback(void);
static int Play(const in_char* FileName);
static void Pause(void);
static void Unpause(void);
static int IsPaused(void);
static void Stop(void);
int GetPlrSongLengthMS(const PlayerA& plr);
int GetPlrSongLengthMS(const FileInfoStorage::SongInfo& songInfo);
int GetLength(void);
static int GetOutputTime(void);
static void SetOutputTime(int time_in_ms);
static void SetVolume(int volume);
static void SetPan(int pan);
const char* GetIniFilePath(void);
FileInfoStorage* GetMainPlayerFIS(void);
void RefreshPlaybackOptions(void);
void RefreshMuting(void);
void RefreshPanning(void);
void UpdatePlayback(void);
static int InfoDialog(const in_char* fileName, HWND hWnd);
static void GetFileInfo(const in_char* fileName, in_char* title, int* length_in_ms);
static void EQ_Set(int on, char data[10], int preamp);
static DWORD WINAPI DecodeThread(LPVOID b);
static UINT8 FilePlayCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam);
static DATA_LOADER* PlayerFileReqCallback(void* userParam, PlayerBase* player, const char* fileName);
// the output module (defined at the end of the file)
extern In_Module WmpMod;
static std::string wmpFileExtList;
static UINT32 decode_pos; // current decoding position (depends on SampleRate)
static int decode_pos_ms; // Used for correcting DSP plug-in pitch changes
static volatile int seek_needed; // if != -1, it is the point that the decode
// thread should seek to, in ms.
static volatile bool killDecodeThread = 0; // the kill switch for the decode thread
static HANDLE thread_handle = INVALID_HANDLE_VALUE; // the handle to the decode thread
static volatile bool InStopFunc;
static volatile bool PausePlay;
static volatile bool EndPlay;
PluginConfig pluginCfg;
static std::vector<std::string> appSearchPaths;
static std::string wa5IniDirBase; // may be empty OR "%APPDATA%\Winamp"
static std::string waIniDir; // either the DLL directory of "{wa5IniDirBase}\Plugins"
static std::string iniFilePath;
static CPCONV* cpcU8_Wide;
static CPCONV* cpcU8_ACP;
static in2string fileNamePlaying; // currently playing file (used for getting info on the current file)
static PlayerA mainPlayer;
static DATA_LOADER* mainDLoad = NULL;
static FileInfoStorage* fisMain;
static FileInfoStorage* fisScan;
static FileInfoStorage* fisFileInfo;
struct SoundChipEnum
{
UINT8 id;
const char* name;
};
static std::vector<SoundChipEnum> soundChipList;
#if 0
extern "C" BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif
void Config(HWND hWndParent)
{
ConfigDlg_Show(WmpMod.hDllInstance, hWndParent);
return;
}
static void About(HWND hWndParent)
{
static const char* msgStr =
INVGM_TITLE
#ifdef UNICODE_INPUT_PLUGIN
" (Unicode build)"
#else
" (ANSI build)"
#endif
"\n"
"by Lee Dicker 2022\n"
"\n"
"Build date: " __DATE__ " (" INVGM_VERSION ")\n"
"\n"
"https://vgmrips.net/\n"
"\n"
"Current status:\n"
"\n"
"VGM file support up to version " VGM_VER_STR "\n"
"\n"
"Currently %u chips are emulated:\n" // CHIP_COUNT
"\n"
"%s\n" // ChipList
"\n"
"Using:\n"
"\n"
"ZLib " ZLIB_VERSION " (http://www.zlib.org/)\n"
"libvgm (https://github.com/ValleyBell/libvgm/)\n"
"\n"
"Special thanks goes to Maxim for the initial in_vgm plugin (v0.3x)\n"
"\n"
"Additional thanks to Valley Bell for libvgm and the rebuild of this plugin to link against it.\n"
"\n"
"Thanks to all the people that worked on/contributed to libvgm.\n";
std::string chipList;
std::string finalMsg;
size_t curChip;
// generate Chip list
for (curChip = 0; curChip < soundChipList.size(); curChip ++)
{
if (curChip && ! (curChip % 6))
{
// replace space with new-line every 6 chips
chipList[chipList.size() - 1] = '\n';
}
chipList += soundChipList[curChip].name;
chipList += ", ";
}
if (chipList.size() >= 2)
chipList = chipList.substr(0, chipList.size() - 2);
finalMsg.resize(strlen(msgStr) + 0x20 + chipList.length());
snprintf(&finalMsg[0], finalMsg.size(), msgStr, (unsigned)soundChipList.size(), chipList.c_str());
MessageBox(hWndParent, finalMsg.c_str(), WmpMod.description, MB_ICONINFORMATION | MB_OK);
return;
}
static char* GetAppFilePath(HMODULE hModule)
{
char* appPath;
int retVal;
#ifdef USE_WMAIN
std::vector<wchar_t> appPathW;
appPathW.resize(MAX_PATH);
retVal = GetModuleFileNameW(hModule, &appPathW[0], appPathW.size());
if (! retVal)
appPathW[0] = L'\0';
retVal = WideCharToMultiByte(CP_UTF8, 0, &appPathW[0], -1, NULL, 0, NULL, NULL);
if (retVal < 0)
retVal = 1;
appPath = (char*)malloc(retVal);
retVal = WideCharToMultiByte(CP_UTF8, 0, &appPathW[0], -1, appPath, retVal, NULL, NULL);
if (retVal < 0)
appPath[0] = '\0';
appPathW.clear();
#else
appPath = (char*)malloc(MAX_PATH * sizeof(char));
retVal = GetModuleFileNameA(hModule, appPath, MAX_PATH);
if (! retVal)
appPath[0] = '\0';
#endif
return appPath;
}
static void InitAppSearchPaths(HMODULE hModule)
{
appSearchPaths.clear();
// 1. DLL's directory ("C:\Program Files\Winamp\Plugins\")
char* appPath = GetAppFilePath(hModule);
const char* appTitle = GetFileTitle(appPath);
if (appTitle != appPath)
appSearchPaths.push_back(std::string(appPath, appTitle - appPath));
free(appPath);
// 2. Winamp configuration dir ("%APPDATA%\Winamp\Plugins)
// Note: checking wa5IniDirBase, but adding waIniDir is intentional
// When wa5IniDirBase is empty, then waIniDir is equal the DLL directory.
if (! wa5IniDirBase.empty())
appSearchPaths.push_back(waIniDir);
// 3. working directory
appSearchPaths.push_back("./");
return;
}
static void DetermineWinampIniPath(HMODULE hModule)
{
const char* result = (const char*)SendMessageA(WmpMod.hMainWindow, WM_WA_IPC, 0, IPC_GETINIDIRECTORY);
if (result != NULL)
{
// Winamp 5.11+ (with user profiles)
wa5IniDirBase = result;
waIniDir = CombinePaths(wa5IniDirBase, "Plugins");
StandardizeDirSeparators(waIniDir);
// make sure folder exists
CreateDirectoryA(waIniDir.c_str(), NULL);
waIniDir += '/';
}
else
{
// old Winamp
wa5IniDirBase = std::string();
char* appPath = GetAppFilePath(hModule);
const char* appTitle = GetFileTitle(appPath);
waIniDir = std::string(appPath, appTitle - appPath);
free(appPath);
}
return;
}
static const char* const FILETYPE_LIST[] =
{
"vgm;vgz", "VGM Audio Files (*.vgm; *.vgz)",
"s98", "T98 Sound Log Files (*.s98)",
"dro", "DOSBox Raw OPL Files (*.dro)",
"gym", "Genecyst Sound Files (*.gym)",
};
static void GenerateFileExtList(void)
{
wmpFileExtList = std::string();
if (pluginCfg.genOpts.fileTypes.vgm)
{
wmpFileExtList += FILETYPE_LIST[0]; wmpFileExtList += '\0';
wmpFileExtList += FILETYPE_LIST[1]; wmpFileExtList += '\0';
}
if (pluginCfg.genOpts.fileTypes.s98)
{
wmpFileExtList += FILETYPE_LIST[2]; wmpFileExtList += '\0';
wmpFileExtList += FILETYPE_LIST[3]; wmpFileExtList += '\0';
}
if (pluginCfg.genOpts.fileTypes.dro)
{
wmpFileExtList += FILETYPE_LIST[4]; wmpFileExtList += '\0';
wmpFileExtList += FILETYPE_LIST[5]; wmpFileExtList += '\0';
}
if (pluginCfg.genOpts.fileTypes.gym)
{
wmpFileExtList += FILETYPE_LIST[6]; wmpFileExtList += '\0';
wmpFileExtList += FILETYPE_LIST[7]; wmpFileExtList += '\0';
}
wmpFileExtList += '\0'; // final terminator
return;
}
static void Init(void)
{
//AllocConsole();
//freopen("CONOUT$", "w", stdout);
setlocale(LC_CTYPE, "");
InitAppSearchPaths(WmpMod.hDllInstance);
DetermineWinampIniPath(WmpMod.hDllInstance);
// The path of the INI to be loaded/written depends on the Winamp version.
// (No fallback paths for now.)
iniFilePath = CombinePaths(waIniDir, "in_vgm.ini");
Ini_SetFilePath(iniFilePath.c_str());
soundChipList.clear();
for (UINT16 curDev = 0; curDev < 0x100; curDev ++)
{
// just stupidly iterate over all possible IDs to figure out what sound chips are supported
const char* devName = SndEmu_GetDevName((UINT8)curDev, 0x00, NULL);
if (devName != NULL)
{
SoundChipEnum sce = {(UINT8)curDev, devName};
soundChipList.push_back(sce);
}
}
cpcU8_Wide = NULL;
CPConv_Init(&cpcU8_Wide, "UTF-8", "UTF-16LE");
{
std::string cpName(0x10, '\0');
snprintf(&cpName[0], cpName.size(), "CP%u", GetACP());
cpcU8_ACP = NULL;
CPConv_Init(&cpcU8_ACP, "UTF-8", cpName.c_str());
}
LoadConfiguration(pluginCfg, iniFilePath.c_str());
if (pluginCfg.genOpts.smplBits < 8)
pluginCfg.genOpts.smplBits = 8; // prevent Winamp *output* plugins from crashing due to div-by-0
else if (pluginCfg.genOpts.smplBits > 32)
pluginCfg.genOpts.smplBits = 32; // prevent buffer overflow (BLOCK_SIZE_MAX allows up to 32 bits only)
GenerateFileExtList();
WmpMod.FileExtensions = &wmpFileExtList[0];
mainPlayer.RegisterPlayerEngine(new VGMPlayer);
mainPlayer.RegisterPlayerEngine(new S98Player);
mainPlayer.RegisterPlayerEngine(new DROPlayer);
mainPlayer.RegisterPlayerEngine(new GYMPlayer);
mainPlayer.SetEventCallback(FilePlayCallback, NULL);
mainPlayer.SetFileReqCallback(PlayerFileReqCallback, NULL);
fisMain = new FileInfoStorage(&mainPlayer);
fisScan = new FileInfoStorage(NULL);
PlayerA* fileScanPlayer = fisScan->GetPlayer();
fisFileInfo = new FileInfoStorage(NULL);
InitFileInfoDialog(fisFileInfo);
// Let's just assume that Winamp doesn't call GetFileInfo() and GetExtendedFileInfo() in parallel
// and thus just share fisScan/cpU8_* between those two.
InitExtFileInfo(fisScan, cpcU8_Wide, cpcU8_ACP);
mainPlayer.SetOutputSettings(pluginCfg.genOpts.smplRate, NUM_CHN, BIT_PER_SEC, RENDER_SAMPLES);
//fileScanPlayer->SetSampleRate(pluginCfg.genOpts.smplRate);
//fileScanPlayer->SetFadeSamples(MSec2Samples(pluginCfg.genOpts.fadeTime, *fileScanPlayer));
ApplyCfg_General(mainPlayer, pluginCfg.genOpts);
for (size_t curChp = 0; curChp < pluginCfg.chipOpts.size(); curChp ++)
{
for (size_t curCSet = 0; curCSet < 2; curCSet ++)
{
const ChipOptions& cOpt = pluginCfg.chipOpts[curChp][curCSet];
if (cOpt.chipType == 0xFF)
continue;
ApplyCfg_Chip(mainPlayer, pluginCfg.genOpts, cOpt);
}
}
//printf("Everything loaded.\n");
return;
}
static void Deinit(void)
{
// one-time deinit, such as memory freeing
SaveConfiguration(pluginCfg, iniFilePath.c_str());
DeinitFileInfoDialog();
DeinitExtFileInfo();
mainPlayer.UnloadFile();
mainPlayer.UnregisterAllPlayers();
delete fisMain;
delete fisScan;
delete fisFileInfo;
CPConv_Deinit(cpcU8_Wide); cpcU8_Wide = NULL;
CPConv_Deinit(cpcU8_ACP); cpcU8_ACP = NULL;
//FreeConsole();
return;
}
static int IsOurFile(const in_char* fn)
{
// used for detecting URL streams - currently unused.
// return ! strncmp(fn, "http://",7); to detect HTTP streams, etc
return 0;
}
static inline UINT32 MulDivRound(UINT64 Number, UINT64 Numerator, UINT64 Denominator)
{
return (UINT32)((Number * Numerator + Denominator / 2) / Denominator);
}
static inline UINT32 MSec2Samples(UINT32 val, const PlayerA& player)
{
return (UINT32)(((UINT64)val * player.GetSampleRate() + 500) / 1000);
}
static void PreparePlayback(void)
{
PlayerBase* player = mainPlayer.GetPlayer();
UINT32 timeMS;
ApplyCfg_General(mainPlayer, pluginCfg.genOpts, true);
for (size_t curChp = 0; curChp < pluginCfg.chipOpts.size(); curChp ++)
{
for (size_t curCSet = 0; curCSet < 2; curCSet ++)
{
ChipOptions& cOpt = pluginCfg.chipOpts[curChp][curCSet];
if (cOpt.chipType == 0xFF)
continue;
ApplyCfg_Chip(mainPlayer, pluginCfg.genOpts, cOpt);
}
}
if (mainPlayer.GetPlayer()->GetPlayerType() == FCC_VGM)
{
VGMPlayer* vgmplay = dynamic_cast<VGMPlayer*>(mainPlayer.GetPlayer());
mainPlayer.SetLoopCount(vgmplay->GetModifiedLoopCount(pluginCfg.genOpts.maxLoops));
}
mainPlayer.SetFadeSamples(MSec2Samples(pluginCfg.genOpts.fadeTime, mainPlayer));
timeMS = (player->GetLoopTicks() == 0) ? pluginCfg.genOpts.pauseTime_jingle : pluginCfg.genOpts.pauseTime_loop;
mainPlayer.SetEndSilenceSamples(MSec2Samples(timeMS, mainPlayer));
return;
}
// called when winamp wants to play a file
static int Play(const in_char* fileName)
{
int maxlatency;
DWORD thread_id;
UINT8 retVal;
PausePlay = false;
EndPlay = false;
decode_pos = 0;
decode_pos_ms = 0;
seek_needed = -1;
mainPlayer.SetSampleRate(pluginCfg.genOpts.smplRate);
// return -1 - jump to next file in playlist
// return +1 - stop the playlist
#ifdef UNICODE_INPUT_PLUGIN
mainDLoad = FileLoader_InitW(fileName);
#else
mainDLoad = FileLoader_Init(fileName);
#endif
DataLoader_SetPreloadBytes(mainDLoad, 0x100);
retVal = DataLoader_Load(mainDLoad);
if (retVal)
{
DataLoader_CancelLoading(mainDLoad);
DataLoader_Deinit(mainDLoad); mainDLoad = NULL;
return -1; // file not found
}
retVal = mainPlayer.LoadFile(mainDLoad);
if (retVal)
{
DataLoader_CancelLoading(mainDLoad);
DataLoader_Deinit(mainDLoad); mainDLoad = NULL;
return +1; // file invalid
}
fisMain->SetFileName(fileName);
fisMain->ReadSongInfo();
fileNamePlaying = fileName;
// -1 and -1 are to specify buffer and prebuffer lengths.
// -1 means to use the default, which all input plug-ins should really do.
maxlatency = WmpMod.outMod->Open(pluginCfg.genOpts.smplRate, NUM_CHN, BIT_PER_SEC, -1, -1);
if (maxlatency < 0) // error opening device
return +1;
PreparePlayback();
mainPlayer.Start();
{
int kBitRate = (int)(fisMain->_songInfo.bitRate / 1000.0 + 0.5);
int sRateKHz = (pluginCfg.genOpts.smplRate + 500) / 1000;
WmpMod.SetInfo(kBitRate, sRateKHz, NUM_CHN, 1);
}
// initialize visualization stuff
WmpMod.SAVSAInit(maxlatency, pluginCfg.genOpts.smplRate);
WmpMod.VSASetInfo(pluginCfg.genOpts.smplRate, NUM_CHN);
// set the output plug-ins default volume.
// volume is 0-255, -666 is a token for current volume.
WmpMod.outMod->SetVolume(-666);
// launch decode thread
killDecodeThread = 0;
thread_handle = CreateThread(NULL, 0, DecodeThread, NULL, 0x00, &thread_id);
Dialogue_TrackChange();
return 0;
}
static void Pause(void)
{
PausePlay = true;
WmpMod.outMod->Pause(PausePlay);
return;
}
static void Unpause(void)
{
PausePlay = false;
WmpMod.outMod->Pause(PausePlay);
return;
}
static int IsPaused(void)
{
return PausePlay;
}
static void Stop(void)
{
if (InStopFunc)
return;
InStopFunc = true;
// TODO: add Mutex for this block.
// Stupid XMPlay seems to call Stop() twice in 2 seperate threads.
if (thread_handle != INVALID_HANDLE_VALUE)
{
killDecodeThread = 1;
if (WaitForSingleObject(thread_handle, 10000) == WAIT_TIMEOUT)
{
MessageBox(WmpMod.hMainWindow, "Error asking thread to die!\n",
"Error killing decode thread", 0);
TerminateThread(thread_handle, 0);
}
CloseHandle(thread_handle);
thread_handle = INVALID_HANDLE_VALUE;
}
// close output system
WmpMod.outMod->Close();
// deinitialize visualization
WmpMod.SAVSADeInit();
if (mainDLoad != NULL)
{
mainPlayer.Stop();
mainPlayer.UnloadFile();
DataLoader_Deinit(mainDLoad); mainDLoad = NULL;
}
if (pluginCfg.genOpts.resetMuting)
{
for (size_t curChp = 0; curChp < pluginCfg.chipOpts.size(); curChp ++)
{
for (size_t curCSet = 0; curCSet < 2; curCSet ++)
{
ChipOptions& cOpt = pluginCfg.chipOpts[curChp][curCSet];
if (cOpt.chipType == 0xFF)
continue;
cOpt.chipDisable = 0x00;
cOpt.muteMask[0] = 0x00;
cOpt.muteMask[1] = 0x00;
ApplyCfg_Chip(mainPlayer, pluginCfg.genOpts, cOpt);
}
}
// that would just make the check boxes flash during track change
// and Winamp's main window is locked when the configuration is open
// so I do it only when muting is reset
Dialogue_TrackChange();
}
fileNamePlaying.clear();
fisMain->ClearSongInfo();
InStopFunc = false;
return;
}
int GetPlrSongLengthMS(const PlayerA& plr)
{
const PlayerBase* player = plr.GetPlayer();
if (player == NULL)
return -1000; // nothing is playing right now
double songTime = plr.GetTotalTime(1);
if (songTime < 0.0)
return -1000; // for infinite loops
// for looping songs, add fade time
if (player->GetLoopTicks() > 0)
songTime += player->Sample2Second(plr.GetFadeSamples());
// Note: I do *not* include the "pause after song end" here. This is intentional.
return (int)(songTime * 1000 + 0.5);
}
int GetPlrSongLengthMS(const FileInfoStorage::SongInfo& songInfo)
{
if (songInfo.fileSize == 0)
return -1000; // nothing is playing right now
if (songInfo.looping && pluginCfg.genOpts.maxLoops == 0)
return -1000; // infinite loop
double songTime = songInfo.songLenL;
// for looping songs, add fade time
if (songInfo.looping)
songTime += pluginCfg.genOpts.fadeTime / 1000.0; // use fade time from options
// Note: I do *not* include the "pause after song end" here. This is intentional.
return (int)(songTime * 1000 + 0.5);
}
int GetLength(void) // return length of playing track
{
return GetPlrSongLengthMS(mainPlayer);
}
// returns current output position, in ms.
// you could just use return mod.outMod->GetOutputTime(),
// but the dsp plug-ins that do tempo changing tend to make that wrong.
static int GetOutputTime(void)
{
if (seek_needed != -1)
return seek_needed; // seeking in progress - return destination time
return decode_pos_ms + (WmpMod.outMod->GetOutputTime() - WmpMod.outMod->GetWrittenTime());
}
static void SetOutputTime(int time_in_ms) // for seeking
{
// Winamp seems to send negative seeking values, when GetLength returns -1000.
if (time_in_ms < 0)
return;
seek_needed = time_in_ms;
return;
}
static void SetVolume(int volume)
{
WmpMod.outMod->SetVolume(volume);
return;
}
static void SetPan(int pan)
{
WmpMod.outMod->SetPan(pan);
return;
}
const char* GetIniFilePath(void)
{
return iniFilePath.c_str();
}
FileInfoStorage* GetMainPlayerFIS(void)
{
return fisMain;
}
void RefreshPlaybackOptions(void)
{
INT32 volume = (INT32)(0x10000 * pluginCfg.genOpts.volume + 0.5);
mainPlayer.SetMasterVolume(volume);
return;
}
void RefreshMuting(void)
{
const std::vector<PlayerBase*>& plrs = mainPlayer.GetRegisteredPlayers();
for (size_t curChp = 0; curChp < pluginCfg.chipOpts.size(); curChp ++)
{
for (size_t curCSet = 0; curCSet < 2; curCSet ++)
{
const ChipOptions& cOpts = pluginCfg.chipOpts[curChp][curCSet];
PLR_MUTE_OPTS muteOpts;
UINT32 devID = PLR_DEV_ID(cOpts.chipType, cOpts.chipInstance);
muteOpts.disable = cOpts.chipDisable;
muteOpts.chnMute[0] = cOpts.muteMask[0];
muteOpts.chnMute[1] = cOpts.muteMask[1];
for (size_t curPlr = 0; curPlr < plrs.size(); curPlr ++)
plrs[curPlr]->SetDeviceMuting(devID, muteOpts);
}
}
return;
}
void RefreshPanning(void)
{
const std::vector<PlayerBase*>& plrs = mainPlayer.GetRegisteredPlayers();
for (size_t curChp = 0; curChp < pluginCfg.chipOpts.size(); curChp ++)
{
for (size_t curCSet = 0; curCSet < 2; curCSet ++)
{
const ChipOptions& cOpts = pluginCfg.chipOpts[curChp][curCSet];
UINT32 devID = PLR_DEV_ID(cOpts.chipType, cOpts.chipInstance);
for (size_t curPlr = 0; curPlr < plrs.size(); curPlr ++)
{
PlayerBase* pBase = plrs[curPlr];
PLR_DEV_OPTS devOpts;
UINT8 retVal = pBase->GetDeviceOptions(devID, devOpts);
if (retVal)
continue; // this player doesn't support this chip
for (size_t curInst = 0; curInst < 2; curInst ++)
{
devOpts.muteOpts.chnMute[curInst] = cOpts.muteMask[curInst];
for (size_t curChn = 0; curChn < 32; curChn ++)
devOpts.panOpts.chnPan[curInst][curChn] = cOpts.panMask[curInst][curChn];
}
pBase->SetDeviceOptions(devID, devOpts);
}
}
}
return;
}
void UpdatePlayback(void)
{
if (WmpMod.outMod == NULL || ! WmpMod.outMod->IsPlaying())
return;
if (pluginCfg.genOpts.immediateUpdate)
{
// add 30 ms - else it sounds like you seek back a little
SetOutputTime(GetOutputTime() + 30);
}
return;
}
static int InfoDialog(const in_char* fileName, HWND hWnd)
{
FileInfoDlg_LoadFile(fileName);
return FileInfoDlg_Show(WmpMod.hDllInstance, hWnd);
}
// This is an odd function. It is used to get the title and/or length of a track.
// If filename is either NULL or of length 0, it means you should
// return the info of LastFileName. Otherwise, return the information
// for the file in filename.
static void GetFileInfo(const in_char* fileName, in_char* title, int* length_in_ms)
{
FileInfoStorage* fis = NULL;
//printf("GetFileInfo(\"%s\")\n", fileName);
// Note: If fileName is be NULL or empty, return info about the current file.
if (fileName == NULL || fileName[0x00] == '\0' || fileNamePlaying == fileName)
{
fis = fisMain;
}
else
{
UINT8 retVal = fisScan->LoadSong(fileName, false);
if (retVal < 0x80)
fis = fisScan;
}
if (length_in_ms != NULL)
{
if (fis == NULL)
*length_in_ms = -1000; // File not found
else if (fis == fisMain)
*length_in_ms = GetPlrSongLengthMS(mainPlayer); // return based on "playing" setting
else
*length_in_ms = GetPlrSongLengthMS(fis->_songInfo); // new file: use songInfo + fading settings from options
//printf(" Length: %u ms\n", *length_in_ms);
}
if (title != NULL) // get non-path portion of fileName
{
if (fis != NULL && ! fis->_songInfo.tags.empty())
{
std::string titleU8 = FormatVGMTag(pluginCfg.genOpts.titleFormat.c_str(), *fis);
size_t titleSize = GETFILEINFO_TITLE_LENGTH - sizeof(in_char);
#ifndef UNICODE_INPUT_PLUGIN
CPConv_StrConvert(cpcU8_ACP, &titleSize, &title,
titleU8.length(), titleU8.c_str());
title[titleSize] = '\0';
#else
CPConv_StrConvert(cpcU8_Wide, &titleSize, reinterpret_cast<char**>(&title),
titleU8.length(), titleU8.c_str());
title[titleSize / sizeof(wchar_t)] = L'\0';
#endif
}
else
{
const in_char* fName = (fis == fisMain) ? fileNamePlaying.c_str() : fileName;
#ifdef UNICODE_INPUT_PLUGIN
wcsncpy(title, GetFileTitle(fName), GETFILEINFO_TITLE_LENGTH);
#else
strncpy(title, GetFileTitle(fName), GETFILEINFO_TITLE_LENGTH);
#endif
}
//printf(" Title: %s|#\n", title);
}
return;
}
static void EQ_Set(int on, char data[10], int preamp)
{
return; // unsupported
}
static DWORD WINAPI DecodeThread(LPVOID b)
{
char smplBuffer[BLOCK_SIZE_MAX * 2]; // has to be twice as big as the blocksize
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
while(! killDecodeThread)
{
if (seek_needed != -1) // requested seeking?
{
decode_pos = MulDivRound(seek_needed, pluginCfg.genOpts.smplRate, 1000);
//decode_pos_ms = MulDivRound(decode_pos, 1000, pluginCfg.genOpts.smplRate);
decode_pos_ms = seek_needed;
mainPlayer.Seek(PLAYPOS_SAMPLE, decode_pos);
// flush output device and set output to seek position
WmpMod.outMod->Flush(decode_pos_ms);
seek_needed = -1;
}
if (EndPlay)
{
// Playback finished
WmpMod.outMod->CanWrite(); // needed for some output drivers
if (! WmpMod.outMod->IsPlaying())
{
// we're done playing, so tell Winamp and quit the thread.
PostMessage(WmpMod.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
break;
}
Sleep(10); // give a little CPU time back to the system.
}
else if (WmpMod.outMod->CanWrite() >= (BLOCK_SIZE * (WmpMod.dsp_isactive() ? 2 : 1)))
// CanWrite() returns the number of bytes you can write, so we check that
// to the block size. the reason we multiply the block size by two if
// mod.dsp_isactive() is that DSP plug-ins can change it by up to a
// factor of two (for tempo adjustment).
{
UINT32 renderedBytes = mainPlayer.Render(BLOCK_SIZE, smplBuffer);
if (renderedBytes)
{
UINT32 RetSamples = renderedBytes / SMPL_BYTES;
// send data to the visualization and dsp systems
WmpMod.SAAddPCMData(smplBuffer, NUM_CHN, BIT_PER_SEC, decode_pos_ms);
WmpMod.VSAAddPCMData(smplBuffer, NUM_CHN, BIT_PER_SEC, decode_pos_ms);
if (WmpMod.dsp_isactive())
RetSamples = WmpMod.dsp_dosamples((short*)smplBuffer, RetSamples,
BIT_PER_SEC, NUM_CHN, pluginCfg.genOpts.smplRate);
WmpMod.outMod->Write(smplBuffer, RetSamples * SMPL_BYTES);
decode_pos += RetSamples;
decode_pos_ms = MulDivRound(decode_pos, 1000, pluginCfg.genOpts.smplRate);
}
}
else
{
Sleep(20); // Wait a little, until we can write again.
}
}
return 0;
}
static UINT8 FilePlayCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam)
{
switch(evtType)
{
case PLREVT_END:
EndPlay = true;
break;
}
return 0x00;
}
static DATA_LOADER* PlayerFileReqCallback(void* userParam, PlayerBase* player, const char* fileName)
{
std::string filePath = FindFile_Single(fileName, appSearchPaths);