2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef AudioDestinationChromium_h
30 #define AudioDestinationChromium_h
33 #include "AudioDestination.h"
34 #include "AudioSourceProvider.h"
35 #include "WebAudioDevice.h"
36 #include "WebVector.h"
38 namespace WebKit { class WebAudioDevice; }
42 // An AudioDestination using Chromium's audio system
44 class AudioDestinationChromium : public AudioDestination, public WebKit::WebAudioDevice::RenderCallback {
46 AudioDestinationChromium(AudioSourceProvider&, float sampleRate);
47 virtual ~AudioDestinationChromium();
51 bool isPlaying() { return m_isPlaying; }
53 float sampleRate() const { return m_sampleRate; }
55 // WebKit::WebAudioDevice::RenderCallback
56 virtual void render(const WebKit::WebVector<float*>& audioData, size_t numberOfFrames);
59 // A FIFO (First In First Out) buffer to handle mismatches in the
60 // audio backend hardware buffer size and the Web Audio render size.
63 // Create a FIFO that gets data from |provider|. The FIFO will
64 // be large enough to hold |fifoLength| frames of data of
65 // |numberOfChannels| channels. The AudioSourceProvider will
66 // be asked to produce |providerSize| frames when the FIFO
68 FIFO(AudioSourceProvider& provider, unsigned numberOfChannels, size_t fifoLength, size_t providerSize);
70 // Read |framesToConsume| frames from the FIFO into the
71 // destination. If the FIFO does not have enough data, we ask
72 // the |provider| to get more data to fulfill the request.
73 void consume(AudioBus* destination, size_t framesToConsume);
76 // Update the FIFO index by the step, with appropriate
77 // wrapping around the endpoint.
78 int updateIndex(int index, int step) { return (index + step) % m_fifoLength; }
80 void findWrapLengths(size_t index, size_t providerSize, size_t& part1Length, size_t& part2Length);
82 // Fill the FIFO buffer with at least |numberOfFrames| more data.
83 void fillBuffer(size_t numberOfFrames);
85 // The provider of the data in our FIFO.
86 AudioSourceProvider& m_provider;
88 // The FIFO itself. In reality, the FIFO is a circular buffer.
89 AudioBus m_fifoAudioBus;
91 // The total available space in the FIFO.
94 // The number of actual elements in the FIFO
95 size_t m_framesInFifo;
97 // Where to start reading from the FIFO.
100 // Where to start writing to the FIFO.
103 // Number of frames of data that the provider will produce per call.
104 unsigned int m_providerSize;
106 // Temporary workspace to hold the data from the provider.
110 AudioSourceProvider& m_provider;
111 AudioBus m_renderBus;
114 OwnPtr<WebKit::WebAudioDevice> m_audioDevice;
115 size_t m_callbackBufferSize;
119 } // namespace WebCore
121 #endif // AudioDestinationChromium_h