OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/webaudio/ConstantSourceNode.h" |
| 6 |
| 7 #include "bindings/core/v8/ExceptionMessages.h" |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "modules/webaudio/AudioNodeOutput.h" |
| 11 #include "modules/webaudio/ConstantSourceOptions.h" |
| 12 #include "platform/audio/AudioUtilities.h" |
| 13 #include "wtf/MathExtras.h" |
| 14 #include "wtf/StdLibExtras.h" |
| 15 #include <algorithm> |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 ConstantSourceHandler::ConstantSourceHandler(AudioNode& node, |
| 20 float sampleRate, |
| 21 AudioParamHandler& offset) |
| 22 : AudioScheduledSourceHandler(NodeTypeConstantSource, node, sampleRate), |
| 23 m_offset(offset), |
| 24 m_sampleAccurateValues(ProcessingSizeInFrames) { |
| 25 // A ConstantSource is always mono. |
| 26 addOutput(1); |
| 27 |
| 28 initialize(); |
| 29 } |
| 30 |
| 31 PassRefPtr<ConstantSourceHandler> ConstantSourceHandler::create( |
| 32 AudioNode& node, |
| 33 float sampleRate, |
| 34 AudioParamHandler& offset) { |
| 35 return adoptRef(new ConstantSourceHandler(node, sampleRate, offset)); |
| 36 } |
| 37 |
| 38 ConstantSourceHandler::~ConstantSourceHandler() { |
| 39 uninitialize(); |
| 40 } |
| 41 |
| 42 void ConstantSourceHandler::process(size_t framesToProcess) { |
| 43 AudioBus* outputBus = output(0).bus(); |
| 44 DCHECK(outputBus); |
| 45 |
| 46 if (!isInitialized() || !outputBus->numberOfChannels()) { |
| 47 outputBus->zero(); |
| 48 return; |
| 49 } |
| 50 |
| 51 // The audio thread can't block on this lock, so we call tryLock() instead. |
| 52 MutexTryLocker tryLocker(m_processLock); |
| 53 if (!tryLocker.locked()) { |
| 54 // Too bad - the tryLock() failed. |
| 55 outputBus->zero(); |
| 56 return; |
| 57 } |
| 58 |
| 59 size_t quantumFrameOffset; |
| 60 size_t nonSilentFramesToProcess; |
| 61 |
| 62 // Figure out where in the current rendering quantum that the source is |
| 63 // active and for how many frames. |
| 64 updateSchedulingInfo(framesToProcess, outputBus, quantumFrameOffset, |
| 65 nonSilentFramesToProcess); |
| 66 |
| 67 if (!nonSilentFramesToProcess) { |
| 68 outputBus->zero(); |
| 69 return; |
| 70 } |
| 71 |
| 72 if (m_offset->hasSampleAccurateValues()) { |
| 73 DCHECK_LE(framesToProcess, m_sampleAccurateValues.size()); |
| 74 if (framesToProcess <= m_sampleAccurateValues.size()) { |
| 75 float* offsets = m_sampleAccurateValues.data(); |
| 76 m_offset->calculateSampleAccurateValues(offsets, framesToProcess); |
| 77 if (nonSilentFramesToProcess > 0) { |
| 78 memcpy(outputBus->channel(0)->mutableData() + quantumFrameOffset, |
| 79 offsets + quantumFrameOffset, |
| 80 nonSilentFramesToProcess * sizeof(*offsets)); |
| 81 outputBus->clearSilentFlag(); |
| 82 } else { |
| 83 outputBus->zero(); |
| 84 } |
| 85 } |
| 86 } else { |
| 87 float value = m_offset->value(); |
| 88 |
| 89 if (value == 0) { |
| 90 outputBus->zero(); |
| 91 } else { |
| 92 float* dest = outputBus->channel(0)->mutableData(); |
| 93 dest += quantumFrameOffset; |
| 94 for (unsigned k = 0; k < nonSilentFramesToProcess; ++k) { |
| 95 dest[k] = value; |
| 96 } |
| 97 outputBus->clearSilentFlag(); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
| 102 bool ConstantSourceHandler::propagatesSilence() const { |
| 103 return !isPlayingOrScheduled() || hasFinished(); |
| 104 } |
| 105 |
| 106 // ---------------------------------------------------------------- |
| 107 ConstantSourceNode::ConstantSourceNode(BaseAudioContext& context) |
| 108 : AudioScheduledSourceNode(context), |
| 109 m_offset(AudioParam::create(context, ParamTypeConstantSourceValue, 1)) { |
| 110 setHandler(ConstantSourceHandler::create(*this, context.sampleRate(), |
| 111 m_offset->handler())); |
| 112 } |
| 113 |
| 114 ConstantSourceNode* ConstantSourceNode::create(BaseAudioContext& context, |
| 115 ExceptionState& exceptionState) { |
| 116 DCHECK(isMainThread()); |
| 117 |
| 118 if (context.isContextClosed()) { |
| 119 context.throwExceptionForClosedState(exceptionState); |
| 120 return nullptr; |
| 121 } |
| 122 |
| 123 return new ConstantSourceNode(context); |
| 124 } |
| 125 |
| 126 ConstantSourceNode* ConstantSourceNode::create( |
| 127 BaseAudioContext* context, |
| 128 const ConstantSourceOptions& options, |
| 129 ExceptionState& exceptionState) { |
| 130 DCHECK(isMainThread()); |
| 131 |
| 132 ConstantSourceNode* node = create(*context, exceptionState); |
| 133 |
| 134 if (!node) |
| 135 return nullptr; |
| 136 |
| 137 node->offset()->setValue(options.offset()); |
| 138 |
| 139 return node; |
| 140 } |
| 141 |
| 142 DEFINE_TRACE(ConstantSourceNode) { |
| 143 visitor->trace(m_offset); |
| 144 AudioScheduledSourceNode::trace(visitor); |
| 145 } |
| 146 |
| 147 ConstantSourceHandler& ConstantSourceNode::constantSourceHandler() const { |
| 148 return static_cast<ConstantSourceHandler&>(handler()); |
| 149 } |
| 150 |
| 151 AudioParam* ConstantSourceNode::offset() { |
| 152 return m_offset; |
| 153 } |
| 154 |
| 155 } // namespace blink |
OLD | NEW |