Class: Resample

Inherits:
Object
  • Object
show all
Defined in:
lib/build_audulus_wavetable_node.rb

Class Method Summary collapse

Class Method Details

.dampen_higher_partials(sample_rate, fundamental, fft) ⇒ Object

kill everything higher than a scaled nyquist limit ease in/out everything else to minimize partials near nyquist



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/build_audulus_wavetable_node.rb', line 191

def self.dampen_higher_partials(sample_rate, fundamental, fft)
  nyquist = sample_rate.to_f / 2
  sample_fundamental = sample_rate.to_f / fft.count
  scaled_nyquist = nyquist / fundamental * sample_fundamental
  sample_duration = fft.count.to_f / sample_rate
  sub_nyquist_sample_count = scaled_nyquist * sample_duration
  fft.each_with_index.map {|power, i|
    hz = i.to_f / fft.count * sample_rate.to_f
    if hz < scaled_nyquist
      scale_partial(i, sub_nyquist_sample_count, power)
    else
      0+0i
    end
  }
end

.resample_for_fundamental(sample_rate, fundamental, samples) ⇒ Object

sample_rate, Hz, e.g. 44100 fundamental, Hz, e.g. 440 samples: -1..1



183
184
185
186
187
# File 'lib/build_audulus_wavetable_node.rb', line 183

def self.resample_for_fundamental(sample_rate, fundamental, samples)
  fft = FFTW3.fft(NArray[samples]).to_a.flatten
  dampened = dampen_higher_partials(sample_rate, fundamental, fft)
  (FFTW3.ifft(NArray[dampened]) / samples.count).real.to_a.flatten
end

.scale_partial(partial_index, partial_count, partial_value) ⇒ Object

dampen partials higher than a certain frequency using a smooth “ease-in-out” shape



209
210
211
# File 'lib/build_audulus_wavetable_node.rb', line 209

def self.scale_partial(partial_index, partial_count, partial_value)
  partial_value * (Math.cos(partial_index.to_f*Math::PI/2/partial_count)**2)
end