fluidlite/synth/
write.rs

1use crate::{ffi, Status, Synth};
2
3/// The trait which implements samples data buffer interface
4pub trait IsSamples {
5    fn write_samples(self, synth: &Synth) -> Status;
6}
7
8impl IsSamples for &mut [i16] {
9    /// Write samples interleaved
10    fn write_samples(self, synth: &Synth) -> Status {
11        let len = self.len() / 2;
12        unsafe { synth.write_i16(len, self.as_mut_ptr(), 0, 2, self.as_mut_ptr(), 1, 2) }
13    }
14}
15
16impl IsSamples for (&mut [i16], &mut [i16]) {
17    /// Write samples non-interleaved
18    fn write_samples(self, synth: &Synth) -> Status {
19        let len = self.0.len().min(self.1.len());
20        unsafe { synth.write_i16(len, self.0.as_mut_ptr(), 0, 1, self.1.as_mut_ptr(), 0, 1) }
21    }
22}
23
24impl IsSamples for &mut [f32] {
25    /// Write samples interleaved
26    fn write_samples(self, synth: &Synth) -> Status {
27        let len = self.len() / 2;
28        unsafe { synth.write_f32(len, self.as_mut_ptr(), 0, 2, self.as_mut_ptr(), 1, 2) }
29    }
30}
31
32impl IsSamples for (&mut [f32], &mut [f32]) {
33    /// Write samples non-interleaved
34    fn write_samples(self, synth: &Synth) -> Status {
35        let len = self.0.len().min(self.1.len());
36        unsafe { synth.write_f32(len, self.0.as_mut_ptr(), 0, 1, self.1.as_mut_ptr(), 0, 1) }
37    }
38}
39
40/**
41Synthesizer plugin
42 */
43impl Synth {
44    /**
45    Write sound samples to the sample data buffer
46     */
47    pub fn write<S: IsSamples>(&self, samples: S) -> Status {
48        samples.write_samples(self)
49    }
50
51    /**
52    Write samples as 16-bit signed integers
53
54    # Safety
55
56    The `len` must corresponds to the lenghtes of buffers.
57     */
58    #[allow(clippy::missing_safety_doc)] // TODO: Remove after closing https://github.com/rust-lang/rust-clippy/issues/5593
59    #[allow(clippy::too_many_arguments)]
60    #[inline]
61    pub unsafe fn write_i16(
62        &self,
63        len: usize,
64        lbuf: *mut i16,
65        loff: u32,
66        lincr: u32,
67        rbuf: *mut i16,
68        roff: u32,
69        rincr: u32,
70    ) -> Status {
71        self.zero_ok(ffi::fluid_synth_write_s16(
72            self.handle,
73            len as _,
74            lbuf as _,
75            loff as _,
76            lincr as _,
77            rbuf as _,
78            roff as _,
79            rincr as _,
80        ))
81    }
82
83    /**
84    Write samples as 32-bit floating-point numbers
85
86    # Safety
87
88    The `len` must corresponds to the lenghtes of buffers.
89     */
90    #[allow(clippy::missing_safety_doc)] // TODO: Remove after closing https://github.com/rust-lang/rust-clippy/issues/5593
91    #[allow(clippy::too_many_arguments)]
92    #[inline]
93    pub unsafe fn write_f32(
94        &self,
95        len: usize,
96        lbuf: *mut f32,
97        loff: u32,
98        lincr: u32,
99        rbuf: *mut f32,
100        roff: u32,
101        rincr: u32,
102    ) -> Status {
103        self.zero_ok(ffi::fluid_synth_write_float(
104            self.handle,
105            len as _,
106            lbuf as _,
107            loff as _,
108            lincr as _,
109            rbuf as _,
110            roff as _,
111            rincr as _,
112        ))
113    }
114}