1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! Underlying data types for callbacks

mod buffer_abstraction;

pub use buffer_abstraction::{input, main, output, DeviceBuffer};

use std::ptr::NonNull;

use crate::types::{ChannelIndex, Device, VoicemeeterApplication};

/// Audio information
pub type AudioInfo = crate::bindings::VBVMR_T_AUDIOINFO;
/// Audio buffers
pub type AudioBuffer = crate::bindings::VBVMR_T_AUDIOBUFFER;

impl AudioBuffer {
    //#[tracing::instrument(level = "debug", skip(self))]
    pub(crate) fn read_write_buffer(&self) -> (&[*mut f32], &[*mut f32]) {
        let first_ptr_r = self.audiobuffer_r[0];
        let first_ptr_w = self.audiobuffer_w[0];
        for (idx, ptr) in self
            .audiobuffer_r
            .iter()
            .enumerate()
            .take(self.audiobuffer_nbi as usize)
        {
            debug_assert!(!ptr.is_null(), "ptr: {:?} was null at idx: {}", ptr, idx);
        }
        for (idx, ptr) in self
            .audiobuffer_w
            .iter()
            .enumerate()
            .take(self.audiobuffer_nbo as usize)
        {
            debug_assert!(!ptr.is_null(), "ptr: {:?} was null at idx: {}", ptr, idx);
        }
        //tracing::trace!("read_write_buffer: {:?}", self);

        let k = (
            &self.audiobuffer_r[..self.audiobuffer_nbi as usize],
            &self.audiobuffer_w[..self.audiobuffer_nbo as usize],
        );
        // sanity check
        debug_assert_eq!(first_ptr_r, k.0[0]);
        debug_assert_eq!(first_ptr_w, k.1[0]);
        k
    }
}

#[repr(transparent)]
/// Raw callback data
pub struct RawCallbackData(NonNull<std::ffi::c_void>);

impl std::fmt::Debug for RawCallbackData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:p}", self.0)
    }
}
// All of these tracing::instrument need to be skipped or we crash.
impl RawCallbackData {
    //#[tracing::instrument(level = "trace", skip_all,name = "RawCallbackData::from_ptr")]'
    /// Create a new `RawCallbackData` from a raw pointer.
    pub fn from_ptr(ptr: *mut std::ffi::c_void) -> Self {
        RawCallbackData(NonNull::new(ptr).unwrap())
    }
    //#[tracing::instrument(level = "trace", skip_all,name = "RawCallbackData::as_audio_info")]
    /// Get the audio information from the raw callback data.
    ///
    /// # Safety
    ///
    /// 1. This should not be called without ensuring that the pointer is in "scope" and that it is an [`AudioInfo`]
    /// 2. All other conditions of [NonNull::as_mut] has to be met as well
    pub unsafe fn as_audio_info<'a>(&self) -> &'a mut AudioInfo {
        unsafe { self.0.cast().as_mut() }
    }
    //#[tracing::instrument(level = "trace", skip_all,name = "RawCallbackData::as_audio_buffer")]
    /// Get the audio information from the raw callback data.
    ///
    /// # Safety
    ///
    /// 1. This should not be called without ensuring that the pointer is in "scope" and that it is an [`AudioBuffer`]
    /// 2. All other conditions of [NonNull::as_mut] has to be met as well
    pub unsafe fn as_audio_buffer<'a>(&self) -> &'a mut AudioBuffer {
        unsafe { self.0.cast().as_mut() }
    }
}

