Class: Sox::Command

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

Overview

Sox.command do |sox|

  sox.input "input1.wav"
  sox.input "input2", :type => "ogg"

  sox.output "output", :type => "ogg", :compression => 8
end

Defined Under Namespace

Classes: Effect, File, Playlist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Command

Returns a new instance of Command.

Yields:

  • (_self)

Yield Parameters:

  • _self (Sox::Command)

    the object that the method was called on



33
34
35
36
37
# File 'lib/sox/command.rb', line 33

def initialize(&block)
  @inputs = []
  @effects = []
  yield self if block_given?
end

Instance Attribute Details

#effectsObject

Returns the value of attribute effects.



29
30
31
# File 'lib/sox/command.rb', line 29

def effects
  @effects
end

#inputsObject

Returns the value of attribute inputs.



29
30
31
# File 'lib/sox/command.rb', line 29

def inputs
  @inputs
end

#output(*arguments) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/sox/command.rb', line 43

def output(*arguments)
  if arguments.empty?
    @output
  else
    self.output=File.new(*arguments)
  end
end

#run_outputObject

Returns the value of attribute run_output.



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

def run_output
  @run_output
end

Instance Method Details

#command_argumentsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sox/command.rb', line 59

def command_arguments
  [].tap do |arguments|
    unless inputs.empty?
      if playlist.useful?
        arguments << playlist.create
      else
        arguments << inputs.collect(&:command_arguments)
      end
    else
      arguments << "-n"
    end

    unless output.blank?
      arguments << output.command_arguments
    else
      arguments << "-n"
    end

    arguments << effects.collect(&:command_arguments).join(' : ')
  end.flatten.delete_if(&:blank?).join(' ')
end

#command_lineObject



55
56
57
# File 'lib/sox/command.rb', line 55

def command_line
  Cocaine::CommandLine.new("sox", "#{command_arguments} 2>&1")
end

#effect(name, *arguments) ⇒ Object



51
52
53
# File 'lib/sox/command.rb', line 51

def effect(name, *arguments)
  effects << Effect.new(name, *arguments)
end

#input(filename, options = {}) ⇒ Object



39
40
41
# File 'lib/sox/command.rb', line 39

def input(filename, options = {})
  inputs << File.new(filename, options)
end

#playlistObject



81
82
83
# File 'lib/sox/command.rb', line 81

def playlist
  @playlist ||= Playlist.new(inputs)
end

#runObject



85
86
87
88
89
90
91
92
# File 'lib/sox/command.rb', line 85

def run
  begin
    run!
    true
  rescue Cocaine::CommandLineError => e
    false
  end
end

#run!Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sox/command.rb', line 94

def run!
  Sox.logger.debug "Run '#{command_line.command}'" if Sox.logger
  
  begin
    self.run_output = command_line.run
  ensure
    playlist.delete
  end

  self
end