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
29 #include "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32 #include "AudioDestinationChromium.h"
33 #include "AudioFIFO.h"
34 #include "AudioPullFIFO.h"
35 #include <public/Platform.h>
36
37 using namespace WebKit;
38
39 namespace WebCore {
40
41 // Buffer size at which the web audio engine will render.
42 const unsigned renderBufferSize = 128;
43
44 // Size of the FIFO
45 const size_t fifoSize = 8192;
46
47 // Factory method: Chromium-implementation
48 PassOwnPtr<AudioDestination> AudioDestination::create(AudioIOCallback& callback, unsigned numberOfInputChannels, unsigned numberOfOutputChannels, float sampleRate)
49 {
50 return adoptPtr(new AudioDestinationChromium(callback, numberOfInputChannels, numberOfOutputChannels, sampleRate));
51 }
52
53 AudioDestinationChromium::AudioDestinationChromium(AudioIOCallback& callback, unsigned numberOfInputChannels, unsigned numberOfOutputChannels, float sampleRate)
54 : m_callback(callback)
55 , m_numberOfOutputChannels(numberOfOutputChannels)
56 , m_inputBus(numberOfInputChannels, renderBufferSize)
57 , m_renderBus(numberOfOutputChannels, renderBufferSize, false)
58 , m_sampleRate(sampleRate)
59 , m_isPlaying(false)
60 {
61 // Use the optimal buffer size recommended by the audio backend.
62 m_callbackBufferSize = WebKit::Platform::current()->audioHardwareBufferSize();
63
64 // Quick exit if the requested size is too large.
65 ASSERT(m_callbackBufferSize + renderBufferSize <= fifoSize);
66 if (m_callbackBufferSize + renderBufferSize > fifoSize)
67 return;
68
69 // FIXME: switch to new API (with input channels) once chromium supports it.
70 m_audioDevice = adoptPtr(WebKit::Platform::current()->createAudioDevice(m_callbackBufferSize, numberOfOutputChannels, sampleRate, this));
71 ASSERT(m_audioDevice);
72
73 // Create a FIFO to handle the possibility of the callback size
74 // not being a multiple of the render size. If the FIFO already
75 // contains enough data, the data will be provided directly.
76 // Otherwise, the FIFO will call the provider enough times to
77 // satisfy the request for data.
78 m_fifo = adoptPtr(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, renderBufferSize));
79
80 // Input buffering.
81 m_inputFifo = adoptPtr(new AudioFIFO(numberOfInputChannels, fifoSize));
82
83 // If the callback size does not match the render size, then we need to buffer some
84 // extra silence for the input. Otherwise, we can over-consume the input FIFO.
85 if (m_callbackBufferSize != renderBufferSize) {
86 // FIXME: handle multi-channel input and don't hard-code to stereo.
87 AudioBus silence(2, renderBufferSize);
88 m_inputFifo->push(&silence);
89 }
90 }
91
92 AudioDestinationChromium::~AudioDestinationChromium()
93 {
94 stop();
95 }
96
97 void AudioDestinationChromium::start()
98 {
99 if (!m_isPlaying && m_audioDevice) {
100 m_audioDevice->start();
101 m_isPlaying = true;
102 }
103 }
104
105 void AudioDestinationChromium::stop()
106 {
107 if (m_isPlaying && m_audioDevice) {
108 m_audioDevice->stop();
109 m_isPlaying = false;
110 }
111 }
112
113 float AudioDestination::hardwareSampleRate()
114 {
115 return static_cast<float>(WebKit::Platform::current()->audioHardwareSampleRate());
116 }
117
118 void AudioDestinationChromium::render(const WebVector<float*>& sourceData, const WebVector<float*>& audioData, size_t numberOfFrames)
119 {
120 bool isNumberOfChannelsGood = audioData.size() == m_numberOfOutputChannels;
121 if (!isNumberOfChannelsGood) {
122 ASSERT_NOT_REACHED();
123 return;
124 }
125
126 bool isBufferSizeGood = numberOfFrames == m_callbackBufferSize;
127 if (!isBufferSizeGood) {
128 ASSERT_NOT_REACHED();
129 return;
130 }
131
132 // Buffer optional live input.
133 if (sourceData.size() >= 2) {
134 // FIXME: handle multi-channel input and don't hard-code to stereo.
135 AudioBus wrapperBus(2, numberOfFrames, false);
136 wrapperBus.setChannelMemory(0, sourceData[0], numberOfFrames);
137 wrapperBus.setChannelMemory(1, sourceData[1], numberOfFrames);
138 m_inputFifo->push(&wrapperBus);
139 }
140
141 m_renderBus.setChannelMemory(0, audioData[0], numberOfFrames);
142 m_renderBus.setChannelMemory(1, audioData[1], numberOfFrames);
143
144 m_fifo->consume(&m_renderBus, numberOfFrames);
145 }
146
147 void AudioDestinationChromium::provideInput(AudioBus* bus, size_t framesToProcess)
148 {
149 AudioBus* sourceBus = 0;
150 if (m_inputFifo->framesInFifo() >= framesToProcess) {
151 m_inputFifo->consume(&m_inputBus, framesToProcess);
152 sourceBus = &m_inputBus;
153 }
154
155 m_callback.render(sourceBus, bus, framesToProcess);
156 }
157
158 } // namespace WebCore
159
160 #endif // ENABLE(WEB_AUDIO)