diff --git a/CMakeLists.txt b/CMakeLists.txt index ef9fe80..0bd7856 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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$<$: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 diff --git a/include/joc_stream.h b/include/joc_stream.h index 137b99e..c469dea 100644 --- a/include/joc_stream.h +++ b/include/joc_stream.h @@ -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 { diff --git a/include/joc_version.h b/include/joc_version.h index d887d51..4b6bcf8 100644 --- a/include/joc_version.h +++ b/include/joc_version.h @@ -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 3 +#define JOC_VERSION_STRING "1.3.3" #endif /* JOC_VERSION_H */ diff --git a/src/api/joc_stream_api.cpp b/src/api/joc_stream_api.cpp index 350d178..2db3d31 100644 --- a/src/api/joc_stream_api.cpp +++ b/src/api/joc_stream_api.cpp @@ -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(consumed); + } + return pushed; + } std::size_t consumed = 0; const std::uint32_t channels = input->channels != 0u ? input->channels : 6u; const joc::Status pushed = diff --git a/src/binaural/multichannel_layout.cpp b/src/binaural/multichannel_layout.cpp new file mode 100644 index 0000000..8daa91a --- /dev/null +++ b/src/binaural/multichannel_layout.cpp @@ -0,0 +1,106 @@ +#include "binaural/multichannel_layout.h" + +#include + +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(destination) * frames + frame] = + interleaved[frame * static_cast(layout.channels) + channel]; + } + if (!spec.lfe) { + ++slot; + } + } + return Status::success(); +} + +} // namespace joc::multichannel diff --git a/src/binaural/multichannel_layout.h b/src/binaural/multichannel_layout.h new file mode 100644 index 0000000..2c7355f --- /dev/null +++ b/src/binaural/multichannel_layout.h @@ -0,0 +1,39 @@ +// Channel order and fixed ADM directions for the multichannel binaural path. +#pragma once + +#include +#include +#include + +#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 diff --git a/src/binaural/pcm_resampler.cpp b/src/binaural/pcm_resampler.cpp new file mode 100644 index 0000000..294fdb5 --- /dev/null +++ b/src/binaural/pcm_resampler.cpp @@ -0,0 +1,101 @@ +#include "binaural/pcm_resampler.h" + +#include +#include + +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* 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(channels)); + return Status::success(); + } + + const double step = static_cast(source_rate) / static_cast(target_rate); + const double cutoff = (target_rate < source_rate) + ? static_cast(target_rate) / source_rate + : 1.0; + const std::size_t output_frames = static_cast(std::llround( + static_cast(frames) * target_rate / source_rate)); + output->assign(output_frames * static_cast(channels), 0.0f); + + for (std::size_t out_frame = 0; out_frame < output_frames; ++out_frame) { + const double position = static_cast(out_frame) * step; + const std::int64_t base = static_cast(std::floor(position)); + const double fraction = position - static_cast(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(frames)) { + continue; + } + sum += static_cast( + input[static_cast(index) * + static_cast(channels) + + channel]) * + weights[tap + kHalfTaps]; + } + output->at(out_frame * static_cast(channels) + channel) = + static_cast(sum / weight_sum); + } + } + return Status::success(); +} + +} // namespace joc::multichannel diff --git a/src/binaural/pcm_resampler.h b/src/binaural/pcm_resampler.h new file mode 100644 index 0000000..39f82f4 --- /dev/null +++ b/src/binaural/pcm_resampler.h @@ -0,0 +1,18 @@ +// Rational sample-rate conversion for the multichannel binaural frontend. +#pragma once + +#include +#include +#include + +#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* output); + +} // namespace joc::multichannel diff --git a/src/cli/joc_multichannel.cpp b/src/cli/joc_multichannel.cpp new file mode 100644 index 0000000..1c3b890 --- /dev/null +++ b/src/cli/joc_multichannel.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include + +#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(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::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(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(wav.channels)); + return 2; + } + + std::vector 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(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 objects16(kSourceChannels * kFrameSamples, 0.0f); + std::vector rendered; + for (std::size_t start = 0, frame = 0; start < frames; start += kFrameSamples, ++frame) { + const std::size_t count = std::min(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(destination) * kFrameSamples + sample] = + pcm48[(start + sample) * layout->channels + channel]; + } + } + status = runtime.submit_frame(objects16.data(), frame == 0 ? &initial : nullptr, + static_cast(frame), 0, 0); + if (!status.ok()) { + std::fprintf(stderr, "render: %s\n", status.message().c_str()); + return 1; + } + std::vector chunk; + runtime.take_output(&chunk); + rendered.insert(rendered.end(), chunk.begin(), chunk.end()); + } + std::vector 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 stereo(rendered.size()); + for (std::size_t index = 0; index < rendered.size(); ++index) { + stereo[index] = static_cast(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::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 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(wav.channels), static_cast(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(target_rate), stereo.size() / 2); + return 0; +} diff --git a/src/io/wav_pcm.cpp b/src/io/wav_pcm.cpp new file mode 100644 index 0000000..8814024 --- /dev/null +++ b/src/io/wav_pcm.cpp @@ -0,0 +1,207 @@ +#include "io/wav_pcm.h" + +#include +#include +#include +#include + +namespace joc::io { + +namespace { + +std::uint16_t read_u16(const std::uint8_t* data) { + return static_cast(data[0] | (data[1] << 8)); +} + +std::uint32_t read_u32(const std::uint8_t* data) { + return static_cast(data[0]) | + (static_cast(data[1]) << 8) | + (static_cast(data[2]) << 16) | + (static_cast(data[3]) << 24); +} + +void write_u16(std::vector* out, std::uint16_t value) { + out->push_back(static_cast(value & 0xFFu)); + out->push_back(static_cast((value >> 8) & 0xFFu)); +} + +void write_u32(std::vector* out, std::uint32_t value) { + out->push_back(static_cast(value & 0xFFu)); + out->push_back(static_cast((value >> 8) & 0xFFu)); + out->push_back(static_cast((value >> 16) & 0xFFu)); + out->push_back(static_cast((value >> 24) & 0xFFu)); +} + +std::string fourcc(const std::uint8_t* data) { + return std::string(reinterpret_cast(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(value); + } + if (bits == 16u) { + const std::int16_t value = static_cast(read_u16(data)); + return static_cast(value) / 32768.0f; + } + if (bits == 24u) { + std::int32_t value = static_cast(data[0]) | + (static_cast(data[1]) << 8) | + (static_cast(data[2]) << 16); + if ((value & 0x00800000) != 0) { + value |= static_cast(0xFF000000u); + } + return static_cast(value) / 8388608.0f; + } + if (bits == 32u) { + const std::int32_t value = static_cast(read_u32(data)); + return static_cast(static_cast(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 bytes((std::istreambuf_iterator(stream)), + std::istreambuf_iterator()); + 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(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(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::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(frames * channels * sizeof(float)); + std::vector 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(channels * sizeof(float))); + write_u16(&header, static_cast(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(header.data()), + static_cast(header.size())); + stream.write(reinterpret_cast(interleaved), + static_cast(data_bytes)); + if (!stream) { + return Status::fail(JOC_ERR_IO, stage::kOutput, + "cannot write WAV output: " + path); + } + return Status::success(); +} + +} // namespace joc::io diff --git a/src/io/wav_pcm.h b/src/io/wav_pcm.h new file mode 100644 index 0000000..62550f1 --- /dev/null +++ b/src/io/wav_pcm.h @@ -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 +#include +#include + +#include "foundation/status.h" + +namespace joc::io { + +struct WavAudio { + std::uint32_t sample_rate = 0; + std::uint16_t channels = 0; + std::vector 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 diff --git a/src/stream/stream.cpp b/src/stream/stream.cpp index bc599d6..3d2d81b 100644 --- a/src/stream/stream.cpp +++ b/src/stream/stream.cpp @@ -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(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(channels), + config_.gain_db); + if (!configured.ok()) { + return configured; + } + } else if (pcm_input_.channels() != static_cast(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(JOC_OUTPUT_CHANNELS) * kFrameSamples; @@ -590,9 +647,11 @@ Status Stream::render_objects16(const std::vector& objects16) { if (speaker_enabled_) { std::string error; const Status stepped = - speaker::step(&speaker_, objects16, pending_metadata_.has_update - ? &pending_metadata_.update - : nullptr, + speaker::step(&speaker_, objects16, + pending_metadata_.has_update + ? &pending_metadata_.update + : (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& 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(info_.frames_out), pending_metadata_.outer_offset, static_cast(config_.object_delay_samples)); @@ -638,7 +698,8 @@ Status Stream::render_objects16(const std::vector& objects16) { Status Stream::render_rosella_objects16(const std::vector& 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(info_.frames_out), pending_metadata_.outer_offset, static_cast(config_.object_delay_samples)); diff --git a/src/stream/stream.h b/src/stream/stream.h index 5740926..8422ca7 100644 --- a/src/stream/stream.h +++ b/src/stream/stream.h @@ -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 bed5_; std::vector lfe_; + virtual_speaker::Input pcm_input_; + oamd::OamdUpdate fixed_update_{}; + bool has_fixed_update_ = false; std::vector objects_pending_; std::size_t objects_read_offset_ = 0; std::vector objects_frame_; diff --git a/src/virtual_speaker/virtual_speaker.cpp b/src/virtual_speaker/virtual_speaker.cpp new file mode 100644 index 0000000..493b927 --- /dev/null +++ b/src/virtual_speaker/virtual_speaker.cpp @@ -0,0 +1,161 @@ +#include "virtual_speaker/virtual_speaker.h" + +#include +#include +#include + +#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(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(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(channels_)); + return Status::success(); +} + +bool Input::frame_ready() const { + return layout_ != nullptr && + pending_.size() - read_offset_ >= + kFrameSamples * static_cast(channels_); +} + +Status Input::take_frame(std::vector* 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(destination) * kFrameSamples + sample] = + source[sample * static_cast(channels_) + channel] * + static_cast(gain_); + } + } + read_offset_ += kFrameSamples * static_cast(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(read_offset_)); + read_offset_ = 0; + } + return Status::success(); +} + +} // namespace joc::virtual_speaker diff --git a/src/virtual_speaker/virtual_speaker.h b/src/virtual_speaker/virtual_speaker.h new file mode 100644 index 0000000..81df060 --- /dev/null +++ b/src/virtual_speaker/virtual_speaker.h @@ -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 +#include +#include +#include + +#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* objects16); + +private: + const Layout* layout_ = nullptr; + int channels_ = 0; + double gain_ = 1.0; + oamd::OamdUpdate fixed_update_{}; + std::vector pending_; + std::size_t read_offset_ = 0; +}; + +} // namespace joc::virtual_speaker diff --git a/tests/test_core.cpp b/tests/test_core.cpp index 43afc6d..b05be73 100644 --- a/tests/test_core.cpp +++ b/tests/test_core.cpp @@ -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" @@ -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 pcm(12); + for (int index = 0; index < 12; ++index) { + pcm[static_cast(index)] = 0.01f * index; + } + std::vector 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 dc(200, 0.5f); + std::vector 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 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 input(200, 0.5f); + std::vector output; + CHECK(joc::multichannel::resample_interleaved( + input.data(), 100, 2, rate, 48000, &output) + .ok()); + const std::size_t expected = static_cast( + std::llround(100.0 * 48000.0 / static_cast(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();