1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibAudio+LibRIFF: Move general RIFF handling to LibRIFF

This splits the RIFFTypes header/TU into the WAV specific parts, which
move to WavTypes.h, as well as the general RIFF parts which move to the
new LibRIFF.

Sidenote for the spec comments: even though they are linked from a site
that explains the WAV format, the document is the (an) overall RIFF spec
from Microsoft. A better source may be used later; the changes to the
header are as minimal as possible.
This commit is contained in:
kleines Filmröllchen 2023-10-12 15:02:39 +02:00 committed by Andrew Kaster
parent 015c47da51
commit d125d16287
11 changed files with 53 additions and 33 deletions

View file

@ -436,6 +436,7 @@ if (BUILD_LAGOM)
PDF PDF
Protocol Protocol
Regex Regex
RIFF
SoftGPU SoftGPU
SQL SQL
Syntax Syntax

View file

@ -13,7 +13,6 @@ shared_library("LibAudio") {
"PlaybackStream.cpp", "PlaybackStream.cpp",
"QOALoader.cpp", "QOALoader.cpp",
"QOATypes.cpp", "QOATypes.cpp",
"RIFFTypes.cpp",
"SampleFormats.cpp", "SampleFormats.cpp",
"UserSampleQueue.cpp", "UserSampleQueue.cpp",
"VorbisComment.cpp", "VorbisComment.cpp",

View file

@ -47,6 +47,7 @@ add_subdirectory(LibPCIDB)
add_subdirectory(LibPDF) add_subdirectory(LibPDF)
add_subdirectory(LibProtocol) add_subdirectory(LibProtocol)
add_subdirectory(LibRegex) add_subdirectory(LibRegex)
add_subdirectory(LibRIFF)
add_subdirectory(LibSanitizer) add_subdirectory(LibSanitizer)
add_subdirectory(LibSoftGPU) add_subdirectory(LibSoftGPU)
add_subdirectory(LibSQL) add_subdirectory(LibSQL)

View file

@ -2,7 +2,6 @@ set(SOURCES
GenericTypes.cpp GenericTypes.cpp
SampleFormats.cpp SampleFormats.cpp
Loader.cpp Loader.cpp
RIFFTypes.cpp
WavLoader.cpp WavLoader.cpp
FlacLoader.cpp FlacLoader.cpp
FlacWriter.cpp FlacWriter.cpp
@ -36,7 +35,7 @@ if (HAVE_PULSEAUDIO)
endif() endif()
serenity_lib(LibAudio audio) serenity_lib(LibAudio audio)
target_link_libraries(LibAudio PRIVATE LibCore LibIPC LibThreading LibUnicode LibCrypto) target_link_libraries(LibAudio PRIVATE LibCore LibRIFF LibIPC LibThreading LibUnicode LibCrypto)
if (HAVE_PULSEAUDIO) if (HAVE_PULSEAUDIO)
target_link_libraries(LibAudio PRIVATE pulse) target_link_libraries(LibAudio PRIVATE pulse)

View file

@ -7,7 +7,7 @@
#include "WavLoader.h" #include "WavLoader.h"
#include "LoaderError.h" #include "LoaderError.h"
#include "RIFFTypes.h" #include "WavTypes.h"
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/Endian.h> #include <AK/Endian.h>
#include <AK/FixedArray.h> #include <AK/FixedArray.h>
@ -36,7 +36,7 @@ bool WavLoaderPlugin::sniff(SeekableStream& stream)
return false; return false;
auto wave = stream.read_value<RIFF::ChunkID>(); auto wave = stream.read_value<RIFF::ChunkID>();
return !wave.is_error() && wave.value() == RIFF::wave_subformat_id; return !wave.is_error() && wave.value() == Wav::wave_subformat_id;
} }
ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> WavLoaderPlugin::create(NonnullOwnPtr<SeekableStream> stream) ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> WavLoaderPlugin::create(NonnullOwnPtr<SeekableStream> stream)
@ -191,14 +191,14 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
TRY(m_stream->read_value<LittleEndian<u32>>()); // File size header TRY(m_stream->read_value<LittleEndian<u32>>()); // File size header
auto wave = TRY(m_stream->read_value<RIFF::ChunkID>()); auto wave = TRY(m_stream->read_value<RIFF::ChunkID>());
CHECK(wave == RIFF::wave_subformat_id, LoaderError::Category::Format, "WAVE subformat id invalid"); CHECK(wave == Wav::wave_subformat_id, LoaderError::Category::Format, "WAVE subformat id invalid");
auto format_chunk = TRY(m_stream->read_value<RIFF::Chunk>()); auto format_chunk = TRY(m_stream->read_value<RIFF::Chunk>());
CHECK(format_chunk.id.as_ascii_string() == RIFF::format_chunk_id, LoaderError::Category::Format, "FMT chunk id invalid"); CHECK(format_chunk.id.as_ascii_string() == Wav::format_chunk_id, LoaderError::Category::Format, "FMT chunk id invalid");
auto format_stream = format_chunk.data_stream(); auto format_stream = format_chunk.data_stream();
u16 audio_format = TRY(format_stream.read_value<LittleEndian<u16>>()); u16 audio_format = TRY(format_stream.read_value<LittleEndian<u16>>());
CHECK(audio_format == to_underlying(RIFF::WaveFormat::Pcm) || audio_format == to_underlying(RIFF::WaveFormat::IEEEFloat) || audio_format == to_underlying(RIFF::WaveFormat::Extensible), CHECK(audio_format == to_underlying(Wav::WaveFormat::Pcm) || audio_format == to_underlying(Wav::WaveFormat::IEEEFloat) || audio_format == to_underlying(Wav::WaveFormat::Extensible),
LoaderError::Category::Unimplemented, "Audio format not supported"); LoaderError::Category::Unimplemented, "Audio format not supported");
m_num_channels = TRY(format_stream.read_value<LittleEndian<u16>>()); m_num_channels = TRY(format_stream.read_value<LittleEndian<u16>>());
@ -211,7 +211,7 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
u16 bits_per_sample = TRY(format_stream.read_value<LittleEndian<u16>>()); u16 bits_per_sample = TRY(format_stream.read_value<LittleEndian<u16>>());
if (audio_format == to_underlying(RIFF::WaveFormat::Extensible)) { if (audio_format == to_underlying(Wav::WaveFormat::Extensible)) {
CHECK(format_chunk.size == 40, LoaderError::Category::Format, "Extensible fmt size is not 40 bytes"); CHECK(format_chunk.size == 40, LoaderError::Category::Format, "Extensible fmt size is not 40 bytes");
// Discard everything until the GUID. // Discard everything until the GUID.
@ -220,12 +220,12 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
// Get the underlying audio format from the first two bytes of GUID // Get the underlying audio format from the first two bytes of GUID
u16 guid_subformat = TRY(format_stream.read_value<LittleEndian<u16>>()); u16 guid_subformat = TRY(format_stream.read_value<LittleEndian<u16>>());
CHECK(guid_subformat == to_underlying(RIFF::WaveFormat::Pcm) || guid_subformat == to_underlying(RIFF::WaveFormat::IEEEFloat), LoaderError::Category::Unimplemented, "GUID SubFormat not supported"); CHECK(guid_subformat == to_underlying(Wav::WaveFormat::Pcm) || guid_subformat == to_underlying(Wav::WaveFormat::IEEEFloat), LoaderError::Category::Unimplemented, "GUID SubFormat not supported");
audio_format = guid_subformat; audio_format = guid_subformat;
} }
if (audio_format == to_underlying(RIFF::WaveFormat::Pcm)) { if (audio_format == to_underlying(Wav::WaveFormat::Pcm)) {
CHECK(bits_per_sample == 8 || bits_per_sample == 16 || bits_per_sample == 24, LoaderError::Category::Unimplemented, "PCM bits per sample not supported"); CHECK(bits_per_sample == 8 || bits_per_sample == 16 || bits_per_sample == 24, LoaderError::Category::Unimplemented, "PCM bits per sample not supported");
// We only support 8-24 bit audio right now because other formats are uncommon // We only support 8-24 bit audio right now because other formats are uncommon
@ -236,7 +236,7 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
} else if (bits_per_sample == 24) { } else if (bits_per_sample == 24) {
m_sample_format = PcmSampleFormat::Int24; m_sample_format = PcmSampleFormat::Int24;
} }
} else if (audio_format == to_underlying(RIFF::WaveFormat::IEEEFloat)) { } else if (audio_format == to_underlying(Wav::WaveFormat::IEEEFloat)) {
CHECK(bits_per_sample == 32 || bits_per_sample == 64, LoaderError::Category::Unimplemented, "Float bits per sample not supported"); CHECK(bits_per_sample == 32 || bits_per_sample == 64, LoaderError::Category::Unimplemented, "Float bits per sample not supported");
// Again, only the common 32 and 64 bit // Again, only the common 32 and 64 bit
@ -256,7 +256,7 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
bool found_data = false; bool found_data = false;
while (!found_data) { while (!found_data) {
auto chunk_header = TRY(m_stream->read_value<RIFF::ChunkID>()); auto chunk_header = TRY(m_stream->read_value<RIFF::ChunkID>());
if (chunk_header == RIFF::data_chunk_id) { if (chunk_header == Wav::data_chunk_id) {
found_data = true; found_data = true;
} else { } else {
TRY(m_stream->seek(-RIFF::chunk_id_size, SeekMode::FromCurrentPosition)); TRY(m_stream->seek(-RIFF::chunk_id_size, SeekMode::FromCurrentPosition));
@ -269,7 +269,7 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
} }
auto list = maybe_list.release_value(); auto list = maybe_list.release_value();
if (list.type == RIFF::info_chunk_id) { if (list.type == Wav::info_chunk_id) {
auto maybe_error = load_wav_info_block(move(list.chunks)); auto maybe_error = load_wav_info_block(move(list.chunks));
if (maybe_error.is_error()) if (maybe_error.is_error())
dbgln("WAV Warning: INFO chunk invalid, error: {}", maybe_error.release_error()); dbgln("WAV Warning: INFO chunk invalid, error: {}", maybe_error.release_error());

View file

@ -14,7 +14,7 @@
#include <AK/Span.h> #include <AK/Span.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <LibAudio/Loader.h> #include <LibAudio/Loader.h>
#include <LibAudio/RIFFTypes.h> #include <LibRIFF/Types.h>
namespace Audio { namespace Audio {

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2023, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
namespace Audio::Wav {
static constexpr StringView const wave_subformat_id = "WAVE"sv;
static constexpr StringView const data_chunk_id = "data"sv;
static constexpr StringView const info_chunk_id = "INFO"sv;
static constexpr StringView const format_chunk_id = "fmt "sv;
// Constants for handling WAVE header data.
enum class WaveFormat : u32 {
Pcm = 0x0001, // WAVE_FORMAT_PCM
IEEEFloat = 0x0003, // WAVE_FORMAT_IEEE_FLOAT
ALaw = 0x0006, // 8-bit ITU-T G.711 A-law
MuLaw = 0x0007, // 8-bit ITU-T G.711 µ-law
Extensible = 0xFFFE, // Determined by SubFormat
};
}

View file

@ -7,6 +7,7 @@
#include <AK/Endian.h> #include <AK/Endian.h>
#include <LibAudio/WavLoader.h> #include <LibAudio/WavLoader.h>
#include <LibAudio/WavTypes.h>
#include <LibAudio/WavWriter.h> #include <LibAudio/WavWriter.h>
namespace Audio { namespace Audio {
@ -111,7 +112,7 @@ ErrorOr<void> WavWriter::write_header()
static u32 fmt_size = 16; static u32 fmt_size = 16;
TRY(m_file->write_value(fmt_size)); TRY(m_file->write_value(fmt_size));
static u16 audio_format = to_underlying(RIFF::WaveFormat::Pcm); static u16 audio_format = to_underlying(Wav::WaveFormat::Pcm);
TRY(m_file->write_value(audio_format)); TRY(m_file->write_value(audio_format));
TRY(m_file->write_value(m_num_channels)); TRY(m_file->write_value(m_num_channels));

View file

@ -0,0 +1,6 @@
set(SOURCES
Types.cpp
)
serenity_lib(LibRIFF riff)
target_link_libraries(LibRIFF PRIVATE LibCore)

View file

@ -4,13 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "RIFFTypes.h" #include "Types.h"
#include <AK/Endian.h> #include <AK/Endian.h>
#include <AK/Stream.h> #include <AK/Stream.h>
#include <AK/Try.h> #include <AK/Try.h>
#include <AK/TypeCasts.h> #include <AK/TypeCasts.h>
namespace Audio::RIFF { namespace RIFF {
ErrorOr<ChunkID> ChunkID::read_from_stream(Stream& stream) ErrorOr<ChunkID> ChunkID::read_from_stream(Stream& stream)
{ {

View file

@ -12,24 +12,10 @@
#include <AK/MemoryStream.h> #include <AK/MemoryStream.h>
#include <AK/Types.h> #include <AK/Types.h>
// RIFF-specific type definitions necessary for handling WAVE files. namespace RIFF {
namespace Audio::RIFF {
static constexpr StringView const riff_magic = "RIFF"sv; static constexpr StringView const riff_magic = "RIFF"sv;
static constexpr StringView const wave_subformat_id = "WAVE"sv;
static constexpr StringView const data_chunk_id = "data"sv;
static constexpr StringView const list_chunk_id = "LIST"sv; static constexpr StringView const list_chunk_id = "LIST"sv;
static constexpr StringView const info_chunk_id = "INFO"sv;
static constexpr StringView const format_chunk_id = "fmt "sv;
// Constants for handling WAVE header data.
enum class WaveFormat : u32 {
Pcm = 0x0001, // WAVE_FORMAT_PCM
IEEEFloat = 0x0003, // WAVE_FORMAT_IEEE_FLOAT
ALaw = 0x0006, // 8-bit ITU-T G.711 A-law
MuLaw = 0x0007, // 8-bit ITU-T G.711 µ-law
Extensible = 0xFFFE, // Determined by SubFormat
};
static constexpr size_t const chunk_id_size = 4; static constexpr size_t const chunk_id_size = 4;