Files
voice_copy/voice-changer/apo/vccore.h
EJClaw 21b4278b77 voice-changer: one-click installer, mic APO instead of virtual cable, keep all files in install folder
- 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>
2026-11-11 16:33:15 +09:00

166 lines
6.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 마이크 APO 와 음성 변환기(app/mic_bridge.py)가 함께 쓰는 공유 메모리 규약과 APO 쪽 처리.
*
* APO 는 실제 마이크의 소리 처리 단계(audiodg.exe 안)에서 10ms 마다 불린다.
* - 변환기가 꺼져 있으면 아무것도 하지 않는다 (원래 목소리가 그대로 나감)
* - 변환기가 켜져 있고 이 마이크를 고르면: 원래 소리를 in 링에 쓰고, 변환기가 out 링에 써 준
* 변환된 소리를 내보낸다. 변환된 소리가 아직 없으면 무음 (원래 목소리가 새지 않게)
* 리눅스 테스트(tests/test_mic_bridge.py)가 이 파일을 그대로 컴파일해 파이썬 쪽과 맞춰 본다.
* 레이아웃을 바꾸면 VC_VERSION 과 mic_bridge.py 의 Header 를 같이 바꿀 것. */
#ifndef VCCORE_H
#define VCCORE_H
#include <stdint.h>
#include <string.h>
#define VC_MAGIC 0x31414356u /* "VCA1" */
#define VC_VERSION 1u
#define VC_RING 262144u /* 프레임 (48kHz 에서 5.4초) */
#define VC_HEADER 4096u
#define VC_SLOTS 8u
#define VC_NAME 128u
#define VC_SIZE (VC_HEADER + 2u * VC_RING * 4u)
typedef uint16_t vc_wchar; /* UTF-16 (Windows wchar_t) */
typedef struct {
vc_wchar name[VC_NAME];
volatile int64_t beat; /* 이 마이크의 APO 가 마지막으로 돈 시각 (ms) */
} vc_slot;
typedef struct {
uint32_t magic, version; /* 0 */
volatile int64_t app_beat; /* 8: 변환기가 살아 있다는 표시 (ms, GetTickCount64) */
volatile int64_t apo_beat; /* 16: 입력을 쓰는 APO 의 마지막 시각 */
volatile int64_t in_w; /* 24: in 링에 쓴 프레임 수 (누적) */
volatile int64_t out_w; /* 32: out 링에 쓴 프레임 수 (누적, 변환기가 씀) */
volatile uint32_t owner; /* 40: 입력을 쓰는 APO 인스턴스 (0 = 없음) */
volatile uint32_t rate; /* 44: 입력 샘플레이트 */
volatile uint32_t active; /* 48: 변환기가 켜져 있음 */
volatile uint32_t prefill_ms; /* 52: 변환된 소리를 이만큼 모은 뒤 내보냄 (끊김 방지 여유) */
volatile uint32_t underruns; /* 56: 변환된 소리가 모자랐던 횟수 */
uint32_t reserved; /* 60 */
vc_wchar target[VC_NAME]; /* 64: 변환할 마이크 이름 (변환기가 씀) */
vc_slot slots[VC_SLOTS]; /* 320: 지금 APO 가 붙어 도는 마이크들 (진단용) */
} vc_header;
typedef struct {
uint32_t id;
int primed; /* 변환된 소리를 내보내는 중 */
int64_t cursor; /* out 링에서 다음에 읽을 위치 */
int64_t first_seen; /* 다시 모으기 시작한 시각 (0 = 아직 소리 없음) */
int64_t slot_beat;
int was_alive;
vc_wchar name[VC_NAME];
} vc_inst;
#if defined(_MSC_VER) || defined(_WIN32)
#include <windows.h>
#define VC_CAS(p, n, o) ((uint32_t)InterlockedCompareExchange((volatile LONG *)(p), (LONG)(n), (LONG)(o)))
#define VC_FENCE() MemoryBarrier()
#else
#define VC_CAS(p, n, o) __sync_val_compare_and_swap((p), (o), (n))
#define VC_FENCE() __sync_synchronize()
#endif
static inline int vc_name_eq(const vc_wchar *a, const vc_wchar *b) {
for (unsigned i = 0; i < VC_NAME; i++) {
if (a[i] != b[i]) return 0;
if (a[i] == 0) return 1;
}
return 1;
}
static inline float *vc_in_ring(vc_header *s) { return (float *)((uint8_t *)s + VC_HEADER); }
static inline float *vc_out_ring(vc_header *s) { return (float *)((uint8_t *)s + VC_HEADER + VC_RING * 4u); }
static inline void vc_init_header(vc_header *s) {
if (s->magic != VC_MAGIC) {
s->version = VC_VERSION;
s->prefill_ms = 60;
VC_FENCE();
s->magic = VC_MAGIC;
}
}
static inline void vc_mark_slot(vc_inst *st, vc_header *s, int64_t now) {
if (now - st->slot_beat < 500) return;
st->slot_beat = now;
unsigned pick = VC_SLOTS;
for (unsigned i = 0; i < VC_SLOTS; i++)
if (vc_name_eq(s->slots[i].name, st->name)) { pick = i; break; }
if (pick == VC_SLOTS) {
int64_t oldest = INT64_MAX;
for (unsigned i = 0; i < VC_SLOTS; i++)
if (s->slots[i].beat < oldest) { oldest = s->slots[i].beat; pick = i; }
memcpy(s->slots[pick].name, st->name, sizeof(st->name));
}
s->slots[pick].beat = now;
}
/* io: 인터리브 float32 (frames × ch). 들어올 때 원래 마이크 소리, 나갈 때 내보낼 소리. */
static inline void vc_process(vc_inst *st, vc_header *s, float *io, uint32_t frames, uint32_t ch,
uint32_t rate, int64_t now) {
if (!s || s->magic != VC_MAGIC || s->version != VC_VERSION || ch == 0) return;
vc_mark_slot(st, s, now);
int alive = s->active && now - s->app_beat < 1500 && vc_name_eq(s->target, st->name);
if (!alive) {
if (s->owner == st->id) VC_CAS(&s->owner, 0u, st->id);
st->was_alive = 0;
return; /* 그대로 내보냄 */
}
if (!st->was_alive) { /* 막 켜짐: 지금 쓰인 곳부터 읽는다 */
st->was_alive = 1;
st->primed = 0;
st->first_seen = 0;
st->cursor = s->out_w;
}
uint32_t o = s->owner;
if (o == 0 || (o != st->id && now - s->apo_beat > 300)) VC_CAS(&s->owner, st->id, o);
if (s->owner == st->id) {
float *in = vc_in_ring(s);
int64_t w = s->in_w;
for (uint32_t i = 0; i < frames; i++) {
float m = 0;
for (uint32_t c = 0; c < ch; c++) m += io[i * ch + c];
in[(uint64_t)(w + i) % VC_RING] = m / (float)ch;
}
s->rate = rate;
VC_FENCE();
s->in_w = w + frames;
s->apo_beat = now;
}
if (s->rate != rate) { memset(io, 0, (size_t)frames * ch * 4); return; }
int64_t ow = s->out_w;
VC_FENCE();
int64_t avail = ow - st->cursor;
if (avail < 0 || avail > (int64_t)(VC_RING / 2)) { /* 너무 뒤처짐: 최신으로 */
st->cursor = ow;
st->primed = 0;
st->first_seen = 0;
avail = 0;
}
if (!st->primed) {
if (avail > 0 && st->first_seen == 0) st->first_seen = now;
if (st->first_seen == 0 || now - st->first_seen < (int64_t)s->prefill_ms) {
memset(io, 0, (size_t)frames * ch * 4);
return;
}
st->primed = 1;
}
const float *out = vc_out_ring(s);
uint32_t n = avail < (int64_t)frames ? (uint32_t)avail : frames;
for (uint32_t i = 0; i < n; i++) {
float v = out[(uint64_t)(st->cursor + i) % VC_RING];
for (uint32_t c = 0; c < ch; c++) io[i * ch + c] = v;
}
if (n < frames) {
memset(io + (size_t)n * ch, 0, (size_t)(frames - n) * ch * 4);
st->primed = 0;
st->first_seen = 0;
s->underruns++;
}
st->cursor += n;
}
#endif