| 48 | } |
| 49 | |
| 50 | void PowerMonitorNode::process(ContextRenderLock & r, int bufferSize) |
| 51 | { |
| 52 | // This node acts as a pass-through if it is embedded in a chain |
| 53 | |
| 54 | AudioBus * outputBus = output(0)->bus(r); |
| 55 | |
| 56 | if (!isInitialized() || !input(0)->isConnected()) |
| 57 | { |
| 58 | if (outputBus) |
| 59 | outputBus->zero(); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | AudioBus * bus = input(0)->bus(r); |
| 64 | bool isBusGood = bus && bus->numberOfChannels() > 0 && bus->channel(0)->length() >= bufferSize; |
| 65 | if (!isBusGood) |
| 66 | { |
| 67 | outputBus->zero(); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // calculate the power of this buffer |
| 72 | { |
| 73 | int start = static_cast<int>(bufferSize) - static_cast<int>(_windowSize->valueUint32()); |
| 74 | int end = static_cast<int>(bufferSize); |
| 75 | if (start < 0) |
| 76 | start = 0; |
| 77 | |
| 78 | float power = 0; |
| 79 | int numberOfChannels = bus->numberOfChannels(); |
| 80 | for (int c = 0; c < numberOfChannels; ++c) { |
| 81 | const float* data = bus->channel(c)->data(); |
| 82 | for (int i = start; i < end; ++i) |
| 83 | { |
| 84 | float p = data[i]; |
| 85 | power += p * p; |
| 86 | } |
| 87 | } |
| 88 | float rms = sqrtf(power / (numberOfChannels * bufferSize)); |
| 89 | |
| 90 | // Protect against accidental overload due to bad values in input stream |
| 91 | const float kMinPower = 0.000125f; |
| 92 | if (std::isinf(power) || std::isnan(power) || power < kMinPower) |
| 93 | power = kMinPower; |
| 94 | |
| 95 | // db is 20 * log10(rms/Vref) where Vref is 1.0 |
| 96 | _db = 20.0f * logf(rms) / logf(10.0f); |
| 97 | } |
| 98 | // to here |
| 99 | |
| 100 | if (bus != outputBus) |
| 101 | outputBus->copyFrom(*bus); |
| 102 | } |
| 103 | |
| 104 | void PowerMonitorNode::reset(ContextRenderLock &) |
| 105 | { |
nothing calls this directly
no test coverage detected