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