2021-06-25 13:54:14 +02:00
/*
2022-01-13 12:07:00 +01:00
* Copyright ( c ) 2021 , kleines Filmröllchen < filmroellchen @ serenityos . org >
2021-06-25 13:54:14 +02:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <AK/Debug.h>
2023-01-08 19:23:00 -05:00
# include <AK/DeprecatedFlyString.h>
2022-12-04 18:02:33 +00:00
# include <AK/DeprecatedString.h>
2021-12-17 18:42:36 +01:00
# include <AK/FixedArray.h>
2021-06-25 13:54:14 +02:00
# include <AK/Format.h>
2022-01-29 16:51:02 +01:00
# include <AK/IntegralMath.h>
2021-07-17 18:29:28 +02:00
# include <AK/Math.h>
2021-08-01 05:42:09 -06:00
# include <AK/ScopeGuard.h>
2021-12-16 23:18:49 +01:00
# include <AK/StdLibExtras.h>
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
# include <AK/Try.h>
2021-12-17 18:42:36 +01:00
# include <AK/TypedTransfer.h>
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
# include <AK/UFixedBigInt.h>
# include <LibAudio/FlacLoader.h>
# include <LibAudio/FlacTypes.h>
# include <LibAudio/LoaderError.h>
2022-04-23 12:30:36 +02:00
# include <LibAudio/Resampler.h>
2022-01-14 01:14:24 +01:00
# include <LibCore/MemoryStream.h>
# include <LibCore/Stream.h>
2021-06-25 13:54:14 +02:00
namespace Audio {
2023-01-22 05:09:11 +01:00
FlacLoaderPlugin : : FlacLoaderPlugin ( NonnullOwnPtr < SeekableStream > stream )
2022-12-05 00:41:23 +01:00
: LoaderPlugin ( move ( stream ) )
2021-06-25 13:54:14 +02:00
{
}
2023-01-28 20:12:17 +00:00
Result < NonnullOwnPtr < FlacLoaderPlugin > , LoaderError > FlacLoaderPlugin : : create ( StringView path )
2021-06-25 13:54:14 +02:00
{
2022-12-05 00:41:23 +01:00
auto stream = LOADER_TRY ( Core : : Stream : : BufferedFile : : create ( LOADER_TRY ( Core : : Stream : : File : : open ( path , Core : : Stream : : OpenMode : : Read ) ) ) ) ;
auto loader = make < FlacLoaderPlugin > ( move ( stream ) ) ;
LOADER_TRY ( loader - > initialize ( ) ) ;
return loader ;
2021-06-25 13:54:14 +02:00
}
2023-01-28 20:12:17 +00:00
Result < NonnullOwnPtr < FlacLoaderPlugin > , LoaderError > FlacLoaderPlugin : : create ( Bytes buffer )
2021-06-25 13:54:14 +02:00
{
2022-12-07 15:47:44 +01:00
auto stream = LOADER_TRY ( Core : : Stream : : FixedMemoryStream : : construct ( buffer ) ) ;
2022-12-05 00:41:23 +01:00
auto loader = make < FlacLoaderPlugin > ( move ( stream ) ) ;
LOADER_TRY ( loader - > initialize ( ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
2022-12-05 00:41:23 +01:00
return loader ;
}
MaybeLoaderError FlacLoaderPlugin : : initialize ( )
{
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
TRY ( parse_header ( ) ) ;
TRY ( reset ( ) ) ;
return { } ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.5 STREAM
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
MaybeLoaderError FlacLoaderPlugin : : parse_header ( )
2021-06-25 13:54:14 +02:00
{
2023-01-22 05:09:11 +01:00
auto bit_input = LOADER_TRY ( BigEndianInputBitStream : : construct ( MaybeOwned < AK : : Stream > ( * m_stream ) ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
// A mixture of VERIFY and the non-crashing TRY().
2022-12-04 18:02:33 +00:00
# define FLAC_VERIFY(check, category, msg) \
do { \
if ( ! ( check ) ) { \
return LoaderError { category , static_cast < size_t > ( m_data_start_location ) , DeprecatedString : : formatted ( " FLAC header: {} " , msg ) } ; \
} \
2021-06-25 13:54:14 +02:00
} while ( 0 )
// Magic number
2022-01-14 01:14:24 +01:00
u32 flac = LOADER_TRY ( bit_input - > read_bits < u32 > ( 32 ) ) ;
2021-06-25 13:54:14 +02:00
m_data_start_location + = 4 ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( flac = = 0x664C6143 , LoaderError : : Category : : Format , " Magic number must be 'flaC' " ) ; // "flaC"
2021-06-25 13:54:14 +02:00
// Receive the streaminfo block
2022-01-14 01:14:24 +01:00
auto streaminfo = TRY ( next_meta_block ( * bit_input ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( streaminfo . type = = FlacMetadataBlockType : : STREAMINFO , LoaderError : : Category : : Format , " First block must be STREAMINFO " ) ;
2022-12-07 15:47:44 +01:00
auto streaminfo_data_memory = LOADER_TRY ( Core : : Stream : : FixedMemoryStream : : construct ( streaminfo . data . bytes ( ) ) ) ;
2023-01-22 05:09:11 +01:00
auto streaminfo_data = LOADER_TRY ( BigEndianInputBitStream : : construct ( MaybeOwned < AK : : Stream > ( * streaminfo_data_memory ) ) ) ;
2021-06-25 13:54:14 +02:00
2022-06-16 21:23:31 +02:00
// 11.10 METADATA_BLOCK_STREAMINFO
2022-01-14 01:14:24 +01:00
m_min_block_size = LOADER_TRY ( streaminfo_data - > read_bits < u16 > ( 16 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( m_min_block_size > = 16 , LoaderError : : Category : : Format , " Minimum block size must be 16 " ) ;
2022-01-14 01:14:24 +01:00
m_max_block_size = LOADER_TRY ( streaminfo_data - > read_bits < u16 > ( 16 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( m_max_block_size > = 16 , LoaderError : : Category : : Format , " Maximum block size " ) ;
2022-01-14 01:14:24 +01:00
m_min_frame_size = LOADER_TRY ( streaminfo_data - > read_bits < u32 > ( 24 ) ) ;
m_max_frame_size = LOADER_TRY ( streaminfo_data - > read_bits < u32 > ( 24 ) ) ;
m_sample_rate = LOADER_TRY ( streaminfo_data - > read_bits < u32 > ( 20 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( m_sample_rate < = 655350 , LoaderError : : Category : : Format , " Sample rate " ) ;
2022-01-14 01:14:24 +01:00
m_num_channels = LOADER_TRY ( streaminfo_data - > read_bits < u8 > ( 3 ) ) + 1 ; // 0 = one channel
2021-06-25 13:54:14 +02:00
2022-01-14 01:14:24 +01:00
u8 bits_per_sample = LOADER_TRY ( streaminfo_data - > read_bits < u8 > ( 5 ) ) + 1 ;
2021-06-25 13:54:14 +02:00
if ( bits_per_sample = = 8 ) {
// FIXME: Signed/Unsigned issues?
m_sample_format = PcmSampleFormat : : Uint8 ;
} else if ( bits_per_sample = = 16 ) {
m_sample_format = PcmSampleFormat : : Int16 ;
} else if ( bits_per_sample = = 24 ) {
m_sample_format = PcmSampleFormat : : Int24 ;
} else if ( bits_per_sample = = 32 ) {
m_sample_format = PcmSampleFormat : : Int32 ;
} else {
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( false , LoaderError : : Category : : Format , " Sample bit depth invalid " ) ;
2021-06-25 13:54:14 +02:00
}
2022-01-14 01:14:24 +01:00
m_total_samples = LOADER_TRY ( streaminfo_data - > read_bits < u64 > ( 36 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( m_total_samples > 0 , LoaderError : : Category : : Format , " Number of samples is zero " ) ;
2021-06-25 13:54:14 +02:00
// Parse checksum into a buffer first
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
[[maybe_unused]] u128 md5_checksum ;
2022-01-14 01:14:24 +01:00
VERIFY ( streaminfo_data - > is_aligned_to_byte_boundary ( ) ) ;
auto md5_bytes_read = LOADER_TRY ( streaminfo_data - > read ( md5_checksum . bytes ( ) ) ) ;
2022-04-15 13:33:02 +01:00
FLAC_VERIFY ( md5_bytes_read . size ( ) = = md5_checksum . my_size ( ) , LoaderError : : Category : : IO , " MD5 Checksum size " ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
md5_checksum . bytes ( ) . copy_to ( { m_md5_checksum , sizeof ( m_md5_checksum ) } ) ;
2021-06-25 13:54:14 +02:00
// Parse other blocks
[[maybe_unused]] u16 meta_blocks_parsed = 1 ;
[[maybe_unused]] u16 total_meta_blocks = meta_blocks_parsed ;
FlacRawMetadataBlock block = streaminfo ;
while ( ! block . is_last_block ) {
2022-01-14 01:14:24 +01:00
block = TRY ( next_meta_block ( * bit_input ) ) ;
2021-12-21 15:07:49 -08:00
switch ( block . type ) {
case ( FlacMetadataBlockType : : SEEKTABLE ) :
TRY ( load_seektable ( block ) ) ;
break ;
2022-10-02 18:48:38 +02:00
case FlacMetadataBlockType : : PICTURE :
TRY ( load_picture ( block ) ) ;
break ;
2022-10-02 16:41:12 +02:00
case FlacMetadataBlockType : : APPLICATION :
// Note: Third-party library can encode specific data in this.
dbgln ( " Unknown 'Application' metadata block encountered. " ) ;
[[fallthrough]] ;
2022-10-02 16:27:18 +02:00
case FlacMetadataBlockType : : PADDING :
// Note: A padding block is empty and does not need any treatment.
2021-12-21 15:07:49 -08:00
default :
// TODO: Parse the remaining metadata block types.
break ;
}
2021-06-25 13:54:14 +02:00
+ + total_meta_blocks ;
}
2022-05-06 22:14:16 +02:00
dbgln_if ( AFLACLOADER_DEBUG , " Parsed FLAC header: blocksize {}-{}{}, framesize {}-{}, {}Hz, {}bit, {} channels, {} samples total ({:.2f}s), MD5 {}, data start at {:x} bytes, {} headers total (skipped {}) " , m_min_block_size , m_max_block_size , is_fixed_blocksize_stream ( ) ? " (constant) " : " " , m_min_frame_size , m_max_frame_size , m_sample_rate , pcm_bits_per_sample ( m_sample_format ) , m_num_channels , m_total_samples , static_cast < float > ( m_total_samples ) / static_cast < float > ( m_sample_rate ) , md5_checksum , m_data_start_location , total_meta_blocks , total_meta_blocks - meta_blocks_parsed ) ;
2021-06-25 13:54:14 +02:00
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return { } ;
2021-06-25 13:54:14 +02:00
}
2022-10-02 18:48:38 +02:00
// 11.19. METADATA_BLOCK_PICTURE
MaybeLoaderError FlacLoaderPlugin : : load_picture ( FlacRawMetadataBlock & block )
{
2022-12-07 15:47:44 +01:00
auto memory_stream = LOADER_TRY ( Core : : Stream : : FixedMemoryStream : : construct ( block . data . bytes ( ) ) ) ;
2023-01-22 05:09:11 +01:00
auto picture_block_bytes = LOADER_TRY ( BigEndianInputBitStream : : construct ( MaybeOwned < AK : : Stream > ( * memory_stream ) ) ) ;
2022-10-02 18:48:38 +02:00
PictureData picture { } ;
picture . type = static_cast < ID3PictureType > ( LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ) ;
auto const mime_string_length = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
// Note: We are seeking before reading the value to ensure that we stayed inside buffer's size.
auto offset_before_seeking = memory_stream - > offset ( ) ;
2023-01-22 05:09:11 +01:00
LOADER_TRY ( memory_stream - > seek ( mime_string_length , SeekMode : : FromCurrentPosition ) ) ;
2022-10-02 18:48:38 +02:00
picture . mime_string = { block . data . bytes ( ) . data ( ) + offset_before_seeking , ( size_t ) mime_string_length } ;
auto const description_string_length = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
offset_before_seeking = memory_stream - > offset ( ) ;
2023-01-22 05:09:11 +01:00
LOADER_TRY ( memory_stream - > seek ( description_string_length , SeekMode : : FromCurrentPosition ) ) ;
2022-10-02 18:48:38 +02:00
picture . description_string = Vector < u32 > { Span < u32 > { reinterpret_cast < u32 * > ( block . data . bytes ( ) . data ( ) + offset_before_seeking ) , ( size_t ) description_string_length } } ;
picture . width = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
picture . height = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
picture . color_depth = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
picture . colors = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
auto const picture_size = LOADER_TRY ( picture_block_bytes - > read_bits ( 32 ) ) ;
offset_before_seeking = memory_stream - > offset ( ) ;
2023-01-22 05:09:11 +01:00
LOADER_TRY ( memory_stream - > seek ( picture_size , SeekMode : : FromCurrentPosition ) ) ;
2022-10-02 18:48:38 +02:00
picture . data = Vector < u8 > { Span < u8 > { block . data . bytes ( ) . data ( ) + offset_before_seeking , ( size_t ) picture_size } } ;
m_pictures . append ( move ( picture ) ) ;
return { } ;
}
2022-06-16 21:23:31 +02:00
// 11.13. METADATA_BLOCK_SEEKTABLE
2021-12-21 15:07:49 -08:00
MaybeLoaderError FlacLoaderPlugin : : load_seektable ( FlacRawMetadataBlock & block )
{
2022-12-07 15:47:44 +01:00
auto memory_stream = LOADER_TRY ( Core : : Stream : : FixedMemoryStream : : construct ( block . data . bytes ( ) ) ) ;
2023-01-22 05:09:11 +01:00
auto seektable_bytes = LOADER_TRY ( BigEndianInputBitStream : : construct ( MaybeOwned < AK : : Stream > ( * memory_stream ) ) ) ;
2021-12-21 15:07:49 -08:00
for ( size_t i = 0 ; i < block . length / 18 ; + + i ) {
2022-06-16 21:23:31 +02:00
// 11.14. SEEKPOINT
2021-12-21 15:07:49 -08:00
FlacSeekPoint seekpoint {
. sample_index = LOADER_TRY ( seektable_bytes - > read_bits < u64 > ( 64 ) ) ,
. byte_offset = LOADER_TRY ( seektable_bytes - > read_bits < u64 > ( 64 ) ) ,
. num_samples = LOADER_TRY ( seektable_bytes - > read_bits < u16 > ( 16 ) )
} ;
m_seektable . append ( seekpoint ) ;
}
2021-12-21 15:43:18 -08:00
dbgln_if ( AFLACLOADER_DEBUG , " Loaded seektable of size {} " , m_seektable . size ( ) ) ;
2021-12-21 15:07:49 -08:00
return { } ;
}
2022-06-16 21:23:31 +02:00
// 11.6 METADATA_BLOCK
2022-01-14 01:14:24 +01:00
ErrorOr < FlacRawMetadataBlock , LoaderError > FlacLoaderPlugin : : next_meta_block ( BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
2022-06-16 21:23:31 +02:00
// 11.7 METADATA_BLOCK_HEADER
2022-01-14 01:14:24 +01:00
bool is_last_block = LOADER_TRY ( bit_input . read_bit ( ) ) ;
2021-06-25 13:54:14 +02:00
// The block type enum constants agree with the specification
2022-01-14 01:14:24 +01:00
FlacMetadataBlockType type = ( FlacMetadataBlockType ) LOADER_TRY ( bit_input . read_bits < u8 > ( 7 ) ) ;
2021-06-25 13:54:14 +02:00
m_data_start_location + = 1 ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( type ! = FlacMetadataBlockType : : INVALID , LoaderError : : Category : : Format , " Invalid metadata block " ) ;
2021-06-25 13:54:14 +02:00
2022-01-14 01:14:24 +01:00
u32 block_length = LOADER_TRY ( bit_input . read_bits < u32 > ( 24 ) ) ;
2021-06-25 13:54:14 +02:00
m_data_start_location + = 3 ;
2021-12-21 15:43:18 -08:00
// Blocks can be zero-sized, which would trip up the raw data reader below.
if ( block_length = = 0 )
return FlacRawMetadataBlock {
. is_last_block = is_last_block ,
. type = type ,
. length = 0 ,
. data = LOADER_TRY ( ByteBuffer : : create_uninitialized ( 0 ) )
} ;
2021-09-06 03:29:52 +04:30
auto block_data_result = ByteBuffer : : create_uninitialized ( block_length ) ;
2022-01-20 17:47:39 +00:00
FLAC_VERIFY ( ! block_data_result . is_error ( ) , LoaderError : : Category : : IO , " Out of memory " ) ;
2021-09-06 03:29:52 +04:30
auto block_data = block_data_result . release_value ( ) ;
2022-07-26 20:27:20 +02:00
// Blocks might exceed our buffer size.
auto bytes_left_to_read = block_data . bytes ( ) ;
while ( bytes_left_to_read . size ( ) ) {
auto read_bytes = LOADER_TRY ( bit_input . read ( bytes_left_to_read ) ) ;
bytes_left_to_read = bytes_left_to_read . slice ( read_bytes . size ( ) ) ;
}
2021-06-25 13:54:14 +02:00
m_data_start_location + = block_length ;
return FlacRawMetadataBlock {
is_last_block ,
type ,
block_length ,
block_data ,
} ;
}
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
# undef FLAC_VERIFY
2021-06-25 13:54:14 +02:00
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
MaybeLoaderError FlacLoaderPlugin : : reset ( )
2021-06-25 13:54:14 +02:00
{
2022-07-26 20:54:39 +02:00
TRY ( seek ( 0 ) ) ;
2021-06-25 13:54:14 +02:00
m_current_frame . clear ( ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return { } ;
2021-06-25 13:54:14 +02:00
}
2021-12-21 15:43:18 -08:00
MaybeLoaderError FlacLoaderPlugin : : seek ( int int_sample_index )
2021-06-25 13:54:14 +02:00
{
2021-12-21 15:43:18 -08:00
auto sample_index = static_cast < size_t > ( int_sample_index ) ;
if ( sample_index = = m_loaded_samples )
return { } ;
auto maybe_target_seekpoint = m_seektable . last_matching ( [ sample_index ] ( auto & seekpoint ) { return seekpoint . sample_index < = sample_index ; } ) ;
// No seektable or no fitting entry: Perform normal forward read
if ( ! maybe_target_seekpoint . has_value ( ) ) {
if ( sample_index < m_loaded_samples ) {
2023-01-22 05:09:11 +01:00
LOADER_TRY ( m_stream - > seek ( m_data_start_location , SeekMode : : SetPosition ) ) ;
2021-12-21 15:43:18 -08:00
m_loaded_samples = 0 ;
}
auto to_read = sample_index - m_loaded_samples ;
if ( to_read = = 0 )
return { } ;
dbgln_if ( AFLACLOADER_DEBUG , " Seeking {} samples manually " , to_read ) ;
( void ) TRY ( get_more_samples ( to_read ) ) ;
} else {
auto target_seekpoint = maybe_target_seekpoint . release_value ( ) ;
// When a small seek happens, we may already be closer to the target than the seekpoint.
if ( sample_index - target_seekpoint . sample_index > sample_index - m_loaded_samples ) {
dbgln_if ( AFLACLOADER_DEBUG , " Close enough to target: seeking {} samples manually " , sample_index - m_loaded_samples ) ;
( void ) TRY ( get_more_samples ( sample_index - m_loaded_samples ) ) ;
return { } ;
}
dbgln_if ( AFLACLOADER_DEBUG , " Seeking to seektable: sample index {}, byte offset {}, sample count {} " , target_seekpoint . sample_index , target_seekpoint . byte_offset , target_seekpoint . num_samples ) ;
auto position = target_seekpoint . byte_offset + m_data_start_location ;
2023-01-22 05:09:11 +01:00
if ( m_stream - > seek ( static_cast < i64 > ( position ) , SeekMode : : SetPosition ) . is_error ( ) )
2022-12-04 18:02:33 +00:00
return LoaderError { LoaderError : : Category : : IO , m_loaded_samples , DeprecatedString : : formatted ( " Invalid seek position {} " , position ) } ;
2021-12-21 15:43:18 -08:00
auto remaining_samples_after_seekpoint = sample_index - m_data_start_location ;
if ( remaining_samples_after_seekpoint > 0 )
( void ) TRY ( get_more_samples ( remaining_samples_after_seekpoint ) ) ;
m_loaded_samples = target_seekpoint . sample_index ;
}
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return { } ;
2021-06-25 13:54:14 +02:00
}
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
LoaderSamples FlacLoaderPlugin : : get_more_samples ( size_t max_bytes_to_read_from_input )
2021-06-25 13:54:14 +02:00
{
2021-11-15 22:27:28 +01:00
ssize_t remaining_samples = static_cast < ssize_t > ( m_total_samples - m_loaded_samples ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
if ( remaining_samples < = 0 )
LibAudio+Userland: Use new audio queue in client-server communication
Previously, we were sending Buffers to the server whenever we had new
audio data for it. This meant that for every audio enqueue action, we
needed to create a new shared memory anonymous buffer, send that
buffer's file descriptor over IPC (+recfd on the other side) and then
map the buffer into the audio server's memory to be able to play it.
This was fine for sending large chunks of audio data, like when playing
existing audio files. However, in the future we want to move to
real-time audio in some applications like Piano. This means that the
size of buffers that are sent need to be very small, as just the size of
a buffer itself is part of the audio latency. If we were to try
real-time audio with the existing system, we would run into problems
really quickly. Dealing with a continuous stream of new anonymous files
like the current audio system is rather expensive, as we need Kernel
help in multiple places. Additionally, every enqueue incurs an IPC call,
which are not optimized for >1000 calls/second (which would be needed
for real-time audio with buffer sizes of ~40 samples). So a fundamental
change in how we handle audio sending in userspace is necessary.
This commit moves the audio sending system onto a shared single producer
circular queue (SSPCQ) (introduced with one of the previous commits).
This queue is intended to live in shared memory and be accessed by
multiple processes at the same time. It was specifically written to
support the audio sending case, so e.g. it only supports a single
producer (the audio client). Now, audio sending follows these general
steps:
- The audio client connects to the audio server.
- The audio client creates a SSPCQ in shared memory.
- The audio client sends the SSPCQ's file descriptor to the audio server
with the set_buffer() IPC call.
- The audio server receives the SSPCQ and maps it.
- The audio client signals start of playback with start_playback().
- At the same time:
- The audio client writes its audio data into the shared-memory queue.
- The audio server reads audio data from the shared-memory queue(s).
Both sides have additional before-queue/after-queue buffers, depending
on the exact application.
- Pausing playback is just an IPC call, nothing happens to the buffer
except that the server stops reading from it until playback is
resumed.
- Muting has nothing to do with whether audio data is read or not.
- When the connection closes, the queues are unmapped on both sides.
This should already improve audio playback performance in a bunch of
places.
Implementation & commit notes:
- Audio loaders don't create LegacyBuffers anymore. LegacyBuffer is kept
for WavLoader, see previous commit message.
- Most intra-process audio data passing is done with FixedArray<Sample>
or Vector<Sample>.
- Improvements to most audio-enqueuing applications. (If necessary I can
try to extract some of the aplay improvements.)
- New APIs on LibAudio/ClientConnection which allows non-realtime
applications to enqueue audio in big chunks like before.
- Removal of status APIs from the audio server connection for
information that can be directly obtained from the shared queue.
- Split the pause playback API into two APIs with more intuitive names.
I know this is a large commit, and you can kinda tell from the commit
message. It's basically impossible to break this up without hacks, so
please forgive me. These are some of the best changes to the audio
subsystem and I hope that that makes up for this :yaktangle: commit.
:yakring:
2022-02-20 13:01:22 +01:00
return FixedArray < Sample > { } ;
2021-07-22 16:56:05 +02:00
2021-12-21 15:43:18 -08:00
// FIXME: samples_to_read is calculated wrong, because when seeking not all samples are loaded.
2021-07-22 16:56:05 +02:00
size_t samples_to_read = min ( max_bytes_to_read_from_input , remaining_samples ) ;
2022-01-08 20:06:03 +01:00
auto samples = FixedArray < Sample > : : must_create_but_fixme_should_propagate_errors ( samples_to_read ) ;
2021-12-17 18:42:36 +01:00
size_t sample_index = 0 ;
if ( m_unread_data . size ( ) > 0 ) {
size_t to_transfer = min ( m_unread_data . size ( ) , samples_to_read ) ;
dbgln_if ( AFLACLOADER_DEBUG , " Reading {} samples from unread sample buffer (size {}) " , to_transfer , m_unread_data . size ( ) ) ;
AK : : TypedTransfer < Sample > : : move ( samples . data ( ) , m_unread_data . data ( ) , to_transfer ) ;
if ( to_transfer < m_unread_data . size ( ) )
m_unread_data . remove ( 0 , to_transfer ) ;
else
m_unread_data . clear_with_capacity ( ) ;
sample_index + = to_transfer ;
}
while ( sample_index < samples_to_read ) {
TRY ( next_frame ( samples . span ( ) . slice ( sample_index ) ) ) ;
sample_index + = m_current_frame - > sample_count ;
2021-06-25 13:54:14 +02:00
}
2021-12-17 18:42:36 +01:00
m_loaded_samples + = sample_index ;
LibAudio+Userland: Use new audio queue in client-server communication
Previously, we were sending Buffers to the server whenever we had new
audio data for it. This meant that for every audio enqueue action, we
needed to create a new shared memory anonymous buffer, send that
buffer's file descriptor over IPC (+recfd on the other side) and then
map the buffer into the audio server's memory to be able to play it.
This was fine for sending large chunks of audio data, like when playing
existing audio files. However, in the future we want to move to
real-time audio in some applications like Piano. This means that the
size of buffers that are sent need to be very small, as just the size of
a buffer itself is part of the audio latency. If we were to try
real-time audio with the existing system, we would run into problems
really quickly. Dealing with a continuous stream of new anonymous files
like the current audio system is rather expensive, as we need Kernel
help in multiple places. Additionally, every enqueue incurs an IPC call,
which are not optimized for >1000 calls/second (which would be needed
for real-time audio with buffer sizes of ~40 samples). So a fundamental
change in how we handle audio sending in userspace is necessary.
This commit moves the audio sending system onto a shared single producer
circular queue (SSPCQ) (introduced with one of the previous commits).
This queue is intended to live in shared memory and be accessed by
multiple processes at the same time. It was specifically written to
support the audio sending case, so e.g. it only supports a single
producer (the audio client). Now, audio sending follows these general
steps:
- The audio client connects to the audio server.
- The audio client creates a SSPCQ in shared memory.
- The audio client sends the SSPCQ's file descriptor to the audio server
with the set_buffer() IPC call.
- The audio server receives the SSPCQ and maps it.
- The audio client signals start of playback with start_playback().
- At the same time:
- The audio client writes its audio data into the shared-memory queue.
- The audio server reads audio data from the shared-memory queue(s).
Both sides have additional before-queue/after-queue buffers, depending
on the exact application.
- Pausing playback is just an IPC call, nothing happens to the buffer
except that the server stops reading from it until playback is
resumed.
- Muting has nothing to do with whether audio data is read or not.
- When the connection closes, the queues are unmapped on both sides.
This should already improve audio playback performance in a bunch of
places.
Implementation & commit notes:
- Audio loaders don't create LegacyBuffers anymore. LegacyBuffer is kept
for WavLoader, see previous commit message.
- Most intra-process audio data passing is done with FixedArray<Sample>
or Vector<Sample>.
- Improvements to most audio-enqueuing applications. (If necessary I can
try to extract some of the aplay improvements.)
- New APIs on LibAudio/ClientConnection which allows non-realtime
applications to enqueue audio in big chunks like before.
- Removal of status APIs from the audio server connection for
information that can be directly obtained from the shared queue.
- Split the pause playback API into two APIs with more intuitive names.
I know this is a large commit, and you can kinda tell from the commit
message. It's basically impossible to break this up without hacks, so
please forgive me. These are some of the best changes to the audio
subsystem and I hope that that makes up for this :yaktangle: commit.
:yakring:
2022-02-20 13:01:22 +01:00
return samples ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.21. FRAME
2021-12-17 18:42:36 +01:00
MaybeLoaderError FlacLoaderPlugin : : next_frame ( Span < Sample > target_vector )
2021-06-25 13:54:14 +02:00
{
2022-12-04 18:02:33 +00:00
# define FLAC_VERIFY(check, category, msg) \
do { \
if ( ! ( check ) ) { \
return LoaderError { category , static_cast < size_t > ( m_current_sample_or_frame ) , DeprecatedString : : formatted ( " FLAC header: {} " , msg ) } ; \
} \
2021-06-25 13:54:14 +02:00
} while ( 0 )
2022-01-14 01:14:24 +01:00
2023-01-22 05:09:11 +01:00
auto bit_stream = LOADER_TRY ( BigEndianInputBitStream : : construct ( MaybeOwned < AK : : Stream > ( * m_stream ) ) ) ;
2021-06-25 13:54:14 +02:00
// TODO: Check the CRC-16 checksum (and others) by keeping track of read data
2022-06-16 21:23:31 +02:00
// 11.22. FRAME_HEADER
2022-01-14 01:14:24 +01:00
u16 sync_code = LOADER_TRY ( bit_stream - > read_bits < u16 > ( 14 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( sync_code = = 0b11111111111110 , LoaderError : : Category : : Format , " Sync code " ) ;
2022-01-14 01:14:24 +01:00
bool reserved_bit = LOADER_TRY ( bit_stream - > read_bit ( ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( reserved_bit = = 0 , LoaderError : : Category : : Format , " Reserved frame header bit " ) ;
2022-06-16 21:23:31 +02:00
// 11.22.2. BLOCKING STRATEGY
2022-01-14 01:14:24 +01:00
[[maybe_unused]] bool blocking_strategy = LOADER_TRY ( bit_stream - > read_bit ( ) ) ;
2021-06-25 13:54:14 +02:00
2022-01-14 01:14:24 +01:00
u32 sample_count = TRY ( convert_sample_count_code ( LOADER_TRY ( bit_stream - > read_bits < u8 > ( 4 ) ) ) ) ;
2021-06-25 13:54:14 +02:00
2022-01-14 01:14:24 +01:00
u32 frame_sample_rate = TRY ( convert_sample_rate_code ( LOADER_TRY ( bit_stream - > read_bits < u8 > ( 4 ) ) ) ) ;
2021-06-25 13:54:14 +02:00
2022-01-14 01:14:24 +01:00
u8 channel_type_num = LOADER_TRY ( bit_stream - > read_bits < u8 > ( 4 ) ) ;
2022-03-17 14:38:24 -06:00
FLAC_VERIFY ( channel_type_num < 0b1011 , LoaderError : : Category : : Format , " Channel assignment " ) ;
2021-06-25 13:54:14 +02:00
FlacFrameChannelType channel_type = ( FlacFrameChannelType ) channel_type_num ;
2022-01-14 01:14:24 +01:00
PcmSampleFormat bit_depth = TRY ( convert_bit_depth_code ( LOADER_TRY ( bit_stream - > read_bits < u8 > ( 3 ) ) ) ) ;
2021-06-25 13:54:14 +02:00
2022-01-14 01:14:24 +01:00
reserved_bit = LOADER_TRY ( bit_stream - > read_bit ( ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
FLAC_VERIFY ( reserved_bit = = 0 , LoaderError : : Category : : Format , " Reserved frame header end bit " ) ;
2021-06-25 13:54:14 +02:00
2022-06-16 21:23:31 +02:00
// 11.22.8. CODED NUMBER
2021-06-25 13:54:14 +02:00
// FIXME: sample number can be 8-56 bits, frame number can be 8-48 bits
2022-01-14 01:14:24 +01:00
m_current_sample_or_frame = LOADER_TRY ( read_utf8_char ( * bit_stream ) ) ;
2021-06-25 13:54:14 +02:00
// Conditional header variables
2022-06-16 21:23:31 +02:00
// 11.22.9. BLOCK SIZE INT
2021-06-25 13:54:14 +02:00
if ( sample_count = = FLAC_BLOCKSIZE_AT_END_OF_HEADER_8 ) {
2022-01-14 01:14:24 +01:00
sample_count = LOADER_TRY ( bit_stream - > read_bits < u32 > ( 8 ) ) + 1 ;
2021-06-25 13:54:14 +02:00
} else if ( sample_count = = FLAC_BLOCKSIZE_AT_END_OF_HEADER_16 ) {
2022-01-14 01:14:24 +01:00
sample_count = LOADER_TRY ( bit_stream - > read_bits < u32 > ( 16 ) ) + 1 ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.22.10. SAMPLE RATE INT
2021-06-25 13:54:14 +02:00
if ( frame_sample_rate = = FLAC_SAMPLERATE_AT_END_OF_HEADER_8 ) {
2022-01-14 01:14:24 +01:00
frame_sample_rate = LOADER_TRY ( bit_stream - > read_bits < u32 > ( 8 ) ) * 1000 ;
2021-06-25 13:54:14 +02:00
} else if ( frame_sample_rate = = FLAC_SAMPLERATE_AT_END_OF_HEADER_16 ) {
2022-01-14 01:14:24 +01:00
frame_sample_rate = LOADER_TRY ( bit_stream - > read_bits < u32 > ( 16 ) ) ;
2021-06-25 13:54:14 +02:00
} else if ( frame_sample_rate = = FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10 ) {
2022-01-14 01:14:24 +01:00
frame_sample_rate = LOADER_TRY ( bit_stream - > read_bits < u32 > ( 16 ) ) * 10 ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.22.11. FRAME CRC
2021-06-25 13:54:14 +02:00
// TODO: check header checksum, see above
2022-01-14 01:14:24 +01:00
[[maybe_unused]] u8 checksum = LOADER_TRY ( bit_stream - > read_bits < u8 > ( 8 ) ) ;
2021-06-25 13:54:14 +02:00
dbgln_if ( AFLACLOADER_DEBUG , " Frame: {} samples, {}bit {}Hz, channeltype {:x}, {} number {}, header checksum {} " , sample_count , pcm_bits_per_sample ( bit_depth ) , frame_sample_rate , channel_type_num , blocking_strategy ? " sample " : " frame " , m_current_sample_or_frame , checksum ) ;
m_current_frame = FlacFrameHeader {
sample_count ,
frame_sample_rate ,
channel_type ,
bit_depth ,
} ;
u8 subframe_count = frame_channel_type_to_channel_count ( channel_type ) ;
Vector < Vector < i32 > > current_subframes ;
current_subframes . ensure_capacity ( subframe_count ) ;
for ( u8 i = 0 ; i < subframe_count ; + + i ) {
2022-01-14 01:14:24 +01:00
FlacSubframeHeader new_subframe = TRY ( next_subframe_header ( * bit_stream , i ) ) ;
Vector < i32 > subframe_samples = TRY ( parse_subframe ( new_subframe , * bit_stream ) ) ;
2021-12-17 18:42:36 +01:00
current_subframes . unchecked_append ( move ( subframe_samples ) ) ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.2. Overview ("The audio data is composed of...")
2022-01-14 01:14:24 +01:00
bit_stream - > align_to_byte_boundary ( ) ;
2021-06-25 13:54:14 +02:00
2022-06-16 21:23:31 +02:00
// 11.23. FRAME_FOOTER
2021-06-25 13:54:14 +02:00
// TODO: check checksum, see above
2022-01-14 01:14:24 +01:00
[[maybe_unused]] u16 footer_checksum = LOADER_TRY ( bit_stream - > read_bits < u16 > ( 16 ) ) ;
2021-12-17 18:42:36 +01:00
dbgln_if ( AFLACLOADER_DEBUG , " Subframe footer checksum: {} " , footer_checksum ) ;
2021-06-25 13:54:14 +02:00
2021-11-15 22:27:28 +01:00
Vector < i32 > left ;
Vector < i32 > right ;
2021-06-25 13:54:14 +02:00
switch ( channel_type ) {
case FlacFrameChannelType : : Mono :
left = right = current_subframes [ 0 ] ;
break ;
case FlacFrameChannelType : : Stereo :
// TODO mix together surround channels on each side?
case FlacFrameChannelType : : StereoCenter :
case FlacFrameChannelType : : Surround4p0 :
case FlacFrameChannelType : : Surround5p0 :
case FlacFrameChannelType : : Surround5p1 :
case FlacFrameChannelType : : Surround6p1 :
case FlacFrameChannelType : : Surround7p1 :
left = current_subframes [ 0 ] ;
right = current_subframes [ 1 ] ;
break ;
case FlacFrameChannelType : : LeftSideStereo :
// channels are left (0) and side (1)
left = current_subframes [ 0 ] ;
right . ensure_capacity ( left . size ( ) ) ;
for ( size_t i = 0 ; i < left . size ( ) ; + + i ) {
// right = left - side
right . unchecked_append ( left [ i ] - current_subframes [ 1 ] [ i ] ) ;
}
break ;
case FlacFrameChannelType : : RightSideStereo :
// channels are side (0) and right (1)
right = current_subframes [ 1 ] ;
left . ensure_capacity ( right . size ( ) ) ;
for ( size_t i = 0 ; i < right . size ( ) ; + + i ) {
// left = right + side
left . unchecked_append ( right [ i ] + current_subframes [ 0 ] [ i ] ) ;
}
break ;
case FlacFrameChannelType : : MidSideStereo :
// channels are mid (0) and side (1)
left . ensure_capacity ( current_subframes [ 0 ] . size ( ) ) ;
right . ensure_capacity ( current_subframes [ 0 ] . size ( ) ) ;
for ( size_t i = 0 ; i < current_subframes [ 0 ] . size ( ) ; + + i ) {
i64 mid = current_subframes [ 0 ] [ i ] ;
i64 side = current_subframes [ 1 ] [ i ] ;
mid * = 2 ;
// prevent integer division errors
left . unchecked_append ( static_cast < i32 > ( ( mid + side ) / 2 ) ) ;
right . unchecked_append ( static_cast < i32 > ( ( mid - side ) / 2 ) ) ;
}
break ;
}
2021-12-17 18:42:36 +01:00
VERIFY ( left . size ( ) = = right . size ( ) & & left . size ( ) = = m_current_frame - > sample_count ) ;
2021-06-25 13:54:14 +02:00
2022-05-06 22:14:16 +02:00
float sample_rescale = static_cast < float > ( 1 < < ( pcm_bits_per_sample ( m_current_frame - > bit_depth ) - 1 ) ) ;
2021-06-25 13:54:14 +02:00
dbgln_if ( AFLACLOADER_DEBUG , " Sample rescaled from {} bits: factor {:.1f} " , pcm_bits_per_sample ( m_current_frame - > bit_depth ) , sample_rescale ) ;
// zip together channels
2021-12-17 18:42:36 +01:00
auto samples_to_directly_copy = min ( target_vector . size ( ) , m_current_frame - > sample_count ) ;
for ( size_t i = 0 ; i < samples_to_directly_copy ; + + i ) {
Sample frame = { left [ i ] / sample_rescale , right [ i ] / sample_rescale } ;
target_vector [ i ] = frame ;
}
2022-01-06 07:07:15 -07:00
// move superfluous data into the class buffer instead
2021-12-17 18:42:36 +01:00
auto result = m_unread_data . try_grow_capacity ( m_current_frame - > sample_count - samples_to_directly_copy ) ;
if ( result . is_error ( ) )
2022-01-06 07:07:15 -07:00
return LoaderError { LoaderError : : Category : : Internal , static_cast < size_t > ( samples_to_directly_copy + m_current_sample_or_frame ) , " Couldn't allocate sample buffer for superfluous data " } ;
2021-12-17 18:42:36 +01:00
for ( size_t i = samples_to_directly_copy ; i < m_current_frame - > sample_count ; + + i ) {
2021-09-23 21:16:03 +02:00
Sample frame = { left [ i ] / sample_rescale , right [ i ] / sample_rescale } ;
2021-12-17 18:42:36 +01:00
m_unread_data . unchecked_append ( frame ) ;
2021-06-25 13:54:14 +02:00
}
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return { } ;
# undef FLAC_VERIFY
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.22.3. INTERCHANNEL SAMPLE BLOCK SIZE
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
ErrorOr < u32 , LoaderError > FlacLoaderPlugin : : convert_sample_count_code ( u8 sample_count_code )
2021-06-25 13:54:14 +02:00
{
// single codes
switch ( sample_count_code ) {
case 0 :
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Reserved block size " } ;
2021-06-25 13:54:14 +02:00
case 1 :
return 192 ;
case 6 :
return FLAC_BLOCKSIZE_AT_END_OF_HEADER_8 ;
case 7 :
return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16 ;
}
if ( sample_count_code > = 2 & & sample_count_code < = 5 ) {
2021-07-17 18:29:28 +02:00
return 576 * AK : : exp2 ( sample_count_code - 2 ) ;
2021-06-25 13:54:14 +02:00
}
2021-07-17 18:29:28 +02:00
return 256 * AK : : exp2 ( sample_count_code - 8 ) ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.22.4. SAMPLE RATE
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
ErrorOr < u32 , LoaderError > FlacLoaderPlugin : : convert_sample_rate_code ( u8 sample_rate_code )
2021-06-25 13:54:14 +02:00
{
switch ( sample_rate_code ) {
case 0 :
return m_sample_rate ;
case 1 :
return 88200 ;
case 2 :
return 176400 ;
case 3 :
return 192000 ;
case 4 :
return 8000 ;
case 5 :
return 16000 ;
case 6 :
return 22050 ;
case 7 :
return 24000 ;
case 8 :
return 32000 ;
case 9 :
return 44100 ;
case 10 :
return 48000 ;
case 11 :
return 96000 ;
case 12 :
return FLAC_SAMPLERATE_AT_END_OF_HEADER_8 ;
case 13 :
return FLAC_SAMPLERATE_AT_END_OF_HEADER_16 ;
case 14 :
return FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10 ;
default :
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Invalid sample rate code " } ;
2021-06-25 13:54:14 +02:00
}
}
2022-06-16 21:23:31 +02:00
// 11.22.6. SAMPLE SIZE
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
ErrorOr < PcmSampleFormat , LoaderError > FlacLoaderPlugin : : convert_bit_depth_code ( u8 bit_depth_code )
2021-06-25 13:54:14 +02:00
{
switch ( bit_depth_code ) {
case 0 :
return m_sample_format ;
case 1 :
return PcmSampleFormat : : Uint8 ;
case 4 :
return PcmSampleFormat : : Int16 ;
case 6 :
return PcmSampleFormat : : Int24 ;
case 3 :
case 7 :
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Reserved sample size " } ;
2021-06-25 13:54:14 +02:00
default :
2022-12-04 18:02:33 +00:00
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , DeprecatedString : : formatted ( " Unsupported sample size {} " , bit_depth_code ) } ;
2021-06-25 13:54:14 +02:00
}
}
2022-06-16 21:23:31 +02:00
// 11.22.5. CHANNEL ASSIGNMENT
2021-06-25 13:54:14 +02:00
u8 frame_channel_type_to_channel_count ( FlacFrameChannelType channel_type )
{
2022-03-17 14:38:24 -06:00
if ( channel_type < = FlacFrameChannelType : : Surround7p1 )
return to_underlying ( channel_type ) + 1 ;
2021-06-25 13:54:14 +02:00
return 2 ;
}
2022-06-16 21:23:31 +02:00
// 11.25. SUBFRAME_HEADER
2022-01-14 01:14:24 +01:00
ErrorOr < FlacSubframeHeader , LoaderError > FlacLoaderPlugin : : next_subframe_header ( BigEndianInputBitStream & bit_stream , u8 channel_index )
2021-06-25 13:54:14 +02:00
{
2021-11-15 22:27:28 +01:00
u8 bits_per_sample = static_cast < u16 > ( pcm_bits_per_sample ( m_current_frame - > bit_depth ) ) ;
2021-06-25 13:54:14 +02:00
// For inter-channel correlation, the side channel needs an extra bit for its samples
switch ( m_current_frame - > channels ) {
2022-03-17 14:38:24 -06:00
case FlacFrameChannelType : : LeftSideStereo :
case FlacFrameChannelType : : MidSideStereo :
2021-06-25 13:54:14 +02:00
if ( channel_index = = 1 ) {
+ + bits_per_sample ;
}
break ;
2022-03-17 14:38:24 -06:00
case FlacFrameChannelType : : RightSideStereo :
2021-06-25 13:54:14 +02:00
if ( channel_index = = 0 ) {
+ + bits_per_sample ;
}
break ;
// "normal" channel types
default :
break ;
}
// zero-bit padding
2022-01-14 01:14:24 +01:00
if ( LOADER_TRY ( bit_stream . read_bit ( ) ) ! = 0 )
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Zero bit padding " } ;
2021-06-25 13:54:14 +02:00
2022-06-16 21:23:31 +02:00
// 11.25.1. SUBFRAME TYPE
2022-01-14 01:14:24 +01:00
u8 subframe_code = LOADER_TRY ( bit_stream . read_bits < u8 > ( 6 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
if ( ( subframe_code > = 0b000010 & & subframe_code < = 0b000111 ) | | ( subframe_code > 0b001100 & & subframe_code < 0b100000 ) )
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Subframe type " } ;
2021-06-25 13:54:14 +02:00
FlacSubframeType subframe_type ;
u8 order = 0 ;
2021-09-06 03:29:52 +04:30
// LPC has the highest bit set
2021-06-25 13:54:14 +02:00
if ( ( subframe_code & 0b100000 ) > 0 ) {
subframe_type = FlacSubframeType : : LPC ;
order = ( subframe_code & 0b011111 ) + 1 ;
} else if ( ( subframe_code & 0b001000 ) > 0 ) {
// Fixed has the third-highest bit set
subframe_type = FlacSubframeType : : Fixed ;
order = ( subframe_code & 0b000111 ) ;
} else {
subframe_type = ( FlacSubframeType ) subframe_code ;
}
2022-06-16 21:23:31 +02:00
// 11.25.2. WASTED BITS PER SAMPLE FLAG
2022-01-14 01:14:24 +01:00
bool has_wasted_bits = LOADER_TRY ( bit_stream . read_bit ( ) ) ;
2021-06-25 13:54:14 +02:00
u8 k = 0 ;
if ( has_wasted_bits ) {
bool current_k_bit = 0 ;
do {
2022-01-14 01:14:24 +01:00
current_k_bit = LOADER_TRY ( bit_stream . read_bit ( ) ) ;
2021-06-25 13:54:14 +02:00
+ + k ;
} while ( current_k_bit ! = 1 ) ;
}
return FlacSubframeHeader {
subframe_type ,
order ,
k ,
bits_per_sample
} ;
}
2022-01-14 01:14:24 +01:00
ErrorOr < Vector < i32 > , LoaderError > FlacLoaderPlugin : : parse_subframe ( FlacSubframeHeader & subframe_header , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
Vector < i32 > samples ;
switch ( subframe_header . type ) {
case FlacSubframeType : : Constant : {
2022-06-16 21:23:31 +02:00
// 11.26. SUBFRAME_CONSTANT
2022-01-14 01:14:24 +01:00
u64 constant_value = LOADER_TRY ( bit_input . read_bits < u64 > ( subframe_header . bits_per_sample - subframe_header . wasted_bits_per_sample ) ) ;
2021-06-25 13:54:14 +02:00
dbgln_if ( AFLACLOADER_DEBUG , " Constant subframe: {} " , constant_value ) ;
samples . ensure_capacity ( m_current_frame - > sample_count ) ;
2021-11-15 22:27:28 +01:00
VERIFY ( subframe_header . bits_per_sample - subframe_header . wasted_bits_per_sample ! = 0 ) ;
i32 constant = sign_extend ( static_cast < u32 > ( constant_value ) , subframe_header . bits_per_sample - subframe_header . wasted_bits_per_sample ) ;
2021-06-25 13:54:14 +02:00
for ( u32 i = 0 ; i < m_current_frame - > sample_count ; + + i ) {
2021-11-15 22:27:28 +01:00
samples . unchecked_append ( constant ) ;
2021-06-25 13:54:14 +02:00
}
break ;
}
case FlacSubframeType : : Fixed : {
dbgln_if ( AFLACLOADER_DEBUG , " Fixed LPC subframe order {} " , subframe_header . order ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
samples = TRY ( decode_fixed_lpc ( subframe_header , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
break ;
}
case FlacSubframeType : : Verbatim : {
dbgln_if ( AFLACLOADER_DEBUG , " Verbatim subframe " ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
samples = TRY ( decode_verbatim ( subframe_header , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
break ;
}
case FlacSubframeType : : LPC : {
dbgln_if ( AFLACLOADER_DEBUG , " Custom LPC subframe order {} " , subframe_header . order ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
samples = TRY ( decode_custom_lpc ( subframe_header , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
break ;
}
default :
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
return LoaderError { LoaderError : : Category : : Unimplemented , static_cast < size_t > ( m_current_sample_or_frame ) , " Unhandled FLAC subframe type " } ;
2021-06-25 13:54:14 +02:00
}
for ( size_t i = 0 ; i < samples . size ( ) ; + + i ) {
samples [ i ] < < = subframe_header . wasted_bits_per_sample ;
}
ResampleHelper < i32 > resampler ( m_current_frame - > sample_rate , m_sample_rate ) ;
return resampler . resample ( samples ) ;
}
2022-06-16 21:23:31 +02:00
// 11.29. SUBFRAME_VERBATIM
2021-08-03 18:01:22 +02:00
// Decode a subframe that isn't actually encoded, usually seen in random data
2022-01-14 01:14:24 +01:00
ErrorOr < Vector < i32 > , LoaderError > FlacLoaderPlugin : : decode_verbatim ( FlacSubframeHeader & subframe , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
2021-08-03 18:01:22 +02:00
Vector < i32 > decoded ;
decoded . ensure_capacity ( m_current_frame - > sample_count ) ;
2021-11-15 22:27:28 +01:00
VERIFY ( subframe . bits_per_sample - subframe . wasted_bits_per_sample ! = 0 ) ;
2021-08-03 18:01:22 +02:00
for ( size_t i = 0 ; i < m_current_frame - > sample_count ; + + i ) {
2021-11-15 22:27:28 +01:00
decoded . unchecked_append ( sign_extend (
2022-01-14 01:14:24 +01:00
LOADER_TRY ( bit_input . read_bits < u32 > ( subframe . bits_per_sample - subframe . wasted_bits_per_sample ) ) ,
2021-11-15 22:27:28 +01:00
subframe . bits_per_sample - subframe . wasted_bits_per_sample ) ) ;
2021-08-03 18:01:22 +02:00
}
return decoded ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.28. SUBFRAME_LPC
2021-06-25 13:54:14 +02:00
// Decode a subframe encoded with a custom linear predictor coding, i.e. the subframe provides the polynomial order and coefficients
2022-01-14 01:14:24 +01:00
ErrorOr < Vector < i32 > , LoaderError > FlacLoaderPlugin : : decode_custom_lpc ( FlacSubframeHeader & subframe , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
Vector < i32 > decoded ;
decoded . ensure_capacity ( m_current_frame - > sample_count ) ;
2021-11-15 22:27:28 +01:00
VERIFY ( subframe . bits_per_sample - subframe . wasted_bits_per_sample ! = 0 ) ;
2021-06-25 13:54:14 +02:00
// warm-up samples
for ( auto i = 0 ; i < subframe . order ; + + i ) {
2021-11-15 22:27:28 +01:00
decoded . unchecked_append ( sign_extend (
2022-01-14 01:14:24 +01:00
LOADER_TRY ( bit_input . read_bits < u32 > ( subframe . bits_per_sample - subframe . wasted_bits_per_sample ) ) ,
2021-11-15 22:27:28 +01:00
subframe . bits_per_sample - subframe . wasted_bits_per_sample ) ) ;
2021-06-25 13:54:14 +02:00
}
// precision of the coefficients
2022-01-14 01:14:24 +01:00
u8 lpc_precision = LOADER_TRY ( bit_input . read_bits < u8 > ( 4 ) ) ;
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
if ( lpc_precision = = 0b1111 )
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Invalid linear predictor coefficient precision " } ;
2021-06-25 13:54:14 +02:00
lpc_precision + = 1 ;
// shift needed on the data (signed!)
2022-01-14 01:14:24 +01:00
i8 lpc_shift = sign_extend ( LOADER_TRY ( bit_input . read_bits < u8 > ( 5 ) ) , 5 ) ;
2021-06-25 13:54:14 +02:00
Vector < i32 > coefficients ;
coefficients . ensure_capacity ( subframe . order ) ;
// read coefficients
for ( auto i = 0 ; i < subframe . order ; + + i ) {
2022-01-14 01:14:24 +01:00
u32 raw_coefficient = LOADER_TRY ( bit_input . read_bits < u32 > ( lpc_precision ) ) ;
2021-11-15 22:27:28 +01:00
i32 coefficient = static_cast < i32 > ( sign_extend ( raw_coefficient , lpc_precision ) ) ;
2021-06-25 13:54:14 +02:00
coefficients . unchecked_append ( coefficient ) ;
}
2021-07-13 11:03:10 +02:00
dbgln_if ( AFLACLOADER_DEBUG , " {}-bit {} shift coefficients: {} " , lpc_precision , lpc_shift , coefficients ) ;
2021-06-25 13:54:14 +02:00
2021-12-17 00:00:03 +01:00
TRY ( decode_residual ( decoded , subframe , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
// approximate the waveform with the predictor
for ( size_t i = subframe . order ; i < m_current_frame - > sample_count ; + + i ) {
2021-11-15 23:00:09 +01:00
// (see below)
2021-08-16 22:04:34 +02:00
i64 sample = 0 ;
2021-06-25 13:54:14 +02:00
for ( size_t t = 0 ; t < subframe . order ; + + t ) {
2021-11-15 23:00:09 +01:00
// It's really important that we compute in 64-bit land here.
// Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression.
2022-01-06 07:07:15 -07:00
// These will easily overflow 32 bits and cause strange white noise that abruptly stops intermittently (at the end of a frame).
2021-11-15 23:00:09 +01:00
// The simple fix of course is to do intermediate computations in 64 bits.
2022-06-16 21:23:31 +02:00
// These considerations are not in the original FLAC spec, but have been added to the IETF standard: https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#appendix-A.3
2021-08-16 22:04:34 +02:00
sample + = static_cast < i64 > ( coefficients [ t ] ) * static_cast < i64 > ( decoded [ i - t - 1 ] ) ;
2021-06-25 13:54:14 +02:00
}
decoded [ i ] + = sample > > lpc_shift ;
}
return decoded ;
}
2022-06-16 21:23:31 +02:00
// 11.27. SUBFRAME_FIXED
2021-06-25 13:54:14 +02:00
// Decode a subframe encoded with one of the fixed linear predictor codings
2022-01-14 01:14:24 +01:00
ErrorOr < Vector < i32 > , LoaderError > FlacLoaderPlugin : : decode_fixed_lpc ( FlacSubframeHeader & subframe , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
Vector < i32 > decoded ;
decoded . ensure_capacity ( m_current_frame - > sample_count ) ;
2021-11-15 22:27:28 +01:00
VERIFY ( subframe . bits_per_sample - subframe . wasted_bits_per_sample ! = 0 ) ;
2021-06-25 13:54:14 +02:00
// warm-up samples
for ( auto i = 0 ; i < subframe . order ; + + i ) {
2021-11-15 22:27:28 +01:00
decoded . unchecked_append ( sign_extend (
2022-01-14 01:14:24 +01:00
LOADER_TRY ( bit_input . read_bits < u32 > ( subframe . bits_per_sample - subframe . wasted_bits_per_sample ) ) ,
2021-11-15 22:27:28 +01:00
subframe . bits_per_sample - subframe . wasted_bits_per_sample ) ) ;
2021-06-25 13:54:14 +02:00
}
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
TRY ( decode_residual ( decoded , subframe , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
dbgln_if ( AFLACLOADER_DEBUG , " decoded length {}, {} order predictor " , decoded . size ( ) , subframe . order ) ;
2022-06-16 21:23:31 +02:00
// Skip these comments if you don't care about the neat math behind fixed LPC :^)
// These coefficients for the recursive prediction formula are the only ones that can be resolved to polynomial predictor functions.
// The order equals the degree of the polynomial - 1, so the second-order predictor has an underlying polynomial of degree 1, a straight line.
// More specifically, the closest approximation to a polynomial is used, and the degree depends on how many previous values are available.
// This makes use of a very neat property of polynomials, which is that they are entirely characterized by their finitely many derivatives.
// (Mathematically speaking, the infinite Taylor series of any polynomial equals the polynomial itself.)
// Now remember that derivation is just the slope of the function, which is the same as the difference of two close-by values.
// Therefore, with two samples we can calculate the first derivative at a sample via the difference, which gives us a polynomial of degree 1.
// With three samples, we can do the same but also calculate the second derivative via the difference in the first derivatives.
// This gives us a polynomial of degree 2, as it has two "proper" (non-constant) derivatives.
// This can be continued for higher-order derivatives when we have more coefficients, giving us higher-order polynomials.
// In essence, it's akin to a Lagrangian polynomial interpolation for every sample (but already pre-solved).
// The coefficients for orders 0-3 originate from the SHORTEN codec:
// http://mi.eng.cam.ac.uk/reports/svr-ftp/auto-pdf/robinson_tr156.pdf page 4
// The coefficients for order 4 are undocumented in the original FLAC specification(s), but can now be found in
// https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#section-10.2.5
2021-06-25 13:54:14 +02:00
switch ( subframe . order ) {
case 0 :
// s_0(t) = 0
2021-07-11 14:47:09 +02:00
for ( u32 i = subframe . order ; i < m_current_frame - > sample_count ; + + i )
2021-06-25 13:54:14 +02:00
decoded [ i ] + = 0 ;
break ;
case 1 :
// s_1(t) = s(t-1)
2021-07-11 14:47:09 +02:00
for ( u32 i = subframe . order ; i < m_current_frame - > sample_count ; + + i )
2021-06-25 13:54:14 +02:00
decoded [ i ] + = decoded [ i - 1 ] ;
break ;
case 2 :
// s_2(t) = 2s(t-1) - s(t-2)
2021-07-11 14:47:09 +02:00
for ( u32 i = subframe . order ; i < m_current_frame - > sample_count ; + + i )
2021-06-25 13:54:14 +02:00
decoded [ i ] + = 2 * decoded [ i - 1 ] - decoded [ i - 2 ] ;
break ;
case 3 :
// s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
2021-07-11 14:47:09 +02:00
for ( u32 i = subframe . order ; i < m_current_frame - > sample_count ; + + i )
2021-06-25 13:54:14 +02:00
decoded [ i ] + = 3 * decoded [ i - 1 ] - 3 * decoded [ i - 2 ] + decoded [ i - 3 ] ;
break ;
case 4 :
// s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
2021-07-11 14:47:09 +02:00
for ( u32 i = subframe . order ; i < m_current_frame - > sample_count ; + + i )
2021-06-25 13:54:14 +02:00
decoded [ i ] + = 4 * decoded [ i - 1 ] - 6 * decoded [ i - 2 ] + 4 * decoded [ i - 3 ] - decoded [ i - 4 ] ;
break ;
default :
2022-12-04 18:02:33 +00:00
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , DeprecatedString : : formatted ( " Unrecognized predictor order {} " , subframe . order ) } ;
2021-06-25 13:54:14 +02:00
}
return decoded ;
}
2022-06-16 21:23:31 +02:00
// 11.30. RESIDUAL
2021-06-25 13:54:14 +02:00
// Decode the residual, the "error" between the function approximation and the actual audio data
2022-01-14 01:14:24 +01:00
MaybeLoaderError FlacLoaderPlugin : : decode_residual ( Vector < i32 > & decoded , FlacSubframeHeader & subframe , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
2022-06-16 21:23:31 +02:00
// 11.30.1. RESIDUAL_CODING_METHOD
2022-03-17 14:38:24 -06:00
auto residual_mode = static_cast < FlacResidualMode > ( LOADER_TRY ( bit_input . read_bits < u8 > ( 2 ) ) ) ;
2022-01-14 01:14:24 +01:00
u8 partition_order = LOADER_TRY ( bit_input . read_bits < u8 > ( 4 ) ) ;
2021-08-16 21:59:53 +02:00
size_t partitions = 1 < < partition_order ;
2021-06-25 13:54:14 +02:00
if ( residual_mode = = FlacResidualMode : : Rice4Bit ) {
2022-06-16 21:23:31 +02:00
// 11.30.2. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB
2021-06-25 13:54:14 +02:00
// decode a single Rice partition with four bits for the order k
2021-08-16 21:59:53 +02:00
for ( size_t i = 0 ; i < partitions ; + + i ) {
2022-01-14 01:14:24 +01:00
auto rice_partition = TRY ( decode_rice_partition ( 4 , partitions , i , subframe , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
decoded . extend ( move ( rice_partition ) ) ;
}
} else if ( residual_mode = = FlacResidualMode : : Rice5Bit ) {
2022-06-16 21:23:31 +02:00
// 11.30.3. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2
2021-06-25 13:54:14 +02:00
// five bits equivalent
2021-08-16 21:59:53 +02:00
for ( size_t i = 0 ; i < partitions ; + + i ) {
2022-01-14 01:14:24 +01:00
auto rice_partition = TRY ( decode_rice_partition ( 5 , partitions , i , subframe , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
decoded . extend ( move ( rice_partition ) ) ;
}
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
} else
return LoaderError { LoaderError : : Category : : Format , static_cast < size_t > ( m_current_sample_or_frame ) , " Reserved residual coding method " } ;
2021-06-25 13:54:14 +02:00
2021-12-17 00:00:03 +01:00
return { } ;
2021-06-25 13:54:14 +02:00
}
2022-06-16 21:23:31 +02:00
// 11.30.2.1. EXP_GOLOMB_PARTITION and 11.30.3.1. EXP_GOLOMB2_PARTITION
2021-06-25 13:54:14 +02:00
// Decode a single Rice partition as part of the residual, every partition can have its own Rice parameter k
2022-01-14 01:14:24 +01:00
ALWAYS_INLINE ErrorOr < Vector < i32 > , LoaderError > FlacLoaderPlugin : : decode_rice_partition ( u8 partition_type , u32 partitions , u32 partition_index , FlacSubframeHeader & subframe , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
2022-06-16 21:23:31 +02:00
// 11.30.2.2. EXP GOLOMB PARTITION ENCODING PARAMETER and 11.30.3.2. EXP-GOLOMB2 PARTITION ENCODING PARAMETER
2022-01-14 01:14:24 +01:00
u8 k = LOADER_TRY ( bit_input . read_bits < u8 > ( partition_type ) ) ;
2021-06-25 13:54:14 +02:00
u32 residual_sample_count ;
if ( partitions = = 0 )
residual_sample_count = m_current_frame - > sample_count - subframe . order ;
else
residual_sample_count = m_current_frame - > sample_count / partitions ;
if ( partition_index = = 0 )
residual_sample_count - = subframe . order ;
Vector < i32 > rice_partition ;
rice_partition . resize ( residual_sample_count ) ;
// escape code for unencoded binary partition
if ( k = = ( 1 < < partition_type ) - 1 ) {
2022-01-14 01:14:24 +01:00
u8 unencoded_bps = LOADER_TRY ( bit_input . read_bits < u8 > ( 5 ) ) ;
2021-08-16 21:59:53 +02:00
for ( size_t r = 0 ; r < residual_sample_count ; + + r ) {
2022-01-14 01:14:24 +01:00
rice_partition [ r ] = LOADER_TRY ( bit_input . read_bits < u8 > ( unencoded_bps ) ) ;
2021-06-25 13:54:14 +02:00
}
} else {
2021-08-16 21:59:53 +02:00
for ( size_t r = 0 ; r < residual_sample_count ; + + r ) {
2022-01-14 01:14:24 +01:00
rice_partition [ r ] = LOADER_TRY ( decode_unsigned_exp_golomb ( k , bit_input ) ) ;
2021-06-25 13:54:14 +02:00
}
}
return rice_partition ;
}
// Decode a single number encoded with Rice/Exponential-Golomb encoding (the unsigned variant)
2022-01-14 01:14:24 +01:00
ALWAYS_INLINE ErrorOr < i32 > decode_unsigned_exp_golomb ( u8 k , BigEndianInputBitStream & bit_input )
2021-06-25 13:54:14 +02:00
{
u8 q = 0 ;
2022-01-14 01:14:24 +01:00
while ( TRY ( bit_input . read_bit ( ) ) = = 0 )
2021-06-25 13:54:14 +02:00
+ + q ;
// least significant bits (remainder)
2022-01-14 01:14:24 +01:00
u32 rem = TRY ( bit_input . read_bits < u32 > ( k ) ) ;
2021-11-15 22:27:28 +01:00
u32 value = q < < k | rem ;
2021-06-25 13:54:14 +02:00
return rice_to_signed ( value ) ;
}
2022-01-14 01:14:24 +01:00
ErrorOr < u64 > read_utf8_char ( BigEndianInputBitStream & input )
2021-06-25 13:54:14 +02:00
{
u64 character ;
2021-09-06 03:29:52 +04:30
u8 buffer = 0 ;
Bytes buffer_bytes { & buffer , 1 } ;
2022-01-14 01:14:24 +01:00
TRY ( input . read ( buffer_bytes ) ) ;
2021-09-06 03:29:52 +04:30
u8 start_byte = buffer_bytes [ 0 ] ;
2021-06-25 13:54:14 +02:00
// Signal byte is zero: ASCII character
if ( ( start_byte & 0b10000000 ) = = 0 ) {
return start_byte ;
} else if ( ( start_byte & 0b11000000 ) = = 0b10000000 ) {
2022-07-11 17:57:32 +00:00
return Error : : from_string_literal ( " Illegal continuation byte " ) ;
2021-06-25 13:54:14 +02:00
}
// This algorithm is too good and supports the theoretical max 0xFF start byte
u8 length = 1 ;
while ( ( ( start_byte < < length ) & 0b10000000 ) = = 0b10000000 )
+ + length ;
u8 bits_from_start_byte = 8 - ( length + 1 ) ;
2021-07-17 18:29:28 +02:00
u8 start_byte_bitmask = AK : : exp2 ( bits_from_start_byte ) - 1 ;
2021-06-25 13:54:14 +02:00
character = start_byte_bitmask & start_byte ;
2021-07-21 15:58:57 +02:00
for ( u8 i = length - 1 ; i > 0 ; - - i ) {
2022-01-14 01:14:24 +01:00
TRY ( input . read ( buffer_bytes ) ) ;
2021-09-06 03:29:52 +04:30
u8 current_byte = buffer_bytes [ 0 ] ;
2021-06-25 13:54:14 +02:00
character = ( character < < 6 ) | ( current_byte & 0b00111111 ) ;
}
return character ;
}
i64 sign_extend ( u32 n , u8 size )
{
// negative
if ( ( n & ( 1 < < ( size - 1 ) ) ) > 0 ) {
return static_cast < i64 > ( n | ( 0xffffffff < < size ) ) ;
}
// positive
return n ;
}
i32 rice_to_signed ( u32 x )
{
// positive numbers are even, negative numbers are odd
// bitmask for conditionally inverting the entire number, thereby "negating" it
2021-11-15 22:27:28 +01:00
i32 sign = - static_cast < i32 > ( x & 1 ) ;
2021-06-25 13:54:14 +02:00
// copies the sign's sign onto the actual magnitude of x
2021-11-15 22:27:28 +01:00
return static_cast < i32 > ( sign ^ ( x > > 1 ) ) ;
2021-06-25 13:54:14 +02:00
}
}