Class: Bitrate

Inherits:
Object
  • Object
show all
Includes:
SanitizePlugin
Defined in:
lib/what_cd/sanitize_plugins/bitrate.rb

Instance Method Summary collapse

Constructor Details

#initializeBitrate

Returns a new instance of Bitrate.



10
11
12
13
14
15
16
17
18
# File 'lib/what_cd/sanitize_plugins/bitrate.rb', line 10

def initialize
  @log = Logging.logger[self]
  @log.appenders = Logging.appenders.stdout
  if $verbose
    @log.level = :debug
  else
    @log.level = :info
  end
end

Instance Method Details

#sanitize(context) ⇒ Object



20
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
52
53
54
55
56
57
58
59
60
61
# File 'lib/what_cd/sanitize_plugins/bitrate.rb', line 20

def sanitize(context)
  path = context[:path]
  files = Dir.chdir(path) { Dir["*.mp3"] }
  return if files.empty?

  bitrates = {}

  files.each do |file_name|
    file_path = path + file_name
    Mp3Info.open(file_path) do |mp3|
      hash = {:bitrate => mp3.bitrate, :vbr => mp3.vbr}
      bitrates[file_name] = hash
    end
  end

  # TODO: Improve this to print out the violating file name
  vbr_list = bitrates.values.map{|value| value[:vbr] }
  if vbr_list.uniq.count > 1
    raise BitrateError.new("Release has both constant and variable bitrate tracks!")
  else 
    vbr = vbr_list.first
  end

  bitrate_list = bitrates.values.map{|value| value[:bitrate] }
  # Remove duplicates
  bitrate_list = bitrate_list.uniq

  # If the only vbr value is false, this is CBR
  if not vbr and bitrate_list.uniq.count > 1
    raise BitrateError.new("Release tracks are constant bit rate but multiple bitrates found!")
  else
    bitrate = bitrate_list.first
  end

  if vbr
    @log.info "Variable bitrate detected"
  else
    @log.info "Constant bitrate detected at #{bitrate} kbps"
  end

  return context
end