PannerNode.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef PannerNode_h
  25. #define PannerNode_h
  26. #include "AudioBus.h"
  27. #include "AudioListener.h"
  28. #include "AudioNode.h"
  29. #include "AudioParam.h"
  30. #include "Cone.h"
  31. #include "Distance.h"
  32. #include "FloatPoint3D.h"
  33. #include "Panner.h"
  34. #include <wtf/OwnPtr.h>
  35. namespace WebCore {
  36. // PannerNode is an AudioNode with one input and one output.
  37. // It positions a sound in 3D space, with the exact effect dependent on the panning model.
  38. // It has a position and an orientation in 3D space which is relative to the position and orientation of the context's AudioListener.
  39. // A distance effect will attenuate the gain as the position moves away from the listener.
  40. // A cone effect will attenuate the gain as the orientation moves away from the listener.
  41. // All of these effects follow the OpenAL specification very closely.
  42. class PannerNode : public AudioNode {
  43. public:
  44. // These must be defined as in the .idl file and must match those in the Panner class.
  45. enum {
  46. EQUALPOWER = 0,
  47. HRTF = 1,
  48. SOUNDFIELD = 2,
  49. };
  50. // These must be defined as in the .idl file and must match those
  51. // in the DistanceEffect class.
  52. enum {
  53. LINEAR_DISTANCE = 0,
  54. INVERSE_DISTANCE = 1,
  55. EXPONENTIAL_DISTANCE = 2,
  56. };
  57. static PassRefPtr<PannerNode> create(AudioContext* context, float sampleRate)
  58. {
  59. return adoptRef(new PannerNode(context, sampleRate));
  60. }
  61. virtual ~PannerNode();
  62. // AudioNode
  63. virtual void process(size_t framesToProcess);
  64. virtual void pullInputs(size_t framesToProcess);
  65. virtual void reset();
  66. virtual void initialize();
  67. virtual void uninitialize();
  68. // Listener
  69. AudioListener* listener();
  70. // Panning model
  71. String panningModel() const;
  72. bool setPanningModel(unsigned); // Returns true on success.
  73. void setPanningModel(const String&);
  74. // Position
  75. FloatPoint3D position() const { return m_position; }
  76. void setPosition(float x, float y, float z) { m_position = FloatPoint3D(x, y, z); }
  77. // Orientation
  78. FloatPoint3D orientation() const { return m_position; }
  79. void setOrientation(float x, float y, float z) { m_orientation = FloatPoint3D(x, y, z); }
  80. // Velocity
  81. FloatPoint3D velocity() const { return m_velocity; }
  82. void setVelocity(float x, float y, float z) { m_velocity = FloatPoint3D(x, y, z); }
  83. // Distance parameters
  84. String distanceModel() const;
  85. bool setDistanceModel(unsigned); // Returns true on success.
  86. void setDistanceModel(const String&);
  87. double refDistance() { return m_distanceEffect.refDistance(); }
  88. void setRefDistance(double refDistance) { m_distanceEffect.setRefDistance(refDistance); }
  89. double maxDistance() { return m_distanceEffect.maxDistance(); }
  90. void setMaxDistance(double maxDistance) { m_distanceEffect.setMaxDistance(maxDistance); }
  91. double rolloffFactor() { return m_distanceEffect.rolloffFactor(); }
  92. void setRolloffFactor(double rolloffFactor) { m_distanceEffect.setRolloffFactor(rolloffFactor); }
  93. // Sound cones - angles in degrees
  94. double coneInnerAngle() const { return m_coneEffect.innerAngle(); }
  95. void setConeInnerAngle(double angle) { m_coneEffect.setInnerAngle(angle); }
  96. double coneOuterAngle() const { return m_coneEffect.outerAngle(); }
  97. void setConeOuterAngle(double angle) { m_coneEffect.setOuterAngle(angle); }
  98. double coneOuterGain() const { return m_coneEffect.outerGain(); }
  99. void setConeOuterGain(double angle) { m_coneEffect.setOuterGain(angle); }
  100. void getAzimuthElevation(double* outAzimuth, double* outElevation);
  101. float dopplerRate();
  102. // Accessors for dynamically calculated gain values.
  103. AudioParam* distanceGain() { return m_distanceGain.get(); }
  104. AudioParam* coneGain() { return m_coneGain.get(); }
  105. virtual double tailTime() const OVERRIDE { return m_panner ? m_panner->tailTime() : 0; }
  106. virtual double latencyTime() const OVERRIDE { return m_panner ? m_panner->latencyTime() : 0; }
  107. private:
  108. PannerNode(AudioContext*, float sampleRate);
  109. // Returns the combined distance and cone gain attenuation.
  110. float distanceConeGain();
  111. // Notifies any AudioBufferSourceNodes connected to us either directly or indirectly about our existence.
  112. // This is in order to handle the pitch change necessary for the doppler shift.
  113. void notifyAudioSourcesConnectedToNode(AudioNode*);
  114. OwnPtr<Panner> m_panner;
  115. unsigned m_panningModel;
  116. FloatPoint3D m_position;
  117. FloatPoint3D m_orientation;
  118. FloatPoint3D m_velocity;
  119. // Gain
  120. RefPtr<AudioParam> m_distanceGain;
  121. RefPtr<AudioParam> m_coneGain;
  122. DistanceEffect m_distanceEffect;
  123. ConeEffect m_coneEffect;
  124. float m_lastGain;
  125. unsigned m_connectionCount;
  126. // Synchronize process() and setPanningModel() which can change the panner.
  127. mutable Mutex m_pannerLock;
  128. };
  129. } // namespace WebCore
  130. #endif // PannerNode_h