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
-
#trim(options = {}) ⇒ Object
Cuts portions out of the audio.
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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/audio_hero.rb', line 193 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/audio_hero.rb', line 115 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" = [:output_options] ? [:output_options] : "-c 1 -b 16 -r 16k" 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/audio_hero.rb', line 178 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
83 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 |
# File 'lib/audio_hero.rb', line 83 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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/audio_hero.rb', line 160 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 |
#trim(options = {}) ⇒ Object
Cuts portions out of the audio. Any number of positions may be given Usage: file = AudioHero::Sox.new(file).trim(“=10 =20 =30 =40”) trim_positions “=10 =20 =30 =40” means retrieving audio from 10s-20s and from 30s-40s, and joining them into one file. See sox trim effect for more examples Default output to 16bit 16 sample rate Wave audio
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/audio_hero.rb', line 136 def trim(={}) raise AudioHeroError, "Trim parameters not given" unless [:trim_positions] input_format = [:input_format] ? [:input_format] : "mp3" output_format = [:output_format] ? [:output_format] : "wav" # Default to wav = [:output_options] ? [:output_options] : "-c 1 -b 16 -r 16k" trim_positions = [:trim_positions] effect = "trim #{trim_positions}" dst = Tempfile.new(["out", ".#{output_format}"]) begin parameters = [] parameters << "-t #{input_format}" parameters << ":source" parameters << if 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 trimming #{@basename} using positions #{trim_positions}" end garbage_collect(@file) if [:gc] == "true" dst end |