logo
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
/// Processes the audio buffer of `f32` in place through the loaded pd patch.
///
/// The processing order is like the following, `input_buffer -> libpd -> output_buffer`.
///
/// Call this in your **audio callback**.
///
/// # Examples
/// ```no_run
/// use libpd_rs::process::process_float;
/// use libpd_rs::block_size;
///
/// let output_channels = 2;
/// // ...
/// // After initializing audio and opening a patch file then in the audio callback..
///
/// // We can imagine that these are the buffers which has been handed to us by the audio callback.
/// let input_buffer = [0.0_f32; 512];
/// let mut output_buffer = [0.0_f32; 1024];
///
/// let buffer_size = output_buffer.len() as i32;
/// let pd_ticks: i32 = buffer_size / (block_size() * output_channels);
///
/// process_float(pd_ticks, &input_buffer, &mut output_buffer);
/// // Or if you wish,
/// // the input buffer can also be an empty slice if you're not going to use any inputs.
/// process_float(pd_ticks, &[], &mut output_buffer);
/// ```
/// # Panics
///
/// This function may panic for multiple reasons,
/// first of all there is a mutex lock used internally and also it processes buffers in place so there are possibilities of segfaults.
/// Use with care.
pub fn process_float(ticks: i32, input_buffer: &[f32], output_buffer: &mut [f32]) {
    unsafe {
        libpd_sys::libpd_process_float(ticks, input_buffer.as_ptr(), output_buffer.as_mut_ptr());
    }
}

/// Processes the audio buffer of `i16` in place through the loaded pd patch.
///
/// The processing order is like the following, `input_buffer -> libpd -> output_buffer`.
///
/// Float samples are converted to short by multiplying by `32767` and casting,
/// so any values received from pd patches beyond `-1` to `1` will result in garbage.
///
/// Note: for efficiency, does *not* clip input
///
/// Call this in your **audio callback**.
///
/// # Examples
/// ```no_run
/// use libpd_rs::process::process_short;
/// use libpd_rs::block_size;
///
/// let output_channels = 2;
/// // ...
/// // After initializing audio and opening a patch file then in the audio callback..
///
/// // We can imagine that these are the buffers which has been handed to us by the audio callback.
/// let input_buffer = [0_i16; 512];
/// let mut output_buffer = [0_i16; 1024];
///
/// let buffer_size = output_buffer.len() as i32;
/// let pd_ticks: i32 = buffer_size / (block_size() * output_channels);
///
/// process_short(pd_ticks, &input_buffer, &mut output_buffer);
/// // Or if you wish,
/// // the input buffer can also be an empty slice if you're not going to use any inputs.
/// process_short(pd_ticks, &[], &mut output_buffer);
/// ```
/// # Panics
///
/// This function may panic for multiple reasons,
/// first of all there is a mutex lock used internally and also it processes buffers in place so there are possibilities of segfaults.
/// Use with care.

pub fn process_short(ticks: i32, input_buffer: &[i16], output_buffer: &mut [i16]) {
    unsafe {
        libpd_sys::libpd_process_short(ticks, input_buffer.as_ptr(), output_buffer.as_mut_ptr());
    }
}

/// Processes the audio buffer of `f64` in place through the loaded pd patch.
///
/// The processing order is like the following, `input_buffer -> libpd -> output_buffer`.
///
/// Call this in your **audio callback**.
///
/// # Examples
/// ```no_run
/// use libpd_rs::process::process_double;
/// use libpd_rs::block_size;
///
/// let output_channels = 2;
/// // ...
/// // After initializing audio and opening a patch file then in the audio callback..
///
/// // We can imagine that these are the buffers which has been handed to us by the audio callback.
/// let input_buffer = [0.0_f64; 512];
/// let mut output_buffer = [0.0_f64; 1024];
///
/// let buffer_size = output_buffer.len() as i32;
/// let pd_ticks: i32 = buffer_size / (block_size() * output_channels);
///
/// process_double(pd_ticks, &input_buffer, &mut output_buffer);
/// // Or if you wish,
/// // the input buffer can also be an empty slice if you're not going to use any inputs.
/// process_double(pd_ticks, &[], &mut output_buffer);
/// ```
/// # Panics
///
/// This function may panic for multiple reasons,
/// first of all there is a mutex lock used internally and also it processes buffers in place so there are possibilities of segfaults.
/// Use with care.
pub fn process_double(ticks: i32, input_buffer: &[f64], output_buffer: &mut [f64]) {
    unsafe {
        libpd_sys::libpd_process_double(ticks, input_buffer.as_ptr(), output_buffer.as_mut_ptr());
    }
}

