Skip to content

Instantly share code, notes, and snippets.

@lxfly2000
Last active May 28, 2018 08:49
Show Gist options
  • Save lxfly2000/ce63aaa804531c7b9be20b907422f159 to your computer and use it in GitHub Desktop.
Save lxfly2000/ce63aaa804531c7b9be20b907422f159 to your computer and use it in GitHub Desktop.
//pmd参考:https://github.com/mistydemeo/pmdmini
#pragma comment(lib,"PMDWin\\PMDWin.lib")
#pragma comment(lib,"mfreadwrite.lib")
#pragma comment(lib,"mfplat.lib")
#pragma comment(lib,"mfuuid.lib")
#include<Windows.h>
#include<mfidl.h>
#include<mfapi.h>
#include<mfreadwrite.h>
#define _WINDOWS
#include"PMDWin\PMDWinImport.h"
#include<stdio.h>
#include<locale.h>
//获取 Windows 支持的媒体文件的信息(此处为采样率)
unsigned GetRateFromFile(PCTSTR path)
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(MFStartup(MF_VERSION)))//mfplat
return 0;
IMFSourceReader *sr;
if (FAILED(MFCreateSourceReaderFromURL(path, NULL, &sr)))//mfreadwrite
return 0;
IMFMediaType *mt;
if (FAILED(sr->GetCurrentMediaType(0, &mt)))
return 0;
unsigned rate;
if (FAILED(mt->GetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, &rate)))//mfuuid
return 0;
sr->Release();
if (FAILED(MFShutdown()))//mfplat
return 0;
CoUninitialize();
return rate;
}
BOOL IsInteger(PCTSTR str)
{
for (int i = lstrlen(str) - 1; i >= 0; i--)
{
if (!isdigit(str[i]))
return FALSE;
}
return TRUE;
}
void ToMSS(int t, char *buf, size_t cb)
{
int m, sec;
sec = t / 1000;
t = t % 1000;
m = sec / 60;
sec = sec % 60;
sprintf_s(buf, cb, "%d:%02d.%03d", m, sec, t);
}
void GetLoop(char *path, unsigned sample_rate, int offset,BOOL inMSS)
{
int length, looppoint;
getlength(path, &length, &looppoint);
looppoint = length - looppoint;
length += offset;
looppoint += offset;
if (inMSS)
{
char chbuf[20];
ToMSS(looppoint, chbuf, sizeof chbuf);
printf(chbuf);
putchar(' ');
ToMSS(length, chbuf, sizeof chbuf);
puts(chbuf);
}
else
{
printf("%I64i %I64i\n", (INT64)looppoint*sample_rate / 1000, (INT64)length*sample_rate / 1000);
}
}
int wmain(int argc, wchar_t *argv[])
{
char pmdfile[MAX_PATH];
int offset = 0;
unsigned sample_rate = 44100;
BOOL inMSS = FALSE;
TCHAR mss[] = L"mss";
char initpath[] = ".";
setlocale(LC_ALL, "");
pmdwininit(initpath);
setpcmrate(SOUND_44K);
switch (argc)
{
case 5:
if (lstrcmp(argv[4], mss) == 0)
inMSS = TRUE;
case 4:offset = _wtoi(argv[3]);
case 3:
if (IsInteger(argv[2]))
sample_rate = _wtoi(argv[2]);
else
sample_rate = GetRateFromFile(argv[2]);
case 2:
wcstombs_s(NULL, pmdfile, MAX_PATH, argv[1], lstrlen(argv[1]));
GetLoop(pmdfile, sample_rate, offset, inMSS);
break;
default:
printf("%S <PMD文件> [采样率或通过文件指定=%d] [偏移=%d] [%S]\n", argv[0], sample_rate, offset, mss);
#ifdef _DEBUG
printf("PMD文件:");
gets_s(pmdfile);
{
TCHAR mffile[MAX_PATH];
printf("采样率或音频文件:");
_getws_s(mffile, MAX_PATH);
if (IsInteger(mffile))
sample_rate = _wtoi(mffile);
else
sample_rate = GetRateFromFile(mffile);
}
printf("偏移毫秒数:");
scanf_s("%d", &offset);
printf("以时间显示[0/1]:");
scanf_s("%d", &inMSS);
GetLoop(pmdfile, sample_rate, offset, inMSS);
#endif
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment