Class: MeanFast

Inherits:
Object
  • Object
show all
Defined in:
lib/Loudness/Mean/MeanFast.rb

Overview

Conputes mean

Defined Under Namespace

Modules: MEAN_MODE

Instance Method Summary collapse

Constructor Details

#initializeMeanFast

Returns a new instance of MeanFast.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/Loudness/Mean/MeanFast.rb', line 14

def initialize
  @bIni = false
  @mode = MEAN_MODE::MEAN_TOTAL

  #Vars used in Overlapped mode
  @lSamplesInInterval = 0
  @lSamplesOverlapped = 0
  @lGetMeanEveryXSamples = 0
  @nSums = 0
  @Sums  = 0
  @lNumSamplesLoaded = 0
  @nSumIndex = 0    
  @queueMean = Array.new
  
  #Vars used in TOTAL mode
  @lNumAcumulatedSamplesMean = 0
  @dAccumulatedMean = 0.0
  
  #if @mode == MEAN_MODE::MEAN_TOTAL the fs, lMeanIntervalms, dOverlappingFactor does not take effect
  Ini(@mode, 48000, 400, 0.75)
end

Instance Method Details

#AddSample(dSample) ⇒ Object

Adds a new sample to mean computation

Parameters:

  • dSample (Float)

    new sample



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/Loudness/Mean/MeanFast.rb', line 77

def AddSample (dSample)
  rc = false
  
  if (@mode == MEAN_MODE::MEAN_OVERLAPPED)
    ni = 0     
    while ni < @nSums do
      @Sums[ni] = @Sums[ni].to_f + (dSample.to_f * dSample.to_f)
      
      ni = ni + 1
    end 
    
    @lNumSamplesLoaded = @lNumSamplesLoaded + 1

    if ((@lNumSamplesLoaded % @lGetMeanEveryXSamples) == 0)
      dMean = @Sums[@nSumIndex].to_f / @lSamplesInInterval.to_f
      
      @queueMean.insert(-1, dMean) #Insert at loast position
      
      rc = true
      @Sums[@nSumIndex] = 0.0

      @nSumIndex = @nSumIndex + 1        
      if (@nSumIndex >= @nSums)
        @nSumIndex = 0
      end
    end
    
    if (@lNumSamplesLoaded >= @lSamplesInInterval)
      @lNumSamplesLoaded = 0
    end        
  else
    dAddVal = dSample.to_f * dSample.to_f

    if (@lNumAcumulatedSamplesMean > 0)
      dMul = @lNumAcumulatedSamplesMean.to_f / (@lNumAcumulatedSamplesMean + 1).to_f
      @dAccumulatedMean = (@dAccumulatedMean.to_f * dMul.to_f) + dAddVal.to_f / (@lNumAcumulatedSamplesMean + 1).to_f
    else
      @dAccumulatedMean = dAddVal
    end
    @lNumAcumulatedSamplesMean = @lNumAcumulatedSamplesMean + 1

    rc = true      
  end
  
  return rc
end

#GetMeanValFloat

Adds a new sample to mean computation

Returns:

  • (Float)

    the resulting mean value



126
127
128
129
130
131
132
# File 'lib/Loudness/Mean/MeanFast.rb', line 126

def GetMeanVal
  if (@mode == MEAN_MODE::MEAN_OVERLAPPED)
    raise 'ERROR function not valid in overlapped mean mode'
  end
  
  return @dAccumulatedMean
end

#Ini(mode, fs = 0, lMeanIntervalms = 0, dOverlappingFactor = 0) ⇒ Object

Initializes the mean computation, and it sets the mean mode

Parameters:

  • mode

    Must be = MEAN_TOTAL

  • fs (int) (defaults to: 0)

    not used in this version

  • lMeanIntervalms (int) (defaults to: 0)

    not used in this version

  • dOverlappingFactor (Float) (defaults to: 0)

    not used in this version



41
42
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
# File 'lib/Loudness/Mean/MeanFast.rb', line 41

def Ini(mode, fs = 0, lMeanIntervalms = 0, dOverlappingFactor = 0)    
  
  if (@bIni == true)
    raise 'ERROR bad state'
  end
  
  if (fs <= 0) or (lMeanIntervalms <= 0) or (dOverlappingFactor <= 0.0) or (dOverlappingFactor >= 1.0)        
    raise 'ERROR bad arguments'
  end
  
  @mode = mode
  
  if (@mode == MEAN_MODE::MEAN_OVERLAPPED)
    @lSamplesInInterval = Integer(fs.to_f * (lMeanIntervalms.to_f / 1000.0))
    @lSamplesOverlapped = Integer(@lSamplesInInterval.to_f * dOverlappingFactor.to_f)
    @lGetMeanEveryXSamples = @lSamplesInInterval - @lSamplesOverlapped;

    if (@lGetMeanEveryXSamples <= 0)
      raise 'ERROR calculating lGetMeanEveryXSamples (bad arguments)'
    end
    
    @nSums = @lSamplesInInterval / @lGetMeanEveryXSamples; 
    @Sums = Array.new(@nSums) {|i| i = 0}

    @lNumSamplesLoaded = 0;
    @nSumIndex = 0;  
  else  
    @lNumAcumulatedSamplesMean = 0;
    @dAccumulatedMean = 0.0;
  end

  @bIni = true;
end