content/media/webaudio/AudioParamTimeline.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:152a1951b5d9
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 #ifndef AudioParamTimeline_h_
8 #define AudioParamTimeline_h_
9
10 #include "AudioEventTimeline.h"
11 #include "mozilla/ErrorResult.h"
12 #include "nsAutoPtr.h"
13 #include "MediaStreamGraph.h"
14 #include "AudioSegment.h"
15
16 namespace mozilla {
17
18 namespace dom {
19
20 // This helper class is used to represent the part of the AudioParam
21 // class that gets sent to AudioNodeEngine instances. In addition to
22 // AudioEventTimeline methods, it holds a pointer to an optional
23 // MediaStream which represents the AudioNode inputs to the AudioParam.
24 // This MediaStream is managed by the AudioParam subclass on the main
25 // thread, and can only be obtained from the AudioNodeEngine instances
26 // consuming this class.
27 class AudioParamTimeline : public AudioEventTimeline<ErrorResult>
28 {
29 typedef AudioEventTimeline<ErrorResult> BaseClass;
30
31 public:
32 explicit AudioParamTimeline(float aDefaultValue)
33 : BaseClass(aDefaultValue)
34 {
35 }
36
37 MediaStream* Stream() const
38 {
39 return mStream;
40 }
41
42 bool HasSimpleValue() const
43 {
44 return BaseClass::HasSimpleValue() && !mStream;
45 }
46
47 // Get the value of the AudioParam at time aTime + aCounter.
48 // aCounter here is an offset to aTime if we try to get the value in ticks,
49 // otherwise it should always be zero. aCounter is meant to be used when
50 // getting the value of an a-rate AudioParam for each tick inside an
51 // AudioNodeEngine implementation.
52 template<class TimeType>
53 float GetValueAtTime(TimeType aTime, size_t aCounter = 0);
54
55 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
56 {
57 return mStream ? mStream->SizeOfIncludingThis(aMallocSizeOf) : 0;
58 }
59
60 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
61 {
62 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
63 }
64
65
66 private:
67 float AudioNodeInputValue(size_t aCounter) const;
68
69 protected:
70 // This is created lazily when needed.
71 nsRefPtr<MediaStream> mStream;
72 };
73
74 template<> inline float
75 AudioParamTimeline::GetValueAtTime(double aTime, size_t aCounter)
76 {
77 MOZ_ASSERT(!aCounter);
78
79 // Getting an AudioParam value on an AudioNode does not consider input from
80 // other AudioNodes, which is managed only on the graph thread.
81 return BaseClass::GetValueAtTime(aTime);
82 }
83
84
85 template<> inline float
86 AudioParamTimeline::GetValueAtTime(int64_t aTime, size_t aCounter)
87 {
88 MOZ_ASSERT(aCounter < WEBAUDIO_BLOCK_SIZE);
89 MOZ_ASSERT(!aCounter || !HasSimpleValue());
90
91 // Mix the value of the AudioParam itself with that of the AudioNode inputs.
92 return BaseClass::GetValueAtTime(static_cast<int64_t>(aTime + aCounter)) +
93 (mStream ? AudioNodeInputValue(aCounter) : 0.0f);
94 }
95
96 }
97 }
98
99 #endif
100

mercurial