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( |
| 20 AudioNode& node, |
| 21 float sampleRate, |
| 22 AudioParamHandler& offset) |
| 23 : AudioScheduledSourceHandler(NodeTypeConstantSource, node, sampleRate) |
| 24 , m_offset(offset) |
| 25 , m_sampleAccurateValues(ProcessingSizeInFrames) |
| 26 { |
| 27 // A ConstantSource is always mono. |
| 28 addOutput(1); |
| 29 |
| 30 initialize(); |
| 31 } |
| 32 |
| 33 PassRefPtr<ConstantSourceHandler> ConstantSourceHandler::create( |
| 34 AudioNode& node, |
| 35 float sampleRate, |
| 36 AudioParamHandler& offset) |
| 37 { |
| 38 return adoptRef(new ConstantSourceHandler(node, sampleRate, offset)); |
| 39 } |
| 40 |
| 41 ConstantSourceHandler::~ConstantSourceHandler() |
| 42 { |
| 43 uninitialize(); |
| 44 } |
| 45 |
| 46 void ConstantSourceHandler::process(size_t framesToProcess) |
| 47 { |
| 48 AudioBus* outputBus = output(0).bus(); |
| 49 DCHECK(outputBus); |
| 50 |
| 51 if (!isInitialized() || !outputBus->numberOfChannels()) { |
| 52 outputBus->zero(); |
| 53 return; |
| 54 } |
| 55 |
| 56 // The audio thread can't block on this lock, so we call tryLock() instead. |
| 57 MutexTryLocker tryLocker(m_processLock); |
| 58 if (!tryLocker.locked()) { |
| 59 // Too bad - the tryLock() failed. |
| 60 outputBus->zero(); |
| 61 return; |
| 62 } |
| 63 |
| 64 size_t quantumFrameOffset; |
| 65 size_t nonSilentFramesToProcess; |
| 66 |
| 67 // Figure out where in the current rendering quantum that the source is |
| 68 // active and for how many frames. |
| 69 updateSchedulingInfo(framesToProcess, outputBus, quantumFrameOffset, nonSile
ntFramesToProcess); |
| 70 |
| 71 if (!nonSilentFramesToProcess) { |
| 72 outputBus->zero(); |
| 73 return; |
| 74 } |
| 75 |
| 76 if (m_offset->hasSampleAccurateValues()) { |
| 77 DCHECK_LE(framesToProcess, m_sampleAccurateValues.size()); |
| 78 if (framesToProcess <= m_sampleAccurateValues.size()) { |
| 79 float* offsets = m_sampleAccurateValues.data(); |
| 80 m_offset->calculateSampleAccurateValues(offsets, framesToProcess); |
| 81 if (nonSilentFramesToProcess > 0) { |
| 82 memcpy( |
| 83 outputBus->channel(0)->mutableData() + quantumFrameOffset, |
| 84 offsets + quantumFrameOffset, |
| 85 nonSilentFramesToProcess * sizeof(*offsets)); |
| 86 outputBus->clearSilentFlag(); |
| 87 } else { |
| 88 outputBus->zero(); |
| 89 } |
| 90 } |
| 91 } else { |
| 92 float value = m_offset->value(); |
| 93 |
| 94 if (value == 0) { |
| 95 outputBus->zero(); |
| 96 } else { |
| 97 float* dest = outputBus->channel(0)->mutableData(); |
| 98 dest += quantumFrameOffset; |
| 99 for (unsigned k = 0; k < nonSilentFramesToProcess; ++k) { |
| 100 dest[k] = value; |
| 101 } |
| 102 outputBus->clearSilentFlag(); |
| 103 } |
| 104 } |
| 105 } |
| 106 |
| 107 bool ConstantSourceHandler::propagatesSilence() const |
| 108 { |
| 109 return !isPlayingOrScheduled() || hasFinished(); |
| 110 } |
| 111 |
| 112 // ---------------------------------------------------------------- |
| 113 ConstantSourceNode::ConstantSourceNode(BaseAudioContext& context) |
| 114 : AudioScheduledSourceNode(context) |
| 115 , m_offset(AudioParam::create(context, ParamTypeConstantSourceValue, 1)) |
| 116 { |
| 117 setHandler( |
| 118 ConstantSourceHandler::create( |
| 119 *this, |
| 120 context.sampleRate(), |
| 121 m_offset->handler())); |
| 122 } |
| 123 |
| 124 ConstantSourceNode* ConstantSourceNode::create( |
| 125 BaseAudioContext& context, |
| 126 ExceptionState& exceptionState) |
| 127 { |
| 128 DCHECK(isMainThread()); |
| 129 |
| 130 if (context.isContextClosed()) { |
| 131 context.throwExceptionForClosedState(exceptionState); |
| 132 return nullptr; |
| 133 } |
| 134 |
| 135 return new ConstantSourceNode(context); |
| 136 } |
| 137 |
| 138 ConstantSourceNode* ConstantSourceNode::create( |
| 139 BaseAudioContext* context, |
| 140 const ConstantSourceOptions& options, |
| 141 ExceptionState& exceptionState) |
| 142 { |
| 143 DCHECK(isMainThread()); |
| 144 |
| 145 ConstantSourceNode* node = create(*context, exceptionState); |
| 146 |
| 147 if (!node) |
| 148 return nullptr; |
| 149 |
| 150 node->offset()->setValue(options.offset()); |
| 151 |
| 152 return node; |
| 153 } |
| 154 |
| 155 DEFINE_TRACE(ConstantSourceNode) |
| 156 { |
| 157 visitor->trace(m_offset); |
| 158 AudioScheduledSourceNode::trace(visitor); |
| 159 } |
| 160 |
| 161 ConstantSourceHandler& ConstantSourceNode::constantSourceHandler() const |
| 162 { |
| 163 return static_cast<ConstantSourceHandler&>(handler()); |
| 164 } |
| 165 |
| 166 AudioParam* ConstantSourceNode::offset() |
| 167 { |
| 168 return m_offset; |
| 169 } |
| 170 |
| 171 } // namespace blink |
OLD | NEW |