Class: Sox::Cmd

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/sox/cmd.rb

Overview

Process audio files using the sox shell command.

Examples:

# Mix 3 files into one
sox = Sox::Cmd.new(:combine => :mix)
sox.add_input("guitar1.flac")
sox.add_input("guitar2.flac")
sox.add_input("drums.flac")
sox.set_output("hell_rock-n-roll.mp3")
sox.set_effects(:rate => 44100, :channels => 2)
sox.run

Constant Summary

Constants included from Shell

Shell::BASH_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shell

#bash, #sh

Constructor Details

#initialize(options = {}) ⇒ Cmd

Returns a new instance of Cmd.

Parameters:

  • options (Hash) (defaults to: {})

    global options for sox command



19
20
21
22
23
# File 'lib/sox/cmd.rb', line 19

def initialize(options = {})
  @options = options
  @inputs  = []
  @effects = {}
end

Instance Attribute Details

#effectsObject (readonly)

Returns the value of attribute effects.



16
17
18
# File 'lib/sox/cmd.rb', line 16

def effects
  @effects
end

#inputsObject (readonly)

Returns the value of attribute inputs.



16
17
18
# File 'lib/sox/cmd.rb', line 16

def inputs
  @inputs
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/sox/cmd.rb', line 16

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



16
17
18
# File 'lib/sox/cmd.rb', line 16

def output
  @output
end

Instance Method Details

#add_input(file_path, input_options = {}) ⇒ Sox::Cmd

Add input file with its options.

Parameters:

  • file_path (String)

    path to file

  • input_options (Hash) (defaults to: {})

    options for input files, see man sox

Returns:



31
32
33
34
# File 'lib/sox/cmd.rb', line 31

def add_input(file_path, input_options = {})
  @inputs << Sox::File.new(file_path, input_options)
  self
end

#runBoolean

Run ‘sox` command. Raise Error on fail.

Returns:

  • (Boolean)

    true in case of success

Raises:



77
78
79
80
81
82
83
# File 'lib/sox/cmd.rb', line 77

def run
  raise(Sox::Error, "Output is missing, specify it with `set_output`")   unless @output
  raise(Sox::Error, "Inputs are missing, specify them with `add_input`") if @inputs.empty?

  cmd = CommandBuilder.new(@inputs, @output, @options, @effects).build
  sh(cmd)
end

#set_effects(effects) ⇒ Sox::Cmd

Set effects on the output file. See man sox section EFFECTS. It receives the effect name as a hash key and the effect arguments as hash values which can be a string or an array of strings. If an effect has no arguments just pass true as the value.

Examples:

# Normalize and use 2 channels for output
sox_cmd.set_effects(:channels => 2, :norm => true)

Parameters:

  • effects (Hash{Symbol, String => Symbol, String, Array<String>})

Returns:



59
60
61
62
# File 'lib/sox/cmd.rb', line 59

def set_effects(effects)
  @effects = effects
  self
end

#set_options(options) ⇒ Sox::Cmd

Set global options. See man sox section Global Options.

Parameters:

  • options (Hash)

    global options for sox command

Returns:



69
70
71
72
# File 'lib/sox/cmd.rb', line 69

def set_options(options)
  @options = options
  self
end

#set_output(file_path, output_options = {}) ⇒ Sox::Cmd

Set output file and its options.

Parameters:

  • file_path (String)

    ouput file path

  • output_options (Hash) (defaults to: {})

    options for output file, see man sox

Returns:



42
43
44
45
# File 'lib/sox/cmd.rb', line 42

def set_output(file_path, output_options = {})
  @output = Sox::File.new(file_path, output_options)
  self
end