/// Processes the **non-interleaved** `f32` audio buffer in place through the loaded pd patch.
///
/// The processing order is like the following, `input_buffer -> libpd -> output_buffer`.
///
/// Copies buffer contents to/from libpd without striping.
///
/// Call this in your **audio callback**.
///
/// # Examples
/// ```no_run
/// use libpd_rs::process::process_raw;
///
/// // After initializing audio and opening a patch file then in the audio callback..
///
/// // We can imagine that these are the buffers which has been handed to us by the audio callback.
/// let input_buffer = [0.0_f32; 512];
/// let mut output_buffer = [0.0_f32; 1024];
///
/// process_raw(&input_buffer, &mut output_buffer);
/// // Or if you wish,
/// // the input buffer can also be an empty slice if you're not going to use any inputs.
/// process_raw(&[], &mut output_buffer);
/// ```
/// # Panics
///
/// This function may panic for multiple reasons,
/// first of all there is a mutex lock used internally and also it processes buffers in place so there are possibilities of segfaults.
/// Use with care.
pub fn process_raw(input_buffer: &[f32], output_buffer: &mut [f32]) {
    unsafe {
        libpd_sys::libpd_process_raw(input_buffer.as_ptr(), output_buffer.as_mut_ptr());
    }
}

/// Processes the **non-interleaved** `i16` audio buffer in place through the loaded pd patch.
///
/// The processing order is like the following, `input_buffer -> libpd -> output_buffer`.
///
/// Copies buffer contents to/from libpd without striping.
///
/// Float samples are converted to short by multiplying by `32767` and casting,
/// so any values received from pd patches beyond `-1` to `1` will result in garbage.
///
/// Note: for efficiency, does *not* clip input
///
/// Call this in your **audio callback**.
///
/// # Examples
/// ```no_run
/// use libpd_rs::process::process_raw_short;
///
/// // After initializing audio and opening a patch file then in the audio callback..
///
/// // We can imagine that these are the buffers which has been handed to us by the audio callback.
/// let input_buffer = [0_i16; 512];
/// let mut output_buffer = [0_i16; 1024];
///
/// process_raw_short(&input_buffer, &mut output_buffer);
/// // Or if you wish,
/// // the input buffer can also be an empty slice if you're not going to use any inputs.
/// process_raw_short(&[], &mut output_buffer);
/// ```
/// # Panics
///
/// This function may panic for multiple reasons,
/// first of all there is a mutex lock used internally and also it processes buffers in place so there are possibilities of segfaults.
/// Use with care.

pub fn process_raw_short(input_buffer: &[i16], output_buffer: &mut [i16]) {
    unsafe {
        libpd_sys::libpd_process_raw_short(input_buffer.as_ptr(), output_buffer.as_mut_ptr());
    }
}

/// Processes the **non-interleaved** `f64` audio buffer in place through the loaded pd patch.
///
/// The processing order is like the following, `input_buffer -> libpd -> output_buffer`.
///
/// Copies buffer contents to/from libpd without striping.
///
/// Call this in your **audio callback**.
///
/// # Examples
/// ```no_run
/// use libpd_rs::process::process_raw_double;
///
/// // After initializing audio and opening a patch file then in the audio callback..
///
/// // We can imagine that these are the buffers which has been handed to us by the audio callback.
/// let input_buffer = [0.0_f64; 512];
/// let mut output_buffer = [0.0_f64; 1024];
///
/// process_raw_double(&input_buffer, &mut output_buffer);
/// // Or if you wish,
/// // the input buffer can also be an empty slice if you're not going to use any inputs.
/// process_raw_double(&[], &mut output_buffer);
/// ```
/// # Panics
///
/// This function may panic for multiple reasons,
/// first of all there is a mutex lock used internally and also it processes buffers in place so there are possibilities of segfaults.
/// Use with care.
pub fn process_raw_double(input_buffer: &[f64], output_buffer: &mut [f64]) {
    unsafe {
        libpd_sys::libpd_process_raw_double(input_buffer.as_ptr(), output_buffer.as_mut_ptr());
    }
}