Class: JOCLoudness

Inherits:
Object
  • Object
show all
Defined in:
lib/JOCLoudness.rb

Overview

JOCLoudness class, used to compute the loudness of a wav file (Based on ITU-R BS.1770)

Instance Method Summary collapse

Constructor Details

#initialize(filename, logfile = nil, loglevel = Logger::INFO) ⇒ JOCLoudness

Initialize JOCLoudness

Parameters:

  • filename (String)

    audio wav file to analize

  • logfile (String) (defaults to: nil)

    log file to save the logs (optional)

  • loglevel (defaults to: Logger::INFO)

    log level, could be Logger::FATAL, Logger::ERROR, Logger::WARN, Logger::INFO, Logger::DEBUG (optional)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/JOCLoudness.rb', line 19

def initialize (filename, logfile = nil, loglevel = Logger::INFO)
  #Instanciate wav reader
  @wav = Wavfile.new(filename)

  #Start logger
  # Keep data for today and the past 3 days.
  @log = nil
  if (logfile != nil)
    @log = Logger.new(logfile)  
    @log.level = loglevel

    #Set logger (optional)
    @wav.Setlogger(@log)      
  end  

  #Read wav header
  @wav.ReadHeader()

  #Get file info (fs, number of channels)
  @fs, @nch = @wav.GetFileInfo()
end

Instance Method Details

#CalcLoudnessFloat

Calculates the loudness of the audio file

Returns:

  • (Float)

    the resulting loudness in LKFS



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/JOCLoudness.rb', line 43

def CalcLoudness ()
  #Instantiate & initialize loudness moudule
  loudness = JOCLoud.new
  
  #Set logger
  if (@log != nil)
    loudness.Setlogger(@log)
  end

  loudness.ini(@fs,@nch) 

  #Read wav file and send the samples to loudness module
  n = 0
  samples = 1

  while samples != nil
    samples = @wav.GetAudioSamples(n)
    if (samples != nil) 
      if (n % @fs == 0)
        puts "Processed #{n / @fs} secs"          
      end

      loudness.Addsample(samples)
  
      n = n + 1
    end
  end 

  loudnessLKFS = loudness.GetLoudnessLKFS()
  
  if (@log != nil)
    strloudnessdata = "Loudness = #{loudnessLKFS.round(1)} LKFS" 
    @log.debug(strloudnessdata)  
  end
  
  return loudnessLKFS 
end

#CloseObject

Closes the opened file



82
83
84
85
86
87
88
89
90
# File 'lib/JOCLoudness.rb', line 82

def Close()
  if (@log!=nil)
    @log.debug("End logger")
  end
  
  if (@wav != nil)
    @wav.Close()
  end
end