2 Commits
Author SHA1 Message Date
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
TheM14 787984afe5 Write OAMD object render metadata into the ADM BWF
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
Port the Python reference ADM metadata change.  The ADM export path now parses
the full OAMD frame state, so the same block-0 render state the speaker
renderer consumes is available to the trajectory builder.

adm::ObjectMetadata (gain, width/depth/height size, divergence, snap) is
attached to Keyframe and OamdEvent.  TrajectoryBuilder updates the per-object
state from frame_state, emits an event when position or metadata changes, and
both compact and dense64 expansion modes carry the snapshot into every ADM
block.  The state is held across frames without a payload; inactive objects
write gain 0.  Divergence is taken from the current payload (0 when the
extended element does not carry it), matching the speaker renderer instead of
holding a stale value.

build_axml writes width/height/depth (relative 0..1), gain in linear units and
objectDivergence positionRange, omitting defaults.  The ADM path requests
parse_id11_state() so frame_state is populated.  test_core adds coverage for
the AXML fields and for the trajectory metadata and divergence reset.

Bump JOC_VERSION to 1.3.2.
2026-11-12 02:01:13 +08:00
21 changed files with 1318 additions and 25 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 1
#define JOC_VERSION_STRING "1.3.1"
#define JOC_VERSION_PATCH 3
#define JOC_VERSION_STRING "1.3.3"
#endif /* JOC_VERSION_H */
+14
View File
@@ -363,6 +363,20 @@ std::string build_axml(const std::vector<Track>& tracks, double duration_sec, st
out += "<position coordinate=\"Z\">" + pynum::format_fixed(block.z, 10) +
"</position>";
}
const ObjectMetadata& meta = block.meta;
if (meta.size[0] > 0.0 || meta.size[1] > 0.0 || meta.size[2] > 0.0) {
out += "<width>" + pynum::format_fixed(meta.size[0], 6) + "</width>";
out += "<height>" + pynum::format_fixed(meta.size[2], 6) + "</height>";
out += "<depth>" + pynum::format_fixed(meta.size[1], 6) + "</depth>";
}
if (std::fabs(meta.gain - 1.0) > 1e-9) {
out += "<gain gainUnit=\"linear\">" + pynum::format_fixed(meta.gain, 6) +
"</gain>";
}
if (meta.divergence > 0.0) {
out += "<objectDivergence positionRange=\"" +
pynum::format_fixed(meta.divergence, 6) + "\"/>";
}
out += "<jumpPosition interpolationLength=\"" +
pynum::format_fixed(static_cast<double>(block.interpolation_samples) / scale,
5) +
+10
View File
@@ -14,6 +14,15 @@ namespace joc::adm {
inline constexpr int kObjectCount = 15;
inline constexpr std::uint32_t kTrackCount = 25;
// Per-block OAMD render metadata the ADM audioBlockFormat can express.
struct ObjectMetadata {
double gain = 1.0;
// OAMD order: width, depth, height (relative units 0..1).
double size[3] = {0.0, 0.0, 0.0};
double divergence = 0.0;
bool snap = false;
};
struct Keyframe {
std::int64_t rtime_samples = 0;
double x = 0.0;
@@ -21,6 +30,7 @@ struct Keyframe {
double z = 0.0;
std::int64_t duration_samples = 0;
std::int64_t interpolation_samples = 0;
ObjectMetadata meta;
};
struct Track {
+61 -13
View File
@@ -16,15 +16,22 @@ struct Point {
double y = 0.0;
double z = 0.0;
std::int64_t interpolation_samples = 0;
ObjectMetadata meta;
};
bool same_meta(const ObjectMetadata& left, const ObjectMetadata& right) {
return left.gain == right.gain && left.size[0] == right.size[0] &&
left.size[1] == right.size[1] && left.size[2] == right.size[2] &&
left.divergence == right.divergence && left.snap == right.snap;
}
// otherwise (the reference drops silently).
void append_point(std::vector<Point>* points, std::int64_t sample, double x, double y, double z,
std::int64_t interpolation_samples) {
std::int64_t interpolation_samples, const ObjectMetadata& meta) {
if (!points->empty() && sample == points->back().sample) {
points->back() = Point{sample, x, y, z, interpolation_samples};
points->back() = Point{sample, x, y, z, interpolation_samples, meta};
} else if (points->empty() || sample > points->back().sample) {
points->push_back(Point{sample, x, y, z, interpolation_samples});
points->push_back(Point{sample, x, y, z, interpolation_samples, meta});
}
}
@@ -52,6 +59,7 @@ void points_to_blocks(const std::vector<Point>& points, std::int64_t total_sampl
keyframe.z = point.z;
keyframe.duration_samples = duration;
keyframe.interpolation_samples = std::min(point.interpolation_samples, duration);
keyframe.meta = point.meta;
out->push_back(keyframe);
}
}
@@ -85,7 +93,8 @@ Status expand_compact(const std::vector<OamdEvent>& events, std::int64_t total_s
std::vector<Point> points;
double current[3] = {events[0].x, events[0].y, events[0].z};
append_point(&points, 0, current[0], current[1], current[2], 0);
ObjectMetadata current_meta = events[0].meta;
append_point(&points, 0, current[0], current[1], current[2], 0, current_meta);
for (std::size_t index = 1; index < events.size(); ++index) {
const OamdEvent& event = events[index];
@@ -131,10 +140,12 @@ Status expand_compact(const std::vector<OamdEvent>& events, std::int64_t total_s
target[2] = z;
interpolation = available;
}
append_point(&points, block_start, target[0], target[1], target[2], interpolation);
append_point(&points, block_start, target[0], target[1], target[2], interpolation,
event.meta);
current[0] = event.x;
current[1] = event.y;
current[2] = event.z;
current_meta = event.meta;
}
points_to_blocks(points, total_samples, out);
@@ -156,7 +167,8 @@ Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_s
std::vector<Point> points;
double current[3] = {events[0].x, events[0].y, events[0].z};
append_point(&points, 0, current[0], current[1], current[2], 0);
ObjectMetadata current_meta = events[0].meta;
append_point(&points, 0, current[0], current[1], current[2], 0, current_meta);
for (std::size_t index = 1; index < events.size(); ++index) {
const OamdEvent& event = events[index];
@@ -171,14 +183,15 @@ Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_s
points.back().sample);
}
if (start > points.back().sample) {
append_point(&points, start, current[0], current[1], current[2], 0);
append_point(&points, start, current[0], current[1], current[2], 0, current_meta);
}
const std::int64_t ramp = std::max<std::int64_t>(0, event.ramp_samples);
if (ramp == 0) {
append_point(&points, start, event.x, event.y, event.z, 0);
append_point(&points, start, event.x, event.y, event.z, 0, event.meta);
current[0] = event.x;
current[1] = event.y;
current[2] = event.z;
current_meta = event.meta;
continue;
}
const std::int64_t steps = (ramp + update_block_samples - 1) / update_block_samples;
@@ -214,12 +227,13 @@ Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_s
break;
}
append_point(&points, sample, position[0], position[1], position[2],
update_block_samples);
update_block_samples, event.meta);
future -= update_block_samples;
}
current[0] = event.x;
current[1] = event.y;
current[2] = event.z;
current_meta = event.meta;
}
points_to_blocks(points, total_samples, out);
@@ -239,18 +253,52 @@ void TrajectoryBuilder::submit_frame(std::int64_t frame_index, const oamd::OamdU
if (fixed_[object - 1]) {
continue; // 固定 bed 声道:位置由布局给出,不跟 OAMD 更新
}
const int slot = object - 1;
// OAMD block 0 对动态对象显式携带 gain/size/snap;divergence 只在扩展
// 元素里出现,缺失时按 0 处理,与 speaker 渲染器的逐 payload 语义一致。
if (update != nullptr && update->has_frame_state &&
static_cast<std::size_t>(object) < update->frame_state.objects.size()) {
const oamd::ObjectElementState& element =
update->frame_state.objects[static_cast<std::size_t>(object)];
if (!element.blocks.empty() && element.kind == oamd::ObjectKind::kDynamic) {
const oamd::ObjectRenderState& render = element.blocks[0].render;
ObjectMetadata& meta = meta_[slot];
if (render.not_active) {
meta.gain = 0.0;
} else {
meta.gain = static_cast<double>(render.gain_q) / 4096.0;
if (render.size_mode == 1u || render.size_mode == 2u) {
for (int axis = 0; axis < 3; ++axis) {
meta.size[axis] = std::min(
std::max(static_cast<double>(render.size_q[axis]) / 32767.0, 0.0),
1.0);
}
} else {
meta.size[0] = 0.0;
meta.size[1] = 0.0;
meta.size[2] = 0.0;
}
meta.divergence = static_cast<double>(render.divergence_q) / 32767.0;
meta.snap = render.snap;
}
}
}
double x = 0.0;
double y = 0.0;
double z = 0.0;
geometry::q_to_adm_xyz(state_.q(object, 0), state_.q(object, 1), state_.q(object, 2), &x,
&y, &z);
const int slot = object - 1;
if (!has_previous_[slot] || previous_[slot][0] != x || previous_[slot][1] != y ||
previous_[slot][2] != z) {
events_[slot].push_back(OamdEvent{event_sample, x, y, z, ramp_samples});
const ObjectMetadata& meta = meta_[slot];
const bool position_changed =
!has_previous_[slot] || previous_[slot][0] != x || previous_[slot][1] != y ||
previous_[slot][2] != z;
const bool meta_changed = !has_previous_[slot] || !same_meta(previous_meta_[slot], meta);
if (position_changed || meta_changed) {
events_[slot].push_back(OamdEvent{event_sample, x, y, z, ramp_samples, meta});
previous_[slot][0] = x;
previous_[slot][1] = y;
previous_[slot][2] = z;
previous_meta_[slot] = meta;
has_previous_[slot] = true;
}
}
+3
View File
@@ -19,6 +19,7 @@ struct OamdEvent {
double y = 0.0;
double z = 0.0;
std::int64_t ramp_samples = 0;
ObjectMetadata meta;
};
enum class TrajectoryMode { Compact, Dense64 };
@@ -62,6 +63,8 @@ private:
std::vector<OamdEvent> events_[kObjectCount];
bool has_previous_[kObjectCount] = {};
double previous_[kObjectCount][3] = {};
ObjectMetadata meta_[kObjectCount];
ObjectMetadata previous_meta_[kObjectCount];
bool fixed_[kObjectCount] = {};
double fixed_xyz_[kObjectCount][3] = {};
};
+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
+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_;
+5 -4
View File
@@ -1845,10 +1845,11 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
stream_done = true;
break;
}
// The speaker and binaural renderers consume the full per-object
// block state (multi-block and every declared object); the ADM
// export path keeps the reference's 15-track single-block view.
const bool full_metadata_state = speaker_enabled || binaural_enabled;
// The speaker, binaural and ADM paths consume the full per-object
// block state (multi-block and every declared object); the legacy
// stream/ADM single-block view only needs the slot fields.
const bool full_metadata_state =
speaker_enabled || binaural_enabled || adm_enabled;
const Status oamd =
full_metadata_state
? ::joc::oamd::parse_id11_state(bytes.data(), bytes.size(),
+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
+107
View File
@@ -12,6 +12,9 @@
#include <vector>
#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"
@@ -955,6 +958,108 @@ void test_dbmd_and_axml_trim() {
CHECK(plain_axml.find("<DolbyAtmosMasterFile>") == std::string::npos);
}
void test_adm_object_metadata() {
std::vector<joc::adm::Track> tracks(15);
for (std::size_t i = 0; i < tracks.size(); ++i) {
tracks[i].name = "obj" + std::to_string(i);
}
joc::adm::Keyframe block;
block.duration_samples = 48000;
block.meta.gain = 0.5;
block.meta.size[0] = 0.516155;
block.meta.size[1] = 0.516155;
block.meta.size[2] = 0.516155;
block.meta.divergence = 0.25;
tracks[1].blocks.push_back(block);
const std::string axml = joc::adm::build_axml(tracks, 1.0, 48000);
CHECK(axml.find("<width>0.516155</width><height>0.516155</height><depth>0.516155</depth>") !=
std::string::npos);
CHECK(axml.find("<gain gainUnit=\"linear\">0.500000</gain>") != std::string::npos);
CHECK(axml.find("<objectDivergence positionRange=\"0.250000\"/>") != std::string::npos);
// Default metadata writes no fields.
tracks[1].blocks[0].meta = joc::adm::ObjectMetadata{};
const std::string plain = joc::adm::build_axml(tracks, 1.0, 48000);
CHECK(plain.find("<width>") == std::string::npos);
CHECK(plain.find("<gain ") == std::string::npos);
CHECK(plain.find("<objectDivergence ") == std::string::npos);
// TrajectoryBuilder carries block-0 metadata and drops divergence when the
// next payload does not send it, matching the speaker renderer.
joc::adm::TrajectoryBuilder builder;
joc::oamd::OamdUpdate update;
update.has_frame_state = true;
update.object_count = 3;
update.frame_state.object_count = 3;
update.frame_state.block_count = 1;
update.frame_state.objects.resize(3);
auto& element = update.frame_state.objects[2];
element.kind = joc::oamd::ObjectKind::kDynamic;
element.blocks.resize(1);
auto& render = element.blocks[0].render;
render.gain_q = 2048;
render.size_mode = 1;
render.size_q[0] = render.size_q[1] = render.size_q[2] = 16384;
render.divergence_q = 16384;
builder.submit_frame(0, &update, 0);
joc::oamd::OamdUpdate second = update;
second.frame_state.objects[2].blocks[0].render.divergence_q = 0;
builder.submit_frame(40, &second, 0);
std::vector<joc::adm::Track> trajectory;
CHECK(builder.build(96000, joc::adm::TrajectoryMode::Compact, &trajectory).ok());
CHECK(trajectory[1].blocks.size() >= 2);
CHECK(std::fabs(trajectory[1].blocks.front().meta.gain - 0.5) < 1e-12);
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};
@@ -2421,6 +2526,8 @@ int main() {
test_speaker_surround_fold();
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();