mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 15: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:
parent
015c47da51
commit
d125d16287
11 changed files with 53 additions and 33 deletions
6
Userland/Libraries/LibRIFF/CMakeLists.txt
Normal file
6
Userland/Libraries/LibRIFF/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
set(SOURCES
|
||||
Types.cpp
|
||||
)
|
||||
|
||||
serenity_lib(LibRIFF riff)
|
||||
target_link_libraries(LibRIFF PRIVATE LibCore)
|
76
Userland/Libraries/LibRIFF/Types.cpp
Normal file
76
Userland/Libraries/LibRIFF/Types.cpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Types.h"
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/Stream.h>
|
||||
#include <AK/Try.h>
|
||||
#include <AK/TypeCasts.h>
|
||||
|
||||
namespace 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() };
|
||||
}
|
||||
|
||||
}
|
49
Userland/Libraries/LibRIFF/Types.h
Normal file
49
Userland/Libraries/LibRIFF/Types.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* 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>
|
||||
|
||||
namespace RIFF {
|
||||
|
||||
static constexpr StringView const riff_magic = "RIFF"sv;
|
||||
static constexpr StringView const list_chunk_id = "LIST"sv;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue