Class: Libis::Format::Converter::AudioConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/libis/format/converter/audio_converter.rb

Instance Attribute Summary

Attributes inherited from Base

#flags, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, using_temp, #using_temp

Constructor Details

#initializeAudioConverter

Returns a new instance of AudioConverter.



22
23
24
# File 'lib/libis/format/converter/audio_converter.rb', line 22

def initialize
  super
end

Class Method Details

.input_typesObject



13
14
15
# File 'lib/libis/format/converter/audio_converter.rb', line 13

def self.input_types
  [:MP3, :FLAC, :AC3, :AAC, :WMA, :ALAC, :WAV, :AIFF, :AMR, :AU, :M4A]
end

.output_types(format = nil) ⇒ Object



17
18
19
20
# File 'lib/libis/format/converter/audio_converter.rb', line 17

def self.output_types(format = nil)
  return [] unless input_types.include?(format)
  [:MP3, :FLAC, :AC3, :AAC, :WMA, :ALAC, :WAV, :AIFF, :AMR, :AU, :M4A]
end

Instance Method Details

#assemble_and_convert(sources, target) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/libis/format/converter/audio_converter.rb', line 104

def assemble_and_convert(sources, target)
  Tempfile.create(%w(list .txt)) do |f|
    sources.each {|src| f.puts src}
    opts[:global] ||= []
    opts[:global] += %w(-f concat)
    f.close
    target = convert_file(f.to_path, target)
  end
  target
end

#bit_rate(value) ⇒ Object



50
51
52
# File 'lib/libis/format/converter/audio_converter.rb', line 50

def bit_rate(value)
  @options[:bit_rate] = value.to_s
end

#channels(value) ⇒ Object



58
59
60
# File 'lib/libis/format/converter/audio_converter.rb', line 58

def channels(value)
  @options[:channels] = value.to_s
end

#codec(codec) ⇒ Object



34
35
36
# File 'lib/libis/format/converter/audio_converter.rb', line 34

def codec(codec)
  @options[:codec] = codec
end

#convert(source, target, _format, opts = {}) ⇒ Object

def encoder(value)

@options[:encoder] = value

end

def encoder_options(value)

@options[:encoder_options] = value

end



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/libis/format/converter/audio_converter.rb', line 80

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

  target

end

#duration(seconds) ⇒ Object



42
43
44
# File 'lib/libis/format/converter/audio_converter.rb', line 42

def duration(seconds)
  @options[:duration] = seconds.to_s
end

#format(format) ⇒ Object



30
31
32
# File 'lib/libis/format/converter/audio_converter.rb', line 30

def format(format)
  @options[:format] = format
end

#preset(stream, name) ⇒ Object



68
69
70
# File 'lib/libis/format/converter/audio_converter.rb', line 68

def preset(stream, name)
  (@options[:preset] ||= {})[stream] = name
end

#quality(value) ⇒ Object



46
47
48
# File 'lib/libis/format/converter/audio_converter.rb', line 46

def quality(value)
  @options[:quality] = value.to_s
end

#quiet(v) ⇒ Object



26
27
28
# File 'lib/libis/format/converter/audio_converter.rb', line 26

def quiet(v)
  @flags[:quiet] = !!v
end

#sampling_freq(value) ⇒ Object



54
55
56
# File 'lib/libis/format/converter/audio_converter.rb', line 54

def sampling_freq(value)
  @options[:sampling_freq] = value.to_s
end

#sounds_like(file1, file2, threshold, rate, channels) ⇒ Object



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
147
148
149
150
151
152
# File 'lib/libis/format/converter/audio_converter.rb', line 115

def sounds_like(file1, file2, threshold, rate, channels)
  rate ||= 96000
  channels ||= 2
  threshold ||= 0.85

  if File.exists?(file1) && File.exists?(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]
    %x"#{cvt_cmd % [file1, file1_raw, rate, channels]}"
    %x"#{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
  error "Error comparing sound file #{file1} and #{file2}: #{e.message} @ #{e.backtrace.first}"
  false
end

#start(seconds) ⇒ Object



38
39
40
# File 'lib/libis/format/converter/audio_converter.rb', line 38

def start(seconds)
  @options[:start] = seconds.to_s
end

#web_stream(value) ⇒ Object



62
63
64
65
66
# File 'lib/libis/format/converter/audio_converter.rb', line 62

def web_stream(value)
  if value
    @options[:codec] = 'mp3'
  end
end