-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmediainfo.cpp
421 lines (364 loc) · 10.6 KB
/
mediainfo.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
#include <string>
#include <vector>
#include <map>
#include <string.h> // for strcpy()
#include <math.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <glob.h>
#endif
#include <stdtype.h>
#include <player/playerbase.hpp>
#include <player/droplayer.hpp>
#include <player/gymplayer.hpp>
#include <player/s98player.hpp>
#include <player/vgmplayer.hpp>
#include <player/playera.hpp>
#include <emu/SoundDevs.h>
#include <emu/SoundEmu.h> // for SndEmu_GetDevName()
#include <utils/StrUtils.h>
#include "utils.hpp"
#include "mediainfo.hpp"
//#define DEBUG_ART_SEARCH 1
static void Tags_LangFilter(std::map<std::string, std::string>& tags, const std::string& tagName,
const std::vector<std::string>& langPostfixes, int defaultLang);
static inline std::string FCC2Str(UINT32 fcc);
MediaInfo::MediaInfo()
{
#ifdef _WIN32
_cpcUTF8toAPI = NULL;
_cpcAPItoUTF8 = NULL;
CPConv_Init(&_cpcUTF8toAPI, "UTF-8", "UTF-16LE");
CPConv_Init(&_cpcAPItoUTF8, "UTF-16LE", "UTF-8");
#endif
}
MediaInfo::~MediaInfo()
{
#ifdef _WIN32
CPConv_Deinit(_cpcUTF8toAPI);
CPConv_Deinit(_cpcAPItoUTF8);
#endif
}
void MediaInfo::PreparePlayback(void)
{
PlayerBase* player = _player.GetPlayer();
PLR_SONG_INFO sInf;
char verStr[0x20];
EnumerateTags(); // must be done before PreparePlayback(), as it may parse some of the tags
player->GetSongInfo(sInf);
// Note: sInf.loopTick is -1 for non-looping songs. player->GetLoopTicks() returns 0 that case.
_looping = (sInf.loopTick != (UINT32)-1);
_fileFmt = player->GetPlayerName();
_fileVerNum = (sInf.fileVerMaj << 8) | (sInf.fileVerMin << 0);
_volGain = sInf.volGain / (double)0x10000;
_isRawLog = false;
if (player->GetPlayerType() == FCC_VGM)
{
VGMPlayer* vgmplay = dynamic_cast<VGMPlayer*>(player);
const VGM_HEADER* vgmhdr = vgmplay->GetFileHeader();
sprintf(verStr, "VGM %X.%02X", (vgmhdr->fileVer >> 8) & 0xFF, (vgmhdr->fileVer >> 0) & 0xFF);
_fileEndPos = vgmhdr->dataEnd;
_fileStartPos = vgmhdr->dataOfs;
// RAW Log: no loop, no/empty Creator tag, no Title tag
if (! vgmhdr->loopOfs && _songTags.find("ENCODED_BY") == _songTags.end() &&
_songTags.find("TITLE") == _songTags.end())
_isRawLog = true;
if (vgmhdr->numTicks == 0)
_isRawLog = false;
}
else if (player->GetPlayerType() == FCC_S98)
{
S98Player* s98play = dynamic_cast<S98Player*>(player);
const S98_HEADER* s98hdr = s98play->GetFileHeader();
sprintf(verStr, "S98 v%u", s98hdr->fileVer);
_fileStartPos = s98hdr->dataOfs;
if (s98hdr->tagOfs > s98hdr->dataOfs)
_fileEndPos = s98hdr->tagOfs;
if (! s98hdr->loopOfs && _songTags.find("TITLE") == _songTags.end())
_isRawLog = true;
}
else if (player->GetPlayerType() == FCC_DRO)
{
DROPlayer* droplay = dynamic_cast<DROPlayer*>(player);
const DRO_HEADER* drohdr = droplay->GetFileHeader();
sprintf(verStr, "DRO v%u", drohdr->verMajor); // DRO has a "verMinor" field, but it's always 0
_fileStartPos = drohdr->dataOfs;
_isRawLog = true;
}
else if (player->GetPlayerType() == FCC_GYM)
{
GYMPlayer* gymplay = dynamic_cast<GYMPlayer*>(player);
const GYM_HEADER* gymhdr = gymplay->GetFileHeader();
if (! gymhdr->hasHeader)
strcpy(verStr, "GYM"); // raw GYM
else if (gymhdr->uncomprSize == 0)
strcpy(verStr, "GYMX"); // GYM with GYMX header, uncompressed
else
strcpy(verStr, "GYMX (z)"); // GYMX, compressed
_fileStartPos = gymhdr->dataOfs;
_fileEndPos = gymhdr->realFileSize;
_isRawLog = true;
}
else
{
strcpy(verStr, "???");
}
_fileVerStr = verStr;
return;
}
static void Tags_LangFilter(std::map<std::string, std::string>& tags, const std::string& tagName,
const std::vector<std::string>& langPostfixes, int defaultLang)
{
// 1. search for matching lang-tag
// 2. save that
// 3. remove all others
std::vector<std::string> langTags;
std::vector<std::string>::const_iterator lpfIt;
std::vector<std::string>::const_iterator ltIt;
std::map<std::string, std::string>::iterator chosenTagIt;
for (lpfIt = langPostfixes.begin(); lpfIt != langPostfixes.end(); ++lpfIt)
langTags.push_back(tagName + *lpfIt);
chosenTagIt = tags.end();
if (defaultLang >= 0 && (size_t)defaultLang < langTags.size())
chosenTagIt = tags.find(langTags[defaultLang]);
if (chosenTagIt == tags.end())
{
for (ltIt = langTags.begin(); ltIt != langTags.end(); ++ltIt)
{
chosenTagIt = tags.find(*ltIt);
if (chosenTagIt != tags.end())
break;
}
}
if (chosenTagIt == tags.end())
return;
if (chosenTagIt->first != tagName)
tags[tagName] = chosenTagIt->second;
for (ltIt = langTags.begin(); ltIt != langTags.end(); ++ltIt)
{
if (*ltIt == tagName)
continue;
tags.erase(*ltIt);
}
return;
}
void MediaInfo::EnumerateTags(void)
{
PlayerBase* player = _player.GetPlayer();
std::vector<std::string> langPostfixes;
int defaultLang = _genOpts.preferJapTag ? 1 : 0;
const char* const* tagList = player->GetTags();
_songTags.clear();
if (tagList == NULL)
return;
for (const char* const* t = tagList; *t != NULL; t += 2)
{
if (t[1][0] == '\0')
continue; // skip empty VGM tags, else the LangFilter may choose them
_songTags[t[0]] = t[1];
}
langPostfixes.push_back("");
langPostfixes.push_back("-JPN");
Tags_LangFilter(_songTags, "TITLE", langPostfixes, defaultLang);
Tags_LangFilter(_songTags, "GAME", langPostfixes, defaultLang);
Tags_LangFilter(_songTags, "SYSTEM", langPostfixes, defaultLang);
Tags_LangFilter(_songTags, "ARTIST", langPostfixes, defaultLang);
return;
}
const char* MediaInfo::GetSongTagForDisp(const std::string& tagName)
{
std::map<std::string, std::string>::const_iterator tagIt = _songTags.find(tagName);
return (tagIt == _songTags.end()) ? "" : tagIt->second.c_str();
}
static inline std::string FCC2Str(UINT32 fcc)
{
std::string result(4, '\0');
result[0] = (char)((fcc >> 24) & 0xFF);
result[1] = (char)((fcc >> 16) & 0xFF);
result[2] = (char)((fcc >> 8) & 0xFF);
result[3] = (char)((fcc >> 0) & 0xFF);
return result;
}
void MediaInfo::EnumerateChips(void)
{
PlayerBase* player = _player.GetPlayer();
std::vector<PLR_DEV_INFO> diList;
player->GetSongDeviceInfo(diList);
_chipList.clear();
for (size_t curDev = 0; curDev < diList.size(); curDev ++)
{
const PLR_DEV_INFO& pdi = diList[curDev];
const char* chipName = SndEmu_GetDevName(pdi.type, 0x01, pdi.devCfg);
if (pdi.type == DEVID_SN76496)
{
if (pdi.devCfg->flags & 0x01)
curDev ++; // the T6W28 consists of two "half" chips in VGMs
}
DeviceItem dItm;
dItm.name = chipName;
dItm.core = FCC2Str(pdi.core);
_chipList.push_back(dItm);
}
return;
}
#ifdef _WIN32
std::wstring MediaInfo::CharConv_UTF8toAPI(const std::string& textU8)
{
size_t textWLen = 0;
wchar_t* textWStr = NULL;
UINT8 retVal = CPConv_StrConvert(_cpcUTF8toAPI, &textWLen, reinterpret_cast<char**>(&textWStr),
textU8.length(), textU8.c_str());
std::wstring result;
if (retVal < 0x80)
result.assign(textWStr, textWStr + textWLen / sizeof(wchar_t));
free(textWStr);
return result;
}
std::string MediaInfo::CharConv_APItoUTF8(const std::wstring& textW)
{
size_t textU8Len = 0;
char* textU8Str = NULL;
UINT8 retVal = CPConv_StrConvert(_cpcAPItoUTF8, &textU8Len, &textU8Str,
textW.length() * sizeof(wchar_t), reinterpret_cast<const char*>(textW.c_str()));
std::string result;
if (retVal < 0x80)
result.assign(textU8Str, textU8Str + textU8Len);
free(textU8Str);
return result;
}
#endif
bool MediaInfo::FileExists(const std::string& fileName)
{
#ifdef _WIN32
std::wstring fileNameW = CharConv_UTF8toAPI(fileName);
FILE* fp = _wfopen(fileNameW.c_str(), L"rb");
#else
FILE* fp = fopen(fileName.c_str(), "rb");
#endif
if (fp == NULL)
return false;
fclose(fp);
return true;
}
void MediaInfo::SearchAlbumImage(void)
{
if (! _enableAlbumImage)
return;
// Thanks to Tasos Sahanidis for the art search algorithm.
#if DEBUG_ART_SEARCH
printf("Starting art search.\n");
#endif
_albumImgPath = std::string();
if (_playlistTrkID != (size_t)-1)
{
// for "abc/d/playlist.m3u", try "abc/d/playlist.png"
const char* fileExt = GetFileExtension(_playlistPath.c_str());
size_t dotPos = (fileExt != NULL) ? (fileExt - 1 - _playlistPath.c_str()) : _playlistPath.size();
std::string imgPath = _playlistPath.substr(0, dotPos) + ".png";
#if DEBUG_ART_SEARCH
printf("Trying %s\n", imgPath.c_str());
#endif
if (FileExists(imgPath))
{
_albumImgPath = imgPath;
return;
}
}
std::string basePath;
{
const char* filePath = _songPath.c_str();
const char* fileTitle = GetFileTitle(filePath);
basePath = _songPath.substr(0, fileTitle - filePath);
}
// If we get here, we're probably in single track mode, or the playlist is named differently.
// So we check [base path] + [album] + ".png"
const char* albumName = GetSongTagForDisp("GAME");
if (albumName[0] != '\0')
{
std::string imgPath = basePath + albumName + ".png";
#if DEBUG_ART_SEARCH
printf("Trying %s\n", imgPath.c_str());
#endif
if (FileExists(imgPath))
{
_albumImgPath = imgPath;
return;
}
}
// As a last resort, pick the first image glob can find in the base path.
// Append the case insensitive extension to the base path.
#ifdef _WIN32
{
std::string pathPattern = basePath + "*.png";
#if DEBUG_ART_SEARCH
printf("Using file search %s\n", pathPattern.c_str());
#endif
std::wstring pathPatW = CharConv_UTF8toAPI(pathPattern);
WIN32_FIND_DATAW ffd;
HANDLE hFind = FindFirstFileW(pathPatW.c_str(), &ffd);
if (hFind != INVALID_HANDLE_VALUE)
{
std::string fileNameU8 = CharConv_APItoUTF8(ffd.cFileName);
_albumImgPath = CombinePaths(basePath, fileNameU8);
FindClose(hFind);
return;
}
}
#else
{
std::string pathPattern = basePath + "*.[pP][nN][gG]";
#if DEBUG_ART_SEARCH
printf("Using glob %s\n", pathPattern.c_str());
#endif
glob_t result;
int ret = glob(pathPattern.c_str(), GLOB_NOSORT, NULL, &result);
if (ret == 0)
{
if (result.gl_pathc > 0)
{
_albumImgPath = result.gl_pathv[0];
globfree(&result);
return;
}
}
globfree(&result);
}
#endif
#if DEBUG_ART_SEARCH
printf("Art search failed.\n");
#endif
return;
}
void MediaInfo::AddSignalCallback(MI_SIGNAL_CB func, void* param)
{
SignalHandler scb = {func, param};
_sigCb.push_back(scb);
return;
}
void MediaInfo::RemoveSignalCallback(MI_SIGNAL_CB func, void* param)
{
std::vector<SignalHandler>::iterator scbIt;
for (scbIt = _sigCb.begin(); scbIt != _sigCb.end(); ++scbIt)
{
if (scbIt->func == func && scbIt->param == param)
{
_sigCb.erase(scbIt);
break;
}
}
return;
}
void MediaInfo::Event(UINT8 evtType, INT32 evtParam)
{
EventData ed = {evtType, evtParam};
_evtQueue.push(ed);
return;
}
void MediaInfo::Signal(UINT8 signalMask)
{
std::vector<SignalHandler>::iterator scbIt;
for (scbIt = _sigCb.begin(); scbIt != _sigCb.end(); ++scbIt)
scbIt->func(this, scbIt->param, signalMask);
return;
}