OLD | NEW |
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 29 matching lines...) Expand all Loading... |
40 | 40 |
41 const unsigned RealtimeAnalyser::DefaultFFTSize = 2048; | 41 const unsigned RealtimeAnalyser::DefaultFFTSize = 2048; |
42 // All FFT implementations are expected to handle power-of-two sizes MinFFTSize
<= size <= MaxFFTSize. | 42 // All FFT implementations are expected to handle power-of-two sizes MinFFTSize
<= size <= MaxFFTSize. |
43 const unsigned RealtimeAnalyser::MinFFTSize = 32; | 43 const unsigned RealtimeAnalyser::MinFFTSize = 32; |
44 const unsigned RealtimeAnalyser::MaxFFTSize = 32768; | 44 const unsigned RealtimeAnalyser::MaxFFTSize = 32768; |
45 const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize
* 2; | 45 const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize
* 2; |
46 | 46 |
47 RealtimeAnalyser::RealtimeAnalyser() | 47 RealtimeAnalyser::RealtimeAnalyser() |
48 : m_inputBuffer(InputBufferSize) | 48 : m_inputBuffer(InputBufferSize) |
49 , m_writeIndex(0) | 49 , m_writeIndex(0) |
| 50 , m_downMixBus(AudioBus::create(1, AudioUtilities::kRenderQuantumFrames)) |
50 , m_fftSize(DefaultFFTSize) | 51 , m_fftSize(DefaultFFTSize) |
51 , m_magnitudeBuffer(DefaultFFTSize / 2) | 52 , m_magnitudeBuffer(DefaultFFTSize / 2) |
52 , m_smoothingTimeConstant(DefaultSmoothingTimeConstant) | 53 , m_smoothingTimeConstant(DefaultSmoothingTimeConstant) |
53 , m_minDecibels(DefaultMinDecibels) | 54 , m_minDecibels(DefaultMinDecibels) |
54 , m_maxDecibels(DefaultMaxDecibels) | 55 , m_maxDecibels(DefaultMaxDecibels) |
55 , m_lastAnalysisTime(-1) | 56 , m_lastAnalysisTime(-1) |
56 { | 57 { |
57 m_analysisFrame = adoptPtr(new FFTFrame(DefaultFFTSize)); | 58 m_analysisFrame = adoptPtr(new FFTFrame(DefaultFFTSize)); |
58 } | 59 } |
59 | 60 |
(...skipping 25 matching lines...) Expand all Loading... |
85 if (!isBusGood) | 86 if (!isBusGood) |
86 return; | 87 return; |
87 | 88 |
88 // FIXME : allow to work with non-FFTSize divisible chunking | 89 // FIXME : allow to work with non-FFTSize divisible chunking |
89 bool isDestinationGood = m_writeIndex < m_inputBuffer.size() && m_writeIndex
+ framesToProcess <= m_inputBuffer.size(); | 90 bool isDestinationGood = m_writeIndex < m_inputBuffer.size() && m_writeIndex
+ framesToProcess <= m_inputBuffer.size(); |
90 ASSERT(isDestinationGood); | 91 ASSERT(isDestinationGood); |
91 if (!isDestinationGood) | 92 if (!isDestinationGood) |
92 return; | 93 return; |
93 | 94 |
94 // Perform real-time analysis | 95 // Perform real-time analysis |
95 const float* source = bus->channel(0)->data(); | |
96 float* dest = m_inputBuffer.data() + m_writeIndex; | 96 float* dest = m_inputBuffer.data() + m_writeIndex; |
97 | 97 |
98 // The source has already been sanity checked with isBusGood above. | 98 // Clear the bus and downmix the input according to the down mixing rules.
Then save the result |
99 memcpy(dest, source, sizeof(float) * framesToProcess); | 99 // in the m_inputBuffer at the appropriate place. |
100 | 100 m_downMixBus->zero(); |
101 // Sum all channels in one if numberOfChannels > 1. | 101 m_downMixBus->sumFrom(*bus); |
102 unsigned numberOfChannels = bus->numberOfChannels(); | 102 memcpy(dest, m_downMixBus->channel(0)->data(), framesToProcess * sizeof(*des
t)); |
103 if (numberOfChannels > 1) { | |
104 for (unsigned i = 1; i < numberOfChannels; ++i) { | |
105 source = bus->channel(i)->data(); | |
106 VectorMath::vadd(dest, 1, source, 1, dest, 1, framesToProcess); | |
107 } | |
108 const float scale = 1.0 / numberOfChannels; | |
109 VectorMath::vsmul(dest, 1, &scale, dest, 1, framesToProcess); | |
110 } | |
111 | 103 |
112 m_writeIndex += framesToProcess; | 104 m_writeIndex += framesToProcess; |
113 if (m_writeIndex >= InputBufferSize) | 105 if (m_writeIndex >= InputBufferSize) |
114 m_writeIndex = 0; | 106 m_writeIndex = 0; |
115 } | 107 } |
116 | 108 |
117 namespace { | 109 namespace { |
118 | 110 |
119 void applyWindow(float* p, size_t n) | 111 void applyWindow(float* p, size_t n) |
120 { | 112 { |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 if (scaledValue > UCHAR_MAX) | 318 if (scaledValue > UCHAR_MAX) |
327 scaledValue = UCHAR_MAX; | 319 scaledValue = UCHAR_MAX; |
328 | 320 |
329 destination[i] = static_cast<unsigned char>(scaledValue); | 321 destination[i] = static_cast<unsigned char>(scaledValue); |
330 } | 322 } |
331 } | 323 } |
332 } | 324 } |
333 | 325 |
334 } // namespace blink | 326 } // namespace blink |
335 | 327 |
OLD | NEW |