NPC speech restored (explicit 5.1 to stereo downmix); graphics memory pools made resident only on first GPU use (about 0.5 GB less held); render targets written through a format-changing view attached with that format, sRGB swapchain presented through a non-sRGB view (#36); bloom / FXAA texture-slot rewrites mark bindings dirty; Mac App plays sound and fills 16:10 screens
The render-target view issue (#36) was found and diagnosed by seedds on another port built from this repository, who also proposed the fix direction. Co-authored-by: seedds <3463322+seedds@users.noreply.github.com>
This commit is contained in:
+59
-15
@@ -1,11 +1,15 @@
|
||||
// nn::audio AudioOut:游戏(FMOD nx 输出层)按块追加 PCM 缓冲,播完由系统放回「已释放」队列并触发事件。
|
||||
// 宿主:真出声 = CoreAudio AudioUnit(Mac DefaultOutput / iOS RemoteIO);静音接收端 = 按真实时间消耗缓冲(不出声,节奏与真机一致)。
|
||||
// iOS 缺省真出声(产品必须有声音),测试要静音显式 DM_AUDIO=0;Mac 缺省静音接收端(回放 / 闸门不出声),DM_AUDIO=1 真出声。
|
||||
// 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 nx output layer) appends PCM buffers block by block; once played the system puts them back on the "released" queue and signals the event.
|
||||
// Host: real audio output = CoreAudio AudioUnit (Mac DefaultOutput / iOS RemoteIO); silent sink = consumes buffers in real time (no sound, pacing matches the real device).
|
||||
// iOS defaults to real audio output (the product must have sound), tests that want silence set DM_AUDIO=0 explicitly; Mac defaults to the silent sink (replay / gate checks are silent), DM_AUDIO=1 for real audio output.
|
||||
// The contents of AudioOut / AudioOutBuffer / AudioOutParameter are read and written only through the interfaces below; the host defines its own internal layout and only writes the front part of the structs.
|
||||
// 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"
|
||||
#include <AudioToolbox/AudioToolbox.h>
|
||||
#include <TargetConditionals.h>
|
||||
@@ -27,6 +31,8 @@ typedef struct {
|
||||
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;
|
||||
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)
|
||||
int16_t *mixbuf; // 下混用的 6 声道暂存(MIXMAX 帧) | 6-channel staging for the downmix (MIXMAX frames)
|
||||
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;
|
||||
@@ -89,14 +95,39 @@ static void pull(aout *a, uint8_t *dst, uint64_t n) {
|
||||
|
||||
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); }
|
||||
// 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);
|
||||
pull(a, bl->mBuffers[0].mData, bl->mBuffers[0].mDataByteSize);
|
||||
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;
|
||||
}
|
||||
static void au_open(aout *a) {
|
||||
#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,
|
||||
@@ -105,15 +136,28 @@ static void au_open(aout *a) {
|
||||
#endif
|
||||
.componentManufacturer = kAudioUnitManufacturer_Apple};
|
||||
AudioComponent c = AudioComponentFindNext(NULL, &d);
|
||||
if (!c || AudioComponentInstanceNew(c, &a->au)) die("AudioUnit 建不出来");
|
||||
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 * a->ch, .mFramesPerPacket = 1, .mBytesPerFrame = 2u * a->ch,
|
||||
.mChannelsPerFrame = (UInt32)a->ch, .mBitsPerChannel = 16};
|
||||
.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)) die("AudioUnit 设置失败");
|
||||
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;
|
||||
}
|
||||
// 静音接收端:每 5 ms 按 48 kHz 实时消耗
|
||||
// Silent sink: consumes in real time at 48 kHz every 5 ms
|
||||
@@ -158,15 +202,15 @@ HLE_FORCE("_ZN2nn5audio12OpenAudioOutEPNS0_8AudioOutEPNS_2os11SystemEventEPKcRKN
|
||||
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);
|
||||
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) { if (AudioOutputUnitStart(a->au)) die("AudioUnit 启动失败"); }
|
||||
else { atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); }
|
||||
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) {
|
||||
@@ -188,7 +232,7 @@ HLE_FORCE("_ZN2nn5audio13CloseAudioOutEPNS0_8AudioOutE", "关闭音频输出", v
|
||||
pthread_mutex_unlock(&g_aos_lock);
|
||||
if (a->au) { AudioUnitUninitialize(a->au); AudioComponentInstanceDispose(a->au); }
|
||||
dispatch_release(a->notify_sem);
|
||||
pthread_mutex_destroy(&a->m); free(a);
|
||||
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; }
|
||||
|
||||
Reference in New Issue
Block a user