class Sox::CommandBuilder

Builds the sox shell command from input files, an output file, options and effects.

@example

builder = Sox::CommandBuilder.new(['in1.mp3', 'in2.ogg'], 'out.wav',
  {:combine => :mix},
  {:rate => 44100, :channels => 2}
)
builder.build  # => "sox --combine mix in1.mp3 in2.ogg out.wav rate 44100 channels 2"

Attributes

effects[RW]
input_files[RW]
options[RW]
output_file[RW]

Public Class Methods

new(input_files, output_file, options = {}, effects = {}) click to toggle source

@param input_files [Array<Sox::File>] @param output_file [Sox::File] @param options [Hash{Symbol => Symbol}] @param effects [Hash{Symbol => Symbol}]

# File lib/sox/command_builder.rb, line 18
def initialize(input_files, output_file, options = {}, effects = {})
  @input_files = input_files
  @output_file = output_file
  @options     = options
  @effects     = effects
end

Public Instance Methods

build() click to toggle source

Build shell command with all arguments and options.

@return [String]

# File lib/sox/command_builder.rb, line 28
def build
  [ Sox::SOX_COMMAND,
    build_options(@options),
    build_input_files,
    build_file(@output_file),
    build_effects
  ].flatten.join(' ')
end

Private Instance Methods

build_effects() click to toggle source

Build effects with their arguments (if present) to be used in shell command.

@return [Array<String>] effects to be concatenated into string

# File lib/sox/command_builder.rb, line 77
def build_effects
  @effects.inject([]) do |result, (effect, val)|
    if val
      result << effect
      result << val.to_s if val != true
    end
    result
  end
end
build_file(file) click to toggle source

Build part of SoX command which represents file(input or output).

@param file [Sox::File] file

@return [String]

# File lib/sox/command_builder.rb, line 51
def build_file(file)
  opts = build_options(file.options)
  file_path = file.escaped? ? file.path : Shellwords.escape(file.path)
  [opts, file_path]
end
build_input_files() click to toggle source

Build input files with their options.

@return [Array<String>]

# File lib/sox/command_builder.rb, line 41
def build_input_files
  @input_files.map { |file| build_file(file) }
end
build_options(options) click to toggle source

Build options with their values (if present) to be used in shell command.

@param options [Hash] options

@return [Array<String>] options to be concatenated into string

# File lib/sox/command_builder.rb, line 63
def build_options(options)
  options.inject([]) do |result, (opt, val)|
    if val
      result << "--#{shellify_opt(opt)}"
      result << shellify_opt(val) if val != true
    end
    result
  end
end
shellify_opt(value) click to toggle source

Convert option or its value to shell style, separating words with “-”.

@param value [Symbol, String] option or value

@return [String] shellified option

# File lib/sox/command_builder.rb, line 93
def shellify_opt(value)
  value.to_s.gsub('_', '-')
end