- apo/: VoiceChangerAPO.dll (SFX APO on the real mic, built with mingw). When the app converts, the mic's own stream carries the converted voice; otherwise it passes the real voice through. Shared-memory protocol in vccore.h, Python side app/mic_bridge.py - scripts/mic-apo.ps1: register/unregister on every capture endpoint with admin value rights only (no ACL changes unless FxProperties is missing), originals backed up and restored - app: mic is the conversion target (WASAPI names), no output device, '마이크 다시 연결' button, 44.1k<->48k streaming resample, caches/temp redirected into <install>/cache - installer/VoiceChanger.iss (Inno Setup): choose install folder, register APO, download engine into that folder, uninstall restores mic settings and asks before deleting models - install.ps1: uv python --no-bin --no-registry, temp/caches inside the folder, uv cache clean - tests: C core compiled with gcc against the Python bridge; converter pump 44.1k round trip Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
458 lines
19 KiB
C++
458 lines
19 KiB
C++
/* VoiceChangerAPO.dll: 실제 마이크에 붙는 Windows 오디오 처리 모듈(SFX APO).
|
|
* 음성 변환기가 켜져 있으면 마이크 소리를 변환된 목소리로 바꿔 디스코드 · 게임에 보낸다.
|
|
* 가상 오디오 드라이버(VB-CABLE 등) 없이 쓰기 위한 것. 처리 내용은 vccore.h.
|
|
*
|
|
* 빌드 (리눅스): scripts/build-release.sh 가 아래처럼 빌드한다
|
|
* x86_64-w64-mingw32-g++ -O2 -shared -static -fno-exceptions -fno-rtti -o VoiceChangerAPO.dll \
|
|
* apo/vcapo.cpp apo/vcapo.def -lole32 -luuid -ladvapi32
|
|
* 등록 · 해제는 scripts/mic-apo.ps1 (설치 프로그램이 관리자 권한으로 실행). */
|
|
#include <windows.h>
|
|
#include <objbase.h>
|
|
#include <mmreg.h>
|
|
#include <propsys.h>
|
|
#include <sddl.h>
|
|
#include <new>
|
|
#include "vccore.h"
|
|
|
|
/* ---- audioenginebaseapo.h / audiomediatype.h 의 필요한 부분 (mingw 에 없음) ---- */
|
|
typedef LONGLONG HNSTIME;
|
|
enum { BUFFER_INVALID = 0, BUFFER_VALID = 1, BUFFER_SILENT = 2 };
|
|
/* FRAMESPERSECOND_MUST_MATCH | BITSPERSAMPLE_MUST_MATCH | INPLACE (Equalizer APO 와 같음) */
|
|
#define VC_APO_FLAGS 0x0000000d
|
|
#define APOERR_NOT_INITIALIZED ((HRESULT)0x887D0002L)
|
|
#define APOERR_FORMAT_NOT_SUPPORTED ((HRESULT)0x887D0003L)
|
|
#define APOERR_ALREADY_UNLOCKED ((HRESULT)0x887D0006L)
|
|
#define APOERR_NUM_CONNECTIONS_INVALID ((HRESULT)0x887D0007L)
|
|
#define APOERR_APO_LOCKED ((HRESULT)0x887D000AL)
|
|
|
|
struct UNCOMPRESSEDAUDIOFORMAT {
|
|
GUID guidFormatType;
|
|
DWORD dwSamplesPerFrame;
|
|
DWORD dwBytesPerSampleContainer;
|
|
DWORD dwValidBitsPerSample;
|
|
FLOAT fFramesPerSecond;
|
|
DWORD dwChannelMask;
|
|
};
|
|
|
|
struct IAudioMediaType : public IUnknown {
|
|
virtual HRESULT STDMETHODCALLTYPE IsCompressedFormat(BOOL *) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE IsEqual(IAudioMediaType *, DWORD *) = 0;
|
|
virtual const WAVEFORMATEX *STDMETHODCALLTYPE GetAudioFormat() = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE GetUncompressedAudioFormat(UNCOMPRESSEDAUDIOFORMAT *) = 0;
|
|
};
|
|
|
|
struct APO_CONNECTION_DESCRIPTOR {
|
|
UINT32 Type;
|
|
UINT_PTR pBuffer;
|
|
UINT32 u32MaxFrameCount;
|
|
IAudioMediaType *pFormat;
|
|
UINT32 u32Signature;
|
|
};
|
|
|
|
struct APO_CONNECTION_PROPERTY {
|
|
UINT_PTR pBuffer;
|
|
UINT32 u32ValidFrameCount;
|
|
UINT32 u32BufferFlags;
|
|
UINT32 u32Signature;
|
|
};
|
|
|
|
struct APO_REG_PROPERTIES {
|
|
CLSID clsid;
|
|
UINT32 Flags;
|
|
WCHAR szFriendlyName[256];
|
|
WCHAR szCopyrightInfo[256];
|
|
UINT32 u32MajorVersion;
|
|
UINT32 u32MinorVersion;
|
|
UINT32 u32MinInputConnections;
|
|
UINT32 u32MaxInputConnections;
|
|
UINT32 u32MinOutputConnections;
|
|
UINT32 u32MaxOutputConnections;
|
|
UINT32 u32MaxInstances;
|
|
UINT32 u32NumAPOInterfaces;
|
|
IID iidAPOInterfaceList[1];
|
|
};
|
|
|
|
struct APOInitBaseStruct {
|
|
UINT32 cbSize;
|
|
CLSID clsid;
|
|
};
|
|
|
|
struct APOInitSystemEffects {
|
|
APOInitBaseStruct APOInit;
|
|
IPropertyStore *pAPOEndpointProperties;
|
|
IPropertyStore *pAPOSystemEffectsProperties;
|
|
void *pReserved;
|
|
IUnknown *pDeviceCollection;
|
|
};
|
|
|
|
struct APOInitSystemEffects2 {
|
|
APOInitBaseStruct APOInit;
|
|
IPropertyStore *pAPOEndpointProperties;
|
|
IPropertyStore *pAPOSystemEffectsProperties;
|
|
void *pReserved;
|
|
IUnknown *pDeviceCollection;
|
|
UINT nSoftwareIoDeviceInCollection;
|
|
UINT nSoftwareIoConnectorIndex;
|
|
GUID AudioProcessingMode;
|
|
BOOL InitializeForDiscoveryOnly;
|
|
};
|
|
|
|
struct IAudioProcessingObject : public IUnknown {
|
|
virtual HRESULT STDMETHODCALLTYPE Reset() = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE GetLatency(HNSTIME *) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE GetRegistrationProperties(APO_REG_PROPERTIES **) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE Initialize(UINT32, BYTE *) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE IsInputFormatSupported(IAudioMediaType *, IAudioMediaType *, IAudioMediaType **) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE IsOutputFormatSupported(IAudioMediaType *, IAudioMediaType *, IAudioMediaType **) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE GetInputChannelCount(UINT32 *) = 0;
|
|
};
|
|
|
|
struct IAudioProcessingObjectRT : public IUnknown {
|
|
virtual void STDMETHODCALLTYPE APOProcess(UINT32, APO_CONNECTION_PROPERTY **, UINT32, APO_CONNECTION_PROPERTY **) = 0;
|
|
virtual UINT32 STDMETHODCALLTYPE CalcInputFrames(UINT32) = 0;
|
|
virtual UINT32 STDMETHODCALLTYPE CalcOutputFrames(UINT32) = 0;
|
|
};
|
|
|
|
struct IAudioProcessingObjectConfiguration : public IUnknown {
|
|
virtual HRESULT STDMETHODCALLTYPE LockForProcess(UINT32, APO_CONNECTION_DESCRIPTOR **, UINT32, APO_CONNECTION_DESCRIPTOR **) = 0;
|
|
virtual HRESULT STDMETHODCALLTYPE UnlockForProcess() = 0;
|
|
};
|
|
|
|
struct IAudioSystemEffects2 : public IUnknown {
|
|
virtual HRESULT STDMETHODCALLTYPE GetEffectsList(LPGUID *, UINT *, HANDLE) = 0;
|
|
};
|
|
|
|
static const IID IID_IAudioProcessingObject_ = {0xFD7F2B29, 0x24D0, 0x4b5c, {0xB1, 0x77, 0x59, 0x2C, 0x39, 0xF9, 0xCA, 0x10}};
|
|
static const IID IID_IAudioProcessingObjectRT_ = {0x9E1D6A6D, 0xDDBC, 0x4E95, {0xA4, 0xC7, 0xAD, 0x64, 0xBA, 0x37, 0x84, 0x6C}};
|
|
static const IID IID_IAudioProcessingObjectConfiguration_ = {0x0E5ED805, 0xABA6, 0x49c3, {0x8F, 0x9A, 0x2B, 0x8C, 0x88, 0x9C, 0x4F, 0xA8}};
|
|
static const IID IID_IAudioSystemEffects_ = {0x5FA00F27, 0xADD6, 0x499a, {0x8A, 0x9D, 0x6B, 0x98, 0x52, 0x1F, 0xA7, 0x5B}};
|
|
static const IID IID_IAudioSystemEffects2_ = {0xBAFE99D2, 0x7436, 0x44CE, {0x9E, 0x0E, 0x4D, 0x89, 0xAF, 0xBF, 0xFF, 0x56}};
|
|
static const GUID SUBTYPE_IEEE_FLOAT = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
|
|
static const PROPERTYKEY PKEY_FriendlyName_ = {{0xa45c254e, 0xdf1c, 0x4efd, {0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0}}, 14};
|
|
|
|
/* 이 APO 의 CLSID. scripts/mic-apo.ps1 의 $Clsid 와 같아야 한다. */
|
|
static const CLSID CLSID_VoiceChangerAPO = {0x6f1c5e2a, 0x3b7d, 0x4c41, {0x9a, 0x0e, 0x5d, 0x2b, 0x8f, 0x61, 0xc4, 0x37}};
|
|
static const wchar_t SHM_NAME[] = L"Global\\VoiceChangerAPO";
|
|
|
|
static LONG g_objects = 0, g_locks = 0;
|
|
|
|
#ifdef VC_DEBUG /* 진단용 빌드: audiodg 의 임시 폴더(LocalService)에 vcapo.log 를 남긴다 */
|
|
#include <stdio.h>
|
|
static void dlog(const char *fmt, ...) {
|
|
wchar_t dir[MAX_PATH], path[MAX_PATH];
|
|
if (!GetTempPathW(MAX_PATH, dir)) return;
|
|
_snwprintf(path, MAX_PATH, L"%lsvcapo.log", dir);
|
|
FILE *f = _wfopen(path, L"a");
|
|
if (!f) return;
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
fprintf(f, "%llu pid=%lu ", (unsigned long long)GetTickCount64(), GetCurrentProcessId());
|
|
vfprintf(f, fmt, ap);
|
|
fputc('\n', f);
|
|
va_end(ap);
|
|
fclose(f);
|
|
}
|
|
#else
|
|
#define dlog(...) ((void)0)
|
|
#endif
|
|
static volatile LONG g_next_id = 0;
|
|
|
|
static bool is_float32(IAudioMediaType *t, UNCOMPRESSEDAUDIOFORMAT *f) {
|
|
if (!t) return false;
|
|
BOOL compressed = TRUE;
|
|
if (FAILED(t->IsCompressedFormat(&compressed)) || compressed) return false;
|
|
if (FAILED(t->GetUncompressedAudioFormat(f))) return false;
|
|
return IsEqualGUID(f->guidFormatType, SUBTYPE_IEEE_FLOAT) && f->dwBytesPerSampleContainer == 4 &&
|
|
f->dwSamplesPerFrame > 0;
|
|
}
|
|
|
|
static bool same_shape(IAudioMediaType *a, IAudioMediaType *b) {
|
|
UNCOMPRESSEDAUDIOFORMAT fa, fb;
|
|
if (!is_float32(a, &fa) || !is_float32(b, &fb)) return false;
|
|
return fa.dwSamplesPerFrame == fb.dwSamplesPerFrame && fa.fFramesPerSecond == fb.fFramesPerSecond;
|
|
}
|
|
|
|
struct INonDelegatingUnknown {
|
|
virtual HRESULT STDMETHODCALLTYPE NDQueryInterface(REFIID, void **) = 0;
|
|
virtual ULONG STDMETHODCALLTYPE NDAddRef() = 0;
|
|
virtual ULONG STDMETHODCALLTYPE NDRelease() = 0;
|
|
};
|
|
|
|
class VoiceChangerAPO : public INonDelegatingUnknown,
|
|
public IAudioProcessingObject,
|
|
public IAudioProcessingObjectRT,
|
|
public IAudioProcessingObjectConfiguration,
|
|
public IAudioSystemEffects2 {
|
|
LONG ref_ = 1;
|
|
IUnknown *outer_;
|
|
bool init_ = false, locked_ = false, discovery_ = false;
|
|
UINT32 ch_ = 0, rate_ = 0;
|
|
HANDLE map_ = NULL;
|
|
vc_header *shm_ = NULL;
|
|
vc_inst st_;
|
|
|
|
void open_shm() {
|
|
if (shm_) return;
|
|
/* audiodg(LocalService) 가 만들고, 일반 권한의 음성 변환기가 연다 */
|
|
SECURITY_ATTRIBUTES sa = {sizeof(sa), NULL, FALSE};
|
|
PSECURITY_DESCRIPTOR sd = NULL;
|
|
if (ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
|
L"D:(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;LS)(A;;GA;;;IU)(A;;GA;;;AU)S:(ML;;NW;;;LW)", SDDL_REVISION_1, &sd,
|
|
NULL))
|
|
sa.lpSecurityDescriptor = sd;
|
|
map_ = CreateFileMappingW(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, VC_SIZE, SHM_NAME);
|
|
dlog("open_shm map=%p err=%lu sd=%p", map_, GetLastError(), sd);
|
|
if (sd) LocalFree(sd);
|
|
if (!map_) return;
|
|
shm_ = (vc_header *)MapViewOfFile(map_, FILE_MAP_ALL_ACCESS, 0, 0, VC_SIZE);
|
|
if (!shm_) {
|
|
CloseHandle(map_);
|
|
map_ = NULL;
|
|
return;
|
|
}
|
|
vc_init_header(shm_);
|
|
}
|
|
|
|
void close_shm() {
|
|
if (shm_) UnmapViewOfFile(shm_);
|
|
if (map_) CloseHandle(map_);
|
|
shm_ = NULL;
|
|
map_ = NULL;
|
|
}
|
|
|
|
public:
|
|
explicit VoiceChangerAPO(IUnknown *outer) {
|
|
outer_ = outer ? outer : reinterpret_cast<IUnknown *>(static_cast<INonDelegatingUnknown *>(this));
|
|
InterlockedIncrement(&g_objects);
|
|
memset(&st_, 0, sizeof(st_));
|
|
st_.id = (uint32_t)InterlockedIncrement(&g_next_id) | ((GetCurrentProcessId() & 0xffff) << 16);
|
|
if (st_.id == 0) st_.id = 1;
|
|
}
|
|
virtual ~VoiceChangerAPO() {
|
|
close_shm();
|
|
InterlockedDecrement(&g_objects);
|
|
}
|
|
|
|
/* 오디오 엔진은 APO 를 COM 집합(aggregation)으로 만든다. 바깥 객체가 있으면 IUnknown 호출은 그쪽으로 넘기고,
|
|
자기 수명은 INonDelegatingUnknown 으로 따로 센다 (Windows SDK 의 CBaseAudioProcessingObject 와 같은 방식). */
|
|
STDMETHODIMP NDQueryInterface(REFIID riid, void **ppv) override {
|
|
if (!ppv) return E_POINTER;
|
|
*ppv = NULL;
|
|
if (IsEqualIID(riid, IID_IUnknown)) {
|
|
*ppv = static_cast<INonDelegatingUnknown *>(this);
|
|
NDAddRef();
|
|
return S_OK;
|
|
}
|
|
if (IsEqualIID(riid, IID_IAudioProcessingObject_))
|
|
*ppv = static_cast<IAudioProcessingObject *>(this);
|
|
else if (IsEqualIID(riid, IID_IAudioProcessingObjectRT_))
|
|
*ppv = static_cast<IAudioProcessingObjectRT *>(this);
|
|
else if (IsEqualIID(riid, IID_IAudioProcessingObjectConfiguration_))
|
|
*ppv = static_cast<IAudioProcessingObjectConfiguration *>(this);
|
|
else if (IsEqualIID(riid, IID_IAudioSystemEffects_) || IsEqualIID(riid, IID_IAudioSystemEffects2_))
|
|
*ppv = static_cast<IAudioSystemEffects2 *>(this);
|
|
else {
|
|
dlog("QI miss %08lx", riid.Data1);
|
|
return E_NOINTERFACE;
|
|
}
|
|
reinterpret_cast<IUnknown *>(*ppv)->AddRef();
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP_(ULONG) NDAddRef() override { return (ULONG)InterlockedIncrement(&ref_); }
|
|
STDMETHODIMP_(ULONG) NDRelease() override {
|
|
LONG r = InterlockedDecrement(&ref_);
|
|
if (r == 0) delete this;
|
|
return (ULONG)r;
|
|
}
|
|
STDMETHODIMP QueryInterface(REFIID riid, void **ppv) override { return outer_->QueryInterface(riid, ppv); }
|
|
STDMETHODIMP_(ULONG) AddRef() override { return outer_->AddRef(); }
|
|
STDMETHODIMP_(ULONG) Release() override { return outer_->Release(); }
|
|
|
|
/* IAudioProcessingObject */
|
|
STDMETHODIMP Reset() override {
|
|
st_.primed = 0;
|
|
st_.first_seen = 0;
|
|
st_.was_alive = 0;
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP GetLatency(HNSTIME *t) override {
|
|
if (!t) return E_POINTER;
|
|
*t = 0;
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP GetRegistrationProperties(APO_REG_PROPERTIES **pp) override {
|
|
if (!pp) return E_POINTER;
|
|
APO_REG_PROPERTIES *p = (APO_REG_PROPERTIES *)CoTaskMemAlloc(sizeof(APO_REG_PROPERTIES));
|
|
if (!p) return E_OUTOFMEMORY;
|
|
memset(p, 0, sizeof(*p));
|
|
p->clsid = CLSID_VoiceChangerAPO;
|
|
p->Flags = VC_APO_FLAGS;
|
|
lstrcpynW(p->szFriendlyName, L"Voice Changer", 256);
|
|
lstrcpynW(p->szCopyrightInfo, L"voice_copy (MIT)", 256);
|
|
p->u32MajorVersion = 1;
|
|
p->u32MinorVersion = 0;
|
|
p->u32MinInputConnections = p->u32MaxInputConnections = 1;
|
|
p->u32MinOutputConnections = p->u32MaxOutputConnections = 1;
|
|
p->u32MaxInstances = 0xffffffff;
|
|
p->u32NumAPOInterfaces = 1;
|
|
p->iidAPOInterfaceList[0] = IID_IAudioProcessingObject_;
|
|
*pp = p;
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP Initialize(UINT32 cb, BYTE *data) override {
|
|
if (init_) return S_OK;
|
|
if (data && cb >= sizeof(APOInitSystemEffects)) {
|
|
APOInitSystemEffects *ini = (APOInitSystemEffects *)data;
|
|
if (cb >= sizeof(APOInitSystemEffects2)) discovery_ = ((APOInitSystemEffects2 *)data)->InitializeForDiscoveryOnly != FALSE;
|
|
if (ini->pAPOEndpointProperties) {
|
|
PROPVARIANT v;
|
|
PropVariantInit(&v);
|
|
if (SUCCEEDED(ini->pAPOEndpointProperties->GetValue(PKEY_FriendlyName_, &v)) && v.vt == VT_LPWSTR && v.pwszVal) {
|
|
unsigned i = 0;
|
|
for (; i + 1 < VC_NAME && v.pwszVal[i]; i++) st_.name[i] = (vc_wchar)v.pwszVal[i];
|
|
st_.name[i] = 0;
|
|
}
|
|
PropVariantClear(&v);
|
|
}
|
|
}
|
|
init_ = true;
|
|
dlog("Initialize cb=%u discovery=%d name0=%04x", cb, (int)discovery_, st_.name[0]);
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP IsInputFormatSupported(IAudioMediaType *opposite, IAudioMediaType *req, IAudioMediaType **out) override {
|
|
return check_format(opposite, req, out);
|
|
}
|
|
STDMETHODIMP IsOutputFormatSupported(IAudioMediaType *opposite, IAudioMediaType *req, IAudioMediaType **out) override {
|
|
return check_format(opposite, req, out);
|
|
}
|
|
HRESULT check_format(IAudioMediaType *opposite, IAudioMediaType *req, IAudioMediaType **out) {
|
|
if (!req || !out) return E_POINTER;
|
|
*out = NULL;
|
|
UNCOMPRESSEDAUDIOFORMAT f;
|
|
{
|
|
UNCOMPRESSEDAUDIOFORMAT r = {};
|
|
BOOL c = 0;
|
|
req->IsCompressedFormat(&c);
|
|
req->GetUncompressedAudioFormat(&r);
|
|
dlog("check_format opp=%p comp=%d bytes=%lu bits=%lu ch=%lu rate=%.0f type=%08lx", opposite, c, r.dwBytesPerSampleContainer,
|
|
r.dwValidBitsPerSample, r.dwSamplesPerFrame, r.fFramesPerSecond, r.guidFormatType.Data1);
|
|
}
|
|
if (is_float32(req, &f) && (!opposite || same_shape(opposite, req))) {
|
|
req->AddRef();
|
|
*out = req;
|
|
return S_OK;
|
|
}
|
|
if (opposite && is_float32(opposite, &f)) { /* 반대쪽과 같은 형식을 제안 */
|
|
opposite->AddRef();
|
|
*out = opposite;
|
|
return S_FALSE;
|
|
}
|
|
return APOERR_FORMAT_NOT_SUPPORTED;
|
|
}
|
|
STDMETHODIMP GetInputChannelCount(UINT32 *n) override {
|
|
if (!n) return E_POINTER;
|
|
if (!locked_) return APOERR_NOT_INITIALIZED;
|
|
*n = ch_;
|
|
return S_OK;
|
|
}
|
|
|
|
/* IAudioProcessingObjectConfiguration */
|
|
STDMETHODIMP LockForProcess(UINT32 nin, APO_CONNECTION_DESCRIPTOR **in, UINT32 nout, APO_CONNECTION_DESCRIPTOR **out) override {
|
|
dlog("LockForProcess nin=%u nout=%u", nin, nout);
|
|
if (locked_) return APOERR_APO_LOCKED;
|
|
if (nin != 1 || nout != 1 || !in || !out || !in[0] || !out[0]) return APOERR_NUM_CONNECTIONS_INVALID;
|
|
UNCOMPRESSEDAUDIOFORMAT fi, fo;
|
|
if (!is_float32(in[0]->pFormat, &fi) || !is_float32(out[0]->pFormat, &fo) ||
|
|
fi.dwSamplesPerFrame != fo.dwSamplesPerFrame || fi.fFramesPerSecond != fo.fFramesPerSecond)
|
|
return APOERR_FORMAT_NOT_SUPPORTED;
|
|
dlog("format ch=%lu rate=%.0f", fi.dwSamplesPerFrame, fi.fFramesPerSecond);
|
|
ch_ = fi.dwSamplesPerFrame;
|
|
rate_ = (UINT32)(fi.fFramesPerSecond + 0.5f);
|
|
if (!discovery_) open_shm();
|
|
Reset();
|
|
locked_ = true;
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP UnlockForProcess() override {
|
|
if (!locked_) return APOERR_ALREADY_UNLOCKED;
|
|
if (shm_ && shm_->owner == st_.id) VC_CAS(&shm_->owner, 0u, st_.id);
|
|
close_shm();
|
|
locked_ = false;
|
|
return S_OK;
|
|
}
|
|
|
|
/* IAudioProcessingObjectRT: 실시간 스레드. 메모리 할당 · 잠금 금지 */
|
|
STDMETHODIMP_(void) APOProcess(UINT32 nin, APO_CONNECTION_PROPERTY **in, UINT32 nout, APO_CONNECTION_PROPERTY **out) override {
|
|
if (nin < 1 || nout < 1) return;
|
|
#ifdef VC_DEBUG
|
|
static volatile LONG calls = 0;
|
|
if (InterlockedIncrement(&calls) % 500 == 1) dlog("APOProcess frames=%u flags=%u shm=%p", in[0]->u32ValidFrameCount, in[0]->u32BufferFlags, shm_);
|
|
#endif
|
|
APO_CONNECTION_PROPERTY *i = in[0], *o = out[0];
|
|
UINT32 frames = i->u32ValidFrameCount;
|
|
float *dst = (float *)o->pBuffer;
|
|
const float *src = (const float *)i->pBuffer;
|
|
size_t bytes = (size_t)frames * ch_ * 4;
|
|
if (i->u32BufferFlags == BUFFER_SILENT)
|
|
memset(dst, 0, bytes);
|
|
else if (dst != src)
|
|
memcpy(dst, src, bytes);
|
|
vc_process(&st_, shm_, dst, frames, ch_, rate_, (int64_t)GetTickCount64());
|
|
o->u32ValidFrameCount = frames;
|
|
o->u32BufferFlags = BUFFER_VALID;
|
|
}
|
|
STDMETHODIMP_(UINT32) CalcInputFrames(UINT32 n) override { return n; }
|
|
STDMETHODIMP_(UINT32) CalcOutputFrames(UINT32 n) override { return n; }
|
|
|
|
/* IAudioSystemEffects2: 윈도우 설정 화면에 보여 줄 효과 목록은 없음 */
|
|
STDMETHODIMP GetEffectsList(LPGUID *ids, UINT *count, HANDLE) override {
|
|
if (!ids || !count) return E_POINTER;
|
|
*ids = NULL;
|
|
*count = 0;
|
|
return S_OK;
|
|
}
|
|
};
|
|
|
|
class Factory : public IClassFactory {
|
|
public:
|
|
STDMETHODIMP QueryInterface(REFIID riid, void **ppv) override {
|
|
if (!ppv) return E_POINTER;
|
|
if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory)) {
|
|
*ppv = static_cast<IClassFactory *>(this);
|
|
return S_OK;
|
|
}
|
|
*ppv = NULL;
|
|
return E_NOINTERFACE;
|
|
}
|
|
STDMETHODIMP_(ULONG) AddRef() override { return 2; }
|
|
STDMETHODIMP_(ULONG) Release() override { return 1; }
|
|
STDMETHODIMP CreateInstance(IUnknown *outer, REFIID riid, void **ppv) override {
|
|
if (!ppv) return E_POINTER;
|
|
*ppv = NULL;
|
|
dlog("CreateInstance outer=%p riid=%08lx", outer, riid.Data1);
|
|
if (outer && !IsEqualIID(riid, IID_IUnknown)) return CLASS_E_NOAGGREGATION;
|
|
VoiceChangerAPO *a = new (std::nothrow) VoiceChangerAPO(outer);
|
|
if (!a) return E_OUTOFMEMORY;
|
|
HRESULT hr = a->NDQueryInterface(riid, ppv);
|
|
a->NDRelease();
|
|
return hr;
|
|
}
|
|
STDMETHODIMP LockServer(BOOL lock) override {
|
|
if (lock)
|
|
InterlockedIncrement(&g_locks);
|
|
else
|
|
InterlockedDecrement(&g_locks);
|
|
return S_OK;
|
|
}
|
|
};
|
|
|
|
static Factory g_factory;
|
|
|
|
extern "C" HRESULT STDAPICALLTYPE DllGetClassObject(REFCLSID clsid, REFIID riid, void **ppv) {
|
|
dlog("DllGetClassObject clsid=%08lx", clsid.Data1);
|
|
if (!IsEqualCLSID(clsid, CLSID_VoiceChangerAPO)) return CLASS_E_CLASSNOTAVAILABLE;
|
|
return g_factory.QueryInterface(riid, ppv);
|
|
}
|
|
|
|
extern "C" HRESULT STDAPICALLTYPE DllCanUnloadNow() { return (g_objects == 0 && g_locks == 0) ? S_OK : S_FALSE; }
|