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
|
/*
* DMOPlugin.h
* -----------
* Purpose: DirectX Media Object plugin handling / processing.
* Notes : Some default plugins only have the same output characteristics in the floating point code path (compared to integer PCM)
* if we feed them input in the range [-32768, +32768] rather than the more usual [-1, +1].
* Hence, OpenMPT uses this range for both the floating-point and integer path.
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "mpt/base/aligned_array.hpp"
#if defined(MPT_WITH_DMO)
#include "DMOPlugin.h"
#include "mpt/uuid/guid.hpp"
#include "../../Sndfile.h"
#include "../PluginManager.h"
#include <uuids.h>
#include <medparam.h>
#include <mmsystem.h>
#endif // MPT_WITH_DMO
OPENMPT_NAMESPACE_BEGIN
#if defined(MPT_WITH_DMO)
#ifdef MPT_ALL_LOGGING
#define DMO_LOG
#else
#define DMO_LOG
#endif
IMixPlugin* DMOPlugin::Create(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN &mixStruct)
{
CLSID clsid;
if(mpt::VerifyStringToCLSID(factory.dllPath.AsNative(), clsid))
{
IMediaObject *pMO = nullptr;
IMediaObjectInPlace *pMOIP = nullptr;
if ((CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, IID_IMediaObject, (VOID **)&pMO) == S_OK) && (pMO))
{
if (pMO->QueryInterface(IID_IMediaObjectInPlace, (void **)&pMOIP) != S_OK) pMOIP = nullptr;
} else pMO = nullptr;
if ((pMO) && (pMOIP))
{
DWORD dwInputs = 0, dwOutputs = 0;
pMO->GetStreamCount(&dwInputs, &dwOutputs);
if (dwInputs == 1 && dwOutputs == 1)
{
DMOPlugin *p = new (std::nothrow) DMOPlugin(factory, sndFile, mixStruct, pMO, pMOIP, clsid.Data1);
return p;
}
#ifdef DMO_LOG
MPT_LOG_GLOBAL(LogDebug, "DMO", factory.libraryName.ToUnicode() + U_(": Unable to use this DMO"));
#endif
}
#ifdef DMO_LOG
else MPT_LOG_GLOBAL(LogDebug, "DMO", factory.libraryName.ToUnicode() + U_(": Failed to get IMediaObject & IMediaObjectInPlace interfaces"));
#endif
if (pMO) pMO->Release();
if (pMOIP) pMOIP->Release();
}
return nullptr;
}
DMOPlugin::DMOPlugin(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN &mixStruct, IMediaObject *pMO, IMediaObjectInPlace *pMOIP, uint32 uid)
: IMixPlugin(factory, sndFile, mixStruct)
, m_pMediaObject(pMO)
, m_pMediaProcess(pMOIP)
, m_pParamInfo(nullptr)
, m_pMediaParams(nullptr)
, m_nSamplesPerSec(sndFile.GetSampleRate())
, m_uid(uid)
{
if(FAILED(m_pMediaObject->QueryInterface(IID_IMediaParamInfo, (void **)&m_pParamInfo)))
m_pParamInfo = nullptr;
if (FAILED(m_pMediaObject->QueryInterface(IID_IMediaParams, (void **)&m_pMediaParams)))
m_pMediaParams = nullptr;
m_alignedBuffer.f32 = mpt::align_bytes<16, MIXBUFFERSIZE * 2>(m_interleavedBuffer.f32);
m_mixBuffer.Initialize(2, 2);
}
DMOPlugin::~DMOPlugin()
{
if(m_pMediaParams)
{
m_pMediaParams->Release();
m_pMediaParams = nullptr;
}
if(m_pParamInfo)
{
m_pParamInfo->Release();
m_pParamInfo = nullptr;
}
if(m_pMediaProcess)
{
m_pMediaProcess->Release();
m_pMediaProcess = nullptr;
}
if(m_pMediaObject)
{
m_pMediaObject->Release();
m_pMediaObject = nullptr;
}
}
uint32 DMOPlugin::GetLatency() const
{
REFERENCE_TIME time; // Unit 100-nanoseconds
if(m_pMediaProcess->GetLatency(&time) == S_OK)
{
return static_cast<uint32>(time * m_nSamplesPerSec / (10 * 1000 * 1000));
}
return 0;
}
static constexpr float _f2si = 32768.0f;
static constexpr float _si2f = 1.0f / 32768.0f;
static void InterleaveStereo(const float * MPT_RESTRICT inputL, const float * MPT_RESTRICT inputR, float * MPT_RESTRICT output, uint32 numFrames)
{
while(numFrames--)
{
*(output++) = *(inputL++) * _f2si;
*(output++) = *(inputR++) * _f2si;
}
}
static void DeinterleaveStereo(const float * MPT_RESTRICT input, float * MPT_RESTRICT outputL, float * MPT_RESTRICT outputR, uint32 numFrames)
{
while(numFrames--)
{
*(outputL++) = *(input++) * _si2f;
*(outputR++) = *(input++) * _si2f;
}
}
// Interleave two float streams into one int16 stereo stream.
static void InterleaveFloatToInt16(const float * MPT_RESTRICT inputL, const float * MPT_RESTRICT inputR, int16 * MPT_RESTRICT output, uint32 numFrames)
{
while(numFrames--)
{
*(output++) = static_cast<int16>(Clamp(*(inputL++) * _f2si, static_cast<float>(int16_min), static_cast<float>(int16_max)));
*(output++) = static_cast<int16>(Clamp(*(inputR++) * _f2si, static_cast<float>(int16_min), static_cast<float>(int16_max)));
}
}
// Deinterleave an int16 stereo stream into two float streams.
static void DeinterleaveInt16ToFloat(const int16 * MPT_RESTRICT input, float * MPT_RESTRICT outputL, float * MPT_RESTRICT outputR, uint32 numFrames)
{
while(numFrames--)
{
*outputL++ += _si2f * static_cast<float>(*input++);
*outputR++ += _si2f * static_cast<float>(*input++);
}
}
void DMOPlugin::Process(float *pOutL, float *pOutR, uint32 numFrames)
{
if(!numFrames || !m_mixBuffer.Ok())
return;
m_mixBuffer.ClearOutputBuffers(numFrames);
REFERENCE_TIME startTime = Util::muldiv(m_SndFile.GetTotalSampleCount(), 10000000, m_nSamplesPerSec);
if(m_useFloat)
{
InterleaveStereo(m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1), m_alignedBuffer.f32, numFrames);
m_pMediaProcess->Process(numFrames * 2 * sizeof(float), reinterpret_cast<BYTE *>(m_alignedBuffer.f32), startTime, DMO_INPLACE_NORMAL);
DeinterleaveStereo(m_alignedBuffer.f32, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
} else
{
InterleaveFloatToInt16(m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1), m_alignedBuffer.i16, numFrames);
m_pMediaProcess->Process(numFrames * 2 * sizeof(int16), reinterpret_cast<BYTE *>(m_alignedBuffer.i16), startTime, DMO_INPLACE_NORMAL);
DeinterleaveInt16ToFloat(m_alignedBuffer.i16, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
}
ProcessMixOps(pOutL, pOutR, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
}
PlugParamIndex DMOPlugin::GetNumParameters() const
{
DWORD dwParamCount = 0;
m_pParamInfo->GetParamCount(&dwParamCount);
return dwParamCount;
}
PlugParamValue DMOPlugin::GetParameter(PlugParamIndex index)
{
if(index < GetNumParameters() && m_pParamInfo != nullptr && m_pMediaParams != nullptr)
{
MP_PARAMINFO mpi{};
MP_DATA md = 0;
if (m_pParamInfo->GetParamInfo(index, &mpi) == S_OK
&& m_pMediaParams->GetParam(index, &md) == S_OK)
{
float fValue = md;
float fMin = mpi.mpdMinValue;
float fMax = mpi.mpdMaxValue;
//float fDefault = mpi.mpdNeutralValue;
if (mpi.mpType == MPT_BOOL)
{
fMin = 0;
fMax = 1;
}
fValue -= fMin;
if (fMax > fMin) fValue /= (fMax - fMin);
return fValue;
}
}
return 0;
}
void DMOPlugin::SetParameter(PlugParamIndex index, PlugParamValue value, PlayState *, CHANNELINDEX)
{
if(index < GetNumParameters() && m_pParamInfo != nullptr && m_pMediaParams != nullptr)
{
MP_PARAMINFO mpi{};
if (m_pParamInfo->GetParamInfo(index, &mpi) == S_OK)
{
float fMin = mpi.mpdMinValue;
float fMax = mpi.mpdMaxValue;
if (mpi.mpType == MPT_BOOL)
{
fMin = 0;
fMax = 1;
value = (value > 0.5f) ? 1.0f : 0.0f;
}
if (fMax > fMin) value *= (fMax - fMin);
value += fMin;
value = mpt::safe_clamp(value, fMin, fMax);
if (mpi.mpType != MPT_FLOAT) value = mpt::round(value);
m_pMediaParams->SetParam(index, value);
}
}
}
void DMOPlugin::Resume()
{
m_nSamplesPerSec = m_SndFile.GetSampleRate();
m_isResumed = true;
DMO_MEDIA_TYPE mt;
WAVEFORMATEX wfx;
mt.majortype = MEDIATYPE_Audio;
mt.subtype = MEDIASUBTYPE_PCM;
mt.bFixedSizeSamples = TRUE;
mt.bTemporalCompression = FALSE;
mt.formattype = FORMAT_WaveFormatEx;
mt.pUnk = nullptr;
mt.pbFormat = (LPBYTE)&wfx;
mt.cbFormat = sizeof(WAVEFORMATEX);
mt.lSampleSize = 2 * sizeof(float);
wfx.wFormatTag = 3; // WAVE_FORMAT_IEEE_FLOAT;
wfx.nChannels = 2;
wfx.nSamplesPerSec = m_nSamplesPerSec;
wfx.wBitsPerSample = sizeof(float) * 8;
wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.cbSize = 0;
// First try 32-bit float (DirectX 9+)
m_useFloat = true;
if(FAILED(m_pMediaObject->SetInputType(0, &mt, 0))
|| FAILED(m_pMediaObject->SetOutputType(0, &mt, 0)))
{
m_useFloat = false;
// Try again with 16-bit PCM
mt.lSampleSize = 2 * sizeof(int16);
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.wBitsPerSample = sizeof(int16) * 8;
wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
if(FAILED(m_pMediaObject->SetInputType(0, &mt, 0))
|| FAILED(m_pMediaObject->SetOutputType(0, &mt, 0)))
{
#ifdef DMO_LOG
MPT_LOG_GLOBAL(LogDebug, "DMO", U_("DMO: Failed to set I/O media type"));
#endif
}
}
}
void DMOPlugin::PositionChanged()
{
m_pMediaObject->Discontinuity(0);
m_pMediaObject->Flush();
}
void DMOPlugin::Suspend()
{
m_isResumed = false;
m_pMediaObject->Flush();
m_pMediaObject->SetInputType(0, nullptr, DMO_SET_TYPEF_CLEAR);
m_pMediaObject->SetOutputType(0, nullptr, DMO_SET_TYPEF_CLEAR);
}
#ifdef MODPLUG_TRACKER
CString DMOPlugin::GetParamName(PlugParamIndex param)
{
if(param < GetNumParameters() && m_pParamInfo != nullptr)
{
MP_PARAMINFO mpi;
mpi.mpType = MPT_INT;
mpi.szUnitText[0] = 0;
mpi.szLabel[0] = 0;
if(m_pParamInfo->GetParamInfo(param, &mpi) == S_OK)
{
return mpt::ToCString(mpi.szLabel);
}
}
return CString();
}
CString DMOPlugin::GetParamLabel(PlugParamIndex param)
{
if(param < GetNumParameters() && m_pParamInfo != nullptr)
{
MP_PARAMINFO mpi;
mpi.mpType = MPT_INT;
mpi.szUnitText[0] = 0;
mpi.szLabel[0] = 0;
if(m_pParamInfo->GetParamInfo(param, &mpi) == S_OK)
{
return mpt::ToCString(mpi.szUnitText);
}
}
return CString();
}
CString DMOPlugin::GetParamDisplay(PlugParamIndex param)
{
if(param < GetNumParameters() && m_pParamInfo != nullptr && m_pMediaParams != nullptr)
{
MP_PARAMINFO mpi;
mpi.mpType = MPT_INT;
mpi.szUnitText[0] = 0;
mpi.szLabel[0] = 0;
if (m_pParamInfo->GetParamInfo(param, &mpi) == S_OK)
{
MP_DATA md;
if(m_pMediaParams->GetParam(param, &md) == S_OK)
{
switch(mpi.mpType)
{
case MPT_FLOAT:
{
CString s;
s.Format(_T("%.2f"), md);
return s;
}
break;
case MPT_BOOL:
return ((int)md) ? _T("Yes") : _T("No");
break;
case MPT_ENUM:
{
WCHAR *text = nullptr;
m_pParamInfo->GetParamText(param, &text);
const int nValue = mpt::saturate_round<int>(md * (mpi.mpdMaxValue - mpi.mpdMinValue));
// Always skip first two strings (param name, unit name)
for(int i = 0; i < nValue + 2; i++)
{
text += wcslen(text) + 1;
}
return mpt::ToCString(text);
}
break;
case MPT_INT:
default:
{
CString s;
s.Format(_T("%d"), mpt::saturate_round<int>(md));
return s;
}
break;
}
}
}
}
return CString();
}
#endif // MODPLUG_TRACKER
#else // !MPT_WITH_DMO
MPT_MSVC_WORKAROUND_LNK4221(DMOPlugin)
#endif // MPT_WITH_DMO
OPENMPT_NAMESPACE_END
|