audionimbus 0.13.0

A safe wrapper around Steam Audio that provides spatial audio capabilities with realistic occlusion, reverb, and HRTF effects, accounting for physical attributes and scene geometry.
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Virtual surround sound rendering for headphones using HRTF.

use super::audio_effect_state::AudioEffectState;
use super::{EffectError, SpeakerLayout};
use crate::audio_buffer::{AudioBuffer, Sample};
use crate::audio_settings::AudioSettings;
use crate::context::Context;
use crate::error::{to_option_error, SteamAudioError};
use crate::ffi_wrapper::FFIWrapper;
use crate::{ChannelPointers, ChannelRequirement, Hrtf};

/// Spatializes multi-channel speaker-based audio (e.g., stereo, quadraphonic, 5.1, or 7.1) using HRTF-based binaural rendering.
///
/// The audio signal for each speaker is spatialized from a point in space corresponding to the speaker’s location.
/// This allows users to experience a surround sound mix over regular stereo headphones.
///
/// Virtual surround is also a fast way to get approximate binaural rendering.
/// All sources can be panned to some surround format (say, 7.1).
/// After the sources are mixed, the mix can be rendered using virtual surround.
/// This can reduce CPU usage, at the cost of spatialization accuracy.
///
/// # Examples
///
/// ```
/// use audionimbus::*;
///
/// let context = Context::default();
/// let audio_settings = AudioSettings::default();
/// let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default())?;
///
/// let mut effect = VirtualSurroundEffect::try_new(
///     &context,
///     &audio_settings,
///     &VirtualSurroundEffectSettings {
///         speaker_layout: SpeakerLayout::Surround7_1,
///         hrtf: &hrtf,
///     },
/// )?;
///
/// let params = VirtualSurroundEffectParams { hrtf: &hrtf };
///
/// const FRAME_SIZE: usize = 1024;
/// let input = vec![0.5; 8 * FRAME_SIZE]; // 8 channels
/// let mut output = vec![0.0; 2 * FRAME_SIZE]; // Stereo output
/// let input_buffer =
///     AudioBuffer::try_with_data_and_settings(&input, AudioBufferSettings::with_num_channels(8))?;
/// let output_buffer = AudioBuffer::try_with_data_and_settings(
///     &mut output,
///     AudioBufferSettings::with_num_channels(2),
/// )?;
///
/// let _ = effect.apply(&params, &input_buffer, &output_buffer);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct VirtualSurroundEffect {
    inner: audionimbus_sys::IPLVirtualSurroundEffect,

    /// Number of input channels needed for the speaker layout specified when creating the effect.
    num_input_channels: u32,
}

impl VirtualSurroundEffect {
    /// Creates a new virtual surround effect.
    ///
    /// # Errors
    ///
    /// Returns [`SteamAudioError`] if effect creation fails.
    pub fn try_new(
        context: &Context,
        audio_settings: &AudioSettings,
        virtual_surround_effect_settings: &VirtualSurroundEffectSettings,
    ) -> Result<Self, SteamAudioError> {
        let mut inner = std::ptr::null_mut();

        let status = unsafe {
            audionimbus_sys::iplVirtualSurroundEffectCreate(
                context.raw_ptr(),
                &mut audionimbus_sys::IPLAudioSettings::from(audio_settings),
                &mut audionimbus_sys::IPLVirtualSurroundEffectSettings::from(
                    virtual_surround_effect_settings,
                ),
                &raw mut inner,
            )
        };

        if let Some(error) = to_option_error(status) {
            return Err(error);
        }

        let num_input_channels = match &virtual_surround_effect_settings.speaker_layout {
            SpeakerLayout::Mono => 1,
            SpeakerLayout::Stereo => 2,
            SpeakerLayout::Quadraphonic => 4,
            SpeakerLayout::Surround5_1 => 6,
            SpeakerLayout::Surround7_1 => 8,
            SpeakerLayout::Custom { speaker_directions } => speaker_directions.len() as u32,
        };

        Ok(Self {
            inner,
            num_input_channels,
        })
    }

    /// Applies a virtual surround effect to an audio buffer.
    ///
    /// This effect CANNOT be applied in-place.
    ///
    /// The input audio buffer must have as many channels as needed for the speaker layout specified
    /// when creating the effect, and the output audio buffer must have 2 channels.
    ///
    /// # Errors
    ///
    /// Returns [`EffectError`] if:
    /// - The input buffer does not have the correct number of channels for the speaker layout
    /// - The output buffer does not have exactly two channels
    pub fn apply<I, O, PI: ChannelPointers, PO: ChannelPointers>(
        &mut self,
        virtual_surround_effect_params: &VirtualSurroundEffectParams,
        input_buffer: &AudioBuffer<I, PI>,
        output_buffer: &AudioBuffer<O, PO>,
    ) -> Result<AudioEffectState, EffectError>
    where
        I: AsRef<[Sample]>,
        O: AsRef<[Sample]> + AsMut<[Sample]>,
    {
        let num_input_channels = input_buffer.num_channels();
        if num_input_channels != self.num_input_channels {
            return Err(EffectError::InvalidInputChannels {
                expected: ChannelRequirement::Exactly(self.num_input_channels),
                actual: num_input_channels,
            });
        }

        let num_output_channels = output_buffer.num_channels();
        if num_output_channels != 2 {
            return Err(EffectError::InvalidOutputChannels {
                expected: ChannelRequirement::Exactly(2),
                actual: num_output_channels,
            });
        }

        let state = unsafe {
            audionimbus_sys::iplVirtualSurroundEffectApply(
                self.raw_ptr(),
                &raw mut *virtual_surround_effect_params.as_ffi(),
                &raw mut *input_buffer.as_ffi(),
                &raw mut *output_buffer.as_ffi(),
            )
        }
        .into();

        Ok(state)
    }

