Module: Muze::Core::Resample

Defined in:
lib/muze/core/resample.rb

Overview

Resampling utilities.

Constant Summary collapse

EPSILON =
1.0e-12

Class Method Summary collapse

Class Method Details

.resample(y, orig_sr:, target_sr:, res_type: :sinc, target_length: nil, taps: 16, beta: 8.6, cutoff: nil) ⇒ Numo::SFloat

Returns resampled waveform.

Parameters:

  • y (Numo::SFloat, Array<Float>)

    waveform signal

  • orig_sr (Integer)

    source sampling rate

  • target_sr (Integer)

    destination sampling rate

  • res_type (Symbol) (defaults to: :sinc)

    :nearest, :linear, :sinc, or :polyphase

  • target_length (Integer, nil) (defaults to: nil)
  • taps (Integer) (defaults to: 16)
  • beta (Float) (defaults to: 8.6)
  • cutoff (Float, nil) (defaults to: nil)

Returns:

  • (Numo::SFloat)

    resampled waveform



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/muze/core/resample.rb', line 19

def resample(y, orig_sr:, target_sr:, res_type: :sinc, target_length: nil, taps: 16, beta: 8.6, cutoff: nil)
  validate_sample_rates!(orig_sr, target_sr)
  validate_resample_options!(target_length:, taps:, beta:, cutoff:)

  signal = Muze::Core::Audio.validate_audio!(y, allow_empty: true)
  return signal if signal.empty?

  if signal.ndim == 2
    return resample_channels(signal, orig_sr:, target_sr:, res_type:, target_length:, taps:, beta:, cutoff:)
  end

  source = signal.to_a
  return adjust_length(source, target_length) if orig_sr == target_sr && target_length
  return signal if orig_sr == target_sr

  case res_type
  when :nearest then nearest_resample(source, orig_sr, target_sr, target_length:)
  when :linear then linear_resample(source, orig_sr, target_sr, target_length:)
  when :sinc then sinc_resample(source, orig_sr, target_sr, target_length:, taps:, beta:, cutoff:)
  when :polyphase then polyphase_resample(source, orig_sr, target_sr, target_length:, taps:, beta:, cutoff:)
  else
    raise Muze::ParameterError, "Unsupported res_type: #{res_type}"
  end
end