Apply Compiled Transform Function (Advanced Performance API)
Source:R/performance-helpers.R
apply_transform_xptr.RdApply a user-defined C++ transform function to sample values, compiled via RcppXPtrUtils, instead of calling an R callback per sample. Requires the RcppXPtrUtils package.
Details
**ADVANCED API** - Requires RcppXPtrUtils package.
The transform function receives sample amplitude and returns transformed amplitude. Common transforms:
Clipping: `x > threshold ? threshold : (x < -threshold ? -threshold : x)`
Soft clipping: `tanh(x * gain)`
Rectification: `fabs(x)`
Squaring: `x * x`
Examples
# \donttest{
# Compiling a C++ transform function takes a few seconds
if (requireNamespace("RcppXPtrUtils", quietly = TRUE)) {
soft_clip <- RcppXPtrUtils::cppXPtr(
"double softclip(double x) { return tanh(x * 2.0); }",
depends = character()
)
sound <- Sound$create_tone(frequency = 220, duration = 0.3, sampling_rate =
16000)
clipped <- apply_transform_xptr(sound, soft_clip)
}
# }