Class: AudioHero::Sox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @file = file
  @basename = get_basename(file)
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/audio_hero.rb', line 13

def file
  @file
end

#output_formatObject

Returns the value of attribute output_format.



13
14
15
# File 'lib/audio_hero.rb', line 13

def output_format
  @output_format
end

#paramsObject

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(options={})
  global = options[:global_options]
  input_options = options[:input_options]
  output_options = options[:output_options]
  effect = options[:effect]
  output_format = options[:output_format] ? options[:output_format] : "wav"

  # Default to wav
  dst = Tempfile.new(["out", ".#{output_format}"])
  begin
    parameters = []
    parameters << global if global
    parameters << input_options if input_options
    parameters << ":source"
    parameters << output_options if output_options
    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 options[: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(options={})
  output_format = options[:output_format] ? options[: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(options={})
  channel = options[:channel]
  input_format = options[:input_format] ? options[:input_format] : "mp3"
  output_format = options[:output_format] ? options[:output_format] : "wav"
  output_options = options[:output_options] ? 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 << output_options if output_options
    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 options[: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(options={})
  rate = options[: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 options[: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(options={})
  above_period_duration = options[:above_period_duration] || "0.1"
  above_period_threshold = options[:above_period_threshold] || "0.03"
  below_period_duration = options[:below_period_duration] || "0.1"
  below_period_threshold = options[:below_period_threshold] || "0.03"
  effect = "silence 1 #{above_period_duration} #{above_period_threshold}% -1 #{below_period_threshold} #{below_period_threshold}%"
  input_format = options[:input_format] ? options[:input_format] : "mp3"
  output_format = options[:output_format] ? options[: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 options[: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(options={})
  above_period_duration = options[:above_period_duration] || "0.5"
  above_period_threshold = options[:above_period_threshold] || "0.05"
  below_period_duration = options[:below_period_duration] || "1.0"
  below_period_threshold = options[: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 = options[:input_format] ? options[:input_format] : "mp3"
  output_format = options[:output_format]
  output_filename = options[: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 options[: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(options={})
  input_format = options[:input_format] ? options[: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 options[: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

Raises:



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(options={})
  raise AudioHeroError, "Trim parameters not given" unless options[:trim_positions]
  input_format = options[:input_format] ? options[:input_format] : "mp3"
  output_format = options[:output_format] ? options[:output_format] : "wav" # Default to wav
  output_options = options[:output_options] ? options[:output_options] : "-c 1 -b 16 -r 16k"
  trim_positions = options[:trim_positions]
  effect = "trim #{trim_positions}"
  dst = Tempfile.new(["out", ".#{output_format}"])
  begin
    parameters = []
    parameters << "-t #{input_format}"
    parameters << ":source"
    parameters << output_options if output_options
    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 options[:gc] == "true"
  dst
end