Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/RealtimeAnalyser.cpp

Issue 1729783002: Don't clip FFT output values to minDecibels. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 } 185 }
186 186
187 void RealtimeAnalyser::getFloatFrequencyData(DOMFloat32Array* destinationArray) 187 void RealtimeAnalyser::getFloatFrequencyData(DOMFloat32Array* destinationArray)
188 { 188 {
189 ASSERT(isMainThread()); 189 ASSERT(isMainThread());
190 ASSERT(destinationArray); 190 ASSERT(destinationArray);
191 191
192 doFFTAnalysis(); 192 doFFTAnalysis();
193 193
194 // Convert from linear magnitude to floating-point decibels. 194 // Convert from linear magnitude to floating-point decibels.
195 const double minDecibels = m_minDecibels;
196 unsigned sourceLength = magnitudeBuffer().size(); 195 unsigned sourceLength = magnitudeBuffer().size();
197 size_t len = std::min(sourceLength, destinationArray->length()); 196 size_t len = std::min(sourceLength, destinationArray->length());
198 if (len > 0) { 197 if (len > 0) {
199 const float* source = magnitudeBuffer().data(); 198 const float* source = magnitudeBuffer().data();
200 float* destination = destinationArray->data(); 199 float* destination = destinationArray->data();
201 200
202 for (unsigned i = 0; i < len; ++i) { 201 for (unsigned i = 0; i < len; ++i) {
203 float linearValue = source[i]; 202 float linearValue = source[i];
204 double dbMag = !linearValue ? minDecibels : AudioUtilities::linearTo Decibels(linearValue); 203 double dbMag = AudioUtilities::linearToDecibels(linearValue);
205 destination[i] = float(dbMag); 204 destination[i] = float(dbMag);
206 } 205 }
207 } 206 }
208 } 207 }
209 208
210 void RealtimeAnalyser::getByteFrequencyData(DOMUint8Array* destinationArray) 209 void RealtimeAnalyser::getByteFrequencyData(DOMUint8Array* destinationArray)
211 { 210 {
212 ASSERT(isMainThread()); 211 ASSERT(isMainThread());
213 ASSERT(destinationArray); 212 ASSERT(destinationArray);
214 213
215 doFFTAnalysis(); 214 doFFTAnalysis();
216 215
217 // Convert from linear magnitude to unsigned-byte decibels. 216 // Convert from linear magnitude to unsigned-byte decibels.
218 unsigned sourceLength = magnitudeBuffer().size(); 217 unsigned sourceLength = magnitudeBuffer().size();
219 size_t len = std::min(sourceLength, destinationArray->length()); 218 size_t len = std::min(sourceLength, destinationArray->length());
220 if (len > 0) { 219 if (len > 0) {
221 const double rangeScaleFactor = m_maxDecibels == m_minDecibels ? 1 : 1 / (m_maxDecibels - m_minDecibels); 220 const double rangeScaleFactor = m_maxDecibels == m_minDecibels ? 1 : 1 / (m_maxDecibels - m_minDecibels);
222 const double minDecibels = m_minDecibels; 221 const double minDecibels = m_minDecibels;
223 222
224 const float* source = magnitudeBuffer().data(); 223 const float* source = magnitudeBuffer().data();
225 unsigned char* destination = destinationArray->data(); 224 unsigned char* destination = destinationArray->data();
226 225
227 for (unsigned i = 0; i < len; ++i) { 226 for (unsigned i = 0; i < len; ++i) {
228 float linearValue = source[i]; 227 float linearValue = source[i];
229 double dbMag = !linearValue ? minDecibels : AudioUtilities::linearTo Decibels(linearValue); 228 double dbMag = AudioUtilities::linearToDecibels(linearValue);
230 229
231 // The range m_minDecibels to m_maxDecibels will be scaled to byte v alues from 0 to UCHAR_MAX. 230 // The range m_minDecibels to m_maxDecibels will be scaled to byte v alues from 0 to UCHAR_MAX.
232 double scaledValue = UCHAR_MAX * (dbMag - minDecibels) * rangeScaleF actor; 231 double scaledValue = UCHAR_MAX * (dbMag - minDecibels) * rangeScaleF actor;
233 232
234 // Clip to valid range. 233 // Clip to valid range.
235 if (scaledValue < 0) 234 if (scaledValue < 0)
236 scaledValue = 0; 235 scaledValue = 0;
237 if (scaledValue > UCHAR_MAX) 236 if (scaledValue > UCHAR_MAX)
238 scaledValue = UCHAR_MAX; 237 scaledValue = UCHAR_MAX;
239 238
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 if (scaledValue > UCHAR_MAX) 299 if (scaledValue > UCHAR_MAX)
301 scaledValue = UCHAR_MAX; 300 scaledValue = UCHAR_MAX;
302 301
303 destination[i] = static_cast<unsigned char>(scaledValue); 302 destination[i] = static_cast<unsigned char>(scaledValue);
304 } 303 }
305 } 304 }
306 } 305 }
307 306
308 } // namespace blink 307 } // namespace blink
309 308
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698