Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / OfflineAudioDestinationNode.cpp
1 /*
2  * Copyright (C) 2011, 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 #include "config.h"
26
27 #if ENABLE(WEB_AUDIO)
28
29 #include "modules/webaudio/OfflineAudioDestinationNode.h"
30
31 #include <algorithm>
32 #include "platform/audio/AudioBus.h"
33 #include "platform/audio/HRTFDatabaseLoader.h"
34 #include "modules/webaudio/AudioContext.h"
35 #include "platform/Task.h"
36 #include "public/platform/Platform.h"
37 #include "wtf/MainThread.h"
38
39 using namespace std;
40
41 namespace WebCore {
42
43 const size_t renderQuantumSize = 128;
44
45 OfflineAudioDestinationNode::OfflineAudioDestinationNode(AudioContext* context, AudioBuffer* renderTarget)
46     : AudioDestinationNode(context, renderTarget->sampleRate())
47     , m_renderTarget(renderTarget)
48     , m_startedRendering(false)
49 {
50     m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuantumSize);
51 }
52
53 OfflineAudioDestinationNode::~OfflineAudioDestinationNode()
54 {
55     uninitialize();
56 }
57
58 void OfflineAudioDestinationNode::initialize()
59 {
60     if (isInitialized())
61         return;
62
63     AudioNode::initialize();
64 }
65
66 void OfflineAudioDestinationNode::uninitialize()
67 {
68     if (!isInitialized())
69         return;
70
71     if (m_renderThread)
72         m_renderThread.clear();
73
74     AudioNode::uninitialize();
75 }
76
77 void OfflineAudioDestinationNode::startRendering()
78 {
79     ASSERT(isMainThread());
80     ASSERT(m_renderTarget.get());
81     if (!m_renderTarget.get())
82         return;
83
84     if (!m_startedRendering) {
85         m_startedRendering = true;
86         ref(); // See corresponding deref() call in notifyCompleteDispatch().
87         m_renderThread = adoptPtr(blink::Platform::current()->createThread("Offline Audio Renderer"));
88         m_renderThread->postTask(new Task(WTF::bind(&OfflineAudioDestinationNode::offlineRender, this)));
89     }
90 }
91
92 void OfflineAudioDestinationNode::offlineRender()
93 {
94     ASSERT(!isMainThread());
95     ASSERT(m_renderBus.get());
96     if (!m_renderBus.get())
97         return;
98
99     bool isAudioContextInitialized = context()->isInitialized();
100     ASSERT(isAudioContextInitialized);
101     if (!isAudioContextInitialized)
102         return;
103
104     bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numberOfChannels();
105     ASSERT(channelsMatch);
106     if (!channelsMatch)
107         return;
108
109     bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize;
110     ASSERT(isRenderBusAllocated);
111     if (!isRenderBusAllocated)
112         return;
113
114     // Break up the render target into smaller "render quantize" sized pieces.
115     // Render until we're finished.
116     size_t framesToProcess = m_renderTarget->length();
117     unsigned numberOfChannels = m_renderTarget->numberOfChannels();
118
119     unsigned n = 0;
120     while (framesToProcess > 0) {
121         // Render one render quantum.
122         render(0, m_renderBus.get(), renderQuantumSize);
123
124         size_t framesAvailableToCopy = min(framesToProcess, renderQuantumSize);
125
126         for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++channelIndex) {
127             const float* source = m_renderBus->channel(channelIndex)->data();
128             float* destination = m_renderTarget->getChannelData(channelIndex)->data();
129             memcpy(destination + n, source, sizeof(float) * framesAvailableToCopy);
130         }
131
132         n += framesAvailableToCopy;
133         framesToProcess -= framesAvailableToCopy;
134     }
135
136     // Our work is done. Let the AudioContext know.
137     callOnMainThread(notifyCompleteDispatch, this);
138 }
139
140 void OfflineAudioDestinationNode::notifyCompleteDispatch(void* userData)
141 {
142     OfflineAudioDestinationNode* destinationNode = static_cast<OfflineAudioDestinationNode*>(userData);
143     ASSERT(destinationNode);
144     if (!destinationNode)
145         return;
146
147     destinationNode->notifyComplete();
148     destinationNode->deref();
149 }
150
151 void OfflineAudioDestinationNode::notifyComplete()
152 {
153     context()->fireCompletionEvent();
154 }
155
156 void OfflineAudioDestinationNode::trace(Visitor* visitor)
157 {
158     visitor->trace(m_renderTarget);
159     AudioDestinationNode::trace(visitor);
160 }
161
162 } // namespace WebCore
163
164 #endif // ENABLE(WEB_AUDIO)