1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 10:57:36 +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

@ -2,7 +2,6 @@ set(SOURCES
GenericTypes.cpp
SampleFormats.cpp
Loader.cpp
RIFFTypes.cpp
WavLoader.cpp
FlacLoader.cpp
FlacWriter.cpp
@ -36,7 +35,7 @@ if (HAVE_PULSEAUDIO)
endif()
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)
target_link_libraries(LibAudio PRIVATE pulse)

View file

@ -1,76 +0,0 @@
/*
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "RIFFTypes.h"
#include <AK/Endian.h>
#include <AK/Stream.h>
#include <AK/Try.h>
#include <AK/TypeCasts.h>
namespace Audio::RIFF {
ErrorOr<ChunkID> ChunkID::read_from_stream(Stream& stream)
{
Array<u8, chunk_id_size> id;
TRY(stream.read_until_filled(id.span()));
return ChunkID { id };
}
ErrorOr<Chunk> Chunk::read_from_stream(Stream& stream)
{
auto id = TRY(stream.read_value<ChunkID>());
u32 size = TRY(stream.read_value<LittleEndian<u32>>());
auto data = TRY(FixedArray<u8>::create(size));
TRY(stream.read_until_filled(data.span()));
// RIFF chunks may have trailing padding to align to x86 "words" (i.e. 2 bytes).
if (is<SeekableStream>(stream)) {
if (!stream.is_eof()) {
auto stream_position = TRY(static_cast<SeekableStream&>(stream).tell());
if (stream_position % 2 != 0)
TRY(static_cast<SeekableStream&>(stream).seek(1, SeekMode::FromCurrentPosition));
}
} else {
dbgln("RIFF Warning: Cannot align stream to 2-byte boundary, next chunk may be bogus!");
}
return Chunk {
id,
size,
move(data),
};
}
ErrorOr<List> List::read_from_stream(Stream& stream)
{
auto type = TRY(stream.read_value<ChunkID>());
Vector<Chunk> chunks;
while (!stream.is_eof())
TRY(chunks.try_append(TRY(stream.read_value<Chunk>())));
return List {
.type = type,
.chunks = move(chunks),
};
}
StringView ChunkID::as_ascii_string() const
{
return StringView { id_data.span() };
}
bool ChunkID::operator==(StringView const& other_string) const
{
return as_ascii_string() == other_string;
}
FixedMemoryStream Chunk::data_stream()
{
return FixedMemoryStream { data.span() };
}
}

View file

@ -1,63 +0,0 @@
/*
* Copyright (c) 2018-2023, the SerenityOS developers.
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FixedArray.h>
#include <AK/Forward.h>
#include <AK/MemoryStream.h>
#include <AK/Types.h>
// RIFF-specific type definitions necessary for handling WAVE files.
namespace Audio::RIFF {
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 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;
struct ChunkID {
static ErrorOr<ChunkID> read_from_stream(Stream& stream);
StringView as_ascii_string() const;
bool operator==(ChunkID const&) const = default;
bool operator==(StringView const&) const;
Array<u8, chunk_id_size> id_data;
};
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf page 11 (Chunks)
struct Chunk {
static ErrorOr<Chunk> read_from_stream(Stream& stream);
FixedMemoryStream data_stream();
ChunkID id;
u32 size;
FixedArray<u8> data;
};
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf page 23 (LIST type)
struct List {
static ErrorOr<List> read_from_stream(Stream& stream);
ChunkID type;
Vector<Chunk> chunks;
};
}

View file

@ -7,7 +7,7 @@
#include "WavLoader.h"
#include "LoaderError.h"
#include "RIFFTypes.h"
#include "WavTypes.h"
#include <AK/Debug.h>
#include <AK/Endian.h>
#include <AK/FixedArray.h>
@ -36,7 +36,7 @@ bool WavLoaderPlugin::sniff(SeekableStream& stream)
return false;
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)
@ -191,14 +191,14 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
TRY(m_stream->read_value<LittleEndian<u32>>()); // File size header
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>());
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();
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");
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>>());
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");
// 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
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;
}
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");
// 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) {
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");
// Again, only the common 32 and 64 bit
@ -256,7 +256,7 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
bool found_data = false;
while (!found_data) {
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;
} else {
TRY(m_stream->seek(-RIFF::chunk_id_size, SeekMode::FromCurrentPosition));
@ -269,7 +269,7 @@ MaybeLoaderError WavLoaderPlugin::parse_header()
}
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));
if (maybe_error.is_error())
dbgln("WAV Warning: INFO chunk invalid, error: {}", maybe_error.release_error());

View file

@ -14,7 +14,7 @@
#include <AK/Span.h>
#include <AK/StringView.h>
#include <LibAudio/Loader.h>
#include <LibAudio/RIFFTypes.h>
#include <LibRIFF/Types.h>
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 <LibAudio/WavLoader.h>
#include <LibAudio/WavTypes.h>
#include <LibAudio/WavWriter.h>
namespace Audio {
@ -111,7 +112,7 @@ ErrorOr<void> WavWriter::write_header()
static u32 fmt_size = 16;
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(m_num_channels));