4 Commits
Author SHA1 Message Date
TheM14 3707ca2f8d Put the sky bed channels back on the room walls
ci / macos-latest (push) Waiting to run
ci / windows-latest (push) Waiting to run
ci / ubuntu-latest (push) Failing after 13s
- Set the X of Tfl/Tfr, Tsl/Tsr and Tbl/Tbr in the bed channel table back to
  -1 / +1 instead of -0.516113 / +0.516113; Y stays 1 / 0 / -1 and Z stays 1.
  The table now agrees with kTableB10 entry by entry.
- Keep LFE and LFE2 at the origin: those channels carry no direction, and the
  origin is the value the ADM and fixed-position paths already used.
- The horizontal pan only varies between the ends of a speaker row, and the top
  row of every output layout spans 0.241943 .. 0.758057, so the wall value still
  lands inside the full-power zone of the end speaker: decoded speaker output is
  unchanged, while the ADM position attribute and the binaural fixed directions
  follow the wall geometry again.
2026-11-18 19:51:31 +08:00
TheM14 6c03fd4d86 Bump joc_version to 1.3.4
ci / macos-latest (push) Waiting to run
ci / windows-latest (push) Waiting to run
ci / ubuntu-latest (push) Failing after 10s
2026-11-18 17:10:59 +08:00
TheM14 a8a40c2d20 Correct the bed channel positions, the bed anchor order and the 5.1 surrounds
ci / macos-latest (push) Waiting to run
ci / windows-latest (push) Waiting to run
ci / ubuntu-latest (push) Failing after 15s
- Put the height row of the bed channel table on the ADM 62-level grid:
  Tfl/Tfr, Tsl/Tsr and Tbl/Tbr take x = -0.516113 / 0.516113 (Q15 7928 /
  24840) with Y = 1 / 0 / -1 instead of the older (-0.5, 0.5, 1) values, and
  Lw/Rw take Y = 21/31 (Q15 5285) instead of 0.5; LFE and LFE2 take the same
  non-spatial entries as the ADM label table.
- Scale bed_channel_q15 by 32768 and round instead of truncating, so the
  half-step positions come out exact (16384, 7928, 24840, 5285).
- Anchor a bed channel to its same-named speaker by that channel's own index:
  channel_names shares the internal order of the region speaker ids, so the
  extra standard_from_internal lookup swapped the side/rear anchors of
  7.1.x/9.1.x and the top front/top back anchors of 9.1.6.
- Keep the single surround pair of 5.1, 5.1.2 and 5.1.4 at the rear corners
  (Y = 32767) instead of Y = 16384.
- Refresh the 5.1 gain expectations in the core tests for the moved surround
  pair; the new values match the Python reference bit for bit.
2026-11-18 17:05:52 +08:00
TheM14 6edcabf777 Bump to 1.3.3 and add PCM layout streaming input
ci / ubuntu-latest (push) Failing after 14s
ci / macos-latest (push) Canceled after 0s
ci / windows-latest (push) Canceled after 0s
ci / release (push) Canceled after 0s
- Add JOC_STREAM_IN_PCM_LAYOUT for interleaved 48 kHz float32 PCM with
  2.0/5.1/7.1 auto layout detection.
- Isolate PCM channel mapping and fixed object positions in the new
  virtual_speaker module; the stream only schedules it.
- Apply the master gain once inside virtual_speaker for the PCM path, while
  the E-AC-3/JOC path keeps its existing rebuild gain.
- Add the multichannel binaural CLI, rational resampler and float32 WAV I/O.
- Extend tests for the multichannel/resampling paths.
2026-11-12 14:31:43 +08:00
18 changed files with 1221 additions and 48 deletions

No files matched your search

