Conflicts resolved: hle_audio.c keeps the AAudio downmix on Android and takes upstream's 5.1 downmix on Apple; hle_nvn.c keeps mtl_queue_flush / ADPF pacing on Android. Android translates synchronously on the submit thread for now (the Vulkan backend's fence also counts readback commits); Vulkan gets no-op mtl_host_latch / mtl_host_apply; compat adds pthread_attr_set_qos_class_np. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
427 lines
35 KiB
C
427 lines
35 KiB
C
// nn::audio AudioOut:游戏(FMOD nx 输出层)按块追加 PCM 缓冲,播完由系统放回「已释放」队列并触发事件。
|
||
// 宿主:真出声 = CoreAudio AudioUnit(Mac DefaultOutput / iOS RemoteIO);静音接收端 = 按真实时间消耗缓冲(不出声,节奏与真机一致)。
|
||
// iOS 缺省真出声(产品必须有声音),测试要静音显式 DM_AUDIO=0;Mac 命令行宿主缺省静音接收端(回放 / 闸门不出声),DM_AUDIO=1 真出声;
|
||
// Mac App(app_mac.m)是产品,启动时设 DM_AUDIO=1(「没有声音」:之前 App 也走了命令行的缺省,从来不出声)。
|
||
// 声卡打不开(没有输出设备等)不再退出:记红,这一路改由静音接收端按实时消耗,游戏照常跑。
|
||
// AudioOut / AudioOutBuffer / AudioOutParameter 的内容只经下列接口读写,宿主自定内部布局,只写结构体前部。
|
||
// nn::audio AudioOut: the game (FMOD's nx output layer) appends PCM buffers in blocks; once played the system puts them back on the "released" queue and signals an event.
|
||
// Host: real sound = CoreAudio AudioUnit (Mac DefaultOutput / iOS RemoteIO); silent sink = buffers consumed in real time (no sound, same pacing as the device).
|
||
// iOS plays real sound by default (the product must have sound), tests set DM_AUDIO=0 explicitly for silence; the Mac command-line host defaults to the silent sink (replays / gates are silent), DM_AUDIO=1 for real sound;
|
||
// the Mac App (app_mac.m) is the product and sets DM_AUDIO=1 at launch ("no sound": the App used to take the command-line default and never played sound).
|
||
// If the sound card cannot be opened (no output device etc.) it no longer exits: logs red, and this channel is consumed in real time by the silent sink while the game runs on.
|
||
// The contents of AudioOut / AudioOutBuffer / AudioOutParameter are read and written only through the interfaces below; the host defines its own internal layout and writes only the front part of each struct.
|
||
#include "hle.h"
|
||
#if defined(__APPLE__)
|
||
#include <AudioToolbox/AudioToolbox.h>
|
||
#elif defined(__ANDROID__)
|
||
// Android:出声端 = AAudio 流(数据回调拉同一条待播队列);句柄借 AudioUnit 这个名字,开 / 起 / 停 / 关见下方 au_open 一节
|
||
// Android: the output = an AAudio stream (its data callback pulls the same pending queue); the handle borrows the AudioUnit name, open / start / stop / close are in the au_open section below
|
||
#include <aaudio/AAudio.h>
|
||
typedef AAudioStream *AudioUnit;
|
||
typedef int OSStatus;
|
||
static OSStatus AudioOutputUnitStart(AudioUnit au);
|
||
static OSStatus AudioOutputUnitStop(AudioUnit au);
|
||
#define AudioUnitUninitialize(au) 0
|
||
#define AudioComponentInstanceDispose(au) AAudioStream_close(au)
|
||
#else
|
||
// Linux:真出声后端没接,只有静音接收端;AudioUnit 句柄恒空,下列调用不会走到
|
||
// Linux: no real audio backend, only the silent sink; the AudioUnit handle is always null and the calls below are never reached
|
||
typedef void *AudioUnit;
|
||
typedef int OSStatus;
|
||
#define AudioOutputUnitStart(au) 0
|
||
#define AudioOutputUnitStop(au) 0
|
||
#define AudioUnitUninitialize(au) 0
|
||
#define AudioComponentInstanceDispose(au) 0
|
||
#endif
|
||
#include <TargetConditionals.h>
|
||
#include <dispatch/dispatch.h>
|
||
#include <stdatomic.h>
|
||
#include <unistd.h>
|
||
|
||
enum { FMT_PCM16 = 2, STATE_STARTED = 0, STATE_STOPPED = 1, RATE = 48000 };
|
||
|
||
typedef struct { void *ptr; uint64_t cap, size; } gbuf; // 写进客体 AudioOutBuffer 的前 24 字节 | The first 24 bytes written into the guest AudioOutBuffer
|
||
#define QMAX 64
|
||
#define RELMAX QMAX // 已释放最多 = 在途缓冲数(游戏收回缓冲才能再追加),与待播同上限 | Released max = number of in-flight buffers (the game must take a buffer back before it can append again), same limit as pending
|
||
typedef struct {
|
||
pthread_mutex_t m;
|
||
void *event; // 客体 SystemEvent:有缓冲被释放就触发 | Guest SystemEvent: signaled whenever a buffer is released
|
||
int ch;
|
||
_Atomic int state;
|
||
gbuf *q[QMAX]; _Atomic uint32_t q_head, q_tail; // 待播:游戏侧写,出声端读 | Pending: written by the game side, read by the output side
|
||
gbuf *rel[RELMAX]; _Atomic uint32_t rel_head, rel_tail; // 已释放:出声端写,游戏侧读 | Released: written by the output side, read by the game side
|
||
uint64_t pos; // 当前块已播字节 | Bytes already played of the current block
|
||
AudioUnit au;
|
||
#if defined(__ANDROID__)
|
||
int out_ch; // 流的声道数:设备收原声道数就等于 ch,否则 2(回调里自己下混) | the stream's channel count: equal to ch when the device takes the original count, otherwise 2 (downmixed in the callback)
|
||
_Atomic int lost; // 设备断开(换耳机 / 蓝牙):等普通线程重开 | device disconnected (headphones / Bluetooth switch): waiting for an ordinary thread to reopen
|
||
#else
|
||
int mix; // 1 = 游戏给 5.1(6 声道),交给 CoreAudio 前我们自己下混成双声道(au_render) | 1 = the game gives 5.1 (6 channels), downmixed to stereo by us before handing to CoreAudio (au_render)
|
||
#endif
|
||
int16_t *mixbuf; // 下混用的暂存(ch 声道) | staging for the downmix (ch channels)
|
||
pthread_t null_th; _Atomic int null_run;
|
||
dispatch_semaphore_t notify_sem;
|
||
pthread_t notify_th; _Atomic int notify_run, notify_pending, notify_armed;
|
||
} aout;
|
||
|
||
// 打开中的输出登记表:前端生命周期(切后台)一次性暂停 / 恢复
|
||
// Registry of open outputs: suspended / resumed all at once by the front-end lifecycle (going to background)
|
||
#define AOMAX 8
|
||
static aout *g_aos[AOMAX];
|
||
static pthread_mutex_t g_aos_lock = PTHREAD_MUTEX_INITIALIZER;
|
||
static int g_suspended;
|
||
static aout *ao(const void *g) { aout *a = objmap_get(g); if (!a) die("AudioOut %p 不是宿主建的", g); return a; }
|
||
|
||
static void notify_release(aout *a) {
|
||
atomic_store_explicit(&a->notify_pending, 1, memory_order_release);
|
||
if (atomic_exchange_explicit(&a->notify_armed, 1, memory_order_acq_rel) == 0)
|
||
dispatch_semaphore_signal(a->notify_sem);
|
||
}
|
||
|
||
static void *event_notify(void *p) {
|
||
aout *a = p;
|
||
for (;;) {
|
||
dispatch_semaphore_wait(a->notify_sem, DISPATCH_TIME_FOREVER);
|
||
if (atomic_exchange_explicit(&a->notify_pending, 0, memory_order_acq_rel) && a->event)
|
||
hle_signal_system_event(a->event);
|
||
atomic_store_explicit(&a->notify_armed, 0, memory_order_release);
|
||
if (!atomic_load_explicit(&a->notify_run, memory_order_acquire)) break;
|
||
if (atomic_load_explicit(&a->notify_pending, memory_order_acquire) &&
|
||
atomic_exchange_explicit(&a->notify_armed, 1, memory_order_acq_rel) == 0)
|
||
dispatch_semaphore_signal(a->notify_sem);
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
// 从待播队列取 n 字节交给出声端;整块播完就挪到已释放并通知普通线程。
|
||
// Take n bytes from the pending queue and hand them to the output side; once a whole block has played, move it to released and notify the ordinary thread.
|
||
static void pull(aout *a, uint8_t *dst, uint64_t n) {
|
||
int released = 0;
|
||
uint32_t qh = atomic_load_explicit(&a->q_head, memory_order_relaxed);
|
||
while (n) {
|
||
uint32_t qt = atomic_load_explicit(&a->q_tail, memory_order_acquire);
|
||
if (qh == qt || atomic_load_explicit(&a->state, memory_order_acquire) != STATE_STARTED) { memset(dst, 0, n); break; }
|
||
gbuf *b = a->q[qh & (QMAX - 1)];
|
||
uint64_t k = b->size - a->pos; if (k > n) k = n;
|
||
memcpy(dst, (uint8_t *)b->ptr + a->pos, k);
|
||
dst += k; n -= k; a->pos += k;
|
||
if (a->pos >= b->size) {
|
||
uint32_t rw = atomic_load_explicit(&a->rel_tail, memory_order_relaxed);
|
||
uint32_t rr = atomic_load_explicit(&a->rel_head, memory_order_acquire);
|
||
if (rw - rr == RELMAX) die("AudioOut 已释放队列溢出");
|
||
a->rel[rw & (RELMAX - 1)] = b;
|
||
atomic_store_explicit(&a->rel_tail, rw + 1, memory_order_release);
|
||
qh++;
|
||
atomic_store_explicit(&a->q_head, qh, memory_order_release);
|
||
a->pos = 0; released = 1;
|
||
}
|
||
}
|
||
if (released) notify_release(a);
|
||
}
|
||
|
||
static _Atomic uint64_t g_render_calls; // 声卡回调累计(生命周期验证:恢复后须继续增长) | Cumulative sound card callbacks (lifecycle verification: must keep growing after resume)
|
||
uint64_t dm_audio_render_calls(void) { return atomic_load_explicit(&g_render_calls, memory_order_relaxed); }
|
||
#if defined(__APPLE__)
|
||
// 5.1 → 双声道(公开 的:原先把游戏的 6 声道原样交给 AudioUnit、不设声道布局,系统隐式转换只取前两路,中置——NPC 对白——整路丢掉)。
|
||
// 游戏的声道顺序(Mac App 走到阿卡拉跟前、对白开口那半秒按声道量能量实测:只有第 3 路跳起来):左前、右前、中置、低音、左后、右后。
|
||
// 系数照 ITU-R BS.775 的 Lo/Ro:L = 左前 + 0.707·中置 + 0.707·左后,R 对称;低音不并入(标准下混不含 LFE,并进去会让低频发闷);
|
||
// 整体再乘 0.707 留 3 dB 余量,仍超出 16 位的饱和截断。Q15 定点:0.707 = 23170,0.707 × 0.707 = 16384
|
||
// 5.1 -> stereo (previously: the game's 6 channels were handed to the AudioUnit as is without a channel layout; the system's implicit conversion took only the first two, and the centre - NPC dialogue - was dropped entirely).
|
||
// The game's channel order (measured in the Mac App by per-channel energy during the half second Akara starts speaking: only channel 3 jumps): front left, front right, centre, LFE, rear left, rear right.
|
||
// Coefficients from the ITU-R BS.775 Lo/Ro downmix: L = front left + 0.707*centre + 0.707*rear left, R symmetric; LFE is not mixed in (the standard downmix excludes LFE, and mixing it in muddies the low end);
|
||
// the whole is multiplied by 0.707 again for 3 dB of headroom, and anything still beyond 16 bits is saturated. Q15 fixed point: 0.707 = 23170, 0.707 x 0.707 = 16384
|
||
#define MIXMAX 4096
|
||
static void downmix51(const int16_t *in, int16_t *out, unsigned frames) {
|
||
for (unsigned i = 0; i < frames; i++, in += 6, out += 2) {
|
||
int32_t l = (23170 * in[0] + 16384 * in[2] + 16384 * in[4]) >> 15, r = (23170 * in[1] + 16384 * in[2] + 16384 * in[5]) >> 15;
|
||
out[0] = (int16_t)(l > 32767 ? 32767 : l < -32768 ? -32768 : l); out[1] = (int16_t)(r > 32767 ? 32767 : r < -32768 ? -32768 : r); }
|
||
}
|
||
static OSStatus au_render(void *ctx, AudioUnitRenderActionFlags *f, const AudioTimeStamp *ts, UInt32 bus, UInt32 frames, AudioBufferList *bl) {
|
||
(void)f; (void)ts; (void)bus; (void)frames;
|
||
aout *a = ctx;
|
||
atomic_fetch_add_explicit(&g_render_calls, 1, memory_order_relaxed);
|
||
if (!a->mix) pull(a, bl->mBuffers[0].mData, bl->mBuffers[0].mDataByteSize);
|
||
else { int16_t *o = bl->mBuffers[0].mData; unsigned left = bl->mBuffers[0].mDataByteSize / 4; // 双声道 16 位:每帧 4 字节 | stereo 16-bit: 4 bytes per frame
|
||
while (left) { unsigned k = left < MIXMAX ? left : MIXMAX; pull(a, (uint8_t *)a->mixbuf, (uint64_t)k * 12); downmix51(a->mixbuf, o, k); o += 2 * k; left -= k; } }
|
||
// 诊断开关 DM_AUDIO_OUT_DUMP=<文件>:交给 CoreAudio 的那份数据原样续写(下混后是双声道)。本机没有回环设备录不到系统输出,判「对白有没有交出去」看这份
|
||
// Diagnostic switch DM_AUDIO_OUT_DUMP=<file>: append the data handed to CoreAudio as is (stereo after the downmix). This machine has no loopback device and cannot record the system output; to tell whether the dialogue was handed over, look at this file
|
||
{ static FILE *df; static int on = -1; if (on < 0) { const char *e = getenv("DM_AUDIO_OUT_DUMP"); on = e != NULL; if (on && !(df = fopen(e, "wb"))) LOG("【红】DM_AUDIO_OUT_DUMP 打不开 %s", e); }
|
||
if (df) fwrite(bl->mBuffers[0].mData, 1, bl->mBuffers[0].mDataByteSize, df); }
|
||
return noErr;
|
||
}
|
||
#if !TARGET_OS_IPHONE
|
||
static void dev_name(AudioObjectID id, char *o, size_t n) { // 输出设备名(日志用) | output device name (for the log)
|
||
CFStringRef nm = NULL; UInt32 sz = sizeof nm; AudioObjectPropertyAddress pa = { kAudioObjectPropertyName, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain };
|
||
o[0] = 0; if (!AudioObjectGetPropertyData(id, &pa, 0, NULL, &sz, &nm) && nm) { CFStringGetCString(nm, o, (CFIndex)n, kCFStringEncodingUTF8); CFRelease(nm); } }
|
||
#endif
|
||
static int au_open(aout *a) { // 0 = 声卡开好了;-1 = 开不了(a->au 置空,调用方走静音接收端) | 0 = the sound card is open; -1 = it cannot be opened (a->au set to null, the caller uses the silent sink)
|
||
AudioComponentDescription d = {.componentType = kAudioUnitType_Output,
|
||
#if TARGET_OS_IPHONE
|
||
.componentSubType = kAudioUnitSubType_RemoteIO,
|
||
#else
|
||
.componentSubType = kAudioUnitSubType_DefaultOutput,
|
||
#endif
|
||
.componentManufacturer = kAudioUnitManufacturer_Apple};
|
||
AudioComponent c = AudioComponentFindNext(NULL, &d);
|
||
if (!c || AudioComponentInstanceNew(c, &a->au)) { a->au = NULL; LOG("【红】AudioUnit 建不出来:这一路改用静音接收端(不出声)"); return -1; }
|
||
a->mix = a->ch == 6; if (a->mix && !a->mixbuf) a->mixbuf = malloc(MIXMAX * 12); // 5.1:我们自己下混,AudioUnit 只收双声道(单 / 双声道照旧原样交) | 5.1: we downmix ourselves and the AudioUnit only takes stereo (mono / stereo are passed through as before)
|
||
if (a->mix && !a->mixbuf) { AudioComponentInstanceDispose(a->au); a->au = NULL; LOG("【红】下混暂存申请不到:这一路改用静音接收端(不出声)"); return -1; }
|
||
int och = a->mix ? 2 : a->ch;
|
||
AudioStreamBasicDescription f = {.mSampleRate = RATE, .mFormatID = kAudioFormatLinearPCM,
|
||
.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
|
||
.mBytesPerPacket = 2u * och, .mFramesPerPacket = 1, .mBytesPerFrame = 2u * och,
|
||
.mChannelsPerFrame = (UInt32)och, .mBitsPerChannel = 16};
|
||
if (a->mix) LOG("audio:游戏给 6 声道(5.1)→ 交给声卡前自己下混成双声道(中置 / 后环绕 ×0.707 并入左右,低音不并入,整体 −3 dB、饱和截断)");
|
||
AURenderCallbackStruct cb = {au_render, a};
|
||
if (AudioUnitSetProperty(a->au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &f, sizeof f) ||
|
||
AudioUnitSetProperty(a->au, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &cb, sizeof cb) ||
|
||
AudioUnitInitialize(a->au)) { AudioComponentInstanceDispose(a->au); a->au = NULL; LOG("【红】AudioUnit 设置失败(没有输出设备?):这一路改用静音接收端(不出声)"); return -1; }
|
||
#if !TARGET_OS_IPHONE
|
||
{ // 缺省输出单元跟随系统缺省输出设备:两个都记下来,核「声音从系统选的那个设备出」 | the default output unit follows the system default output device: record both, to check that "the sound comes out of the device the system chose"
|
||
AudioObjectID cur = 0, def = 0; UInt32 sz = sizeof cur; char cn[128], dn[128];
|
||
AudioUnitGetProperty(a->au, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &cur, &sz);
|
||
AudioObjectPropertyAddress pa = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain }; sz = sizeof def;
|
||
AudioObjectGetPropertyData(kAudioObjectSystemObject, &pa, 0, NULL, &sz, &def); dev_name(cur, cn, sizeof cn); dev_name(def, dn, sizeof dn);
|
||
LOG("audio:输出设备「%s」(%u),系统缺省输出「%s」(%u)%s", cn, (unsigned)cur, dn, (unsigned)def, cur == def ? ",一致" : "【红】不一致"); }
|
||
#endif
|
||
return 0;
|
||
}
|
||
#elif defined(__ANDROID__)
|
||
// AAudio:48 kHz PCM16,先按游戏的声道数(常见 6)开;设备不收就开双声道,回调里按 ITU 系数把 5.1 下混成立体声
|
||
// AAudio: 48 kHz PCM16, opened first with the game's channel count (usually 6); if the device refuses, open stereo and downmix 5.1 to stereo with ITU coefficients in the callback
|
||
enum { MIX_FRAMES = 1024 };
|
||
static void downmix(const int16_t *in, int16_t *out, int frames, int ch) {
|
||
for (int i = 0; i < frames; i++, in += ch, out += 2) {
|
||
float l = in[0], r = ch > 1 ? in[1] : in[0];
|
||
if (ch >= 6) { float c = in[2] * .7071f; l += c + in[4] * .7071f; r += c + in[5] * .7071f; } // FL FR C LFE SL SR;低音声道不混 | FL FR C LFE SL SR; the LFE channel is not mixed in
|
||
else if (ch >= 4) { l += in[2] * .7071f; r += in[3] * .7071f; }
|
||
if (ch >= 4) { l *= .5858f; r *= .5858f; } // 1/(1+.7071+.7071):不削波 | 1/(1+.7071+.7071): no clipping
|
||
out[0] = (int16_t)(l > 32767 ? 32767 : l < -32768 ? -32768 : l); out[1] = (int16_t)(r > 32767 ? 32767 : r < -32768 ? -32768 : r);
|
||
}
|
||
}
|
||
static aaudio_data_callback_result_t au_render(AAudioStream *st, void *ctx, void *data, int32_t frames) {
|
||
(void)st; aout *a = ctx;
|
||
atomic_fetch_add_explicit(&g_render_calls, 1, memory_order_relaxed);
|
||
if (a->out_ch == a->ch) { pull(a, data, (uint64_t)frames * 2 * a->ch); return AAUDIO_CALLBACK_RESULT_CONTINUE; }
|
||
int16_t *out = data;
|
||
while (frames > 0) { int n = frames < MIX_FRAMES ? frames : MIX_FRAMES;
|
||
pull(a, (uint8_t *)a->mixbuf, (uint64_t)n * 2 * a->ch); downmix(a->mixbuf, out, n, a->ch);
|
||
out += n * 2; frames -= n; }
|
||
return AAUDIO_CALLBACK_RESULT_CONTINUE;
|
||
}
|
||
static void au_reopen(aout *a);
|
||
static void *null_sink(void *p);
|
||
// 设备断开(拔耳机、切蓝牙):AAudio 的回调线程里不许关流重开,交给一条普通线程 | device disconnected (headphones unplugged, Bluetooth switched): the stream must not be closed / reopened on AAudio's callback thread, hand it to an ordinary thread
|
||
static void *au_reopen_th(void *p) { au_reopen(p); return NULL; }
|
||
static void au_error(AAudioStream *st, void *ctx, aaudio_result_t err) {
|
||
(void)st; aout *a = ctx;
|
||
LOG("【红】AAudio 出错 %d(%s):换设备重开", err, AAudio_convertResultToText(err));
|
||
if (atomic_exchange(&a->lost, 1)) return;
|
||
pthread_t t; if (!pthread_create(&t, NULL, au_reopen_th, a)) pthread_detach(t);
|
||
}
|
||
static AAudioStream *au_stream(aout *a, int ch, int low_latency) {
|
||
AAudioStreamBuilder *b; AAudioStream *st = NULL;
|
||
if (AAudio_createStreamBuilder(&b) != AAUDIO_OK) return NULL;
|
||
AAudioStreamBuilder_setDirection(b, AAUDIO_DIRECTION_OUTPUT);
|
||
AAudioStreamBuilder_setSharingMode(b, AAUDIO_SHARING_MODE_SHARED);
|
||
AAudioStreamBuilder_setSampleRate(b, RATE); AAudioStreamBuilder_setChannelCount(b, ch);
|
||
AAudioStreamBuilder_setFormat(b, AAUDIO_FORMAT_PCM_I16);
|
||
AAudioStreamBuilder_setUsage(b, AAUDIO_USAGE_GAME); AAudioStreamBuilder_setContentType(b, AAUDIO_CONTENT_TYPE_MUSIC);
|
||
AAudioStreamBuilder_setPerformanceMode(b, low_latency ? AAUDIO_PERFORMANCE_MODE_LOW_LATENCY : AAUDIO_PERFORMANCE_MODE_NONE);
|
||
AAudioStreamBuilder_setDataCallback(b, au_render, a); AAudioStreamBuilder_setErrorCallback(b, au_error, a);
|
||
if (AAudioStreamBuilder_openStream(b, &st) != AAUDIO_OK) st = NULL;
|
||
AAudioStreamBuilder_delete(b);
|
||
// 系统可能悄悄改格式(重采样到别的率、改声道):不是要的就不用 | the system may silently change the format (resample to another rate, change channels): reject it if it is not what was asked for
|
||
if (st && (AAudioStream_getSampleRate(st) != RATE || AAudioStream_getChannelCount(st) != ch || AAudioStream_getFormat(st) != AAUDIO_FORMAT_PCM_I16)) { AAudioStream_close(st); st = NULL; }
|
||
return st;
|
||
}
|
||
static int au_try_open(aout *a) {
|
||
for (int lat = 1; lat >= 0; lat--) {
|
||
if ((a->au = au_stream(a, a->ch, lat))) { a->out_ch = a->ch; break; }
|
||
if (a->ch != 2 && (a->au = au_stream(a, 2, lat))) { a->out_ch = 2; break; }
|
||
}
|
||
if (!a->au) return 0;
|
||
if (a->out_ch != a->ch && !a->mixbuf) a->mixbuf = malloc((size_t)MIX_FRAMES * 2 * a->ch);
|
||
int burst = AAudioStream_getFramesPerBurst(a->au);
|
||
if (burst > 0) AAudioStream_setBufferSizeInFrames(a->au, burst * 4); // 4 个突发 ≈ 几毫秒:游戏自己有 FMOD 缓冲,这里少留防爆音 | 4 bursts ≈ a few ms: the game has its own FMOD buffering, keep a little here against crackles
|
||
LOG("AAudio 打开:48 kHz PCM16 声道 %d(游戏 %d%s)突发 %d 帧 性能模式 %d", a->out_ch, a->ch, a->out_ch != a->ch ? ",下混" : "", burst, AAudioStream_getPerformanceMode(a->au));
|
||
return 1;
|
||
}
|
||
static void au_open(aout *a) { if (!au_try_open(a)) LOG("【红】AAudio 打不开:用静音接收端"); }
|
||
static OSStatus au_wait(AudioUnit au, aaudio_stream_state_t want) {
|
||
aaudio_stream_state_t st = AAudioStream_getState(au), next;
|
||
for (int i = 0; i < 10 && st != want; i++) { // 超时不算失败(回前台时 STARTING 常要一百多毫秒):最多等 1 秒 | a timeout is not a failure (STARTING often takes 100+ ms when returning to foreground): wait up to 1 s
|
||
aaudio_result_t r = AAudioStream_waitForStateChange(au, st, &next, 100000000);
|
||
if (r == AAUDIO_OK) st = next; else if (r == AAUDIO_ERROR_TIMEOUT) st = AAudioStream_getState(au); else break; }
|
||
return st == want ? 0 : -1;
|
||
}
|
||
static OSStatus AudioOutputUnitStart(AudioUnit au) {
|
||
aaudio_result_t r = AAudioStream_requestStart(au); OSStatus st = r == AAUDIO_OK ? au_wait(au, AAUDIO_STREAM_STATE_STARTED) : -1;
|
||
if (st) LOG("AAudio 启动失败:%s,流状态 %s", AAudio_convertResultToText(r), AAudio_convertStreamStateToText(AAudioStream_getState(au)));
|
||
return st;
|
||
}
|
||
// 停要等回调真停:之后静音接收端接手同一条队列,两边不能同时拉 | stopping waits until callbacks have really stopped: afterwards the silent sink takes over the same queue, the two must not pull at once
|
||
static OSStatus AudioOutputUnitStop(AudioUnit au) { return AAudioStream_requestStop(au) == AAUDIO_OK ? au_wait(au, AAUDIO_STREAM_STATE_STOPPED) : -1; }
|
||
static void au_reopen(aout *a) {
|
||
pthread_mutex_lock(&g_aos_lock);
|
||
int live = 0; for (int i = 0; i < AOMAX; i++) live |= g_aos[i] == a;
|
||
if (live && a->au) {
|
||
AAudioStream_close(a->au); a->au = NULL; // 断开的流回调已停 | callbacks of a disconnected stream have stopped
|
||
if (!au_try_open(a)) { LOG("【红】AAudio 重开失败:改用静音接收端");
|
||
if (atomic_load(&a->state) == STATE_STARTED && !g_suspended) { atomic_store(&a->null_run, 1); pthread_create(&a->null_th, NULL, null_sink, a); } }
|
||
else if (atomic_load(&a->state) == STATE_STARTED && !g_suspended && AudioOutputUnitStart(a->au)) LOG("【红】AAudio 重开后启动失败");
|
||
}
|
||
atomic_store(&a->lost, 0);
|
||
pthread_mutex_unlock(&g_aos_lock);
|
||
}
|
||
#else
|
||
static void au_open(aout *a) { (void)a; die("Linux 宿主还没有真出声后端:用 DM_AUDIO=0(静音接收端)"); }
|
||
#endif
|
||
// 静音接收端:每 5 ms 按 48 kHz 实时消耗
|
||
// Silent sink: consumes in real time at 48 kHz every 5 ms
|
||
static void *null_sink(void *p) {
|
||
aout *a = p; uint8_t scratch[RATE / 200 * 2 * 8];
|
||
uint64_t bytes = (uint64_t)RATE / 200 * 2 * a->ch, t = now_ns();
|
||
while (atomic_load_explicit(&a->null_run, memory_order_acquire)) {
|
||
t += 5000000; uint64_t now = now_ns(); if (t > now) usleep((useconds_t)((t - now) / 1000));
|
||
pull(a, scratch, bytes);
|
||
}
|
||
return NULL;
|
||
}
|
||
#if defined(__ANDROID__)
|
||
#define DM_AUDIO_DEFAULT 1 // Android App 同 iOS:缺省真出声 | Android app, like iOS: real audio by default
|
||
#else
|
||
#define DM_AUDIO_DEFAULT TARGET_OS_IPHONE
|
||
#endif
|
||
static int real_audio(void) { const char *e = getenv("DM_AUDIO"); return e ? atoi(e) != 0 : DM_AUDIO_DEFAULT; }
|
||
|
||
// AudioOutParameter = { int32 sampleRate; uint16 channelCount; uint16 _r; ... },0 = 系统默认(48 kHz / 2 声道)
|
||
// AudioOutParameter = { int32 sampleRate; uint16 channelCount; uint16 _r; ... }, 0 = system default (48 kHz / 2 channels)
|
||
HLE_FORCE("_ZN2nn5audio27InitializeAudioOutParameterEPNS0_17AudioOutParameterE", "音频输出参数默认值", void, h_au_InitParam, uint8_t *p) { memset(p, 0, 8); }
|
||
// AudioOutInfo = { char name[256] }
|
||
HLE_FORCE("_ZN2nn5audio13ListAudioOutsEPNS0_12AudioOutInfoEi", "音频输出设备列表;宿主 1 个", int, h_au_List, char *info, int n) {
|
||
if (n < 1) return 0;
|
||
memset(info, 0, 256); strcpy(info, "DeviceOut"); return 1;
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio12OpenAudioOutEPNS0_8AudioOutEPNS_2os11SystemEventEPKcRKNS0_17AudioOutParameterE", "连 audout 服务;宿主 AudioUnit / 静音接收端",
|
||
Result, h_au_Open, void *g, void *ev, const char *name, const uint8_t *param) {
|
||
int ch = *(const uint16_t *)(param + 4); int rate = *(const int32_t *)param;
|
||
if (!ch) ch = 2;
|
||
if (rate && rate != RATE) die("AudioOut 采样率 %d 不支持(只做 48 kHz)", rate);
|
||
aout *a = calloc(1, sizeof *a);
|
||
pthread_mutex_init(&a->m, NULL); a->event = ev; a->ch = ch;
|
||
atomic_init(&a->state, STATE_STOPPED);
|
||
atomic_init(&a->q_head, 0); atomic_init(&a->q_tail, 0);
|
||
atomic_init(&a->rel_head, 0); atomic_init(&a->rel_tail, 0);
|
||
atomic_init(&a->null_run, 0); atomic_init(&a->notify_run, 1);
|
||
atomic_init(&a->notify_pending, 0); atomic_init(&a->notify_armed, 0);
|
||
a->notify_sem = dispatch_semaphore_create(0);
|
||
pthread_create(&a->notify_th, NULL, event_notify, a);
|
||
if (ev) hle_bind_system_event(ev, 1); // 缓冲释放事件:客体线程上绑内核事件(自动清除),之后由音频线程触发 | Buffer release event: bind a kernel event on the guest thread (auto-clear), afterwards signaled by the audio thread
|
||
objmap_put(g, a);
|
||
pthread_mutex_lock(&g_aos_lock);
|
||
int slot = -1; for (int i = 0; i < AOMAX; i++) if (!g_aos[i]) { slot = i; break; }
|
||
if (slot < 0) die("AudioOut 同时打开超过 %d 个", AOMAX);
|
||
g_aos[slot] = a;
|
||
pthread_mutex_unlock(&g_aos_lock);
|
||
LOG("audio::OpenAudioOut %s 声道 %d %s", name ? name : "(默认)", ch, real_audio() ? "真出声" : "静音接收端");
|
||
if (real_audio()) au_open(a); // 开不了就 a->au 为空,开始播放时走静音接收端 | if it cannot be opened, a->au is null, and playback goes to the silent sink when it starts
|
||
return RES_OK;
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio13StartAudioOutEPNS0_8AudioOutE", "开始播放", Result, h_au_Start, void *g) {
|
||
aout *a = ao(g);
|
||
pthread_mutex_lock(&a->m); int was = atomic_load_explicit(&a->state, memory_order_relaxed); atomic_store_explicit(&a->state, STATE_STARTED, memory_order_release); pthread_mutex_unlock(&a->m);
|
||
if (was == STATE_STARTED) return RES_OK;
|
||
if (a->au && !g_suspended && AudioOutputUnitStart(a->au)) { LOG("【红】AudioUnit 启动失败:这一路改用静音接收端(不出声)"); AudioUnitUninitialize(a->au); AudioComponentInstanceDispose(a->au); a->au = NULL; }
|
||
if (!a->au || g_suspended) { atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); }
|
||
return RES_OK;
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio12StopAudioOutEPNS0_8AudioOutE", "停止播放", void, h_au_Stop, void *g) {
|
||
aout *a = ao(g);
|
||
pthread_mutex_lock(&a->m); int was = atomic_load_explicit(&a->state, memory_order_relaxed); atomic_store_explicit(&a->state, STATE_STOPPED, memory_order_release); pthread_mutex_unlock(&a->m);
|
||
if (was != STATE_STARTED) return;
|
||
pthread_mutex_lock(&g_aos_lock); int susp = g_suspended; pthread_mutex_unlock(&g_aos_lock);
|
||
if (a->au && !susp) AudioOutputUnitStop(a->au);
|
||
else { atomic_store_explicit(&a->null_run, 0, memory_order_release); pthread_join(a->null_th, NULL); } // 静音接收端(含暂停期间接手的) | Silent sink (including one that took over during suspension)
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio13CloseAudioOutEPNS0_8AudioOutE", "关闭音频输出", void, h_au_Close, void *g) {
|
||
h_au_Stop(g);
|
||
aout *a = objmap_del(g);
|
||
atomic_store_explicit(&a->notify_run, 0, memory_order_release);
|
||
dispatch_semaphore_signal(a->notify_sem);
|
||
pthread_join(a->notify_th, NULL);
|
||
pthread_mutex_lock(&g_aos_lock);
|
||
for (int i = 0; i < AOMAX; i++) if (g_aos[i] == a) g_aos[i] = NULL;
|
||
pthread_mutex_unlock(&g_aos_lock);
|
||
if (a->au) { AudioUnitUninitialize(a->au); AudioComponentInstanceDispose(a->au); }
|
||
dispatch_release(a->notify_sem);
|
||
free(a->mixbuf); pthread_mutex_destroy(&a->m); free(a);
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio16GetAudioOutStateEPKNS0_8AudioOutE", "播放状态", int, h_au_State, const void *g) { return atomic_load_explicit(&ao(g)->state, memory_order_acquire); }
|
||
HLE_FORCE("_ZN2nn5audio21GetAudioOutSampleRateEPKNS0_8AudioOutE", "采样率", int, h_au_Rate, const void *g) { (void)ao(g); return RATE; }
|
||
HLE_FORCE("_ZN2nn5audio23GetAudioOutChannelCountEPKNS0_8AudioOutE", "声道数", int, h_au_Ch, const void *g) { return ao(g)->ch; }
|
||
HLE_FORCE("_ZN2nn5audio23GetAudioOutSampleFormatEPKNS0_8AudioOutE", "采样格式;只做 PCM16", int, h_au_Fmt, const void *g) { (void)ao(g); return FMT_PCM16; }
|
||
|
||
HLE_FORCE("_ZN2nn5audio21SetAudioOutBufferInfoEPNS0_14AudioOutBufferEPvmm", "缓冲描述;写进客体结构体前 24 字节", void, h_au_SetBuf, gbuf *b, void *p, uint64_t cap, uint64_t size) {
|
||
b->ptr = p; b->cap = cap; b->size = size;
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio28GetAudioOutBufferDataPointerEPKNS0_14AudioOutBufferE", "缓冲数据指针", void *, h_au_BufPtr, const gbuf *b) { return b->ptr; }
|
||
HLE_FORCE("_ZN2nn5audio25GetAudioOutBufferDataSizeEPKNS0_14AudioOutBufferE", "缓冲数据大小", uint64_t, h_au_BufSize, const gbuf *b) { return b->size; }
|
||
HLE_FORCE("_ZN2nn5audio20AppendAudioOutBufferEPNS0_8AudioOutEPNS0_14AudioOutBufferE", "追加待播缓冲", unsigned, h_au_Append, void *g, gbuf *b) {
|
||
aout *a = ao(g);
|
||
// 诊断开关 DM_AUDIO_DUMP=<文件>:游戏追加的每块 PCM 原样续写(16 位、48 kHz、声道数见 OpenAudioOut 日志,常见 6)。判「某个音播没播」用:
|
||
// 拿原版音频对它做互相关(一档掉落音就这么验的)。只在设了变量时打开文件,没设时这里只多一次已缓存的判断
|
||
// Diagnostic switch DM_AUDIO_DUMP=<file>: every PCM block the game appends is written out as is (16-bit, 48 kHz, channel count per the OpenAudioOut log, usually 6). Used to judge "did a given sound play or not":
|
||
// cross-correlate the original game's audio against it (this is how the tier-one drop sound was verified). The file is opened only when the variable is set; when unset this costs only one extra cached check
|
||
{ static FILE *df; static int on = -1; if (on < 0) { const char *e = getenv("DM_AUDIO_DUMP"); on = e != NULL; if (on && !(df = fopen(e, "wb"))) LOG("【红】DM_AUDIO_DUMP 打不开 %s", e); }
|
||
if (df && b && b->ptr && b->size) fwrite(b->ptr, 1, (size_t)b->size, df); }
|
||
pthread_mutex_lock(&a->m);
|
||
uint32_t qt = atomic_load_explicit(&a->q_tail, memory_order_relaxed);
|
||
uint32_t qh = atomic_load_explicit(&a->q_head, memory_order_acquire);
|
||
int ok = qt - qh < QMAX;
|
||
if (ok) { a->q[qt & (QMAX - 1)] = b; atomic_store_explicit(&a->q_tail, qt + 1, memory_order_release); }
|
||
pthread_mutex_unlock(&a->m);
|
||
return ok; // bool:队列满时 false(真机同样有上限) | bool: false when the queue is full (the real device has a limit too)
|
||
}
|
||
HLE_FORCE("_ZN2nn5audio25GetReleasedAudioOutBufferEPNS0_8AudioOutE", "取已播完的缓冲;没有则 NULL", gbuf *, h_au_Released, void *g) {
|
||
aout *a = ao(g); gbuf *b = NULL;
|
||
pthread_mutex_lock(&a->m);
|
||
uint32_t rr = atomic_load_explicit(&a->rel_head, memory_order_relaxed);
|
||
uint32_t rw = atomic_load_explicit(&a->rel_tail, memory_order_acquire);
|
||
if (rr != rw) { b = a->rel[rr & (RELMAX - 1)]; atomic_store_explicit(&a->rel_head, rr + 1, memory_order_release); }
|
||
pthread_mutex_unlock(&a->m);
|
||
return b;
|
||
}
|
||
|
||
// 前端生命周期 / 音频会话打断:暂停真出声,期间由静音接收端接手按实时消耗(节奏不断);恢复时切回 AudioUnit。
|
||
// 前端在 切后台 / 打断开始 时调 1,回前台 / 打断结束(shouldResume)时调 0。
|
||
// Front-end lifecycle / audio session interruption: suspend real audio output, with the silent sink taking over real-time consumption meanwhile (pacing unbroken); switch back to the AudioUnit on resume.
|
||
// The front end calls with 1 on going to background / interruption began, and with 0 on returning to foreground / interruption ended (shouldResume).
|
||
void dm_audio_suspend(int on) {
|
||
pthread_mutex_lock(&g_aos_lock);
|
||
if (g_suspended != on) {
|
||
g_suspended = on; int failed = 0;
|
||
for (int i = 0; i < AOMAX; i++) {
|
||
aout *a = g_aos[i];
|
||
if (!a || !a->au || atomic_load_explicit(&a->state, memory_order_acquire) != STATE_STARTED) continue;
|
||
// 暂停期间改由静音接收端按实时消耗:否则缓冲不再释放,FMOD 输出线程永久等待、游戏音频节奏卡死(来电打断同理)
|
||
// While suspended the silent sink consumes in real time instead: otherwise buffers are no longer released, the FMOD output thread waits forever and the game's audio pacing stalls (same for an incoming call interruption)
|
||
if (on) { AudioOutputUnitStop(a->au); atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); }
|
||
else { atomic_store_explicit(&a->null_run, 0, memory_order_release); pthread_join(a->null_th, NULL);
|
||
OSStatus st = AudioOutputUnitStart(a->au);
|
||
#if defined(__ANDROID__)
|
||
if (st) { // 后台期间流被系统收走(断开 / 起不来):关掉重开一次再起 | the stream was taken away by the system while in the background (disconnected / won't start): close, reopen once and start again
|
||
AAudioStream_close(a->au); a->au = NULL;
|
||
st = au_try_open(a) ? AudioOutputUnitStart(a->au) : -1;
|
||
LOG("AAudio 回前台重开%s", st ? "仍失败" : "后起来了"); }
|
||
#endif
|
||
if (st) { // 会话还被别人占着(通话没挂就切回游戏、打断结束时人还在别的 App):不致命,继续由静音接收端顶着,等下一次恢复再试 | The session is still held by someone else (switched back to the game with a call not yet hung up, or the user is still in another app when the interruption ends): not fatal, the silent sink keeps covering, and we retry at the next resume
|
||
LOG("【红】AudioUnit 恢复失败(状态 %d):保持静音接收端,等下次恢复重试", (int)st);
|
||
atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); failed = 1; } }
|
||
}
|
||
if (failed) g_suspended = 1; // 没恢复成:状态留在「暂停」,下次调 0 会再试 | Resume did not succeed: the state stays "suspended", and the next call with 0 will retry
|
||
LOG("audio %s", on ? "暂停(切后台 / 被打断)" : failed ? "恢复未成" : "恢复(回前台 / 打断结束)");
|
||
}
|
||
pthread_mutex_unlock(&g_aos_lock);
|
||
}
|