Class: MusicMaster::Processes::Normalize

Inherits:
Object
  • Object
show all
Defined in:
lib/MusicMaster/Processes/Normalize.rb

Instance Method Summary collapse

Instance Method Details

#execute(iInputFileName, iOutputFileName, iTempDir, iParams) ⇒ Object

Execute the process

Parameters
  • iInputFileName (String): File name we want to apply effects to

  • iOutputFileName (String): File name to write

  • iTempDir (String): Temporary directory that can be used

  • iParams (map<Symbol,Object>): Parameters



19
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
# File 'lib/MusicMaster/Processes/Normalize.rb', line 19

def execute(iInputFileName, iOutputFileName, iTempDir, iParams)
  require 'rational'
  # First, analyze
  lAnalyzeResultFileName = "#{iTempDir}/#{File.basename(iInputFileName)}.analyze"
  if (File.exists?(lAnalyzeResultFileName))
    log_warn "File #{lAnalyzeResultFileName} already exists. Will not overwrite it."
  else
    wsk(iInputFileName, "#{iTempDir}/Dummy.wav", 'Analyze')
    File.unlink("#{iTempDir}/Dummy.wav")
    FileUtils::mv('analyze.result', lAnalyzeResultFileName)
  end
  lAnalyzeResult = nil
  File.open(lAnalyzeResultFileName, 'rb') do |iFile|
    lAnalyzeResult = Marshal.load(iFile.read)
  end
  lMaxDataValue = lAnalyzeResult[:MaxValues].sort[-1]
  lMinDataValue = lAnalyzeResult[:MinValues].sort[0]
  lMaxPossibleValue = (2**(lAnalyzeResult[:SampleSize]-1)) - 1
  lMinPossibleValue = -(2**(lAnalyzeResult[:SampleSize]-1))
  lCoeffNormalizeMax = Rational(lMaxPossibleValue, lMaxDataValue)
  lCoeffNormalizeMin = Rational(lMinPossibleValue, lMinDataValue)
  lCoeff = lCoeffNormalizeMax
  if (lCoeffNormalizeMin < lCoeff)
    lCoeff = lCoeffNormalizeMin
  end
  log_info "Maximal value: #{lMaxDataValue}/#{lMaxPossibleValue}. Minimal value: #{lMinDataValue}/#{lMinPossibleValue}. Volume correction: #{lCoeff}."
  wsk(iInputFileName, iOutputFileName, 'Multiply', "--coeff \"#{lCoeff.numerator}/#{lCoeff.denominator}\"")
end