audio_master/resampler/
base.rs

1// Safe libsamplerate wrapper
2
3use std::{
4    any::TypeId,
5    ffi::{CStr, c_double, c_int, c_long},
6    marker::PhantomData,
7    ptr,
8};
9
10use crate::{
11    audio_buffer::AudioChannelLayout, consts::{MAX_RESAMPLE_RATIO, MIN_RESAMPLE_RATIO}, ffi::libsamplerate::{
12        SRC_CONVERTER_TYPE, SRC_DATA, SRC_STATE, src_delete, src_new, src_process, src_reset,
13        src_strerror,
14    }, float_type::FloatType
15};
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18pub enum ResamplerQuality {
19    /// SRC_CONVERTER_TYPE::SRC_LINEAR
20    Linear,
21
22    /// SRC_CONVERTER_TYPE::SRC_SINC_FASTEST
23    SincLow,
24
25    /// SRC_CONVERTER_TYPE::SRC_SINC_MEDIUM_QUALITY
26    SincMedium,
27
28    /// SRC_CONVERTER_TYPE::SRC_SINC_BEST_QUALITY
29    SincHigh,
30}
31
32/* Public Resampler API */
33pub trait ResamplerImpl<T: FloatType>: Sized {
34    fn new(quality: ResamplerQuality, layout: AudioChannelLayout) -> Resampler<T>;
35
36    /// Accept interleaved buffers
37    fn process(&mut self, input: &[T], output: &mut [T], src_ratio: f64) -> usize;
38
39    /// Set other ResamplerQuality.
40    /// Reallocates SRC_STATE
41    fn set_quality(&mut self, quality: ResamplerQuality);
42
43    /// ResamplerQuality getter.
44    fn get_quality(&self) -> ResamplerQuality;
45
46    fn get_channel_layout(&self) -> AudioChannelLayout;
47    fn set_channel_layout(&mut self, layout: AudioChannelLayout);
48
49    /// Panics if a reset error occured.
50    fn reset(&mut self);
51}
52
53#[cfg(feature = "libsamplerate")]
54pub struct Resampler<T: FloatType> {
55    /// Drops in ResamplerPrivateImpl::_drop();
56    pub(super) state: *mut SRC_STATE,
57
58    pub(super) ch_layout: AudioChannelLayout,
59    pub(super) quality: ResamplerQuality,
60
61    pub(super) _phantom: PhantomData<T>,
62}
63
64#[cfg(not(feature = "libsamplerate"))]
65pub struct Resampler<T: FloatType> {
66    pub(super) ch_layout: AudioChannelLayout,
67
68    pub(super) _ph: PhantomData<T>,
69    pub(super) kernel_size: usize,
70    pub(super) window: Vec<T>,
71    pub(super) quality: ResamplerQuality
72}
73
74#[cfg(not(feature = "libsamplerate"))]
75/// Kernel size
76pub enum QTable {
77    Linear = 16,
78    SincLow = 32,
79    SincMedium = 64,
80    SincHigh = 128,
81}