Class: WSK::Actions::Compare

Inherits:
Object
  • Object
show all
Includes:
Common, Maps
Defined in:
lib/WSK/Actions/Compare.rb

Instance Method Summary collapse

Methods included from Maps

#apply_map_functions

Methods included from Common

#accessInputWaveFile, #accessOutputWaveFile, #getWaveFileAccesses, #parsePlugins, #readDuration, #readFFTProfile, #readThresholds, #val2db

Instance Method Details

#execute(iInputData, oOutputData) ⇒ Object

Execute

Parameters
  • iInputData (WSK::Model::InputData): The input data

  • oOutputData (Object): The output data to fill

Return
  • Exception: An error, or nil if success



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/WSK/Actions/Compare.rb', line 59

def execute(iInputData, oOutputData)
  @NbrBitsPerSample = iInputData.Header.NbrBitsPerSample
  @NbrChannels = iInputData.Header.NbrChannels
  if (@GenMap == 1)
    # We want to generate the map
    @DistortionMap = [nil]*(2**@NbrBitsPerSample)
  else
    @DistortionMap = nil
  end
  # Measure the cumulative errors
  @CumulativeErrors = 0
  @MaxValue = 2**(@NbrBitsPerSample-1)-1
  @MinValue = -2**(@NbrBitsPerSample-1)
  # Get the second input file
  rError = accessInputWaveFile(@InputFileName2) do |iInputHeader2, iInputData2|
    # Loop on both files.
    # !!! We count on the same buffer size for both files.
    require 'WSK/ArithmUtils/ArithmUtils'
    @ArithmUtils = WSK::ArithmUtils::ArithmUtils.new
    # Initialize buffers
    lNbrSamplesProcessed = 0
    iInputData.each_raw_buffer do |iRawBuffer, iNbrSamples, iNbrChannels|
      break
    end
    lRawBuffer1, lNbrSamples1, lNbrChannels = iInputData.get_current_raw_buffer
    iInputData2.each_raw_buffer do |iRawBuffer, iNbrSamples, iNbrChannels|
      break
    end
    lRawBuffer2, lNbrSamples2, lNbrChannels = iInputData2.get_current_raw_buffer
    while ((lRawBuffer1 != nil) or
           (lRawBuffer2 != nil))
      if (lRawBuffer1 == nil)
        oOutputData.pushRawBuffer(lRawBuffer2)
        lNbrSamplesProcessed += lNbrSamples2
        if (lNbrSamplesProcessed == @TotalNbrSamples)
          lRawBuffer2 = nil
        end
      elsif (lRawBuffer2 == nil)
        computeInverseMap
        oOutputData.pushRawBuffer(@ArithmUtils.applyMap(@InverseMap, lRawBuffer1, @NbrBitsPerSample, lNbrSamples1))
        lNbrSamplesProcessed += lNbrSamples1
        if (lNbrSamplesProcessed == @TotalNbrSamples)
          lRawBuffer1 = nil
        end
      elsif (lNbrSamples1 == lNbrSamples2)
        lOutputBuffer, lCumulativeErrors = @ArithmUtils.compareBuffers(
          lRawBuffer1,
          lRawBuffer2,
          @NbrBitsPerSample,
          @NbrChannels,
          lNbrSamples1,
          @Coeff,
          @DistortionMap
        )
        oOutputData.pushRawBuffer(lOutputBuffer)
        @CumulativeErrors += lCumulativeErrors
        lNbrSamplesProcessed += lNbrSamples1
        if (lNbrSamplesProcessed == @TotalNbrSamples)
          lRawBuffer1 = nil
          lRawBuffer2 = nil
        end
      elsif (lNbrSamples1 > lNbrSamples2)
        lOutputBuffer, lCumulativeErrors = @ArithmUtils.compareBuffers(
          lRawBuffer1[0..lRawBuffer2.size-1],
          lRawBuffer2,
          @NbrBitsPerSample,
          @NbrChannels,
          lNbrSamples1,
          @Coeff,
          @DistortionMap
        )
        oOutputData.pushRawBuffer(lOutputBuffer)
        @CumulativeErrors += lCumulativeErrors
        # Write remaining buffer (-Buffer1)
        computeInverseMap
        oOutputData.pushRawBuffer(@ArithmUtils.applyMap(@InverseMap, lRawBuffer1[lRawBuffer2.size..-1], @NbrBitsPerSample, lNbrSamples2 - lNbrSamples1))
        # Buffer2 is finished
        lRawBuffer2 = nil
        lNbrSamplesProcessed += lNbrSamples1
      else
        lOutputBuffer, lCumulativeErrors = @ArithmUtils.compareBuffers(
          lRawBuffer1,
          lRawBuffer2[0..lRawBuffer1.size-1],
          @NbrBitsPerSample,
          @NbrChannels,
          lNbrSamples1,
          @Coeff,
          @DistortionMap
        )
        oOutputData.pushRawBuffer(lOutputBuffer)
        @CumulativeErrors += lCumulativeErrors
        # Write remaining buffer (Buffer2)
        oOutputData.pushRawBuffer(lRawBuffer2[lRawBuffer1.size..-1])
        # Buffer1 is finished
        lRawBuffer1 = nil
        lNbrSamplesProcessed += lNbrSamples2
      end
      # Read next buffers if they are not finished
      if (lRawBuffer1 != nil)
        iInputData.each_raw_buffer(lNbrSamplesProcessed) do |iRawBuffer, iNbrSamples, iNbrChannels|
          break
        end
        lRawBuffer1, lNbrSamples1, lNbrChannels = iInputData.get_current_raw_buffer
      end
      if (lRawBuffer2 != nil)
        iInputData2.each_raw_buffer(lNbrSamplesProcessed) do |iRawBuffer, iNbrSamples, iNbrChannels|
          break
        end
        lRawBuffer2, lNbrSamples2, lNbrChannels = iInputData2.get_current_raw_buffer
      end
    end
  end
  if (@DistortionMap != nil)
    # Write the distortion map
    log_info 'Generate distortion map in distortion.diffmap'
    File.open('distortion.diffmap', 'wb') do |oFile|
      oFile.write(Marshal.dump(@DistortionMap))
    end
    log_info 'Generate invert map in invert.map'
    # We want to spot the values that are missing, and the duplicate values
    lInvertMap = [nil]*(2**@NbrBitsPerSample)
    (@MinValue .. @MaxValue).each do |iValue|
      if (@DistortionMap[iValue] == nil)
        log_warn "Value #{iValue} was not part of the input file"
      else
        lRecordedValue = iValue + @DistortionMap[iValue]
        if (lInvertMap[lRecordedValue] == nil)
          lInvertMap[lRecordedValue] = iValue
        else
          if (iValue.abs < lInvertMap[lRecordedValue].abs)
            log_warn "Recorded value #{lRecordedValue} is used for both input values #{iValue} and #{lInvertMap[lRecordedValue]}. Setting it to #{iValue}."
            lInvertMap[lRecordedValue] = iValue
          else
            log_warn "Recorded value #{lRecordedValue} is used for both input values #{iValue} and #{lInvertMap[lRecordedValue]}. Keeping it to #{lInvertMap[lRecordedValue]}."
          end
        end
      end
    end
    (@MinValue .. @MaxValue).each do |iValue|
      if (lInvertMap[iValue] == nil)
        log_warn "Missing value that has never been recorded: #{iValue}"
      end
    end
    File.open('invert.map', 'wb') do |oFile|
      oFile.write(Marshal.dump(lInvertMap))
    end
  end
  puts "Cumulative errors: #{@CumulativeErrors} (#{Float(@CumulativeErrors*100)/Float(iInputData.NbrSamples*(2**@NbrBitsPerSample))} %)"

  return rError
