xsynth_core/
audio_pipe.rs

1use crate::AudioStreamParams;
2
3/// An object to read audio samples from.
4pub trait AudioPipe {
5    /// The audio stream parameters of the audio pipe.
6    fn stream_params(&self) -> &'_ AudioStreamParams;
7
8    /// Reads samples from the pipe.
9    ///
10    /// When using in a MIDI synthesizer, the amount of samples determines the
11    /// time of the current active MIDI events. For example if we send a note
12    /// on event and read 44100 samples (with a 44.1kHz sample rate), then the
13    /// note will be audible for 1 second. If after reading those samples we
14    /// send a note off event for the same key, then on the next read the key
15    /// will be released. If we don't, then the note will keep playing.
16    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    /// Reads samples from the pipe without checking the channel count of the output.
22    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}