trait BufferDataExt<'a> {
    /// Get the data inside the buffer as slices of pointers
    fn data<'b>(&'b self) -> (&'a [*mut f32], &'a [*mut f32]);
    /// Given a device, return a channel index
    fn channel_index_write(&self, device: &Device) -> Option<ChannelIndex>;
    fn channel_index_read(&self, device: &Device) -> Option<ChannelIndex>;
    fn samples_per_frame(&self) -> usize;
    /// Get the device write buffer for a device at idx.start..idx.start+N.
    ///
    /// This is unsafe because it can create mutable references arbitrarily.
    #[inline]
    unsafe fn device_write<'b, const N: usize>(
        &'a self,
        device: &Device,
    ) -> DeviceBuffer<[&'b mut [f32]; N]> {
        let idx = if let Some(idx) = self.channel_index_write(device) {
            idx
        } else {
            return DeviceBuffer::None;
        };
        assert_eq!(N, idx.size);
        let data = self.data().1;
        // Each channel inside idx.start..(idx..start+idx.size) will be a *mut f32 that points to an array of `nbs` f32s.
        let mut array = [(); N].map(|_| Default::default());
        for i in 0..N {
            let ptr = data[idx.start + i];
            array[i] = unsafe { std::slice::from_raw_parts_mut(ptr, self.samples_per_frame()) };
        }
        DeviceBuffer::Buffer(array)
    }
    /// Get the device read buffer for a device at idx.start..idx.start+N.
    ///
    /// This is unsafe because it can create mutable references arbitrarily.
    #[inline]
    unsafe fn device_read<'b, const N: usize>(
        &'a self,
        device: &Device,
    ) -> DeviceBuffer<[&'b [f32]; N]> {
        let idx = if let Some(idx) = self.channel_index_read(device) {
            idx
        } else {
            return DeviceBuffer::None;
        };
        assert_eq!(N, idx.size, "on device: {device:?}");
        let data = self.data().0;
        // Each channel inside idx.start..(idx..start+idx.size) will be a *mut f32 that points to an array of `nbs` f32s.
        let mut array = [(); N].map(|_| Default::default());
        for i in 0..N {
            let ptr = data[idx.start + i];
            array[i] = unsafe { std::slice::from_raw_parts(ptr, self.samples_per_frame()) };
        }
        DeviceBuffer::Buffer(array)
    }
}

/// Buffer for main mode.
///
/// Retrieved via [`CallbackCommand::BufferMain`](crate::interface::callback::CallbackCommand::BufferMain)
pub struct BufferMainData<'a> {
    /// Read
    pub read: main::ReadDevices<'a, 'a>,
    /// Write
    pub write: main::WriteDevices<'a, 'a>,
}

pub(crate) struct Main<'a> {
    program: VoicemeeterApplication,
    samples_per_frame: usize,
    data: (&'a [*mut f32], &'a [*mut f32]),
}

impl<'a> BufferDataExt<'a> for Main<'a> {
    #[inline]
    fn data<'b>(&'b self) -> (&'a [*mut f32], &'a [*mut f32]) {
        self.data
    }

    #[inline]
    fn channel_index_read(&self, device: &Device) -> Option<ChannelIndex> {
        device.main(&self.program).0
    }

    #[inline]
    fn channel_index_write(&self, device: &Device) -> Option<ChannelIndex> {
        device.main(&self.program).1
    }

    #[inline]
    fn samples_per_frame(&self) -> usize {
        self.samples_per_frame
    }
}

//#[tracing::instrument(skip_all, name = "BufferMainData::new")]
impl<'a> BufferMainData<'a> {
    pub(crate) fn new<'b: 'a>(
        program: VoicemeeterApplication,
        data: &'b AudioBuffer,
        samples_per_frame: usize,
    ) -> Self {
        let mut data = Main {
            data: data.read_write_buffer(),
            samples_per_frame,
            program,
        };
        unsafe {
            Self {
                read: main::ReadDevices::new(&mut data),
                write: main::WriteDevices::new(&mut data),
            }
        }
    }

    /// Convenience function to get the read and write buffers
    ///
    /// ```rust,no_run
    /// # use voicemeeter::interface::callback::BufferMain; let data: BufferMain = unimplemented!();
    /// let (read, mut write) = data.buffer.get_buffers();
    /// ```
    pub fn get_buffers(self) -> (main::ReadDevices<'a, 'a>, main::WriteDevices<'a, 'a>) {
        (self.read, self.write)
    }
}

