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 : #ifndef AudioParamTimeline_h_
8 : #define AudioParamTimeline_h_
9 :
10 : #include "AudioEventTimeline.h"
11 : #include "mozilla/ErrorResult.h"
12 : #include "MediaStreamGraph.h"
13 : #include "AudioSegment.h"
14 :
15 : namespace mozilla {
16 :
17 : namespace dom {
18 :
19 : // This helper class is used to represent the part of the AudioParam
20 : // class that gets sent to AudioNodeEngine instances. In addition to
21 : // AudioEventTimeline methods, it holds a pointer to an optional
22 : // MediaStream which represents the AudioNode inputs to the AudioParam.
23 : // This MediaStream is managed by the AudioParam subclass on the main
24 : // thread, and can only be obtained from the AudioNodeEngine instances
25 : // consuming this class.
26 0 : class AudioParamTimeline : public AudioEventTimeline
27 : {
28 : typedef AudioEventTimeline BaseClass;
29 :
30 : public:
31 0 : explicit AudioParamTimeline(float aDefaultValue)
32 0 : : BaseClass(aDefaultValue)
33 : {
34 0 : }
35 :
36 : MediaStream* Stream() const
37 : {
38 : return mStream;
39 : }
40 :
41 0 : bool HasSimpleValue() const
42 : {
43 0 : return BaseClass::HasSimpleValue() && !mStream;
44 : }
45 :
46 : template<class TimeType>
47 0 : float GetValueAtTime(TimeType aTime)
48 : {
49 0 : return GetValueAtTime(aTime, 0);
50 : }
51 :
52 : template<typename TimeType>
53 0 : void InsertEvent(const AudioTimelineEvent& aEvent)
54 : {
55 0 : if (aEvent.mType == AudioTimelineEvent::Cancel) {
56 0 : CancelScheduledValues(aEvent.template Time<TimeType>());
57 0 : return;
58 : }
59 0 : if (aEvent.mType == AudioTimelineEvent::Stream) {
60 0 : mStream = aEvent.mStream;
61 0 : return;
62 : }
63 0 : if (aEvent.mType == AudioTimelineEvent::SetValue) {
64 0 : AudioEventTimeline::SetValue(aEvent.mValue);
65 0 : return;
66 : }
67 0 : AudioEventTimeline::InsertEvent<TimeType>(aEvent);
68 : }
69 :
70 : // Get the value of the AudioParam at time aTime + aCounter.
71 : // aCounter here is an offset to aTime if we try to get the value in ticks,
72 : // otherwise it should always be zero. aCounter is meant to be used when
73 : template<class TimeType>
74 : float GetValueAtTime(TimeType aTime, size_t aCounter);
75 :
76 : // Get the values of the AudioParam at time aTime + (0 to aSize).
77 : // aBuffer must have the correct aSize.
78 : // aSize here is an offset to aTime if we try to get the value in ticks,
79 : // otherwise it should always be zero. aSize is meant to be used when
80 : // getting the value of an a-rate AudioParam for each tick inside an
81 : // AudioNodeEngine implementation.
82 : template<class TimeType>
83 : void GetValuesAtTime(TimeType aTime, float* aBuffer, const size_t aSize);
84 :
85 0 : virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
86 : {
87 0 : return mStream ? mStream->SizeOfIncludingThis(aMallocSizeOf) : 0;
88 : }
89 :
90 0 : virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
91 : {
92 0 : return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
93 : }
94 :
95 :
96 : private:
97 : float AudioNodeInputValue(size_t aCounter) const;
98 :
99 : protected:
100 : // This is created lazily when needed.
101 : RefPtr<MediaStream> mStream;
102 : };
103 :
104 : template<> inline float
105 0 : AudioParamTimeline::GetValueAtTime(double aTime, size_t aCounter)
106 : {
107 0 : MOZ_ASSERT(!aCounter);
108 :
109 : // Getting an AudioParam value on an AudioNode does not consider input from
110 : // other AudioNodes, which is managed only on the graph thread.
111 0 : return BaseClass::GetValueAtTime(aTime);
112 : }
113 :
114 : template<> inline float
115 0 : AudioParamTimeline::GetValueAtTime(int64_t aTime, size_t aCounter)
116 : {
117 0 : MOZ_ASSERT(aCounter < WEBAUDIO_BLOCK_SIZE);
118 0 : MOZ_ASSERT(!aCounter || !HasSimpleValue());
119 :
120 : // Mix the value of the AudioParam itself with that of the AudioNode inputs.
121 0 : return BaseClass::GetValueAtTime(static_cast<int64_t>(aTime + aCounter)) +
122 0 : (mStream ? AudioNodeInputValue(aCounter) : 0.0f);
123 : }
124 :
125 : template<> inline void
126 : AudioParamTimeline::GetValuesAtTime(double aTime, float* aBuffer,
127 : const size_t aSize)
128 : {
129 : MOZ_ASSERT(aBuffer);
130 : MOZ_ASSERT(aSize == 1);
131 :
132 : // Getting an AudioParam value on an AudioNode does not consider input from
133 : // other AudioNodes, which is managed only on the graph thread.
134 : *aBuffer = BaseClass::GetValueAtTime(aTime);
135 : }
136 :
137 : template<> inline void
138 0 : AudioParamTimeline::GetValuesAtTime(int64_t aTime, float* aBuffer,
139 : const size_t aSize)
140 : {
141 0 : MOZ_ASSERT(aBuffer);
142 0 : MOZ_ASSERT(aSize <= WEBAUDIO_BLOCK_SIZE);
143 0 : MOZ_ASSERT(aSize == 1 || !HasSimpleValue());
144 :
145 : // Mix the value of the AudioParam itself with that of the AudioNode inputs.
146 0 : BaseClass::GetValuesAtTime(aTime, aBuffer, aSize);
147 0 : if (mStream) {
148 0 : for (size_t i = 0; i < aSize; ++i) {
149 0 : aBuffer[i] += AudioNodeInputValue(i);
150 : }
151 : }
152 0 : }
153 :
154 : } // namespace dom
155 : } // namespace mozilla
156 :
157 : #endif
|