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.len() as u32).is_multiple_of(self.stream_params().channels.count() as u32));
18 self.read_samples_unchecked(to);
19 }
20
21 fn read_samples_unchecked(&mut self, to: &mut [f32]);
23}
24
25pub struct FunctionAudioPipe<F: 'static + FnMut(&mut [f32]) + Send> {
26 func: F,
27 stream_params: AudioStreamParams,
28}
29
30impl<F: 'static + FnMut(&mut [f32]) + Send> AudioPipe for FunctionAudioPipe<F> {
31 fn stream_params(&self) -> &'_ AudioStreamParams {
32 &self.stream_params
33 }
34
35 fn read_samples_unchecked(&mut self, to: &mut [f32]) {
36 (self.func)(to);
37 }
38}
39
40impl<F: 'static + FnMut(&mut [f32]) + Send> FunctionAudioPipe<F> {
41 pub fn new(stream_params: AudioStreamParams, func: F) -> Self {
42 FunctionAudioPipe {
43 func,
44 stream_params,
45 }
46 }
47}