1/*
2 * Copyright (C) 2010 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 *
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 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.
16 *
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.
27 */
28
29#ifndef AudioParam_h
30#define AudioParam_h
31
32#include "AudioContext.h"
33#include "AudioParamTimeline.h"
34#include "AudioSummingJunction.h"
35#include <runtime/Float32Array.h>
36#include <sys/types.h>
37#include <wtf/PassRefPtr.h>
38#include <wtf/RefCounted.h>
39#include <wtf/text/WTFString.h>
40
41namespace WebCore {
42
43class AudioNodeOutput;
44
45class AudioParam : public AudioSummingJunction, public RefCounted<AudioParam> {
46public:
47    static const double DefaultSmoothingConstant;
48    static const double SnapThreshold;
49
50    static PassRefPtr<AudioParam> create(AudioContext* context, const String& name, double defaultValue, double minValue, double maxValue, unsigned units = 0)
51    {
52        return adoptRef(new AudioParam(context, name, defaultValue, minValue, maxValue, units));
53    }
54
55    // AudioSummingJunction
56    virtual bool canUpdateState() override { return true; }
57    virtual void didUpdate() override { }
58
59    // Intrinsic value.
60    float value();
61    void setValue(float);
62
63    // Final value for k-rate parameters, otherwise use calculateSampleAccurateValues() for a-rate.
64    // Must be called in the audio thread.
65    float finalValue();
66
67    String name() const { return m_name; }
68
69    float minValue() const { return static_cast<float>(m_minValue); }
70    float maxValue() const { return static_cast<float>(m_maxValue); }
71    float defaultValue() const { return static_cast<float>(m_defaultValue); }
72    unsigned units() const { return m_units; }
73
74    // Value smoothing:
75
76    // When a new value is set with setValue(), in our internal use of the parameter we don't immediately jump to it.
77    // Instead we smoothly approach this value to avoid glitching.
78    float smoothedValue();
79
80    // Smoothly exponentially approaches to (de-zippers) the desired value.
81    // Returns true if smoothed value has already snapped exactly to value.
82    bool smooth();
83
84    void resetSmoothedValue() { m_smoothedValue = m_value; }
85    void setSmoothingConstant(double k) { m_smoothingConstant = k; }
86
87    // Parameter automation.
88    void setValueAtTime(float value, float time) { m_timeline.setValueAtTime(value, time); }
89    void linearRampToValueAtTime(float value, float time) { m_timeline.linearRampToValueAtTime(value, time); }
90    void exponentialRampToValueAtTime(float value, float time) { m_timeline.exponentialRampToValueAtTime(value, time); }
91    void setTargetAtTime(float target, float time, float timeConstant) { m_timeline.setTargetAtTime(target, time, timeConstant); }
92    void setValueCurveAtTime(Float32Array* curve, float time, float duration) { m_timeline.setValueCurveAtTime(curve, time, duration); }
93    void cancelScheduledValues(float startTime) { m_timeline.cancelScheduledValues(startTime); }
94
95    bool hasSampleAccurateValues() { return m_timeline.hasValues() || numberOfRenderingConnections(); }
96
97    // Calculates numberOfValues parameter values starting at the context's current time.
98    // Must be called in the context's render thread.
99    void calculateSampleAccurateValues(float* values, unsigned numberOfValues);
100
101    // Connect an audio-rate signal to control this parameter.
102    void connect(AudioNodeOutput*);
103    void disconnect(AudioNodeOutput*);
104
105protected:
106    AudioParam(AudioContext* context, const String& name, double defaultValue, double minValue, double maxValue, unsigned units = 0)
107        : AudioSummingJunction(context)
108        , m_name(name)
109        , m_value(defaultValue)
110        , m_defaultValue(defaultValue)
111        , m_minValue(minValue)
112        , m_maxValue(maxValue)
113        , m_units(units)
114        , m_smoothedValue(defaultValue)
115        , m_smoothingConstant(DefaultSmoothingConstant)
116    {
117    }
118
119private:
120    // sampleAccurate corresponds to a-rate (audio rate) vs. k-rate in the Web Audio specification.
121    void calculateFinalValues(float* values, unsigned numberOfValues, bool sampleAccurate);
122    void calculateTimelineValues(float* values, unsigned numberOfValues);
123
124    String m_name;
125    double m_value;
126    double m_defaultValue;
127    double m_minValue;
128    double m_maxValue;
129    unsigned m_units;
130
131    // Smoothing (de-zippering)
132    double m_smoothedValue;
133    double m_smoothingConstant;
134
135    AudioParamTimeline m_timeline;
136};
137
138} // namespace WebCore
139
140#endif // AudioParam_h
141