RealtimeAnalyser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2010, Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #if ENABLE(WEB_AUDIO)
  26. #include "RealtimeAnalyser.h"
  27. #include "AudioBus.h"
  28. #include "AudioUtilities.h"
  29. #include "FFTFrame.h"
  30. #include "VectorMath.h"
  31. #include <algorithm>
  32. #include <complex>
  33. #include <wtf/Float32Array.h>
  34. #include <wtf/MainThread.h>
  35. #include <wtf/MathExtras.h>
  36. #include <wtf/Uint8Array.h>
  37. namespace WebCore {
  38. const double RealtimeAnalyser::DefaultSmoothingTimeConstant = 0.8;
  39. const double RealtimeAnalyser::DefaultMinDecibels = -100;
  40. const double RealtimeAnalyser::DefaultMaxDecibels = -30;
  41. const unsigned RealtimeAnalyser::DefaultFFTSize = 2048;
  42. // All FFT implementations are expected to handle power-of-two sizes MinFFTSize <= size <= MaxFFTSize.
  43. const unsigned RealtimeAnalyser::MinFFTSize = 32;
  44. const unsigned RealtimeAnalyser::MaxFFTSize = 2048;
  45. const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize * 2;
  46. RealtimeAnalyser::RealtimeAnalyser()
  47. : m_inputBuffer(InputBufferSize)
  48. , m_writeIndex(0)
  49. , m_fftSize(DefaultFFTSize)
  50. , m_magnitudeBuffer(DefaultFFTSize / 2)
  51. , m_smoothingTimeConstant(DefaultSmoothingTimeConstant)
  52. , m_minDecibels(DefaultMinDecibels)
  53. , m_maxDecibels(DefaultMaxDecibels)
  54. {
  55. m_analysisFrame = adoptPtr(new FFTFrame(DefaultFFTSize));
  56. }
  57. RealtimeAnalyser::~RealtimeAnalyser()
  58. {
  59. }
  60. void RealtimeAnalyser::reset()
  61. {
  62. m_writeIndex = 0;
  63. m_inputBuffer.zero();
  64. m_magnitudeBuffer.zero();
  65. }
  66. bool RealtimeAnalyser::setFftSize(size_t size)
  67. {
  68. ASSERT(isMainThread());
  69. // Only allow powers of two.
  70. unsigned log2size = static_cast<unsigned>(log2(size));
  71. bool isPOT(1UL << log2size == size);
  72. if (!isPOT || size > MaxFFTSize || size < MinFFTSize)
  73. return false;
  74. if (m_fftSize != size) {
  75. m_analysisFrame = adoptPtr(new FFTFrame(size));
  76. // m_magnitudeBuffer has size = fftSize / 2 because it contains floats reduced from complex values in m_analysisFrame.
  77. m_magnitudeBuffer.allocate(size / 2);
  78. m_fftSize = size;
  79. }
  80. return true;
  81. }
  82. void RealtimeAnalyser::writeInput(AudioBus* bus, size_t framesToProcess)
  83. {
  84. bool isBusGood = bus && bus->numberOfChannels() > 0 && bus->channel(0)->length() >= framesToProcess;
  85. ASSERT(isBusGood);
  86. if (!isBusGood)
  87. return;
  88. // FIXME : allow to work with non-FFTSize divisible chunking
  89. bool isDestinationGood = m_writeIndex < m_inputBuffer.size() && m_writeIndex + framesToProcess <= m_inputBuffer.size();
  90. ASSERT(isDestinationGood);
  91. if (!isDestinationGood)
  92. return;
  93. // Perform real-time analysis
  94. const float* source = bus->channel(0)->data();
  95. float* dest = m_inputBuffer.data() + m_writeIndex;
  96. // The source has already been sanity checked with isBusGood above.
  97. memcpy(dest, source, sizeof(float) * framesToProcess);
  98. // Sum all channels in one if numberOfChannels > 1.
  99. unsigned numberOfChannels = bus->numberOfChannels();
  100. if (numberOfChannels > 1) {
  101. for (unsigned i = 1; i < numberOfChannels; i++) {
  102. source = bus->channel(i)->data();
  103. VectorMath::vadd(dest, 1, source, 1, dest, 1, framesToProcess);
  104. }
  105. const float scale = 1.0 / numberOfChannels;
  106. VectorMath::vsmul(dest, 1, &scale, dest, 1, framesToProcess);
  107. }
  108. m_writeIndex += framesToProcess;
  109. if (m_writeIndex >= InputBufferSize)
  110. m_writeIndex = 0;
  111. }
  112. namespace {
  113. void applyWindow(float* p, size_t n)
  114. {
  115. ASSERT(isMainThread());
  116. // Blackman window
  117. double alpha = 0.16;
  118. double a0 = 0.5 * (1 - alpha);
  119. double a1 = 0.5;
  120. double a2 = 0.5 * alpha;
  121. for (unsigned i = 0; i < n; ++i) {
  122. double x = static_cast<double>(i) / static_cast<double>(n);
  123. double window = a0 - a1 * cos(2 * piDouble * x) + a2 * cos(4 * piDouble * x);
  124. p[i] *= float(window);
  125. }
  126. }
  127. } // namespace
  128. void RealtimeAnalyser::doFFTAnalysis()
  129. {
  130. ASSERT(isMainThread());
  131. // Unroll the input buffer into a temporary buffer, where we'll apply an analysis window followed by an FFT.
  132. size_t fftSize = this->fftSize();
  133. AudioFloatArray temporaryBuffer(fftSize);
  134. float* inputBuffer = m_inputBuffer.data();
  135. float* tempP = temporaryBuffer.data();
  136. // Take the previous fftSize values from the input buffer and copy into the temporary buffer.
  137. unsigned writeIndex = m_writeIndex;
  138. if (writeIndex < fftSize) {
  139. memcpy(tempP, inputBuffer + writeIndex - fftSize + InputBufferSize, sizeof(*tempP) * (fftSize - writeIndex));
  140. memcpy(tempP + fftSize - writeIndex, inputBuffer, sizeof(*tempP) * writeIndex);
  141. } else
  142. memcpy(tempP, inputBuffer + writeIndex - fftSize, sizeof(*tempP) * fftSize);
  143. // Window the input samples.
  144. applyWindow(tempP, fftSize);
  145. // Do the analysis.
  146. m_analysisFrame->doFFT(tempP);
  147. float* realP = m_analysisFrame->realData();
  148. float* imagP = m_analysisFrame->imagData();
  149. // Blow away the packed nyquist component.
  150. imagP[0] = 0;
  151. // Normalize so than an input sine wave at 0dBfs registers as 0dBfs (undo FFT scaling factor).
  152. const double magnitudeScale = 1.0 / DefaultFFTSize;
  153. // A value of 0 does no averaging with the previous result. Larger values produce slower, but smoother changes.
  154. double k = m_smoothingTimeConstant;
  155. k = std::max(0.0, k);
  156. k = std::min(1.0, k);
  157. // Convert the analysis data from complex to magnitude and average with the previous result.
  158. float* destination = magnitudeBuffer().data();
  159. size_t n = magnitudeBuffer().size();
  160. for (size_t i = 0; i < n; ++i) {
  161. std::complex<double> c(realP[i], imagP[i]);
  162. double scalarMagnitude = abs(c) * magnitudeScale;
  163. destination[i] = static_cast<float>(k * destination[i] + (1 - k) * scalarMagnitude);
  164. }
  165. }
  166. void RealtimeAnalyser::getFloatFrequencyData(Float32Array* destinationArray)
  167. {
  168. ASSERT(isMainThread());
  169. if (!destinationArray)
  170. return;
  171. doFFTAnalysis();
  172. // Convert from linear magnitude to floating-point decibels.
  173. const double minDecibels = m_minDecibels;
  174. unsigned sourceLength = magnitudeBuffer().size();
  175. size_t len = std::min(sourceLength, destinationArray->length());
  176. if (len > 0) {
  177. const float* source = magnitudeBuffer().data();
  178. float* destination = destinationArray->data();
  179. for (unsigned i = 0; i < len; ++i) {
  180. float linearValue = source[i];
  181. double dbMag = !linearValue ? minDecibels : AudioUtilities::linearToDecibels(linearValue);
  182. destination[i] = static_cast<float>(dbMag);
  183. }
  184. }
  185. }
  186. void RealtimeAnalyser::getByteFrequencyData(Uint8Array* destinationArray)
  187. {
  188. ASSERT(isMainThread());
  189. if (!destinationArray)
  190. return;
  191. doFFTAnalysis();
  192. // Convert from linear magnitude to unsigned-byte decibels.
  193. unsigned sourceLength = magnitudeBuffer().size();
  194. size_t len = std::min(sourceLength, destinationArray->length());
  195. if (len > 0) {
  196. const double rangeScaleFactor = m_maxDecibels == m_minDecibels ? 1 : 1 / (m_maxDecibels - m_minDecibels);
  197. const double minDecibels = m_minDecibels;
  198. const float* source = magnitudeBuffer().data();
  199. unsigned char* destination = destinationArray->data();
  200. for (unsigned i = 0; i < len; ++i) {
  201. float linearValue = source[i];
  202. double dbMag = !linearValue ? minDecibels : AudioUtilities::linearToDecibels(linearValue);
  203. // The range m_minDecibels to m_maxDecibels will be scaled to byte values from 0 to UCHAR_MAX.
  204. double scaledValue = UCHAR_MAX * (dbMag - minDecibels) * rangeScaleFactor;
  205. // Clip to valid range.
  206. if (scaledValue < 0)
  207. scaledValue = 0;
  208. if (scaledValue > UCHAR_MAX)
  209. scaledValue = UCHAR_MAX;
  210. destination[i] = static_cast<unsigned char>(scaledValue);
  211. }
  212. }
  213. }
  214. void RealtimeAnalyser::getByteTimeDomainData(Uint8Array* destinationArray)
  215. {
  216. ASSERT(isMainThread());
  217. if (!destinationArray)
  218. return;
  219. unsigned fftSize = this->fftSize();
  220. size_t len = std::min(fftSize, destinationArray->length());
  221. if (len > 0) {
  222. bool isInputBufferGood = m_inputBuffer.size() == InputBufferSize && m_inputBuffer.size() > fftSize;
  223. ASSERT(isInputBufferGood);
  224. if (!isInputBufferGood)
  225. return;
  226. float* inputBuffer = m_inputBuffer.data();
  227. unsigned char* destination = destinationArray->data();
  228. unsigned writeIndex = m_writeIndex;
  229. for (unsigned i = 0; i < len; ++i) {
  230. // Buffer access is protected due to modulo operation.
  231. float value = inputBuffer[(i + writeIndex - fftSize + InputBufferSize) % InputBufferSize];
  232. // Scale from nominal -1 -> +1 to unsigned byte.
  233. double scaledValue = 128 * (value + 1);
  234. // Clip to valid range.
  235. if (scaledValue < 0)
  236. scaledValue = 0;
  237. if (scaledValue > UCHAR_MAX)
  238. scaledValue = UCHAR_MAX;
  239. destination[i] = static_cast<unsigned char>(scaledValue);
  240. }
  241. }
  242. }
  243. } // namespace WebCore
  244. #endif // ENABLE(WEB_AUDIO)