OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/AudioWorkletNode.h" |
| 6 |
| 7 #include "modules/webaudio/AudioNodeInput.h" |
| 8 #include "modules/webaudio/AudioNodeOutput.h" |
| 9 #include "platform/audio/AudioBus.h" |
| 10 #include "platform/audio/AudioUtilities.h" |
| 11 |
| 12 #include "bindings/core/v8/Dictionary.h" |
| 13 #include "core/dom/TaskRunnerHelper.h" |
| 14 #include "modules/webaudio/AudioWorkletGlobalScope.h" |
| 15 #include "modules/webaudio/AudioWorkletProcessor.h" |
| 16 #include "platform/heap/Persistent.h" |
| 17 #include "modules/webaudio/AudioBuffer.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 AudioWorkletHandler::AudioWorkletHandler(AudioNode& node, |
| 22 float sample_rate, |
| 23 String name) |
| 24 : AudioHandler(kNodeTypeAudioWorklet, node, sample_rate) { |
| 25 DCHECK(IsMainThread()); |
| 26 |
| 27 AddInput(); |
| 28 AddOutput(1); |
| 29 |
| 30 WaitableEvent doneConstruction; |
| 31 Context()->GetWorkletMessagingProxy()->CreateProcessorInstance( |
| 32 name, this, &doneConstruction); |
| 33 doneConstruction.Wait(); |
| 34 |
| 35 // Initialize AudioParams. |
| 36 // for (const auto& descriptor : processor_->GetAudioParamDescriptors()) { |
| 37 |
| 38 // AudioParam* audio_param = AudioParam::Create(*Context(), |
| 39 // kParamTypeAudioWorkletNode, |
| 40 // descriptor.defaultValue(), |
| 41 // descriptor.minValue(), |
| 42 // descriptor.maxValue()); |
| 43 |
| 44 // static_cast<AudioWorkletNode&>(node).AddAudioParam( |
| 45 // descriptor.name(), audio_param); |
| 46 // param_handler_map_.insert(descriptor.name(), &(audio_param->Handler())); |
| 47 |
| 48 // // LOG(INFO) << " param name = " << descriptor.name(); |
| 49 // // LOG(INFO) << " param defaultValue = " << descriptor.defaultValue(); |
| 50 // // LOG(INFO) << " param minValue = " << descriptor.minValue(); |
| 51 // // LOG(INFO) << " param maxValue = " << descriptor.maxValue(); |
| 52 // } |
| 53 |
| 54 Initialize(); |
| 55 } |
| 56 |
| 57 AudioWorkletHandler::~AudioWorkletHandler() { |
| 58 Uninitialize(); |
| 59 processor_ = nullptr; |
| 60 } |
| 61 |
| 62 PassRefPtr<AudioWorkletHandler> AudioWorkletHandler::Create(AudioNode& node, |
| 63 float sample_rate, |
| 64 String name) { |
| 65 return AdoptRef(new AudioWorkletHandler(node, sample_rate, name)); |
| 66 } |
| 67 |
| 68 void AudioWorkletHandler::Process(size_t frames_to_process) { |
| 69 AudioBus* output_bus = Output(0).Bus(); |
| 70 DCHECK(output_bus); |
| 71 |
| 72 if (!IsInitialized()) { |
| 73 output_bus->Zero(); |
| 74 return; |
| 75 } |
| 76 |
| 77 AudioBuffer* input_buffer = Input(0).IsConnected() |
| 78 ? AudioBuffer::CreateFromAudioBus(Input(0).Bus()) |
| 79 : AudioBuffer::Create(1, 128, Context()->sampleRate()); |
| 80 AudioBuffer* output_buffer = |
| 81 AudioBuffer::Create(1, 128, Context()->sampleRate()); |
| 82 |
| 83 processor_->Process(input_buffer, output_buffer); |
| 84 |
| 85 for (unsigned i = 0; i < 1; ++i) { |
| 86 memcpy(output_bus->Channel(i)->MutableData(), |
| 87 output_buffer->getChannelData(i).View()->Data(), |
| 88 sizeof(float) * frames_to_process); |
| 89 } |
| 90 } |
| 91 |
| 92 void AudioWorkletHandler::SetProcessor(AudioWorkletProcessor* processor) { |
| 93 DCHECK(!IsMainThread()); |
| 94 processor_ = processor; |
| 95 } |
| 96 |
| 97 // ---------------------------------------------------------------- |
| 98 |
| 99 AudioWorkletNode::AudioWorkletNode(BaseAudioContext& context, |
| 100 const String& name) |
| 101 : AudioNode(context), name_(name) { |
| 102 SetHandler(AudioWorkletHandler::Create(*this, context.sampleRate(), name)); |
| 103 } |
| 104 |
| 105 AudioWorkletNode* AudioWorkletNode::Create(BaseAudioContext* context, |
| 106 const String& name, |
| 107 ExceptionState& exception_state) { |
| 108 DCHECK(IsMainThread()); |
| 109 |
| 110 if (context->IsContextClosed()) { |
| 111 context->ThrowExceptionForClosedState(exception_state); |
| 112 return nullptr; |
| 113 } |
| 114 |
| 115 AudioWorkletNode* node = new AudioWorkletNode(*context, name); |
| 116 |
| 117 if (!node) |
| 118 return nullptr; |
| 119 |
| 120 // Do something with node. |
| 121 // node->handleChannelOptions(options, exception_state); |
| 122 |
| 123 return node; |
| 124 } |
| 125 |
| 126 bool AudioWorkletNode::HasPendingActivity() const { |
| 127 return !context()->IsContextClosed(); |
| 128 } |
| 129 |
| 130 AudioParamMap* AudioWorkletNode::parameters() const { |
| 131 return parameter_map_; |
| 132 } |
| 133 |
| 134 // void AudioWorkletNode::AddAudioParam( |
| 135 // const String& param_name, AudioParam* audio_param) { |
| 136 // parameter_map_->AddParameter(param_name, audio_param); |
| 137 // } |
| 138 |
| 139 AudioWorkletHandler& AudioWorkletNode::GetWorkletHandler() const { |
| 140 return static_cast<AudioWorkletHandler&>(Handler()); |
| 141 } |
| 142 |
| 143 DEFINE_TRACE(AudioWorkletNode) { |
| 144 visitor->Trace(parameter_map_); |
| 145 AudioNode::Trace(visitor); |
| 146 } |
| 147 |
| 148 } // namespace blink |
OLD | NEW |