From 5c3083b11ceb3ee4dabf42fc999aac2d44177289 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Wed, 14 Oct 2026 17:34:29 +0800 Subject: [PATCH] Add forced 24-bit output (int24) for the delivered samples --- README.en.md | 14 +++++++ README.md | 9 +++++ src/input_joc.cpp | 77 ++++++++++++++++++++++++++++++------ src/joc_decode.cpp | 40 +++++++++++++++++++ src/joc_decode.h | 31 +++++++++++++++ src/prefs.cpp | 19 ++++++++- src/prefs.rc | 43 ++++++++++---------- src/resource.h | 3 ++ src/settings.cpp | 23 +++++++++++ src/settings.h | 2 + tests/prefs_layout_check.cpp | 23 +++++++---- tests/render_harness.cpp | 72 +++++++++++++++++++++++++-------- 12 files changed, 299 insertions(+), 57 deletions(-) diff --git a/README.en.md b/README.en.md index a8be17e..714dba6 100644 --- a/README.en.md +++ b/README.en.md @@ -65,6 +65,15 @@ Preferences → Tools → **JOC decoder**: `binaural.personalized_headphone`; * **Gain** — a switch and a value in dB. Binaural rendering can exceed full scale on material that does not clip in the core mix, so attenuation belongs here; +* **Force 24-bit output (int24)** — off by default, which delivers float32. When on, every sample + is quantised with the core's own int24 rule (clip to ±1, ×8388607, truncate) before it is handed + over, the samples that had to be clipped are counted in the log, and the component reports the + file as 24-bit fixed point. The renderer itself stays float32: this is the precision the decoder + delivers, not the output device's bit depth, which is set in Preferences → Output. **Reload a + file's information after flipping this switch** (Properties → *Reload info*, or remove the file + from the Media Library and add it again): foobar2000 caches technical information per file, and + until it is reloaded Properties and the Converter still go by the old 32-bit floating-point + answer, warning that the encoder does not take floating-point input. * the **ffmpeg** executable to use. No control on the page is disabled, and the status line states what is in effect. @@ -111,6 +120,11 @@ every container in the library, which is where the volume comes from. behind it) can be **read but not written**: no component claims raw E-AC-3 for writing, and rewriting the whole file to insert a tag is not this component's job. * Transport streams (`.ts`, `.m2ts`) are not claimed. +* **Force 24-bit output** only changes the precision the decoder delivers: foobar2000 converts the + int24 it receives into its own floating-point samples, and which bit depth the output device runs + at is still decided in Preferences → Output, not by this component. Samples outside ±1 follow the + core's `continue` clip policy (binaural rendering can exceed full scale, so use **Gain** when + attenuation is wanted). * Playback length is exactly the file's duration. The binaural renderer still computes its room tail, but it is not delivered as playback time the file does not have. * A seek re-enters the bitstream at the frame holding the target instead of decoding everything diff --git a/README.md b/README.md index a9b73b8..96a0f4d 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,12 @@ Preferences → Tools → **JOC decoder**: * **HRTF source** —— **SOFA** 文件或 **Rosella** `.personalized_headphone` 模型。路径留空表示 用默认位置 `<组件目录>\HRTF\` 下的 `binaural.sofa` 或 `binaural.personalized_headphone`; * **Gain** —— 开关加 dB 值。双耳渲染在核心混音不削顶的素材上也可能超过满刻度,衰减放在这里; +* **强制 24bit 输出(int24)** —— 默认关闭,交付 float32。打开后,交付前按内核写 WAV 的 int24 + 规则量化(截到 ±1 → ×8388607 → 截断),超范围的样本被截断到边缘、次数记进日志;组件同时把该 + 文件的技术信息报成 24bit 定点。渲染本身仍是 float32,这一项说的是交付给 foobar2000 的采样精度, + 不是输出设备的位深(那个在 Preferences → Output 里设置)。**切换这个开关后要刷新文件信息** + (Properties → Reload info,或把文件从媒体库移除后重新加入):foobar2000 按文件缓存技术信息, + 缓存没刷新时 Properties 与转换器仍按旧的 32bit 浮点判断,会提示"编码器不支持浮点输入"。 * **ffmpeg** 可执行文件路径。 页面上的控件都不禁用,状态行会说明当前生效的是什么。 @@ -94,6 +100,9 @@ pwsh -File tools/package.ps1 # 两个架构,打包到 dist\*.fb2k-c * 裸流 `.eac3` / `.ec3` 的标签(流前面的 ID3v2,或后面的 APEv2/ID3v1)**能读不能写**:没有 组件声明可以写裸 E-AC-3,为插入标签重写整个文件也不是本组件该做的事。 * 传输流(`.ts`、`.m2ts`)不接管。 +* **强制 24bit 输出**只改变解码器交付的采样精度:交付的 int24 会被 foobar2000 转成它内部的 + 浮点,输出设备用什么位深仍由 Preferences → Output 决定,组件不能替它选。超出 ±1 的样本按内核 + 的 `continue` 策略截断(双耳渲染可能超过满刻度,需要衰减时用 **Gain**)。 * 播放长度严格等于文件时长。双耳渲染器仍会算出房间尾音,但它不作为文件本身没有的播放时间交付。 * 跳转会从包含目标位置的那个帧重新进入码流,而不是把前面的内容全部解码一遍——这是跳转代价与 目标位置无关的原因。位置精确、不漂移;样本是同一段波形交给了从该处开始的解码器,与从头播放 diff --git a/src/input_joc.cpp b/src/input_joc.cpp index a3fe49e..1fba448 100644 --- a/src/input_joc.cpp +++ b/src/input_joc.cpp @@ -354,14 +354,22 @@ public: channels = layout_channels != 0 ? layout_channels : 6; render = "speaker " + settings.speaker_layout; } + // The custom field and the log line state the delivery format, because it + // is the one thing about the output that a listener cannot hear. + if (settings.force_int24) render += ", 24-bit delivery"; info.info_set("codec", "E-AC-3 JOC"); info.info_set("codec_long", "E-AC-3 JOC (Dolby Atmos)"); info.info_set("encoding", "lossy"); info.info_set_int("samplerate", kSampleRate); info.info_set_int("channels", channels); - info.info_set_int("bitspersample", 32); - info.info_set("bitspersample_extra", "floating-point"); + // What the component hands over, which is what the host keeps if it writes a + // file from this stream: float32 by default, int24 when forced. This is also + // what foobar2000's own lossless-conversion warning reads + // (SDK file_info.cpp:is_encoding_float), so it has to use the SDK's convention: + // 32 bits with "floating-point", or a bit depth that is fixed-point by + // definition (anything but 32/64, with no extra field at all). + info.info_set_bitspersample(settings.force_int24 ? 24u : 32u, !settings.force_int24); info.set_length(duration); m_length = duration; // The renderer keeps its room tail, but the stream this component hands over @@ -455,8 +463,15 @@ public: m_frames_delivered = 0; m_reported = false; m_delivery_mismatches = 0; - joc_log::line("decoder: engine ready, %u output channel(s), %u frames per read", - m_channels, static_cast(kRunFrames)); + // The clip total belongs to the file rather than to one run of it, so a seek + // does not clear it: the log line at the end of the stream is a statement + // about everything this component handed over. + m_clipped_samples = 0; + m_clip_reported = false; + joc_log::line("decoder: engine ready, %u output channel(s), %u frames per read, " + "delivery=%s", + m_channels, static_cast(kRunFrames), + m_settings.force_int24 ? "int24" : "float32"); } bool decode_run(audio_chunk& chunk, abort_callback& abort) { @@ -472,6 +487,10 @@ public: static_cast(m_frames_delivered), m_delivery_mismatches == 0 ? "" : " (the delivery changed samples)"); + if (m_clipped_samples != 0) { + joc_log::line("decoder: forced int24 truncated %llu sample(s) in total", + static_cast(m_clipped_samples)); + } return false; } @@ -479,30 +498,58 @@ public: // 32-bit builds and double on 64-bit ones (SDK audio_math.h): the samples are // converted, not copied. set_data_32() is the SDK's conversion for a float32 // source, and it sets the channel count, the sample rate and the sample count. - chunk.set_data_32(m_buffer.data(), frames, m_channels, kSampleRate); + // + // A forced int24 delivery goes the other way: the samples are packed to the + // core's int24 bytes and the host converts them back, so the audio the rest of + // the pipeline sees is the 24-bit grid rather than the renderer's float -- which + // is the whole point of the option. The quantisation happens here and nowhere + // else, so a sample is rounded once. + double expected_delivered_peak = 0.0; + if (m_settings.force_int24) { + m_int24.resize(frames * m_channels * 3); + const joc_decode::Int24Block block = joc_decode::pack_int24( + m_buffer.data(), frames * m_channels, m_int24.data()); + m_clipped_samples += block.clipped; + if (block.clipped != 0 && !m_clip_reported) { + m_clip_reported = true; + joc_log::line("decoder: forced int24: %llu sample(s) outside [-1,1] in this " + "block are truncated to the edge (the core's clip policy; " + "attenuate with the gain setting to avoid it)", + static_cast(block.clipped)); + } + chunk.set_data_fixedpoint_signed(m_int24.data(), m_int24.size(), kSampleRate, + m_channels, 24, + audio_chunk::g_guess_channel_config(m_channels)); + expected_delivered_peak = + static_cast(block.peak) / joc_decode::kInt24ImportScale; + } else { + chunk.set_data_32(m_buffer.data(), frames, m_channels, kSampleRate); + expected_delivered_peak = peak_of(m_buffer.data(), frames * m_channels); + } m_frames_delivered += frames; // A delivery that mangled the samples would be heard as noise rather than reported // as a failure, so every chunk is checked: the conversion is exact, and the peak of - // what the chunk holds has to equal the peak of what the renderer produced. - const double produced = peak_of(m_buffer.data(), frames * m_channels); + // what the chunk holds has to equal the peak of what the renderer produced -- or, + // for int24, the peak the packing step actually wrote. const double delivered = peak_of(chunk.get_data(), chunk.get_sample_count() * chunk.get_channels()); - if (delivered > produced + 1e-6 + produced * 1e-6 || - delivered < produced - 1e-6 - produced * 1e-6) { + if (delivered > expected_delivered_peak + 1e-6 + expected_delivered_peak * 1e-6 || + delivered < expected_delivered_peak - 1e-6 - expected_delivered_peak * 1e-6) { ++m_delivery_mismatches; if (m_delivery_mismatches == 1) { joc_log::line("decoder: delivery changed the samples: peak %.9f produced, " "%.9f delivered", - produced, delivered); + expected_delivered_peak, delivered); } } if (!m_reported) { m_reported = true; - joc_log::line("decoder: first %llu frames delivered (%u ch), peak %.6f, " + joc_log::line("decoder: first %llu frames delivered (%u ch, %s), peak %.6f, " "delivered peak %.6f", - static_cast(frames), m_channels, produced, - delivered); + static_cast(frames), m_channels, + m_settings.force_int24 ? "int24" : "float32", + peak_of(m_buffer.data(), frames * m_channels), delivered); } return true; } @@ -605,6 +652,10 @@ private: unsigned m_audio_index = 0; joc_container::Result m_container; std::vector m_buffer; + // int24 delivery: the packed bytes handed to the host, and what the clip caught. + std::vector m_int24; + std::uint64_t m_clipped_samples = 0; + bool m_clip_reported = false; unsigned m_channels = 2; std::uint64_t m_frames_delivered = 0; bool m_reported = false; diff --git a/src/joc_decode.cpp b/src/joc_decode.cpp index 9d0a427..bc3abbd 100644 --- a/src/joc_decode.cpp +++ b/src/joc_decode.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include // The kernel copy that is compiled into this component; see kernel/. @@ -309,6 +310,45 @@ unsigned speaker_channels(const std::string& layout) { return 0; } +namespace { + +// The core's conversion, reproduced: kernel/src/io/wav_writer.cpp:to_int32. +// (min)() is written that way because the test tools compile this file without +// NOMINMAX, and windows.h's min macro would otherwise swallow the call. +std::int32_t to_int32(const float value) { + if (!std::isfinite(value)) { + return (std::numeric_limits::min)(); + } + return static_cast(value); +} + +} // namespace + +Int24Block pack_int24(const float* interleaved, std::size_t count, std::uint8_t* out) { + Int24Block block; + for (std::size_t i = 0; i < count; ++i) { + float value = interleaved[i]; + if (value > 1.0f) { + value = 1.0f; + ++block.clipped; + } else if (value < -1.0f) { + value = -1.0f; + ++block.clipped; + } + const std::int32_t scaled = to_int32(value * 8388607.0f); + const std::uint32_t bits = static_cast(scaled); + out[i * 3 + 0] = static_cast(bits & 0xFFu); + out[i * 3 + 1] = static_cast((bits >> 8) & 0xFFu); + out[i * 3 + 2] = static_cast((bits >> 16) & 0xFFu); + // What the host will read back is the sign-extended low 24 bits. + std::int32_t written = static_cast(bits & 0xFFFFFFu); + if ((written & 0x800000) != 0) written -= 0x1000000; + const std::int32_t magnitude = (written < 0) ? -written : written; + if (magnitude > block.peak) block.peak = magnitude; + } + return block; +} + std::string component_directory() { HMODULE self = nullptr; if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | diff --git a/src/joc_decode.h b/src/joc_decode.h index e49ab03..67e08c1 100644 --- a/src/joc_decode.h +++ b/src/joc_decode.h @@ -96,6 +96,15 @@ struct Settings { // is what the reference CLI's --duration does. Zero means "the whole file". // Only the comparison harness sets it; playback leaves it at zero. std::uint64_t input_frame_limit = 0; + // Deliver 24-bit fixed-point PCM instead of float32. The renderer itself is + // always float32 and this does not change it: the flag says how the *caller* + // hands the samples on, and the quantisation happens once, where the format is + // decided (see pack_int24 below). It exists because the streaming interface + // has no int24 output -- the core's int24 rule lives in its file-writing path + // (kernel/src/io/wav_writer.cpp), which the component cannot reach, so the + // rule is reproduced here instead. Out-of-range samples follow the core's + // JOC_CLIP_CONTINUE policy: truncated to the edge, and reported. + bool force_int24 = false; }; // Channel count of a named layout, or 0 when the layout is not one of the @@ -139,6 +148,28 @@ FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes = 0, bool probe_container_joc(const std::string& ffmpeg_path, const std::string& path, unsigned audio_index, bool* joc, std::string* detail); +// ---- forced int24 delivery ------------------------------------------------ +// +// The core's own int24 rule, reproduced byte for byte from +// kernel/src/io/wav_writer.cpp: clip to [-1, 1], multiply by 8388607, truncate +// toward zero (the core's comment: the int32 conversion is "identical to NumPy's +// float32 -> int32 cast after clipping"), write the low three bytes, little +// endian; a non-finite sample becomes 0, exactly as the core writes it. +// +// Sample values that have been through this are what foobar2000's int24 import +// gives back, i.e. n / 2^23 (SDK/pfc audio_math.cpp:_import24). That is the scale +// a caller should use when it wants to check what the host received. +constexpr double kInt24ImportScale = 8388608.0; + +struct Int24Block { + std::size_t clipped = 0; // samples that were outside [-1, 1] + std::int32_t peak = 0; // largest magnitude actually written +}; + +// Packs `count` interleaved float32 samples into `out`, which must hold +// count * 3 bytes, and reports what the clip caught. +Int24Block pack_int24(const float* interleaved, std::size_t count, std::uint8_t* out); + class Engine { public: Engine(); diff --git a/src/prefs.cpp b/src/prefs.cpp index e13427d..adf1800 100644 --- a/src/prefs.cpp +++ b/src/prefs.cpp @@ -214,7 +214,7 @@ private: notify(); return TRUE; } - if (id == IDC_CHECK_GAIN) { + if (id == IDC_CHECK_GAIN || id == IDC_CHECK_INT24) { update_enabled_state(); notify(); return TRUE; @@ -301,6 +301,8 @@ private: SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_GAIN), BM_SETCHECK, values.gain_enabled ? BST_CHECKED : BST_UNCHECKED, 0); + SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_INT24), BM_SETCHECK, + values.force_int24 ? BST_CHECKED : BST_UNCHECKED, 0); update_enabled_state(); } @@ -328,6 +330,8 @@ private: values.gain_db = std::atof(get_text(GetDlgItem(m_hwnd, IDC_EDIT_GAIN)).c_str()); values.gain_enabled = IsDlgButtonChecked(m_hwnd, IDC_CHECK_GAIN) == BST_CHECKED; + values.force_int24 = + IsDlgButtonChecked(m_hwnd, IDC_CHECK_INT24) == BST_CHECKED; // Advanced values are not on this page; keep whatever is stored. const joc_settings::Values stored = joc_settings::read(); values.object_delay_samples = stored.object_delay_samples; @@ -354,6 +358,18 @@ private: } set_text(GetDlgItem(m_hwnd, IDC_LABEL_OUTPUT_STATUS), output_status); + // The hint says what the switch does and what the other state means: the + // renderer is float32 either way, so "24bit" is about the delivery, and + // that is not something a checkbox label can carry on its own. The second + // half matters because this switch changes what the component *reports* + // about a file, and foobar2000 caches that per file: an item whose + // information was read before the switch was flipped keeps the old format + // until it is reloaded. + set_text(GetDlgItem(m_hwnd, IDC_LABEL_INT24_HINT), + values.force_int24 + ? "开启:量化到 int24,超出 ±1 截断;切换后需刷新文件信息" + : "关闭:按 32 位浮点交付(默认)"); + joc_decode::Settings effective = joc_settings::current(); effective.hrtf_source = static_cast(values.hrtf_source); @@ -383,6 +399,7 @@ private: if (now.hrtf_file != m_initial.hrtf_file) return true; if (now.binaural_mode != m_initial.binaural_mode) return true; if (now.gain_enabled != m_initial.gain_enabled) return true; + if (now.force_int24 != m_initial.force_int24) return true; if (now.gain_db != m_initial.gain_db) return true; if (now.tail_seconds != m_initial.tail_seconds) return true; if (now.ffmpeg_path != m_initial.ffmpeg_path) return true; diff --git a/src/prefs.rc b/src/prefs.rc index 8ce17c8..1229702 100644 --- a/src/prefs.rc +++ b/src/prefs.rc @@ -7,11 +7,11 @@ // sample page uses (SDK\foobar2000\foo_sample\foo_sample.rc). A CAPTION statement // implies WS_CAPTION, which would draw a second frame inside the host's and shift // every control down by the caption height -- visible but not clickable. -IDD_JOC_PREFS DIALOGEX 0, 0, 330, 273 +IDD_JOC_PREFS DIALOGEX 0, 0, 330, 299 STYLE DS_SETFONT | WS_CHILD FONT 8, "Microsoft Sans Serif", 400, 0, 0x0 BEGIN - GROUPBOX "输出", IDC_GROUP_OUTPUT, 7, 5, 316, 61 + GROUPBOX "输出", IDC_GROUP_OUTPUT, 7, 5, 316, 86 CONTROL "双耳(HRTF)", IDC_RADIO_BINAURAL, "Button", BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 20, 72, 10 CONTROL "扬声器布局", IDC_RADIO_SPEAKER, "Button", @@ -25,30 +25,33 @@ BEGIN LTEXT "房间尾音(秒)", IDC_LABEL_TAIL, 172, 41, 62, 10 EDITTEXT IDC_EDIT_TAIL, 238, 38, 40, 13, ES_AUTOHSCROLL LTEXT "", IDC_LABEL_OUTPUT_STATUS, 16, 52, 300, 10 + CONTROL "强制 24bit 输出(int24)", IDC_CHECK_INT24, "Button", + BS_AUTOCHECKBOX | WS_TABSTOP, 16, 64, 116, 10 + LTEXT "", IDC_LABEL_INT24_HINT, 16, 76, 300, 10 - GROUPBOX "HRTF", IDC_GROUP_HRTF, 7, 71, 316, 100 + GROUPBOX "HRTF", IDC_GROUP_HRTF, 7, 97, 316, 100 CONTROL "SOFA 文件", IDC_RADIO_HRTF_SOFA, "Button", - BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 86, 56, 10 + BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, 16, 112, 56, 10 CONTROL "Rosella 个性化模型", IDC_RADIO_HRTF_ROSELLA, "Button", - BS_AUTORADIOBUTTON | WS_TABSTOP, 78, 86, 78, 10 - LTEXT "HRTF 文件(留空=默认位置)", IDC_LABEL_HRTF, 16, 105, 106, 10 - EDITTEXT IDC_EDIT_HRTF, 126, 102, 148, 13, ES_AUTOHSCROLL - PUSHBUTTON "浏览…", IDC_BROWSE_HRTF, 279, 102, 38, 13 + BS_AUTORADIOBUTTON | WS_TABSTOP, 78, 112, 78, 10 + LTEXT "HRTF 文件(留空=默认位置)", IDC_LABEL_HRTF, 16, 131, 106, 10 + EDITTEXT IDC_EDIT_HRTF, 126, 128, 148, 13, ES_AUTOHSCROLL + PUSHBUTTON "浏览…", IDC_BROWSE_HRTF, 279, 128, 38, 13 LTEXT "留空时读组件目录下 HRTF\\binaural.sofa 或 binaural.personalized_headphone;SOFA 由渲染器内部编译,滤波组表已编入库中。", - IDC_LABEL_HRTF_HINT, 16, 121, 300, 20 - LTEXT "", IDC_LABEL_STATUS, 16, 145, 300, 20 + IDC_LABEL_HRTF_HINT, 16, 147, 300, 20 + LTEXT "", IDC_LABEL_STATUS, 16, 171, 300, 20 - GROUPBOX "增益", IDC_GROUP_GAIN, 7, 176, 316, 48 + GROUPBOX "增益", IDC_GROUP_GAIN, 7, 202, 316, 48 CONTROL "应用增益", IDC_CHECK_GAIN, "Button", - BS_AUTOCHECKBOX | WS_TABSTOP, 16, 192, 58, 10 - LTEXT "dB", IDC_LABEL_GAIN, 80, 192, 12, 10 - EDITTEXT IDC_EDIT_GAIN, 96, 189, 40, 13, ES_AUTOHSCROLL - LTEXT "", IDC_LABEL_GAIN_STATUS, 145, 192, 171, 10 + BS_AUTOCHECKBOX | WS_TABSTOP, 16, 218, 58, 10 + LTEXT "dB", IDC_LABEL_GAIN, 80, 218, 12, 10 + EDITTEXT IDC_EDIT_GAIN, 96, 215, 40, 13, ES_AUTOHSCROLL + LTEXT "", IDC_LABEL_GAIN_STATUS, 145, 218, 171, 10 LTEXT "双耳渲染后可能超过 0 dBFS,可用负值衰减。", - IDC_LABEL_GAIN_HINT, 16, 208, 300, 10 + IDC_LABEL_GAIN_HINT, 16, 234, 300, 10 - GROUPBOX "其它", IDC_GROUP_MISC, 7, 229, 316, 38 - LTEXT "ffmpeg.exe", IDC_LABEL_FFMPEG, 16, 245, 44, 10 - EDITTEXT IDC_EDIT_FFMPEG, 64, 242, 210, 13, ES_AUTOHSCROLL - PUSHBUTTON "浏览…", IDC_BROWSE_FFMPEG, 279, 242, 38, 13 + GROUPBOX "其它", IDC_GROUP_MISC, 7, 255, 316, 38 + LTEXT "ffmpeg.exe", IDC_LABEL_FFMPEG, 16, 271, 44, 10 + EDITTEXT IDC_EDIT_FFMPEG, 64, 268, 210, 13, ES_AUTOHSCROLL + PUSHBUTTON "浏览…", IDC_BROWSE_FFMPEG, 279, 268, 38, 13 END diff --git a/src/resource.h b/src/resource.h index 910bd44..1c1980f 100644 --- a/src/resource.h +++ b/src/resource.h @@ -13,6 +13,9 @@ #define IDC_LABEL_TAIL 2017 #define IDC_EDIT_TAIL 2018 #define IDC_LABEL_OUTPUT_STATUS 2019 +// Delivery format, in the same group: what the decoder hands to foobar2000. +#define IDC_CHECK_INT24 2050 +#define IDC_LABEL_INT24_HINT 2051 #define IDC_GROUP_HRTF 2020 #define IDC_RADIO_HRTF_SOFA 2021 diff --git a/src/settings.cpp b/src/settings.cpp index db90f56..c8ce551 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -27,6 +28,7 @@ constexpr GUID kGuidBinauralMode = {0x1a57c9e4, 0x83b6, 0x42f1, {0x9c, 0x50, 0x2 constexpr GUID kGuidGainEnabled = {0x74b1e5c8, 0x2a39, 0x4d16, {0x8f, 0x62, 0x0d, 0x51, 0xb7, 0x3c, 0x9a, 0x28}}; constexpr GUID kGuidGain = {0x6c04d8b2, 0xe75f, 0x4a39, {0x8d, 0x26, 0x51, 0xb3, 0xc8, 0x40, 0x9e, 0x27}}; constexpr GUID kGuidTail = {0x38f1b6a7, 0x0d24, 0x4e88, {0xb7, 0x95, 0x2c, 0x61, 0x4f, 0xd2, 0x08, 0xa3}}; +constexpr GUID kGuidForceInt24 = {0xd2c67f41, 0x8b53, 0x4a2e, {0x97, 0x61, 0x0f, 0xc8, 0x35, 0xba, 0x64, 0x12}}; constexpr GUID kGuidObjectDelay = {0x5b2e9c18, 0xa640, 0x4d7b, {0x92, 0x0e, 0x37, 0xc5, 0x81, 0x6b, 0x2a, 0x54}}; constexpr GUID kGuidThreads = {0xcb7a4015, 0x3f98, 0x4c62, {0xa0, 0x8d, 0x1b, 0x74, 0x29, 0xe6, 0x53, 0x0f}}; constexpr GUID kGuidFfmpeg = {0x0e9d37c2, 0x5a18, 0x4b40, {0xb6, 0x3f, 0x88, 0x2d, 0x9a, 0x70, 0x1c, 0x65}}; @@ -42,10 +44,23 @@ cfg_bool g_gain_enabled(kGuidGainEnabled, true); cfg_int g_gain_mdb(kGuidGain, 0); // milli-seconds cfg_uint g_tail_ms(kGuidTail, 5000); +// Deliver int24 instead of float32. +cfg_bool g_force_int24(kGuidForceInt24, false); cfg_uint g_object_delay(kGuidObjectDelay, 1473); cfg_uint g_threads(kGuidThreads, 0); cfg_string g_ffmpeg(kGuidFfmpeg, "ffmpeg"); +// Appends an optional trailing field to one of the fixed describe() buffers, so a +// setting that is off leaves the line exactly as it was. +template +void append_to(char (&buffer)[N], const char* text) { + if (text == nullptr || *text == '\0') return; + const std::size_t used = std::strlen(buffer); + const std::size_t extra = std::strlen(text); + if (used + extra + 1u > N) return; + std::memcpy(buffer + used, text, extra + 1u); +} + std::string environment(const char* name) { char buffer[4096] = {}; const DWORD length = GetEnvironmentVariableA(name, buffer, sizeof(buffer)); @@ -108,6 +123,7 @@ Values read() { values.gain_enabled = static_cast(g_gain_enabled); values.gain_db = static_cast(static_cast(g_gain_mdb)) / 1000.0; values.tail_seconds = static_cast(static_cast(g_tail_ms)) / 1000.0; + values.force_int24 = static_cast(g_force_int24); values.object_delay_samples = static_cast(g_object_delay); values.native_threads = static_cast(g_threads); values.ffmpeg_path = g_ffmpeg.get_ptr(); @@ -124,6 +140,7 @@ void write(const Values& values) { g_gain_enabled = values.gain_enabled; g_gain_mdb = static_cast(values.gain_db * 1000.0 + (values.gain_db >= 0 ? 0.5 : -0.5)); g_tail_ms = static_cast(values.tail_seconds * 1000.0 + 0.5); + g_force_int24 = values.force_int24; g_object_delay = values.object_delay_samples; g_threads = values.native_threads; g_ffmpeg = values.ffmpeg_path.c_str(); @@ -146,6 +163,7 @@ joc_decode::Settings current() { (void)env_double("JOC_GAIN_DB", &values.gain_db); if (env_string("JOC_GAIN_ENABLED", &text)) values.gain_enabled = (text != "0"); (void)env_double("JOC_TAIL_SECONDS", &values.tail_seconds); + if (env_string("JOC_FORCE_INT24", &text)) values.force_int24 = (text != "0"); (void)env_uint("JOC_OBJECT_DELAY", &values.object_delay_samples); (void)env_uint("JOC_THREADS", &values.native_threads); @@ -161,6 +179,7 @@ joc_decode::Settings current() { // attenuation has to be visible and switchable. settings.gain_db = values.gain_enabled ? values.gain_db : 0.0; settings.tail_seconds = values.tail_seconds; + settings.force_int24 = values.force_int24; settings.object_delay_samples = values.object_delay_samples; settings.native_threads = values.native_threads; settings.ffmpeg_path = values.ffmpeg_path.empty() ? "ffmpeg" : values.ffmpeg_path; @@ -187,6 +206,9 @@ std::string describe(const joc_decode::Settings& settings) { settings.object_delay_samples, settings.native_threads, settings.ffmpeg_path.c_str()); } + // Stated only when it is not the default, so a default log line reads as it + // always did. + append_to(text, settings.force_int24 ? ", int24=forced" : ""); return text; } @@ -200,6 +222,7 @@ std::string describe(const Values& values) { values.binaural_mode, values.gain_enabled ? "" : "(off) ", values.gain_db, values.tail_seconds, values.object_delay_samples, values.native_threads, values.ffmpeg_path.c_str()); + append_to(text, values.force_int24 ? " int24=forced" : ""); return text; } diff --git a/src/settings.h b/src/settings.h index 3306a0c..82a4fae 100644 --- a/src/settings.h +++ b/src/settings.h @@ -40,6 +40,8 @@ struct Values { bool gain_enabled = true; double gain_db = 0.0; double tail_seconds = 5.0; + // Deliver int24 instead of float32; see joc_decode::Settings::force_int24. + bool force_int24 = false; unsigned object_delay_samples = 1473; unsigned native_threads = 0; std::string ffmpeg_path = "ffmpeg"; diff --git a/tests/prefs_layout_check.cpp b/tests/prefs_layout_check.cpp index a68f635..7139438 100644 --- a/tests/prefs_layout_check.cpp +++ b/tests/prefs_layout_check.cpp @@ -94,12 +94,23 @@ bool is_descendant(HWND candidate, HWND ancestor) { // A control the user can operate: static text and group boxes answer WM_NCHITTEST // with HTTRANSPARENT, so a real click passes straight through them. +// +// BS_GROUPBOX is 0x0007, whose bits are a subset of what BS_AUTOCHECKBOX (0x0003) +// and BS_AUTORADIOBUTTON (0x0009) set, so testing the style against BS_GROUPBOX +// alone classifies every checkbox and radio button as a group box -- and then +// exempts exactly the controls this tool exists to check. BS_TYPEMASK is what +// tells the button styles apart. +bool is_group_box_window(HWND window) { + if (class_of(window) != "Button") return false; + const LONG_PTR style = GetWindowLongPtrW(window, GWL_STYLE); + return (style & BS_TYPEMASK) == BS_GROUPBOX; +} + bool is_interactive(HWND window) { const std::string window_class = class_of(window); if (window_class == "Edit" || window_class == "ComboBox") return true; if (window_class != "Button") return false; - const LONG_PTR style = GetWindowLongPtrW(window, GWL_STYLE); - return (style & BS_GROUPBOX) == 0; + return !is_group_box_window(window); } // True when another interactive sibling sits above the control at that point -- @@ -209,11 +220,9 @@ int main(int argc, char** argv) { // Only controls the user is meant to operate count as problems: static // text and group boxes are transparent to the mouse by design. - const LONG_PTR control_style = GetWindowLongPtrW(control.window, GWL_STYLE); - const bool is_group_box = - control.window_class == "Button" && (control_style & BS_GROUPBOX) != 0; - const bool is_label = control.window_class == "Static" && !is_group_box; - const bool interactive = !is_group_box && !is_label; + const bool group_box = is_group_box_window(control.window); + const bool is_label = control.window_class == "Static"; + const bool interactive = !group_box && !is_label; const bool bad = interactive && (!enabled || !inside || !hits_self); if (bad) ++problems; diff --git a/tests/render_harness.cpp b/tests/render_harness.cpp index 42c97ec..388f84e 100644 --- a/tests/render_harness.cpp +++ b/tests/render_harness.cpp @@ -4,6 +4,7 @@ // // render_harness --input --output [options] // --mode binaural|speaker default binaural +// --format float32|int24 delivered sample format (default float32) // --layout NAME speaker layout (default 7.1) // --hrtf-source sofa|rosella kind of model --hrtf points at (default sofa) // --hrtf PATH HRTF to render with; supplied by the caller and @@ -36,8 +37,14 @@ // It exists because the plugin's engine (src/joc_decode.*) has no foobar2000 // dependency: the very code that plays in foobar2000 can be run here, against the // reference renderer or against the same engine's continuous render. The WAV header -// mirrors the renderer's own writer: a simple IEEE-float header up to two channels, -// WAVE_FORMAT_EXTENSIBLE above that with a channel mask of zero. +// mirrors the renderer's own writer: a simple header up to two channels, +// WAVE_FORMAT_EXTENSIBLE above that with a channel mask of zero -- for float32 +// (IEEE_FLOAT) and for int24 (PCM) alike. +// +// --format int24 is the byte-for-byte counterpart of the CLI's +// --speaker-format/--binaural-format int24: the component renders float32 and the +// int24 packing is the core's own rule (src/joc_decode.h:pack_int24), so the file +// this writes is directly comparable with the one the reference writes. #include #include @@ -54,6 +61,7 @@ struct Options { std::string input; std::string output; std::string mode = "binaural"; + std::string format = "float32"; // float32 | int24 std::string layout = "7.1"; std::string hrtf; // SOFA file, or Rosella model file std::string hrtf_source = "sofa"; // sofa | rosella @@ -83,33 +91,37 @@ void put_u32(std::string* out, unsigned long long value) { for (int i = 0; i < 4; ++i) out->push_back(static_cast((value >> (8 * i)) & 0xFFu)); } -// Same bytes the core's wav_writer produces. -std::string wav_header(unsigned channels, unsigned rate, std::uint64_t frames) { - const unsigned bytes_per_sample = 4; +// Same bytes the core's wav_writer produces, for either format it can write. +std::string wav_header(unsigned channels, unsigned rate, std::uint64_t frames, bool int24) { + const unsigned bytes_per_sample = int24 ? 3u : 4u; + const unsigned bits_per_sample = 8u * bytes_per_sample; const unsigned block_align = channels * bytes_per_sample; const std::uint64_t data_bytes = frames * block_align; std::string fmt; if (channels <= 2) { - put_u16(&fmt, 3); // WAVE_FORMAT_IEEE_FLOAT + put_u16(&fmt, int24 ? 0x0001u : 0x0003u); // WAVE_FORMAT_PCM / IEEE_FLOAT put_u16(&fmt, channels); put_u32(&fmt, rate); put_u32(&fmt, rate * block_align); put_u16(&fmt, block_align); - put_u16(&fmt, 8 * bytes_per_sample); + put_u16(&fmt, bits_per_sample); } else { put_u16(&fmt, 0xFFFE); // WAVE_FORMAT_EXTENSIBLE put_u16(&fmt, channels); put_u32(&fmt, rate); put_u32(&fmt, rate * block_align); put_u16(&fmt, block_align); - put_u16(&fmt, 8 * bytes_per_sample); + put_u16(&fmt, bits_per_sample); put_u16(&fmt, 22); // cbSize - put_u16(&fmt, 8 * bytes_per_sample); + put_u16(&fmt, bits_per_sample); put_u32(&fmt, 0); // channel mask, as the core writes it static const unsigned char kFloatGuid[16] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71}; - fmt.append(reinterpret_cast(kFloatGuid), 16); + static const unsigned char kPcmGuid[16] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71}; + const unsigned char* guid = int24 ? kPcmGuid : kFloatGuid; + fmt.append(reinterpret_cast(guid), 16); } std::string header; @@ -131,6 +143,7 @@ bool parse(int argc, char** argv, Options* options) { if (arg == "--input") options->input = next(); else if (arg == "--output") options->output = next(); else if (arg == "--mode") options->mode = next(); + else if (arg == "--format") options->format = next(); else if (arg == "--layout") options->layout = next(); else if (arg == "--hrtf") options->hrtf = next(); else if (arg == "--hrtf-source") options->hrtf_source = next(); @@ -165,6 +178,10 @@ bool parse(int argc, char** argv, Options* options) { return false; } } + if (options->format != "float32" && options->format != "int24") { + std::fprintf(stderr, "render_harness: --format wants float32 or int24\n"); + return false; + } return !options->input.empty() && !options->output.empty(); } @@ -190,13 +207,18 @@ bool render_once(const joc_decode::Settings& settings, const Options& options, return false; } // The header carries the length, so write a placeholder and come back to it. - const std::string header = wav_header(channels, 48000, 0); + const bool int24 = settings.force_int24; + const std::string header = wav_header(channels, 48000, 0, int24); std::fwrite(header.data(), 1, header.size(), file); constexpr std::size_t kChunk = 4096; std::vector buffer(kChunk * channels); + std::vector packed; std::uint64_t frames_written = 0; double peak = 0.0; + // What the int24 packing wrote, so the run reports the grid it delivered. + std::uint64_t clipped_samples = 0; + std::int32_t grid_peak = 0; std::vector applied(options.seeks_at.size(), 0); for (;;) { for (std::size_t i = 0; i < options.seeks_at.size(); ++i) { @@ -226,19 +248,34 @@ bool render_once(const joc_decode::Settings& settings, const Options& options, : static_cast(buffer[i]); if (value > peak) peak = value; } - std::fwrite(buffer.data(), sizeof(float), frames * channels, file); + if (int24) { + packed.resize(frames * channels * 3u); + const joc_decode::Int24Block block = + joc_decode::pack_int24(buffer.data(), frames * channels, packed.data()); + clipped_samples += block.clipped; + if (block.peak > grid_peak) grid_peak = block.peak; + std::fwrite(packed.data(), 1, packed.size(), file); + } else { + std::fwrite(buffer.data(), sizeof(float), frames * channels, file); + } frames_written += frames; } engine.stop(); - const std::string final_header = wav_header(channels, 48000, frames_written); + const std::string final_header = wav_header(channels, 48000, frames_written, int24); std::fseek(file, 0, SEEK_SET); std::fwrite(final_header.data(), 1, final_header.size(), file); std::fclose(file); std::printf("render_harness: %s -> %s\n", options.input.c_str(), output.c_str()); - std::printf(" channels=%u frames=%llu peak=%.9f\n", channels, - static_cast(frames_written), peak); + std::printf(" channels=%u frames=%llu format=%s peak=%.9f\n", channels, + static_cast(frames_written), + int24 ? "int24" : "float32", peak); + if (int24) { + std::printf(" int24 grid_peak=%.9f clipped=%llu\n", + static_cast(grid_peak) / joc_decode::kInt24ImportScale, + static_cast(clipped_samples)); + } return true; } @@ -253,7 +290,8 @@ int main(int argc, char** argv) { if (!parse(argc, argv, &options)) { std::fprintf(stderr, "usage: render_harness --input --output [--mode binaural|speaker]\n" - " [--layout NAME] [--hrtf-source sofa|rosella] [--hrtf PATH]\n" + " [--format float32|int24] [--layout NAME]\n" + " [--hrtf-source sofa|rosella] [--hrtf PATH]\n" " [--gain-db X] [--tail S] [--ffmpeg path] [--max-frames N]\n" " [--max-input-frames N] [--seek-to S] [--seek-at N:S]\n" " [--total-seconds S] [--pipeline-delay N] [--preroll-frames N]\n" @@ -279,6 +317,8 @@ int main(int argc, char** argv) { settings.input_kind = options.container ? joc_decode::InputKind::kContainer : joc_decode::InputKind::kBare; settings.audio_index = options.audio_index; + // The engine always renders float32; the flag is what the writer above packs. + settings.force_int24 = (options.format == "int24"); std::string error; if (options.parallel != 0) {