Class: DTAS::RGState

Inherits:
Object
  • Object
show all
Includes:
Serialize
Defined in:
lib/dtas/rg_state.rb

Overview

:nodoc:

Constant Summary collapse

RG_MODE =
{
  # attribute name => method to use
  "album_gain" => :rg_vol_gain,
  "track_gain" => :rg_vol_gain,
  "album_peak" => :rg_vol_norm,
  "track_peak" => :rg_vol_norm,
}
RG_DEFAULT =
{
  # skip the effect if the adjustment is too small to be noticeable
  "gain_threshold" => 0.00000001, # in dB
  "norm_threshold" => 0.00000001,

  "preamp" => 0, # no extra adjustment
  # "mode" => "album_gain", # nil: off
  "mode" => nil, # nil: off
  "fallback_gain" => -6.0, # adjustment dB if necessary RG tag is missing
  "fallback_track" => true,
  "norm_level" => 1.0, # dBFS
}
SIVS =
RG_DEFAULT.keys

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialize

#ivars_to_hash

Constructor Details

#initializeRGState

Returns a new instance of RGState.



35
36
37
38
39
# File 'lib/dtas/rg_state.rb', line 35

def initialize
  RG_DEFAULT.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
end

Class Method Details

.load(hash) ⇒ Object



41
42
43
44
45
# File 'lib/dtas/rg_state.rb', line 41

def self.load(hash)
  rv = new
  hash.each { |k,v| rv.__send__("#{k}=", v) } if hash
  rv
end

Instance Method Details

#effect(source) ⇒ Object

returns an array (for command-line argument) for the effect needed to apply ReplayGain this may return nil



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dtas/rg_state.rb', line 86

def effect(source)
  return unless @mode
  rg = source.replaygain or
    return rg_fallback_effect("ReplayGain tags missing")
  val = rg.__send__(@mode)
  if ! val && @fallback_track && @mode =~ /\Aalbum_(\w+)/
    tag = "track_#$1"
    val = rg.__send__(tag) or
      return rg_fallback_effect("ReplayGain tag for #@mode missing")
    warn("tag for #@mode missing, using #{tag}")
  end
  # this may be nil if the adjustment is too small:
  __send__(RG_MODE[@mode], val)
end

#rg_fallback_effect(reason) ⇒ Object

The ReplayGain fallback adjustment value (in dB), in case a file is missing ReplayGain tags. This is useful to avoid damage to speakers, eardrums and amplifiers in case a file without then necessary ReplayGain tag slips into the queue



75
76
77
78
79
80
81
# File 'lib/dtas/rg_state.rb', line 75

def rg_fallback_effect(reason)
  @fallback_gain or return
  val = @fallback_gain + @preamp
  return if val.abs < @gain_threshold
  warn(reason) if $DEBUG
  "vol #{val}dB"
end

#rg_vol_gain(val) ⇒ Object

returns a dB argument to the “vol” effect, nil if nothing found



57
58
59
60
61
# File 'lib/dtas/rg_state.rb', line 57

def rg_vol_gain(val)
  val = val.to_f + @preamp
  return if val.abs < @gain_threshold
  sprintf('vol %0.8gdB', val)
end

#rg_vol_norm(val) ⇒ Object

returns a linear argument to the “vol” effect



64
65
66
67
68
69
# File 'lib/dtas/rg_state.rb', line 64

def rg_vol_norm(val)
  diff = @norm_level - val.to_f
  return if (@norm_level - diff).abs < @norm_threshold
  diff += @norm_level
  sprintf('vol %0.8g', diff)
end

#to_hashObject



47
48
49
# File 'lib/dtas/rg_state.rb', line 47

def to_hash
  ivars_to_hash(SIVS)
end

#to_hshObject



51
52
53
54
# File 'lib/dtas/rg_state.rb', line 51

def to_hsh
  # no point in dumping default values, it's just a waste of space
  to_hash.delete_if { |k,v| RG_DEFAULT[k] == v }
end