HResampler
A resampler.
Asynchronous Resamplers
The resampling is based on band-limited interpolation using sinc interpolation filters. The sinc interpolation upsamples by an adjustable factor, and then the new sample points are calculated by interpolating between these points.
The resampling ratio can be updated at any time.
SincFixedIn
SincFixedOut
FastFixedIn
FastFixedOut
Synchronous Resamplers
Is implemented via FFT. The data is FFTed, the spectrum modified, and then inverse FFTed to get the resampled data.
This type of resampler is considerably faster but doesn’t support changing the resampling ratio.
FftFixedIn
FftFixedInOut
FftFixedOut
Methods
new_fft
new_fft(sr_in: integer, sr_out: integer, chunk_size: integer, sub_chunks: integer, nbr_channels: integer, res_type: HResamplerType, dtype: HDataType) -> HResampler
source
Creates a new FFT type HResampler.
Supports any of [FftFixedIn, FftFixedInOut, FftFixedOut]
HResamplerType
.
The resampling is done by FFTing the input data. The spectrum is then extended or truncated as well as multiplied with an antialiasing filter before it’s inverse transformed to get the resampled waveforms.
FftFixedIn
A synchronous resampler that needs a fixed number of audio frames for input and returns a variable number of frames.
FftFixedInOut
A synchronous resampler that accepts a fixed number of audio frames for input and returns a fixed number of frames.
FftFixedOut
A synchronous resampler that needs a fixed number of audio frames for input and returns a variable number of frames.
Arguments
sr_in
The input sampling rate in hz.
sr_out
The output sampling rate in hz.
chunk_size
Chunks size of input or output data in frames.
It can be used as input or output, depending on HResamplerType
.
sub_chunks
Desired number of subchunks for processing, actual number may be different.
nbr_channels
Number of channels in input and output.
Must be the same number of channels as the HAudio
that will be processed by the HResampler
.
res_type
An HResamplerType
to indicate which type of HResampler
to be created.
dtype
A float HDataType
to indicate the dtype that the HResampler
will be working with.
Must be the same as the HAudio
’s dtype that will be processed by the HResampler
.
Returns
A FFT type HResampler
.
Examples
library(harmonium)
= 48000L
sr_in = 44100L
sr_out = 1024L
chunk_size = 2L
sub_chunks = 2L
nbr_channels = HResamplerType$FftFixedIn
res_type = HDataType$Float32
dtype
= HResampler$new_fft(sr_in, sr_out, chunk_size, sub_chunks, nbr_channels, res_type, dtype) hresampler
new_sinc
new_sinc(resample_ratio: double, max_resample_ratio_relative: double, parameters: HSincInterpolationParameters, chunk_size: integer, nchannels: integer, res_type: HResamplerType, dtype: HDataType) -> HResampler
source
Creates a new Sinc type HResampler.
Supports any of [SincFixedIn, SincFixedOut]
HResamplerType
.
The resampling is done by creating a number of intermediate points (defined by oversampling_factor) by sinc interpolation. The new samples are then calculated by interpolating between these points.
SincFixedIn
An asynchronous resampler that accepts a fixed number of audio frames for input and returns a variable number of frames.
SincFixedOut
An asynchronous resampler that accepts a variable number of audio frames for input nad returns a fixed number of frames.
Arguments
resample_ratio
The output’s sampling rate divided by the input’s sampling rate.
max_resample_ratio_relative
Maximum ratio that can be set with set_resample_ratio
relative to resample_ratio
, must be >= 1.0. The minimum relative ratio is the reciprocal of the maximum. For example, with max_resample_ratio_relative
of 10.0, the ratio can be set between resample_ratio * 10.0
and resample_ratio / 10.0
.
parameters
An HSincInterpolationParameters
. Parameters for interpolation.
chunk_size
Chunks size of input or output data in frames.
nchannels
Number of channels in input and output.
Must be the same number of channels as the HAudio
that will be processed by the HResampler
.
res_type
An HResamplerType
. Indicates which type of HResampler
to be created.
dtype
A float HDataType
to indicate the dtype that the HResampler
will be working with.
Must be the same as the HAudio
’s dtype that will be processed by the HResampler
.
Returns
A Sinc type HResampler
.
Examples
library(harmonium)
= 44100L
sr_in = 48000L
sr_out = sr_out / sr_in
resample_ratio = 2
max_resample_ratio_relative = HSincInterpolationParameters$new(256, 0.95, 256, "linear", "blackmanharris2")
hparams = 512L
chunk_size = 2L
nchannels = HResamplerType$SincFixedOut
res_type = HDataType$Float32
dtype
= HResampler$new_sinc(resample_ratio, max_resample_ratio_relative, hparams, chunk_size, nchannels, res_type, dtype) res
new_fast
new_sinc(resample_ratio: double, max_resample_ratio_relative: double, pol_deg: HPolynomialDegree, chunk_size: integer, nchannels: integer, res_type: HResamplerType, dtype: HDataType) -> HResampler
source
Creates a new Fast type HResampler.
Supports any of [FastFixedIn, FastFixedOut]
HResamplerType
.
The resampling is done by interpolating between the input samples by fitting polynomials.
Note that no anti-aliasing filter is used. This makes it run considerably faster than the corresponding SincFixedIn
, which performs anti-aliasing filtering. The price is that the resampling creates some artefacts in the output, mainly at higher frequencies. Use SincFixedIn
if this can not be tolerated.
FastFixedIn
An asynchronous resampler that accepts a fixed number of audio frames for input and returns a variable number of frames.
FastFixedOut
An asynchronous resampler that accepts a variable number of audio frames for input nad returns a fixed number of frames.
Arguments
resample_ratio
The output’s sampling rate divided by the input’s sampling rate.
max_resample_ratio_relative
Maximum ratio that can be set with set_resample_ratio
relative to resample_ratio
, must be >= 1.0. The minimum relative ratio is the reciprocal of the maximum. For example, with max_resample_ratio_relative
of 10.0, the ratio can be set between resample_ratio * 10.0
and resample_ratio / 10.0
.
pol_deg
An HPolynomialDegree
. Used to select the polynomial degree for interpolation.
chunk_size
Chunks size of input or output data in frames.
nchannels
Number of channels in input and output.
Must be the same number of channels as the HAudio
that will be processed by the HResampler
.
res_type
An HResamplerType
. Indicates which type of HResampler
to be created.
dtype
A float HDataType
to indicate the dtype that the HResampler
will be working with.
Must be the same as the HAudio
’s dtype that will be processed by the HResampler
.
Returns
A Fast type HResampler
.
Examples
library(harmonium)
= 44100L
sr_in = 48000L
sr_out = sr_out / sr_in
resample_ratio = 2
max_resample_ratio_relative = HPolynomialDegree$linear
pol_deg = 512L
chunk_size = 2L
nchannels = HResamplerType$FastFixedOut
res_type = HDataType$Float32
dtype
= HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype) res
process
process(harray: HArray)
source
Process the resampler, changing the HArray
’s sampling rate.
Arguments
harray
An HArray
that will have it’s sampling rate converted.
Examples
library(harmonium)
= matrix(0, nrow = 512, ncol = 2)
arr = HArray$new_from_values(arr, dtype = HDataType$Float64)
harray = HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
hparams = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res $process(harray) res
set_resample_ratio
set_resample_ratio(new_ratio: double, ramp: bool)
source
Update the resample ratio.
For asynchronous resamplers, the ratio must be within original / maximum
to original * maximum
, where original
and maximum
are the resampling ratios that were provided to the constructor. Trying to set the ratio outside these bounds will return an error.
For synchronous resamplers, this will always return an error.
Arguments
new_ratio
The new resample_ratio
to be set.
ramp
If TRUE
, the ratio will be ramped from the old to the new value during processing of the next chunk. This allows smooth transitions from one ratio to another. If FALSE
, the new ratio will be applied from the start of the next chunk.
Examples
library(harmonium)
= matrix(0, nrow = 512, ncol = 2)
data = HAudio$new_from_values(data, 44100, dtype = HDataType$Float64)
haudio = HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
hparams = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res $set_resample_ratio(1, FALSE) res
set_resample_ratio_relative
set_resample_ratio_relative(rel_ratio: double, ramp: bool)
source
Update the resample ratio as a factor relative to the original one.
For asynchronous resamplers, the relative ratio must be within 1 / maximum
to maximum
, where maximum
is the maximum resampling ratio that was provided to the constructor. Trying to set the ratio outside these bounds will return an error. Higher ratios above 1.0
slow down the output and lower the pitch. Lower ratios below 1.0
speed up the output and raise the pitch.
For synchronous resamplers, this will always return an error.
Arguments
rel_ratio
: A factor to update the resample_ratio relative to the original one.ramp
: IfTRUE
, the ratio will be ramped from the old to the new value during processing of the next chunk. This allows smooth transitions from one ratio to another. If ramp is false, the new ratio will be applied from the start of the next chunk.
Examples
library(harmonium)
= matrix(0, nrow = 512, ncol = 2)
data = HAudio$new_from_values(data, 44100, dtype = HDataType$Float64)
haudio = HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
hparams = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res $set_resample_ratio_relative(0.5, FALSE) res
nbr_channels
Get the maximum number of channels this Resampler is configured for. source
Examples
library(harmonium)
= HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
hparams = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res $nbr_channels() res
set_chunk_size
set_chunk_size(chunk_size: integer)
source
Change the chunk size for the resampler. This is not supported by all resampler types. The value must be equal to or smaller than the chunk size the value that the resampler was created with. ResampleError::InvalidChunkSize is returned if the value is zero or too large.
The meaning of chunk size depends on the resampler, it refers to the input size for FixedIn, and output size for FixedOut types.
Types that do not support changing the chunk size return ResampleError::ChunkSizeNotAdjustable.
Arguments
chunk_size
: The newchunk_size
to be set.
Examples
library(harmonium)
= HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
hparams = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res $set_chunk_size(1024L) res
reset
reset()
source
Reset the resampler state and clear all internal buffers.
Examples
library(harmonium)
= 44100L
sr_in = 48000L
sr_out = sr_out / sr_in
resample_ratio = 2
max_resample_ratio_relative = HPolynomialDegree$linear
pol_deg = 512L
chunk_size = 2L
nchannels = HResamplerType$FastFixedOut
res_type = HDataType$Float32
dtype
= HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res $reset() res
res_type
res_type() -> HResamplerType
source
Gets the HResampler
’s type.
Returns
An HResamplerType
.
Examples
library(harmonium)
= 44100L
sr_in = 48000L
sr_out = sr_out / sr_in
resample_ratio = 2
max_resample_ratio_relative = HPolynomialDegree$linear
pol_deg = 512L
chunk_size = 2L
nchannels = HResamplerType$FastFixedOut
res_type = HDataType$Float32
dtype
= HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res $res_type() res
dtype
dtype() -> HDataType
source
Gets the HResampler
’s dtype.
Returns
An HDataType
.
Examples
library(harmonium)
= 44100L
sr_in = 48000L
sr_out = sr_out / sr_in
resample_ratio = 2
max_resample_ratio_relative = HPolynomialDegree$linear
pol_deg = 512L
chunk_size = 2L
nchannels = HResamplerType$FastFixedOut
res_type = HDataType$Float32
dtype
= HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res $dtype() res
print()
source
Prints the HResampler
.
Differently from R’s normal behaviour, print
doesn’t return the value invisibly.
Examples
library(harmonium)
= 44100L
sr_in = 48000L
sr_out = sr_out / sr_in
resample_ratio = 2
max_resample_ratio_relative = HPolynomialDegree$linear
pol_deg = 512L
chunk_size = 2L
nchannels = HResamplerType$FastFixedOut
res_type = HDataType$Float32
dtype
= HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res $print()
res
# or similarly:
print(res)