2 * Copyright (C) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "AudioScheduledSourceNode.h"
31 #include "AudioContext.h"
32 #include "AudioUtilities.h"
34 #include <wtf/MathExtras.h>
40 const double AudioScheduledSourceNode::UnknownTime = -1;
42 AudioScheduledSourceNode::AudioScheduledSourceNode(AudioContext* context, float sampleRate)
43 : AudioSourceNode(context, sampleRate)
44 , m_playbackState(UNSCHEDULED_STATE)
46 , m_endTime(UnknownTime)
50 void AudioScheduledSourceNode::updateSchedulingInfo(size_t quantumFrameSize,
52 size_t& quantumFrameOffset,
53 size_t& nonSilentFramesToProcess)
59 ASSERT(quantumFrameSize == AudioNode::ProcessingSizeInFrames);
60 if (quantumFrameSize != AudioNode::ProcessingSizeInFrames)
63 double sampleRate = this->sampleRate();
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);
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)
78 if (m_playbackState == UNSCHEDULED_STATE || m_playbackState == FINISHED_STATE || startFrame >= quantumEndFrame) {
81 nonSilentFramesToProcess = 0;
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();
92 quantumFrameOffset = startFrame > quantumStartFrame ? startFrame - quantumStartFrame : 0;
93 quantumFrameOffset = min(quantumFrameOffset, quantumFrameSize); // clamp to valid range
94 nonSilentFramesToProcess = quantumFrameSize - quantumFrameOffset;
96 if (!nonSilentFramesToProcess) {
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);
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;
116 bool isSafe = zeroStartFrame < quantumFrameSize && framesToZero <= quantumFrameSize && zeroStartFrame + framesToZero <= quantumFrameSize;
120 if (framesToZero > nonSilentFramesToProcess)
121 nonSilentFramesToProcess = 0;
123 nonSilentFramesToProcess -= framesToZero;
125 for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
126 memset(outputBus->channel(i)->mutableData() + zeroStartFrame, 0, sizeof(float) * framesToZero);
135 #if ENABLE(TIZEN_WEB_AUDIO)
136 void AudioScheduledSourceNode::start(double when, ExceptionCode& ec)
138 ASSERT(isMainThread());
139 if (m_playbackState != UNSCHEDULED_STATE) {
140 ec = INVALID_STATE_ERR;
145 m_playbackState = SCHEDULED_STATE;
148 void AudioScheduledSourceNode::stop(double when, ExceptionCode& ec)
150 ASSERT(isMainThread());
151 if (!(m_playbackState == SCHEDULED_STATE || m_playbackState == PLAYING_STATE) || (m_endTime != UnknownTime)) {
152 ec = INVALID_STATE_ERR;
156 when = max(0.0, when);
160 #if ENABLE(LEGACY_WEB_AUDIO)
161 void AudioScheduledSourceNode::noteOn(double when, ExceptionCode& ec)
166 void AudioScheduledSourceNode::noteOff(double when, ExceptionCode& ec)
172 void AudioScheduledSourceNode::start(double when)
174 ASSERT(isMainThread());
175 if (m_playbackState != UNSCHEDULED_STATE)
179 m_playbackState = SCHEDULED_STATE;
182 void AudioScheduledSourceNode::stop(double when)
184 ASSERT(isMainThread());
185 if (!(m_playbackState == SCHEDULED_STATE || m_playbackState == PLAYING_STATE))
188 when = max(0.0, when);
192 #if ENABLE(LEGACY_WEB_AUDIO)
193 void AudioScheduledSourceNode::noteOn(double when)
198 void AudioScheduledSourceNode::noteOff(double when)
205 void AudioScheduledSourceNode::finish()
207 if (m_playbackState != FINISHED_STATE) {
208 // Let the context dereference this AudioNode.
209 context()->notifyNodeFinishedProcessing(this);
210 m_playbackState = FINISHED_STATE;
211 context()->decrementActiveSourceCount();
215 } // namespace WebCore
217 #endif // ENABLE(WEB_AUDIO)