Skip to main content

r8brain_sys/
resampler.rs

1use core::ffi::c_int;
2
3use crate::EDSPFilterPhaseResponse;
4
5macro_rules! opaque {
6    ($($name:ident),+ $(,)?) => {$(
7        #[repr(C)]
8        #[derive(Debug)]
9        pub struct $name {
10            _unused: [u8; 0],
11        }
12    )+};
13}
14
15opaque!(
16    HCDSPResampler,
17    HCDSPResampler16,
18    HCDSPResampler16IR,
19    HCDSPResampler24,
20);
21
22unsafe extern "C" {
23    /// Constructs `r8b::CDSPResampler` with explicit filter parameters.
24    pub fn resampler_create(
25        SrcSampleRate: f64,
26        DstSampleRate: f64,
27        MaxInLen: c_int,
28        ReqTransBand: f64,
29        ReqAtten: f64,
30        ReqPhase: EDSPFilterPhaseResponse,
31    ) -> *mut HCDSPResampler;
32
33    /// Constructs the 16-bit linear-phase preset.
34    pub fn resampler16_create(
35        SrcSampleRate: f64,
36        DstSampleRate: f64,
37        MaxInLen: c_int,
38        ReqTransBand: f64,
39    ) -> *mut HCDSPResampler16;
40
41    /// Constructs the 16-bit impulse-response preset.
42    pub fn resampler16IR_create(
43        SrcSampleRate: f64,
44        DstSampleRate: f64,
45        MaxInLen: c_int,
46        ReqTransBand: f64,
47    ) -> *mut HCDSPResampler16IR;
48
49    /// Constructs the 24-bit/32-bit-floating-point linear-phase preset.
50    pub fn resampler24_create(
51        SrcSampleRate: f64,
52        DstSampleRate: f64,
53        MaxInLen: c_int,
54        ReqTransBand: f64,
55    ) -> *mut HCDSPResampler24;
56}
57
58macro_rules! declare_resampler_methods {
59    ($handle:ty; $destroy:ident, $clear:ident, $before_pos:ident,
60     $before_start:ident, $required:ident, $latency:ident, $latency_frac:ident,
61     $max_out:ident, $process:ident, $oneshot:ident) => {
62        unsafe extern "C" {
63            /// Destroys the C++ resampler object.
64            pub fn $destroy(resampler: *mut $handle);
65            /// Restores the object to its post-construction state.
66            pub fn $clear(resampler: *mut $handle);
67            /// Returns the input length before the requested output position.
68            pub fn $before_pos(resampler: *mut $handle, ReqOutPos: c_int) -> c_int;
69            /// Iterative legacy version of `getInLenBeforeOutPos`; clears the object.
70            pub fn $before_start(resampler: *mut $handle, ReqOutPos: c_int) -> c_int;
71            /// Returns the input required to produce at least this many outputs.
72            pub fn $required(resampler: *mut $handle, ReqOutSamples: c_int) -> c_int;
73            /// Returns whole-sample output latency.
74            pub fn $latency(resampler: *mut $handle) -> c_int;
75            /// Returns fractional output latency.
76            pub fn $latency_frac(resampler: *mut $handle) -> f64;
77            /// Returns the maximum output length for a processing call.
78            pub fn $max_out(resampler: *mut $handle, MaxInLen: c_int) -> c_int;
79            /// Processes a streaming input block and updates the borrowed output pointer.
80            pub fn $process(
81                resampler: *mut $handle,
82                input: *mut f64,
83                InputLen: c_int,
84                output: *mut *mut f64,
85            ) -> c_int;
86            /// Performs exact-length one-shot `f64` resampling and clears the object.
87            pub fn $oneshot(
88                resampler: *mut $handle,
89                input: *const f64,
90                InputLen: c_int,
91                output: *mut f64,
92                OutputLen: c_int,
93            );
94        }
95    };
96}
97
98declare_resampler_methods!(HCDSPResampler;
99    resampler_destroy, resampler_clear, resampler_getInLenBeforeOutPos,
100    resampler_getInLenBeforeOutStart, resampler_getInputRequiredForOutput,
101    resampler_getLatency, resampler_getLatencyFrac, resampler_getMaxOutLen,
102    resampler_process, resampler_oneshot);
103declare_resampler_methods!(HCDSPResampler16;
104    resampler16_destroy, resampler16_clear, resampler16_getInLenBeforeOutPos,
105    resampler16_getInLenBeforeOutStart,
106    resampler16_getInputRequiredForOutput, resampler16_getLatency,
107    resampler16_getLatencyFrac, resampler16_getMaxOutLen,
108    resampler16_process, resampler16_oneshot);
109declare_resampler_methods!(HCDSPResampler16IR;
110    resampler16IR_destroy, resampler16IR_clear,
111    resampler16IR_getInLenBeforeOutPos, resampler16IR_getInLenBeforeOutStart,
112    resampler16IR_getInputRequiredForOutput, resampler16IR_getLatency,
113    resampler16IR_getLatencyFrac, resampler16IR_getMaxOutLen,
114    resampler16IR_process, resampler16IR_oneshot);
115declare_resampler_methods!(HCDSPResampler24;
116    resampler24_destroy, resampler24_clear, resampler24_getInLenBeforeOutPos,
117    resampler24_getInLenBeforeOutStart,
118    resampler24_getInputRequiredForOutput, resampler24_getLatency,
119    resampler24_getLatencyFrac, resampler24_getMaxOutLen,
120    resampler24_process, resampler24_oneshot);