| Index: third_party/WebKit/Source/modules/webaudio/AudioWorkletNode.cpp
|
| diff --git a/third_party/WebKit/Source/modules/webaudio/AudioWorkletNode.cpp b/third_party/WebKit/Source/modules/webaudio/AudioWorkletNode.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..827dcfae6780095f69bb309a80db2dd8b3e6f6dc
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/modules/webaudio/AudioWorkletNode.cpp
|
| @@ -0,0 +1,148 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "modules/webaudio/AudioWorkletNode.h"
|
| +
|
| +#include "modules/webaudio/AudioNodeInput.h"
|
| +#include "modules/webaudio/AudioNodeOutput.h"
|
| +#include "platform/audio/AudioBus.h"
|
| +#include "platform/audio/AudioUtilities.h"
|
| +
|
| +#include "bindings/core/v8/Dictionary.h"
|
| +#include "core/dom/TaskRunnerHelper.h"
|
| +#include "modules/webaudio/AudioWorkletGlobalScope.h"
|
| +#include "modules/webaudio/AudioWorkletProcessor.h"
|
| +#include "platform/heap/Persistent.h"
|
| +#include "modules/webaudio/AudioBuffer.h"
|
| +
|
| +namespace blink {
|
| +
|
| +AudioWorkletHandler::AudioWorkletHandler(AudioNode& node,
|
| + float sample_rate,
|
| + String name)
|
| + : AudioHandler(kNodeTypeAudioWorklet, node, sample_rate) {
|
| + DCHECK(IsMainThread());
|
| +
|
| + AddInput();
|
| + AddOutput(1);
|
| +
|
| + WaitableEvent doneConstruction;
|
| + Context()->GetWorkletMessagingProxy()->CreateProcessorInstance(
|
| + name, this, &doneConstruction);
|
| + doneConstruction.Wait();
|
| +
|
| + // Initialize AudioParams.
|
| + // for (const auto& descriptor : processor_->GetAudioParamDescriptors()) {
|
| +
|
| + // AudioParam* audio_param = AudioParam::Create(*Context(),
|
| + // kParamTypeAudioWorkletNode,
|
| + // descriptor.defaultValue(),
|
| + // descriptor.minValue(),
|
| + // descriptor.maxValue());
|
| +
|
| + // static_cast<AudioWorkletNode&>(node).AddAudioParam(
|
| + // descriptor.name(), audio_param);
|
| + // param_handler_map_.insert(descriptor.name(), &(audio_param->Handler()));
|
| +
|
| + // // LOG(INFO) << " param name = " << descriptor.name();
|
| + // // LOG(INFO) << " param defaultValue = " << descriptor.defaultValue();
|
| + // // LOG(INFO) << " param minValue = " << descriptor.minValue();
|
| + // // LOG(INFO) << " param maxValue = " << descriptor.maxValue();
|
| + // }
|
| +
|
| + Initialize();
|
| +}
|
| +
|
| +AudioWorkletHandler::~AudioWorkletHandler() {
|
| + Uninitialize();
|
| + processor_ = nullptr;
|
| +}
|
| +
|
| +PassRefPtr<AudioWorkletHandler> AudioWorkletHandler::Create(AudioNode& node,
|
| + float sample_rate,
|
| + String name) {
|
| + return AdoptRef(new AudioWorkletHandler(node, sample_rate, name));
|
| +}
|
| +
|
| +void AudioWorkletHandler::Process(size_t frames_to_process) {
|
| + AudioBus* output_bus = Output(0).Bus();
|
| + DCHECK(output_bus);
|
| +
|
| + if (!IsInitialized()) {
|
| + output_bus->Zero();
|
| + return;
|
| + }
|
| +
|
| + AudioBuffer* input_buffer = Input(0).IsConnected()
|
| + ? AudioBuffer::CreateFromAudioBus(Input(0).Bus())
|
| + : AudioBuffer::Create(1, 128, Context()->sampleRate());
|
| + AudioBuffer* output_buffer =
|
| + AudioBuffer::Create(1, 128, Context()->sampleRate());
|
| +
|
| + processor_->Process(input_buffer, output_buffer);
|
| +
|
| + for (unsigned i = 0; i < 1; ++i) {
|
| + memcpy(output_bus->Channel(i)->MutableData(),
|
| + output_buffer->getChannelData(i).View()->Data(),
|
| + sizeof(float) * frames_to_process);
|
| + }
|
| +}
|
| +
|
| +void AudioWorkletHandler::SetProcessor(AudioWorkletProcessor* processor) {
|
| + DCHECK(!IsMainThread());
|
| + processor_ = processor;
|
| +}
|
| +
|
| +// ----------------------------------------------------------------
|
| +
|
| +AudioWorkletNode::AudioWorkletNode(BaseAudioContext& context,
|
| + const String& name)
|
| + : AudioNode(context), name_(name) {
|
| + SetHandler(AudioWorkletHandler::Create(*this, context.sampleRate(), name));
|
| +}
|
| +
|
| +AudioWorkletNode* AudioWorkletNode::Create(BaseAudioContext* context,
|
| + const String& name,
|
| + ExceptionState& exception_state) {
|
| + DCHECK(IsMainThread());
|
| +
|
| + if (context->IsContextClosed()) {
|
| + context->ThrowExceptionForClosedState(exception_state);
|
| + return nullptr;
|
| + }
|
| +
|
| + AudioWorkletNode* node = new AudioWorkletNode(*context, name);
|
| +
|
| + if (!node)
|
| + return nullptr;
|
| +
|
| + // Do something with node.
|
| + // node->handleChannelOptions(options, exception_state);
|
| +
|
| + return node;
|
| +}
|
| +
|
| +bool AudioWorkletNode::HasPendingActivity() const {
|
| + return !context()->IsContextClosed();
|
| +}
|
| +
|
| +AudioParamMap* AudioWorkletNode::parameters() const {
|
| + return parameter_map_;
|
| +}
|
| +
|
| +// void AudioWorkletNode::AddAudioParam(
|
| +// const String& param_name, AudioParam* audio_param) {
|
| +// parameter_map_->AddParameter(param_name, audio_param);
|
| +// }
|
| +
|
| +AudioWorkletHandler& AudioWorkletNode::GetWorkletHandler() const {
|
| + return static_cast<AudioWorkletHandler&>(Handler());
|
| +}
|
| +
|
| +DEFINE_TRACE(AudioWorkletNode) {
|
| + visitor->Trace(parameter_map_);
|
| + AudioNode::Trace(visitor);
|
| +}
|
| +
|
| +} // namespace blink
|
|
|