Class: AudioTrimmer

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

Overview

author: Ryan Brigden date: June 13, 2015

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ AudioTrimmer

Returns a new instance of AudioTrimmer.


8
9
10
11
12
13
14
15
16
# File 'lib/audio_trimmer.rb', line 8

def initialize(params = {})
  input = params.fetch(:input, "")
  input_length = 0   
  if input.empty? 
    raise "please specify input filepath"
  else
    @input = File.expand_path(input)
  end  
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.


6
7
8
# File 'lib/audio_trimmer.rb', line 6

def input
  @input
end

Instance Method Details

#get_length(file_path) ⇒ Object


33
34
35
# File 'lib/audio_trimmer.rb', line 33

def get_length file_path
  return `soxi -D #{File.expand_path(file_path)}`.to_f
end

#trim(start: 0, finish: get_length(@input), output: "") ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/audio_trimmer.rb', line 18

def trim(start: 0, finish: get_length(@input), output: "")
  unless File.exists?(@input) or File.exists?(output) then raise "bad filepath" end
  if output.empty? or File.expand_path(output)  == @input
    out_arr = @input.split('.')
    out_arr[out_arr.length-2] += "_out" 
    output = out_arr.join(".")
    `sox #{@input} #{output} trim #{start} =#{finish}`
    `mv #{output} #{@input}`
  else
    output = File.expand_path(output)
    `sox #{@input} #{output} trim #{start} =#{finish}`
  end
  return "trim success"
end