Class: AudioHero::Sox
- Inherits:
-
Object
- Object
- AudioHero::Sox
- Defined in:
- lib/audio_hero.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#output_format ⇒ Object
Returns the value of attribute output_format.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #command(options = {}) ⇒ Object
-
#concat(options = {}) ⇒ Object
Concat takes in an array of audio files (same format) and concatenate them.
-
#convert(options = {}) ⇒ Object
Usage: file = AudioHero::Sox.new(file).convert(“-c 1 -b 16 -r 16k”, output_format: “mp3”, channel: “left”); file.close.
-
#extract_features(options = {}) ⇒ Object
Requires custom version of yaafe.
-
#initialize(file, options = {}) ⇒ Sox
constructor
A new instance of Sox.
-
#remove_silence(options = {}) ⇒ Object
Usage: file = AudioHero::Sox.new(file).remove_silence.
-
#split_by_silence(options = {}) ⇒ Object
Usage: AudioHero::Sox.new(file).split_by_silence(“wav”) Returns an array of the full path of the splitted files, can split two input files at one go using file option.
- #stats(options = {}) ⇒ Object
Constructor Details
#initialize(file, options = {}) ⇒ Sox
Returns a new instance of Sox.
15 16 17 18 |
# File 'lib/audio_hero.rb', line 15 def initialize(file, ={}) @file = file @basename = get_basename(file) end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
13 14 15 |
# File 'lib/audio_hero.rb', line 13 def file @file end |
#output_format ⇒ Object
Returns the value of attribute output_format.
13 14 15 |
# File 'lib/audio_hero.rb', line 13 def output_format @output_format end |
#params ⇒ Object
Returns the value of attribute params.
13 14 15 |
# File 'lib/audio_hero.rb', line 13 def params @params end |
Instance Method Details
#command(options = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/audio_hero.rb', line 165 def command(={}) global = [:global_options] = [:input_options] = [:output_options] effect = [:effect] output_format = [:output_format] ? [:output_format] : "wav" # Default to wav dst = Tempfile.new(["out", ".#{output_format}"]) begin parameters = [] parameters << global if global parameters << if parameters << ":source" parameters << if parameters << ":dest" parameters << effect if effect parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") sox = Cocaine::CommandLine.new("sox", parameters) command = sox.command(:source => get_path(@file), :dest => get_path(dst)) success = sox.run(:source => get_path(@file), :dest => get_path(dst)) rescue => e raise AudioHeroError, "There was an error excuting command: #{command}" end garbage_collect(@file) if [:gc] == "true" dst end |
#concat(options = {}) ⇒ Object
Concat takes in an array of audio files (same format) and concatenate them.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/audio_hero.rb', line 116 def concat(={}) output_format = [:output_format] ? [:output_format] : "wav" dst = Tempfile.new(["out", ".#{output_format}"]) files = get_array(@file) begin parameters = files.dup parameters << ":dest" parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = Cocaine::CommandLine.new("sox", parameters).run(:dest => get_path(dst)) rescue => e raise AudioHeroError, "There was an error joining #{@file}" end # no garbage collect option for join dst end |
#convert(options = {}) ⇒ Object
Usage: file = AudioHero::Sox.new(file).convert(“-c 1 -b 16 -r 16k”, output_format: “mp3”, channel: “left”); file.close
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/audio_hero.rb', line 21 def convert(={}) channel = [:channel] input_format = [:input_format] ? [:input_format] : "mp3" output_format = [:output_format] ? [:output_format] : "wav" = [:default] ? "-c 1 -b 16 -r 16k" : [:output_options] case channel when "left" channel = "remix 1" when "right" channel = "remix 2" else channel = nil end # Default conversion to wav dst = Tempfile.new(["out", ".#{output_format}"]) begin parameters = [] parameters << "-t #{input_format}" parameters << ":source" parameters << if parameters << ":dest" parameters << channel if channel parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = Cocaine::CommandLine.new("sox", parameters).run(:source => get_path(@file), :dest => get_path(dst)) rescue => e raise AudioHeroError, "There was an error converting #{@basename} to #{output_format}" end garbage_collect(@file) if [:gc] == "true" dst end |
#extract_features(options = {}) ⇒ Object
Requires custom version of yaafe
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/audio_hero.rb', line 150 def extract_features(={}) rate = [:sample_rate] || "8000" begin parameters = [] parameters << "-r #{rate}" parameters << ":source" parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = Cocaine::CommandLine.new("yaafehero", parameters).run(:source => get_path(@file)) rescue => e raise AudioHeroError, "These was an issue getting stats from #{@basename}" end garbage_collect(@file) if [:gc] == "true" MessagePack.unpack(success) end |
#remove_silence(options = {}) ⇒ Object
Usage: file = AudioHero::Sox.new(file).remove_silence
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/audio_hero.rb', line 54 def remove_silence(={}) above_period_duration = [:above_period_duration] || "0.1" above_period_threshold = [:above_period_threshold] || "0.03" below_period_duration = [:below_period_duration] || "0.1" below_period_threshold = [:below_period_threshold] || "0.03" effect = "silence 1 #{above_period_duration} #{above_period_threshold}% -1 #{below_period_threshold} #{below_period_threshold}%" input_format = [:input_format] ? [:input_format] : "mp3" output_format = [:output_format] ? [:output_format] : "wav" # Default to wav dst = Tempfile.new(["out", ".#{output_format}"]) begin parameters = [] parameters << "-t #{input_format}" parameters << ":source" parameters << ":dest" parameters << effect parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = Cocaine::CommandLine.new("sox", parameters).run(:source => get_path(@file), :dest => get_path(dst)) rescue => e raise AudioHeroError, "There was an error converting #{@basename} to #{output_format}" end garbage_collect(@file) if [:gc] == "true" dst end |
#split_by_silence(options = {}) ⇒ Object
Usage: AudioHero::Sox.new(file).split_by_silence(“wav”) Returns an array of the full path of the splitted files, can split two input files at one go using file option. Remember its good practice to remove the temp directory after use. FileUtils.remove_entry tempdir
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/audio_hero.rb', line 84 def split_by_silence(={}) above_period_duration = [:above_period_duration] || "0.5" above_period_threshold = [:above_period_threshold] || "0.05" below_period_duration = [:below_period_duration] || "1.0" below_period_threshold = [:below_period_threshold] || "0.02" effect = "silence 1 #{above_period_duration} #{above_period_threshold}% 1 #{below_period_duration} #{below_period_threshold}% : newfile : restart" input_format = [:input_format] ? [:input_format] : "mp3" output_format = [:output_format] output_filename = [:output_filename] || "out" # Default to wav dir = Dir.mktmpdir format = output_format ? ".#{output_format}" : ".wav" dst = "#{dir}/#{output_filename}#{format}" begin parameters = [] parameters << "-t #{input_format}" parameters << ":source" parameters << ":dest" parameters << effect parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = Cocaine::CommandLine.new("sox", parameters).run(:source => get_path(@file), :dest => dst) rescue => e raise AudioHeroError, "There was an error splitting #{@basename}" end garbage_collect(@file) if [:gc] == "true" Dir["#{dir}/**/*#{format}"] end |
#stats(options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/audio_hero.rb', line 132 def stats(={}) input_format = [:input_format] ? [:input_format] : "mp3" begin parameters = [] parameters << "-t #{input_format}" parameters << ":source" parameters << "-n stats" parameters << "2>&1" # redirect stderr to stdout parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = Cocaine::CommandLine.new("sox", parameters).run(:source => get_path(@file)) rescue => e raise AudioHeroError, "These was an issue getting stats from #{@basename}" end garbage_collect(@file) if [:gc] == "true" parse_stats(success) end |