Class: StepSequencer::SoundBuilder::DefaultEffects::Pitch

Inherits:
Object
  • Object
show all
Defined in:
lib/step_sequencer/sound_builder/default_effects/pitch.rb

Class Method Summary collapse

Class Method Details

.build(sources:, value:, speed_correction: true) ⇒ Object



5
6
7
# File 'lib/step_sequencer/sound_builder/default_effects/pitch.rb', line 5

def self.build(sources:, value:, speed_correction: true)
  sources.map &change_pitch(value, speed_correction)
end

.build_outfile_name(source, value) ⇒ Object



27
28
29
# File 'lib/step_sequencer/sound_builder/default_effects/pitch.rb', line 27

def build_outfile_name(source, value)
  "#{output_dir}/#{SecureRandom.urlsafe_base64}.mp3"
end

.change_pitch(value, speed_correction) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/step_sequencer/sound_builder/default_effects/pitch.rb', line 9

def self.change_pitch(value, speed_correction)
  ->(source){
    outfile = build_outfile_name(source, value)
    cmd = <<-SH
      ffmpeg -y -i #{source} -af   \
        asetrate=44100*#{value} \
        #{outfile}              \
        2> /dev/null
    SH
    system cmd
    return outfile unless speed_correction
    outfile_with_correct_speed = correct_speed(outfile, value)
    outfile_with_correct_speed
  }
end

.correct_speed(outfile, pitch_change_value) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/step_sequencer/sound_builder/default_effects/pitch.rb', line 30

def correct_speed(outfile, pitch_change_value)
  inverse_value = Rational(1) / Rational(pitch_change_value)
  builder.build(
    sources: [outfile],
    effect: :Speed,
    args: [value: inverse_value]
  ).shift
end