end

#get_nbr_samples(iInputData) ⇒ Object

Get the number of samples that will be written. This is called before execute, as it is needed to write the output file. It is possible to give a majoration: it will be padded with silence.

Parameters
  • iInputData (WSK::Model::InputData): The input data

Return
  • Integer: The number of samples to be written



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
# File 'lib/WSK/Actions/Compare.rb', line 23

def get_nbr_samples(iInputData)
  rNbrSamples = iInputData.NbrSamples

  # Get the second input file
  lError = accessInputWaveFile(@InputFileName2) do |iInputHeader2, iInputData2|
    rSubError = nil
    # First check that headers are the same
    if (iInputHeader2 != iInputData.Header)
      rSubError = RuntimeError.new("Mismatch headers: First input file: #{iInputData.Header.inspect} Second input file: #{iInputHeader2.inspect}")
    end
    # Then we will return the maximal samples
    if (iInputData2.NbrSamples > iInputData.NbrSamples)
      log_warn "Second file has more samples (#{iInputData2.NbrSamples} > #{iInputData.NbrSamples})."
      rNbrSamples = iInputData2.NbrSamples
    elsif (iInputData2.NbrSamples < iInputData.NbrSamples)
      log_warn "Second file has less samples (#{iInputData2.NbrSamples} < #{iInputData.NbrSamples})."
    else
      log_info 'Files have the same number of samples.'
    end
    next rSubError
  end
  if (lError != nil)
    raise lError
  end
  @TotalNbrSamples = rNbrSamples

  return rNbrSamples
end