xsynth_core/
audio_pipe.rs1use crate::AudioStreamParams;
2
3pub trait AudioPipe {
5 fn stream_params(&self) -> &'_ AudioStreamParams;
7
8 fn read_samples(&mut self, to: &mut [f32]) {
17 assert!(to
18 .len()
19 .is_multiple_of(self.stream_params().channels.count() as usize));
20 self.read_samples_unchecked(to);
21 }
22
23 fn read_samples_unchecked(&mut self, to: &mut [f32]);
25}
26
27pub struct FunctionAudioPipe<F: 'static + FnMut(&mut [f32]) + Send> {
28 func: F,
29 stream_params: AudioStreamParams,
30}
31
32impl<F: 'static + FnMut(&mut [f32]) + Send> AudioPipe for FunctionAudioPipe<F> {
33 fn stream_params(&self) -> &'_ AudioStreamParams {
34 &self.stream_params
35 }
36
37 fn read_samples_unchecked(&mut self, to: &mut [f32]) {
38 (self.func)(to);
39 }
40}
41
42impl<F: 'static + FnMut(&mut [f32]) + Send> FunctionAudioPipe<F> {
43 pub fn new(stream_params: AudioStreamParams, func: F) -> Self {
44 FunctionAudioPipe {
45 func,
46 stream_params,
47 }
48 }
49}