|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Ladybird Contributors |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <LibMedia/Audio/Loader.h> |
| 8 | +#include <LibMedia/Audio/SampleFormats.h> |
| 9 | +#include <LibTest/TestCase.h> |
| 10 | + |
| 11 | +TEST_CASE(ffmpeg_loader_vorbis_format) |
| 12 | +{ |
| 13 | + // Test that FFmpegLoader correctly reports the PCM format for Vorbis audio |
| 14 | + // Vorbis audio is typically decoded as floating-point samples |
| 15 | + auto loader = TRY_OR_FAIL(Audio::Loader::create("vorbis/44_1Khz_stereo.ogg")); |
| 16 | + |
| 17 | + EXPECT_EQ(loader->format_name(), "ogg"); |
| 18 | + EXPECT_EQ(loader->sample_rate(), 44100u); |
| 19 | + EXPECT_EQ(loader->num_channels(), 2); |
| 20 | + |
| 21 | + // Vorbis audio is decoded as Float32 by FFmpeg |
| 22 | + auto pcm_format = loader->pcm_format(); |
| 23 | + EXPECT_EQ(pcm_format, Audio::PcmSampleFormat::Float32); |
| 24 | +} |
| 25 | + |
| 26 | +TEST_CASE(ffmpeg_loader_wav_format) |
| 27 | +{ |
| 28 | + // Test that FFmpegLoader correctly reports the PCM format for WAV audio |
| 29 | + // WAV files with 32-bit samples should be detected as Int32 |
| 30 | + auto loader = TRY_OR_FAIL(Audio::Loader::create("WAV/tone_44100_stereo.wav")); |
| 31 | + |
| 32 | + EXPECT_EQ(loader->format_name(), "wav"); |
| 33 | + EXPECT_EQ(loader->sample_rate(), 44100u); |
| 34 | + EXPECT_EQ(loader->num_channels(), 2); |
| 35 | + |
| 36 | + // The WAV test files are 32-bit PCM, which FFmpeg reports as S32 |
| 37 | + // However, the WAV loader (not FFmpeg) might be used, so we just verify |
| 38 | + // that pcm_format() returns a valid format without crashing |
| 39 | + auto pcm_format = loader->pcm_format(); |
| 40 | + |
| 41 | + // Valid formats are Uint8, Int16, Int24, Int32, or Float32 |
| 42 | + bool is_valid_format = pcm_format == Audio::PcmSampleFormat::Uint8 |
| 43 | + || pcm_format == Audio::PcmSampleFormat::Int16 |
| 44 | + || pcm_format == Audio::PcmSampleFormat::Int24 |
| 45 | + || pcm_format == Audio::PcmSampleFormat::Int32 |
| 46 | + || pcm_format == Audio::PcmSampleFormat::Float32; |
| 47 | + |
| 48 | + EXPECT(is_valid_format); |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE(ffmpeg_loader_basic_functionality) |
| 52 | +{ |
| 53 | + // Basic test to ensure FFmpegLoader can load and decode audio |
| 54 | + auto loader = TRY_OR_FAIL(Audio::Loader::create("vorbis/44_1Khz_stereo.ogg")); |
| 55 | + |
| 56 | + // Verify we can read some samples without crashing |
| 57 | + auto samples = TRY_OR_FAIL(loader->get_more_samples(1024)); |
| 58 | + |
| 59 | + EXPECT_EQ(samples.size(), 1024u); |
| 60 | + EXPECT(loader->loaded_samples() >= 1024); |
| 61 | +} |
0 commit comments