Class: Lossfully::AudioFile
- Inherits:
-
Object
- Object
- Lossfully::AudioFile
- Defined in:
- lib/lossfully/audio_file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.bitrate_kbps(path) ⇒ Object
Return the bitrate of the file as an integer in kbps (‘soxi -B’).
- .def_soxi(method, options = '') ⇒ Object
-
.delegate_and_memoize(method, class_method = nil) ⇒ Object
This could be done with method_missing.
-
.duration(path) ⇒ Object
(also: length)
Return the duration of the file in seconds as a Float (‘soxi -D’).
- .encode(input_path, output_path, options = '', effect_options = '') ⇒ Object
- .soxi_command(path, options = '') ⇒ Object
-
.type(path) ⇒ Object
(also: is_audio?)
Return the file-type as a symbol (‘soxi -t’).
Instance Method Summary collapse
- #encode(output_path, options = '', effect_options = '') ⇒ Object
-
#initialize(path) ⇒ AudioFile
constructor
A new instance of AudioFile.
Constructor Details
#initialize(path) ⇒ AudioFile
Returns a new instance of AudioFile.
99 100 101 102 103 104 105 106 107 |
# File 'lib/lossfully/audio_file.rb', line 99 def initialize path @path = path # Actually, let's not do this. This gets checked every time a # method is run anyway, so this way we can just use the class as # a wrapper around a path string for files that aren't audio. # # raise "Not recognized as an audio file: #{file}" unless is_audio? end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
109 110 111 |
# File 'lib/lossfully/audio_file.rb', line 109 def path @path end |
Class Method Details
.bitrate_kbps(path) ⇒ Object
Return the bitrate of the file as an integer in kbps (‘soxi -B’). If soxi does not recognize the file as audio, return nil.
75 76 77 78 |
# File 'lib/lossfully/audio_file.rb', line 75 def self.bitrate_kbps path b = bitrate(path) return b.to_f * (b[-1..-1] == 'k' ? 1 : 1000) end |
.def_soxi(method, options = '') ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/lossfully/audio_file.rb', line 45 def self.def_soxi(method, ='') class_eval <<-EOM def self.#{method} path self.soxi_command path, '#{}' end EOM end |
.delegate_and_memoize(method, class_method = nil) ⇒ Object
This could be done with method_missing.
112 113 114 115 116 117 118 119 |
# File 'lib/lossfully/audio_file.rb', line 112 def self.delegate_and_memoize(method, class_method=nil) class_method ||= method class_eval <<-EOM def #{method} @#{method} ||= self.class.#{class_method}(@path) end EOM end |
.duration(path) ⇒ Object Also known as: length
Return the duration of the file in seconds as a Float (‘soxi -D’). If soxi does not recognize the file as audio, return nil.
84 85 86 87 |
# File 'lib/lossfully/audio_file.rb', line 84 def self.duration path #(soxi_command path, '-D').to_f `soxi -V0 -D \"#{path}\"`.chomp.to_f end |
.encode(input_path, output_path, options = '', effect_options = '') ⇒ Object
94 95 96 97 |
# File 'lib/lossfully/audio_file.rb', line 94 def self.encode input_path, output_path, ='', ='' FileUtils.mkdir_p(File.dirname(output_path)) unless File.directory? output_path system("sox \"#{input_path}\" #{} \"#{output_path}\" #{}") end |
.soxi_command(path, options = '') ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/lossfully/audio_file.rb', line 36 def self.soxi_command path, ='' check_file path # system("soxi -V0 #{path}") return nil if File.extname(path) == '.m3u' p = IO.popen("sox --info -V0 #{} \"#{path}\"") return ((Process.wait2 p.pid)[1] == 0) ? p.gets.chomp : nil end |
.type(path) ⇒ Object Also known as: is_audio?
Return the file-type as a symbol (‘soxi -t’). If soxi does not recognize the file as audio, return nil.
61 62 63 64 |
# File 'lib/lossfully/audio_file.rb', line 61 def self.type path t = soxi_command path, '-t' return t ? t.to_sym : nil end |
Instance Method Details
#encode(output_path, options = '', effect_options = '') ⇒ Object
121 122 123 |
# File 'lib/lossfully/audio_file.rb', line 121 def encode output_path, ='', ='' self.class.encode @path, output_path, , end |