diff --git a/README.en.md b/README.en.md index efab57d..0d371b9 100644 --- a/README.en.md +++ b/README.en.md @@ -62,9 +62,8 @@ Preferences → Tools → **JOC decoder**: * **Output** — **Binaural (HRTF)**, or a **Speaker layout** from 2.0 to 9.1.6 (names and channel order in the [table below](#speaker-layouts-and-channel-order)); * **Binaural mode** (near / mid / far) and the room **tail** in seconds; -* **HRTF** — a **SOFA file** or a **Rosella personalized model**. An empty - path means the default location, `\HRTF\binaural.sofa` or - `binaural.personalized_headphone`; +* **HRTF** — a **Rosella personalized model**. An empty path means the default + location, `\HRTF\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 @@ -76,6 +75,13 @@ Preferences → Tools → **JOC decoder**: 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. +* **Swap side and rear channels** — off by default. When on, `Ls` and `Lrs`, and + `Rs` and `Rrs`, are exchanged just before the PCM is handed over (5↔7 and 6↔8 + counting from one in 7.1). It only applies to layouts that carry both a side and + a rear pair — 7.1, 7.1.2, 7.1.4, 9.1.4 and 9.1.6 — so the top channels in the + last two slots of 5.1.2 and 5.1.4 are never touched. Only the delivered channel + order changes; decoding, object positions and the layout table are untouched, + and both the float32 and the int24 delivery carry the swap. * the **ffmpeg.exe** executable to use. * **Language** — **Follow system** / **简体中文** / **English**. Every string on the page, the status lines and the errors a user can see use the selected language, and switching takes effect diff --git a/README.md b/README.md index 48a8782..0bfedc2 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,8 @@ Preferences → Tools → **JOC 解码器**: * **输出** —— **双耳(HRTF)**,或 **扬声器布局**:2.0 到 9.1.6,布局名与声道顺序见[下表](#扬声器布局与声道顺序); * **双耳模式**(near / mid / far)与房间**尾音**秒数; -* **HRTF** —— **SOFA 文件**或 **Rosella 个性化模型**。路径留空表示 - 用默认位置 `<组件目录>\HRTF\` 下的 `binaural.sofa` 或 `binaural.personalized_headphone`; +* **HRTF** —— **Rosella 个性化模型**。路径留空表示用默认位置 + `<组件目录>\HRTFinaural.personalized_headphone`; * **增益** —— 开关加 dB 值。双耳渲染在核心混音不削顶的素材上也可能超过满刻度,衰减放在这里; * **强制 24bit 输出(int24)** —— 默认关闭,交付 float32。打开后,交付前按内核写 WAV 的 int24 规则量化(截到 ±1 → ×8388607 → 截断),超范围的样本被截断到边缘、次数记进日志;组件同时把该 @@ -63,6 +63,10 @@ Preferences → Tools → **JOC 解码器**: 不是输出设备的位深(那个在 Preferences → Output 里设置)。**切换这个开关后要刷新文件信息** (Properties → Reload info,或把文件从媒体库移除后重新加入):foobar2000 按文件缓存技术信息, 缓存没刷新时 Properties 与转换器仍按旧的 32bit 浮点判断,会提示"编码器不支持浮点输入"。 +* **交换侧环绕与后环绕声道** —— 默认关闭。打开后,交付前把 `Ls` 与 `Lrs`、`Rs` 与 `Rrs` + 对调(7.1 里是 5↔7、6↔8,从 1 数)。只在这种布局同时有侧环绕和后环绕时生效: + 7.1、7.1.2、7.1.4、9.1.4、9.1.6;5.1.2/5.1.4 的后两个槽位是天空声道,不会被误换。 + 它只改交付 PCM 的声道顺序,不动解码、对象位置与布局定义,float32 与 int24 交付都生效。 * **ffmpeg.exe** 可执行文件路径。 * **界面语言** —— **跟随系统** / **简体中文** / **English**。设置页的全部文字、状态行和用户可见的 错误信息都用这一项选定的语言,切换后立刻生效;选择存在组件自己的配置里。"跟随系统"按 Windows 的 diff --git a/src/input_joc.cpp b/src/input_joc.cpp index 4155581..7398e23 100644 --- a/src/input_joc.cpp +++ b/src/input_joc.cpp @@ -495,6 +495,13 @@ public: if (m_channels == 0) m_channels = 2; // Only the map the component can describe is declared; see channel_map_for(). m_channel_config = channel_map_for(m_channels); + // The swap is a delivery-time reorder of the renderer's own channels, so it + // only applies when the layout the core rendered is the one being delivered. + m_swap_channels = joc_decode::SideRearChannels(); + if (m_settings.swap_side_rear && m_settings.output == joc_decode::Output::kSpeaker && + joc_decode::speaker_channels(m_settings.speaker_layout) == m_channels) { + m_swap_channels = joc_decode::side_rear_channels(m_settings.speaker_layout); + } m_buffer.resize(kRunFrames * m_channels); m_frames_delivered = 0; m_reported = false; @@ -541,6 +548,9 @@ public: // 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. + if (m_swap_channels.complete()) { + joc_decode::swap_side_rear(m_buffer.data(), frames, m_channels, m_swap_channels); + } const float* samples = m_buffer.data(); double expected_delivered_peak = 0.0; @@ -703,6 +713,9 @@ private: std::uint64_t m_clipped_samples = 0; bool m_clip_reported = false; unsigned m_channels = 2; + // Set at open() when the layout has both a side and a rear pair and the + // setting is on; empty otherwise, which makes the swap a no-op. + joc_decode::SideRearChannels m_swap_channels; std::uint64_t m_frames_delivered = 0; bool m_reported = false; // Chunks whose delivered samples did not match what the renderer produced. diff --git a/src/joc_decode.cpp b/src/joc_decode.cpp index 2d403e5..905a8df 100644 --- a/src/joc_decode.cpp +++ b/src/joc_decode.cpp @@ -2,11 +2,13 @@ #include +#include #include #include #include #include #include +#include #include // The kernel copy that is compiled into this component; see kernel/. @@ -387,6 +389,39 @@ unsigned speaker_channels(const std::string& layout) { return spec != nullptr ? spec->channels : 0; } +static int channel_index(const LayoutSpec& spec, const char* name) { + for (unsigned i = 0; i < spec.channels; ++i) { + if (std::strcmp(spec.speakers[i], name) == 0) return static_cast(i); + } + return -1; +} + +SideRearChannels side_rear_channels(const std::string& layout) { + SideRearChannels map; + const LayoutSpec* spec = find_layout(layout); + if (spec == nullptr) return map; + map.left_side = channel_index(*spec, "Ls"); + map.right_side = channel_index(*spec, "Rs"); + map.left_rear = channel_index(*spec, "Lrs"); + map.right_rear = channel_index(*spec, "Rrs"); + return map; +} + +void swap_side_rear(float* interleaved, std::size_t frames, unsigned channels, + const SideRearChannels& map) { + if (interleaved == nullptr || channels == 0 || !map.complete()) return; + int highest = map.left_side; + if (map.right_side > highest) highest = map.right_side; + if (map.left_rear > highest) highest = map.left_rear; + if (map.right_rear > highest) highest = map.right_rear; + if (static_cast(highest) >= channels) return; + for (std::size_t frame = 0; frame < frames; ++frame) { + float* block = interleaved + frame * channels; + std::swap(block[map.left_side], block[map.left_rear]); + std::swap(block[map.right_side], block[map.right_rear]); + } +} + namespace { // The core's conversion, reproduced: kernel/src/io/wav_writer.cpp:to_int32. diff --git a/src/joc_decode.h b/src/joc_decode.h index 6a7111f..69d11db 100644 --- a/src/joc_decode.h +++ b/src/joc_decode.h @@ -91,6 +91,10 @@ struct Settings { // 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; + // Swap Ls with Lrs and Rs with Rrs in the delivered PCM. Only layouts that + // carry both the side and the rear pair are affected, so a 5.1.x layout whose + // rear slots hold top channels is left alone. + bool swap_side_rear = false; }; // ---- speaker delivery ------------------------------------------------------ @@ -125,6 +129,29 @@ const LayoutSpec* layout_table(std::size_t* count); // layouts the core accepts. unsigned speaker_channels(const std::string& layout); +// The channels a side/rear swap moves, as positions in the layout's own order. +// A layout without both pairs leaves them at -1, which makes the swap a no-op; +// the lookup is by name, so a layout that reuses a slot for something else (5.1.2 +// and 5.1.4 put top channels where 7.1 has the rear pair) is never touched. +struct SideRearChannels { + int left_side = -1; + int right_side = -1; + int left_rear = -1; + int right_rear = -1; + + bool complete() const { + return left_side >= 0 && right_side >= 0 && left_rear >= 0 && right_rear >= 0; + } +}; + +// Looks the layout up in the table above and locates its Ls/Rs and Lrs/Rrs. +SideRearChannels side_rear_channels(const std::string& layout); + +// Swaps Ls with Lrs and Rs with Rrs in place, `frames` blocks of `channels` +// interleaved float32 samples. Everything else in the block is untouched. +void swap_side_rear(float* interleaved, std::size_t frames, unsigned channels, + const SideRearChannels& map); + // Directory this component's DLL was loaded from. The default HRTF folder is // \HRTF, the same place the reference CLI looks next to its exe. std::string component_directory(); diff --git a/src/lang.cpp b/src/lang.cpp index efcebb0..ddac4ac 100644 --- a/src/lang.cpp +++ b/src/lang.cpp @@ -53,6 +53,8 @@ constexpr Entry kTexts[] = { {Text::kGainStatusOff, "增益关(输出不衰减)", "gain off (no attenuation)"}, {Text::kGainHint, "双耳渲染后可能超过 0 dBFS,可用负值衰减。", "Binaural rendering can exceed 0 dBFS; attenuate with a negative value."}, + {Text::kCheckSwapSideRear, "交换侧环绕与后环绕声道", + "Swap side and rear channels"}, {Text::kGroupOther, "其它", "Other"}, {Text::kLabelLanguage, "界面语言", "Language"}, {Text::kLabelFfmpeg, "ffmpeg.exe", "ffmpeg.exe"}, diff --git a/src/lang.h b/src/lang.h index b700cc2..99b08ed 100644 --- a/src/lang.h +++ b/src/lang.h @@ -49,6 +49,7 @@ enum class Text { kGainStatusOn, kGainStatusOff, kGainHint, + kCheckSwapSideRear, kGroupOther, kLabelLanguage, kLabelFfmpeg, diff --git a/src/prefs.cpp b/src/prefs.cpp index 891aaac..280beb6 100644 --- a/src/prefs.cpp +++ b/src/prefs.cpp @@ -284,7 +284,8 @@ private: notify(); return TRUE; } - if (id == IDC_CHECK_GAIN || id == IDC_CHECK_INT24) { + if (id == IDC_CHECK_GAIN || id == IDC_CHECK_INT24 || + id == IDC_CHECK_SWAP_SIDE_REAR) { update_status(); notify(); return TRUE; @@ -367,6 +368,7 @@ private: label(IDC_LABEL_MODE, joc_lang::Text::kLabelMode); label(IDC_LABEL_TAIL, joc_lang::Text::kLabelTail); label(IDC_CHECK_INT24, joc_lang::Text::kCheckInt24); + label(IDC_CHECK_SWAP_SIDE_REAR, joc_lang::Text::kCheckSwapSideRear); label(IDC_LABEL_INT24_HINT, joc_lang::Text::kInt24Hint); label(IDC_GROUP_HRTF, joc_lang::Text::kGroupHrtf); label(IDC_RADIO_HRTF_ROSELLA, joc_lang::Text::kRadioRosella); @@ -406,6 +408,8 @@ private: 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); + SendMessageW(GetDlgItem(m_hwnd, IDC_CHECK_SWAP_SIDE_REAR), BM_SETCHECK, + values.swap_side_rear ? BST_CHECKED : BST_UNCHECKED, 0); // The item lists are language-dependent, so they are built (in the stored // language) before the selections are restored, and the texts are applied @@ -457,6 +461,8 @@ private: IsDlgButtonChecked(m_hwnd, IDC_CHECK_GAIN) == BST_CHECKED; values.force_int24 = IsDlgButtonChecked(m_hwnd, IDC_CHECK_INT24) == BST_CHECKED; + values.swap_side_rear = + IsDlgButtonChecked(m_hwnd, IDC_CHECK_SWAP_SIDE_REAR) == BST_CHECKED; values.language = selected_language(); // Advanced values are not on this page; keep whatever is stored. const joc_settings::Values stored = joc_settings::read(); @@ -519,6 +525,7 @@ private: 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.swap_side_rear != m_initial.swap_side_rear) 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 692d967..6634b06 100644 --- a/src/prefs.rc +++ b/src/prefs.rc @@ -57,6 +57,8 @@ BEGIN IDC_LABEL_GAIN_HINT, 16, 202, 300, 10 GROUPBOX "其它", IDC_GROUP_MISC, 7, 219, 316, 48 + CONTROL "交换侧环绕与后环绕声道", IDC_CHECK_SWAP_SIDE_REAR, "Button", + BS_AUTOCHECKBOX | WS_TABSTOP, 176, 232, 140, 10 LTEXT "界面语言", IDC_LABEL_LANGUAGE, 16, 232, 34, 10 COMBOBOX IDC_COMBO_LANGUAGE, 52, 229, 110, 90, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP diff --git a/src/resource.h b/src/resource.h index 6a2c7e3..9ea31d2 100644 --- a/src/resource.h +++ b/src/resource.h @@ -19,6 +19,7 @@ #define IDC_GROUP_HRTF 2020 #define IDC_RADIO_HRTF_SOFA 2021 #define IDC_RADIO_HRTF_ROSELLA 2022 +#define IDC_CHECK_SWAP_SIDE_REAR 2023 #define IDC_LABEL_HRTF 2024 #define IDC_EDIT_HRTF 2025 #define IDC_BROWSE_HRTF 2026 diff --git a/src/settings.cpp b/src/settings.cpp index bfb3875..f547e75 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -29,6 +29,7 @@ constexpr GUID kGuidGainEnabled = {0x74b1e5c8, 0x2a39, 0x4d16, {0x8f, 0x62, 0x0d 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 kGuidSwapSideRear = {0x2b7f6d18, 0x59c4, 0x4a83, {0x8e, 0x15, 0x73, 0xac, 0x06, 0xd9, 0x4b, 0x27}}; 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}}; @@ -45,6 +46,8 @@ cfg_int g_gain_mdb(kGuidGain, 0); cfg_uint g_tail_ms(kGuidTail, 5000); // Deliver int24 instead of float32. cfg_bool g_force_int24(kGuidForceInt24, false); +// Swap the side and rear surround pairs in the delivered PCM. +cfg_bool g_swap_side_rear(kGuidSwapSideRear, false); cfg_uint g_object_delay(kGuidObjectDelay, 1473); cfg_uint g_threads(kGuidThreads, 0); cfg_string g_ffmpeg(kGuidFfmpeg, "ffmpeg"); @@ -124,6 +127,7 @@ Values read() { 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.swap_side_rear = static_cast(g_swap_side_rear); values.object_delay_samples = static_cast(g_object_delay); values.native_threads = static_cast(g_threads); values.ffmpeg_path = g_ffmpeg.get_ptr(); @@ -153,6 +157,7 @@ void write(const Values& values) { 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_swap_side_rear = values.swap_side_rear; g_object_delay = values.object_delay_samples; g_threads = values.native_threads; g_ffmpeg = values.ffmpeg_path.c_str(); @@ -174,6 +179,7 @@ joc_decode::Settings current() { 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"); + if (env_string("JOC_SWAP_SIDE_REAR", &text)) values.swap_side_rear = (text != "0"); (void)env_uint("JOC_OBJECT_DELAY", &values.object_delay_samples); (void)env_uint("JOC_THREADS", &values.native_threads); @@ -189,6 +195,7 @@ joc_decode::Settings current() { settings.gain_db = values.gain_enabled ? values.gain_db : 0.0; settings.tail_seconds = values.tail_seconds; settings.force_int24 = values.force_int24; + settings.swap_side_rear = values.swap_side_rear; 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; @@ -217,6 +224,7 @@ std::string describe(const joc_decode::Settings& settings) { // 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" : ""); + append_to(text, settings.swap_side_rear ? ", swap=side/rear" : ""); return text; } @@ -230,6 +238,7 @@ std::string describe(const Values& values) { values.tail_seconds, values.object_delay_samples, values.native_threads, values.ffmpeg_path.c_str()); append_to(text, values.force_int24 ? " int24=forced" : ""); + append_to(text, values.swap_side_rear ? " swap=side/rear" : ""); append_to(text, values.language == joc_lang::Language::kSystem ? " language=system" : (values.language == joc_lang::Language::kEnglish diff --git a/src/settings.h b/src/settings.h index bc67d55..087dcb9 100644 --- a/src/settings.h +++ b/src/settings.h @@ -33,6 +33,9 @@ struct Values { double tail_seconds = 5.0; // Deliver int24 instead of float32; see joc_decode::Settings::force_int24. bool force_int24 = false; + // Swap Ls with Lrs and Rs with Rrs in the delivered speaker PCM; a layout + // without both pairs is delivered unchanged. + bool swap_side_rear = false; unsigned object_delay_samples = 1473; unsigned native_threads = 0; std::string ffmpeg_path = "ffmpeg"; diff --git a/tests/channel_swap_test.cpp b/tests/channel_swap_test.cpp new file mode 100644 index 0000000..38856f3 --- /dev/null +++ b/tests/channel_swap_test.cpp @@ -0,0 +1,168 @@ +// Offline check of the side/rear channel swap the component applies to the PCM it +// hands to foobar2000. +// +// channel_swap_test +// +// Two things are checked, and neither needs a file or a render: +// +// * which layouts the swap touches. It has to be decided by the channel names +// the layout table gives, because 5.1.2 and 5.1.4 reuse the slots 7.1 keeps +// for the rear pair: a count-based rule would swap top channels there. +// * the reorder itself: exactly Ls<->Lrs and Rs<->Rrs, every other slot +// untouched, and the operation is its own inverse. + +#include +#include +#include +#include + +#include "../src/joc_decode.h" + +namespace { + +int g_failures = 0; + +void check(bool condition, const char* what, const std::string& detail) { + if (condition) return; + ++g_failures; + std::printf("FAIL %s: %s\n", what, detail.c_str()); +} + +// A block whose sample values are the channel index, so the reorder is readable. +std::vector make_block(unsigned channels, std::size_t frames) { + std::vector block(frames * channels); + for (std::size_t frame = 0; frame < frames; ++frame) { + for (unsigned channel = 0; channel < channels; ++channel) { + block[frame * channels + channel] = static_cast(channel + frame * 100); + } + } + return block; +} + +void expect_swappable(const char* layout, bool expected) { + const joc_decode::SideRearChannels map = joc_decode::side_rear_channels(layout); + check(map.complete() == expected, "side_rear_channels", layout); +} + +void expect_layout_indices(const char* layout, int ls, int rs, int lrs, int rrs) { + const joc_decode::SideRearChannels map = joc_decode::side_rear_channels(layout); + check(map.left_side == ls, "left side index", layout); + check(map.right_side == rs, "right side index", layout); + check(map.left_rear == lrs, "left rear index", layout); + check(map.right_rear == rrs, "right rear index", layout); +} + +void expect_swap(const char* layout, unsigned channels, std::size_t frames) { + const joc_decode::SideRearChannels map = joc_decode::side_rear_channels(layout); + check(map.complete(), "layout carries both pairs", layout); + if (!map.complete()) return; + + std::vector block = make_block(channels, frames); + joc_decode::swap_side_rear(block.data(), frames, channels, map); + for (std::size_t frame = 0; frame < frames; ++frame) { + for (unsigned channel = 0; channel < channels; ++channel) { + int expected = static_cast(channel) + static_cast(frame) * 100; + if (static_cast(channel) == map.left_side) expected = map.left_rear + static_cast(frame) * 100; + else if (static_cast(channel) == map.right_side) expected = map.right_rear + static_cast(frame) * 100; + else if (static_cast(channel) == map.left_rear) expected = map.left_side + static_cast(frame) * 100; + else if (static_cast(channel) == map.right_rear) expected = map.right_side + static_cast(frame) * 100; + const float actual = block[frame * channels + channel]; + check(actual == static_cast(expected), "swapped sample", layout); + } + } + + // Swapping twice restores the original order. + joc_decode::swap_side_rear(block.data(), frames, channels, map); + const std::vector original = make_block(channels, frames); + check(block == original, "swap is its own inverse", layout); +} + +// A layout without both pairs must come through untouched, including the 5.1.x +// ones whose last slots are top channels rather than a rear pair. +void expect_untouched(const char* layout, unsigned channels) { + const joc_decode::SideRearChannels map = joc_decode::side_rear_channels(layout); + check(!map.complete(), "layout has no side/rear pair", layout); + std::vector block = make_block(channels, 3); + joc_decode::swap_side_rear(block.data(), 3, channels, map); + check(block == make_block(channels, 3), "untouched", layout); +} + +// A caller that passes fewer channels than the map needs is left alone rather than +// reading past the block: the decoder only enables the swap when the counts agree, +// but the guard is cheap. +void expect_guard() { + const joc_decode::SideRearChannels map = joc_decode::side_rear_channels("7.1"); + std::vector block = make_block(2, 4); + joc_decode::swap_side_rear(block.data(), 4, 2, map); + check(block == make_block(2, 4), "narrow block untouched", "7.1 in 2 channels"); + joc_decode::swap_side_rear(nullptr, 4, 8, map); +} + +} // namespace + +int main() { + // Layouts with a real side pair and a real rear pair. + expect_swappable("7.1", true); + expect_swappable("7.1.2", true); + expect_swappable("7.1.4", true); + expect_swappable("9.1.4", true); + expect_swappable("9.1.6", true); + + // Stereo, 3.1, 4.0 and the 5.1 family have no rear pair to swap with. + expect_swappable("2.0", false); + expect_swappable("3.1", false); + expect_swappable("4.0", false); + expect_swappable("5.1", false); + expect_swappable("5.1.2", false); + expect_swappable("5.1.4", false); + // Unknown layouts are no-ops too. + expect_swappable("", false); + expect_swappable("7.1.8", false); + + // In the table's order the side pair is 4/5 and the rear pair 6/7. + expect_layout_indices("7.1", 4, 5, 6, 7); + expect_layout_indices("7.1.2", 4, 5, 6, 7); + expect_layout_indices("7.1.4", 4, 5, 6, 7); + expect_layout_indices("9.1.4", 4, 5, 6, 7); + expect_layout_indices("9.1.6", 4, 5, 6, 7); + + expect_swap("7.1", 8, 4); + expect_swap("7.1.2", 10, 2); + expect_swap("7.1.4", 12, 3); + expect_swap("9.1.4", 14, 2); + expect_swap("9.1.6", 16, 2); + + expect_untouched("5.1", 6); + expect_untouched("5.1.2", 8); + expect_untouched("5.1.4", 10); + expect_untouched("4.0", 4); + expect_guard(); + + // The delivery path packs the swapped block to int24 after the reorder, so the + // int24 output is checked as well: the bytes for the swapped block have to + // equal the bytes of a hand-permuted copy, i.e. the packing keeps the order. + { + const joc_decode::SideRearChannels map = joc_decode::side_rear_channels("7.1"); + std::vector block(8); + for (unsigned channel = 0; channel < 8; ++channel) { + block[channel] = static_cast(channel + 1) / 32.0f; + } + std::vector expected = block; + std::swap(expected[4], expected[6]); + std::swap(expected[5], expected[7]); + + joc_decode::swap_side_rear(block.data(), 1, 8, map); + std::vector packed(8 * 3); + std::vector reference(8 * 3); + joc_decode::pack_int24(block.data(), 8, packed.data()); + joc_decode::pack_int24(expected.data(), 8, reference.data()); + check(packed == reference, "int24 carries the swapped order", "7.1"); + } + + if (g_failures != 0) { + std::printf("%d check(s) failed\n", g_failures); + return 1; + } + std::printf("ok: side/rear swap covers 7.1 and 9.1 layouts only\n"); + return 0; +} diff --git a/tests/render_harness.cpp b/tests/render_harness.cpp index 388f84e..84d2df5 100644 --- a/tests/render_harness.cpp +++ b/tests/render_harness.cpp @@ -6,7 +6,6 @@ // --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 // never part of this repository. Without it the // default location beside the executable is used. @@ -63,8 +62,8 @@ struct Options { 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 + std::string hrtf; // Rosella model file + bool swap_side_rear = false; // --swap-side-rear std::string ffmpeg = "ffmpeg"; double gain_db = 0.0; double tail_seconds = 5.0; @@ -146,7 +145,7 @@ bool parse(int argc, char** argv, Options* options) { 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(); + else if (arg == "--swap-side-rear") options->swap_side_rear = true; else if (arg == "--ffmpeg") options->ffmpeg = next(); else if (arg == "--gain-db") options->gain_db = std::atof(next().c_str()); else if (arg == "--tail") options->tail_seconds = std::atof(next().c_str()); @@ -211,6 +210,16 @@ bool render_once(const joc_decode::Settings& settings, const Options& options, const std::string header = wav_header(channels, 48000, 0, int24); std::fwrite(header.data(), 1, header.size(), file); + // The same lookup the component does, so the harness exercises the delivery + // reorder with the layout's own channel positions. + const joc_decode::SideRearChannels swap_map = + options.swap_side_rear ? joc_decode::side_rear_channels(settings.speaker_layout) + : joc_decode::SideRearChannels(); + if (options.swap_side_rear && !swap_map.complete()) { + std::fprintf(stderr, "harness: --swap-side-rear has no effect on layout %s\n", + settings.speaker_layout.c_str()); + } + constexpr std::size_t kChunk = 4096; std::vector buffer(kChunk * channels); std::vector packed; @@ -236,6 +245,9 @@ bool render_once(const joc_decode::Settings& settings, const Options& options, if (left < want) want = static_cast(left); } const std::size_t frames = engine.read(buffer.data(), want, error); + if (frames != 0 && options.swap_side_rear) { + joc_decode::swap_side_rear(buffer.data(), frames, channels, swap_map); + } if (frames == 0) { if (!error->empty()) { std::fclose(file); @@ -291,7 +303,6 @@ int main(int argc, char** argv) { std::fprintf(stderr, "usage: render_harness --input --output [--mode binaural|speaker]\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" @@ -304,8 +315,6 @@ int main(int argc, char** argv) { settings.output = (options.mode == "speaker") ? joc_decode::Output::kSpeaker : joc_decode::Output::kBinaural; settings.speaker_layout = options.layout; - settings.hrtf_source = (options.hrtf_source == "rosella") ? joc_decode::HrtfSource::kRosella - : joc_decode::HrtfSource::kSofa; settings.hrtf_file = options.hrtf; settings.gain_db = options.gain_db; settings.tail_seconds = options.tail_seconds; diff --git a/tests/ui_text_fit_test.cpp b/tests/ui_text_fit_test.cpp index 9883cdc..617dd1f 100644 --- a/tests/ui_text_fit_test.cpp +++ b/tests/ui_text_fit_test.cpp @@ -121,9 +121,9 @@ const Binding kBindings[] = { {IDC_LABEL_MODE, joc_lang::Text::kLabelMode}, {IDC_LABEL_TAIL, joc_lang::Text::kLabelTail}, {IDC_CHECK_INT24, joc_lang::Text::kCheckInt24}, + {IDC_CHECK_SWAP_SIDE_REAR, joc_lang::Text::kCheckSwapSideRear}, {IDC_LABEL_INT24_HINT, joc_lang::Text::kInt24Hint}, {IDC_GROUP_HRTF, joc_lang::Text::kGroupHrtf}, - {IDC_RADIO_HRTF_SOFA, joc_lang::Text::kRadioSofa}, {IDC_RADIO_HRTF_ROSELLA, joc_lang::Text::kRadioRosella}, {IDC_LABEL_HRTF, joc_lang::Text::kLabelHrtfFile}, {IDC_BROWSE_HRTF, joc_lang::Text::kButtonBrowse}, diff --git a/tools/build_tests.ps1 b/tools/build_tests.ps1 index e282ae5..9141c01 100644 --- a/tools/build_tests.ps1 +++ b/tools/build_tests.ps1 @@ -47,6 +47,7 @@ $lines = @( "cl $common /Fe:`"$outDir\scan_crosscheck.exe`" /Fo:`"$outDir\\`" tests\scan_crosscheck.cpp src\eac3_scan.cpp `"$coreLib`" shell32.lib", "cl $common /Fe:`"$outDir\render_harness.exe`" /Fo:`"$outDir\\`" tests\render_harness.cpp src\joc_decode.cpp src\eac3_scan.cpp src\container_scan.cpp src\log.cpp `"$coreLib`" shell32.lib", "cl $common /Fe:`"$outDir\layout_table_test.exe`" /Fo:`"$outDir\\`" tests\layout_table_test.cpp src\joc_decode.cpp src\eac3_scan.cpp src\container_scan.cpp src\log.cpp `"$coreLib`" shell32.lib", + "cl $common /Fe:`"$outDir\channel_swap_test.exe`" /Fo:`"$outDir\\`" tests\channel_swap_test.cpp src\joc_decode.cpp src\eac3_scan.cpp src\container_scan.cpp src\log.cpp `"$coreLib`" shell32.lib", "cl $common /Fe:`"$outDir\ui_text_fit_test.exe`" /Fo:`"$outDir\\`" tests\ui_text_fit_test.cpp src\lang.cpp src\joc_decode.cpp src\eac3_scan.cpp src\container_scan.cpp src\log.cpp `"$coreLib`" shell32.lib user32.lib gdi32.lib", 'if errorlevel 1 exit /b 1', 'exit /b 0'