AudioParam.h 5.8 KB

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