+15
View File
@@ -96,6 +96,9 @@ set(JOC_INTERNAL_SOURCES
src/adm/adm_tracks.cpp
src/binaural/jochrtf_runtime.cpp
src/binaural/jochrtf_renderer.cpp
src/binaural/multichannel_layout.cpp
src/binaural/pcm_resampler.cpp
src/virtual_speaker/virtual_speaker.cpp
src/eac3_transport/eac3_reader.cpp
src/emdf/emdf_parser.cpp
src/foundation/bit_reader.cpp
@@ -116,6 +119,7 @@ set(JOC_INTERNAL_SOURCES
src/io/inflate.cpp
src/io/npy.cpp
src/io/npy_writer.cpp
src/io/wav_pcm.cpp
src/io/process.cpp
src/io/wav_writer.cpp
src/io/zip_reader.cpp
@@ -250,6 +254,17 @@ if(JOC_BUILD_TESTS OR JOC_BUILD_DEVTOOLS)
target_compile_definitions(joc_core_impl PUBLIC EJOC_STATIC)
endif()
if(JOC_BUILD_TESTS OR JOC_BUILD_DEVTOOLS)
add_executable(joc_multichannel src/cli/joc_multichannel.cpp)
target_link_libraries(joc_multichannel PRIVATE joc_core_impl)
target_include_directories(joc_multichannel PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
set_target_properties(joc_multichannel PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
if(WIN32)
target_link_libraries(joc_multichannel PRIVATE shell32)
endif()
endif()
add_library(joc_core SHARED
src/api/joc_api.cpp
src/api/joc_stream_api.cpp
+4 -1
View File
@@ -40,7 +40,8 @@ typedef struct joc_stream joc_stream;
typedef enum joc_stream_input {
JOC_STREAM_IN_EAC3 = 0, /* bare E-AC-3 syncframes (the metadata stream) */
JOC_STREAM_IN_PCM_OBJECTS16 = 1, /* objects16 frames, planar [16][1536] each */
JOC_STREAM_IN_CORE_PCM = 3 /* the 5.1 core PCM of the pushed E-AC-3 frames */
JOC_STREAM_IN_CORE_PCM = 3, /* the 5.1 core PCM of the pushed E-AC-3 frames */
JOC_STREAM_IN_PCM_LAYOUT = 4 /* interleaved 48k f32, mapped by pcm_layout */
} joc_stream_input;
typedef enum joc_stream_output {
@@ -68,6 +69,8 @@ typedef struct joc_stream_config {
* .jochrtf field (hrtf_path) or a Rosella .personalized_headphone model. */
const char* personalized_headphone_path; /* Rosella .personalized_headphone input */
uint32_t jochrtf_room; /* non-zero enables the project room */
/* PCM_LAYOUT input: "2.0", "5.1", "7.1", or NULL/"" to infer by channels. */
const char* pcm_layout;
} joc_stream_config;
typedef struct joc_stream_buffer {
+2 -2
View File
@@ -10,7 +10,7 @@
#define JOC_VERSION_MAJOR 1
#define JOC_VERSION_MINOR 3
#define JOC_VERSION_PATCH 2
#define JOC_VERSION_STRING "1.3.2"
#define JOC_VERSION_PATCH 4
#define JOC_VERSION_STRING "1.3.4"
#endif /* JOC_VERSION_H */
+14
View File
@@ -46,6 +46,7 @@ joc::stream::Config to_config(const joc_stream_config& config) {
if (config.rosella_filterbank != 0u) {
out.rosella_filterbank = config.rosella_filterbank;
}
if (config.pcm_layout != nullptr) { out.pcm_layout = config.pcm_layout; }
return out;
}
@@ -95,6 +96,19 @@ joc_error JOC_CALL joc_stream_push(joc_stream* stream, const joc_stream_buffer*
}
return pushed;
}
if (input->kind == JOC_STREAM_IN_PCM_LAYOUT) {
if (input->sample_rate != 0u && input->sample_rate != 48000u) {
return joc::Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender,
"PCM layout input must be 48 kHz");
}
std::size_t consumed = 0;
const joc::Status pushed = stream->instance.push_pcm_layout(
input->pcm, input->sample_count, input->channels, &consumed);
if (consumed_samples != nullptr) {
*consumed_samples = static_cast<std::uint32_t>(consumed);
}
return pushed;
}
std::size_t consumed = 0;
const std::uint32_t channels = input->channels != 0u ? input->channels : 6u;
const joc::Status pushed =
+106
View File
@@ -0,0 +1,106 @@
#include "binaural/multichannel_layout.h"
#include <cstring>
namespace joc::multichannel {
namespace {
constexpr ChannelSpec kStereo[] = {
{"L", false, {-1.0, 1.0, 0.0}},
{"R", false, {1.0, 1.0, 0.0}},
};
constexpr ChannelSpec kSurround51[] = {
{"L", false, {-1.0, 1.0, 0.0}},
{"R", false, {1.0, 1.0, 0.0}},
{"C", false, {0.0, 1.0, 0.0}},
{"LFE", true, {0.0, 1.0, 0.0}},
{"BL", false, {-1.0, -1.0, 0.0}},
{"BR", false, {1.0, -1.0, 0.0}},
};
constexpr ChannelSpec kSurround71[] = {
{"L", false, {-1.0, 1.0, 0.0}},
{"R", false, {1.0, 1.0, 0.0}},
{"C", false, {0.0, 1.0, 0.0}},
{"LFE", true, {0.0, 1.0, 0.0}},
{"BL", false, {-1.0, -1.0, 0.0}},
{"BR", false, {1.0, -1.0, 0.0}},
{"SL", false, {-1.0, 0.0, 0.0}},
{"SR", false, {1.0, 0.0, 0.0}},
};
constexpr Layout kLayouts[] = {
{"2.0", 2, kStereo},
{"5.1", 6, kSurround51},
{"7.1", 8, kSurround71},
};
} // namespace
const Layout* find_layout(const std::string& name) {
for (const Layout& layout : kLayouts) {
if (name == layout.name) {
return &layout;
}
}
return nullptr;
}
const Layout* detect_layout(int channels) {
for (const Layout& layout : kLayouts) {
if (layout.channels == channels) {
return &layout;
}
}
return nullptr;
}
Status object_positions(const Layout& layout, double positions[15][3]) {
if (positions == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kFoundation,
"object position output is null");
}
std::memset(positions, 0, sizeof(double) * 15 * 3);
int slot = 0;
for (int channel = 0; channel < layout.channels; ++channel) {
const ChannelSpec& spec = layout.specs[channel];
if (spec.lfe) {
continue;
}
if (slot >= 15) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kFoundation,
"layout needs more than 15 object channels");
}
for (int axis = 0; axis < 3; ++axis) {
positions[slot][axis] = spec.position[axis];
}
++slot;
}
return Status::success();
}
Status pcm_to_objects16(const float* interleaved, std::size_t frames, const Layout& layout,
float* objects16_planar) {
if (interleaved == nullptr || objects16_planar == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kFoundation,
"multichannel PCM buffers are null");
}
std::memset(objects16_planar, 0, sizeof(float) * frames * 16u);
int slot = 0;
for (int channel = 0; channel < layout.channels; ++channel) {
const ChannelSpec& spec = layout.specs[channel];
const int destination = spec.lfe ? 0 : 1 + slot;
for (std::size_t frame = 0; frame < frames; ++frame) {
objects16_planar[static_cast<std::size_t>(destination) * frames + frame] =
interleaved[frame * static_cast<std::size_t>(layout.channels) + channel];
}
if (!spec.lfe) {
++slot;
}
}
return Status::success();
}
} // namespace joc::multichannel
+39
View File
@@ -0,0 +1,39 @@
// Channel order and fixed ADM directions for the multichannel binaural path.
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include "foundation/status.h"
namespace joc::multichannel {
struct ChannelSpec {
const char* label;
bool lfe;
double position[3];
};
struct Layout {
const char* name;
int channels;
const ChannelSpec* specs;
};
// WAV channel order follows the ffmpeg definitions:
// 2.0 = FL FR
// 5.1 = FL FR FC LFE BL BR
// 7.1 = FL FR FC LFE BL BR SL SR
// BL/BR are the rear pair, SL/SR the side pair.
const Layout* find_layout(const std::string& name);
const Layout* detect_layout(int channels);
Status object_positions(const Layout& layout, double positions[15][3]);
// Maps interleaved layout PCM to the JOC source layout: channel 0 is LFE,
// channels 1..15 are objects. Unused object channels stay silent.
Status pcm_to_objects16(const float* interleaved, std::size_t frames, const Layout& layout,
float* objects16_planar);
} // namespace joc::multichannel
+101
View File
@@ -0,0 +1,101 @@
#include "binaural/pcm_resampler.h"
#include <algorithm>
#include <cmath>
namespace joc::multichannel {
namespace {
constexpr double kPi = 3.14159265358979323846264338327950288;
constexpr int kHalfTaps = 32;
double bessel_i0(double value) {
double sum = 1.0;
double term = 1.0;
const double half = value * 0.5;
for (int k = 1; k < 32; ++k) {
term *= (half / k) * (half / k);
sum += term;
if (term < sum * 1.0e-12) {
break;
}
}
return sum;
}
double kaiser_window(double x) {
constexpr double kBeta = 8.6;
const double ratio = std::max(0.0, 1.0 - (x / kHalfTaps) * (x / kHalfTaps));
return bessel_i0(kBeta * std::sqrt(ratio)) / bessel_i0(kBeta);
}
double sinc(double x) {
if (std::fabs(x) < 1.0e-12) {
return 1.0;
}
return std::sin(kPi * x) / (kPi * x);
}
} // namespace
Status resample_interleaved(const float* input, std::size_t frames, int channels,
std::uint32_t source_rate, std::uint32_t target_rate,
std::vector<float>* output) {
if (output == nullptr || channels <= 0 || frames == 0) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kFoundation,
"resampler needs samples, channels and an output vector");
}
if (source_rate == 0 || target_rate == 0) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kFoundation,
"resampler sample rates must be positive");
}
if (source_rate == target_rate) {
output->assign(input, input + frames * static_cast<std::size_t>(channels));
return Status::success();
}
const double step = static_cast<double>(source_rate) / static_cast<double>(target_rate);
const double cutoff = (target_rate < source_rate)
? static_cast<double>(target_rate) / source_rate
: 1.0;
const std::size_t output_frames = static_cast<std::size_t>(std::llround(
static_cast<double>(frames) * target_rate / source_rate));
output->assign(output_frames * static_cast<std::size_t>(channels), 0.0f);
for (std::size_t out_frame = 0; out_frame < output_frames; ++out_frame) {
const double position = static_cast<double>(out_frame) * step;
const std::int64_t base = static_cast<std::int64_t>(std::floor(position));
const double fraction = position - static_cast<double>(base);
double weights[2 * kHalfTaps + 1];
double weight_sum = 0.0;
for (int tap = -kHalfTaps + 1; tap <= kHalfTaps; ++tap) {
const double distance = tap - fraction;
const double weight = cutoff * sinc(cutoff * distance) * kaiser_window(distance);
weights[tap + kHalfTaps] = weight;
weight_sum += weight;
}
if (std::fabs(weight_sum) < 1.0e-12) {
continue;
}
for (int channel = 0; channel < channels; ++channel) {
double sum = 0.0;
for (int tap = -kHalfTaps + 1; tap <= kHalfTaps; ++tap) {
const std::int64_t index = base + tap;
if (index < 0 || index >= static_cast<std::int64_t>(frames)) {
continue;
}
sum += static_cast<double>(
input[static_cast<std::size_t>(index) *
static_cast<std::size_t>(channels) +
channel]) *
weights[tap + kHalfTaps];
}
output->at(out_frame * static_cast<std::size_t>(channels) + channel) =
static_cast<float>(sum / weight_sum);
}
}
return Status::success();
}
} // namespace joc::multichannel
+18
View File
@@ -0,0 +1,18 @@
// Rational sample-rate conversion for the multichannel binaural frontend.
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
#include "foundation/status.h"
namespace joc::multichannel {
// Resamples interleaved PCM with a windowed-sinc low-pass. The binaural
// runtime is native 48 kHz, so any other input rate passes through this first.
Status resample_interleaved(const float* input, std::size_t frames, int channels,
std::uint32_t source_rate, std::uint32_t target_rate,
std::vector<float>* output);
} // namespace joc::multichannel
+296
View File
@@ -0,0 +1,296 @@
// Multichannel PCM WAV -> binaural WAV frontend.
//
// Ordinary 2.0/5.1/7.1 PCM has no OAMD. Each non-LFE channel is assigned a
// fixed ADM direction, the LFE channel goes to source 0, and the resulting
// sixteen JOC sources feed the Rosella binaural runtime. The runtime is
// native 48 kHz; other input rates are resampled on the way in and the stereo
// result is resampled to the requested output rate on the way out.
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include "binaural/multichannel_layout.h"
#include "binaural/pcm_resampler.h"
#include "foundation/status.h"
#include "hrtf/rosella_model.h"
#include "hrtf/rosella_renderer.h"
#include "io/wav_pcm.h"
#include "oamd/oamd_parser.h"
namespace {
constexpr std::uint32_t kFrameSamples = 1536;
constexpr std::uint32_t kSourceChannels = 16;
constexpr std::uint32_t kTargetRate = 48000;
void usage() {
std::printf(
"usage: joc_multichannel INPUT.wav -o OUTPUT.wav [options]\n"
" --layout auto|2.0|5.1|7.1 WAV channel layout (auto by channel count)\n"
" --model PATH Rosella .personalized_headphone model\n"
" --mode near|mid|far Rosella binaural profile (default mid)\n"
" --filterbank 1|2 Rosella filterbank (default 1)\n"
" --tail-seconds SECONDS room tail (default 2.0)\n"
" --output-rate 48000|input|N output rate (default 48000; non-48k adds\n"
" a second resampling pass)\n"
" --list-layouts\n"
"\n"
"Prefer a 48 kHz float32 WAV; let ffmpeg/libsoxr do the one conversion:\n"
" ffmpeg -i INPUT -af \"aresample=resampler=soxr:precision=28:osr=48000\" "
"-c:a pcm_f32le OUT.wav\n"
"44.1/88.2/176.4 or 48/96/192 kHz accepted; non-48k is converted once in.\n");
}
std::int16_t q_for_position(double value) {
if (value <= -0.999) {
return 0;
}
if (value >= 0.999) {
return 32767;
}
return 16384;
}
void fill_initial_update(const joc::multichannel::Layout& layout, joc::oamd::OamdUpdate* update) {
update->has_frame_state = true;
update->object_count = kSourceChannels;
update->frame_state.object_count = kSourceChannels;
update->frame_state.block_count = 1;
update->frame_state.blocks.resize(1);
update->frame_state.objects.resize(kSourceChannels);
double positions[15][3];
const joc::Status status = joc::multichannel::object_positions(layout, positions);
if (!status.ok()) {
return;
}
for (int object = 0; object < static_cast<int>(kSourceChannels); ++object) {
joc::oamd::ObjectElementState& element = update->frame_state.objects[object];
element.kind = object == 0 ? joc::oamd::ObjectKind::kBed
: joc::oamd::ObjectKind::kDynamic;
element.blocks.resize(1);
joc::oamd::ObjectRenderState& render = element.blocks[0].render;
render.not_active = false;
render.gain_q = 4096;
render.priority_q = 32767;
render.enable_elevation = true;
double x = 0.0;
double y = 1.0;
double z = 0.0;
if (object > 0) {
x = positions[object - 1][0];
y = positions[object - 1][1];
z = positions[object - 1][2];
}
render.qx = q_for_position(x);
render.qy = q_for_position(-y);
render.qz = static_cast<std::int32_t>(std::llround(z * 32767.0));
}
}
std::string model_default() {
return "HRTF/binaural.personalized_headphone";
}
} // namespace
int main(int argc, char** argv) {
std::string input;
std::string output;
std::string layout_name = "auto";
std::string model_path = model_default();
std::string mode = "mid";
int filterbank = 1;
double tail_seconds = 2.0;
std::string output_rate = "48000";
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
const auto next = [&](const char* option) -> std::string {
if (i + 1 >= argc) {
std::fprintf(stderr, "missing value for %s\n", option);
std::exit(2);
}
return argv[++i];
};
if (arg == "--layout") {
layout_name = next("--layout");
} else if (arg == "--model") {
model_path = next("--model");
} else if (arg == "--mode") {
mode = next("--mode");
} else if (arg == "--filterbank") {
filterbank = std::stoi(next("--filterbank"));
} else if (arg == "--tail-seconds") {
tail_seconds = std::stod(next("--tail-seconds"));
} else if (arg == "--output-rate") {
output_rate = next("--output-rate");
} else if (arg == "--list-layouts") {
std::printf("2.0: 2 channels\n5.1: 6 channels\n7.1: 8 channels\n");
return 0;
} else if (arg == "-o" || arg == "--output") {
output = next("-o");
} else if (arg == "-h" || arg == "--help") {
usage();
return 0;
} else if (!arg.empty() && arg[0] == '-') {
std::fprintf(stderr, "unknown option: %s\n", arg.c_str());
return 2;
} else if (input.empty()) {
input = arg;
} else {
std::fprintf(stderr, "unexpected argument: %s\n", arg.c_str());
return 2;
}
}
if (input.empty()) {
usage();
return 2;
}
if (output.empty()) {
output = input + ".binaural.wav";
}
joc::io::WavAudio wav;
joc::Status status = joc::io::read_wav_file(input, &wav);
if (!status.ok()) {
std::fprintf(stderr, "read: %s\n", status.message().c_str());
return 1;
}
const joc::multichannel::Layout* layout =
(layout_name == "auto") ? joc::multichannel::detect_layout(wav.channels)
: joc::multichannel::find_layout(layout_name);
if (layout == nullptr) {
std::fprintf(stderr, "unsupported layout: %s (%u channels)\n", layout_name.c_str(),
static_cast<unsigned>(wav.channels));
return 2;
}
if (wav.channels != layout->channels) {
std::fprintf(stderr, "layout %s expects %d channels, WAV has %u\n", layout->name,
layout->channels, static_cast<unsigned>(wav.channels));
return 2;
}
std::vector<float> pcm48;
if (wav.sample_rate == kTargetRate) {
pcm48 = wav.samples;
} else {
std::fprintf(
stderr,
"warning: input is %u Hz and was converted once to 48000 Hz; prefer "
"ffmpeg/libsoxr to produce 48 kHz float32 first:\n"
" ffmpeg -i INPUT -af \"aresample=resampler=soxr:precision=28:osr=48000\" "
"-c:a pcm_f32le OUT.wav\n",
static_cast<unsigned>(wav.sample_rate));
status = joc::multichannel::resample_interleaved(
wav.samples.data(), wav.samples.size() / wav.channels, wav.channels,
wav.sample_rate, kTargetRate, &pcm48);
if (!status.ok()) {
std::fprintf(stderr, "resample: %s\n", status.message().c_str());
return 1;
}
}
const std::size_t frames = pcm48.size() / wav.channels;
joc::hrtf::RosellaModel model;
status = joc::hrtf::load_personalized_headphone(model_path, &model);
if (!status.ok()) {
std::fprintf(stderr, "model: %s\n", status.message().c_str());
return 1;
}
joc::hrtf::RosellaRenderOptions options;
options.profile = (mode == "near") ? joc::hrtf::RosellaProfile::Near
: (mode == "far") ? joc::hrtf::RosellaProfile::Far
: joc::hrtf::RosellaProfile::Mid;
options.bank = (filterbank == 2) ? joc::hrtf::RosellaBankKind::Captured
: joc::hrtf::RosellaBankKind::Public;
options.tail_seconds = tail_seconds;
joc::hrtf::RosellaRuntime runtime;
status = runtime.open(model, options);
if (!status.ok()) {
std::fprintf(stderr, "open: %s\n", status.message().c_str());
return 1;
}
joc::oamd::OamdUpdate initial{};
fill_initial_update(*layout, &initial);
std::vector<float> objects16(kSourceChannels * kFrameSamples, 0.0f);
std::vector<double> rendered;
for (std::size_t start = 0, frame = 0; start < frames; start += kFrameSamples, ++frame) {
const std::size_t count = std::min<std::size_t>(kFrameSamples, frames - start);
std::fill(objects16.begin(), objects16.end(), 0.0f);
for (int channel = 0; channel < layout->channels; ++channel) {
const joc::multichannel::ChannelSpec& spec = layout->specs[channel];
int destination = 0;
if (!spec.lfe) {
destination = 1;
for (int previous = 0; previous < channel; ++previous) {
if (!layout->specs[previous].lfe) {
++destination;
}
}
}
for (std::size_t sample = 0; sample < count; ++sample) {
objects16[static_cast<std::size_t>(destination) * kFrameSamples + sample] =
pcm48[(start + sample) * layout->channels + channel];
}
}
status = runtime.submit_frame(objects16.data(), frame == 0 ? &initial : nullptr,
static_cast<std::int64_t>(frame), 0, 0);
if (!status.ok()) {
std::fprintf(stderr, "render: %s\n", status.message().c_str());
return 1;
}
std::vector<double> chunk;
runtime.take_output(&chunk);
rendered.insert(rendered.end(), chunk.begin(), chunk.end());
}
std::vector<double> tail;
status = runtime.finish(runtime.finish_capacity(tail_seconds), &tail);
if (!status.ok()) {
std::fprintf(stderr, "finish: %s\n", status.message().c_str());
return 1;
}
rendered.insert(rendered.end(), tail.begin(), tail.end());
std::vector<float> stereo(rendered.size());
for (std::size_t index = 0; index < rendered.size(); ++index) {
stereo[index] = static_cast<float>(rendered[index]);
}
std::uint32_t target_rate = wav.sample_rate;
if (output_rate == "48k" || output_rate == "48000") {
target_rate = kTargetRate;
} else if (output_rate != "input") {
target_rate = static_cast<std::uint32_t>(std::stoul(output_rate));
}
if (target_rate != kTargetRate) {
std::fprintf(stderr,
"warning: non-48k output adds a second resampling pass; the "
"native binaural output rate is 48000 Hz\n");
std::vector<float> converted;
status = joc::multichannel::resample_interleaved(
stereo.data(), stereo.size() / 2, 2, kTargetRate, target_rate, &converted);
if (!status.ok()) {
std::fprintf(stderr, "output resample: %s\n", status.message().c_str());
return 1;
}
stereo.swap(converted);
}
status = joc::io::write_wav_float32(output, target_rate, 2, stereo.data(),
stereo.size() / 2);
if (!status.ok()) {
std::fprintf(stderr, "write: %s\n", status.message().c_str());
return 1;
}
std::printf("input: %s (%u ch, %u Hz, %zu frames)\n", input.c_str(),
static_cast<unsigned>(wav.channels), static_cast<unsigned>(wav.sample_rate),
frames);
std::printf("layout: %s\n", layout->name);
std::printf("output: %s (%u Hz, %zu frames)\n", output.c_str(),
static_cast<unsigned>(target_rate), stereo.size() / 2);
return 0;
}
+207
View File
@@ -0,0 +1,207 @@
#include "io/wav_pcm.h"
#include <cstring>
#include <fstream>
#include <iterator>
#include <limits>
namespace joc::io {
namespace {
std::uint16_t read_u16(const std::uint8_t* data) {
return static_cast<std::uint16_t>(data[0] | (data[1] << 8));
}
std::uint32_t read_u32(const std::uint8_t* data) {
return static_cast<std::uint32_t>(data[0]) |
(static_cast<std::uint32_t>(data[1]) << 8) |
(static_cast<std::uint32_t>(data[2]) << 16) |
(static_cast<std::uint32_t>(data[3]) << 24);
}
void write_u16(std::vector<std::uint8_t>* out, std::uint16_t value) {
out->push_back(static_cast<std::uint8_t>(value & 0xFFu));
out->push_back(static_cast<std::uint8_t>((value >> 8) & 0xFFu));
}
void write_u32(std::vector<std::uint8_t>* out, std::uint32_t value) {
out->push_back(static_cast<std::uint8_t>(value & 0xFFu));
out->push_back(static_cast<std::uint8_t>((value >> 8) & 0xFFu));
out->push_back(static_cast<std::uint8_t>((value >> 16) & 0xFFu));
out->push_back(static_cast<std::uint8_t>((value >> 24) & 0xFFu));
}
std::string fourcc(const std::uint8_t* data) {
return std::string(reinterpret_cast<const char*>(data), 4);
}
float sample_to_float(const std::uint8_t* data, std::uint16_t format, std::uint16_t bits) {
if (format == 3u) {
if (bits == 32u) {
float value = 0.0f;
std::memcpy(&value, data, sizeof(value));
return value;
}
double value = 0.0;
std::memcpy(&value, data, sizeof(value));
return static_cast<float>(value);
}
if (bits == 16u) {
const std::int16_t value = static_cast<std::int16_t>(read_u16(data));
return static_cast<float>(value) / 32768.0f;
}
if (bits == 24u) {
std::int32_t value = static_cast<std::int32_t>(data[0]) |
(static_cast<std::int32_t>(data[1]) << 8) |
(static_cast<std::int32_t>(data[2]) << 16);
if ((value & 0x00800000) != 0) {
value |= static_cast<std::int32_t>(0xFF000000u);
}
return static_cast<float>(value) / 8388608.0f;
}
if (bits == 32u) {
const std::int32_t value = static_cast<std::int32_t>(read_u32(data));
return static_cast<float>(static_cast<double>(value) / 2147483648.0);
}
return 0.0f;
}
} // namespace
Status read_wav_file(const std::string& path, WavAudio* out) {
if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kFoundation,
"WAV output pointer is null");
}
std::ifstream stream(path, std::ios::binary);
if (!stream) {
return Status::fail(JOC_ERR_IO, stage::kFoundation,
"cannot open WAV file: " + path);
}
std::vector<std::uint8_t> bytes((std::istreambuf_iterator<char>(stream)),
std::istreambuf_iterator<char>());
if (bytes.size() < 12u || fourcc(bytes.data()) != "RIFF" ||
fourcc(bytes.data() + 8) != "WAVE") {
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kFoundation,
"not a RIFF/WAVE file: " + path);
}
std::uint16_t format = 0;
std::uint16_t channels = 0;
std::uint32_t sample_rate = 0;
std::uint16_t block_align = 0;
std::uint16_t bits = 0;
bool have_format = false;
std::size_t data_offset = 0;
std::size_t data_size = 0;
bool have_data = false;
std::size_t offset = 12;
while (offset + 8u <= bytes.size()) {
const std::string id = fourcc(bytes.data() + offset);
const std::uint32_t size = read_u32(bytes.data() + offset + 4);
const std::size_t payload = offset + 8u;
if (payload + size > bytes.size()) {
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kFoundation,
"truncated WAV chunk: " + id);
}
if (id == "fmt " && size >= 16u) {
format = read_u16(bytes.data() + payload);
channels = read_u16(bytes.data() + payload + 2);
sample_rate = read_u32(bytes.data() + payload + 4);
block_align = read_u16(bytes.data() + payload + 12);
bits = read_u16(bytes.data() + payload + 14);
if (format == 0xFFFEu && size >= 40u) {
// WAVEFORMATEXTENSIBLE: the sub-format GUID starts at +24.
format = read_u16(bytes.data() + payload + 24);
}
have_format = true;
} else if (id == "data") {
data_offset = payload;
data_size = size;
have_data = true;
}
offset = payload + size + (size & 1u);
}
if (!have_format || !have_data || channels == 0 || sample_rate == 0) {
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kFoundation,
"WAV file has no usable fmt/data chunk: " + path);
}
if (format != 1u && format != 3u) {
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kFoundation,
"unsupported WAV sample format");
}
const std::uint16_t bytes_per_sample = static_cast<std::uint16_t>(bits / 8u);
if (bytes_per_sample == 0u) {
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kFoundation,
"unsupported WAV bit depth");
}
if (block_align == 0u) {
block_align = static_cast<std::uint16_t>(channels * bytes_per_sample);
}
if (block_align < channels * bytes_per_sample) {
return Status::fail(JOC_ERR_INPUT_FORMAT, stage::kFoundation,
"invalid WAV block alignment");
}
const std::size_t frames = data_size / block_align;
out->sample_rate = sample_rate;
out->channels = channels;
out->samples.resize(frames * channels);
const std::uint8_t* source = bytes.data() + data_offset;
for (std::size_t frame = 0; frame < frames; ++frame) {
for (std::size_t channel = 0; channel < channels; ++channel) {
out->samples[frame * channels + channel] = sample_to_float(
source + frame * block_align + channel * bytes_per_sample, format, bits);
}
}
return Status::success();
}
Status write_wav_float32(const std::string& path, std::uint32_t sample_rate,
std::uint16_t channels, const float* interleaved, std::size_t frames) {
if (channels == 0u || sample_rate == 0u || interleaved == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput,
"WAV writer needs channels, a sample rate and samples");
}
if (frames > (std::numeric_limits<std::uint32_t>::max() - 36u) /
(channels * sizeof(float))) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput,
"WAV output is too large for RIFF");
}
const std::uint32_t data_bytes =
static_cast<std::uint32_t>(frames * channels * sizeof(float));
std::vector<std::uint8_t> header;
header.reserve(44);
header.insert(header.end(), {'R', 'I', 'F', 'F'});
write_u32(&header, 36u + data_bytes);
header.insert(header.end(), {'W', 'A', 'V', 'E'});
header.insert(header.end(), {'f', 'm', 't', ' '});
write_u32(&header, 16u);
write_u16(&header, 3u); // IEEE float
write_u16(&header, channels);
write_u32(&header, sample_rate);
write_u32(&header, sample_rate * static_cast<std::uint32_t>(channels * sizeof(float)));
write_u16(&header, static_cast<std::uint16_t>(channels * sizeof(float)));
write_u16(&header, 32u);
header.insert(header.end(), {'d', 'a', 't', 'a'});
write_u32(&header, data_bytes);
std::ofstream stream(path, std::ios::binary);
if (!stream) {
return Status::fail(JOC_ERR_IO, stage::kOutput,
"cannot open WAV output: " + path);
}
stream.write(reinterpret_cast<const char*>(header.data()),
static_cast<std::streamsize>(header.size()));
stream.write(reinterpret_cast<const char*>(interleaved),
static_cast<std::streamsize>(data_bytes));
if (!stream) {
return Status::fail(JOC_ERR_IO, stage::kOutput,
"cannot write WAV output: " + path);
}
return Status::success();
}
} // namespace joc::io
+25
View File
@@ -0,0 +1,25 @@
// Minimal RIFF/WAVE reader and float32 writer for the multichannel binaural
// frontend. It accepts the formats ffmpeg writes for PCM input: 16/24/32-bit
// integer, 32/64-bit float, including WAVE_FORMAT_EXTENSIBLE.
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include "foundation/status.h"
namespace joc::io {
struct WavAudio {
std::uint32_t sample_rate = 0;
std::uint16_t channels = 0;
std::vector<float> samples; // interleaved
};
Status read_wav_file(const std::string& path, WavAudio* out);
Status write_wav_float32(const std::string& path, std::uint32_t sample_rate,
std::uint16_t channels, const float* interleaved, std::size_t frames);
} // namespace joc::io
+28 -16
View File
@@ -44,6 +44,10 @@ constexpr BedChannel kNonStandardBits[17] = {
BedChannel::kLeft,
};
// 每个 bed 声道的固定方位:speakerLabel 各自对应唯一的 ADM (X,Y,Z),与目标扬声器
// 布局无关。天空声道的 X 取墙边(±1):点声源的横向声像只在扬声器跨度的端点之间分摊,
// 取端点仍落在输出布局顶部声道的满功率区里,与顶部声道互相下混的结果一致。Lw/Rw 的 Y
// 记作 0,677419,即 42/62 = 21/31。LFE/LFE2 不参与摆位,位置记为原点。
struct ChannelSpec {
BedChannel channel;
const char* short_name;
@@ -60,14 +64,14 @@ constexpr ChannelSpec kChannels[] = {
{BedChannel::kRightSurround, "Rs", "RC_RS", {1.0, 0.0, 0.0}},
{BedChannel::kLeftRearSurround, "Lrs", "RC_LB", {-1.0, -1.0, 0.0}},
{BedChannel::kRightRearSurround, "Rrs", "RC_RB", {1.0, -1.0, 0.0}},
{BedChannel::kTopFrontLeft, "Ltf", "RC_TFL", {-0.5, 0.5, 1.0}},
{BedChannel::kTopFrontRight, "Rtf", "RC_TFR", {0.5, 0.5, 1.0}},
{BedChannel::kTopFrontLeft, "Ltf", "RC_TFL", {-1.0, 1.0, 1.0}},
{BedChannel::kTopFrontRight, "Rtf", "RC_TFR", {1.0, 1.0, 1.0}},
{BedChannel::kTopSideLeft, "Ltm", "RC_TSL", {-1.0, 0.0, 1.0}},
{BedChannel::kTopSideRight, "Rtm", "RC_TSR", {1.0, 0.0, 1.0}},
{BedChannel::kTopBackLeft, "Ltr", "RC_TBL", {-0.5, -0.5, 1.0}},
{BedChannel::kTopBackRight, "Rtr", "RC_TBR", {0.5, -0.5, 1.0}},
{BedChannel::kLeftWide, "Lw", "RC_LW", {-1.0, 0.5, 0.0}},
{BedChannel::kRightWide, "Rw", "RC_RW", {1.0, 0.5, 0.0}},
{BedChannel::kTopBackLeft, "Ltr", "RC_TBL", {-1.0, -1.0, 1.0}},
{BedChannel::kTopBackRight, "Rtr", "RC_TBR", {1.0, -1.0, 1.0}},
{BedChannel::kLeftWide, "Lw", "RC_LW", {-1.0, 21.0 / 31.0, 0.0}},
{BedChannel::kRightWide, "Rw", "RC_RW", {1.0, 21.0 / 31.0, 0.0}},
{BedChannel::kLfe2, "LFE2", "RC_LFE2", {0.0, 0.0, 0.0}},
};
@@ -279,12 +283,18 @@ bool bed_channel_q15(BedChannel channel, std::uint16_t q15[3]) {
if (!bed_channel_adm_position(channel, xyz) || q15 == nullptr) {
return false;
}
const double q1 = (xyz[0] + 1.0) * 0.5 * 32767.0;
const double q2 = (1.0 - xyz[1]) * 0.5 * 32767.0;
const double q3 = xyz[2] * 32767.0;
q15[0] = static_cast<std::uint16_t>(q1 < 0.0 ? 0.0 : (q1 > 32767.0 ? 32767.0 : q1));
q15[1] = static_cast<std::uint16_t>(q2 < 0.0 ? 0.0 : (q2 > 32767.0 ? 32767.0 : q2));
q15[2] = static_cast<std::uint16_t>(q3 < 0.0 ? 0.0 : (q3 > 32767.0 ? 32767.0 : q3));
// Q15 满刻度是 32768(渲染器按 q / 32768 还原方位),所以用 32768 定标并四舍五入;
// 截断会让半格位置(16384、7928)各低 1。
const double q1 = (xyz[0] + 1.0) * 0.5 * 32768.0;
const double q2 = (1.0 - xyz[1]) * 0.5 * 32768.0;
const double q3 = xyz[2] * 32768.0;
const double rounded[3] = {q1 + 0.5, q2 + 0.5, q3 + 0.5};
for (int axis = 0; axis < 3; ++axis) {
const double value = rounded[axis] < 0.0
? 0.0
: (rounded[axis] > 32767.0 ? 32767.0 : rounded[axis]);
q15[axis] = static_cast<std::uint16_t>(value);
}
return true;
}
@@ -308,17 +318,19 @@ bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel,
if (table == nullptr || geometry == nullptr) {
return false;
}
int standard_index = -1;
// channel_names 与区域几何同序(region 的 speaker_id 就是这里的下标),
// 因此同名声道不需要再经 standard_from_internal 换序。
int internal_index = -1;
for (std::uint32_t index = 0; index < table->count; ++index) {
if (std::strcmp(table->names[index], name) == 0) {
standard_index = static_cast<int>(index);
internal_index = static_cast<int>(index);
break;
}
}
if (standard_index < 0) {
if (internal_index < 0) {
return false;
}
const std::uint8_t internal = geometry->standard_from_internal[standard_index];
const std::uint8_t internal = static_cast<std::uint8_t>(internal_index);
const ejoc::speaker_tables::RegionGeometry& region = geometry->regions[0];
for (std::uint32_t point = 0; point < region.point_count; ++point) {
if (region.points[point].speaker_id != internal) {
+15 -15
View File
@@ -190,25 +190,25 @@ inline constexpr std::array<LayoutGeometry, 12> kLayouts{{
{{
RegionGeometry{
5, 2, 2, 0,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
5, 2, 2, 0,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
5, 2, 2, 0,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
3, 2, 2, 0,
{{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
@@ -220,7 +220,7 @@ inline constexpr std::array<LayoutGeometry, 12> kLayouts{{
},
RegionGeometry{
2, 1, 1, 0,
{{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
@@ -286,25 +286,25 @@ inline constexpr std::array<LayoutGeometry, 12> kLayouts{{
{{
RegionGeometry{
7, 3, 2, 1,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
7, 3, 2, 1,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
7, 3, 2, 1,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
5, 3, 2, 1,
{{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
@@ -316,7 +316,7 @@ inline constexpr std::array<LayoutGeometry, 12> kLayouts{{
},
RegionGeometry{
4, 3, 1, 1,
{{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}
},
@@ -334,25 +334,25 @@ inline constexpr std::array<LayoutGeometry, 12> kLayouts{{
{{
RegionGeometry{
9, 3, 2, 2,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
9, 3, 2, 2,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
9, 3, 2, 2,
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}}
},
RegionGeometry{
7, 3, 2, 2,
{{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}}
},
@@ -364,7 +364,7 @@ inline constexpr std::array<LayoutGeometry, 12> kLayouts{{
},
RegionGeometry{
6, 3, 1, 2,
{{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}},
{{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}},
{{AxisGroup{2, {2, 3, 0}}, AxisGroup{2, {4, 5, 0}}, AxisGroup{}, AxisGroup{}}}
},
+66 -5
View File
@@ -6,6 +6,7 @@
#include "adm/adm_metadata.h"
#include "foundation/status.h"
#include "virtual_speaker/virtual_speaker.h"
#include "hrtf/jochrtf.h"
#include "hrtf/rosella_model.h"
#include "joc_bitstream/bed_layout_map.h"
@@ -45,6 +46,7 @@ void Stream::reset_state() {
bed_pending_.clear();
bed_read_offset_ = 0;
objects16_.clear();
pcm_input_.reset();
objects_pending_.clear();
objects_read_offset_ = 0;
output_.clear();
@@ -77,7 +79,7 @@ Status Stream::create(const Config& config) {
return Status::fail(JOC_ERR_STATE, stage::kRender, "stream already created");
}
config_ = config;
if (config_.input > JOC_STREAM_IN_PCM_OBJECTS16 ||
if (config_.input > JOC_STREAM_IN_PCM_LAYOUT ||
config_.output > JOC_STREAM_OUT_BINAURAL) {
return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kRender, "unknown stream kind");
}
@@ -87,6 +89,20 @@ Status Stream::create(const Config& config) {
"objects16 input with objects16 output would do nothing");
}
gain_ = static_cast<float>(std::pow(10.0, config_.gain_db / 20.0));
if (config_.input == JOC_STREAM_IN_PCM_LAYOUT && !config_.pcm_layout.empty() &&
config_.pcm_layout != "auto") {
const virtual_speaker::Layout* layout =
virtual_speaker::find_layout(config_.pcm_layout);
if (layout == nullptr) {
return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kRender,
"unknown PCM layout: " + config_.pcm_layout);
}
const Status configured =
pcm_input_.configure(config_.pcm_layout, layout->channels, config_.gain_db);
if (!configured.ok()) {
return configured;
}
}
if (config_.input == JOC_STREAM_IN_EAC3) {
rebuilder_ = ejoc_renderer_create();
@@ -453,6 +469,47 @@ Status Stream::push_objects16(const float* planar16, std::size_t samples, std::s
return process_objects16_frames(false);
}
Status Stream::push_pcm_layout(const float* interleaved, std::size_t samples,
std::uint32_t channels, std::size_t* consumed) {
if (consumed != nullptr) {
*consumed = 0;
}
if (interleaved == nullptr || samples == 0u) {
return Status::success();
}
if (pcm_input_.layout() == nullptr) {
const Status configured = pcm_input_.configure(config_.pcm_layout,
static_cast<int>(channels),
config_.gain_db);
if (!configured.ok()) {
return configured;
}
} else if (pcm_input_.channels() != static_cast<int>(channels)) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender,
"PCM channel count changed mid-stream");
}
Status status = pcm_input_.append(interleaved, samples);
if (!status.ok()) {
return status;
}
std::size_t processed = 0;
while (pcm_input_.frame_ready()) {
status = pcm_input_.take_frame(&objects_frame_);
if (!status.ok()) {
return status;
}
status = push_objects16(objects_frame_.data(), kFrameSamples, nullptr);
if (!status.ok()) {
return status;
}
processed += kFrameSamples;
}
if (consumed != nullptr) {
*consumed = processed;
}
return Status::success();
}
Status Stream::process_objects16_frames(bool drain_all) {
constexpr std::size_t kFrameValues =
static_cast<std::size_t>(JOC_OUTPUT_CHANNELS) * kFrameSamples;
@@ -590,9 +647,11 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
if (speaker_enabled_) {
std::string error;
const Status stepped =
speaker::step(&speaker_, objects16, pending_metadata_.has_update
speaker::step(&speaker_, objects16,
pending_metadata_.has_update
? &pending_metadata_.update
: nullptr,
: (pcm_input_.layout() != nullptr ? &pcm_input_.fixed_update()
: nullptr),
&error);
if (!stepped.ok()) {
return Status::fail(stepped.code(), stage::kRender, error);
@@ -611,7 +670,8 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
const Status submitted =
binaural_.submit_frame(objects16.data(),
pending_metadata_.has_update ? &pending_metadata_.update : nullptr,
pending_metadata_.has_update ? &pending_metadata_.update
: (pcm_input_.layout() != nullptr ? &pcm_input_.fixed_update() : nullptr),
static_cast<std::int64_t>(info_.frames_out),
pending_metadata_.outer_offset,
static_cast<std::int64_t>(config_.object_delay_samples));
@@ -638,7 +698,8 @@ Status Stream::render_objects16(const std::vector<float>& objects16) {
Status Stream::render_rosella_objects16(const std::vector<float>& objects16) {
const Status submitted =
rosella_.submit_frame(objects16.data(),
pending_metadata_.has_update ? &pending_metadata_.update : nullptr,
pending_metadata_.has_update ? &pending_metadata_.update
: (pcm_input_.layout() != nullptr ? &pcm_input_.fixed_update() : nullptr),
static_cast<std::int64_t>(info_.frames_out),
pending_metadata_.outer_offset,
static_cast<std::int64_t>(config_.object_delay_samples));
+7
View File
@@ -15,6 +15,7 @@
#include "joc_stream.h"
#include "oamd/oamd_parser.h"
#include "speaker/speaker_step.h"
#include "virtual_speaker/virtual_speaker.h"
namespace joc::stream {
@@ -34,6 +35,7 @@ struct Config {
// (.jochrtf).
std::string personalized_headphone_path;
std::uint32_t jochrtf_room = 1;
std::string pcm_layout;
// Filterbank of the Rosella path; the other binaural inputs ignore it.
std::uint32_t rosella_filterbank = JOC_ROSELLA_FILTERBANK_PUBLIC;
};
@@ -99,6 +101,8 @@ public:
Status push_bed(const float* interleaved, std::size_t samples, std::uint32_t channels,
std::size_t* consumed);
Status push_objects16(const float* planar16, std::size_t samples, std::size_t* consumed);
Status push_pcm_layout(const float* interleaved, std::size_t samples, std::uint32_t channels,
std::size_t* consumed);
Status pull(float* destination, std::size_t capacity_samples, std::size_t* produced);
Status flush();
Status reset();
@@ -160,6 +164,9 @@ private:
std::size_t bed_read_offset_ = 0;
std::vector<float> bed5_;
std::vector<float> lfe_;
virtual_speaker::Input pcm_input_;
oamd::OamdUpdate fixed_update_{};
bool has_fixed_update_ = false;
std::vector<float> objects_pending_;
std::size_t objects_read_offset_ = 0;
std::vector<float> objects_frame_;
+161
View File
@@ -0,0 +1,161 @@
#include "virtual_speaker/virtual_speaker.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include "joc_core.h"
namespace joc::virtual_speaker {
namespace {
constexpr std::size_t kFrameSamples = JOC_FRAME_SAMPLES;
constexpr std::size_t kSourceChannels = JOC_OUTPUT_CHANNELS;
// WAV order follows the ffmpeg layouts: 5.1 = L R C LFE BL BR,
// 7.1 = L R C LFE BL BR SL SR.
constexpr Layout kLayouts[] = {
{"2.0",
2,
{{false, -1.0, 1.0, 0.0},
{false, 1.0, 1.0, 0.0}}},
{"5.1",
6,
{{false, -1.0, 1.0, 0.0},
{false, 1.0, 1.0, 0.0},
{false, 0.0, 1.0, 0.0},
{true, 0.0, 1.0, 0.0},
{false, -1.0, -1.0, 0.0},
{false, 1.0, -1.0, 0.0}}},
{"7.1",
8,
{{false, -1.0, 1.0, 0.0},
{false, 1.0, 1.0, 0.0},
{false, 0.0, 1.0, 0.0},
{true, 0.0, 1.0, 0.0},
{false, -1.0, -1.0, 0.0},
{false, 1.0, -1.0, 0.0},
{false, -1.0, 0.0, 0.0},
{false, 1.0, 0.0, 0.0}}},
};
std::int16_t q_for_axis(double value) {
if (value <= -0.999) {
return 0;
}
if (value >= 0.999) {
return 32767;
}
return 16384;
}
void fill_fixed_positions(const Layout& layout, oamd::OamdUpdate* update) {
update->object_count = static_cast<std::uint32_t>(kSourceChannels);
update->block_offset_samples = 0;
update->ramp_duration_samples = 0;
int slot = 1;
for (int channel = 0; channel < layout.channels; ++channel) {
const ChannelSpec& spec = layout.channel_specs[channel];
const int destination = spec.lfe ? 0 : slot++;
update->slots[destination].q1 = q_for_axis(spec.x);
update->slots[destination].q2 = q_for_axis(-spec.y);
update->slots[destination].q3 = q_for_axis(spec.z);
}
}
} // namespace
const Layout* find_layout(const std::string& name) {
for (const Layout& layout : kLayouts) {
if (name == layout.name) {
return &layout;
}
}
return nullptr;
}
const Layout* detect_layout(int channels) {
for (const Layout& layout : kLayouts) {
if (layout.channels == channels) {
return &layout;
}
}
return nullptr;
}
Status Input::configure(const std::string& layout_name, int channels, double gain_db) {
const Layout* layout =
(layout_name.empty() || layout_name == "auto")
? detect_layout(channels)
: find_layout(layout_name);
if (layout == nullptr || layout->channels != channels) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender,
"PCM layout does not match the channel count");
}
layout_ = layout;
channels_ = channels;
gain_ = static_cast<double>(std::pow(10.0, gain_db / 20.0));
pending_.clear();
read_offset_ = 0;
fill_fixed_positions(*layout_, &fixed_update_);
return Status::success();
}
void Input::reset() {
pending_.clear();
read_offset_ = 0;
}
Status Input::append(const float* interleaved, std::size_t frames) {
if (interleaved == nullptr || frames == 0u) {
return Status::success();
}
if (layout_ == nullptr || channels_ <= 0) {
return Status::fail(JOC_ERR_STATE, stage::kRender,
"virtual speaker input is not configured");
}
pending_.insert(pending_.end(), interleaved,
interleaved + frames * static_cast<std::size_t>(channels_));
return Status::success();
}
bool Input::frame_ready() const {
return layout_ != nullptr &&
pending_.size() - read_offset_ >=
kFrameSamples * static_cast<std::size_t>(channels_);
}
Status Input::take_frame(std::vector<float>* objects16) {
if (objects16 == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender,
"objects16 output is null");
}
if (!frame_ready()) {
return Status::fail(JOC_ERR_STATE, stage::kRender,
"no complete PCM frame is buffered");
}
objects16->assign(kSourceChannels * kFrameSamples, 0.0f);
const float* source = pending_.data() + read_offset_;
int slot = 1;
for (int channel = 0; channel < layout_->channels; ++channel) {
const int destination = layout_->channel_specs[channel].lfe ? 0 : slot++;
for (std::size_t sample = 0; sample < kFrameSamples; ++sample) {
(*objects16)[static_cast<std::size_t>(destination) * kFrameSamples + sample] =
source[sample * static_cast<std::size_t>(channels_) + channel] *
static_cast<float>(gain_);
}
}
read_offset_ += kFrameSamples * static_cast<std::size_t>(channels_);
if (read_offset_ == pending_.size()) {
pending_.clear();
read_offset_ = 0;
} else if (read_offset_ >= (1u << 20)) {
pending_.erase(pending_.begin(),
pending_.begin() + static_cast<std::ptrdiff_t>(read_offset_));
read_offset_ = 0;
}
return Status::success();
}
} // namespace joc::virtual_speaker
+57
View File
@@ -0,0 +1,57 @@
// Virtual speaker input: ordinary multichannel PCM as fixed-direction JOC
// objects. It owns only the layout mapping and the PCM-to-objects16 shape;
// decoding, speaker rendering and binaural rendering stay in their own modules.
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include "foundation/status.h"
#include "oamd/oamd_parser.h"
namespace joc::virtual_speaker {
struct ChannelSpec {
bool lfe;
double x;
double y;
double z;
};
struct Layout {
const char* name;
int channels;
ChannelSpec channel_specs[8];
};
const Layout* find_layout(const std::string& name);
const Layout* detect_layout(int channels);
class Input {
public:
// gain_db is the module's master attenuation; it is applied to every
// mapped channel once, before the objects16 frame leaves the module.
Status configure(const std::string& layout_name, int channels, double gain_db = 0.0);
void reset();
const oamd::OamdUpdate& fixed_update() const { return fixed_update_; }
int channels() const { return channels_; }
const Layout* layout() const { return layout_; }
// Appends interleaved frames; take_frame() emits whole 1536-frame blocks.
Status append(const float* interleaved, std::size_t frames);
bool frame_ready() const;
Status take_frame(std::vector<float>* objects16);
private:
const Layout* layout_ = nullptr;
int channels_ = 0;
double gain_ = 1.0;
oamd::OamdUpdate fixed_update_{};
std::vector<float> pending_;
std::size_t read_offset_ = 0;
};
} // namespace joc::virtual_speaker
+60 -9
View File
@@ -13,6 +13,8 @@
#include "adm/adm_metadata.h"
#include "adm/adm_tracks.h"
#include "binaural/multichannel_layout.h"
#include "binaural/pcm_resampler.h"
#include "eac3_transport/eac3_reader.h"
#include "eac3joc_core.h"
#include "foundation/bit_reader.h"
@@ -585,8 +587,8 @@ void test_speaker_warp_and_trim() {
CHECK(joc::speaker::parameters_from_oamd(nullptr, layout514, &automatic514).ok());
// Warp 0 leaves the floor Y alone; warp 1 doubles it before the clamp.
const std::vector<double> warp0 = {0.85973646214220434, 0, 0, 0, 0.35611450268596406, 0};
const std::vector<double> warp1 = {0.65801380052399572, 0, 0, 0, 0.65801380052399572, 0};
const std::vector<double> warp0 = {0.9126902722074105, 0, 0, 0, 0.18155106823708969, 0};
const std::vector<double> warp1 = {0.85973219419392444, 0, 0, 0, 0.35612480624995158, 0};
const std::vector<double> clamped = {0, 0, 0, 0, 0.70794578438413791, 0};
check_gains(speaker_gains(layout51, automatic51, 0, 4096, 0), warp0);
ejoc_speaker_controls warped = automatic51;
@@ -610,10 +612,10 @@ void test_speaker_warp_and_trim() {
explicit_controls.surround_db = -6.0;
explicit_controls.listener_balance = 9.0 / 16.0;
explicit_controls.overhead_balance = -9.0 / 16.0;
const std::vector<double> trimmed = {0.36269933962647599, 0, 0, 0, 0.71183753429070229, 0};
const std::vector<double> trimmed = {0.68117911059016056, 0, 0, 0, 0.41744281702545366, 0};
check_gains(speaker_gains(layout51, explicit_controls, 0, 4096, 0), trimmed);
// Per-object bypass keeps warp but drops balance and trim.
const std::vector<double> bypassed = {0.70710678118654757, 0, 0, 0, 0.70710678118654757, 0};
const std::vector<double> bypassed = {0.92387494614065435, 0, 0, 0, 0.38269450465561039, 0};
check_gains(speaker_gains(layout51, explicit_controls, 0, 4096, 0, true), bypassed);
// Centre trim is local to the front-centre region; height trim scales with Z.
@@ -794,7 +796,7 @@ void test_speaker_step_trim_events() {
update.trim.global_trim_mode = 0;
update.trim.object_count = 16;
CHECK(joc::speaker::step(&step, frame, &update, &error).ok());
const std::vector<double> warp1 = {0.65801380052399572, 0, 0, 0, 0.65801380052399572, 0};
const std::vector<double> warp1 = {0.85973219419392444, 0, 0, 0, 0.35612480624995158, 0};
std::vector<double> gains(step.layout.channel_count);
for (std::uint32_t channel = 0; channel < step.layout.channel_count; ++channel) {
gains[channel] = step.output[read + channel];
@@ -808,8 +810,8 @@ void test_speaker_step_trim_events() {
for (std::uint32_t channel = 0; channel < step.layout.channel_count; ++channel) {
gains[channel] = step.output[read + channel];
}
const std::vector<double> half_warp1 = {0.89123112643513935, 0, 0, 0,
0.36916001977848539, 0};
const std::vector<double> half_warp1 = {0.9461247896349676, 0, 0, 0,
0.18820181552760662, 0};
check_gains(gains, half_warp1);
// A new trim element switches the controls back to warp 0.
@@ -823,8 +825,8 @@ void test_speaker_step_trim_events() {
for (std::uint32_t channel = 0; channel < step.layout.channel_count; ++channel) {
gains[channel] = step.output[read + channel];
}
const std::vector<double> half_warp0 = {0.94612591737884011, 0, 0, 0,
0.18819614606507579, 0};
const std::vector<double> half_warp0 = {0.96001622724230795, 0, 0, 0,
0.094556249720175942, 0};
check_gains(gains, half_warp0);
// A warp 2 element renders through the expanded layout and folds.
@@ -1010,6 +1012,54 @@ void test_adm_object_metadata() {
CHECK(trajectory[1].blocks.back().meta.divergence == 0.0);
}
void test_multichannel_binaural_helpers() {
const joc::multichannel::Layout* layout = joc::multichannel::find_layout("5.1");
CHECK(layout != nullptr);
CHECK(layout->channels == 6);
double positions[15][3] = {};
CHECK(joc::multichannel::object_positions(*layout, positions).ok());
CHECK(positions[0][0] == -1.0 && positions[0][1] == 1.0);
CHECK(positions[2][0] == 0.0 && positions[2][1] == 1.0);
CHECK(positions[3][0] == -1.0 && positions[3][1] == -1.0);
const joc::multichannel::Layout* surround = joc::multichannel::detect_layout(8);
CHECK(surround != nullptr && std::string(surround->name) == "7.1");
std::vector<float> pcm(12);
for (int index = 0; index < 12; ++index) {
pcm[static_cast<std::size_t>(index)] = 0.01f * index;
}
std::vector<float> objects(16 * 2, 0.0f);
CHECK(joc::multichannel::pcm_to_objects16(pcm.data(), 2, *layout, objects.data()).ok());
CHECK(objects[0 * 2 + 0] == pcm[3]);
CHECK(objects[1 * 2 + 0] == pcm[0]);
CHECK(objects[3 * 2 + 0] == pcm[2]);
CHECK(objects[4 * 2 + 1] == pcm[10]);
CHECK(objects[6 * 2 + 0] == 0.0f);
std::vector<float> dc(200, 0.5f);
std::vector<float> up;
CHECK(joc::multichannel::resample_interleaved(dc.data(), 100, 2, 48000, 96000, &up).ok());
CHECK(up.size() == 400);
CHECK(std::fabs(up[100] - 0.5f) < 1.0e-3f);
std::vector<float> down;
CHECK(joc::multichannel::resample_interleaved(up.data(), 200, 2, 96000, 48000, &down).ok());
CHECK(down.size() == 200);
CHECK(std::fabs(down[100] - 0.5f) < 1.0e-3f);
for (const std::uint32_t rate :
{44100u, 88200u, 176400u, 48000u, 96000u, 192000u}) {
std::vector<float> input(200, 0.5f);
std::vector<float> output;
CHECK(joc::multichannel::resample_interleaved(
input.data(), 100, 2, rate, 48000, &output)
.ok());
const std::size_t expected = static_cast<std::size_t>(
std::llround(100.0 * 48000.0 / static_cast<double>(rate)));
CHECK(output.size() == expected * 2);
CHECK(std::fabs(output[output.size() / 2] - 0.5f) < 1.0e-3f);
}
}
void test_int24_packing() {
// Interleaved (L,R) for two frames: 0.0, +1.0, -1.0, +2.0 (clips).
const float values[4] = {0.0f, 1.0f, -1.0f, 2.0f};
@@ -2477,6 +2527,7 @@ int main() {
test_speaker_step_trim_events();
test_dbmd_and_axml_trim();
test_adm_object_metadata();
test_multichannel_binaural_helpers();
test_int24_packing();
test_utf8_paths();
test_process_runner();