AudioDestinationNode.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 AudioDestinationNode_h
  25. #define AudioDestinationNode_h
  26. #include "AudioBuffer.h"
  27. #include "AudioBus.h"
  28. #include "AudioIOCallback.h"
  29. #include "AudioNode.h"
  30. #include "AudioSourceProvider.h"
  31. namespace WebCore {
  32. class AudioBus;
  33. class AudioContext;
  34. class AudioDestinationNode : public AudioNode, public AudioIOCallback {
  35. public:
  36. AudioDestinationNode(AudioContext*, float sampleRate);
  37. virtual ~AudioDestinationNode();
  38. // AudioNode
  39. virtual void process(size_t) { }; // we're pulled by hardware so this is never called
  40. virtual void reset() { m_currentSampleFrame = 0; };
  41. // The audio hardware calls render() to get the next render quantum of audio into destinationBus.
  42. // It will optionally give us local/live audio input in sourceBus (if it's not 0).
  43. virtual void render(AudioBus* sourceBus, AudioBus* destinationBus, size_t numberOfFrames);
  44. size_t currentSampleFrame() const { return m_currentSampleFrame; }
  45. double currentTime() const { return currentSampleFrame() / static_cast<double>(sampleRate()); }
  46. virtual unsigned long maxChannelCount() const { return 0; }
  47. // Enable local/live input for the specified device.
  48. virtual void enableInput(const String& inputDeviceId) = 0;
  49. virtual void startRendering() = 0;
  50. AudioSourceProvider* localAudioInputProvider() { return &m_localAudioInputProvider; }
  51. protected:
  52. // LocalAudioInputProvider allows us to expose an AudioSourceProvider for local/live audio input.
  53. // If there is local/live audio input, we call set() with the audio input data every render quantum.
  54. class LocalAudioInputProvider : public AudioSourceProvider {
  55. public:
  56. LocalAudioInputProvider()
  57. : m_sourceBus(AudioBus::create(2, AudioNode::ProcessingSizeInFrames)) // FIXME: handle non-stereo local input.
  58. {
  59. }
  60. void set(AudioBus* bus)
  61. {
  62. if (bus)
  63. m_sourceBus->copyFrom(*bus);
  64. }
  65. // AudioSourceProvider.
  66. virtual void provideInput(AudioBus* destinationBus, size_t numberOfFrames)
  67. {
  68. bool isGood = destinationBus && destinationBus->length() == numberOfFrames && m_sourceBus->length() == numberOfFrames;
  69. ASSERT(isGood);
  70. if (isGood)
  71. destinationBus->copyFrom(*m_sourceBus);
  72. }
  73. private:
  74. RefPtr<AudioBus> m_sourceBus;
  75. };
  76. virtual double tailTime() const OVERRIDE { return 0; }
  77. virtual double latencyTime() const OVERRIDE { return 0; }
  78. // Counts the number of sample-frames processed by the destination.
  79. size_t m_currentSampleFrame;
  80. LocalAudioInputProvider m_localAudioInputProvider;
  81. };
  82. } // namespace WebCore
  83. #endif // AudioDestinationNode_h