2 * Copyright (C) 2010 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "AudioContext.h"
33 #include "AudioParamTimeline.h"
34 #include "Float32Array.h"
35 #include "PlatformString.h"
36 #include <sys/types.h>
37 #include <wtf/PassRefPtr.h>
38 #include <wtf/RefCounted.h>
42 class AudioParam : public RefCounted<AudioParam> {
44 static const double DefaultSmoothingConstant;
45 static const double SnapThreshold;
47 static PassRefPtr<AudioParam> create(const String& name, double defaultValue, double minValue, double maxValue, unsigned units = 0)
49 return adoptRef(new AudioParam(name, defaultValue, minValue, maxValue, units));
52 AudioParam(const String& name, double defaultValue, double minValue, double maxValue, unsigned units = 0)
54 , m_value(defaultValue)
55 , m_defaultValue(defaultValue)
56 , m_minValue(minValue)
57 , m_maxValue(maxValue)
59 , m_smoothedValue(defaultValue)
60 , m_smoothingConstant(DefaultSmoothingConstant)
64 void setContext(AudioContext* context) { m_context = context; }
65 AudioContext* context() { return m_context.get(); }
71 String name() const { return m_name; }
73 float minValue() const { return static_cast<float>(m_minValue); }
74 float maxValue() const { return static_cast<float>(m_maxValue); }
75 float defaultValue() const { return static_cast<float>(m_defaultValue); }
76 unsigned units() const { return m_units; }
80 // When a new value is set with setValue(), in our internal use of the parameter we don't immediately jump to it.
81 // Instead we smoothly approach this value to avoid glitching.
82 float smoothedValue();
84 // Smoothly exponentially approaches to (de-zippers) the desired value.
85 // Returns true if smoothed value has already snapped exactly to value.
88 void resetSmoothedValue() { m_smoothedValue = m_value; }
89 void setSmoothingConstant(double k) { m_smoothingConstant = k; }
91 // Parameter automation.
92 void setValueAtTime(float value, float time) { m_timeline.setValueAtTime(value, time); }
93 void linearRampToValueAtTime(float value, float time) { m_timeline.linearRampToValueAtTime(value, time); }
94 void exponentialRampToValueAtTime(float value, float time) { m_timeline.exponentialRampToValueAtTime(value, time); }
95 void setTargetValueAtTime(float targetValue, float time, float timeConstant) { m_timeline.setTargetValueAtTime(targetValue, time, timeConstant); }
96 void setValueCurveAtTime(Float32Array* curve, float time, float duration) { m_timeline.setValueCurveAtTime(curve, time, duration); }
97 void cancelScheduledValues(float startTime) { m_timeline.cancelScheduledValues(startTime); }
99 bool hasTimelineValues() { return m_timeline.hasValues(); }
101 // Calculates numberOfValues parameter values starting at the context's current time.
102 // Must be called in the context's render thread.
103 void calculateSampleAccurateValues(float* values, unsigned numberOfValues);
106 RefPtr<AudioContext> m_context;
109 double m_defaultValue;
114 // Smoothing (de-zippering)
115 double m_smoothedValue;
116 double m_smoothingConstant;
118 AudioParamTimeline m_timeline;
121 } // namespace WebCore
123 #endif // AudioParam_h