    /// Retrieves a single frame of tail samples from a virtual surround effect’s internal buffers.
    ///
    /// After the input to the virtual surround effect has stopped, this function must be called instead of [`Self::apply`] until the return value indicates that no more tail samples remain.
    ///
    /// The output audio buffer must be 2-channel.
    ///
    /// # Errors
    ///
    /// Returns [`EffectError`] if the output buffer does not have exactly 2 channels.
    pub fn tail<O>(&self, output_buffer: &AudioBuffer<O>) -> Result<AudioEffectState, EffectError>
    where
        O: AsRef<[Sample]> + AsMut<[Sample]>,
    {
        let num_output_channels = output_buffer.num_channels();
        if num_output_channels != 2 {
            return Err(EffectError::InvalidOutputChannels {
                expected: ChannelRequirement::Exactly(2),
                actual: num_output_channels,
            });
        }

        let state = unsafe {
            audionimbus_sys::iplVirtualSurroundEffectGetTail(
                self.raw_ptr(),
                &raw mut *output_buffer.as_ffi(),
            )
        }
        .into();

        Ok(state)
    }

    /// Returns the number of tail samples remaining in a virtual surround effect’s internal buffers.
    ///
    /// Tail samples are audio samples that should be played even after the input to the effect has stopped playing and no further input samples are available.
    pub fn tail_size(&self) -> usize {
        unsafe { audionimbus_sys::iplVirtualSurroundEffectGetTailSize(self.raw_ptr()) as usize }
    }

    /// Resets the internal processing state of a virtual surround effect.
    pub fn reset(&mut self) {
        unsafe { audionimbus_sys::iplVirtualSurroundEffectReset(self.raw_ptr()) };
    }

    /// Returns the raw FFI pointer to the underlying virtual surround effect.
    ///
    /// This is intended for internal use and advanced scenarios.
    pub const fn raw_ptr(&self) -> audionimbus_sys::IPLVirtualSurroundEffect {
        self.inner
    }

    /// Returns a mutable reference to the raw FFI pointer.
    ///
    /// This is intended for internal use and advanced scenarios.
    pub const fn raw_ptr_mut(&mut self) -> &mut audionimbus_sys::IPLVirtualSurroundEffect {
        &mut self.inner
    }
}

impl Drop for VirtualSurroundEffect {
    fn drop(&mut self) {
        unsafe { audionimbus_sys::iplVirtualSurroundEffectRelease(&raw mut self.inner) }
    }
}

unsafe impl Send for VirtualSurroundEffect {}
unsafe impl Sync for VirtualSurroundEffect {}

impl Clone for VirtualSurroundEffect {
    /// Retains an additional reference to the virtual surround effect.
    ///
    /// The returned [`VirtualSurroundEffect`] shares the same underlying Steam Audio object.
    fn clone(&self) -> Self {
        // SAFETY: The virtual surround effect will not be destroyed until all references are released.
        Self {
            inner: unsafe { audionimbus_sys::iplVirtualSurroundEffectRetain(self.inner) },
            num_input_channels: self.num_input_channels,
        }
    }
}

/// Settings used to create a virtual surround effect.
#[derive(Debug)]
pub struct VirtualSurroundEffectSettings<'a> {
    /// The speaker layout that will be used by input audio buffers.
    pub speaker_layout: SpeakerLayout,

    /// The HRTF to use.
    pub hrtf: &'a Hrtf,
}

impl From<&VirtualSurroundEffectSettings<'_>>
    for audionimbus_sys::IPLVirtualSurroundEffectSettings
{
    fn from(settings: &VirtualSurroundEffectSettings) -> Self {
        Self {
            speakerLayout: (&settings.speaker_layout).into(),
            hrtf: settings.hrtf.raw_ptr(),
        }
    }
}

/// Parameters for applying a virtual surround effect to an audio buffer.
#[derive(Debug)]
pub struct VirtualSurroundEffectParams<'a> {
    /// The HRTF to use.
    pub hrtf: &'a Hrtf,
}

