lib_jsl_dsp/discrete/stream_operator.rs
1//! The streaming interface shared by filters and other block processors.
2//!
3//! See the book entry [Convolution and correlation](https://jwkunz.github.io/lib_jsl/book/filters/convolution.html#implementation).
4
5use lib_jsl_core::ErrorsJSL;
6
7/// A stream operator trait indicates a standard interface for processing streams of data, such as audio or video streams. It defines a set of methods that can be implemented by different types of stream operators, allowing them to be used interchangeably in a streaming pipeline.
8/// The trait typically includes methods for initializing the operator, processing input data, and producing output data.
9/// By defining a common interface, the stream operator trait enables flexibility and modularity in the design of streaming applications, allowing developers to easily swap out different operators or add new ones without affecting the overall structure of the pipeline.
10/// This promotes code reuse and makes it easier to maintain and extend streaming applications over time.
11pub trait StreamOperatorManagement{
12 /// Resets the internal state of the operator, preparing it for a new stream of data. This method is typically called at the beginning of a new stream or when the operator needs to be reinitialized.
13 fn reset(&mut self) -> Result<(), ErrorsJSL>;
14 /// Finalizes the processing of the stream, performing any necessary cleanup or final computations. This method is typically called at the end of a stream or when the operator needs to be shut down.
15 fn finalize(&mut self) -> Result<(), ErrorsJSL>;
16}
17
18pub trait StreamOperator<Input, Output> : StreamOperatorManagement {
19 /// Processes a chunk of input data and produces a corresponding chunk of output data. The input and output types can be defined based on the specific requirements of the application, such as audio samples, video frames, or other types of data.
20 /// This method is called repeatedly as new data becomes available in the stream.
21 fn process(&mut self, data_in: &[Input]) -> Result<Option<Vec<Output>>, ErrorsJSL>;
22 /// Flushes any remaining data in the operator's internal buffers and produces any final output data. This method is typically called at the end of a stream or when the operator needs to be shut down, allowing it to perform any necessary cleanup or final computations before the stream is terminated.
23 fn flush(&mut self) -> Result<Option<Vec<Output>>, ErrorsJSL>;
24}