Class: GoogleSpeech::Utility

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

Constant Summary collapse

SOX_ERROR_RE =
/error:/

Class Method Summary collapse

Class Method Details

.audio_file_duration(path) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/google_speech/utility.rb', line 14

def audio_file_duration(path)
  check_local_file(path)

  soxi_duration, err = run_command("soxi -V0 -D '#{path}'", :nice=>false, :echo_return=>false)
  duration = soxi_duration.chomp.to_f
  duration
end

.check_local_file(file_path) ⇒ Object



78
79
80
# File 'lib/google_speech/utility.rb', line 78

def check_local_file(file_path)
  raise "File missing or 0 length: #{file_path}" unless (File.size?(file_path).to_i > 0)
end

.loggerObject



82
83
84
# File 'lib/google_speech/utility.rb', line 82

def logger
  GoogleSpeech.logger        
end

.run_command(command, options = {}) ⇒ Object

Pass the command to run, and various options :timeout - seconds to wait for command to complete, defaults to 2 hours :echo_return - gets the return value via appended ‘; echo $?’, true by default :nice - call with nice -19 by default, set to false to stop, or integer to set different level



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/google_speech/utility.rb', line 36

def run_command(command, options={})
  timeout = options[:timeout] || 7200
  
  # default to adding a nice 19 if nothing specified
  nice = if options.key?(:nice)
    !options[:nice] ? '' : "nice -n #{options[:nice].to_i} "
  else
    'nice -n 19 '
  end
  
  echo_return = (options.key?(:echo_return) && !options[:echo_return]) ? '' : '; echo $?'
  
  cmd = "#{nice}#{command}#{echo_return}"
  
  # logger.info "google_speech - run_command: #{cmd}"
  begin
    result = Timeout::timeout(timeout) {
      Open3::popen3(cmd) do |i,o,e|
        out_str = ""
        err_str = ""
        i.close # important!
        o.sync = true
        e.sync = true
        o.each{|line|
          out_str << line
          line.chomp!
          # logger.debug "stdout:    #{line}"
        }
        e.each { |line| 
          err_str << line
          line.chomp!
          # logger.debug "stderr:    #{line}"
        }
        return out_str, err_str
      end
    }
  rescue Timeout::Error => toe
    # logger.debug "run_command:Timeout Error - running command, took longer than #{timeout} seconds to execute: '#{cmd}'"
    raise toe
  end
end

.trim_and_encode(wav_path, flac_path, start, length, rate) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/google_speech/utility.rb', line 22

def trim_and_encode(wav_path, flac_path, start, length, rate)
  check_local_file(wav_path)

  command = "sox -t wav '#{wav_path}' -r 8000 -c 1 -t flac '#{flac_path}' trim #{start} #{length} compand .5,2 -80,-80,-75,-50,-30,-15,0,0 norm -0.1"
  # command = "sox -t wav '#{wav_path}' -t wav '#{flac_path}' norm channels 1 rate #{rate} trim #{start} #{length} compand .5,2 -80,-80,-75,-50,-30,-15,0,0"
  out, err = run_command(command)
  response = out + err
  response.split("\n").each{ |l| raise("trim_and_encode: error cmd: '#{command}'\nout: '#{response}'") if l =~ SOX_ERROR_RE }
end