4cfcc13264e99deec21597fb1609d7b185359ba8
[framework/web/webkit-efl.git] / Source / WebCore / Modules / webaudio / AudioScheduledSourceNode.cpp
1 /*
2  * Copyright (C) 2012, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #if ENABLE(WEB_AUDIO)
28
29 #include "AudioScheduledSourceNode.h"
30
31 #include "AudioContext.h"
32 #include "AudioUtilities.h"
33 #include <algorithm>
34 #include <wtf/MathExtras.h>
35
36 using namespace std;
37
38 namespace WebCore {
39
40 const double AudioScheduledSourceNode::UnknownTime = -1;
41
42 AudioScheduledSourceNode::AudioScheduledSourceNode(AudioContext* context, float sampleRate)
43     : AudioSourceNode(context, sampleRate)
44     , m_playbackState(UNSCHEDULED_STATE)
45     , m_startTime(0)
46     , m_endTime(UnknownTime)
47 {
48 }
49
50 void AudioScheduledSourceNode::updateSchedulingInfo(size_t quantumFrameSize,
51                                                     AudioBus* outputBus,
52                                                     size_t& quantumFrameOffset,
53                                                     size_t& nonSilentFramesToProcess)
54 {
55     ASSERT(outputBus);
56     if (!outputBus)
57         return;
58
59     ASSERT(quantumFrameSize == AudioNode::ProcessingSizeInFrames);
60     if (quantumFrameSize != AudioNode::ProcessingSizeInFrames)
61         return;
62
63     double sampleRate = this->sampleRate();
64
65     // quantumStartFrame     : Start frame of the current time quantum.
66     // quantumEndFrame       : End frame of the current time quantum.
67     // startFrame            : Start frame for this source.
68     // endFrame              : End frame for this source.
69     size_t quantumStartFrame = context()->currentSampleFrame();
70     size_t quantumEndFrame = quantumStartFrame + quantumFrameSize;
71     size_t startFrame = AudioUtilities::timeToSampleFrame(m_startTime, sampleRate);
72     size_t endFrame = m_endTime == UnknownTime ? 0 : AudioUtilities::timeToSampleFrame(m_endTime, sampleRate);
73
74     // If we know the end time and it's already passed, then don't bother doing any more rendering this cycle.
75     if (m_endTime != UnknownTime && endFrame <= quantumStartFrame)
76         finish();
77
78     if (m_playbackState == UNSCHEDULED_STATE || m_playbackState == FINISHED_STATE || startFrame >= quantumEndFrame) {
79         // Output silence.
80         outputBus->zero();
81         nonSilentFramesToProcess = 0;
82         return;
83     }
84
85     // Check if it's time to start playing.
86     if (m_playbackState == SCHEDULED_STATE) {
87         // Increment the active source count only if we're transitioning from SCHEDULED_STATE to PLAYING_STATE.
88         m_playbackState = PLAYING_STATE;
89         context()->incrementActiveSourceCount();
90     }
91
92     quantumFrameOffset = startFrame > quantumStartFrame ? startFrame - quantumStartFrame : 0;
93     quantumFrameOffset = min(quantumFrameOffset, quantumFrameSize); // clamp to valid range
94     nonSilentFramesToProcess = quantumFrameSize - quantumFrameOffset;
95
96     if (!nonSilentFramesToProcess) {
97         // Output silence.
98         outputBus->zero();
99         return;
100     }
101
102     // Handle silence before we start playing.
103     // Zero any initial frames representing silence leading up to a rendering start time in the middle of the quantum.
104     if (quantumFrameOffset) {
105         for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
106             memset(outputBus->channel(i)->mutableData(), 0, sizeof(float) * quantumFrameOffset);
107     }
108
109     // Handle silence after we're done playing.
110     // If the end time is somewhere in the middle of this time quantum, then zero out the
111     // frames from the end time to the very end of the quantum.
112     if (m_endTime != UnknownTime && endFrame >= quantumStartFrame && endFrame < quantumEndFrame) {
113         size_t zeroStartFrame = endFrame - quantumStartFrame;
114         size_t framesToZero = quantumFrameSize - zeroStartFrame;
115
116         bool isSafe = zeroStartFrame < quantumFrameSize && framesToZero <= quantumFrameSize && zeroStartFrame + framesToZero <= quantumFrameSize;
117         ASSERT(isSafe);
118
119         if (isSafe) {
120             if (framesToZero > nonSilentFramesToProcess)
121                 nonSilentFramesToProcess = 0;
122             else
123                 nonSilentFramesToProcess -= framesToZero;
124
125             for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
126                 memset(outputBus->channel(i)->mutableData() + zeroStartFrame, 0, sizeof(float) * framesToZero);
127         }
128
129         finish();
130     }
131
132     return;
133 }
134
135 #if ENABLE(TIZEN_WEB_AUDIO)
136 void AudioScheduledSourceNode::start(double when, ExceptionCode& ec)
137 {
138     ASSERT(isMainThread());
139     if (m_playbackState != UNSCHEDULED_STATE) {
140         ec = INVALID_STATE_ERR;
141         return;
142     }
143
144     m_startTime = when;
145     m_playbackState = SCHEDULED_STATE;
146
147     context()->destination()->startRendering();
148 }
149
150 void AudioScheduledSourceNode::stop(double when, ExceptionCode& ec)
151 {
152     ASSERT(isMainThread());
153     if (!(m_playbackState == SCHEDULED_STATE || m_playbackState == PLAYING_STATE) || (m_endTime != UnknownTime)) {
154         ec = INVALID_STATE_ERR;
155         return;
156     }
157
158     when = max(0.0, when);
159     m_endTime = when;
160 }
161
162 #if ENABLE(LEGACY_WEB_AUDIO)
163 void AudioScheduledSourceNode::noteOn(double when, ExceptionCode& ec)
164 {
165     start(when, ec);
166 }
167
168 void AudioScheduledSourceNode::noteOff(double when, ExceptionCode& ec)
169 {
170     stop(when, ec);
171 }
172 #endif
173 #else
174 void AudioScheduledSourceNode::start(double when)
175 {
176     ASSERT(isMainThread());
177     if (m_playbackState != UNSCHEDULED_STATE)
178         return;
179
180     m_startTime = when;
181     m_playbackState = SCHEDULED_STATE;
182 }
183
184 void AudioScheduledSourceNode::stop(double when)
185 {
186     ASSERT(isMainThread());
187     if (!(m_playbackState == SCHEDULED_STATE || m_playbackState == PLAYING_STATE))
188         return;
189     
190     when = max(0.0, when);
191     m_endTime = when;
192 }
193
194 #if ENABLE(LEGACY_WEB_AUDIO)
195 void AudioScheduledSourceNode::noteOn(double when)
196 {
197     start(when);
198 }
199
200 void AudioScheduledSourceNode::noteOff(double when)
201 {
202     stop(when);
203 }
204 #endif
205 #endif
206
207 void AudioScheduledSourceNode::finish()
208 {
209     if (m_playbackState != FINISHED_STATE) {
210         // Let the context dereference this AudioNode.
211         context()->notifyNodeFinishedProcessing(this);
212         m_playbackState = FINISHED_STATE;
213         context()->decrementActiveSourceCount();
214     }
215 }
216
217 } // namespace WebCore
218
219 #endif // ENABLE(WEB_AUDIO)