Class: Attached::Processor::Audio

Inherits:
Base
  • Object
show all
Defined in:
lib/attached/processor/audio.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#file, #options

Instance Method Summary collapse

Methods inherited from Base

process

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Audio

Create a processor.

Parameters:

  • file - The file to be processed.

  • options - The options to be applied to the processing.

  • attachment - The attachment the processor is being run for.



21
22
23
24
25
26
27
# File 'lib/attached/processor/audio.rb', line 21

def initialize(file, options = {}, attachment = nil)
  super
  @path      = self.file.path
  @preset    = options[:preset]
  @extension = options[:extension]
  @extension ||= self.attachment.extension
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



11
12
13
# File 'lib/attached/processor/audio.rb', line 11

def attachment
  @attachment
end

#extensionObject (readonly)

Returns the value of attribute extension.



9
10
11
# File 'lib/attached/processor/audio.rb', line 9

def extension
  @extension
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/attached/processor/audio.rb', line 8

def path
  @path
end

#presetObject (readonly)

Returns the value of attribute preset.



10
11
12
# File 'lib/attached/processor/audio.rb', line 10

def preset
  @preset
end

Instance Method Details

#processObject

Helper function for calling processors.

Usage:

self.process


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/attached/processor/audio.rb', line 41

def process
  result = Tempfile.new(["", self.extension])
  result.binmode
  begin
    parameters = []
    parameters << "--preset #{self.preset}" if self.preset
    parameters << self.path
    parameters << result.path
    parameters = parameters.join(" ").squeeze(" ")
    `lame #{parameters} #{redirect}`
    raise Errno::ENOENT if $?.exitstatus == 127
  rescue Errno::ENOENT
    raise "command 'lame' not found: ensure LAME is installed"
  end
  unless $?.exitstatus == 0
    raise Attached::Processor::Error, "must be an audio file"
  end
  return result
end

#redirectObject

Redirect output path.



31
32
33
# File 'lib/attached/processor/audio.rb', line 31

def redirect
  ">/dev/null 2>&1" if File.exist?("/dev/null")
end