Struct anira::HostConfig#
-
struct HostConfig#
Configuration structure for host system parameters.
The HostConfig struct encapsulates the host system’s configuration parameters that are needed for proper integration with neural network inference processing. It defines the buffer characteristics, sample rate, and processing constraints that the inference system must adapt to.
The struct provides utility methods for calculating relative buffer sizes and sample rates when working with multiple tensors that may have different processing requirements or dimensions.
- Reference stream
The buffer size and sample rate are stated in samples of one streamable tensor, the reference stream. It is either selected explicitly (m_tensor_index together with m_tensor_is_input) or, with the default m_tensor_index == k_first_streamable, resolved automatically as the first streamable input tensor and, if no input is streamable (a generator model whose inputs are all control parameters), the first streamable output tensor. An explicit reference that is out of range or not streamable is an error: resolve_reference() throws std::invalid_argument, and so does prepare(). There is no silent fallback.
Note
This struct is designed to be lightweight and suitable for frequent copying and comparison operations in real-time contexts.
Public Functions
-
HostConfig() = default#
Default constructor that creates an empty host configuration.
Initializes all parameters to default values (zero buffer size, zero sample rate). The configuration must be properly initialized before use in audio processing.
-
inline HostConfig(float host_buffer_size, float host_sample_rate, bool allow_smaller_buffers = false, size_t tensor_index = k_first_streamable, bool tensor_is_input = true)#
Constructor that initializes host configuration with specified parameters.
Creates a host configuration with the specified audio system parameters. This constructor allows full customization of the audio host environment.
- Parameters:
host_buffer_size – Buffer size of the host, in samples of the reference stream
host_sample_rate – Sample rate of the host, in samples of the reference stream per second
allow_smaller_buffers – Whether to allow processing of buffers smaller than the host buffer size (default: false)
tensor_index – Index of the reference tensor (default: k_first_streamable, i.e. the first streamable input, else the first streamable output)
tensor_is_input – Whether tensor_index refers to an input (true, default) or an output tensor (false); ignored when tensor_index is k_first_streamable
-
inline bool operator==(const HostConfig &other) const#
Equality comparison operator.
Compares two HostConfig instances for equality using appropriate tolerance for floating-point comparisons. All member variables must match within acceptable precision for the configs to be considered equal.
Note
Floating-point comparisons use a tolerance of 1e-6 to handle precision issues in floating-point arithmetic.
- Parameters:
other – The HostConfig instance to compare with
- Returns:
True if both configurations are equivalent, false otherwise
-
inline bool operator!=(const HostConfig &other) const#
Inequality comparison operator.
Compares two HostConfig instances for inequality by negating the equality operator.
- Parameters:
other – The HostConfig instance to compare with
- Returns:
True if the configurations are different, false if they are equivalent
-
inline ReferenceStream resolve_reference(const InferenceConfig &inference_config) const#
Resolves the reference stream against an inference configuration.
With m_tensor_index == k_first_streamable the reference is the first input tensor with a non-zero preprocess_input_size, or, if there is none, the first output tensor with a non-zero postprocess_output_size. Otherwise m_tensor_index names a tensor in the input list (m_tensor_is_input == true) or the output list (m_tensor_is_input == false), which must exist and be streamable.
- Parameters:
inference_config – The inference configuration providing the tensor sizes
- Throws:
std::invalid_argument – if an explicit reference is out of range or not streamable, or if no tensor on either side is streamable
- Returns:
The resolved reference stream
-
inline float get_reference_size(const InferenceConfig &inference_config) const#
Size of the reference stream in samples per inference.
The preprocess_input_size (input reference) or postprocess_output_size (output reference) of the tensor returned by resolve_reference(). All relative buffer size and sample rate calculations scale against this value.
- Parameters:
inference_config – The inference configuration providing the tensor sizes
- Throws:
std::invalid_argument – if the reference stream cannot be resolved
- Returns:
The reference tensor’s streamable size
-
inline float get_relative_buffer_size(const InferenceConfig &inference_config, size_t tensor_index, bool input = true) const#
Calculates the relative buffer size for a specific tensor.
Computes the appropriate buffer size for a given tensor based on the ratio between this host configuration’s buffer size and the reference stream’s size. This is useful when working with multiple tensors that may have different dimensional requirements while maintaining proportional scaling.
The calculation uses the reference stream (see resolve_reference()) to establish a scaling ratio, then applies this ratio to the target tensor’s dimensions.
Note
The returned value maintains the proportional relationship between different tensor sizes based on the host buffer configuration.
- Parameters:
inference_config – The inference configuration containing tensor dimension information
tensor_index – The index of the tensor to calculate the buffer size for
input – Whether to calculate for input tensors (true) or output tensors (false)
- Throws:
std::invalid_argument – if the reference stream cannot be resolved
- Returns:
The calculated relative buffer size for the specified tensor
-
inline float get_relative_sample_rate(const InferenceConfig &inference_config, size_t tensor_index, bool input = true) const#
Calculates the relative sample rate for a specific tensor.
Computes the appropriate sample rate for a given tensor based on the ratio between this host configuration’s sample rate and the reference stream’s size. This is useful when different tensors represent audio data at different effective sample rates due to processing or downsampling.
The calculation uses the reference stream (see resolve_reference()) to establish a scaling ratio, then applies this ratio to the target tensor’s dimensions to determine the effective sample rate.
Note
This method is useful for handling models that process audio at different effective sample rates or with different temporal resolutions.
- Parameters:
inference_config – The inference configuration containing tensor dimension information
tensor_index – The index of the tensor to calculate the sample rate for
input – Whether to calculate for input tensors (true) or output tensors (false)
- Throws:
std::invalid_argument – if the reference stream cannot be resolved
- Returns:
The calculated relative sample rate for the specified tensor
Public Members
-
float m_buffer_size = 0#
Maximum buffer size of the host, in samples of the reference stream
-
float m_sample_rate = 0.0#
Sample rate of the host in Hz, in samples of the reference stream per second
-
bool m_allow_smaller_buffers = false#
Whether to allow processing of buffers smaller than the maximum size
-
size_t m_tensor_index = k_first_streamable#
Index of the reference tensor, or k_first_streamable to resolve it automatically
-
bool m_tensor_is_input = true#
Whether m_tensor_index refers to an input (true) or an output (false) tensor; ignored while m_tensor_index is k_first_streamable
Public Static Attributes
-
static constexpr size_t k_first_streamable = static_cast<size_t>(-1)#
Sentinel for m_tensor_index: resolve the reference stream automatically.
The reference is the first streamable input tensor, or the first streamable output tensor if no input is streamable. See resolve_reference().