Truly independent web browser
LibAudio+LibMedia+Tests: Map top middle speakers of CoreAudio layouts
Aliaksandr Kalenikb88bc8aa25bc
diff --git a/Libraries/LibAudio/ChannelMap.h b/Libraries/LibAudio/ChannelMap.hindex d923177d75b..38589a26b89 100644--- a/Libraries/LibAudio/ChannelMap.h+++ b/Libraries/LibAudio/ChannelMap.h@@ -35,6 +35,8 @@ enum class Channel : u8 {TopBackLeft,TopBackCenter,TopBackRight,+ TopSideLeft,+ TopSideRight,Count,};@@ -177,6 +179,10 @@ constexpr StringView audio_channel_to_string(Channel channel)return "TopBackCenter"sv;case Audio::Channel::TopBackRight:return "TopBackRight"sv;+ case Audio::Channel::TopSideLeft:+ return "TopSideLeft"sv;+ case Audio::Channel::TopSideRight:+ return "TopSideRight"sv;case Audio::Channel::Count:break;}diff --git a/Libraries/LibAudio/CoreAudioChannelLayout.cpp b/Libraries/LibAudio/CoreAudioChannelLayout.cppindex 05f33b1b225..9e02fa16f6c 100644--- a/Libraries/LibAudio/CoreAudioChannelLayout.cpp+++ b/Libraries/LibAudio/CoreAudioChannelLayout.cpp@@ -41,7 +41,9 @@ static void check_core_audio_channel_layout_size(AudioChannelLayout const& layouC(TopBackRight, Channel::TopBackRight) \C(LeftTopFront, Channel::TopFrontLeft) \C(CenterTopFront, Channel::TopFrontCenter) \- C(RightTopFront, Channel::TopFrontRight)+ C(RightTopFront, Channel::TopFrontRight) \+ C(LeftTopMiddle, Channel::TopSideLeft) \+ C(RightTopMiddle, Channel::TopSideRight)ErrorOr<ChannelMap> core_audio_channel_layout_to_channel_map(AudioChannelLayout const& channel_layout, u32 channel_layout_size){diff --git a/Libraries/LibMedia/Containers/WAV/Reader.cpp b/Libraries/LibMedia/Containers/WAV/Reader.cppindex 9520e803509..36de47f1f54 100644--- a/Libraries/LibMedia/Containers/WAV/Reader.cpp+++ b/Libraries/LibMedia/Containers/WAV/Reader.cpp@@ -57,11 +57,12 @@ static DecoderErrorOr<CodecID> codec_id_for(u16 format_tag, u16 bits_per_sample)return DecoderError::format(DecoderErrorCategory::NotImplemented, "WAVE format {:#x} with {} bits per sample is not supported", format_tag, bits_per_sample);}-// The speaker position bits of the extensible header follow the order of Audio::Channel after Unknown.+// The speaker position bits of the extensible header follow the order of Audio::Channel after Unknown, up to+// TopBackRight, the last position the header defines.static Audio::ChannelMap channel_map_for(u16 channel_count, u32 speaker_position_mask){Vector<Audio::Channel, Audio::ChannelMap::capacity()> channels;- for (u8 position = 1; position < to_underlying(Audio::Channel::Count); position++) {+ for (u8 position = 1; position <= to_underlying(Audio::Channel::TopBackRight); position++) {if ((speaker_position_mask >> (position - 1)) & 1)channels.append(static_cast<Audio::Channel>(position));}diff --git a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cppindex 6ec5384d584..6c74d6ac1e9 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp+++ b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp@@ -26,7 +26,9 @@ namespace Media::FFmpeg {C(Audio::Channel::TopFrontRight, AVChannel::AV_CHAN_TOP_FRONT_RIGHT) \C(Audio::Channel::TopBackLeft, AVChannel::AV_CHAN_TOP_BACK_LEFT) \C(Audio::Channel::TopBackCenter, AVChannel::AV_CHAN_TOP_BACK_CENTER) \- C(Audio::Channel::TopBackRight, AVChannel::AV_CHAN_TOP_BACK_RIGHT)+ C(Audio::Channel::TopBackRight, AVChannel::AV_CHAN_TOP_BACK_RIGHT) \+ C(Audio::Channel::TopSideLeft, AVChannel::AV_CHAN_TOP_SIDE_LEFT) \+ C(Audio::Channel::TopSideRight, AVChannel::AV_CHAN_TOP_SIDE_RIGHT)ErrorOr<Audio::ChannelMap> FFmpegChannelLayout::to_channel_map() const{diff --git a/Tests/LibAudio/CMakeLists.txt b/Tests/LibAudio/CMakeLists.txtindex 2d208ff6bb1..e66c9bbcb31 100644--- a/Tests/LibAudio/CMakeLists.txt+++ b/Tests/LibAudio/CMakeLists.txt@@ -6,5 +6,9 @@ set(TEST_SOURCESTestSpscAudioFrameRing.cpp)+if (APPLE)+ list(APPEND TEST_SOURCES TestCoreAudioChannelLayout.cpp)+endif()+target_sources(TestLibAudio PRIVATE ${TEST_SOURCES})target_link_libraries(TestLibAudio PRIVATE LibAudio LibCore LibThreading)diff --git a/Tests/LibAudio/TestCoreAudioChannelLayout.cpp b/Tests/LibAudio/TestCoreAudioChannelLayout.cppnew file mode 100644index 00000000000..b0344483ac3--- /dev/null+++ b/Tests/LibAudio/TestCoreAudioChannelLayout.cpp@@ -0,0 +1,61 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Array.h>+#include <LibAudio/CoreAudioChannelLayout.h>+#include <LibTest/TestCase.h>++#include <CoreAudioTypes/CoreAudioBaseTypes.h>++TEST_CASE(layout_with_top_middle_speakers_names_every_channel)+{+ Array<AudioChannelLabel, 8> labels {+ kAudioChannelLabel_Left,+ kAudioChannelLabel_Right,+ kAudioChannelLabel_Center,+ kAudioChannelLabel_LFEScreen,+ kAudioChannelLabel_LeftSurround,+ kAudioChannelLabel_RightSurround,+ kAudioChannelLabel_LeftTopMiddle,+ kAudioChannelLabel_RightTopMiddle,+ };+ constexpr size_t layout_size = offsetof(AudioChannelLayout, mChannelDescriptions) + labels.size() * sizeof(AudioChannelDescription);+ alignas(AudioChannelLayout) Array<u8, layout_size> layout_storage {};+ auto& layout = *reinterpret_cast<AudioChannelLayout*>(layout_storage.data());+ layout.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;+ layout.mNumberChannelDescriptions = labels.size();+ // The SDK declares one description, so the rest are reached through the storage rather than past that array.+ auto* descriptions = reinterpret_cast<AudioChannelDescription*>(layout_storage.data() + offsetof(AudioChannelLayout, mChannelDescriptions));+ for (size_t i = 0; i < labels.size(); i++)+ descriptions[i].mChannelLabel = labels[i];++ auto channel_map = MUST(Audio::core_audio_channel_layout_to_channel_map(layout, layout_size));+ EXPECT_EQ(channel_map,+ Audio::ChannelMap(+ Audio::Channel::FrontLeft,+ Audio::Channel::FrontRight,+ Audio::Channel::FrontCenter,+ Audio::Channel::LowFrequency,+ Audio::Channel::BackLeft,+ Audio::Channel::BackRight,+ Audio::Channel::TopSideLeft,+ Audio::Channel::TopSideRight));+}++TEST_CASE(channel_bitmap_with_top_middle_speakers_names_every_channel)+{+ AudioChannelLayout layout {};+ layout.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelBitmap;+ layout.mChannelBitmap = kAudioChannelBit_Left | kAudioChannelBit_Right | kAudioChannelBit_LeftTopMiddle | kAudioChannelBit_RightTopMiddle;++ auto channel_map = MUST(Audio::core_audio_channel_layout_to_channel_map(layout, sizeof(layout)));+ EXPECT_EQ(channel_map,+ Audio::ChannelMap(+ Audio::Channel::FrontLeft,+ Audio::Channel::FrontRight,+ Audio::Channel::TopSideLeft,+ Audio::Channel::TopSideRight));+}diff --git a/Tests/LibMedia/CMakeLists.txt b/Tests/LibMedia/CMakeLists.txtindex 47ddcd3e21b..06b64892edc 100644--- a/Tests/LibMedia/CMakeLists.txt+++ b/Tests/LibMedia/CMakeLists.txt@@ -80,6 +80,8 @@ endif()target_sources(TestLibMedia PRIVATE ${TEST_SOURCES})target_link_libraries(TestLibMedia PRIVATE LibAudio LibFileSystem LibGfx LibIPC LibMedia LibMediaCodecs LibThreading)+# The header of FFmpegChannelLayout includes FFmpeg's headers.+target_link_libraries(TestLibMedia PRIVATE PkgConfig::AVCODEC PkgConfig::AVUTIL)if ("TestFFmpegVulkanDecode.cpp" IN_LIST TEST_SOURCES)target_link_libraries(TestLibMedia PRIVATE LibPlatformFeatures Vulkan::Vulkan)diff --git a/Tests/LibMedia/TestFFmpegAudioConverter.cpp b/Tests/LibMedia/TestFFmpegAudioConverter.cppindex 1c27f94607c..0863521a88a 100644--- a/Tests/LibMedia/TestFFmpegAudioConverter.cpp+++ b/Tests/LibMedia/TestFFmpegAudioConverter.cpp@@ -7,6 +7,7 @@#include <AK/NonnullOwnPtr.h>#include <LibAudio/SampleSpecification.h>#include <LibMedia/FFmpeg/FFmpegAudioConverter.h>+#include <LibMedia/FFmpeg/FFmpegChannelLayout.h>#include <LibMediaCodecs/AudioBlock.h>#include <LibMediaCodecs/Limits.h>#include <LibTest/TestCase.h>@@ -39,3 +40,59 @@ TEST_CASE(output_specifications_outside_the_sample_rate_range_are_refused)EXPECT(set_output_sample_rate(1).is_error());EXPECT(set_output_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE + 1).is_error());}++TEST_CASE(top_side_speakers_are_ffmpegs_top_side_channels)+{+ auto channel_map = Audio::ChannelMap(+ Audio::Channel::FrontLeft,+ Audio::Channel::FrontRight,+ Audio::Channel::FrontCenter,+ Audio::Channel::LowFrequency,+ Audio::Channel::BackLeft,+ Audio::Channel::BackRight,+ Audio::Channel::TopSideLeft,+ Audio::Channel::TopSideRight);+ auto layout = MUST(Media::FFmpeg::FFmpegChannelLayout::from_channel_map(Media::FFmpeg::FFmpegFunctions::bundled(), channel_map));+ EXPECT_EQ(layout.layout().order, AV_CHANNEL_ORDER_NATIVE);+ if (layout.layout().order != AV_CHANNEL_ORDER_NATIVE)+ return;+ EXPECT_EQ(layout.layout().u.mask, AV_CH_LAYOUT_5POINT1_BACK | AV_CH_TOP_SIDE_LEFT | AV_CH_TOP_SIDE_RIGHT);+ EXPECT_EQ(TRY_OR_FAIL(layout.to_channel_map()), channel_map);+}++TEST_CASE(stereo_converts_into_a_layout_with_top_side_speakers)+{+ auto layout_with_top_side_speakers = Audio::ChannelMap(+ Audio::Channel::FrontLeft,+ Audio::Channel::FrontRight,+ Audio::Channel::FrontCenter,+ Audio::Channel::LowFrequency,+ Audio::Channel::BackLeft,+ Audio::Channel::BackRight,+ Audio::Channel::TopSideLeft,+ Audio::Channel::TopSideRight);+ auto converter = MUST(Media::FFmpeg::FFmpegAudioConverter::try_create());+ MUST(converter->set_output_sample_specification(Audio::SampleSpecification { 48'000, layout_with_top_side_speakers }));++ constexpr size_t frame_count = 480;+ constexpr float front_left_sample = 0.5f;+ constexpr float front_right_sample = -0.25f;+ Media::AudioBlock stereo_block;+ stereo_block.initialize(Audio::SampleSpecification { 48'000, Audio::ChannelMap::stereo() }, 0, frame_count);+ for (size_t frame = 0; frame < frame_count; frame++) {+ stereo_block.set_sample(0, frame, front_left_sample);+ stereo_block.set_sample(1, frame, front_right_sample);+ }+ MUST(converter->push_block(stereo_block));++ Media::AudioBlock converted_block;+ MUST(converter->retrieve_block(converted_block));+ EXPECT_EQ(converted_block.sample_specification().channel_map(), layout_with_top_side_speakers);+ EXPECT_EQ(converted_block.frame_count(), frame_count);+ for (size_t frame = 0; frame < converted_block.frame_count(); frame++) {+ EXPECT_APPROXIMATE(converted_block.sample(0, frame), front_left_sample);+ EXPECT_APPROXIMATE(converted_block.sample(1, frame), front_right_sample);+ for (size_t channel = 2; channel < converted_block.channel_count(); channel++)+ EXPECT_EQ(converted_block.sample(channel, frame), 0.0f);+ }+}diff --git a/Tests/LibMedia/TestWAVDemuxer.cpp b/Tests/LibMedia/TestWAVDemuxer.cppindex 42b2352456e..8b37c41fe4a 100644--- a/Tests/LibMedia/TestWAVDemuxer.cpp+++ b/Tests/LibMedia/TestWAVDemuxer.cpp@@ -255,6 +255,10 @@ TEST_CASE(demuxer_reads_speaker_positions_from_an_extensible_header)EXPECT_EQ(channel_map_of({ .channel_count = 4, .speaker_position_mask = front_left | front_right | side_left | side_right }), Audio::ChannelMap(Channel::FrontLeft, Channel::FrontRight, Channel::SideLeft, Channel::SideRight));EXPECT_EQ(channel_map_of({ .channel_count = 6, .speaker_position_mask = 0x3F }), Audio::ChannelMap::surround_5_1());+ // The bits past the last position the header defines name no speaker.+ constexpr u32 reserved_positions = (1 << 18) | (1 << 19);+ EXPECT_EQ(channel_map_of({ .channel_count = 2, .speaker_position_mask = reserved_positions }), Audio::ChannelMap::stereo());+// Positions that do not account for every channel are ignored in favour of the usual layout for the count.EXPECT_EQ(channel_map_of({ .channel_count = 6, .speaker_position_mask = front_left | front_right }), Audio::ChannelMap::surround_5_1());EXPECT_EQ(channel_map_of({ .channel_count = 6 }), Audio::ChannelMap::surround_5_1());