/// Buffer for output mode.
///
/// Retrieved via [`CallbackCommand::BufferOut`](crate::interface::callback::CallbackCommand::BufferOut)
pub struct BufferOutData<'a> {
    /// Read
    pub read: output::ReadDevices<'a, 'a>,
    /// Write
    pub write: output::WriteDevices<'a, 'a>,
}
pub(crate) struct Output<'a> {
    program: VoicemeeterApplication,
    samples_per_frame: usize,
    data: (&'a [*mut f32], &'a [*mut f32]),
}

impl<'a> BufferDataExt<'a> for Output<'a> {
    #[inline]
    fn data<'b>(&'b self) -> (&'a [*mut f32], &'a [*mut f32]) {
        self.data
    }
    #[inline]
    fn channel_index_read(&self, device: &Device) -> Option<ChannelIndex> {
        device.output(&self.program)
    }

    #[inline]
    fn channel_index_write(&self, device: &Device) -> Option<ChannelIndex> {
        device.output(&self.program)
    }

    #[inline]
    fn samples_per_frame(&self) -> usize {
        self.samples_per_frame
    }
}

impl<'a> BufferOutData<'a> {
    //#[tracing::instrument(skip_all, name = "BufferOutData::new")]
    pub(crate) fn new(
        program: VoicemeeterApplication,
        data: &'a mut AudioBuffer,
        samples_per_frame: usize,
    ) -> Self {
        let mut data = Output {
            data: data.read_write_buffer(),
            samples_per_frame,
            program,
        };
        unsafe {
            Self {
                read: output::ReadDevices::new(&mut data),
                write: output::WriteDevices::new(&mut data),
            }
        }
    }

    /// Convenience function to get the read and write buffers
    ///
    /// ```rust,no_run
    /// # use voicemeeter::interface::callback::BufferOut; let data: BufferOut = unimplemented!();
    /// let (read, mut write) = data.buffer.get_buffers();
    /// ```
    pub fn get_buffers(self) -> (output::ReadDevices<'a, 'a>, output::WriteDevices<'a, 'a>) {
        (self.read, self.write)
    }
}

/// Buffer for input mode.
///
/// Retrieved via [`CallbackCommand::BufferIn`](crate::interface::callback::CallbackCommand::BufferIn)
pub struct BufferInData<'a> {
    /// Read
    pub read: input::ReadDevices<'a, 'a>,
    /// Write
    pub write: input::WriteDevices<'a, 'a>,
}
pub(crate) struct Input<'a> {
    program: VoicemeeterApplication,
    samples_per_frame: usize,
    data: (&'a [*mut f32], &'a [*mut f32]),
}

impl<'a> BufferDataExt<'a> for Input<'a> {
    #[inline]
    fn data<'b>(&'b self) -> (&'a [*mut f32], &'a [*mut f32]) {
        self.data
    }
    #[inline]
    fn channel_index_read(&self, device: &Device) -> Option<ChannelIndex> {
        device.input(&self.program)
    }

    #[inline]
    fn channel_index_write(&self, device: &Device) -> Option<ChannelIndex> {
        device.input(&self.program)
    }

    #[inline]
    fn samples_per_frame(&self) -> usize {
        self.samples_per_frame
    }
}

impl<'a> BufferInData<'a> {
    //#[tracing::instrument(skip_all, name = "BufferInData::new")]
    pub(crate) fn new(
        program: VoicemeeterApplication,
        data: &'a mut AudioBuffer,
        samples_per_frame: usize,
    ) -> Self {
        let mut data = Input {
            data: data.read_write_buffer(),
            samples_per_frame,
            program,
        };
        unsafe {
            Self {
                read: input::ReadDevices::new(&mut data),
                write: input::WriteDevices::new(&mut data),
            }
        }
    }

    /// Convenience function to get the read and write buffers
    ///
    /// ```rust,no_run
    /// # use voicemeeter::interface::callback::BufferIn; let data: BufferIn = unimplemented!();
    /// let (read, mut write) = data.buffer.get_buffers();
    /// ```
    pub fn get_buffers(self) -> (input::ReadDevices<'a, 'a>, input::WriteDevices<'a, 'a>) {
        (self.read, self.write)
    }
}