1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/ChannelMergerNode.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "mozilla/dom/ChannelMergerNode.h" 1.11 +#include "mozilla/dom/ChannelMergerNodeBinding.h" 1.12 +#include "AudioNodeEngine.h" 1.13 +#include "AudioNodeStream.h" 1.14 + 1.15 +namespace mozilla { 1.16 +namespace dom { 1.17 + 1.18 +NS_IMPL_ISUPPORTS_INHERITED0(ChannelMergerNode, AudioNode) 1.19 + 1.20 +class ChannelMergerNodeEngine : public AudioNodeEngine 1.21 +{ 1.22 +public: 1.23 + ChannelMergerNodeEngine(ChannelMergerNode* aNode) 1.24 + : AudioNodeEngine(aNode) 1.25 + { 1.26 + MOZ_ASSERT(NS_IsMainThread()); 1.27 + } 1.28 + 1.29 + virtual void ProcessBlocksOnPorts(AudioNodeStream* aStream, 1.30 + const OutputChunks& aInput, 1.31 + OutputChunks& aOutput, 1.32 + bool* aFinished) MOZ_OVERRIDE 1.33 + { 1.34 + MOZ_ASSERT(aInput.Length() >= 1, "Should have one or more input ports"); 1.35 + 1.36 + // Get the number of output channels, and allocate it 1.37 + uint32_t channelCount = 0; 1.38 + for (uint16_t i = 0; i < InputCount(); ++i) { 1.39 + channelCount += aInput[i].mChannelData.Length(); 1.40 + } 1.41 + if (channelCount == 0) { 1.42 + aOutput[0].SetNull(WEBAUDIO_BLOCK_SIZE); 1.43 + return; 1.44 + } 1.45 + channelCount = std::min(channelCount, WebAudioUtils::MaxChannelCount); 1.46 + AllocateAudioBlock(channelCount, &aOutput[0]); 1.47 + 1.48 + // Append each channel in each input to the output 1.49 + uint32_t channelIndex = 0; 1.50 + for (uint16_t i = 0; true; ++i) { 1.51 + MOZ_ASSERT(i < InputCount()); 1.52 + for (uint32_t j = 0; j < aInput[i].mChannelData.Length(); ++j) { 1.53 + AudioBlockCopyChannelWithScale( 1.54 + static_cast<const float*>(aInput[i].mChannelData[j]), 1.55 + aInput[i].mVolume, 1.56 + static_cast<float*>(const_cast<void*>(aOutput[0].mChannelData[channelIndex]))); 1.57 + ++channelIndex; 1.58 + if (channelIndex >= channelCount) { 1.59 + return; 1.60 + } 1.61 + } 1.62 + } 1.63 + } 1.64 + 1.65 + virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE 1.66 + { 1.67 + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); 1.68 + } 1.69 +}; 1.70 + 1.71 +ChannelMergerNode::ChannelMergerNode(AudioContext* aContext, 1.72 + uint16_t aInputCount) 1.73 + : AudioNode(aContext, 1.74 + 2, 1.75 + ChannelCountMode::Max, 1.76 + ChannelInterpretation::Speakers) 1.77 + , mInputCount(aInputCount) 1.78 +{ 1.79 + mStream = aContext->Graph()->CreateAudioNodeStream(new ChannelMergerNodeEngine(this), 1.80 + MediaStreamGraph::INTERNAL_STREAM); 1.81 +} 1.82 + 1.83 +JSObject* 1.84 +ChannelMergerNode::WrapObject(JSContext* aCx) 1.85 +{ 1.86 + return ChannelMergerNodeBinding::Wrap(aCx, this); 1.87 +} 1.88 + 1.89 +} 1.90 +} 1.91 +