Class: Libis::Format::Converter::AudioConverter
- Defined in:
- lib/libis/format/converter/audio_converter.rb
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
- .input_types ⇒ Object
- .output_types(format = nil) ⇒ Object
- .sounds_like(file1, file2, threshold, rate, channels) ⇒ Object
Instance Method Summary collapse
- #assemble_and_convert(sources, target) ⇒ Object
- #bit_rate(value) ⇒ Object
- #channels(value) ⇒ Object
- #codec(codec) ⇒ Object
-
#convert(source, target, _format, opts = {}) ⇒ Object
def encoder(value) @options = value end.
- #duration(seconds) ⇒ Object
- #format(format) ⇒ Object
- #preset(stream, name) ⇒ Object
- #quality(value) ⇒ Object
- #quiet(value) ⇒ Object
- #sampling_freq(value) ⇒ Object
- #start(seconds) ⇒ Object
- #web_stream(value) ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from Libis::Format::Converter::Base
Class Method Details
.input_types ⇒ Object
13 14 15 |
# File 'lib/libis/format/converter/audio_converter.rb', line 13 def self.input_types %i[MP3 FLAC AC3 AAC WMA ALAC WAV AIFF AMR AU M4A] end |
.output_types(format = nil) ⇒ Object
17 18 19 20 21 |
# File 'lib/libis/format/converter/audio_converter.rb', line 17 def self.output_types(format = nil) return [] unless input_types.include?(format) %i[MP3 FLAC AC3 AAC WMA ALAC WAV AIFF AMR AU M4A] end |
.sounds_like(file1, file2, threshold, rate, channels) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/libis/format/converter/audio_converter.rb', line 110 def self.sounds_like(file1, file2, threshold, rate, channels) rate ||= 96_000 channels ||= 2 threshold ||= 0.85 if File.exist?(file1) && File.exist?(file2) # Convert input files into raw 16-bit signed audio (WAV) to process with Chramaprint file1_raw = File.join('', 'tmp', "#{File.basename(file1)}.wav") file2_raw = File.join('', 'tmp', "#{File.basename(file2)}.wav") FileUtils.rm(file1_raw, force: true) FileUtils.rm(file2_raw, force: true) cvt_cmd = Libis::Format::Config[:raw_audio_convert_cmd] `#{format(cvt_cmd, file1, file1_raw, rate, channels)}` `#{format(cvt_cmd, file2, file2_raw, rate, channels)}` file1_audio = File.binread(file1_raw) file2_audio = File.binread(file2_raw) # Get audio fingerprints chromaprint = Chromaprint::Context.new(rate, channels, Chromaprint::ALGORITHM_TEST3) file1_fp = chromaprint.get_fingerprint(file1_audio) file2_fp = chromaprint.get_fingerprint(file2_audio) # Cleanup files FileUtils.rm(file1_raw, force: true) FileUtils.rm(file2_raw, force: true) # Compare fingerprints and compare result against threshold cmp = file1_fp.compare(file2_fp) # puts "Threshold[#{File.basename(exp_file)},#{File.basename(tgt_file)}: #{cmp}" cmp > threshold else false end rescue Exception => e puts "Error comparing sound file #{file1} and #{file2}: #{e.} @ #{e.backtrace.first}" false end |
Instance Method Details
#assemble_and_convert(sources, target) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/libis/format/converter/audio_converter.rb', line 98 def assemble_and_convert(sources, target) result = {} Tempfile.create(%w[list .txt]) do |f| sources.each { |src| f.puts src } opts[:global] ||= [] opts[:global] += %w[-f concat] f.close result = convert_file(f.to_path, target) end result end |
#bit_rate(value) ⇒ Object
47 48 49 |
# File 'lib/libis/format/converter/audio_converter.rb', line 47 def bit_rate(value) @options[:bit_rate] = value.to_s end |
#channels(value) ⇒ Object
55 56 57 |
# File 'lib/libis/format/converter/audio_converter.rb', line 55 def channels(value) @options[:channels] = value.to_s end |
#codec(codec) ⇒ Object
31 32 33 |
# File 'lib/libis/format/converter/audio_converter.rb', line 31 def codec(codec) @options[:codec] = codec end |
#convert(source, target, _format, opts = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/libis/format/converter/audio_converter.rb', line 77 def convert(source, target, _format, opts = {}) super FileUtils.mkpath(File.dirname(target)) if source.is_a? Array assemble_and_convert(source, target) elsif File.directory?(source) sources = Dir[File.join(source, '**', '*')].reject { |p| File.directory? p } assemble_and_convert(sources, target) else convert_file(source, target) end end |
#duration(seconds) ⇒ Object
39 40 41 |
# File 'lib/libis/format/converter/audio_converter.rb', line 39 def duration(seconds) @options[:duration] = seconds.to_s end |
#format(format) ⇒ Object
27 28 29 |
# File 'lib/libis/format/converter/audio_converter.rb', line 27 def format(format) @options[:format] = format end |
#preset(stream, name) ⇒ Object
65 66 67 |
# File 'lib/libis/format/converter/audio_converter.rb', line 65 def preset(stream, name) (@options[:preset] ||= {})[stream] = name end |
#quality(value) ⇒ Object
43 44 45 |
# File 'lib/libis/format/converter/audio_converter.rb', line 43 def quality(value) @options[:quality] = value.to_s end |
#quiet(value) ⇒ Object
23 24 25 |
# File 'lib/libis/format/converter/audio_converter.rb', line 23 def quiet(value) @flags[:quiet] = !!value end |
#sampling_freq(value) ⇒ Object
51 52 53 |
# File 'lib/libis/format/converter/audio_converter.rb', line 51 def sampling_freq(value) @options[:sampling_freq] = value.to_s end |
#start(seconds) ⇒ Object
35 36 37 |
# File 'lib/libis/format/converter/audio_converter.rb', line 35 def start(seconds) @options[:start] = seconds.to_s end |
#web_stream(value) ⇒ Object
59 60 61 62 63 |
# File 'lib/libis/format/converter/audio_converter.rb', line 59 def web_stream(value) return unless value @options[:codec] = 'mp3' end |