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)
sr_in = 48000L
sr_out = 44100L
chunk_size = 1024L
sub_chunks = 2L
nbr_channels = 2L
res_type = HResamplerType$FftFixedIn
dtype = HDataType$Float32

hresampler = HResampler$new_fft(sr_in, sr_out, chunk_size, sub_chunks, nbr_channels, res_type, dtype)

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)
sr_in = 44100L
sr_out = 48000L
resample_ratio = sr_out / sr_in
max_resample_ratio_relative = 2
hparams = HSincInterpolationParameters$new(256, 0.95, 256, "linear", "blackmanharris2")
chunk_size = 512L
nchannels = 2L
res_type = HResamplerType$SincFixedOut
dtype = HDataType$Float32

res = HResampler$new_sinc(resample_ratio, max_resample_ratio_relative, hparams, chunk_size, nchannels, res_type, dtype)

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)
sr_in = 44100L
sr_out = 48000L
resample_ratio = sr_out / sr_in
max_resample_ratio_relative = 2
pol_deg = HPolynomialDegree$linear
chunk_size = 512L
nchannels = 2L
res_type = HResamplerType$FastFixedOut
dtype = HDataType$Float32

res = HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)

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)
arr = matrix(0, nrow = 512, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$Float64)
hparams = HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
res = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res$process(harray)

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)
data = matrix(0, nrow = 512, ncol = 2)
haudio = HAudio$new_from_values(data, 44100, dtype = HDataType$Float64)
hparams = HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
res = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res$set_resample_ratio(1, FALSE)

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

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 ramp is false, the new ratio will be applied from the start of the next chunk.

Examples

library(harmonium)
data = matrix(0, nrow = 512, ncol = 2)
haudio = HAudio$new_from_values(data, 44100, dtype = HDataType$Float64)
hparams = HSincInterpolationParameters$new(256L, 0.95, 256L, "linear", "blackmanharris2")
res = HResampler$new_sinc(48000L / 44100L, 2, hparams, 512L, 2L, HResamplerType$SincFixedIn, HDataType$Float64)
res$set_resample_ratio_relative(0.5, FALSE)

reset

reset() source

Reset the resampler state and clear all internal buffers.

Examples

library(harmonium)
sr_in = 44100L
sr_out = 48000L
resample_ratio = sr_out / sr_in
max_resample_ratio_relative = 2
pol_deg = HPolynomialDegree$linear
chunk_size = 512L
nchannels = 2L
res_type = HResamplerType$FastFixedOut
dtype = HDataType$Float32

res = HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res$reset()

res_type

res_type() -> HResamplerType source

Gets the HResampler’s type.

Returns

An HResamplerType.

Examples

library(harmonium)
sr_in = 44100L
sr_out = 48000L
resample_ratio = sr_out / sr_in
max_resample_ratio_relative = 2
pol_deg = HPolynomialDegree$linear
chunk_size = 512L
nchannels = 2L
res_type = HResamplerType$FastFixedOut
dtype = HDataType$Float32

res = HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res$res_type()

dtype

dtype() -> HDataType source

Gets the HResampler’s dtype.

Returns

An HDataType.

Examples

library(harmonium)
sr_in = 44100L
sr_out = 48000L
resample_ratio = sr_out / sr_in
max_resample_ratio_relative = 2
pol_deg = HPolynomialDegree$linear
chunk_size = 512L
nchannels = 2L
res_type = HResamplerType$FastFixedOut
dtype = HDataType$Float32

res = HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res$dtype()

print

print() source

Prints the HResampler.

Differently from R’s normal behaviour, print doesn’t return the value invisibly.

Examples

library(harmonium)
sr_in = 44100L
sr_out = 48000L
resample_ratio = sr_out / sr_in
max_resample_ratio_relative = 2
pol_deg = HPolynomialDegree$linear
chunk_size = 512L
nchannels = 2L
res_type = HResamplerType$FastFixedOut
dtype = HDataType$Float32

res = HResampler$new_fast(resample_ratio, max_resample_ratio_relative, pol_deg, chunk_size, nchannels, res_type, dtype)
res$print()

# or similarly:
print(res)