The crate is designed to process data expressed by TLV (Type-Length-Value) style in ALSA control interface. The crate produces encoder and decoder for the u32 array of data of TLV style as well as structures and enumerations to express the content.
The data of TLV style is used for several purposes. As of Linux kernel 5.10, it includes
information about dB expression as well as information about channel mapping in ALSA
PCM substream. The definitions are under include/uapi/sound/tlv.h of source code of
Linux kernel.
Structures and enumerations
Linux kernel has the series of macro to build u32 array for data of TLV, instead of definitions of structure in C language. This is convenient to embed binary data to object file, however not friendly to developers and users. The crate has some structures and enumerations to express the data of TLV. The relationship between structures and macros is listed below:
DbScaleSNDRV_CTL_TLVT_DB_SCALE
DbIntervalSNDRV_CTL_TLVT_DB_LINEARSNDRV_CTL_TLVT_DB_MINMAXSNDRV_CTL_TLVT_DB_MINMAX_MUTE
Chmap/ChmapMode/ChmapEntry/ChmapPos/ChmapGenericPosSNDRV_CTL_TLVT_CHMAP_FIXEDSNDRV_CTL_TLVT_CHMAP_VARSNDRV_CTL_TLVT_CHMAP_PAIRED
DbRange/DbRangeEntry/DbRangeEntryDataSNDRV_CTL_TLVT_DB_RANGE
ContainerSNDRV_CTL_TLVT_CONTAINER
The crate has TlvItem enumeration to dispatch data of TLV for the above structures.
Usage
Add the following line to your Cargo.toml file:
[]
= "0.1"
[TlvItem] enumeration is a good start to use the crate.
use TlvItem;
use TryFrom;
// Prepare raw data of TLV as array of u32 elements.
let raw = ; // This is for SNDRV_CTL_TLVT_DB_LINEAR.
match try_from
It implements TryFrom<&[u32]> to decode raw data of TLV which is array of u32 elements. The type
of data is retrieved by a shape of Rust enumeration items. Each item has associated value. Both of
enumeration itself and the structure of associated value has trait boundary to
Vec::<u32>: From(&Self) to generate raw data of TLV.
The associated value can be instantiated directly, then raw data can be generated:
use DbScale;
let scale = DbScale;
let raw_generated: = .into;
let raw_expected = ;
assert_eq!;
Some of the associated value are container type, which aggregates the other items. In this
case, [TlvItem] is used for the aggregation of [Container].
use *;
let cntr = Container;
let raw_generated: = .into;
let raw_expected = ;
assert_eq!;
Utilities
Some programs are available under src/bin directory.
tlv-decode.rs
This program decodes raw data of TLV from stdin, or numeric literals as arguments of command line, then print parsed structure.
Without any command line argument, it prints help message and exit.
|
For data of TLV from arguments in command line:
)
For data of TLV from STDIN, in the case that the machine architecture is little endian:
| \
)
The data of TLV can be printed in C language macro expression:
| \
)
The data of TLV can be printed as either u32 numeric literal array or u8 binary aligned to host endian:
| \
| \
db-calculate.rs
This program calculates between dB value and raw value for control element, based on data of TLV from STDIN or command line argument. It uses double precision floating point number for dB calculation internally. For linear type of dB calculation, it uses exponentiation and logarithm.
Without any command line argument, it prints help message and exit.
|
| |
For calculation from dB to value based on data of TLV from STDIN, in the case that the machine architecture is little endian:
| \
For calculation to dB from value based on data of TLV from arguments of command line:
The calculation has no validated numerics.
License
The alsa-ctl-tlv-codec crate is released under MIT license.
Support
If finding issue, please file it in https://github.com/alsa-project/snd-firewire-ctl-services/.