Modified: trunk/Source/WebCore/ChangeLog (87012 => 87013)
--- trunk/Source/WebCore/ChangeLog 2011-05-21 08:47:59 UTC (rev 87012)
+++ trunk/Source/WebCore/ChangeLog 2011-05-21 09:42:08 UTC (rev 87013)
@@ -1,3 +1,21 @@
+2011-05-21 Chris Rogers <[email protected]>
+
+ Reviewed by Kenneth Russell.
+
+ AudioBufferSourceNode noteOff() method must respect scheduling time
+ https://bugs.webkit.org/show_bug.cgi?id=61226
+
+ No new tests since audio API is not yet implemented.
+
+ * webaudio/AudioBufferSourceNode.cpp:
+ (WebCore::AudioBufferSourceNode::AudioBufferSourceNode):
+ (WebCore::AudioBufferSourceNode::process):
+ (WebCore::AudioBufferSourceNode::provideInput):
+ (WebCore::AudioBufferSourceNode::finish):
+ (WebCore::AudioBufferSourceNode::noteGrainOn):
+ (WebCore::AudioBufferSourceNode::noteOff):
+ * webaudio/AudioBufferSourceNode.h:
+
2011-05-20 Dirk Schulze <[email protected]>
Reviewed by Nikolas Zimmermann.
Modified: trunk/Source/WebCore/webaudio/AudioBufferSourceNode.cpp (87012 => 87013)
--- trunk/Source/WebCore/webaudio/AudioBufferSourceNode.cpp 2011-05-21 08:47:59 UTC (rev 87012)
+++ trunk/Source/WebCore/webaudio/AudioBufferSourceNode.cpp 2011-05-21 09:42:08 UTC (rev 87013)
@@ -38,6 +38,7 @@
namespace WebCore {
const double DefaultGrainDuration = 0.020; // 20ms
+const double UnknownTime = -1;
PassRefPtr<AudioBufferSourceNode> AudioBufferSourceNode::create(AudioContext* context, double sampleRate)
{
@@ -51,6 +52,7 @@
, m_isLooping(false)
, m_hasFinished(false)
, m_startTime(0.0)
+ , m_endTime(UnknownTime)
, m_schedulingFrameDelay(0)
, m_readIndex(0)
, m_isGrain(false)
@@ -94,6 +96,13 @@
double quantumStartTime = context()->currentTime();
double quantumEndTime = quantumStartTime + framesToProcess / sampleRate;
+ // If we know the end time and it's already passed, then don't bother doing any more rendering this cycle.
+ if (m_endTime != UnknownTime && m_endTime <= quantumStartTime) {
+ m_isPlaying = false;
+ m_readIndex = 0;
+ finish();
+ }
+
if (!m_isPlaying || m_hasFinished || !buffer() || m_startTime >= quantumEndTime) {
// FIXME: can optimize here by propagating silent hint instead of forcing the whole chain to process silence.
outputBus->zero();
@@ -127,6 +136,25 @@
double totalGain = gain()->value() * m_buffer->gain();
outputBus->copyWithGainFrom(*outputBus, &m_lastGain, totalGain);
+ // If the end time is somewhere in the middle of this time quantum, then simply zero out the
+ // frames starting at the end time.
+ if (m_endTime != UnknownTime && m_endTime >= quantumStartTime && m_endTime < quantumEndTime) {
+ unsigned zeroStartFrame = (m_endTime - quantumStartTime) * sampleRate;
+ unsigned framesToZero = framesToProcess - zeroStartFrame;
+
+ bool isSafe = zeroStartFrame < framesToProcess && framesToZero <= framesToProcess && zeroStartFrame + framesToZero <= framesToProcess;
+ ASSERT(isSafe);
+
+ if (isSafe) {
+ for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
+ memset(outputBus->channel(i)->data() + zeroStartFrame, 0, sizeof(float) * framesToZero);
+ }
+
+ m_isPlaying = false;
+ m_readIndex = 0;
+ finish();
+ }
+
m_processLock.unlock();
} else {
// Too bad - the tryLock() failed. We must be in the middle of changing buffers and were already outputting silence anyway.
@@ -249,11 +277,7 @@
memset(destinationR, 0, sizeof(float) * framesToProcess);
}
- if (!m_hasFinished) {
- // Let the context dereference this AudioNode.
- context()->notifyNodeFinishedProcessing(this);
- m_hasFinished = true;
- }
+ finish();
return;
}
}
@@ -342,6 +366,15 @@
m_lastGain = gain()->value();
}
+void AudioBufferSourceNode::finish()
+{
+ if (!m_hasFinished) {
+ // Let the context dereference this AudioNode.
+ context()->notifyNodeFinishedProcessing(this);
+ m_hasFinished = true;
+ }
+}
+
void AudioBufferSourceNode::setBuffer(AudioBuffer* buffer)
{
ASSERT(isMainThread());
@@ -399,7 +432,7 @@
maxGrainOffset = max(0.0, maxGrainOffset);
grainOffset = max(0.0, grainOffset);
- grainOffset = min(maxGrainOffset, grainOffset);
+ grainOffset = min(maxGrainOffset, grainOffset);
m_grainOffset = grainOffset;
m_grainDuration = grainDuration;
@@ -411,15 +444,14 @@
m_isPlaying = true;
}
-void AudioBufferSourceNode::noteOff(double)
+void AudioBufferSourceNode::noteOff(double when)
{
ASSERT(isMainThread());
if (!m_isPlaying)
return;
-
- // FIXME: the "when" argument to this method is ignored.
- m_isPlaying = false;
- m_readIndex = 0;
+
+ when = max(0.0, when);
+ m_endTime = when;
}
double AudioBufferSourceNode::totalPitchRate()
Modified: trunk/Source/WebCore/webaudio/AudioBufferSourceNode.h (87012 => 87013)
--- trunk/Source/WebCore/webaudio/AudioBufferSourceNode.h 2011-05-21 08:47:59 UTC (rev 87012)
+++ trunk/Source/WebCore/webaudio/AudioBufferSourceNode.h 2011-05-21 09:42:08 UTC (rev 87013)
@@ -104,6 +104,11 @@
// m_startTime is the time to start playing based on the context's timeline (0.0 or a time less than the context's current time means "now").
double m_startTime; // in seconds
+ // m_endTime is the time to stop playing based on the context's timeline (0.0 or a time less than the context's current time means "now").
+ // If it hasn't been set explicitly, then the sound will not stop playing (if looping) or will stop when the end of the AudioBuffer
+ // has been reached.
+ double m_endTime; // in seconds
+
// m_schedulingFrameDelay is the sample-accurate scheduling offset.
// It's used so that we start rendering audio samples at a very precise point in time.
// It will only be a non-zero value the very first render quantum that we render from the buffer.
@@ -140,6 +145,9 @@
// readFromBufferWithGrainEnvelope() is a low-level blitter which reads from the AudioBuffer and applies a grain envelope.
void readFromBufferWithGrainEnvelope(float* sourceL, float* sourceR, float* destinationL, float* destinationR, size_t framesToProcess);
+
+ // Handles the time when we reach the end of sample data (non-looping) or the noteOff() time has been reached.
+ void finish();
};
} // namespace WebCore