Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "mozilla/dom/ChannelSplitterNode.h"
8 : #include "mozilla/dom/ChannelSplitterNodeBinding.h"
9 : #include "AudioNodeEngine.h"
10 : #include "AudioNodeStream.h"
11 :
12 : namespace mozilla {
13 : namespace dom {
14 :
15 0 : NS_IMPL_ISUPPORTS_INHERITED0(ChannelSplitterNode, AudioNode)
16 :
17 0 : class ChannelSplitterNodeEngine final : public AudioNodeEngine
18 : {
19 : public:
20 0 : explicit ChannelSplitterNodeEngine(ChannelSplitterNode* aNode)
21 0 : : AudioNodeEngine(aNode)
22 : {
23 0 : MOZ_ASSERT(NS_IsMainThread());
24 0 : }
25 :
26 0 : void ProcessBlocksOnPorts(AudioNodeStream* aStream,
27 : const OutputChunks& aInput,
28 : OutputChunks& aOutput,
29 : bool* aFinished) override
30 : {
31 0 : MOZ_ASSERT(aInput.Length() == 1, "Should only have one input port");
32 :
33 0 : aOutput.SetLength(OutputCount());
34 0 : for (uint16_t i = 0; i < OutputCount(); ++i) {
35 0 : if (i < aInput[0].ChannelCount()) {
36 : // Split out existing channels
37 0 : aOutput[i].AllocateChannels(1);
38 0 : AudioBlockCopyChannelWithScale(
39 0 : static_cast<const float*>(aInput[0].mChannelData[i]),
40 0 : aInput[0].mVolume,
41 0 : aOutput[i].ChannelFloatsForWrite(0));
42 : } else {
43 : // Pad with silent channels if needed
44 0 : aOutput[i].SetNull(WEBAUDIO_BLOCK_SIZE);
45 : }
46 : }
47 0 : }
48 :
49 0 : size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
50 : {
51 0 : return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
52 : }
53 : };
54 :
55 0 : ChannelSplitterNode::ChannelSplitterNode(AudioContext* aContext,
56 0 : uint16_t aOutputCount)
57 : : AudioNode(aContext,
58 : 2,
59 : ChannelCountMode::Max,
60 : ChannelInterpretation::Speakers)
61 0 : , mOutputCount(aOutputCount)
62 : {
63 0 : mStream = AudioNodeStream::Create(aContext,
64 0 : new ChannelSplitterNodeEngine(this),
65 : AudioNodeStream::NO_STREAM_FLAGS,
66 0 : aContext->Graph());
67 0 : }
68 :
69 : /* static */ already_AddRefed<ChannelSplitterNode>
70 0 : ChannelSplitterNode::Create(AudioContext& aAudioContext,
71 : const ChannelSplitterOptions& aOptions,
72 : ErrorResult& aRv)
73 : {
74 0 : if (aAudioContext.CheckClosed(aRv)) {
75 0 : return nullptr;
76 : }
77 :
78 0 : if (aOptions.mNumberOfOutputs == 0 ||
79 0 : aOptions.mNumberOfOutputs > WebAudioUtils::MaxChannelCount) {
80 0 : aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
81 0 : return nullptr;
82 : }
83 :
84 : RefPtr<ChannelSplitterNode> audioNode =
85 0 : new ChannelSplitterNode(&aAudioContext, aOptions.mNumberOfOutputs);
86 :
87 0 : audioNode->Initialize(aOptions, aRv);
88 0 : if (NS_WARN_IF(aRv.Failed())) {
89 0 : return nullptr;
90 : }
91 :
92 0 : return audioNode.forget();
93 : }
94 :
95 : JSObject*
96 0 : ChannelSplitterNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
97 : {
98 0 : return ChannelSplitterNodeBinding::Wrap(aCx, this, aGivenProto);
99 : }
100 :
101 : } // namespace dom
102 : } // namespace mozilla
|