DMOPlugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * DMOPlugin.h
  3. * -----------
  4. * Purpose: DirectX Media Object plugin handling / processing.
  5. * Notes : Some default plugins only have the same output characteristics in the floating point code path (compared to integer PCM)
  6. * if we feed them input in the range [-32768, +32768] rather than the more usual [-1, +1].
  7. * Hence, OpenMPT uses this range for both the floating-point and integer path.
  8. * Authors: OpenMPT Devs
  9. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  10. */
  11. #include "stdafx.h"
  12. #include "mpt/base/aligned_array.hpp"
  13. #if defined(MPT_WITH_DMO)
  14. #include "mpt/uuid/guid.hpp"
  15. #include "../../Sndfile.h"
  16. #include "DMOPlugin.h"
  17. #include "../PluginManager.h"
  18. #include <uuids.h>
  19. #include <medparam.h>
  20. #include <mmsystem.h>
  21. #endif // MPT_WITH_DMO
  22. OPENMPT_NAMESPACE_BEGIN
  23. #if defined(MPT_WITH_DMO)
  24. #ifdef MPT_ALL_LOGGING
  25. #define DMO_LOG
  26. #else
  27. #define DMO_LOG
  28. #endif
  29. IMixPlugin* DMOPlugin::Create(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct)
  30. {
  31. CLSID clsid;
  32. if(mpt::VerifyStringToCLSID(factory.dllPath.AsNative(), clsid))
  33. {
  34. IMediaObject *pMO = nullptr;
  35. IMediaObjectInPlace *pMOIP = nullptr;
  36. if ((CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, IID_IMediaObject, (VOID **)&pMO) == S_OK) && (pMO))
  37. {
  38. if (pMO->QueryInterface(IID_IMediaObjectInPlace, (void **)&pMOIP) != S_OK) pMOIP = nullptr;
  39. } else pMO = nullptr;
  40. if ((pMO) && (pMOIP))
  41. {
  42. DWORD dwInputs = 0, dwOutputs = 0;
  43. pMO->GetStreamCount(&dwInputs, &dwOutputs);
  44. if (dwInputs == 1 && dwOutputs == 1)
  45. {
  46. DMOPlugin *p = new (std::nothrow) DMOPlugin(factory, sndFile, mixStruct, pMO, pMOIP, clsid.Data1);
  47. return p;
  48. }
  49. #ifdef DMO_LOG
  50. MPT_LOG_GLOBAL(LogDebug, "DMO", factory.libraryName.ToUnicode() + U_(": Unable to use this DMO"));
  51. #endif
  52. }
  53. #ifdef DMO_LOG
  54. else MPT_LOG_GLOBAL(LogDebug, "DMO", factory.libraryName.ToUnicode() + U_(": Failed to get IMediaObject & IMediaObjectInPlace interfaces"));
  55. #endif
  56. if (pMO) pMO->Release();
  57. if (pMOIP) pMOIP->Release();
  58. }
  59. return nullptr;
  60. }
  61. DMOPlugin::DMOPlugin(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct, IMediaObject *pMO, IMediaObjectInPlace *pMOIP, uint32 uid)
  62. : IMixPlugin(factory, sndFile, mixStruct)
  63. , m_pMediaObject(pMO)
  64. , m_pMediaProcess(pMOIP)
  65. , m_pParamInfo(nullptr)
  66. , m_pMediaParams(nullptr)
  67. , m_nSamplesPerSec(sndFile.GetSampleRate())
  68. , m_uid(uid)
  69. {
  70. if(FAILED(m_pMediaObject->QueryInterface(IID_IMediaParamInfo, (void **)&m_pParamInfo)))
  71. m_pParamInfo = nullptr;
  72. if (FAILED(m_pMediaObject->QueryInterface(IID_IMediaParams, (void **)&m_pMediaParams)))
  73. m_pMediaParams = nullptr;
  74. m_alignedBuffer.f32 = mpt::align_bytes<16, MIXBUFFERSIZE * 2>(m_interleavedBuffer.f32);
  75. m_mixBuffer.Initialize(2, 2);
  76. InsertIntoFactoryList();
  77. }
  78. DMOPlugin::~DMOPlugin()
  79. {
  80. if(m_pMediaParams)
  81. {
  82. m_pMediaParams->Release();
  83. m_pMediaParams = nullptr;
  84. }
  85. if(m_pParamInfo)
  86. {
  87. m_pParamInfo->Release();
  88. m_pParamInfo = nullptr;
  89. }
  90. if(m_pMediaProcess)
  91. {
  92. m_pMediaProcess->Release();
  93. m_pMediaProcess = nullptr;
  94. }
  95. if(m_pMediaObject)
  96. {
  97. m_pMediaObject->Release();
  98. m_pMediaObject = nullptr;
  99. }
  100. }
  101. uint32 DMOPlugin::GetLatency() const
  102. {
  103. REFERENCE_TIME time; // Unit 100-nanoseconds
  104. if(m_pMediaProcess->GetLatency(&time) == S_OK)
  105. {
  106. return static_cast<uint32>(time * m_nSamplesPerSec / (10 * 1000 * 1000));
  107. }
  108. return 0;
  109. }
  110. static constexpr float _f2si = 32768.0f;
  111. static constexpr float _si2f = 1.0f / 32768.0f;
  112. static void InterleaveStereo(const float * MPT_RESTRICT inputL, const float * MPT_RESTRICT inputR, float * MPT_RESTRICT output, uint32 numFrames)
  113. {
  114. while(numFrames--)
  115. {
  116. *(output++) = *(inputL++) * _f2si;
  117. *(output++) = *(inputR++) * _f2si;
  118. }
  119. }
  120. static void DeinterleaveStereo(const float * MPT_RESTRICT input, float * MPT_RESTRICT outputL, float * MPT_RESTRICT outputR, uint32 numFrames)
  121. {
  122. while(numFrames--)
  123. {
  124. *(outputL++) = *(input++) * _si2f;
  125. *(outputR++) = *(input++) * _si2f;
  126. }
  127. }
  128. // Interleave two float streams into one int16 stereo stream.
  129. static void InterleaveFloatToInt16(const float * MPT_RESTRICT inputL, const float * MPT_RESTRICT inputR, int16 * MPT_RESTRICT output, uint32 numFrames)
  130. {
  131. while(numFrames--)
  132. {
  133. *(output++) = static_cast<int16>(Clamp(*(inputL++) * _f2si, static_cast<float>(int16_min), static_cast<float>(int16_max)));
  134. *(output++) = static_cast<int16>(Clamp(*(inputR++) * _f2si, static_cast<float>(int16_min), static_cast<float>(int16_max)));
  135. }
  136. }
  137. // Deinterleave an int16 stereo stream into two float streams.
  138. static void DeinterleaveInt16ToFloat(const int16 * MPT_RESTRICT input, float * MPT_RESTRICT outputL, float * MPT_RESTRICT outputR, uint32 numFrames)
  139. {
  140. while(numFrames--)
  141. {
  142. *outputL++ += _si2f * static_cast<float>(*input++);
  143. *outputR++ += _si2f * static_cast<float>(*input++);
  144. }
  145. }
  146. void DMOPlugin::Process(float *pOutL, float *pOutR, uint32 numFrames)
  147. {
  148. if(!numFrames || !m_mixBuffer.Ok())
  149. return;
  150. m_mixBuffer.ClearOutputBuffers(numFrames);
  151. REFERENCE_TIME startTime = Util::muldiv(m_SndFile.GetTotalSampleCount(), 10000000, m_nSamplesPerSec);
  152. if(m_useFloat)
  153. {
  154. InterleaveStereo(m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1), m_alignedBuffer.f32, numFrames);
  155. m_pMediaProcess->Process(numFrames * 2 * sizeof(float), reinterpret_cast<BYTE *>(m_alignedBuffer.f32), startTime, DMO_INPLACE_NORMAL);
  156. DeinterleaveStereo(m_alignedBuffer.f32, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
  157. } else
  158. {
  159. InterleaveFloatToInt16(m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1), m_alignedBuffer.i16, numFrames);
  160. m_pMediaProcess->Process(numFrames * 2 * sizeof(int16), reinterpret_cast<BYTE *>(m_alignedBuffer.i16), startTime, DMO_INPLACE_NORMAL);
  161. DeinterleaveInt16ToFloat(m_alignedBuffer.i16, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
  162. }
  163. ProcessMixOps(pOutL, pOutR, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
  164. }
  165. PlugParamIndex DMOPlugin::GetNumParameters() const
  166. {
  167. DWORD dwParamCount = 0;
  168. m_pParamInfo->GetParamCount(&dwParamCount);
  169. return dwParamCount;
  170. }
  171. PlugParamValue DMOPlugin::GetParameter(PlugParamIndex index)
  172. {
  173. if(index < GetNumParameters() && m_pParamInfo != nullptr && m_pMediaParams != nullptr)
  174. {
  175. MP_PARAMINFO mpi;
  176. MP_DATA md;
  177. MemsetZero(mpi);
  178. md = 0;
  179. if (m_pParamInfo->GetParamInfo(index, &mpi) == S_OK
  180. && m_pMediaParams->GetParam(index, &md) == S_OK)
  181. {
  182. float fValue, fMin, fMax, fDefault;
  183. fValue = md;
  184. fMin = mpi.mpdMinValue;
  185. fMax = mpi.mpdMaxValue;
  186. fDefault = mpi.mpdNeutralValue;
  187. if (mpi.mpType == MPT_BOOL)
  188. {
  189. fMin = 0;
  190. fMax = 1;
  191. }
  192. fValue -= fMin;
  193. if (fMax > fMin) fValue /= (fMax - fMin);
  194. return fValue;
  195. }
  196. }
  197. return 0;
  198. }
  199. void DMOPlugin::SetParameter(PlugParamIndex index, PlugParamValue value)
  200. {
  201. if(index < GetNumParameters() && m_pParamInfo != nullptr && m_pMediaParams != nullptr)
  202. {
  203. MP_PARAMINFO mpi;
  204. MemsetZero(mpi);
  205. if (m_pParamInfo->GetParamInfo(index, &mpi) == S_OK)
  206. {
  207. float fMin = mpi.mpdMinValue;
  208. float fMax = mpi.mpdMaxValue;
  209. if (mpi.mpType == MPT_BOOL)
  210. {
  211. fMin = 0;
  212. fMax = 1;
  213. value = (value > 0.5f) ? 1.0f : 0.0f;
  214. }
  215. if (fMax > fMin) value *= (fMax - fMin);
  216. value += fMin;
  217. value = mpt::safe_clamp(value, fMin, fMax);
  218. if (mpi.mpType != MPT_FLOAT) value = mpt::round(value);
  219. m_pMediaParams->SetParam(index, value);
  220. }
  221. }
  222. }
  223. void DMOPlugin::Resume()
  224. {
  225. m_nSamplesPerSec = m_SndFile.GetSampleRate();
  226. m_isResumed = true;
  227. DMO_MEDIA_TYPE mt;
  228. WAVEFORMATEX wfx;
  229. mt.majortype = MEDIATYPE_Audio;
  230. mt.subtype = MEDIASUBTYPE_PCM;
  231. mt.bFixedSizeSamples = TRUE;
  232. mt.bTemporalCompression = FALSE;
  233. mt.formattype = FORMAT_WaveFormatEx;
  234. mt.pUnk = nullptr;
  235. mt.pbFormat = (LPBYTE)&wfx;
  236. mt.cbFormat = sizeof(WAVEFORMATEX);
  237. mt.lSampleSize = 2 * sizeof(float);
  238. wfx.wFormatTag = 3; // WAVE_FORMAT_IEEE_FLOAT;
  239. wfx.nChannels = 2;
  240. wfx.nSamplesPerSec = m_nSamplesPerSec;
  241. wfx.wBitsPerSample = sizeof(float) * 8;
  242. wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
  243. wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
  244. wfx.cbSize = 0;
  245. // First try 32-bit float (DirectX 9+)
  246. m_useFloat = true;
  247. if(FAILED(m_pMediaObject->SetInputType(0, &mt, 0))
  248. || FAILED(m_pMediaObject->SetOutputType(0, &mt, 0)))
  249. {
  250. m_useFloat = false;
  251. // Try again with 16-bit PCM
  252. mt.lSampleSize = 2 * sizeof(int16);
  253. wfx.wFormatTag = WAVE_FORMAT_PCM;
  254. wfx.wBitsPerSample = sizeof(int16) * 8;
  255. wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
  256. wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
  257. if(FAILED(m_pMediaObject->SetInputType(0, &mt, 0))
  258. || FAILED(m_pMediaObject->SetOutputType(0, &mt, 0)))
  259. {
  260. #ifdef DMO_LOG
  261. MPT_LOG_GLOBAL(LogDebug, "DMO", U_("DMO: Failed to set I/O media type"));
  262. #endif
  263. }
  264. }
  265. }
  266. void DMOPlugin::PositionChanged()
  267. {
  268. m_pMediaObject->Discontinuity(0);
  269. m_pMediaObject->Flush();
  270. }
  271. void DMOPlugin::Suspend()
  272. {
  273. m_isResumed = false;
  274. m_pMediaObject->Flush();
  275. m_pMediaObject->SetInputType(0, nullptr, DMO_SET_TYPEF_CLEAR);
  276. m_pMediaObject->SetOutputType(0, nullptr, DMO_SET_TYPEF_CLEAR);
  277. }
  278. #ifdef MODPLUG_TRACKER
  279. CString DMOPlugin::GetParamName(PlugParamIndex param)
  280. {
  281. if(param < GetNumParameters() && m_pParamInfo != nullptr)
  282. {
  283. MP_PARAMINFO mpi;
  284. mpi.mpType = MPT_INT;
  285. mpi.szUnitText[0] = 0;
  286. mpi.szLabel[0] = 0;
  287. if(m_pParamInfo->GetParamInfo(param, &mpi) == S_OK)
  288. {
  289. return mpt::ToCString(mpi.szLabel);
  290. }
  291. }
  292. return CString();
  293. }
  294. CString DMOPlugin::GetParamLabel(PlugParamIndex param)
  295. {
  296. if(param < GetNumParameters() && m_pParamInfo != nullptr)
  297. {
  298. MP_PARAMINFO mpi;
  299. mpi.mpType = MPT_INT;
  300. mpi.szUnitText[0] = 0;
  301. mpi.szLabel[0] = 0;
  302. if(m_pParamInfo->GetParamInfo(param, &mpi) == S_OK)
  303. {
  304. return mpt::ToCString(mpi.szUnitText);
  305. }
  306. }
  307. return CString();
  308. }
  309. CString DMOPlugin::GetParamDisplay(PlugParamIndex param)
  310. {
  311. if(param < GetNumParameters() && m_pParamInfo != nullptr && m_pMediaParams != nullptr)
  312. {
  313. MP_PARAMINFO mpi;
  314. mpi.mpType = MPT_INT;
  315. mpi.szUnitText[0] = 0;
  316. mpi.szLabel[0] = 0;
  317. if (m_pParamInfo->GetParamInfo(param, &mpi) == S_OK)
  318. {
  319. MP_DATA md;
  320. if(m_pMediaParams->GetParam(param, &md) == S_OK)
  321. {
  322. switch(mpi.mpType)
  323. {
  324. case MPT_FLOAT:
  325. {
  326. CString s;
  327. s.Format(_T("%.2f"), md);
  328. return s;
  329. }
  330. break;
  331. case MPT_BOOL:
  332. return ((int)md) ? _T("Yes") : _T("No");
  333. break;
  334. case MPT_ENUM:
  335. {
  336. WCHAR *text = nullptr;
  337. m_pParamInfo->GetParamText(param, &text);
  338. const int nValue = mpt::saturate_round<int>(md * (mpi.mpdMaxValue - mpi.mpdMinValue));
  339. // Always skip first two strings (param name, unit name)
  340. for(int i = 0; i < nValue + 2; i++)
  341. {
  342. text += wcslen(text) + 1;
  343. }
  344. return mpt::ToCString(text);
  345. }
  346. break;
  347. case MPT_INT:
  348. default:
  349. {
  350. CString s;
  351. s.Format(_T("%d"), mpt::saturate_round<int>(md));
  352. return s;
  353. }
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. return CString();
  360. }
  361. #endif // MODPLUG_TRACKER
  362. #else // !MPT_WITH_DMO
  363. MPT_MSVC_WORKAROUND_LNK4221(DMOPlugin)
  364. #endif // MPT_WITH_DMO
  365. OPENMPT_NAMESPACE_END