| 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 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include "AudioScheduledSourceNode.h" |
| 28 | #include <wtf/Lock.h> |
| 29 | #include <wtf/UniqueArray.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | class AudioBuffer; |
| 34 | class PannerNode; |
| 35 | |
| 36 | // AudioBufferSourceNode is an AudioNode representing an audio source from an in-memory audio asset represented by an AudioBuffer. |
| 37 | // It generally will be used for short sounds which require a high degree of scheduling flexibility (can playback in rhythmically perfect ways). |
| 38 | |
| 39 | class AudioBufferSourceNode final : public AudioScheduledSourceNode { |
| 40 | WTF_MAKE_ISO_ALLOCATED(AudioBufferSourceNode); |
| 41 | public: |
| 42 | static Ref<AudioBufferSourceNode> create(AudioContext&, float sampleRate); |
| 43 | |
| 44 | virtual ~AudioBufferSourceNode(); |
| 45 | |
| 46 | // AudioNode |
| 47 | void process(size_t framesToProcess) final; |
| 48 | void reset() final; |
| 49 | |
| 50 | // setBuffer() is called on the main thread. This is the buffer we use for playback. |
| 51 | // returns true on success. |
| 52 | void setBuffer(RefPtr<AudioBuffer>&&); |
| 53 | AudioBuffer* buffer() { return m_buffer.get(); } |
| 54 | |
| 55 | // numberOfChannels() returns the number of output channels. This value equals the number of channels from the buffer. |
| 56 | // If a new buffer is set with a different number of channels, then this value will dynamically change. |
| 57 | unsigned numberOfChannels(); |
| 58 | |
| 59 | // Play-state |
| 60 | ExceptionOr<void> start(double when, double grainOffset, Optional<double> grainDuration); |
| 61 | |
| 62 | // Note: the attribute was originally exposed as .looping, but to be more consistent in naming with <audio> |
| 63 | // and with how it's described in the specification, the proper attribute name is .loop |
| 64 | // The old attribute is kept for backwards compatibility. |
| 65 | bool loop() const { return m_isLooping; } |
| 66 | void setLoop(bool looping) { m_isLooping = looping; } |
| 67 | |
| 68 | // Loop times in seconds. |
| 69 | double loopStart() const { return m_loopStart; } |
| 70 | double loopEnd() const { return m_loopEnd; } |
| 71 | void setLoopStart(double loopStart) { m_loopStart = loopStart; } |
| 72 | void setLoopEnd(double loopEnd) { m_loopEnd = loopEnd; } |
| 73 | |
| 74 | // Deprecated. |
| 75 | bool looping(); |
| 76 | void setLooping(bool); |
| 77 | |
| 78 | AudioParam* gain() { return m_gain.get(); } |
| 79 | AudioParam* playbackRate() { return m_playbackRate.get(); } |
| 80 | |
| 81 | // If a panner node is set, then we can incorporate doppler shift into the playback pitch rate. |
| 82 | void setPannerNode(PannerNode*); |
| 83 | void clearPannerNode(); |
| 84 | |
| 85 | // If we are no longer playing, propogate silence ahead to downstream nodes. |
| 86 | bool propagatesSilence() const final; |
| 87 | |
| 88 | // AudioScheduledSourceNode |
| 89 | void finish() final; |
| 90 | |
| 91 | private: |
| 92 | AudioBufferSourceNode(AudioContext&, float sampleRate); |
| 93 | |
| 94 | double tailTime() const final { return 0; } |
| 95 | double latencyTime() const final { return 0; } |
| 96 | |
| 97 | enum BufferPlaybackMode { |
| 98 | Entire, |
| 99 | Partial |
| 100 | }; |
| 101 | |
| 102 | ExceptionOr<void> startPlaying(BufferPlaybackMode, double when, double grainOffset, double grainDuration); |
| 103 | |
| 104 | // Returns true on success. |
| 105 | bool renderFromBuffer(AudioBus*, unsigned destinationFrameOffset, size_t numberOfFrames); |
| 106 | |
| 107 | // Render silence starting from "index" frame in AudioBus. |
| 108 | inline bool renderSilenceAndFinishIfNotLooping(AudioBus*, unsigned index, size_t framesToProcess); |
| 109 | |
| 110 | // m_buffer holds the sample data which this node outputs. |
| 111 | RefPtr<AudioBuffer> m_buffer; |
| 112 | |
| 113 | // Pointers for the buffer and destination. |
| 114 | UniqueArray<const float*> m_sourceChannels; |
| 115 | UniqueArray<float*> m_destinationChannels; |
| 116 | |
| 117 | // Used for the "gain" and "playbackRate" attributes. |
| 118 | RefPtr<AudioParam> m_gain; |
| 119 | RefPtr<AudioParam> m_playbackRate; |
| 120 | |
| 121 | // If m_isLooping is false, then this node will be done playing and become inactive after it reaches the end of the sample data in the buffer. |
| 122 | // If true, it will wrap around to the start of the buffer each time it reaches the end. |
| 123 | bool m_isLooping; |
| 124 | |
| 125 | double m_loopStart; |
| 126 | double m_loopEnd; |
| 127 | |
| 128 | // m_virtualReadIndex is a sample-frame index into our buffer representing the current playback position. |
| 129 | // Since it's floating-point, it has sub-sample accuracy. |
| 130 | double m_virtualReadIndex; |
| 131 | |
| 132 | // Granular playback |
| 133 | bool m_isGrain; |
| 134 | double m_grainOffset; // in seconds |
| 135 | double m_grainDuration; // in seconds |
| 136 | |
| 137 | // totalPitchRate() returns the instantaneous pitch rate (non-time preserving). |
| 138 | // It incorporates the base pitch rate, any sample-rate conversion factor from the buffer, and any doppler shift from an associated panner node. |
| 139 | double totalPitchRate(); |
| 140 | |
| 141 | // m_lastGain provides continuity when we dynamically adjust the gain. |
| 142 | float m_lastGain; |
| 143 | |
| 144 | // We optionally keep track of a panner node which has a doppler shift that is incorporated into |
| 145 | // the pitch rate. We manually manage ref-counting because we want to use RefTypeConnection. |
| 146 | PannerNode* m_pannerNode; |
| 147 | |
| 148 | // This synchronizes process() with setBuffer() which can cause dynamic channel count changes. |
| 149 | mutable Lock m_processMutex; |
| 150 | }; |
| 151 | |
| 152 | } // namespace WebCore |
| 153 | |