impl VirtualSurroundEffectParams<'_> {
    pub(crate) fn as_ffi(
        &self,
    ) -> FFIWrapper<'_, audionimbus_sys::IPLVirtualSurroundEffectParams, Self> {
        let virtual_surround_effect_params = audionimbus_sys::IPLVirtualSurroundEffectParams {
            hrtf: self.hrtf.raw_ptr(),
        };

        FFIWrapper::new(virtual_surround_effect_params)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::*;

    mod apply {
        use super::*;

        #[test]
        fn test_valid_stereo() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let mut effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Stereo,
                    hrtf: &hrtf,
                },
            )
            .unwrap();

            let params = VirtualSurroundEffectParams { hrtf: &hrtf };

            let input = vec![0.5; 2 * 1024];
            let input_buffer = AudioBuffer::try_with_data_and_settings(
                &input,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            let mut output = vec![0.0; 2 * 1024];
            let output_buffer = AudioBuffer::try_with_data_and_settings(
                &mut output,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            assert!(effect.apply(&params, &input_buffer, &output_buffer).is_ok());
        }

        #[test]
        fn test_valid_surround_7_1() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let mut effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Surround7_1,
                    hrtf: &hrtf,
                },
            )
            .unwrap();

            let params = VirtualSurroundEffectParams { hrtf: &hrtf };

            let input = vec![0.5; 8 * 1024];
            let input_buffer = AudioBuffer::try_with_data_and_settings(
                &input,
                AudioBufferSettings::with_num_channels(8),
            )
            .unwrap();

            let mut output = vec![0.0; 2 * 1024];
            let output_buffer = AudioBuffer::try_with_data_and_settings(
                &mut output,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            assert!(effect.apply(&params, &input_buffer, &output_buffer).is_ok());
        }

        #[test]
        fn test_invalid_input_channels() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let mut effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Surround5_1,
                    hrtf: &hrtf,
                },
            )
            .unwrap();

            let params = VirtualSurroundEffectParams { hrtf: &hrtf };

            let input = vec![0.5; 2 * 1024];
            let input_buffer = AudioBuffer::try_with_data_and_settings(
                &input,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            let mut output = vec![0.0; 2 * 1024];
            let output_buffer = AudioBuffer::try_with_data_and_settings(
                &mut output,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            assert_eq!(
                effect.apply(&params, &input_buffer, &output_buffer),
                Err(EffectError::InvalidInputChannels {
                    expected: ChannelRequirement::Exactly(6),
                    actual: 2
                })
            );
        }

        #[test]
        fn test_invalid_output_channels() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let mut effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Stereo,
                    hrtf: &hrtf,
                },
            )
            .unwrap();

            let params = VirtualSurroundEffectParams { hrtf: &hrtf };

            let input = vec![0.5; 2 * 1024];
            let input_buffer = AudioBuffer::try_with_data_and_settings(
                &input,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            let mut output = vec![0.0; 4 * 1024];
            let output_buffer = AudioBuffer::try_with_data_and_settings(
                &mut output,
                AudioBufferSettings::with_num_channels(4),
            )
            .unwrap();

            assert_eq!(
                effect.apply(&params, &input_buffer, &output_buffer),
                Err(EffectError::InvalidOutputChannels {
                    expected: ChannelRequirement::Exactly(2),
                    actual: 4
                })
            );
        }
    }

    mod tail {
        use super::*;

        #[test]
        fn test_valid() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Stereo,
                    hrtf: &hrtf,
                },
            )
            .unwrap();

            let mut output = vec![0.0; 2 * 1024];
            let output_buffer = AudioBuffer::try_with_data_and_settings(
                &mut output,
                AudioBufferSettings::with_num_channels(2),
            )
            .unwrap();

            assert!(effect.tail(&output_buffer).is_ok());
        }

        #[test]
        fn test_invalid_output_channels() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Stereo,
                    hrtf: &hrtf,
                },
            )
            .unwrap();

            let mut output = vec![0.0; 4 * 1024];
            let output_buffer = AudioBuffer::try_with_data_and_settings(
                &mut output,
                AudioBufferSettings::with_num_channels(4),
            )
            .unwrap();

            assert_eq!(
                effect.tail(&output_buffer),
                Err(EffectError::InvalidOutputChannels {
                    expected: ChannelRequirement::Exactly(2),
                    actual: 4
                })
            );
        }
    }

    mod clone {
        use super::*;

        #[test]
        fn test_clone() {
            let context = Context::default();
            let audio_settings = AudioSettings::default();
            let hrtf = Hrtf::try_new(&context, &audio_settings, &HrtfSettings::default()).unwrap();

            let effect = VirtualSurroundEffect::try_new(
                &context,
                &audio_settings,
                &VirtualSurroundEffectSettings {
                    speaker_layout: SpeakerLayout::Stereo,
                    hrtf: &hrtf,
                },
            )
            .unwrap();
            let clone = effect.clone();
            assert_eq!(effect.raw_ptr(), clone.raw_ptr());
            drop(effect);
            assert!(!clone.raw_ptr().is_null());
        }
    }
}